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.36 |
#ifdef MEMORY_LOCK |
34 |
lefcha |
1.26 |
uid_t ruid, euid; /* Real and effective UID. */ |
35 |
lefcha |
1.37 |
#endif |
36 |
lefcha |
1.35 |
jmp_buf acctloop; |
37 |
lefcha |
1.1 |
|
38 |
lefcha |
1.29 |
|
39 |
lefcha |
1.2 |
/* |
40 |
lefcha |
1.8 |
* In the beginning there was main()... |
41 |
lefcha |
1.2 |
*/ |
42 |
lefcha |
1.16 |
int main(int argc, char *argv[]) |
43 |
lefcha |
1.1 |
{ |
44 |
lefcha |
1.29 |
int c, r, f = 0; |
45 |
|
|
pid_t pid; |
46 |
lefcha |
1.11 |
char *confile = NULL; /* Configuration file. */ |
47 |
|
|
account_t *ca; /* Current account. */ |
48 |
|
|
mbox_t *cm; /* Current mailbox. */ |
49 |
lefcha |
1.33 |
|
50 |
lefcha |
1.36 |
#ifdef MEMORY_LOCK |
51 |
lefcha |
1.26 |
ruid = getuid(); |
52 |
|
|
euid = geteuid(); |
53 |
|
|
seteuid(ruid); /* Drop root privileges. */ |
54 |
lefcha |
1.36 |
#endif |
55 |
lefcha |
1.28 |
|
56 |
lefcha |
1.40 |
setlocale(LC_CTYPE, ""); |
57 |
|
|
|
58 |
lefcha |
1.28 |
home = getenv("HOME"); |
59 |
lefcha |
1.34 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_WARNING); |
60 |
lefcha |
1.40 |
*charset = 0; |
61 |
lefcha |
1.12 |
*logfile = 0; |
62 |
lefcha |
1.33 |
|
63 |
lefcha |
1.29 |
while ((c = getopt(argc, argv, "c:d:hkl:" |
64 |
|
|
#ifdef ENCRYPTED_PASSWORDS |
65 |
|
|
"p" |
66 |
lefcha |
1.25 |
#endif |
67 |
lefcha |
1.38 |
"qvV")) != -1) { |
68 |
lefcha |
1.1 |
switch (c) { |
69 |
lefcha |
1.3 |
case 'c': |
70 |
lefcha |
1.11 |
confile = optarg; |
71 |
lefcha |
1.3 |
break; |
72 |
lefcha |
1.29 |
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 |
lefcha |
1.2 |
case 'h': |
80 |
|
|
usage(); |
81 |
lefcha |
1.12 |
exit(ERROR_UNDEFINED); |
82 |
lefcha |
1.2 |
break; |
83 |
lefcha |
1.29 |
case 'k': |
84 |
|
|
kill_imapfilter(); |
85 |
|
|
break; |
86 |
lefcha |
1.1 |
case 'l': |
87 |
lefcha |
1.12 |
strncat(logfile, optarg, PATH_MAX - 1); |
88 |
lefcha |
1.1 |
break; |
89 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
90 |
|
|
case 'p': |
91 |
|
|
options |= OPTION_PASSWORD_EDITOR; |
92 |
|
|
break; |
93 |
|
|
#endif |
94 |
lefcha |
1.1 |
case 'q': |
95 |
lefcha |
1.11 |
options &= OPTION_DETAILS_CLEAR; |
96 |
lefcha |
1.15 |
options |= OPTION_DETAILS_QUIET; |
97 |
lefcha |
1.1 |
break; |
98 |
|
|
case 'v': |
99 |
lefcha |
1.11 |
options &= OPTION_DETAILS_CLEAR; |
100 |
|
|
options |= OPTION_DETAILS_VERBOSE; |
101 |
lefcha |
1.1 |
break; |
102 |
lefcha |
1.38 |
case 'V': |
103 |
|
|
version(); |
104 |
|
|
exit(ERROR_UNDEFINED); |
105 |
|
|
break; |
106 |
lefcha |
1.1 |
default: |
107 |
lefcha |
1.2 |
usage(); |
108 |
lefcha |
1.12 |
exit(ERROR_UNDEFINED); |
109 |
lefcha |
1.1 |
break; |
110 |
|
|
} |
111 |
|
|
} |
112 |
lefcha |
1.32 |
|
113 |
|
|
create_homedir(); |
114 |
lefcha |
1.33 |
|
115 |
lefcha |
1.28 |
lockfile_check(); |
116 |
|
|
lockfile_create(); |
117 |
|
|
|
118 |
|
|
corefile_disable(); |
119 |
lefcha |
1.33 |
|
120 |
lefcha |
1.26 |
tty_store(); |
121 |
lefcha |
1.24 |
catch_signals(); |
122 |
|
|
|
123 |
lefcha |
1.12 |
read_config(confile); |
124 |
lefcha |
1.33 |
|
125 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
126 |
|
|
read_passwords(); |
127 |
lefcha |
1.33 |
|
128 |
lefcha |
1.32 |
if ((options & OPTION_PASSWORD_EDITOR)) { |
129 |
lefcha |
1.25 |
password_editor(); |
130 |
lefcha |
1.33 |
|
131 |
lefcha |
1.32 |
secmem_clear(); |
132 |
|
|
lockfile_remove(); |
133 |
lefcha |
1.33 |
|
134 |
lefcha |
1.32 |
exit(0); |
135 |
|
|
} |
136 |
lefcha |
1.25 |
#endif |
137 |
lefcha |
1.33 |
|
138 |
lefcha |
1.32 |
open_logfile(); |
139 |
|
|
|
140 |
|
|
init_vbuf(); |
141 |
|
|
|
142 |
lefcha |
1.29 |
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 |
lefcha |
1.7 |
|
150 |
lefcha |
1.35 |
if (setjmp(acctloop)) |
151 |
|
|
continue; |
152 |
|
|
|
153 |
lefcha |
1.33 |
if (init_connection(&sockpri, ca->server, ca->port, ca->ssl)) |
154 |
|
|
continue; |
155 |
lefcha |
1.21 |
|
156 |
lefcha |
1.33 |
r = greeting_response(&sockpri); |
157 |
lefcha |
1.18 |
|
158 |
lefcha |
1.35 |
if (check_capabilities(&sockpri)) |
159 |
lefcha |
1.33 |
continue; |
160 |
lefcha |
1.1 |
|
161 |
|
|
#ifdef DEBUG |
162 |
lefcha |
1.33 |
test(&sockpri); |
163 |
lefcha |
1.1 |
#endif |
164 |
|
|
|
165 |
lefcha |
1.33 |
if (r != RESPONSE_PREAUTH) { |
166 |
|
|
if (ca->passwdattr == PASSWORD_NONE) { |
167 |
lefcha |
1.35 |
printf("Enter password for %s@%s: ", ca->username, |
168 |
|
|
ca->server); |
169 |
lefcha |
1.33 |
get_password(ca->password, PASSWORD_LEN); |
170 |
|
|
ca->passwdattr = PASSWORD_PLAIN; |
171 |
|
|
} |
172 |
lefcha |
1.35 |
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 |
lefcha |
1.33 |
continue; |
177 |
|
|
} |
178 |
lefcha |
1.30 |
} |
179 |
lefcha |
1.33 |
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 |
lefcha |
1.24 |
} |
192 |
lefcha |
1.11 |
|
193 |
lefcha |
1.33 |
/* 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 |
lefcha |
1.36 |
#ifdef MEMORY_LOCK |
203 |
lefcha |
1.33 |
secmem_lock(); |
204 |
|
|
setuid(ruid); /* Capability to regain root privileges will |
205 |
|
|
not be needed any more. */ |
206 |
lefcha |
1.36 |
#endif |
207 |
lefcha |
1.33 |
lockfile_create(); |
208 |
|
|
corefile_disable(); |
209 |
|
|
break; |
210 |
|
|
default: |
211 |
|
|
secmem_clear(); |
212 |
|
|
close_logfile(); |
213 |
|
|
exit(0); |
214 |
|
|
break; |
215 |
lefcha |
1.14 |
} |
216 |
lefcha |
1.29 |
} |
217 |
lefcha |
1.33 |
if (interval) |
218 |
|
|
sleep(interval); |
219 |
lefcha |
1.29 |
} while (options & OPTION_DAEMON_MODE && interval); |
220 |
lefcha |
1.17 |
|
221 |
lefcha |
1.26 |
secmem_clear(); |
222 |
lefcha |
1.1 |
close_logfile(); |
223 |
lefcha |
1.33 |
|
224 |
lefcha |
1.28 |
lockfile_remove(); |
225 |
lefcha |
1.33 |
|
226 |
lefcha |
1.1 |
exit(0); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
|
230 |
|
|
/* |
231 |
lefcha |
1.11 |
* Print a very brief usage message. |
232 |
lefcha |
1.2 |
*/ |
233 |
|
|
void usage(void) |
234 |
|
|
{ |
235 |
lefcha |
1.25 |
fprintf(stderr, |
236 |
lefcha |
1.29 |
"usage: imapfilter [-hk" |
237 |
lefcha |
1.26 |
#ifdef ENCRYPTED_PASSWORDS |
238 |
|
|
"p" |
239 |
lefcha |
1.33 |
#endif |
240 |
lefcha |
1.38 |
"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 |
lefcha |
1.1 |
} |