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