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.26 |
uid_t ruid, euid; /* Real and effective UID. */ |
26 |
lefcha |
1.35 |
jmp_buf acctloop; |
27 |
lefcha |
1.1 |
|
28 |
lefcha |
1.29 |
|
29 |
lefcha |
1.2 |
/* |
30 |
lefcha |
1.8 |
* In the beginning there was main()... |
31 |
lefcha |
1.2 |
*/ |
32 |
lefcha |
1.16 |
int main(int argc, char *argv[]) |
33 |
lefcha |
1.1 |
{ |
34 |
lefcha |
1.29 |
int c, r, f = 0; |
35 |
|
|
pid_t pid; |
36 |
lefcha |
1.11 |
char *confile = NULL; /* Configuration file. */ |
37 |
|
|
account_t *ca; /* Current account. */ |
38 |
|
|
mbox_t *cm; /* Current mailbox. */ |
39 |
lefcha |
1.33 |
|
40 |
lefcha |
1.26 |
ruid = getuid(); |
41 |
|
|
euid = geteuid(); |
42 |
|
|
seteuid(ruid); /* Drop root privileges. */ |
43 |
lefcha |
1.28 |
|
44 |
|
|
home = getenv("HOME"); |
45 |
lefcha |
1.34 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_WARNING); |
46 |
lefcha |
1.12 |
*logfile = 0; |
47 |
lefcha |
1.33 |
|
48 |
lefcha |
1.29 |
while ((c = getopt(argc, argv, "c:d:hkl:" |
49 |
|
|
#ifdef ENCRYPTED_PASSWORDS |
50 |
|
|
"p" |
51 |
lefcha |
1.25 |
#endif |
52 |
lefcha |
1.29 |
"qv")) != -1) { |
53 |
lefcha |
1.1 |
switch (c) { |
54 |
lefcha |
1.3 |
case 'c': |
55 |
lefcha |
1.11 |
confile = optarg; |
56 |
lefcha |
1.3 |
break; |
57 |
lefcha |
1.29 |
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 |
lefcha |
1.2 |
case 'h': |
65 |
|
|
usage(); |
66 |
lefcha |
1.12 |
exit(ERROR_UNDEFINED); |
67 |
lefcha |
1.2 |
break; |
68 |
lefcha |
1.29 |
case 'k': |
69 |
|
|
kill_imapfilter(); |
70 |
|
|
break; |
71 |
lefcha |
1.1 |
case 'l': |
72 |
lefcha |
1.12 |
strncat(logfile, optarg, PATH_MAX - 1); |
73 |
lefcha |
1.1 |
break; |
74 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
75 |
|
|
case 'p': |
76 |
|
|
options |= OPTION_PASSWORD_EDITOR; |
77 |
|
|
break; |
78 |
|
|
#endif |
79 |
lefcha |
1.1 |
case 'q': |
80 |
lefcha |
1.11 |
options &= OPTION_DETAILS_CLEAR; |
81 |
lefcha |
1.15 |
options |= OPTION_DETAILS_QUIET; |
82 |
lefcha |
1.1 |
break; |
83 |
|
|
case 'v': |
84 |
lefcha |
1.11 |
options &= OPTION_DETAILS_CLEAR; |
85 |
|
|
options |= OPTION_DETAILS_VERBOSE; |
86 |
lefcha |
1.1 |
break; |
87 |
|
|
default: |
88 |
lefcha |
1.2 |
usage(); |
89 |
lefcha |
1.12 |
exit(ERROR_UNDEFINED); |
90 |
lefcha |
1.1 |
break; |
91 |
|
|
} |
92 |
|
|
} |
93 |
lefcha |
1.32 |
|
94 |
|
|
create_homedir(); |
95 |
lefcha |
1.33 |
|
96 |
lefcha |
1.28 |
lockfile_check(); |
97 |
|
|
lockfile_create(); |
98 |
|
|
|
99 |
|
|
corefile_disable(); |
100 |
lefcha |
1.33 |
|
101 |
lefcha |
1.26 |
tty_store(); |
102 |
lefcha |
1.24 |
catch_signals(); |
103 |
|
|
|
104 |
lefcha |
1.12 |
read_config(confile); |
105 |
lefcha |
1.33 |
|
106 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
107 |
|
|
read_passwords(); |
108 |
lefcha |
1.33 |
|
109 |
lefcha |
1.32 |
if ((options & OPTION_PASSWORD_EDITOR)) { |
110 |
lefcha |
1.25 |
password_editor(); |
111 |
lefcha |
1.33 |
|
112 |
lefcha |
1.32 |
secmem_clear(); |
113 |
|
|
lockfile_remove(); |
114 |
lefcha |
1.33 |
|
115 |
lefcha |
1.32 |
exit(0); |
116 |
|
|
} |
117 |
lefcha |
1.25 |
#endif |
118 |
lefcha |
1.33 |
|
119 |
lefcha |
1.32 |
open_logfile(); |
120 |
|
|
|
121 |
|
|
init_vbuf(); |
122 |
|
|
|
123 |
lefcha |
1.29 |
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 |
lefcha |
1.7 |
|
131 |
lefcha |
1.35 |
if (setjmp(acctloop)) |
132 |
|
|
continue; |
133 |
|
|
|
134 |
lefcha |
1.33 |
if (init_connection(&sockpri, ca->server, ca->port, ca->ssl)) |
135 |
|
|
continue; |
136 |
lefcha |
1.21 |
|
137 |
lefcha |
1.33 |
r = greeting_response(&sockpri); |
138 |
lefcha |
1.18 |
|
139 |
lefcha |
1.35 |
if (check_capabilities(&sockpri)) |
140 |
lefcha |
1.33 |
continue; |
141 |
lefcha |
1.1 |
|
142 |
|
|
#ifdef DEBUG |
143 |
lefcha |
1.33 |
test(&sockpri); |
144 |
lefcha |
1.1 |
#endif |
145 |
|
|
|
146 |
lefcha |
1.33 |
if (r != RESPONSE_PREAUTH) { |
147 |
|
|
if (ca->passwdattr == PASSWORD_NONE) { |
148 |
lefcha |
1.35 |
printf("Enter password for %s@%s: ", ca->username, |
149 |
|
|
ca->server); |
150 |
lefcha |
1.33 |
get_password(ca->password, PASSWORD_LEN); |
151 |
|
|
ca->passwdattr = PASSWORD_PLAIN; |
152 |
|
|
} |
153 |
lefcha |
1.35 |
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 |
lefcha |
1.33 |
continue; |
158 |
|
|
} |
159 |
lefcha |
1.30 |
} |
160 |
lefcha |
1.33 |
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 |
lefcha |
1.24 |
} |
173 |
lefcha |
1.11 |
|
174 |
lefcha |
1.33 |
/* 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 |
lefcha |
1.14 |
} |
195 |
lefcha |
1.29 |
} |
196 |
lefcha |
1.33 |
if (interval) |
197 |
|
|
sleep(interval); |
198 |
lefcha |
1.29 |
} while (options & OPTION_DAEMON_MODE && interval); |
199 |
lefcha |
1.17 |
|
200 |
lefcha |
1.26 |
secmem_clear(); |
201 |
lefcha |
1.1 |
close_logfile(); |
202 |
lefcha |
1.33 |
|
203 |
lefcha |
1.28 |
lockfile_remove(); |
204 |
lefcha |
1.33 |
|
205 |
lefcha |
1.1 |
exit(0); |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
|
209 |
|
|
/* |
210 |
lefcha |
1.11 |
* Print a very brief usage message. |
211 |
lefcha |
1.2 |
*/ |
212 |
|
|
void usage(void) |
213 |
|
|
{ |
214 |
lefcha |
1.25 |
fprintf(stderr, |
215 |
lefcha |
1.29 |
"usage: imapfilter [-hk" |
216 |
lefcha |
1.26 |
#ifdef ENCRYPTED_PASSWORDS |
217 |
|
|
"p" |
218 |
lefcha |
1.33 |
#endif |
219 |
lefcha |
1.29 |
"qv] [-c configfile] [-d interval] [-l logfile]\n"); |
220 |
lefcha |
1.1 |
} |