/[imapfilter]/imapfilter/imapfilter.c
ViewVC logotype

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.70 - (hide annotations)
Sat Feb 14 23:09:20 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
CVS Tags: HEAD
Changes since 1.69: +1 -0 lines
File MIME type: text/plain
Initialize force_protocol variable.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26