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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.59 - (show annotations)
Sat Feb 7 23:54:16 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
Changes since 1.58: +2 -1 lines
File MIME type: text/plain
Removed CHECK_PERMISSIONS compilation option and added variable to control permissions checking.

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:hkl:"
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 'h':
83 usage();
84 exit(ERROR_UNDEFINED);
85 break;
86 case 'k':
87 kill_imapfilter();
88 break;
89 case 'l':
90 strncat(logfile, optarg, PATH_MAX - 1);
91 break;
92 #ifdef ENCRYPTED_PASSWORDS
93 case 'p':
94 options |= OPTION_PASSWORD_EDITOR;
95 break;
96 #endif
97 case 'q':
98 options &= ~(OPTION_DETAILS_QUIET |
99 OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
100 options |= OPTION_DETAILS_QUIET;
101 break;
102 case 'v':
103 options &= ~(OPTION_DETAILS_QUIET |
104 OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
105 options |= OPTION_DETAILS_VERBOSE;
106 break;
107 case 'V':
108 version();
109 exit(ERROR_UNDEFINED);
110 break;
111 default:
112 usage();
113 exit(ERROR_UNDEFINED);
114 break;
115 }
116 }
117
118 create_homedir();
119
120 lockfile_check();
121 lockfile_create();
122
123 #ifndef DEBUG
124 corefile_disable();
125 #endif
126
127 tty_store();
128 catch_signals();
129
130 read_config(conffile);
131
132 #ifdef ENCRYPTED_PASSWORDS
133 read_passwords();
134
135 if ((options & OPTION_PASSWORD_EDITOR)) {
136 password_editor();
137
138 secmem_clear();
139 lockfile_remove();
140
141 exit(0);
142 }
143 #endif
144
145 open_logfile();
146
147 init_buffer(&ibuf);
148 init_buffer(&obuf);
149
150 if (options & OPTION_DAEMON_MODE) {
151 options &= ~(OPTION_DETAILS_QUIET |
152 OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
153 options |= OPTION_DETAILS_QUIET;
154 }
155 do {
156 for (ca = accounts; ca != NULL; ca = ca->next) {
157
158 if (setjmp(acctloop))
159 continue;
160
161 if (init_connection(&connpri, ca->server, ca->port,
162 ca->ssl))
163 continue;
164
165 r = greeting_response(&connpri);
166
167 #ifdef DEBUG
168 test(&connpri);
169 #endif
170
171 if (check_capabilities(&connpri))
172 continue;
173
174 #ifdef SSL_TLS
175 if (ca->ssl == SSL_DISABLED &&
176 connpri.caps & CAPABILITY_STARTTLS)
177 if (negotiate_tls(&connpri) == RESPONSE_OK)
178 check_capabilities(&connpri);
179 #endif
180
181 log_info(LOG_ACCOUNT, ca->key);
182
183 if (r != RESPONSE_PREAUTH) {
184 if (ca->passwdattr == PASSWORD_NONE) {
185 printf("Enter password for %s@%s: ",
186 ca->username, ca->server);
187 get_password(ca->password, PASSWORD_LEN);
188 ca->passwdattr = PASSWORD_PLAIN;
189 }
190 #ifdef CRAM_MD5
191 if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5)
192 r = auth_cram_md5(&connpri,
193 ca->username, ca->password);
194 else
195 #endif
196 r = login(&connpri, ca->username,
197 ca->password);
198
199 if (r == RESPONSE_NO) {
200 error("username %s or password rejected "
201 "at %s\n", ca->username, ca->server);
202 continue;
203 }
204 }
205 check_namespace(&connpri);
206
207 for (cm = ca->mboxes; cm != NULL; cm = cm->next)
208 if (*cm->filters == NULL)
209 mailbox_status(&connpri, cm->name);
210 else if (!select_mailbox(&connpri, cm->name)) {
211 apply_filters(cm->name, cm->filters);
212 close_mailbox(&connpri);
213 }
214 logout(&connpri);
215
216 close_connection(&connpri);
217 }
218
219 /* Fork if in daemon mode. */
220 if (options & OPTION_DAEMON_MODE &&
221 !(flags & FLAG_DAEMON_MODE)) {
222
223 close_logfile();
224
225 switch (fork()) {
226 case -1:
227 fatal(ERROR_FORK, "forking; %s\n",
228 strerror(errno));
229 break;
230 case 0:
231 break;
232 default:
233 secmem_clear();
234 exit(0);
235 break;
236 }
237
238 if (setsid() == -1)
239 fatal(ERROR_FORK, "creating session; %s\n",
240 strerror(errno));
241
242 switch (fork()) {
243 case -1:
244 fatal(ERROR_FORK, "forking; %s\n",
245 strerror(errno));
246 break;
247 case 0:
248 break;
249 default:
250 secmem_clear();
251 exit(0);
252 break;
253 }
254
255 close(STDIN_FILENO);
256 close(STDOUT_FILENO);
257 close(STDERR_FILENO);
258 if (open("/dev/null", O_RDWR) != -1) {
259 dup(STDIN_FILENO);
260 dup(STDIN_FILENO);
261 }
262 open_logfile();
263 lockfile_create();
264 corefile_disable();
265
266 flags |= FLAG_DAEMON_MODE;
267 }
268 if (options & OPTION_DAEMON_MODE &&
269 flags & FLAG_SIGUSR1_RECEIVED) {
270 reread_config(conffile);
271 continue;
272 }
273 if (interval)
274 sleep(interval);
275 } while (options & OPTION_DAEMON_MODE && interval);
276
277 secmem_clear();
278 close_logfile();
279
280 lockfile_remove();
281
282 exit(0);
283 }
284
285
286 /*
287 * Print a very brief usage message.
288 */
289 void
290 usage(void)
291 {
292 fprintf(stderr,
293 "usage: imapfilter [-hk"
294 #ifdef ENCRYPTED_PASSWORDS
295 "p"
296 #endif
297 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
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 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26