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 |
|
14 |
extern int sockpri; |
15 |
extern account_t *accounts; |
16 |
extern filter_t *filters; |
17 |
extern namesp_t nsppri; |
18 |
|
19 |
unsigned int options; /* Program options. */ |
20 |
unsigned int flags = 0; /* Program flags. */ |
21 |
unsigned int capabilities; /* Capabilities of mail server. */ |
22 |
unsigned int interval = 0; /* Poll at the specified interval. */ |
23 |
char logfile[PATH_MAX]; /* Log file. */ |
24 |
char *home = NULL; /* User's home directory. */ |
25 |
uid_t ruid, euid; /* Real and effective UID. */ |
26 |
jmp_buf acctloop; |
27 |
|
28 |
|
29 |
/* |
30 |
* In the beginning there was main()... |
31 |
*/ |
32 |
int main(int argc, char *argv[]) |
33 |
{ |
34 |
int c, r, f = 0; |
35 |
pid_t pid; |
36 |
char *confile = NULL; /* Configuration file. */ |
37 |
account_t *ca; /* Current account. */ |
38 |
mbox_t *cm; /* Current mailbox. */ |
39 |
|
40 |
ruid = getuid(); |
41 |
euid = geteuid(); |
42 |
seteuid(ruid); /* Drop root privileges. */ |
43 |
|
44 |
home = getenv("HOME"); |
45 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_WARNING); |
46 |
*logfile = 0; |
47 |
|
48 |
while ((c = getopt(argc, argv, "c:d:hkl:" |
49 |
#ifdef ENCRYPTED_PASSWORDS |
50 |
"p" |
51 |
#endif |
52 |
"qv")) != -1) { |
53 |
switch (c) { |
54 |
case 'c': |
55 |
confile = optarg; |
56 |
break; |
57 |
case 'd': |
58 |
options |= OPTION_DAEMON_MODE; |
59 |
errno = 0; |
60 |
interval = strtoul(optarg, NULL, 10); |
61 |
if (errno) |
62 |
interval = 0; |
63 |
break; |
64 |
case 'h': |
65 |
usage(); |
66 |
exit(ERROR_UNDEFINED); |
67 |
break; |
68 |
case 'k': |
69 |
kill_imapfilter(); |
70 |
break; |
71 |
case 'l': |
72 |
strncat(logfile, optarg, PATH_MAX - 1); |
73 |
break; |
74 |
#ifdef ENCRYPTED_PASSWORDS |
75 |
case 'p': |
76 |
options |= OPTION_PASSWORD_EDITOR; |
77 |
break; |
78 |
#endif |
79 |
case 'q': |
80 |
options &= OPTION_DETAILS_CLEAR; |
81 |
options |= OPTION_DETAILS_QUIET; |
82 |
break; |
83 |
case 'v': |
84 |
options &= OPTION_DETAILS_CLEAR; |
85 |
options |= OPTION_DETAILS_VERBOSE; |
86 |
break; |
87 |
default: |
88 |
usage(); |
89 |
exit(ERROR_UNDEFINED); |
90 |
break; |
91 |
} |
92 |
} |
93 |
|
94 |
create_homedir(); |
95 |
|
96 |
lockfile_check(); |
97 |
lockfile_create(); |
98 |
|
99 |
corefile_disable(); |
100 |
|
101 |
tty_store(); |
102 |
catch_signals(); |
103 |
|
104 |
read_config(confile); |
105 |
|
106 |
#ifdef ENCRYPTED_PASSWORDS |
107 |
read_passwords(); |
108 |
|
109 |
if ((options & OPTION_PASSWORD_EDITOR)) { |
110 |
password_editor(); |
111 |
|
112 |
secmem_clear(); |
113 |
lockfile_remove(); |
114 |
|
115 |
exit(0); |
116 |
} |
117 |
#endif |
118 |
|
119 |
open_logfile(); |
120 |
|
121 |
init_vbuf(); |
122 |
|
123 |
if (options & OPTION_DAEMON_MODE) { |
124 |
f = 1; |
125 |
options &= OPTION_DETAILS_CLEAR; |
126 |
options |= OPTION_DETAILS_QUIET; |
127 |
} |
128 |
do { |
129 |
for (ca = accounts; ca; ca = ca->next) { |
130 |
|
131 |
if (setjmp(acctloop)) |
132 |
continue; |
133 |
|
134 |
if (init_connection(&sockpri, ca->server, ca->port, ca->ssl)) |
135 |
continue; |
136 |
|
137 |
r = greeting_response(&sockpri); |
138 |
|
139 |
if (check_capabilities(&sockpri)) |
140 |
continue; |
141 |
|
142 |
#ifdef DEBUG |
143 |
test(&sockpri); |
144 |
#endif |
145 |
|
146 |
if (r != RESPONSE_PREAUTH) { |
147 |
if (ca->passwdattr == PASSWORD_NONE) { |
148 |
printf("Enter password for %s@%s: ", ca->username, |
149 |
ca->server); |
150 |
get_password(ca->password, PASSWORD_LEN); |
151 |
ca->passwdattr = PASSWORD_PLAIN; |
152 |
} |
153 |
if (login(&sockpri, ca->username, ca->password) == |
154 |
RESPONSE_NO) { |
155 |
error("imapfilter: username %s or password rejected " |
156 |
"at %s\n", ca->username, ca->server); |
157 |
continue; |
158 |
} |
159 |
} |
160 |
check_namespace(&sockpri, &nsppri); |
161 |
|
162 |
for (cm = ca->mboxes; cm; cm = cm->next) |
163 |
if (!*cm->filters) |
164 |
mailbox_status(&sockpri, cm->name, &nsppri); |
165 |
else if (!select_mailbox(&sockpri, cm->name, &nsppri)) { |
166 |
apply_filters(cm->filters); |
167 |
close_mailbox(&sockpri); |
168 |
} |
169 |
logout(&sockpri); |
170 |
|
171 |
close_connection(&sockpri); |
172 |
} |
173 |
|
174 |
/* Fork if in daemon mode. */ |
175 |
if (f) { |
176 |
f = 0; |
177 |
pid = fork(); |
178 |
switch (pid) { |
179 |
case -1: |
180 |
fatal(ERROR_FORK, "imapfilter: forking; %s\n", strerror(errno)); |
181 |
break; |
182 |
case 0: |
183 |
secmem_lock(); |
184 |
setuid(ruid); /* Capability to regain root privileges will |
185 |
not be needed any more. */ |
186 |
lockfile_create(); |
187 |
corefile_disable(); |
188 |
break; |
189 |
default: |
190 |
secmem_clear(); |
191 |
close_logfile(); |
192 |
exit(0); |
193 |
break; |
194 |
} |
195 |
} |
196 |
if (interval) |
197 |
sleep(interval); |
198 |
} while (options & OPTION_DAEMON_MODE && interval); |
199 |
|
200 |
secmem_clear(); |
201 |
close_logfile(); |
202 |
|
203 |
lockfile_remove(); |
204 |
|
205 |
exit(0); |
206 |
} |
207 |
|
208 |
|
209 |
/* |
210 |
* Print a very brief usage message. |
211 |
*/ |
212 |
void usage(void) |
213 |
{ |
214 |
fprintf(stderr, |
215 |
"usage: imapfilter [-hk" |
216 |
#ifdef ENCRYPTED_PASSWORDS |
217 |
"p" |
218 |
#endif |
219 |
"qv] [-c configfile] [-d interval] [-l logfile]\n"); |
220 |
} |