1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
lefcha |
1.57 |
#include <string.h> |
3 |
|
|
#include <errno.h> |
4 |
lefcha |
1.28 |
#include <unistd.h> |
5 |
lefcha |
1.5 |
#include <sys/types.h> |
6 |
lefcha |
1.10 |
#include <sys/stat.h> |
7 |
lefcha |
1.28 |
#include <fcntl.h> |
8 |
lefcha |
1.1 |
|
9 |
|
|
#include "config.h" |
10 |
|
|
#include "imapfilter.h" |
11 |
lefcha |
1.57 |
#include "pathnames.h" |
12 |
lefcha |
1.50 |
|
13 |
lefcha |
1.1 |
|
14 |
lefcha |
1.29 |
extern char *home; |
15 |
|
|
|
16 |
|
|
|
17 |
|
|
/* |
18 |
lefcha |
1.57 |
* Create imapfilter's home directory. |
19 |
lefcha |
1.31 |
*/ |
20 |
lefcha |
1.48 |
int |
21 |
|
|
create_homedir(void) |
22 |
lefcha |
1.31 |
{ |
23 |
lefcha |
1.48 |
if (home != NULL) |
24 |
|
|
if (chdir(home)) |
25 |
|
|
error("could not change directory; %s\n", |
26 |
|
|
strerror(errno)); |
27 |
|
|
|
28 |
lefcha |
1.57 |
if (!exists_dir(PATHNAME_HOME_DIR)) { |
29 |
|
|
if (mkdir(PATHNAME_HOME_DIR, S_IRUSR | S_IWUSR | S_IXUSR)) |
30 |
|
|
error("could not create directory %s; %s\n", |
31 |
|
|
PATHNAME_HOME_DIR, strerror(errno)); |
32 |
lefcha |
1.49 |
} |
33 |
|
|
#ifdef CHECK_PERMISSIONS |
34 |
|
|
else { |
35 |
lefcha |
1.57 |
check_dir_perms(PATHNAME_HOME_DIR, S_IRUSR | S_IWUSR | S_IXUSR); |
36 |
lefcha |
1.49 |
} |
37 |
|
|
#endif |
38 |
lefcha |
1.36 |
|
39 |
lefcha |
1.48 |
return 0; |
40 |
lefcha |
1.31 |
} |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
/* |
44 |
|
|
* Check if a file exists. |
45 |
|
|
*/ |
46 |
lefcha |
1.48 |
int |
47 |
|
|
exists_file(char *fname) |
48 |
lefcha |
1.31 |
{ |
49 |
lefcha |
1.48 |
struct stat fs; |
50 |
lefcha |
1.38 |
|
51 |
lefcha |
1.48 |
if (access(fname, F_OK)) |
52 |
|
|
return 0; |
53 |
lefcha |
1.38 |
|
54 |
lefcha |
1.48 |
stat(fname, &fs); |
55 |
|
|
if (!S_ISREG(fs.st_mode)) { |
56 |
|
|
error("file %s not a regular file\n", fname); |
57 |
|
|
return ERROR_FILE_OPEN; |
58 |
|
|
} |
59 |
|
|
return 1; |
60 |
lefcha |
1.31 |
} |
61 |
|
|
|
62 |
|
|
|
63 |
lefcha |
1.38 |
/* |
64 |
lefcha |
1.31 |
* Check if a directory exists. |
65 |
|
|
*/ |
66 |
lefcha |
1.48 |
int |
67 |
|
|
exists_dir(char *dname) |
68 |
lefcha |
1.31 |
{ |
69 |
lefcha |
1.48 |
struct stat ds; |
70 |
lefcha |
1.38 |
|
71 |
lefcha |
1.48 |
if (access(dname, F_OK)) |
72 |
|
|
return 0; |
73 |
lefcha |
1.38 |
|
74 |
lefcha |
1.48 |
stat(dname, &ds); |
75 |
|
|
if (!S_ISDIR(ds.st_mode)) { |
76 |
|
|
error("file %s not a directory\n", dname); |
77 |
|
|
return ERROR_FILE_OPEN; |
78 |
|
|
} |
79 |
|
|
return 1; |
80 |
lefcha |
1.31 |
} |
81 |
|
|
|
82 |
|
|
|
83 |
|
|
/* |
84 |
lefcha |
1.36 |
* Create a file with the specified permissions. |
85 |
lefcha |
1.31 |
*/ |
86 |
lefcha |
1.48 |
int |
87 |
|
|
create_file(char *fname, mode_t mode) |
88 |
lefcha |
1.31 |
{ |
89 |
lefcha |
1.48 |
int fd; |
90 |
|
|
|
91 |
|
|
fd = 0; |
92 |
lefcha |
1.38 |
|
93 |
lefcha |
1.48 |
if (!exists_file(fname)) { |
94 |
|
|
fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode); |
95 |
|
|
if (fd == -1) { |
96 |
|
|
error("could not create file %s; %s\n", fname, |
97 |
|
|
strerror(errno)); |
98 |
|
|
return ERROR_FILE_OPEN; |
99 |
|
|
} |
100 |
|
|
close(fd); |
101 |
|
|
} |
102 |
|
|
return 0; |
103 |
lefcha |
1.31 |
} |
104 |
|
|
|
105 |
|
|
|
106 |
|
|
#ifdef CHECK_PERMISSIONS |
107 |
|
|
/* |
108 |
|
|
* Check the permissions of a file. |
109 |
|
|
*/ |
110 |
lefcha |
1.48 |
int |
111 |
|
|
check_file_perms(char *fname, mode_t mode) |
112 |
lefcha |
1.31 |
{ |
113 |
lefcha |
1.48 |
struct stat fs; |
114 |
lefcha |
1.38 |
|
115 |
lefcha |
1.48 |
if (stat(fname, &fs)) { |
116 |
|
|
error("getting file %s status; %s\n", fname, |
117 |
|
|
strerror(errno)); |
118 |
|
|
return ERROR_TRIVIAL; |
119 |
|
|
} |
120 |
|
|
if (!S_ISREG(fs.st_mode)) { |
121 |
|
|
error("file %s not a regular file\n", fname); |
122 |
|
|
return ERROR_TRIVIAL; |
123 |
|
|
} |
124 |
|
|
if ((fs.st_mode & 00777) != mode) { |
125 |
|
|
error("warning: improper file %s permissions\n", fname); |
126 |
|
|
error("warning: file's mode should be %o not %o\n", mode, |
127 |
|
|
fs.st_mode & 00777); |
128 |
|
|
return ERROR_TRIVIAL; |
129 |
|
|
} |
130 |
|
|
return 0; |
131 |
lefcha |
1.31 |
} |
132 |
|
|
|
133 |
|
|
|
134 |
|
|
/* |
135 |
|
|
* Check the permissions of a directory. |
136 |
|
|
*/ |
137 |
lefcha |
1.48 |
int |
138 |
|
|
check_dir_perms(char *dname, mode_t mode) |
139 |
lefcha |
1.31 |
{ |
140 |
lefcha |
1.48 |
struct stat ds; |
141 |
lefcha |
1.31 |
|
142 |
lefcha |
1.48 |
if (stat(dname, &ds)) { |
143 |
|
|
error("getting file %s status; %s\n", dname, |
144 |
|
|
strerror(errno)); |
145 |
|
|
return ERROR_TRIVIAL; |
146 |
|
|
} |
147 |
|
|
if (!S_ISDIR(ds.st_mode)) { |
148 |
|
|
error("file %s not a directory\n", dname); |
149 |
|
|
return ERROR_TRIVIAL; |
150 |
|
|
} |
151 |
|
|
if ((ds.st_mode & 00777) != mode) { |
152 |
|
|
error("warning: improper dir %s permissions\n", dname); |
153 |
lefcha |
1.50 |
error("warning: dir's mode should be %o not %o\n", mode, |
154 |
lefcha |
1.48 |
ds.st_mode & 00777); |
155 |
|
|
return ERROR_TRIVIAL; |
156 |
|
|
} |
157 |
|
|
return 0; |
158 |
lefcha |
1.31 |
} |
159 |
lefcha |
1.47 |
#endif /* CHECK_PERMISSIONS */ |