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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.54.2.4 - (hide annotations)
Mon Jan 26 21:38:51 2004 UTC (20 years, 2 months ago) by lefcha
Branch: release-0_9-patches
Changes since 1.54.2.3: +38 -12 lines
File MIME type: text/plain
Daemon mode corrections/improvements.

1 lefcha 1.1 #include <stdio.h>
2 lefcha 1.53 #include <stdlib.h>
3 lefcha 1.54.2.1 #include <sys/types.h>
4 lefcha 1.1 #include <unistd.h>
5 lefcha 1.54.2.1 #include <string.h>
6 lefcha 1.52 #include <errno.h>
7 lefcha 1.7 #include <limits.h>
8 lefcha 1.54.2.4 #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.54.2.1 #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.54.2.4 int c, r;
52 lefcha 1.54.2.1 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.54.2.2 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_PEEK);
60 lefcha 1.42 *charset = 0;
61     *logfile = 0;
62 lefcha 1.54.2.1 conffile = NULL;
63     connpri.sock = connaux.sock = -1;
64 lefcha 1.33
65 lefcha 1.42 while ((c = getopt(argc, argv, "c:d:hkl:"
66 lefcha 1.29 #ifdef ENCRYPTED_PASSWORDS
67 lefcha 1.42 "p"
68 lefcha 1.25 #endif
69 lefcha 1.42 "qvV")) != -1) {
70     switch (c) {
71     case 'c':
72 lefcha 1.54.2.1 conffile = optarg;
73 lefcha 1.42 break;
74     case 'd':
75     options |= OPTION_DAEMON_MODE;
76     errno = 0;
77     interval = strtoul(optarg, NULL, 10);
78     if (errno)
79     interval = 0;
80     break;
81     case 'h':
82     usage();
83     exit(ERROR_UNDEFINED);
84     break;
85     case 'k':
86     kill_imapfilter();
87     break;
88     case 'l':
89     strncat(logfile, optarg, PATH_MAX - 1);
90     break;
91 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
92 lefcha 1.42 case 'p':
93     options |= OPTION_PASSWORD_EDITOR;
94     break;
95     #endif
96     case 'q':
97 lefcha 1.54.2.3 options &= ~(OPTION_DETAILS_QUIET |
98     OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
99 lefcha 1.42 options |= OPTION_DETAILS_QUIET;
100     break;
101     case 'v':
102 lefcha 1.54.2.3 options &= ~(OPTION_DETAILS_QUIET |
103     OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
104 lefcha 1.42 options |= OPTION_DETAILS_VERBOSE;
105     break;
106     case 'V':
107     version();
108     exit(ERROR_UNDEFINED);
109     break;
110     default:
111     usage();
112     exit(ERROR_UNDEFINED);
113     break;
114     }
115 lefcha 1.1 }
116 lefcha 1.32
117 lefcha 1.42 create_homedir();
118 lefcha 1.33
119 lefcha 1.42 lockfile_check();
120     lockfile_create();
121 lefcha 1.28
122 lefcha 1.47 #ifndef DEBUG
123 lefcha 1.42 corefile_disable();
124 lefcha 1.47 #endif
125 lefcha 1.33
126 lefcha 1.42 tty_store();
127     catch_signals();
128 lefcha 1.24
129 lefcha 1.54.2.1 read_config(conffile);
130 lefcha 1.33
131 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
132 lefcha 1.42 read_passwords();
133 lefcha 1.33
134 lefcha 1.42 if ((options & OPTION_PASSWORD_EDITOR)) {
135     password_editor();
136 lefcha 1.33
137 lefcha 1.42 secmem_clear();
138     lockfile_remove();
139 lefcha 1.33
140 lefcha 1.42 exit(0);
141     }
142 lefcha 1.25 #endif
143 lefcha 1.33
144 lefcha 1.42 open_logfile();
145 lefcha 1.32
146 lefcha 1.50 init_buffer(&ibuf);
147     init_buffer(&obuf);
148 lefcha 1.32
149 lefcha 1.42 if (options & OPTION_DAEMON_MODE) {
150 lefcha 1.54.2.3 options &= ~(OPTION_DETAILS_QUIET |
151     OPTION_DETAILS_NORMAL | OPTION_DETAILS_VERBOSE);
152 lefcha 1.42 options |= OPTION_DETAILS_QUIET;
153     }
154     do {
155     for (ca = accounts; ca != NULL; ca = ca->next) {
156 lefcha 1.7
157 lefcha 1.42 if (setjmp(acctloop))
158     continue;
159 lefcha 1.35
160 lefcha 1.51 if (init_connection(&connpri, ca->server, ca->port,
161 lefcha 1.42 ca->ssl))
162     continue;
163 lefcha 1.21
164 lefcha 1.51 r = greeting_response(&connpri);
165 lefcha 1.18
166 lefcha 1.45 #ifdef DEBUG
167 lefcha 1.51 test(&connpri);
168 lefcha 1.45 #endif
169    
170 lefcha 1.51 if (check_capabilities(&connpri))
171 lefcha 1.42 continue;
172 lefcha 1.1
173 lefcha 1.49 #ifdef SSL_TLS
174     if (ca->ssl == SSL_DISABLED &&
175 lefcha 1.51 connpri.caps & CAPABILITY_STARTTLS)
176 lefcha 1.52 if (negotiate_tls(&connpri) == RESPONSE_OK)
177 lefcha 1.51 check_capabilities(&connpri);
178 lefcha 1.49 #endif
179    
180 lefcha 1.42 log_info(LOG_ACCOUNT, ca->key);
181 lefcha 1.1
182 lefcha 1.42 if (r != RESPONSE_PREAUTH) {
183     if (ca->passwdattr == PASSWORD_NONE) {
184     printf("Enter password for %s@%s: ",
185     ca->username, ca->server);
186     get_password(ca->password, PASSWORD_LEN);
187     ca->passwdattr = PASSWORD_PLAIN;
188     }
189 lefcha 1.48 #ifdef CRAM_MD5
190 lefcha 1.51 if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5)
191 lefcha 1.52 r = auth_cram_md5(&connpri,
192 lefcha 1.48 ca->username, ca->password);
193     else
194     #endif
195 lefcha 1.51 r = login(&connpri, ca->username,
196 lefcha 1.48 ca->password);
197    
198     if (r == RESPONSE_NO) {
199 lefcha 1.42 error("username %s or password rejected "
200     "at %s\n", ca->username, ca->server);
201     continue;
202     }
203     }
204 lefcha 1.51 check_namespace(&connpri);
205 lefcha 1.42
206     for (cm = ca->mboxes; cm != NULL; cm = cm->next)
207     if (*cm->filters == NULL)
208 lefcha 1.51 mailbox_status(&connpri, cm->name);
209     else if (!select_mailbox(&connpri, cm->name)) {
210 lefcha 1.43 apply_filters(cm->name, cm->filters);
211 lefcha 1.51 close_mailbox(&connpri);
212 lefcha 1.42 }
213 lefcha 1.51 logout(&connpri);
214 lefcha 1.33
215 lefcha 1.51 close_connection(&connpri);
216 lefcha 1.33 }
217 lefcha 1.11
218 lefcha 1.42 /* Fork if in daemon mode. */
219 lefcha 1.54.2.4 if (options & OPTION_DAEMON_MODE &&
220     !(flags & FLAG_DAEMON_MODE)) {
221    
222     close_logfile();
223    
224     switch (fork()) {
225     case -1:
226     fatal(ERROR_FORK, "forking; %s\n",
227     strerror(errno));
228     break;
229     case 0:
230     break;
231     default:
232     secmem_clear();
233     exit(0);
234     break;
235     }
236    
237     if (setsid() == -1)
238     fatal(ERROR_FORK, "creating session; %s\n",
239     strerror(errno));
240    
241     switch (fork()) {
242 lefcha 1.42 case -1:
243     fatal(ERROR_FORK, "forking; %s\n",
244     strerror(errno));
245     break;
246     case 0:
247     break;
248     default:
249     secmem_clear();
250     exit(0);
251     break;
252     }
253 lefcha 1.54.2.4
254     close(STDIN_FILENO);
255     close(STDOUT_FILENO);
256     close(STDERR_FILENO);
257     if (open("/dev/null", O_RDWR) != -1) {
258     dup(STDIN_FILENO);
259     dup(STDIN_FILENO);
260     }
261     open_logfile();
262     lockfile_create();
263     corefile_disable();
264    
265     flags |= FLAG_DAEMON_MODE;
266 lefcha 1.46 }
267     if (options & OPTION_DAEMON_MODE &&
268 lefcha 1.54 flags & FLAG_SIGUSR1_RECEIVED) {
269 lefcha 1.54.2.1 reread_config(conffile);
270 lefcha 1.46 continue;
271 lefcha 1.42 }
272     if (interval)
273     sleep(interval);
274     } while (options & OPTION_DAEMON_MODE && interval);
275 lefcha 1.17
276 lefcha 1.42 secmem_clear();
277     close_logfile();
278 lefcha 1.33
279 lefcha 1.42 lockfile_remove();
280 lefcha 1.33
281 lefcha 1.54.2.1 exit(0);
282 lefcha 1.1 }
283    
284    
285     /*
286 lefcha 1.11 * Print a very brief usage message.
287 lefcha 1.2 */
288 lefcha 1.42 void
289     usage(void)
290 lefcha 1.2 {
291 lefcha 1.42 fprintf(stderr,
292 lefcha 1.29 "usage: imapfilter [-hk"
293 lefcha 1.26 #ifdef ENCRYPTED_PASSWORDS
294     "p"
295 lefcha 1.33 #endif
296 lefcha 1.38 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
297     }
298    
299    
300     /*
301     * Print program's version, and if it is built in, OpenSSL's version number.
302     */
303 lefcha 1.42 void
304     version(void)
305 lefcha 1.38 {
306 lefcha 1.42 fprintf(stderr, "IMAPFilter %s"
307 lefcha 1.53 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
308 lefcha 1.38 ", OpenSSL 0x%8.8lx"
309     #endif
310     "\n", IMAPFILTER_VERSION
311 lefcha 1.53 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
312 lefcha 1.38 ,SSLeay()
313     #endif
314 lefcha 1.42 );
315 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26