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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.57 - (show annotations)
Tue Jan 20 02:05:06 2004 UTC (20 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.56: +6 -3 lines
File MIME type: text/plain
Got rid of OPTIONS_DETAILS_CLEAR.

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 <setjmp.h>
9 #include <locale.h>
10
11 #include "config.h"
12 #include "imapfilter.h"
13 #include "version.h"
14 #include "account.h"
15 #include "filter.h"
16 #include "buffer.h"
17
18 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
19 #include <openssl/opensslv.h>
20 #include <openssl/crypto.h>
21 #endif
22
23
24 extern account_t *accounts;
25 extern filter_t *filters;
26 extern buffer_t ibuf, obuf;
27
28 unsigned int options; /* Program options. */
29 unsigned int flags = 0; /* Program flags. */
30 unsigned int interval = 0; /* Poll at the specified interval. */
31 conn_t connpri, connaux; /* Primary and auxiliary IMAP connection. */
32
33 char logfile[PATH_MAX]; /* Log file. */
34 char *home = NULL; /* User's home directory. */
35 char charset[CHARSET_LEN]; /* Charset for IMAP SEARCH requests. */
36
37 jmp_buf acctloop; /* Non-local exit in case of network error. */
38
39
40 void usage(void);
41 void version(void);
42
43
44 /*
45 * IMAPFilter: an IMAP mail filtering utility.
46 */
47 int
48 main(int argc, char *argv[])
49 {
50 int c, r, f;
51 pid_t pid;
52 char *conffile; /* Configuration file. */
53 account_t *ca; /* Current account. */
54 mbox_t *cm; /* Current mailbox. */
55
56 setlocale(LC_ALL, "");
57
58 f = 0;
59 home = getenv("HOME");
60 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_PEEK);
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 f = 1;
152 options &= ~(OPTION_DETAILS_QUIET |
153 OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
154 options |= OPTION_DETAILS_QUIET;
155 }
156 do {
157 for (ca = accounts; ca != NULL; ca = ca->next) {
158
159 if (setjmp(acctloop))
160 continue;
161
162 if (init_connection(&connpri, ca->server, ca->port,
163 ca->ssl))
164 continue;
165
166 r = greeting_response(&connpri);
167
168 #ifdef DEBUG
169 test(&connpri);
170 #endif
171
172 if (check_capabilities(&connpri))
173 continue;
174
175 #ifdef SSL_TLS
176 if (ca->ssl == SSL_DISABLED &&
177 connpri.caps & CAPABILITY_STARTTLS)
178 if (negotiate_tls(&connpri) == RESPONSE_OK)
179 check_capabilities(&connpri);
180 #endif
181
182 log_info(LOG_ACCOUNT, ca->key);
183
184 if (r != RESPONSE_PREAUTH) {
185 if (ca->passwdattr == PASSWORD_NONE) {
186 printf("Enter password for %s@%s: ",
187 ca->username, ca->server);
188 get_password(ca->password, PASSWORD_LEN);
189 ca->passwdattr = PASSWORD_PLAIN;
190 }
191 #ifdef CRAM_MD5
192 if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5)
193 r = auth_cram_md5(&connpri,
194 ca->username, ca->password);
195 else
196 #endif
197 r = login(&connpri, ca->username,
198 ca->password);
199
200 if (r == RESPONSE_NO) {
201 error("username %s or password rejected "
202 "at %s\n", ca->username, ca->server);
203 continue;
204 }
205 }
206 check_namespace(&connpri);
207
208 for (cm = ca->mboxes; cm != NULL; cm = cm->next)
209 if (*cm->filters == NULL)
210 mailbox_status(&connpri, cm->name);
211 else if (!select_mailbox(&connpri, cm->name)) {
212 apply_filters(cm->name, cm->filters);
213 close_mailbox(&connpri);
214 }
215 logout(&connpri);
216
217 close_connection(&connpri);
218 }
219
220 /* Fork if in daemon mode. */
221 if (f) {
222 f = 0;
223 pid = fork();
224 switch (pid) {
225 case -1:
226 fatal(ERROR_FORK, "forking; %s\n",
227 strerror(errno));
228 break;
229 case 0:
230 lockfile_create();
231 corefile_disable();
232 flags |= FLAG_DAEMON_MODE;
233 break;
234 default:
235 secmem_clear();
236 close_logfile();
237 exit(0);
238 break;
239 }
240 }
241 if (options & OPTION_DAEMON_MODE &&
242 flags & FLAG_SIGUSR1_RECEIVED) {
243 reread_config(conffile);
244 continue;
245 }
246 if (interval)
247 sleep(interval);
248 } while (options & OPTION_DAEMON_MODE && interval);
249
250 secmem_clear();
251 close_logfile();
252
253 lockfile_remove();
254
255 exit(0);
256 }
257
258
259 /*
260 * Print a very brief usage message.
261 */
262 void
263 usage(void)
264 {
265 fprintf(stderr,
266 "usage: imapfilter [-hk"
267 #ifdef ENCRYPTED_PASSWORDS
268 "p"
269 #endif
270 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
271 }
272
273
274 /*
275 * Print program's version, and if it is built in, OpenSSL's version number.
276 */
277 void
278 version(void)
279 {
280 fprintf(stderr, "IMAPFilter %s"
281 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
282 ", OpenSSL 0x%8.8lx"
283 #endif
284 "\n", IMAPFILTER_VERSION
285 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
286 ,SSLeay()
287 #endif
288 );
289 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26