1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <sys/types.h> |
4 |
#include <unistd.h> |
5 |
#include <string.h> |
6 |
#include <errno.h> |
7 |
#include <limits.h> |
8 |
#include <fcntl.h> |
9 |
#include <setjmp.h> |
10 |
#include <locale.h> |
11 |
|
12 |
#include "config.h" |
13 |
#include "imapfilter.h" |
14 |
#include "version.h" |
15 |
#include "account.h" |
16 |
#include "filter.h" |
17 |
#include "buffer.h" |
18 |
|
19 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5 |
20 |
#include <openssl/opensslv.h> |
21 |
#include <openssl/crypto.h> |
22 |
#endif |
23 |
|
24 |
|
25 |
extern account_t *accounts; |
26 |
extern filter_t *filters; |
27 |
extern buffer_t ibuf, obuf; |
28 |
|
29 |
unsigned int options; /* Program options. */ |
30 |
unsigned int flags = 0; /* Program flags. */ |
31 |
unsigned int interval = 0; /* Poll at the specified interval. */ |
32 |
conn_t connpri, connaux; /* Primary and auxiliary IMAP connection. */ |
33 |
|
34 |
char logfile[PATH_MAX]; /* Log file. */ |
35 |
char *home = NULL; /* User's home directory. */ |
36 |
char charset[CHARSET_LEN]; /* Charset for IMAP SEARCH requests. */ |
37 |
|
38 |
jmp_buf acctloop; /* Non-local exit in case of network error. */ |
39 |
|
40 |
|
41 |
void usage(void); |
42 |
void version(void); |
43 |
|
44 |
|
45 |
/* |
46 |
* IMAPFilter: an IMAP mail filtering utility. |
47 |
*/ |
48 |
int |
49 |
main(int argc, char *argv[]) |
50 |
{ |
51 |
int c, r; |
52 |
char *conffile; /* Configuration file. */ |
53 |
account_t *ca; /* Current account. */ |
54 |
mbox_t *cm; /* Current mailbox. */ |
55 |
|
56 |
setlocale(LC_ALL, ""); |
57 |
|
58 |
home = getenv("HOME"); |
59 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_PEEK | |
60 |
OPTION_PERMISSIONS); |
61 |
*charset = 0; |
62 |
*logfile = 0; |
63 |
conffile = NULL; |
64 |
connpri.sock = connaux.sock = -1; |
65 |
|
66 |
while ((c = getopt(argc, argv, "c:d:Dhkl:" |
67 |
#ifdef ENCRYPTED_PASSWORDS |
68 |
"p" |
69 |
#endif |
70 |
"qvVw")) != -1) { |
71 |
switch (c) { |
72 |
case 'c': |
73 |
conffile = optarg; |
74 |
break; |
75 |
case 'd': |
76 |
options |= OPTION_DAEMON_MODE; |
77 |
errno = 0; |
78 |
interval = strtoul(optarg, NULL, 10); |
79 |
if (errno) |
80 |
interval = 0; |
81 |
break; |
82 |
case 'D': |
83 |
options |= OPTION_DEBUG; |
84 |
break; |
85 |
case 'h': |
86 |
usage(); |
87 |
exit(ERROR_UNDEFINED); |
88 |
break; |
89 |
case 'k': |
90 |
kill_imapfilter(); |
91 |
break; |
92 |
case 'l': |
93 |
strncat(logfile, optarg, PATH_MAX - 1); |
94 |
break; |
95 |
#ifdef ENCRYPTED_PASSWORDS |
96 |
case 'p': |
97 |
options |= OPTION_PASSWORD_EDITOR; |
98 |
break; |
99 |
#endif |
100 |
case 'q': |
101 |
options &= ~(OPTION_DETAILS_QUIET | |
102 |
OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE); |
103 |
options |= OPTION_DETAILS_QUIET; |
104 |
break; |
105 |
case 'v': |
106 |
options &= ~(OPTION_DETAILS_QUIET | |
107 |
OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE); |
108 |
options |= OPTION_DETAILS_VERBOSE; |
109 |
break; |
110 |
case 'V': |
111 |
version(); |
112 |
exit(ERROR_UNDEFINED); |
113 |
break; |
114 |
case 'w': |
115 |
options &= ~(OPTION_PERMISSIONS); |
116 |
break; |
117 |
default: |
118 |
usage(); |
119 |
exit(ERROR_UNDEFINED); |
120 |
break; |
121 |
} |
122 |
} |
123 |
|
124 |
debug_start(); |
125 |
|
126 |
create_homedir(); |
127 |
|
128 |
lockfile_check(); |
129 |
lockfile_create(); |
130 |
|
131 |
if (!(options & OPTION_DEBUG)) |
132 |
corefile_disable(); |
133 |
|
134 |
tty_store(); |
135 |
catch_signals(); |
136 |
|
137 |
read_config(conffile); |
138 |
|
139 |
#ifdef ENCRYPTED_PASSWORDS |
140 |
read_passwords(); |
141 |
|
142 |
if ((options & OPTION_PASSWORD_EDITOR)) { |
143 |
password_editor(); |
144 |
|
145 |
secmem_clear(); |
146 |
lockfile_remove(); |
147 |
|
148 |
exit(0); |
149 |
} |
150 |
#endif |
151 |
|
152 |
log_start(); |
153 |
|
154 |
init_buffer(&ibuf); |
155 |
init_buffer(&obuf); |
156 |
|
157 |
if (options & OPTION_DAEMON_MODE) { |
158 |
options &= ~(OPTION_DETAILS_QUIET | |
159 |
OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE); |
160 |
options |= OPTION_DETAILS_QUIET; |
161 |
} |
162 |
do { |
163 |
for (ca = accounts; ca != NULL; ca = ca->next) { |
164 |
|
165 |
if (setjmp(acctloop)) |
166 |
continue; |
167 |
|
168 |
if (init_connection(&connpri, ca->server, ca->port, |
169 |
ca->ssl)) |
170 |
continue; |
171 |
|
172 |
r = greeting_response(&connpri); |
173 |
|
174 |
if (options & OPTION_DEBUG) |
175 |
test(&connpri); |
176 |
|
177 |
if (check_capabilities(&connpri)) |
178 |
continue; |
179 |
|
180 |
#ifdef SSL_TLS |
181 |
if (ca->ssl == SSL_DISABLED && |
182 |
connpri.caps & CAPABILITY_STARTTLS) |
183 |
if (negotiate_tls(&connpri) == RESPONSE_OK) |
184 |
check_capabilities(&connpri); |
185 |
#endif |
186 |
|
187 |
log_info(LOG_ACCOUNT, ca->key); |
188 |
|
189 |
if (r != RESPONSE_PREAUTH) { |
190 |
if (ca->passwdattr == PASSWORD_NONE) { |
191 |
printf("Enter password for %s@%s: ", |
192 |
ca->username, ca->server); |
193 |
get_password(ca->password, PASSWORD_LEN); |
194 |
ca->passwdattr = PASSWORD_PLAIN; |
195 |
} |
196 |
#ifdef CRAM_MD5 |
197 |
if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5) |
198 |
r = auth_cram_md5(&connpri, |
199 |
ca->username, ca->password); |
200 |
else |
201 |
#endif |
202 |
r = login(&connpri, ca->username, |
203 |
ca->password); |
204 |
|
205 |
if (r == RESPONSE_NO) { |
206 |
error("username %s or password rejected " |
207 |
"at %s\n", ca->username, ca->server); |
208 |
continue; |
209 |
} |
210 |
} |
211 |
check_namespace(&connpri); |
212 |
|
213 |
for (cm = ca->mboxes; cm != NULL; cm = cm->next) |
214 |
if (*cm->filters == NULL) |
215 |
mailbox_status(&connpri, cm->name); |
216 |
else if (!select_mailbox(&connpri, cm->name)) { |
217 |
apply_filters(cm->name, cm->filters); |
218 |
close_mailbox(&connpri); |
219 |
} |
220 |
logout(&connpri); |
221 |
|
222 |
close_connection(&connpri); |
223 |
} |
224 |
|
225 |
/* Fork if in daemon mode. */ |
226 |
if (options & OPTION_DAEMON_MODE && |
227 |
!(flags & FLAG_DAEMON_MODE)) { |
228 |
|
229 |
switch (fork()) { |
230 |
case -1: |
231 |
fatal(ERROR_FORK, "forking; %s\n", |
232 |
strerror(errno)); |
233 |
break; |
234 |
case 0: |
235 |
break; |
236 |
default: |
237 |
log_stop(); |
238 |
secmem_clear(); |
239 |
debug_stop(); |
240 |
exit(0); |
241 |
break; |
242 |
} |
243 |
|
244 |
if (setsid() == -1) |
245 |
fatal(ERROR_FORK, "creating session; %s\n", |
246 |
strerror(errno)); |
247 |
|
248 |
switch (fork()) { |
249 |
case -1: |
250 |
fatal(ERROR_FORK, "forking; %s\n", |
251 |
strerror(errno)); |
252 |
break; |
253 |
case 0: |
254 |
break; |
255 |
default: |
256 |
log_stop(); |
257 |
secmem_clear(); |
258 |
debug_stop(); |
259 |
exit(0); |
260 |
break; |
261 |
} |
262 |
|
263 |
close(STDIN_FILENO); |
264 |
close(STDOUT_FILENO); |
265 |
close(STDERR_FILENO); |
266 |
if (open("/dev/null", O_RDWR) != -1) { |
267 |
dup(STDIN_FILENO); |
268 |
dup(STDIN_FILENO); |
269 |
} |
270 |
lockfile_create(); |
271 |
corefile_disable(); |
272 |
|
273 |
flags |= FLAG_DAEMON_MODE; |
274 |
} |
275 |
if (options & OPTION_DAEMON_MODE && |
276 |
flags & FLAG_SIGUSR1_RECEIVED) { |
277 |
reread_config(conffile); |
278 |
continue; |
279 |
} |
280 |
if (interval) |
281 |
sleep(interval); |
282 |
} while (options & OPTION_DAEMON_MODE && interval); |
283 |
|
284 |
log_stop(); |
285 |
secmem_clear(); |
286 |
|
287 |
lockfile_remove(); |
288 |
|
289 |
debug_stop(); |
290 |
|
291 |
exit(0); |
292 |
} |
293 |
|
294 |
|
295 |
/* |
296 |
* Print a very brief usage message. |
297 |
*/ |
298 |
void |
299 |
usage(void) |
300 |
{ |
301 |
fprintf(stderr, |
302 |
"usage: imapfilter [-bDhk" |
303 |
#ifdef ENCRYPTED_PASSWORDS |
304 |
"p" |
305 |
#endif |
306 |
"qvVw] [-c configfile] [-d interval] [-l logfile]\n"); |
307 |
} |
308 |
|
309 |
|
310 |
/* |
311 |
* Print program's version, and if it is built in, OpenSSL's version number. |
312 |
*/ |
313 |
void |
314 |
version(void) |
315 |
{ |
316 |
fprintf(stderr, "IMAPFilter %s" |
317 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5 |
318 |
", OpenSSL 0x%8.8lx" |
319 |
#endif |
320 |
"\n", IMAPFILTER_VERSION |
321 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5 |
322 |
,SSLeay() |
323 |
#endif |
324 |
); |
325 |
} |