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