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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.54.2.2 - (show annotations)
Sun Nov 23 22:52:54 2003 UTC (20 years, 4 months ago) by lefcha
Branch: release-0_9-patches
Changes since 1.54.2.1: +1 -1 lines
File MIME type: text/plain
dded "peek" option that causes use of BODY or BODY.PEEK when doing FETCH.

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_CLEAR;
99 options |= OPTION_DETAILS_QUIET;
100 break;
101 case 'v':
102 options &= OPTION_DETAILS_CLEAR;
103 options |= OPTION_DETAILS_VERBOSE;
104 break;
105 case 'V':
106 version();
107 exit(ERROR_UNDEFINED);
108 break;
109 default:
110 usage();
111 exit(ERROR_UNDEFINED);
112 break;
113 }
114 }
115
116 create_homedir();
117
118 lockfile_check();
119 lockfile_create();
120
121 #ifndef DEBUG
122 corefile_disable();
123 #endif
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 open_logfile();
144
145 init_buffer(&ibuf);
146 init_buffer(&obuf);
147
148 if (options & OPTION_DAEMON_MODE) {
149 f = 1;
150 options &= OPTION_DETAILS_CLEAR;
151 options |= OPTION_DETAILS_QUIET;
152 }
153 do {
154 for (ca = accounts; ca != NULL; ca = ca->next) {
155
156 if (setjmp(acctloop))
157 continue;
158
159 if (init_connection(&connpri, ca->server, ca->port,
160 ca->ssl))
161 continue;
162
163 r = greeting_response(&connpri);
164
165 #ifdef DEBUG
166 test(&connpri);
167 #endif
168
169 if (check_capabilities(&connpri))
170 continue;
171
172 #ifdef SSL_TLS
173 if (ca->ssl == SSL_DISABLED &&
174 connpri.caps & CAPABILITY_STARTTLS)
175 if (negotiate_tls(&connpri) == RESPONSE_OK)
176 check_capabilities(&connpri);
177 #endif
178
179 log_info(LOG_ACCOUNT, ca->key);
180
181 if (r != RESPONSE_PREAUTH) {
182 if (ca->passwdattr == PASSWORD_NONE) {
183 printf("Enter password for %s@%s: ",
184 ca->username, ca->server);
185 get_password(ca->password, PASSWORD_LEN);
186 ca->passwdattr = PASSWORD_PLAIN;
187 }
188 #ifdef CRAM_MD5
189 if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5)
190 r = auth_cram_md5(&connpri,
191 ca->username, ca->password);
192 else
193 #endif
194 r = login(&connpri, ca->username,
195 ca->password);
196
197 if (r == RESPONSE_NO) {
198 error("username %s or password rejected "
199 "at %s\n", ca->username, ca->server);
200 continue;
201 }
202 }
203 check_namespace(&connpri);
204
205 for (cm = ca->mboxes; cm != NULL; cm = cm->next)
206 if (*cm->filters == NULL)
207 mailbox_status(&connpri, cm->name);
208 else if (!select_mailbox(&connpri, cm->name)) {
209 apply_filters(cm->name, cm->filters);
210 close_mailbox(&connpri);
211 }
212 logout(&connpri);
213
214 close_connection(&connpri);
215 }
216
217 /* Fork if in daemon mode. */
218 if (f) {
219 f = 0;
220 pid = fork();
221 switch (pid) {
222 case -1:
223 fatal(ERROR_FORK, "forking; %s\n",
224 strerror(errno));
225 break;
226 case 0:
227 lockfile_create();
228 corefile_disable();
229 flags |= FLAG_DAEMON_MODE;
230 break;
231 default:
232 secmem_clear();
233 close_logfile();
234 exit(0);
235 break;
236 }
237 }
238 if (options & OPTION_DAEMON_MODE &&
239 flags & FLAG_SIGUSR1_RECEIVED) {
240 reread_config(conffile);
241 continue;
242 }
243 if (interval)
244 sleep(interval);
245 } while (options & OPTION_DAEMON_MODE && interval);
246
247 secmem_clear();
248 close_logfile();
249
250 lockfile_remove();
251
252 exit(0);
253 }
254
255
256 /*
257 * Print a very brief usage message.
258 */
259 void
260 usage(void)
261 {
262 fprintf(stderr,
263 "usage: imapfilter [-hk"
264 #ifdef ENCRYPTED_PASSWORDS
265 "p"
266 #endif
267 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
268 }
269
270
271 /*
272 * Print program's version, and if it is built in, OpenSSL's version number.
273 */
274 void
275 version(void)
276 {
277 fprintf(stderr, "IMAPFilter %s"
278 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
279 ", OpenSSL 0x%8.8lx"
280 #endif
281 "\n", IMAPFILTER_VERSION
282 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
283 ,SSLeay()
284 #endif
285 );
286 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26