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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.63 - (show annotations)
Mon Feb 9 20:39:23 2004 UTC (20 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.62: +16 -17 lines
File MIME type: text/plain
Removed -h option and changed ERROR_ values.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26