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 |
|
9 |
#include "config.h" |
10 |
#include "imapfilter.h" |
11 |
#include "data.h" |
12 |
|
13 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
14 |
#include <openssl/crypto.h> |
15 |
#endif |
16 |
|
17 |
|
18 |
extern int sockpri; |
19 |
extern account_t *accounts; |
20 |
extern filter_t *filters; |
21 |
extern namesp_t nsppri; |
22 |
|
23 |
unsigned int options; /* Program options. */ |
24 |
unsigned int flags = 0; /* Program flags. */ |
25 |
unsigned int capabilities; /* Capabilities of mail server. */ |
26 |
unsigned int interval = 0; /* Poll at the specified interval. */ |
27 |
char logfile[PATH_MAX]; /* Log file. */ |
28 |
char *home = NULL; /* User's home directory. */ |
29 |
#ifdef MEMORY_LOCK |
30 |
uid_t ruid, euid; /* Real and effective UID. */ |
31 |
#endif |
32 |
jmp_buf acctloop; |
33 |
|
34 |
|
35 |
/* |
36 |
* In the beginning there was main()... |
37 |
*/ |
38 |
int main(int argc, char *argv[]) |
39 |
{ |
40 |
int c, r, f = 0; |
41 |
pid_t pid; |
42 |
char *confile = NULL; /* Configuration file. */ |
43 |
account_t *ca; /* Current account. */ |
44 |
mbox_t *cm; /* Current mailbox. */ |
45 |
|
46 |
#ifdef MEMORY_LOCK |
47 |
ruid = getuid(); |
48 |
euid = geteuid(); |
49 |
seteuid(ruid); /* Drop root privileges. */ |
50 |
#endif |
51 |
|
52 |
home = getenv("HOME"); |
53 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_WARNING); |
54 |
*logfile = 0; |
55 |
|
56 |
while ((c = getopt(argc, argv, "c:d:hkl:" |
57 |
#ifdef ENCRYPTED_PASSWORDS |
58 |
"p" |
59 |
#endif |
60 |
"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 |
#ifdef ENCRYPTED_PASSWORDS |
83 |
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 |
} |
105 |
|
106 |
create_homedir(); |
107 |
|
108 |
lockfile_check(); |
109 |
lockfile_create(); |
110 |
|
111 |
corefile_disable(); |
112 |
|
113 |
tty_store(); |
114 |
catch_signals(); |
115 |
|
116 |
read_config(confile); |
117 |
|
118 |
#ifdef ENCRYPTED_PASSWORDS |
119 |
read_passwords(); |
120 |
|
121 |
if ((options & OPTION_PASSWORD_EDITOR)) { |
122 |
password_editor(); |
123 |
|
124 |
secmem_clear(); |
125 |
lockfile_remove(); |
126 |
|
127 |
exit(0); |
128 |
} |
129 |
#endif |
130 |
|
131 |
open_logfile(); |
132 |
|
133 |
init_vbuf(); |
134 |
|
135 |
if (options & OPTION_DAEMON_MODE) { |
136 |
f = 1; |
137 |
options &= OPTION_DETAILS_CLEAR; |
138 |
options |= OPTION_DETAILS_QUIET; |
139 |
} |
140 |
do { |
141 |
for (ca = accounts; ca; ca = ca->next) { |
142 |
|
143 |
if (setjmp(acctloop)) |
144 |
continue; |
145 |
|
146 |
if (init_connection(&sockpri, ca->server, ca->port, ca->ssl)) |
147 |
continue; |
148 |
|
149 |
r = greeting_response(&sockpri); |
150 |
|
151 |
if (check_capabilities(&sockpri)) |
152 |
continue; |
153 |
|
154 |
log_info(LOG_ACCOUNT, ca->key); |
155 |
|
156 |
#ifdef DEBUG |
157 |
test(&sockpri); |
158 |
#endif |
159 |
|
160 |
if (r != RESPONSE_PREAUTH) { |
161 |
if (ca->passwdattr == PASSWORD_NONE) { |
162 |
printf("Enter password for %s@%s: ", ca->username, |
163 |
ca->server); |
164 |
get_password(ca->password, PASSWORD_LEN); |
165 |
ca->passwdattr = PASSWORD_PLAIN; |
166 |
} |
167 |
if (login(&sockpri, ca->username, ca->password) == |
168 |
RESPONSE_NO) { |
169 |
error("imapfilter: username %s or password rejected " |
170 |
"at %s\n", ca->username, ca->server); |
171 |
continue; |
172 |
} |
173 |
} |
174 |
check_namespace(&sockpri, &nsppri); |
175 |
|
176 |
for (cm = ca->mboxes; cm; cm = cm->next) |
177 |
if (!*cm->filters) |
178 |
mailbox_status(&sockpri, cm->name, &nsppri); |
179 |
else if (!select_mailbox(&sockpri, cm->name, &nsppri)) { |
180 |
apply_filters(cm->filters); |
181 |
close_mailbox(&sockpri); |
182 |
} |
183 |
logout(&sockpri); |
184 |
|
185 |
close_connection(&sockpri); |
186 |
} |
187 |
|
188 |
/* Fork if in daemon mode. */ |
189 |
if (f) { |
190 |
f = 0; |
191 |
pid = fork(); |
192 |
switch (pid) { |
193 |
case -1: |
194 |
fatal(ERROR_FORK, "imapfilter: forking; %s\n", strerror(errno)); |
195 |
break; |
196 |
case 0: |
197 |
#ifdef MEMORY_LOCK |
198 |
secmem_lock(); |
199 |
setuid(ruid); /* Capability to regain root privileges will |
200 |
not be needed any more. */ |
201 |
#endif |
202 |
lockfile_create(); |
203 |
corefile_disable(); |
204 |
break; |
205 |
default: |
206 |
secmem_clear(); |
207 |
close_logfile(); |
208 |
exit(0); |
209 |
break; |
210 |
} |
211 |
} |
212 |
if (interval) |
213 |
sleep(interval); |
214 |
} while (options & OPTION_DAEMON_MODE && interval); |
215 |
|
216 |
secmem_clear(); |
217 |
close_logfile(); |
218 |
|
219 |
lockfile_remove(); |
220 |
|
221 |
exit(0); |
222 |
} |
223 |
|
224 |
|
225 |
/* |
226 |
* Print a very brief usage message. |
227 |
*/ |
228 |
void usage(void) |
229 |
{ |
230 |
fprintf(stderr, |
231 |
"usage: imapfilter [-hk" |
232 |
#ifdef ENCRYPTED_PASSWORDS |
233 |
"p" |
234 |
#endif |
235 |
"qvV] [-c configfile] [-d interval] [-l logfile]\n"); |
236 |
} |
237 |
|
238 |
|
239 |
/* |
240 |
* Print program's version, and if it is built in, OpenSSL's version number. |
241 |
*/ |
242 |
void version(void) |
243 |
{ |
244 |
fprintf(stderr, "IMAPFilter %s" |
245 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
246 |
", OpenSSL 0x%8.8lx" |
247 |
#endif |
248 |
"\n", IMAPFILTER_VERSION |
249 |
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
250 |
,SSLeay() |
251 |
#endif |
252 |
); |
253 |
} |