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.49 |
} |
34 |
lefcha |
1.48 |
return 0; |
35 |
lefcha |
1.31 |
} |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
/* |
39 |
|
|
* Check if a file exists. |
40 |
|
|
*/ |
41 |
lefcha |
1.48 |
int |
42 |
|
|
exists_file(char *fname) |
43 |
lefcha |
1.31 |
{ |
44 |
lefcha |
1.48 |
struct stat fs; |
45 |
lefcha |
1.38 |
|
46 |
lefcha |
1.48 |
if (access(fname, F_OK)) |
47 |
|
|
return 0; |
48 |
lefcha |
1.38 |
|
49 |
lefcha |
1.48 |
stat(fname, &fs); |
50 |
|
|
if (!S_ISREG(fs.st_mode)) { |
51 |
|
|
error("file %s not a regular file\n", fname); |
52 |
|
|
return ERROR_FILE_OPEN; |
53 |
|
|
} |
54 |
|
|
return 1; |
55 |
lefcha |
1.31 |
} |
56 |
|
|
|
57 |
|
|
|
58 |
lefcha |
1.38 |
/* |
59 |
lefcha |
1.31 |
* Check if a directory exists. |
60 |
|
|
*/ |
61 |
lefcha |
1.48 |
int |
62 |
|
|
exists_dir(char *dname) |
63 |
lefcha |
1.31 |
{ |
64 |
lefcha |
1.48 |
struct stat ds; |
65 |
lefcha |
1.38 |
|
66 |
lefcha |
1.48 |
if (access(dname, F_OK)) |
67 |
|
|
return 0; |
68 |
lefcha |
1.38 |
|
69 |
lefcha |
1.48 |
stat(dname, &ds); |
70 |
|
|
if (!S_ISDIR(ds.st_mode)) { |
71 |
|
|
error("file %s not a directory\n", dname); |
72 |
|
|
return ERROR_FILE_OPEN; |
73 |
|
|
} |
74 |
|
|
return 1; |
75 |
lefcha |
1.31 |
} |
76 |
|
|
|
77 |
|
|
|
78 |
|
|
/* |
79 |
lefcha |
1.36 |
* Create a file with the specified permissions. |
80 |
lefcha |
1.31 |
*/ |
81 |
lefcha |
1.48 |
int |
82 |
|
|
create_file(char *fname, mode_t mode) |
83 |
lefcha |
1.31 |
{ |
84 |
lefcha |
1.48 |
int fd; |
85 |
|
|
|
86 |
|
|
fd = 0; |
87 |
lefcha |
1.38 |
|
88 |
lefcha |
1.48 |
if (!exists_file(fname)) { |
89 |
|
|
fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode); |
90 |
|
|
if (fd == -1) { |
91 |
|
|
error("could not create file %s; %s\n", fname, |
92 |
|
|
strerror(errno)); |
93 |
|
|
return ERROR_FILE_OPEN; |
94 |
|
|
} |
95 |
|
|
close(fd); |
96 |
|
|
} |
97 |
|
|
return 0; |
98 |
lefcha |
1.31 |
} |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
/* |
102 |
|
|
* Check the permissions of a file. |
103 |
|
|
*/ |
104 |
lefcha |
1.48 |
int |
105 |
|
|
check_file_perms(char *fname, mode_t mode) |
106 |
lefcha |
1.31 |
{ |
107 |
lefcha |
1.48 |
struct stat fs; |
108 |
lefcha |
1.38 |
|
109 |
lefcha |
1.59 |
if (!(options & OPTION_PERMISSIONS)) |
110 |
|
|
return 0; |
111 |
|
|
|
112 |
lefcha |
1.48 |
if (stat(fname, &fs)) { |
113 |
|
|
error("getting file %s status; %s\n", fname, |
114 |
|
|
strerror(errno)); |
115 |
|
|
return ERROR_TRIVIAL; |
116 |
|
|
} |
117 |
|
|
if (!S_ISREG(fs.st_mode)) { |
118 |
|
|
error("file %s not a regular file\n", fname); |
119 |
|
|
return ERROR_TRIVIAL; |
120 |
|
|
} |
121 |
|
|
if ((fs.st_mode & 00777) != mode) { |
122 |
lefcha |
1.59 |
error("warning: file's %s mode should be %o not %o\n", fname, |
123 |
|
|
mode, fs.st_mode & 00777); |
124 |
lefcha |
1.48 |
return ERROR_TRIVIAL; |
125 |
|
|
} |
126 |
|
|
return 0; |
127 |
lefcha |
1.31 |
} |
128 |
|
|
|
129 |
|
|
|
130 |
|
|
/* |
131 |
|
|
* Check the permissions of a directory. |
132 |
lefcha |
1.59 |
* |
133 |
lefcha |
1.48 |
int |
134 |
|
|
check_dir_perms(char *dname, mode_t mode) |
135 |
lefcha |
1.31 |
{ |
136 |
lefcha |
1.48 |
struct stat ds; |
137 |
lefcha |
1.31 |
|
138 |
lefcha |
1.59 |
if (!(options & OPTION_PERMISSIONS)) |
139 |
|
|
return 0; |
140 |
|
|
|
141 |
lefcha |
1.48 |
if (stat(dname, &ds)) { |
142 |
|
|
error("getting file %s status; %s\n", dname, |
143 |
|
|
strerror(errno)); |
144 |
|
|
return ERROR_TRIVIAL; |
145 |
|
|
} |
146 |
|
|
if (!S_ISDIR(ds.st_mode)) { |
147 |
|
|
error("file %s not a directory\n", dname); |
148 |
|
|
return ERROR_TRIVIAL; |
149 |
|
|
} |
150 |
|
|
if ((ds.st_mode & 00777) != mode) { |
151 |
lefcha |
1.59 |
error("warning: dir's %s mode should be %o not %o\n", dname, |
152 |
|
|
mode, ds.st_mode & 00777); |
153 |
lefcha |
1.48 |
return ERROR_TRIVIAL; |
154 |
|
|
} |
155 |
|
|
return 0; |
156 |
lefcha |
1.59 |
}*/ |