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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.53 - (show annotations)
Sun Aug 3 16:05:24 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.52: +4 -3 lines
File MIME type: text/plain
Include stdlib.h and check for CRAM_MD5 when printing OpenSSL version.

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <limits.h>
7 #include <setjmp.h>
8 #include <locale.h>
9
10 #include "config.h"
11 #include "imapfilter.h"
12 #include "version.h"
13 #include "account.h"
14 #include "filter.h"
15 #include "buffer.h"
16
17 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
18 #include <openssl/crypto.h>
19 #endif
20
21
22 extern account_t *accounts;
23 extern filter_t *filters;
24 extern buffer_t ibuf, obuf;
25
26 unsigned int options; /* Program options. */
27 unsigned int flags = 0; /* Program flags. */
28 unsigned int interval = 0; /* Poll at the specified interval. */
29 conn_t connpri, connaux; /* Primary and auxiliary IMAP connection. */
30
31 char logfile[PATH_MAX]; /* Log file. */
32 char *home = NULL; /* User's home directory. */
33 char charset[CHARSET_LEN]; /* Charset for IMAP SEARCH requests. */
34
35 jmp_buf acctloop; /* Non-local exit in case of network error. */
36
37
38 void usage(void);
39 void version(void);
40
41
42 /*
43 * IMAPFilter: an IMAP mail filtering utility.
44 */
45 int
46 main(int argc, char *argv[])
47 {
48 int c, r, f;
49 pid_t pid;
50 char *confile; /* Configuration file. */
51 account_t *ca; /* Current account. */
52 mbox_t *cm; /* Current mailbox. */
53
54 setlocale(LC_ALL, "");
55
56 f = 0;
57 home = getenv("HOME");
58 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE);
59 *charset = 0;
60 *logfile = 0;
61 confile = NULL;
62
63 while ((c = getopt(argc, argv, "c:d:hkl:"
64 #ifdef ENCRYPTED_PASSWORDS
65 "p"
66 #endif
67 "qvV")) != -1) {
68 switch (c) {
69 case 'c':
70 confile = optarg;
71 break;
72 case 'd':
73 options |= OPTION_DAEMON_MODE;
74 errno = 0;
75 interval = strtoul(optarg, NULL, 10);
76 if (errno)
77 interval = 0;
78 break;
79 case 'h':
80 usage();
81 exit(ERROR_UNDEFINED);
82 break;
83 case 'k':
84 kill_imapfilter();
85 break;
86 case 'l':
87 strncat(logfile, optarg, PATH_MAX - 1);
88 break;
89 #ifdef ENCRYPTED_PASSWORDS
90 case 'p':
91 options |= OPTION_PASSWORD_EDITOR;
92 break;
93 #endif
94 case 'q':
95 options &= OPTION_DETAILS_CLEAR;
96 options |= OPTION_DETAILS_QUIET;
97 break;
98 case 'v':
99 options &= OPTION_DETAILS_CLEAR;
100 options |= OPTION_DETAILS_VERBOSE;
101 break;
102 case 'V':
103 version();
104 exit(ERROR_UNDEFINED);
105 break;
106 default:
107 usage();
108 exit(ERROR_UNDEFINED);
109 break;
110 }
111 }
112
113 create_homedir();
114
115 lockfile_check();
116 lockfile_create();
117
118 #ifndef DEBUG
119 corefile_disable();
120 #endif
121
122 tty_store();
123 catch_signals();
124
125 read_config(confile);
126
127 #ifdef ENCRYPTED_PASSWORDS
128 read_passwords();
129
130 if ((options & OPTION_PASSWORD_EDITOR)) {
131 password_editor();
132
133 secmem_clear();
134 lockfile_remove();
135
136 exit(0);
137 }
138 #endif
139
140 open_logfile();
141
142 init_buffer(&ibuf);
143 init_buffer(&obuf);
144
145 if (options & OPTION_DAEMON_MODE) {
146 f = 1;
147 options &= OPTION_DETAILS_CLEAR;
148 options |= OPTION_DETAILS_QUIET;
149 }
150 do {
151 for (ca = accounts; ca != NULL; ca = ca->next) {
152
153 if (setjmp(acctloop))
154 continue;
155
156 if (init_connection(&connpri, ca->server, ca->port,
157 ca->ssl))
158 continue;
159
160 r = greeting_response(&connpri);
161
162 #ifdef DEBUG
163 test(&connpri);
164 #endif
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 (f) {
216 f = 0;
217 pid = fork();
218 switch (pid) {
219 case -1:
220 fatal(ERROR_FORK, "forking; %s\n",
221 strerror(errno));
222 break;
223 case 0:
224 lockfile_create();
225 corefile_disable();
226 flags |= FLAG_DAEMON_MODE;
227 break;
228 default:
229 secmem_clear();
230 close_logfile();
231 exit(0);
232 break;
233 }
234 }
235 if (options & OPTION_DAEMON_MODE &&
236 flags & FLAG_SIGHUP_RECEIVED) {
237 reread_config(confile);
238 continue;
239 }
240 if (interval)
241 sleep(interval);
242 } while (options & OPTION_DAEMON_MODE && interval);
243
244 secmem_clear();
245 close_logfile();
246
247 lockfile_remove();
248
249 return 0;
250 }
251
252
253 /*
254 * Print a very brief usage message.
255 */
256 void
257 usage(void)
258 {
259 fprintf(stderr,
260 "usage: imapfilter [-hk"
261 #ifdef ENCRYPTED_PASSWORDS
262 "p"
263 #endif
264 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
265 }
266
267
268 /*
269 * Print program's version, and if it is built in, OpenSSL's version number.
270 */
271 void
272 version(void)
273 {
274 fprintf(stderr, "IMAPFilter %s"
275 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
276 ", OpenSSL 0x%8.8lx"
277 #endif
278 "\n", IMAPFILTER_VERSION
279 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
280 ,SSLeay()
281 #endif
282 );
283 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26