/[imapfilter]/imapfilter/imapfilter.c
ViewVC logotype

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.61 - (hide annotations)
Mon Feb 9 18:23:49 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
Changes since 1.60: +5 -2 lines
File MIME type: text/plain
Remove permissions variable and add command line option -w to suppress related warning messages.

1 lefcha 1.1 #include <stdio.h>
2 lefcha 1.53 #include <stdlib.h>
3 lefcha 1.55 #include <sys/types.h>
4     #include <unistd.h>
5 lefcha 1.52 #include <string.h>
6     #include <errno.h>
7 lefcha 1.7 #include <limits.h>
8 lefcha 1.58 #include <fcntl.h>
9 lefcha 1.35 #include <setjmp.h>
10 lefcha 1.40 #include <locale.h>
11 lefcha 1.1
12 lefcha 1.39 #include "config.h"
13     #include "imapfilter.h"
14 lefcha 1.52 #include "version.h"
15     #include "account.h"
16     #include "filter.h"
17     #include "buffer.h"
18 lefcha 1.39
19 lefcha 1.53 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
20 lefcha 1.55 #include <openssl/opensslv.h>
21 lefcha 1.38 #include <openssl/crypto.h>
22     #endif
23 lefcha 1.1
24    
25 lefcha 1.7 extern account_t *accounts;
26 lefcha 1.11 extern filter_t *filters;
27 lefcha 1.50 extern buffer_t ibuf, obuf;
28 lefcha 1.7
29 lefcha 1.10 unsigned int options; /* Program options. */
30 lefcha 1.29 unsigned int flags = 0; /* Program flags. */
31     unsigned int interval = 0; /* Poll at the specified interval. */
32 lefcha 1.51 conn_t connpri, connaux; /* Primary and auxiliary IMAP connection. */
33 lefcha 1.41
34 lefcha 1.5 char logfile[PATH_MAX]; /* Log file. */
35 lefcha 1.25 char *home = NULL; /* User's home directory. */
36 lefcha 1.40 char charset[CHARSET_LEN]; /* Charset for IMAP SEARCH requests. */
37 lefcha 1.42
38 lefcha 1.46 jmp_buf acctloop; /* Non-local exit in case of network error. */
39 lefcha 1.1
40 lefcha 1.29
41 lefcha 1.52 void usage(void);
42     void version(void);
43    
44    
45 lefcha 1.2 /*
46 lefcha 1.46 * IMAPFilter: an IMAP mail filtering utility.
47 lefcha 1.2 */
48 lefcha 1.42 int
49     main(int argc, char *argv[])
50 lefcha 1.1 {
51 lefcha 1.58 int c, r;
52 lefcha 1.55 char *conffile; /* Configuration file. */
53 lefcha 1.42 account_t *ca; /* Current account. */
54     mbox_t *cm; /* Current mailbox. */
55 lefcha 1.33
56 lefcha 1.45 setlocale(LC_ALL, "");
57 lefcha 1.40
58 lefcha 1.42 home = getenv("HOME");
59 lefcha 1.59 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_PEEK |
60     OPTION_PERMISSIONS);
61 lefcha 1.42 *charset = 0;
62     *logfile = 0;
63 lefcha 1.55 conffile = NULL;
64     connpri.sock = connaux.sock = -1;
65 lefcha 1.33
66 lefcha 1.60 while ((c = getopt(argc, argv, "c:d:Dhkl:"
67 lefcha 1.29 #ifdef ENCRYPTED_PASSWORDS
68 lefcha 1.42 "p"
69 lefcha 1.25 #endif
70 lefcha 1.61 "qvVw")) != -1) {
71 lefcha 1.42 switch (c) {
72     case 'c':
73 lefcha 1.55 conffile = optarg;
74 lefcha 1.42 break;
75     case 'd':
76     options |= OPTION_DAEMON_MODE;
77     errno = 0;
78     interval = strtoul(optarg, NULL, 10);
79     if (errno)
80     interval = 0;
81     break;
82 lefcha 1.60 case 'D':
83     options |= OPTION_DEBUG;
84     break;
85 lefcha 1.42 case 'h':
86     usage();
87     exit(ERROR_UNDEFINED);
88     break;
89     case 'k':
90     kill_imapfilter();
91     break;
92     case 'l':
93     strncat(logfile, optarg, PATH_MAX - 1);
94     break;
95 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
96 lefcha 1.42 case 'p':
97     options |= OPTION_PASSWORD_EDITOR;
98     break;
99     #endif
100     case 'q':
101 lefcha 1.57 options &= ~(OPTION_DETAILS_QUIET |
102     OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
103 lefcha 1.42 options |= OPTION_DETAILS_QUIET;
104     break;
105     case 'v':
106 lefcha 1.57 options &= ~(OPTION_DETAILS_QUIET |
107     OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
108 lefcha 1.42 options |= OPTION_DETAILS_VERBOSE;
109     break;
110     case 'V':
111     version();
112     exit(ERROR_UNDEFINED);
113     break;
114 lefcha 1.61 case 'w':
115     options &= ~(OPTION_PERMISSIONS);
116     break;
117 lefcha 1.42 default:
118     usage();
119     exit(ERROR_UNDEFINED);
120     break;
121     }
122 lefcha 1.1 }
123 lefcha 1.32
124 lefcha 1.60 debug_start();
125    
126 lefcha 1.42 create_homedir();
127 lefcha 1.33
128 lefcha 1.42 lockfile_check();
129     lockfile_create();
130 lefcha 1.28
131 lefcha 1.60 if (!(options & OPTION_DEBUG))
132     corefile_disable();
133 lefcha 1.33
134 lefcha 1.42 tty_store();
135     catch_signals();
136 lefcha 1.24
137 lefcha 1.55 read_config(conffile);
138 lefcha 1.33
139 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
140 lefcha 1.42 read_passwords();
141 lefcha 1.33
142 lefcha 1.42 if ((options & OPTION_PASSWORD_EDITOR)) {
143     password_editor();
144 lefcha 1.33
145 lefcha 1.42 secmem_clear();
146     lockfile_remove();
147 lefcha 1.33
148 lefcha 1.42 exit(0);
149     }
150 lefcha 1.25 #endif
151 lefcha 1.33
152 lefcha 1.60 log_start();
153 lefcha 1.32
154 lefcha 1.50 init_buffer(&ibuf);
155     init_buffer(&obuf);
156 lefcha 1.32
157 lefcha 1.42 if (options & OPTION_DAEMON_MODE) {
158 lefcha 1.57 options &= ~(OPTION_DETAILS_QUIET |
159     OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
160 lefcha 1.42 options |= OPTION_DETAILS_QUIET;
161     }
162     do {
163     for (ca = accounts; ca != NULL; ca = ca->next) {
164 lefcha 1.7
165 lefcha 1.42 if (setjmp(acctloop))
166     continue;
167 lefcha 1.35
168 lefcha 1.51 if (init_connection(&connpri, ca->server, ca->port,
169 lefcha 1.42 ca->ssl))
170     continue;
171 lefcha 1.21
172 lefcha 1.51 r = greeting_response(&connpri);
173 lefcha 1.18
174 lefcha 1.60 if (options & OPTION_DEBUG)
175     test(&connpri);
176 lefcha 1.45
177 lefcha 1.51 if (check_capabilities(&connpri))
178 lefcha 1.42 continue;
179 lefcha 1.1
180 lefcha 1.49 #ifdef SSL_TLS
181     if (ca->ssl == SSL_DISABLED &&
182 lefcha 1.51 connpri.caps & CAPABILITY_STARTTLS)
183 lefcha 1.52 if (negotiate_tls(&connpri) == RESPONSE_OK)
184 lefcha 1.51 check_capabilities(&connpri);
185 lefcha 1.49 #endif
186    
187 lefcha 1.42 log_info(LOG_ACCOUNT, ca->key);
188 lefcha 1.1
189 lefcha 1.42 if (r != RESPONSE_PREAUTH) {
190     if (ca->passwdattr == PASSWORD_NONE) {
191     printf("Enter password for %s@%s: ",
192     ca->username, ca->server);
193     get_password(ca->password, PASSWORD_LEN);
194     ca->passwdattr = PASSWORD_PLAIN;
195     }
196 lefcha 1.48 #ifdef CRAM_MD5
197 lefcha 1.51 if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5)
198 lefcha 1.52 r = auth_cram_md5(&connpri,
199 lefcha 1.48 ca->username, ca->password);
200     else
201     #endif
202 lefcha 1.51 r = login(&connpri, ca->username,
203 lefcha 1.48 ca->password);
204    
205     if (r == RESPONSE_NO) {
206 lefcha 1.42 error("username %s or password rejected "
207     "at %s\n", ca->username, ca->server);
208     continue;
209     }
210     }
211 lefcha 1.51 check_namespace(&connpri);
212 lefcha 1.42
213     for (cm = ca->mboxes; cm != NULL; cm = cm->next)
214     if (*cm->filters == NULL)
215 lefcha 1.51 mailbox_status(&connpri, cm->name);
216     else if (!select_mailbox(&connpri, cm->name)) {
217 lefcha 1.43 apply_filters(cm->name, cm->filters);
218 lefcha 1.51 close_mailbox(&connpri);
219 lefcha 1.42 }
220 lefcha 1.51 logout(&connpri);
221 lefcha 1.33
222 lefcha 1.51 close_connection(&connpri);
223 lefcha 1.33 }
224 lefcha 1.11
225 lefcha 1.42 /* Fork if in daemon mode. */
226 lefcha 1.58 if (options & OPTION_DAEMON_MODE &&
227     !(flags & FLAG_DAEMON_MODE)) {
228    
229     switch (fork()) {
230     case -1:
231     fatal(ERROR_FORK, "forking; %s\n",
232     strerror(errno));
233     break;
234     case 0:
235     break;
236     default:
237 lefcha 1.60 log_stop();
238 lefcha 1.58 secmem_clear();
239 lefcha 1.60 debug_stop();
240 lefcha 1.58 exit(0);
241     break;
242     }
243    
244     if (setsid() == -1)
245     fatal(ERROR_FORK, "creating session; %s\n",
246     strerror(errno));
247    
248     switch (fork()) {
249 lefcha 1.42 case -1:
250     fatal(ERROR_FORK, "forking; %s\n",
251     strerror(errno));
252     break;
253     case 0:
254     break;
255     default:
256 lefcha 1.60 log_stop();
257 lefcha 1.42 secmem_clear();
258 lefcha 1.60 debug_stop();
259 lefcha 1.42 exit(0);
260     break;
261     }
262 lefcha 1.58
263     close(STDIN_FILENO);
264     close(STDOUT_FILENO);
265     close(STDERR_FILENO);
266     if (open("/dev/null", O_RDWR) != -1) {
267     dup(STDIN_FILENO);
268     dup(STDIN_FILENO);
269     }
270     lockfile_create();
271     corefile_disable();
272    
273     flags |= FLAG_DAEMON_MODE;
274 lefcha 1.46 }
275     if (options & OPTION_DAEMON_MODE &&
276 lefcha 1.54 flags & FLAG_SIGUSR1_RECEIVED) {
277 lefcha 1.55 reread_config(conffile);
278 lefcha 1.46 continue;
279 lefcha 1.42 }
280     if (interval)
281     sleep(interval);
282     } while (options & OPTION_DAEMON_MODE && interval);
283 lefcha 1.17
284 lefcha 1.60 log_stop();
285 lefcha 1.42 secmem_clear();
286 lefcha 1.33
287 lefcha 1.42 lockfile_remove();
288 lefcha 1.33
289 lefcha 1.60 debug_stop();
290    
291 lefcha 1.55 exit(0);
292 lefcha 1.1 }
293    
294    
295     /*
296 lefcha 1.11 * Print a very brief usage message.
297 lefcha 1.2 */
298 lefcha 1.42 void
299     usage(void)
300 lefcha 1.2 {
301 lefcha 1.42 fprintf(stderr,
302 lefcha 1.60 "usage: imapfilter [-bDhk"
303 lefcha 1.26 #ifdef ENCRYPTED_PASSWORDS
304     "p"
305 lefcha 1.33 #endif
306 lefcha 1.61 "qvVw] [-c configfile] [-d interval] [-l logfile]\n");
307 lefcha 1.38 }
308    
309    
310     /*
311     * Print program's version, and if it is built in, OpenSSL's version number.
312     */
313 lefcha 1.42 void
314     version(void)
315 lefcha 1.38 {
316 lefcha 1.42 fprintf(stderr, "IMAPFilter %s"
317 lefcha 1.53 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
318 lefcha 1.38 ", OpenSSL 0x%8.8lx"
319     #endif
320     "\n", IMAPFILTER_VERSION
321 lefcha 1.53 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
322 lefcha 1.38 ,SSLeay()
323     #endif
324 lefcha 1.42 );
325 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26