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