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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.48 - (hide annotations)
Sat Jul 26 19:41:30 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.47: +14 -6 lines
File MIME type: text/plain
Added CRAM-MD5 support and fixed bug with capabilities of different servers.

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.42 log_info(LOG_ACCOUNT, ca->key);
164 lefcha 1.1
165 lefcha 1.42 if (r != RESPONSE_PREAUTH) {
166     if (ca->passwdattr == PASSWORD_NONE) {
167     printf("Enter password for %s@%s: ",
168     ca->username, ca->server);
169     get_password(ca->password, PASSWORD_LEN);
170     ca->passwdattr = PASSWORD_PLAIN;
171     }
172 lefcha 1.48 #ifdef CRAM_MD5
173     if (capspri & CAPABILITY_AUTH_CRAM_MD5)
174     r = imf_cram_md5(&sockpri,
175     ca->username, ca->password);
176     else
177     #endif
178     r = login(&sockpri, ca->username,
179     ca->password);
180    
181     if (r == RESPONSE_NO) {
182 lefcha 1.42 error("username %s or password rejected "
183     "at %s\n", ca->username, ca->server);
184     continue;
185     }
186     }
187     check_namespace(&sockpri, &nsppri);
188    
189     for (cm = ca->mboxes; cm != NULL; cm = cm->next)
190     if (*cm->filters == NULL)
191     mailbox_status(&sockpri, cm->name,
192     &nsppri);
193     else if (!select_mailbox(&sockpri, cm->name,
194     &nsppri)) {
195 lefcha 1.43 apply_filters(cm->name, cm->filters);
196 lefcha 1.42 close_mailbox(&sockpri);
197     }
198     logout(&sockpri);
199 lefcha 1.33
200 lefcha 1.42 close_connection(&sockpri);
201 lefcha 1.33 }
202 lefcha 1.11
203 lefcha 1.42 /* Fork if in daemon mode. */
204     if (f) {
205     f = 0;
206     pid = fork();
207     switch (pid) {
208     case -1:
209     fatal(ERROR_FORK, "forking; %s\n",
210     strerror(errno));
211     break;
212     case 0:
213     lockfile_create();
214     corefile_disable();
215 lefcha 1.44 flags |= FLAG_DAEMON_MODE;
216 lefcha 1.42 break;
217     default:
218     secmem_clear();
219     close_logfile();
220     exit(0);
221     break;
222     }
223 lefcha 1.46 }
224     if (options & OPTION_DAEMON_MODE &&
225     flags & FLAG_SIGHUP_RECEIVED) {
226     reread_config(confile);
227     continue;
228 lefcha 1.42 }
229     if (interval)
230     sleep(interval);
231     } while (options & OPTION_DAEMON_MODE && interval);
232 lefcha 1.17
233 lefcha 1.42 secmem_clear();
234     close_logfile();
235 lefcha 1.33
236 lefcha 1.42 lockfile_remove();
237 lefcha 1.33
238 lefcha 1.48 return 0;
239 lefcha 1.1 }
240    
241    
242     /*
243 lefcha 1.11 * Print a very brief usage message.
244 lefcha 1.2 */
245 lefcha 1.42 void
246     usage(void)
247 lefcha 1.2 {
248 lefcha 1.42 fprintf(stderr,
249 lefcha 1.29 "usage: imapfilter [-hk"
250 lefcha 1.26 #ifdef ENCRYPTED_PASSWORDS
251     "p"
252 lefcha 1.33 #endif
253 lefcha 1.38 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
254     }
255    
256    
257     /*
258     * Print program's version, and if it is built in, OpenSSL's version number.
259     */
260 lefcha 1.42 void
261     version(void)
262 lefcha 1.38 {
263 lefcha 1.42 fprintf(stderr, "IMAPFilter %s"
264 lefcha 1.38 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
265     ", OpenSSL 0x%8.8lx"
266     #endif
267     "\n", IMAPFILTER_VERSION
268     #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
269     ,SSLeay()
270     #endif
271 lefcha 1.42 );
272 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26