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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.60 - (show 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 #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 "qvV")) != -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 default:
115 usage();
116 exit(ERROR_UNDEFINED);
117 break;
118 }
119 }
120
121 debug_start();
122
123 create_homedir();
124
125 lockfile_check();
126 lockfile_create();
127
128 if (!(options & OPTION_DEBUG))
129 corefile_disable();
130
131 tty_store();
132 catch_signals();
133
134 read_config(conffile);
135
136 #ifdef ENCRYPTED_PASSWORDS
137 read_passwords();
138
139 if ((options & OPTION_PASSWORD_EDITOR)) {
140 password_editor();
141
142 secmem_clear();
143 lockfile_remove();
144
145 exit(0);
146 }
147 #endif
148
149 log_start();
150
151 init_buffer(&ibuf);
152 init_buffer(&obuf);
153
154 if (options & OPTION_DAEMON_MODE) {
155 options &= ~(OPTION_DETAILS_QUIET |
156 OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
157 options |= OPTION_DETAILS_QUIET;
158 }
159 do {
160 for (ca = accounts; ca != NULL; ca = ca->next) {
161
162 if (setjmp(acctloop))
163 continue;
164
165 if (init_connection(&connpri, ca->server, ca->port,
166 ca->ssl))
167 continue;
168
169 r = greeting_response(&connpri);
170
171 if (options & OPTION_DEBUG)
172 test(&connpri);
173
174 if (check_capabilities(&connpri))
175 continue;
176
177 #ifdef SSL_TLS
178 if (ca->ssl == SSL_DISABLED &&
179 connpri.caps & CAPABILITY_STARTTLS)
180 if (negotiate_tls(&connpri) == RESPONSE_OK)
181 check_capabilities(&connpri);
182 #endif
183
184 log_info(LOG_ACCOUNT, ca->key);
185
186 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 #ifdef CRAM_MD5
194 if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5)
195 r = auth_cram_md5(&connpri,
196 ca->username, ca->password);
197 else
198 #endif
199 r = login(&connpri, ca->username,
200 ca->password);
201
202 if (r == RESPONSE_NO) {
203 error("username %s or password rejected "
204 "at %s\n", ca->username, ca->server);
205 continue;
206 }
207 }
208 check_namespace(&connpri);
209
210 for (cm = ca->mboxes; cm != NULL; cm = cm->next)
211 if (*cm->filters == NULL)
212 mailbox_status(&connpri, cm->name);
213 else if (!select_mailbox(&connpri, cm->name)) {
214 apply_filters(cm->name, cm->filters);
215 close_mailbox(&connpri);
216 }
217 logout(&connpri);
218
219 close_connection(&connpri);
220 }
221
222 /* Fork if in daemon mode. */
223 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 log_stop();
235 secmem_clear();
236 debug_stop();
237 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 case -1:
247 fatal(ERROR_FORK, "forking; %s\n",
248 strerror(errno));
249 break;
250 case 0:
251 break;
252 default:
253 log_stop();
254 secmem_clear();
255 debug_stop();
256 exit(0);
257 break;
258 }
259
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 }
272 if (options & OPTION_DAEMON_MODE &&
273 flags & FLAG_SIGUSR1_RECEIVED) {
274 reread_config(conffile);
275 continue;
276 }
277 if (interval)
278 sleep(interval);
279 } while (options & OPTION_DAEMON_MODE && interval);
280
281 log_stop();
282 secmem_clear();
283
284 lockfile_remove();
285
286 debug_stop();
287
288 exit(0);
289 }
290
291
292 /*
293 * Print a very brief usage message.
294 */
295 void
296 usage(void)
297 {
298 fprintf(stderr,
299 "usage: imapfilter [-bDhk"
300 #ifdef ENCRYPTED_PASSWORDS
301 "p"
302 #endif
303 "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 void
311 version(void)
312 {
313 fprintf(stderr, "IMAPFilter %s"
314 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
315 ", OpenSSL 0x%8.8lx"
316 #endif
317 "\n", IMAPFILTER_VERSION
318 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
319 ,SSLeay()
320 #endif
321 );
322 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26