1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
lefcha |
1.16 |
#include <stdlib.h> |
3 |
lefcha |
1.1 |
#include <unistd.h> |
4 |
|
|
#include <string.h> |
5 |
lefcha |
1.7 |
#include <limits.h> |
6 |
lefcha |
1.11 |
#include <errno.h> |
7 |
lefcha |
1.35 |
#include <setjmp.h> |
8 |
lefcha |
1.40 |
#include <locale.h> |
9 |
lefcha |
1.1 |
|
10 |
lefcha |
1.39 |
#include "config.h" |
11 |
|
|
#include "imapfilter.h" |
12 |
|
|
#include "data.h" |
13 |
|
|
|
14 |
lefcha |
1.38 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
15 |
|
|
#include <openssl/crypto.h> |
16 |
|
|
#endif |
17 |
lefcha |
1.1 |
|
18 |
|
|
|
19 |
lefcha |
1.7 |
extern account_t *accounts; |
20 |
lefcha |
1.11 |
extern filter_t *filters; |
21 |
lefcha |
1.50 |
extern buffer_t ibuf, obuf; |
22 |
lefcha |
1.7 |
|
23 |
lefcha |
1.10 |
unsigned int options; /* Program options. */ |
24 |
lefcha |
1.29 |
unsigned int flags = 0; /* Program flags. */ |
25 |
|
|
unsigned int interval = 0; /* Poll at the specified interval. */ |
26 |
lefcha |
1.51 |
conn_t connpri, connaux; /* Primary and auxiliary IMAP connection. */ |
27 |
lefcha |
1.41 |
|
28 |
lefcha |
1.5 |
char logfile[PATH_MAX]; /* Log file. */ |
29 |
lefcha |
1.25 |
char *home = NULL; /* User's home directory. */ |
30 |
lefcha |
1.40 |
char charset[CHARSET_LEN]; /* Charset for IMAP SEARCH requests. */ |
31 |
lefcha |
1.42 |
|
32 |
lefcha |
1.46 |
jmp_buf acctloop; /* Non-local exit in case of network error. */ |
33 |
lefcha |
1.1 |
|
34 |
lefcha |
1.29 |
|
35 |
lefcha |
1.2 |
/* |
36 |
lefcha |
1.46 |
* IMAPFilter: an IMAP mail filtering utility. |
37 |
lefcha |
1.2 |
*/ |
38 |
lefcha |
1.42 |
int |
39 |
|
|
main(int argc, char *argv[]) |
40 |
lefcha |
1.1 |
{ |
41 |
lefcha |
1.42 |
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 |
lefcha |
1.33 |
|
47 |
lefcha |
1.45 |
setlocale(LC_ALL, ""); |
48 |
lefcha |
1.40 |
|
49 |
lefcha |
1.42 |
f = 0; |
50 |
|
|
home = getenv("HOME"); |
51 |
lefcha |
1.47 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE); |
52 |
lefcha |
1.42 |
*charset = 0; |
53 |
|
|
*logfile = 0; |
54 |
|
|
confile = NULL; |
55 |
lefcha |
1.33 |
|
56 |
lefcha |
1.42 |
while ((c = getopt(argc, argv, "c:d:hkl:" |
57 |
lefcha |
1.29 |
#ifdef ENCRYPTED_PASSWORDS |
58 |
lefcha |
1.42 |
"p" |
59 |
lefcha |
1.25 |
#endif |
60 |
lefcha |
1.42 |
"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 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
83 |
lefcha |
1.42 |
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 |
lefcha |
1.1 |
} |
105 |
lefcha |
1.32 |
|
106 |
lefcha |
1.42 |
create_homedir(); |
107 |
lefcha |
1.33 |
|
108 |
lefcha |
1.42 |
lockfile_check(); |
109 |
|
|
lockfile_create(); |
110 |
lefcha |
1.28 |
|
111 |
lefcha |
1.47 |
#ifndef DEBUG |
112 |
lefcha |
1.42 |
corefile_disable(); |
113 |
lefcha |
1.47 |
#endif |
114 |
lefcha |
1.33 |
|
115 |
lefcha |
1.42 |
tty_store(); |
116 |
|
|
catch_signals(); |
117 |
lefcha |
1.24 |
|
118 |
lefcha |
1.42 |
read_config(confile); |
119 |
lefcha |
1.33 |
|
120 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
121 |
lefcha |
1.42 |
read_passwords(); |
122 |
lefcha |
1.33 |
|
123 |
lefcha |
1.42 |
if ((options & OPTION_PASSWORD_EDITOR)) { |
124 |
|
|
password_editor(); |
125 |
lefcha |
1.33 |
|
126 |
lefcha |
1.42 |
secmem_clear(); |
127 |
|
|
lockfile_remove(); |
128 |
lefcha |
1.33 |
|
129 |
lefcha |
1.42 |
exit(0); |
130 |
|
|
} |
131 |
lefcha |
1.25 |
#endif |
132 |
lefcha |
1.33 |
|
133 |
lefcha |
1.42 |
open_logfile(); |
134 |
lefcha |
1.32 |
|
135 |
lefcha |
1.50 |
init_buffer(&ibuf); |
136 |
|
|
init_buffer(&obuf); |
137 |
lefcha |
1.32 |
|
138 |
lefcha |
1.42 |
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 |
lefcha |
1.7 |
|
146 |
lefcha |
1.42 |
if (setjmp(acctloop)) |
147 |
|
|
continue; |
148 |
lefcha |
1.35 |
|
149 |
lefcha |
1.51 |
if (init_connection(&connpri, ca->server, ca->port, |
150 |
lefcha |
1.42 |
ca->ssl)) |
151 |
|
|
continue; |
152 |
lefcha |
1.21 |
|
153 |
lefcha |
1.51 |
r = greeting_response(&connpri); |
154 |
lefcha |
1.18 |
|
155 |
lefcha |
1.45 |
#ifdef DEBUG |
156 |
lefcha |
1.51 |
test(&connpri); |
157 |
lefcha |
1.45 |
#endif |
158 |
|
|
|
159 |
lefcha |
1.51 |
if (check_capabilities(&connpri)) |
160 |
lefcha |
1.42 |
continue; |
161 |
lefcha |
1.1 |
|
162 |
lefcha |
1.49 |
#ifdef SSL_TLS |
163 |
|
|
if (ca->ssl == SSL_DISABLED && |
164 |
lefcha |
1.51 |
connpri.caps & CAPABILITY_STARTTLS) |
165 |
|
|
if (imf_starttls(&connpri) == RESPONSE_OK) |
166 |
|
|
check_capabilities(&connpri); |
167 |
lefcha |
1.49 |
#endif |
168 |
|
|
|
169 |
lefcha |
1.42 |
log_info(LOG_ACCOUNT, ca->key); |
170 |
lefcha |
1.1 |
|
171 |
lefcha |
1.42 |
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 |
lefcha |
1.48 |
#ifdef CRAM_MD5 |
179 |
lefcha |
1.51 |
if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5) |
180 |
|
|
r = imf_cram_md5(&connpri, |
181 |
lefcha |
1.48 |
ca->username, ca->password); |
182 |
|
|
else |
183 |
|
|
#endif |
184 |
lefcha |
1.51 |
r = login(&connpri, ca->username, |
185 |
lefcha |
1.48 |
ca->password); |
186 |
|
|
|
187 |
|
|
if (r == RESPONSE_NO) { |
188 |
lefcha |
1.42 |
error("username %s or password rejected " |
189 |
|
|
"at %s\n", ca->username, ca->server); |
190 |
|
|
continue; |
191 |
|
|
} |
192 |
|
|
} |
193 |
lefcha |
1.51 |
check_namespace(&connpri); |
194 |
lefcha |
1.42 |
|
195 |
|
|
for (cm = ca->mboxes; cm != NULL; cm = cm->next) |
196 |
|
|
if (*cm->filters == NULL) |
197 |
lefcha |
1.51 |
mailbox_status(&connpri, cm->name); |
198 |
|
|
else if (!select_mailbox(&connpri, cm->name)) { |
199 |
lefcha |
1.43 |
apply_filters(cm->name, cm->filters); |
200 |
lefcha |
1.51 |
close_mailbox(&connpri); |
201 |
lefcha |
1.42 |
} |
202 |
lefcha |
1.51 |
logout(&connpri); |
203 |
lefcha |
1.33 |
|
204 |
lefcha |
1.51 |
close_connection(&connpri); |
205 |
lefcha |
1.33 |
} |
206 |
lefcha |
1.11 |
|
207 |
lefcha |
1.42 |
/* 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 |
lefcha |
1.44 |
flags |= FLAG_DAEMON_MODE; |
220 |
lefcha |
1.42 |
break; |
221 |
|
|
default: |
222 |
|
|
secmem_clear(); |
223 |
|
|
close_logfile(); |
224 |
|
|
exit(0); |
225 |
|
|
break; |
226 |
|
|
} |
227 |
lefcha |
1.46 |
} |
228 |
|
|
if (options & OPTION_DAEMON_MODE && |
229 |
|
|
flags & FLAG_SIGHUP_RECEIVED) { |
230 |
|
|
reread_config(confile); |
231 |
|
|
continue; |
232 |
lefcha |
1.42 |
} |
233 |
|
|
if (interval) |
234 |
|
|
sleep(interval); |
235 |
|
|
} while (options & OPTION_DAEMON_MODE && interval); |
236 |
lefcha |
1.17 |
|
237 |
lefcha |
1.42 |
secmem_clear(); |
238 |
|
|
close_logfile(); |
239 |
lefcha |
1.33 |
|
240 |
lefcha |
1.42 |
lockfile_remove(); |
241 |
lefcha |
1.33 |
|
242 |
lefcha |
1.48 |
return 0; |
243 |
lefcha |
1.1 |
} |
244 |
|
|
|
245 |
|
|
|
246 |
|
|
/* |
247 |
lefcha |
1.11 |
* Print a very brief usage message. |
248 |
lefcha |
1.2 |
*/ |
249 |
lefcha |
1.42 |
void |
250 |
|
|
usage(void) |
251 |
lefcha |
1.2 |
{ |
252 |
lefcha |
1.42 |
fprintf(stderr, |
253 |
lefcha |
1.29 |
"usage: imapfilter [-hk" |
254 |
lefcha |
1.26 |
#ifdef ENCRYPTED_PASSWORDS |
255 |
|
|
"p" |
256 |
lefcha |
1.33 |
#endif |
257 |
lefcha |
1.38 |
"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 |
lefcha |
1.42 |
void |
265 |
|
|
version(void) |
266 |
lefcha |
1.38 |
{ |
267 |
lefcha |
1.42 |
fprintf(stderr, "IMAPFilter %s" |
268 |
lefcha |
1.38 |
#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 |
lefcha |
1.42 |
); |
276 |
lefcha |
1.1 |
} |