4 |
#include <string.h> |
#include <string.h> |
5 |
#include <limits.h> |
#include <limits.h> |
6 |
#include <errno.h> |
#include <errno.h> |
7 |
|
#include <setjmp.h> |
8 |
|
|
9 |
|
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
10 |
|
#include <openssl/crypto.h> |
11 |
|
#endif |
12 |
|
|
13 |
#include "config.h" |
#include "config.h" |
14 |
#include "imapfilter.h" |
#include "imapfilter.h" |
26 |
unsigned int interval = 0; /* Poll at the specified interval. */ |
unsigned int interval = 0; /* Poll at the specified interval. */ |
27 |
char logfile[PATH_MAX]; /* Log file. */ |
char logfile[PATH_MAX]; /* Log file. */ |
28 |
char *home = NULL; /* User's home directory. */ |
char *home = NULL; /* User's home directory. */ |
29 |
|
#ifdef MEMORY_LOCK |
30 |
uid_t ruid, euid; /* Real and effective UID. */ |
uid_t ruid, euid; /* Real and effective UID. */ |
31 |
|
#endif |
32 |
|
jmp_buf acctloop; |
33 |
|
|
34 |
|
|
35 |
/* |
/* |
43 |
account_t *ca; /* Current account. */ |
account_t *ca; /* Current account. */ |
44 |
mbox_t *cm; /* Current mailbox. */ |
mbox_t *cm; /* Current mailbox. */ |
45 |
|
|
46 |
|
#ifdef MEMORY_LOCK |
47 |
ruid = getuid(); |
ruid = getuid(); |
48 |
euid = geteuid(); |
euid = geteuid(); |
49 |
seteuid(ruid); /* Drop root privileges. */ |
seteuid(ruid); /* Drop root privileges. */ |
50 |
|
#endif |
51 |
|
|
52 |
home = getenv("HOME"); |
home = getenv("HOME"); |
53 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_WARNING); |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_WARNING); |
57 |
#ifdef ENCRYPTED_PASSWORDS |
#ifdef ENCRYPTED_PASSWORDS |
58 |
"p" |
"p" |
59 |
#endif |
#endif |
60 |
"qv")) != -1) { |
"qvV")) != -1) { |
61 |
switch (c) { |
switch (c) { |
62 |
case 'c': |
case 'c': |
63 |
confile = optarg; |
confile = optarg; |
92 |
options &= OPTION_DETAILS_CLEAR; |
options &= OPTION_DETAILS_CLEAR; |
93 |
options |= OPTION_DETAILS_VERBOSE; |
options |= OPTION_DETAILS_VERBOSE; |
94 |
break; |
break; |
95 |
|
case 'V': |
96 |
|
version(); |
97 |
|
exit(ERROR_UNDEFINED); |
98 |
|
break; |
99 |
default: |
default: |
100 |
usage(); |
usage(); |
101 |
exit(ERROR_UNDEFINED); |
exit(ERROR_UNDEFINED); |
132 |
|
|
133 |
init_vbuf(); |
init_vbuf(); |
134 |
|
|
|
|
|
135 |
if (options & OPTION_DAEMON_MODE) { |
if (options & OPTION_DAEMON_MODE) { |
136 |
f = 1; |
f = 1; |
137 |
options &= OPTION_DETAILS_CLEAR; |
options &= OPTION_DETAILS_CLEAR; |
140 |
do { |
do { |
141 |
for (ca = accounts; ca; ca = ca->next) { |
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)) |
if (init_connection(&sockpri, ca->server, ca->port, ca->ssl)) |
147 |
continue; |
continue; |
148 |
|
|
149 |
r = greeting_response(&sockpri); |
r = greeting_response(&sockpri); |
150 |
|
|
151 |
if (r == RESPONSE_BYE || check_capabilities(&sockpri)) |
if (check_capabilities(&sockpri)) |
152 |
continue; |
continue; |
153 |
|
|
154 |
#ifdef DEBUG |
#ifdef DEBUG |
157 |
|
|
158 |
if (r != RESPONSE_PREAUTH) { |
if (r != RESPONSE_PREAUTH) { |
159 |
if (ca->passwdattr == PASSWORD_NONE) { |
if (ca->passwdattr == PASSWORD_NONE) { |
160 |
printf("Enter password for %s@%s: ", ca->username, ca->server); |
printf("Enter password for %s@%s: ", ca->username, |
161 |
|
ca->server); |
162 |
get_password(ca->password, PASSWORD_LEN); |
get_password(ca->password, PASSWORD_LEN); |
163 |
ca->passwdattr = PASSWORD_PLAIN; |
ca->passwdattr = PASSWORD_PLAIN; |
164 |
} |
} |
165 |
if (login(&sockpri, ca->username, ca->password) == RESPONSE_NO) { |
if (login(&sockpri, ca->username, ca->password) == |
166 |
error("imapfilter: username %s or password rejected at %s\n", |
RESPONSE_NO) { |
167 |
ca->username, ca->server); |
error("imapfilter: username %s or password rejected " |
168 |
|
"at %s\n", ca->username, ca->server); |
169 |
continue; |
continue; |
170 |
} |
} |
171 |
} |
} |
192 |
fatal(ERROR_FORK, "imapfilter: forking; %s\n", strerror(errno)); |
fatal(ERROR_FORK, "imapfilter: forking; %s\n", strerror(errno)); |
193 |
break; |
break; |
194 |
case 0: |
case 0: |
195 |
|
#ifdef MEMORY_LOCK |
196 |
secmem_lock(); |
secmem_lock(); |
197 |
setuid(ruid); /* Capability to regain root privileges will |
setuid(ruid); /* Capability to regain root privileges will |
198 |
not be needed any more. */ |
not be needed any more. */ |
199 |
|
#endif |
200 |
lockfile_create(); |
lockfile_create(); |
201 |
corefile_disable(); |
corefile_disable(); |
202 |
break; |
break; |
230 |
#ifdef ENCRYPTED_PASSWORDS |
#ifdef ENCRYPTED_PASSWORDS |
231 |
"p" |
"p" |
232 |
#endif |
#endif |
233 |
"qv] [-c configfile] [-d interval] [-l logfile]\n"); |
"qvV] [-c configfile] [-d interval] [-l logfile]\n"); |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
/* |
238 |
|
* Print program's version, and if it is built in, OpenSSL's version number. |
239 |
|
*/ |
240 |
|
void version(void) |
241 |
|
{ |
242 |
|
fprintf(stderr, "IMAPFilter %s" |
243 |
|
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
244 |
|
", OpenSSL 0x%8.8lx" |
245 |
|
#endif |
246 |
|
"\n", IMAPFILTER_VERSION |
247 |
|
#if defined SSL_TLS || defined ENCRYPTED_PASSWORDS |
248 |
|
,SSLeay() |
249 |
|
#endif |
250 |
|
); |
251 |
} |
} |