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.7 |
|
24 |
lefcha |
1.10 |
unsigned int options; /* Program options. */ |
25 |
lefcha |
1.29 |
unsigned int flags = 0; /* Program flags. */ |
26 |
lefcha |
1.24 |
unsigned int capabilities; /* Capabilities of mail server. */ |
27 |
lefcha |
1.29 |
unsigned int interval = 0; /* Poll at the specified interval. */ |
28 |
lefcha |
1.41 |
|
29 |
|
|
char authmech[AUTH_MECH_LEN]; /* Authentication mechanisms. */ |
30 |
lefcha |
1.5 |
char logfile[PATH_MAX]; /* Log file. */ |
31 |
lefcha |
1.25 |
char *home = NULL; /* User's home directory. */ |
32 |
lefcha |
1.40 |
char charset[CHARSET_LEN]; /* Charset for IMAP SEARCH requests. */ |
33 |
lefcha |
1.42 |
|
34 |
lefcha |
1.46 |
jmp_buf acctloop; /* Non-local exit in case of network error. */ |
35 |
lefcha |
1.1 |
|
36 |
lefcha |
1.29 |
|
37 |
lefcha |
1.2 |
/* |
38 |
lefcha |
1.46 |
* IMAPFilter: an IMAP mail filtering utility. |
39 |
lefcha |
1.2 |
*/ |
40 |
lefcha |
1.42 |
int |
41 |
|
|
main(int argc, char *argv[]) |
42 |
lefcha |
1.1 |
{ |
43 |
lefcha |
1.42 |
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 |
lefcha |
1.33 |
|
49 |
lefcha |
1.45 |
setlocale(LC_ALL, ""); |
50 |
lefcha |
1.40 |
|
51 |
lefcha |
1.42 |
f = 0; |
52 |
|
|
home = getenv("HOME"); |
53 |
lefcha |
1.47 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE); |
54 |
lefcha |
1.42 |
*charset = 0; |
55 |
|
|
*logfile = 0; |
56 |
|
|
confile = NULL; |
57 |
lefcha |
1.33 |
|
58 |
lefcha |
1.42 |
while ((c = getopt(argc, argv, "c:d:hkl:" |
59 |
lefcha |
1.29 |
#ifdef ENCRYPTED_PASSWORDS |
60 |
lefcha |
1.42 |
"p" |
61 |
lefcha |
1.25 |
#endif |
62 |
lefcha |
1.42 |
"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 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
85 |
lefcha |
1.42 |
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 |
lefcha |
1.1 |
} |
107 |
lefcha |
1.32 |
|
108 |
lefcha |
1.42 |
create_homedir(); |
109 |
lefcha |
1.33 |
|
110 |
lefcha |
1.42 |
lockfile_check(); |
111 |
|
|
lockfile_create(); |
112 |
lefcha |
1.28 |
|
113 |
lefcha |
1.47 |
#ifndef DEBUG |
114 |
lefcha |
1.42 |
corefile_disable(); |
115 |
lefcha |
1.47 |
#endif |
116 |
lefcha |
1.33 |
|
117 |
lefcha |
1.42 |
tty_store(); |
118 |
|
|
catch_signals(); |
119 |
lefcha |
1.24 |
|
120 |
lefcha |
1.42 |
read_config(confile); |
121 |
lefcha |
1.33 |
|
122 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
123 |
lefcha |
1.42 |
read_passwords(); |
124 |
lefcha |
1.33 |
|
125 |
lefcha |
1.42 |
if ((options & OPTION_PASSWORD_EDITOR)) { |
126 |
|
|
password_editor(); |
127 |
lefcha |
1.33 |
|
128 |
lefcha |
1.42 |
secmem_clear(); |
129 |
|
|
lockfile_remove(); |
130 |
lefcha |
1.33 |
|
131 |
lefcha |
1.42 |
exit(0); |
132 |
|
|
} |
133 |
lefcha |
1.25 |
#endif |
134 |
lefcha |
1.33 |
|
135 |
lefcha |
1.42 |
open_logfile(); |
136 |
lefcha |
1.32 |
|
137 |
lefcha |
1.42 |
init_vbuf(); |
138 |
lefcha |
1.32 |
|
139 |
lefcha |
1.42 |
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 |
lefcha |
1.7 |
|
147 |
lefcha |
1.42 |
if (setjmp(acctloop)) |
148 |
|
|
continue; |
149 |
lefcha |
1.35 |
|
150 |
lefcha |
1.42 |
if (init_connection(&sockpri, ca->server, ca->port, |
151 |
|
|
ca->ssl)) |
152 |
|
|
continue; |
153 |
lefcha |
1.21 |
|
154 |
lefcha |
1.42 |
r = greeting_response(&sockpri); |
155 |
lefcha |
1.18 |
|
156 |
lefcha |
1.45 |
#ifdef DEBUG |
157 |
|
|
test(&sockpri); |
158 |
|
|
#endif |
159 |
|
|
|
160 |
lefcha |
1.42 |
if (check_capabilities(&sockpri)) |
161 |
|
|
continue; |
162 |
lefcha |
1.1 |
|
163 |
lefcha |
1.42 |
log_info(LOG_ACCOUNT, ca->key); |
164 |
lefcha |
1.1 |
|
165 |
lefcha |
1.42 |
if (r != RESPONSE_PREAUTH) { |
166 |
|
|
if (ca->passwdattr == PASSWORD_NONE) { |
167 |
|
|
printf("Enter password for %s@%s: ", |
168 |
|
|
ca->username, ca->server); |
169 |
|
|
get_password(ca->password, PASSWORD_LEN); |
170 |
|
|
ca->passwdattr = PASSWORD_PLAIN; |
171 |
|
|
} |
172 |
|
|
if (login(&sockpri, ca->username, |
173 |
|
|
ca->password) == RESPONSE_NO) { |
174 |
|
|
error("username %s or password rejected " |
175 |
|
|
"at %s\n", ca->username, ca->server); |
176 |
|
|
continue; |
177 |
|
|
} |
178 |
|
|
} |
179 |
|
|
check_namespace(&sockpri, &nsppri); |
180 |
|
|
|
181 |
|
|
for (cm = ca->mboxes; cm != NULL; cm = cm->next) |
182 |
|
|
if (*cm->filters == NULL) |
183 |
|
|
mailbox_status(&sockpri, cm->name, |
184 |
|
|
&nsppri); |
185 |
|
|
else if (!select_mailbox(&sockpri, cm->name, |
186 |
|
|
&nsppri)) { |
187 |
lefcha |
1.43 |
apply_filters(cm->name, cm->filters); |
188 |
lefcha |
1.42 |
close_mailbox(&sockpri); |
189 |
|
|
} |
190 |
|
|
logout(&sockpri); |
191 |
lefcha |
1.33 |
|
192 |
lefcha |
1.42 |
close_connection(&sockpri); |
193 |
lefcha |
1.33 |
} |
194 |
lefcha |
1.11 |
|
195 |
lefcha |
1.42 |
/* Fork if in daemon mode. */ |
196 |
|
|
if (f) { |
197 |
|
|
f = 0; |
198 |
|
|
pid = fork(); |
199 |
|
|
switch (pid) { |
200 |
|
|
case -1: |
201 |
|
|
fatal(ERROR_FORK, "forking; %s\n", |
202 |
|
|
strerror(errno)); |
203 |
|
|
break; |
204 |
|
|
case 0: |
205 |
|
|
lockfile_create(); |
206 |
|
|
corefile_disable(); |
207 |
lefcha |
1.44 |
flags |= FLAG_DAEMON_MODE; |
208 |
lefcha |
1.42 |
break; |
209 |
|
|
default: |
210 |
|
|
secmem_clear(); |
211 |
|
|
close_logfile(); |
212 |
|
|
exit(0); |
213 |
|
|
break; |
214 |
|
|
} |
215 |
lefcha |
1.46 |
} |
216 |
|
|
if (options & OPTION_DAEMON_MODE && |
217 |
|
|
flags & FLAG_SIGHUP_RECEIVED) { |
218 |
|
|
reread_config(confile); |
219 |
|
|
continue; |
220 |
lefcha |
1.42 |
} |
221 |
|
|
if (interval) |
222 |
|
|
sleep(interval); |
223 |
|
|
} while (options & OPTION_DAEMON_MODE && interval); |
224 |
lefcha |
1.17 |
|
225 |
lefcha |
1.42 |
secmem_clear(); |
226 |
|
|
close_logfile(); |
227 |
lefcha |
1.33 |
|
228 |
lefcha |
1.42 |
lockfile_remove(); |
229 |
lefcha |
1.33 |
|
230 |
lefcha |
1.42 |
exit(0); |
231 |
lefcha |
1.1 |
} |
232 |
|
|
|
233 |
|
|
|
234 |
|
|
/* |
235 |
lefcha |
1.11 |
* Print a very brief usage message. |
236 |
lefcha |
1.2 |
*/ |
237 |
lefcha |
1.42 |
void |
238 |
|
|
usage(void) |
239 |
lefcha |
1.2 |
{ |
240 |
lefcha |
1.42 |
fprintf(stderr, |
241 |
lefcha |
1.29 |
"usage: imapfilter [-hk" |
242 |
lefcha |
1.26 |
#ifdef ENCRYPTED_PASSWORDS |
243 |
|
|
"p" |
244 |
lefcha |
1.33 |
#endif |
245 |
lefcha |
1.38 |
"qvV] [-c configfile] [-d interval] [-l logfile]\n"); |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
|
249 |
|
|
/* |
250 |
|
|
* Print program's version, and if it is built in, OpenSSL's version number. |
251 |
|
|
*/ |
252 |
lefcha |
1.42 |
void |
253 |
|
|
version(void) |
254 |
lefcha |
1.38 |
{ |
255 |
lefcha |
1.42 |
fprintf(stderr, "IMAPFilter %s" |
256 |
lefcha |
1.38 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
257 |
|
|
", OpenSSL 0x%8.8lx" |
258 |
|
|
#endif |
259 |
|
|
"\n", IMAPFILTER_VERSION |
260 |
|
|
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
261 |
|
|
,SSLeay() |
262 |
|
|
#endif |
263 |
lefcha |
1.42 |
); |
264 |
lefcha |
1.1 |
} |