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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.57 - (hide annotations)
Tue Jan 20 02:05:06 2004 UTC (20 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.56: +6 -3 lines
File MIME type: text/plain
Got rid of OPTIONS_DETAILS_CLEAR.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26