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 capabilities; /* Capabilities of mail server. */ |
27 |
unsigned int interval = 0; /* Poll at the specified interval. */ |
28 |
|
29 |
char authmech[AUTH_MECH_LEN]; /* Authentication mechanisms. */ |
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 |
#ifdef MEMORY_LOCK |
34 |
uid_t ruid, euid; /* Real and effective UID. */ |
35 |
#endif |
36 |
jmp_buf acctloop; |
37 |
|
38 |
|
39 |
/* |
40 |
* In the beginning there was main()... |
41 |
*/ |
42 |
int main(int argc, char *argv[]) |
43 |
{ |
44 |
int c, r, f = 0; |
45 |
pid_t pid; |
46 |
char *confile = NULL; /* Configuration file. */ |
47 |
account_t *ca; /* Current account. */ |
48 |
mbox_t *cm; /* Current mailbox. */ |
49 |
|
50 |
#ifdef MEMORY_LOCK |
51 |
ruid = getuid(); |
52 |
euid = geteuid(); |
53 |
seteuid(ruid); /* Drop root privileges. */ |
54 |
#endif |
55 |
|
56 |
setlocale(LC_CTYPE, ""); |
57 |
|
58 |
home = getenv("HOME"); |
59 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_WARNING); |
60 |
*charset = 0; |
61 |
*logfile = 0; |
62 |
|
63 |
while ((c = getopt(argc, argv, "c:d:hkl:" |
64 |
#ifdef ENCRYPTED_PASSWORDS |
65 |
"p" |
66 |
#endif |
67 |
"qvV")) != -1) { |
68 |
switch (c) { |
69 |
case 'c': |
70 |
confile = optarg; |
71 |
break; |
72 |
case 'd': |
73 |
options |= OPTION_DAEMON_MODE; |
74 |
errno = 0; |
75 |
interval = strtoul(optarg, NULL, 10); |
76 |
if (errno) |
77 |
interval = 0; |
78 |
break; |
79 |
case 'h': |
80 |
usage(); |
81 |
exit(ERROR_UNDEFINED); |
82 |
break; |
83 |
case 'k': |
84 |
kill_imapfilter(); |
85 |
break; |
86 |
case 'l': |
87 |
strncat(logfile, optarg, PATH_MAX - 1); |
88 |
break; |
89 |
#ifdef ENCRYPTED_PASSWORDS |
90 |
case 'p': |
91 |
options |= OPTION_PASSWORD_EDITOR; |
92 |
break; |
93 |
#endif |
94 |
case 'q': |
95 |
options &= OPTION_DETAILS_CLEAR; |
96 |
options |= OPTION_DETAILS_QUIET; |
97 |
break; |
98 |
case 'v': |
99 |
options &= OPTION_DETAILS_CLEAR; |
100 |
options |= OPTION_DETAILS_VERBOSE; |
101 |
break; |
102 |
case 'V': |
103 |
version(); |
104 |
exit(ERROR_UNDEFINED); |
105 |
break; |
106 |
default: |
107 |
usage(); |
108 |
exit(ERROR_UNDEFINED); |
109 |
break; |
110 |
} |
111 |
} |
112 |
|
113 |
create_homedir(); |
114 |
|
115 |
lockfile_check(); |
116 |
lockfile_create(); |
117 |
|
118 |
corefile_disable(); |
119 |
|
120 |
tty_store(); |
121 |
catch_signals(); |
122 |
|
123 |
read_config(confile); |
124 |
|
125 |
#ifdef ENCRYPTED_PASSWORDS |
126 |
read_passwords(); |
127 |
|
128 |
if ((options & OPTION_PASSWORD_EDITOR)) { |
129 |
password_editor(); |
130 |
|
131 |
secmem_clear(); |
132 |
lockfile_remove(); |
133 |
|
134 |
exit(0); |
135 |
} |
136 |
#endif |
137 |
|
138 |
open_logfile(); |
139 |
|
140 |
init_vbuf(); |
141 |
|
142 |
if (options & OPTION_DAEMON_MODE) { |
143 |
f = 1; |
144 |
options &= OPTION_DETAILS_CLEAR; |
145 |
options |= OPTION_DETAILS_QUIET; |
146 |
} |
147 |
do { |
148 |
for (ca = accounts; ca; ca = ca->next) { |
149 |
|
150 |
if (setjmp(acctloop)) |
151 |
continue; |
152 |
|
153 |
if (init_connection(&sockpri, ca->server, ca->port, ca->ssl)) |
154 |
continue; |
155 |
|
156 |
r = greeting_response(&sockpri); |
157 |
|
158 |
if (check_capabilities(&sockpri)) |
159 |
continue; |
160 |
|
161 |
#ifdef DEBUG |
162 |
test(&sockpri); |
163 |
#endif |
164 |
|
165 |
if (r != RESPONSE_PREAUTH) { |
166 |
if (ca->passwdattr == PASSWORD_NONE) { |
167 |
printf("Enter password for %s@%s: ", ca->username, |
168 |
ca->server); |
169 |
get_password(ca->password, PASSWORD_LEN); |
170 |
ca->passwdattr = PASSWORD_PLAIN; |
171 |
} |
172 |
if (login(&sockpri, ca->username, ca->password) == |
173 |
RESPONSE_NO) { |
174 |
error("imapfilter: 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; cm = cm->next) |
182 |
if (!*cm->filters) |
183 |
mailbox_status(&sockpri, cm->name, &nsppri); |
184 |
else if (!select_mailbox(&sockpri, cm->name, &nsppri)) { |
185 |
apply_filters(cm->filters); |
186 |
close_mailbox(&sockpri); |
187 |
} |
188 |
logout(&sockpri); |
189 |
|
190 |
close_connection(&sockpri); |
191 |
} |
192 |
|
193 |
/* Fork if in daemon mode. */ |
194 |
if (f) { |
195 |
f = 0; |
196 |
pid = fork(); |
197 |
switch (pid) { |
198 |
case -1: |
199 |
fatal(ERROR_FORK, "imapfilter: forking; %s\n", strerror(errno)); |
200 |
break; |
201 |
case 0: |
202 |
#ifdef MEMORY_LOCK |
203 |
secmem_lock(); |
204 |
setuid(ruid); /* Capability to regain root privileges will |
205 |
not be needed any more. */ |
206 |
#endif |
207 |
lockfile_create(); |
208 |
corefile_disable(); |
209 |
break; |
210 |
default: |
211 |
secmem_clear(); |
212 |
close_logfile(); |
213 |
exit(0); |
214 |
break; |
215 |
} |
216 |
} |
217 |
if (interval) |
218 |
sleep(interval); |
219 |
} while (options & OPTION_DAEMON_MODE && interval); |
220 |
|
221 |
secmem_clear(); |
222 |
close_logfile(); |
223 |
|
224 |
lockfile_remove(); |
225 |
|
226 |
exit(0); |
227 |
} |
228 |
|
229 |
|
230 |
/* |
231 |
* Print a very brief usage message. |
232 |
*/ |
233 |
void usage(void) |
234 |
{ |
235 |
fprintf(stderr, |
236 |
"usage: imapfilter [-hk" |
237 |
#ifdef ENCRYPTED_PASSWORDS |
238 |
"p" |
239 |
#endif |
240 |
"qvV] [-c configfile] [-d interval] [-l logfile]\n"); |
241 |
} |
242 |
|
243 |
|
244 |
/* |
245 |
* Print program's version, and if it is built in, OpenSSL's version number. |
246 |
*/ |
247 |
void version(void) |
248 |
{ |
249 |
fprintf(stderr, "IMAPFilter %s" |
250 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
251 |
", OpenSSL 0x%8.8lx" |
252 |
#endif |
253 |
"\n", IMAPFILTER_VERSION |
254 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
255 |
,SSLeay() |
256 |
#endif |
257 |
); |
258 |
} |