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