36 |
jmp_buf acctloop; /* Non-local exit in case of network error. */ |
jmp_buf acctloop; /* Non-local exit in case of network error. */ |
37 |
|
|
38 |
|
|
39 |
|
void imapfilter(void); |
40 |
|
void daemonize(void); |
41 |
void usage(void); |
void usage(void); |
42 |
void version(void); |
void version(void); |
43 |
|
|
48 |
int |
int |
49 |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
50 |
{ |
{ |
51 |
int c, r; |
int c; |
52 |
char *cf; /* Configuration file. */ |
char *cf; |
|
account_t *ca; /* Current account. */ |
|
|
mbox_t *cm; /* Current mailbox. */ |
|
53 |
|
|
54 |
setlocale(LC_ALL, ""); |
setlocale(LC_ALL, ""); |
55 |
|
|
156 |
opts.verbosity = -2; |
opts.verbosity = -2; |
157 |
|
|
158 |
do { |
do { |
159 |
for (ca = accounts; ca != NULL; ca = ca->next) { |
imapfilter(); |
160 |
|
|
161 |
if (setjmp(acctloop)) |
if (opts.daemon && !(flags & FLAG_DAEMON)) |
162 |
continue; |
daemonize(); |
163 |
|
if (opts.daemon && flags & FLAG_SIGUSR1) { |
164 |
|
reread_config(cf); |
165 |
|
continue; |
166 |
|
} |
167 |
|
if (opts.daemon > 0) |
168 |
|
sleep(opts.daemon); |
169 |
|
} while (opts.daemon); |
170 |
|
|
171 |
if (init_connection(&connpri, ca->server, ca->port, |
log_stop(); |
172 |
ca->ssl)) |
secmem_clear(); |
173 |
continue; |
|
174 |
|
lockfile_remove(); |
175 |
|
|
176 |
r = response_greeting(&connpri); |
debug_stop(); |
177 |
|
|
178 |
if (opts.debug) |
exit(0); |
179 |
test(&connpri); |
} |
180 |
|
|
181 |
if (check_capabilities(&connpri)) |
|
182 |
continue; |
/* |
183 |
|
* Go through all accounts and apply filters to mailboxes as defined. |
184 |
|
*/ |
185 |
|
void |
186 |
|
imapfilter(void) |
187 |
|
{ |
188 |
|
int r; |
189 |
|
account_t *ca; |
190 |
|
mbox_t *cm; |
191 |
|
|
192 |
|
for (ca = accounts; ca != NULL; ca = ca->next) { |
193 |
|
|
194 |
|
if (setjmp(acctloop)) |
195 |
|
continue; |
196 |
|
|
197 |
|
if (init_connection(&connpri, ca->server, ca->port, |
198 |
|
ca->ssl)) |
199 |
|
continue; |
200 |
|
|
201 |
|
r = response_greeting(&connpri); |
202 |
|
|
203 |
|
if (opts.debug) |
204 |
|
test(&connpri); |
205 |
|
|
206 |
|
if (check_capabilities(&connpri)) |
207 |
|
continue; |
208 |
|
|
209 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
210 |
if (ca->ssl == SSL_DISABLED && |
if (ca->ssl == SSL_DISABLED && |
211 |
connpri.caps & CAPS_STARTTLS) |
connpri.caps & CAPS_STARTTLS) |
212 |
if (negotiate_tls(&connpri) == RESPONSE_OK) |
if (negotiate_tls(&connpri) == RESPONSE_OK) |
213 |
check_capabilities(&connpri); |
check_capabilities(&connpri); |
214 |
#endif |
#endif |
215 |
|
|
216 |
log_info(LOG_ACCOUNT, ca->key); |
log_info(LOG_ACCOUNT, ca->key); |
217 |
|
|
218 |
if (r != RESPONSE_PREAUTH) { |
if (r != RESPONSE_PREAUTH) { |
219 |
if (ca->pass_attr == PASS_ATTR_NONE) { |
if (ca->pass_attr == PASS_ATTR_NONE) { |
220 |
printf("Enter password for %s@%s: ", |
printf("Enter password for %s@%s: ", |
221 |
ca->user, ca->server); |
ca->user, ca->server); |
222 |
get_password(ca->pass, PASS_LEN); |
get_password(ca->pass, PASS_LEN); |
223 |
ca->pass_attr = PASS_ATTR_PLAIN; |
ca->pass_attr = PASS_ATTR_PLAIN; |
224 |
} |
} |
225 |
#ifdef CRAM_MD5 |
#ifdef CRAM_MD5 |
226 |
if (connpri.caps & CAPS_CRAMMD5) |
if (connpri.caps & CAPS_CRAMMD5) |
227 |
r = auth_cram_md5(&connpri, |
r = auth_cram_md5(&connpri, |
228 |
ca->user, ca->pass); |
ca->user, ca->pass); |
229 |
else |
else |
230 |
#endif |
#endif |
231 |
r = login(&connpri, ca->user, |
r = login(&connpri, ca->user, |
232 |
ca->pass); |
ca->pass); |
233 |
|
|
234 |
if (r == RESPONSE_NO) { |
if (r == RESPONSE_NO) { |
235 |
error("username %s or password rejected " |
error("username %s or password rejected " |
236 |
"at %s\n", ca->user, ca->server); |
"at %s\n", ca->user, ca->server); |
237 |
continue; |
continue; |
|
} |
|
238 |
} |
} |
|
check_namespace(&connpri); |
|
|
|
|
|
for (cm = ca->mboxes; cm != NULL; cm = cm->next) |
|
|
if (*cm->filters == NULL) |
|
|
mailbox_status(&connpri, cm->name); |
|
|
else if (!select_mailbox(&connpri, cm->name)) { |
|
|
apply_filters(cm->name, cm->filters); |
|
|
close_mailbox(&connpri); |
|
|
} |
|
|
logout(&connpri); |
|
|
|
|
|
close_connection(&connpri); |
|
239 |
} |
} |
240 |
|
check_namespace(&connpri); |
241 |
|
|
242 |
/* Fork if in daemon mode. */ |
for (cm = ca->mboxes; cm != NULL; cm = cm->next) |
243 |
if (opts.daemon && !(flags & FLAG_DAEMON)) { |
if (*cm->filters == NULL) |
244 |
switch (fork()) { |
mailbox_status(&connpri, cm->name); |
245 |
case -1: |
else if (!select_mailbox(&connpri, cm->name)) { |
246 |
fatal(ERROR_FORK, "forking; %s\n", |
apply_filters(cm->name, cm->filters); |
247 |
strerror(errno)); |
close_mailbox(&connpri); |
|
break; |
|
|
case 0: |
|
|
break; |
|
|
default: |
|
|
log_stop(); |
|
|
secmem_clear(); |
|
|
debug_stop(); |
|
|
exit(0); |
|
|
break; |
|
248 |
} |
} |
249 |
|
logout(&connpri); |
250 |
|
|
251 |
if (setsid() == -1) |
close_connection(&connpri); |
252 |
fatal(ERROR_FORK, "creating session; %s\n", |
} |
253 |
strerror(errno)); |
} |
|
|
|
|
switch (fork()) { |
|
|
case -1: |
|
|
fatal(ERROR_FORK, "forking; %s\n", |
|
|
strerror(errno)); |
|
|
break; |
|
|
case 0: |
|
|
break; |
|
|
default: |
|
|
log_stop(); |
|
|
secmem_clear(); |
|
|
debug_stop(); |
|
|
exit(0); |
|
|
break; |
|
|
} |
|
254 |
|
|
|
close(STDIN_FILENO); |
|
|
close(STDOUT_FILENO); |
|
|
close(STDERR_FILENO); |
|
|
if (open("/dev/null", O_RDWR) != -1) { |
|
|
dup(STDIN_FILENO); |
|
|
dup(STDIN_FILENO); |
|
|
} |
|
|
lockfile_create(); |
|
|
corefile_disable(); |
|
255 |
|
|
256 |
flags |= FLAG_DAEMON; |
/* |
257 |
} |
* Fork if in daemon mode. |
258 |
if (opts.daemon && flags & FLAG_SIGUSR1) { |
*/ |
259 |
reread_config(cf); |
void |
260 |
continue; |
daemonize(void) |
261 |
} |
{ |
|
if (opts.daemon) |
|
|
sleep(opts.daemon); |
|
|
} while (opts.daemon); |
|
262 |
|
|
263 |
log_stop(); |
switch (fork()) { |
264 |
secmem_clear(); |
case -1: |
265 |
|
fatal(ERROR_FORK, "forking; %s\n", |
266 |
|
strerror(errno)); |
267 |
|
break; |
268 |
|
case 0: |
269 |
|
break; |
270 |
|
default: |
271 |
|
log_stop(); |
272 |
|
secmem_clear(); |
273 |
|
debug_stop(); |
274 |
|
exit(0); |
275 |
|
break; |
276 |
|
} |
277 |
|
|
278 |
lockfile_remove(); |
if (setsid() == -1) |
279 |
|
fatal(ERROR_FORK, "creating session; %s\n", |
280 |
|
strerror(errno)); |
281 |
|
|
282 |
|
switch (fork()) { |
283 |
|
case -1: |
284 |
|
fatal(ERROR_FORK, "forking; %s\n", |
285 |
|
strerror(errno)); |
286 |
|
break; |
287 |
|
case 0: |
288 |
|
break; |
289 |
|
default: |
290 |
|
log_stop(); |
291 |
|
secmem_clear(); |
292 |
|
debug_stop(); |
293 |
|
exit(0); |
294 |
|
break; |
295 |
|
} |
296 |
|
|
297 |
debug_stop(); |
close(STDIN_FILENO); |
298 |
|
close(STDOUT_FILENO); |
299 |
|
close(STDERR_FILENO); |
300 |
|
if (open("/dev/null", O_RDWR) != -1) { |
301 |
|
dup(STDIN_FILENO); |
302 |
|
dup(STDIN_FILENO); |
303 |
|
} |
304 |
|
lockfile_create(); |
305 |
|
corefile_disable(); |
306 |
|
|
307 |
exit(0); |
flags |= FLAG_DAEMON; |
308 |
} |
} |
309 |
|
|
310 |
|
|
314 |
void |
void |
315 |
usage(void) |
usage(void) |
316 |
{ |
{ |
317 |
|
|
318 |
fprintf(stderr, |
fprintf(stderr, |
319 |
"usage: imapfilter [-DVbk" |
"usage: imapfilter [-DVbk" |
320 |
#ifdef ENCRYPTED_PASSWORDS |
#ifdef ENCRYPTED_PASSWORDS |
332 |
void |
void |
333 |
version(void) |
version(void) |
334 |
{ |
{ |
335 |
|
|
336 |
fprintf(stderr, "IMAPFilter %s" |
fprintf(stderr, "IMAPFilter %s" |
337 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5 |
338 |
", OpenSSL 0x%8.8lx" |
", OpenSSL 0x%8.8lx" |