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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.53 - (hide annotations)
Sun Aug 3 16:05:24 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.52: +4 -3 lines
File MIME type: text/plain
Include stdlib.h and check for CRAM_MD5 when printing OpenSSL version.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26