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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.60 - (hide annotations)
Mon Feb 9 17:34:56 2004 UTC (20 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.59: +19 -13 lines
File MIME type: text/plain
Move DEBUG from compilation #define variable to runtime command line option.

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.10 unsigned int options; /* Program options. */
30 lefcha 1.29 unsigned int flags = 0; /* Program flags. */
31     unsigned int interval = 0; /* Poll at the specified interval. */
32 lefcha 1.51 conn_t connpri, connaux; /* Primary and auxiliary IMAP connection. */
33 lefcha 1.41
34 lefcha 1.5 char logfile[PATH_MAX]; /* Log file. */
35 lefcha 1.25 char *home = NULL; /* User's home directory. */
36 lefcha 1.40 char charset[CHARSET_LEN]; /* Charset for IMAP SEARCH requests. */
37 lefcha 1.42
38 lefcha 1.46 jmp_buf acctloop; /* Non-local exit in case of network error. */
39 lefcha 1.1
40 lefcha 1.29
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.58 int c, r;
52 lefcha 1.55 char *conffile; /* Configuration file. */
53 lefcha 1.42 account_t *ca; /* Current account. */
54     mbox_t *cm; /* Current mailbox. */
55 lefcha 1.33
56 lefcha 1.45 setlocale(LC_ALL, "");
57 lefcha 1.40
58 lefcha 1.42 home = getenv("HOME");
59 lefcha 1.59 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_PEEK |
60     OPTION_PERMISSIONS);
61 lefcha 1.42 *charset = 0;
62     *logfile = 0;
63 lefcha 1.55 conffile = NULL;
64     connpri.sock = connaux.sock = -1;
65 lefcha 1.33
66 lefcha 1.60 while ((c = getopt(argc, argv, "c:d:Dhkl:"
67 lefcha 1.29 #ifdef ENCRYPTED_PASSWORDS
68 lefcha 1.42 "p"
69 lefcha 1.25 #endif
70 lefcha 1.42 "qvV")) != -1) {
71     switch (c) {
72     case 'c':
73 lefcha 1.55 conffile = optarg;
74 lefcha 1.42 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 lefcha 1.60 case 'D':
83     options |= OPTION_DEBUG;
84     break;
85 lefcha 1.42 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 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
96 lefcha 1.42 case 'p':
97     options |= OPTION_PASSWORD_EDITOR;
98     break;
99     #endif
100     case 'q':
101 lefcha 1.57 options &= ~(OPTION_DETAILS_QUIET |
102     OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
103 lefcha 1.42 options |= OPTION_DETAILS_QUIET;
104     break;
105     case 'v':
106 lefcha 1.57 options &= ~(OPTION_DETAILS_QUIET |
107     OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
108 lefcha 1.42 options |= OPTION_DETAILS_VERBOSE;
109     break;
110     case 'V':
111     version();
112     exit(ERROR_UNDEFINED);
113     break;
114     default:
115     usage();
116     exit(ERROR_UNDEFINED);
117     break;
118     }
119 lefcha 1.1 }
120 lefcha 1.32
121 lefcha 1.60 debug_start();
122    
123 lefcha 1.42 create_homedir();
124 lefcha 1.33
125 lefcha 1.42 lockfile_check();
126     lockfile_create();
127 lefcha 1.28
128 lefcha 1.60 if (!(options & OPTION_DEBUG))
129     corefile_disable();
130 lefcha 1.33
131 lefcha 1.42 tty_store();
132     catch_signals();
133 lefcha 1.24
134 lefcha 1.55 read_config(conffile);
135 lefcha 1.33
136 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
137 lefcha 1.42 read_passwords();
138 lefcha 1.33
139 lefcha 1.42 if ((options & OPTION_PASSWORD_EDITOR)) {
140     password_editor();
141 lefcha 1.33
142 lefcha 1.42 secmem_clear();
143     lockfile_remove();
144 lefcha 1.33
145 lefcha 1.42 exit(0);
146     }
147 lefcha 1.25 #endif
148 lefcha 1.33
149 lefcha 1.60 log_start();
150 lefcha 1.32
151 lefcha 1.50 init_buffer(&ibuf);
152     init_buffer(&obuf);
153 lefcha 1.32
154 lefcha 1.42 if (options & OPTION_DAEMON_MODE) {
155 lefcha 1.57 options &= ~(OPTION_DETAILS_QUIET |
156     OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
157 lefcha 1.42 options |= OPTION_DETAILS_QUIET;
158     }
159     do {
160     for (ca = accounts; ca != NULL; ca = ca->next) {
161 lefcha 1.7
162 lefcha 1.42 if (setjmp(acctloop))
163     continue;
164 lefcha 1.35
165 lefcha 1.51 if (init_connection(&connpri, ca->server, ca->port,
166 lefcha 1.42 ca->ssl))
167     continue;
168 lefcha 1.21
169 lefcha 1.51 r = greeting_response(&connpri);
170 lefcha 1.18
171 lefcha 1.60 if (options & OPTION_DEBUG)
172     test(&connpri);
173 lefcha 1.45
174 lefcha 1.51 if (check_capabilities(&connpri))
175 lefcha 1.42 continue;
176 lefcha 1.1
177 lefcha 1.49 #ifdef SSL_TLS
178     if (ca->ssl == SSL_DISABLED &&
179 lefcha 1.51 connpri.caps & CAPABILITY_STARTTLS)
180 lefcha 1.52 if (negotiate_tls(&connpri) == RESPONSE_OK)
181 lefcha 1.51 check_capabilities(&connpri);
182 lefcha 1.49 #endif
183    
184 lefcha 1.42 log_info(LOG_ACCOUNT, ca->key);
185 lefcha 1.1
186 lefcha 1.42 if (r != RESPONSE_PREAUTH) {
187     if (ca->passwdattr == PASSWORD_NONE) {
188     printf("Enter password for %s@%s: ",
189     ca->username, ca->server);
190     get_password(ca->password, PASSWORD_LEN);
191     ca->passwdattr = PASSWORD_PLAIN;
192     }
193 lefcha 1.48 #ifdef CRAM_MD5
194 lefcha 1.51 if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5)
195 lefcha 1.52 r = auth_cram_md5(&connpri,
196 lefcha 1.48 ca->username, ca->password);
197     else
198     #endif
199 lefcha 1.51 r = login(&connpri, ca->username,
200 lefcha 1.48 ca->password);
201    
202     if (r == RESPONSE_NO) {
203 lefcha 1.42 error("username %s or password rejected "
204     "at %s\n", ca->username, ca->server);
205     continue;
206     }
207     }
208 lefcha 1.51 check_namespace(&connpri);
209 lefcha 1.42
210     for (cm = ca->mboxes; cm != NULL; cm = cm->next)
211     if (*cm->filters == NULL)
212 lefcha 1.51 mailbox_status(&connpri, cm->name);
213     else if (!select_mailbox(&connpri, cm->name)) {
214 lefcha 1.43 apply_filters(cm->name, cm->filters);
215 lefcha 1.51 close_mailbox(&connpri);
216 lefcha 1.42 }
217 lefcha 1.51 logout(&connpri);
218 lefcha 1.33
219 lefcha 1.51 close_connection(&connpri);
220 lefcha 1.33 }
221 lefcha 1.11
222 lefcha 1.42 /* Fork if in daemon mode. */
223 lefcha 1.58 if (options & OPTION_DAEMON_MODE &&
224     !(flags & FLAG_DAEMON_MODE)) {
225    
226     switch (fork()) {
227     case -1:
228     fatal(ERROR_FORK, "forking; %s\n",
229     strerror(errno));
230     break;
231     case 0:
232     break;
233     default:
234 lefcha 1.60 log_stop();
235 lefcha 1.58 secmem_clear();
236 lefcha 1.60 debug_stop();
237 lefcha 1.58 exit(0);
238     break;
239     }
240    
241     if (setsid() == -1)
242     fatal(ERROR_FORK, "creating session; %s\n",
243     strerror(errno));
244    
245     switch (fork()) {
246 lefcha 1.42 case -1:
247     fatal(ERROR_FORK, "forking; %s\n",
248     strerror(errno));
249     break;
250     case 0:
251     break;
252     default:
253 lefcha 1.60 log_stop();
254 lefcha 1.42 secmem_clear();
255 lefcha 1.60 debug_stop();
256 lefcha 1.42 exit(0);
257     break;
258     }
259 lefcha 1.58
260     close(STDIN_FILENO);
261     close(STDOUT_FILENO);
262     close(STDERR_FILENO);
263     if (open("/dev/null", O_RDWR) != -1) {
264     dup(STDIN_FILENO);
265     dup(STDIN_FILENO);
266     }
267     lockfile_create();
268     corefile_disable();
269    
270     flags |= FLAG_DAEMON_MODE;
271 lefcha 1.46 }
272     if (options & OPTION_DAEMON_MODE &&
273 lefcha 1.54 flags & FLAG_SIGUSR1_RECEIVED) {
274 lefcha 1.55 reread_config(conffile);
275 lefcha 1.46 continue;
276 lefcha 1.42 }
277     if (interval)
278     sleep(interval);
279     } while (options & OPTION_DAEMON_MODE && interval);
280 lefcha 1.17
281 lefcha 1.60 log_stop();
282 lefcha 1.42 secmem_clear();
283 lefcha 1.33
284 lefcha 1.42 lockfile_remove();
285 lefcha 1.33
286 lefcha 1.60 debug_stop();
287    
288 lefcha 1.55 exit(0);
289 lefcha 1.1 }
290    
291    
292     /*
293 lefcha 1.11 * Print a very brief usage message.
294 lefcha 1.2 */
295 lefcha 1.42 void
296     usage(void)
297 lefcha 1.2 {
298 lefcha 1.42 fprintf(stderr,
299 lefcha 1.60 "usage: imapfilter [-bDhk"
300 lefcha 1.26 #ifdef ENCRYPTED_PASSWORDS
301     "p"
302 lefcha 1.33 #endif
303 lefcha 1.38 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
304     }
305    
306    
307     /*
308     * Print program's version, and if it is built in, OpenSSL's version number.
309     */
310 lefcha 1.42 void
311     version(void)
312 lefcha 1.38 {
313 lefcha 1.42 fprintf(stderr, "IMAPFilter %s"
314 lefcha 1.53 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
315 lefcha 1.38 ", OpenSSL 0x%8.8lx"
316     #endif
317     "\n", IMAPFILTER_VERSION
318 lefcha 1.53 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
319 lefcha 1.38 ,SSLeay()
320     #endif
321 lefcha 1.42 );
322 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26