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