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