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