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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.49 - (hide annotations)
Sun Jul 27 10:08:42 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.48: +7 -0 lines
File MIME type: text/plain
Added STARTTLS support.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26