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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.56 - (hide annotations)
Sun Nov 23 22:47:27 2003 UTC (20 years, 4 months ago) by lefcha
Branch: MAIN
Changes since 1.55: +1 -1 lines
File MIME type: text/plain
Added "peek" option that causes use of BODY or BODY.PEEK when doing FETCH.

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     options &= OPTION_DETAILS_CLEAR;
99     options |= OPTION_DETAILS_QUIET;
100     break;
101     case 'v':
102     options &= OPTION_DETAILS_CLEAR;
103     options |= OPTION_DETAILS_VERBOSE;
104     break;
105     case 'V':
106     version();
107     exit(ERROR_UNDEFINED);
108     break;
109     default:
110     usage();
111     exit(ERROR_UNDEFINED);
112     break;
113     }
114 lefcha 1.1 }
115 lefcha 1.32
116 lefcha 1.42 create_homedir();
117 lefcha 1.33
118 lefcha 1.42 lockfile_check();
119     lockfile_create();
120 lefcha 1.28
121 lefcha 1.47 #ifndef DEBUG
122 lefcha 1.42 corefile_disable();
123 lefcha 1.47 #endif
124 lefcha 1.33
125 lefcha 1.42 tty_store();
126     catch_signals();
127 lefcha 1.24
128 lefcha 1.55 read_config(conffile);
129 lefcha 1.33
130 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
131 lefcha 1.42 read_passwords();
132 lefcha 1.33
133 lefcha 1.42 if ((options & OPTION_PASSWORD_EDITOR)) {
134     password_editor();
135 lefcha 1.33
136 lefcha 1.42 secmem_clear();
137     lockfile_remove();
138 lefcha 1.33
139 lefcha 1.42 exit(0);
140     }
141 lefcha 1.25 #endif
142 lefcha 1.33
143 lefcha 1.42 open_logfile();
144 lefcha 1.32
145 lefcha 1.50 init_buffer(&ibuf);
146     init_buffer(&obuf);
147 lefcha 1.32
148 lefcha 1.42 if (options & OPTION_DAEMON_MODE) {
149     f = 1;
150     options &= OPTION_DETAILS_CLEAR;
151     options |= OPTION_DETAILS_QUIET;
152     }
153     do {
154     for (ca = accounts; ca != NULL; ca = ca->next) {
155 lefcha 1.7
156 lefcha 1.42 if (setjmp(acctloop))
157     continue;
158 lefcha 1.35
159 lefcha 1.51 if (init_connection(&connpri, ca->server, ca->port,
160 lefcha 1.42 ca->ssl))
161     continue;
162 lefcha 1.21
163 lefcha 1.51 r = greeting_response(&connpri);
164 lefcha 1.18
165 lefcha 1.45 #ifdef DEBUG
166 lefcha 1.51 test(&connpri);
167 lefcha 1.45 #endif
168    
169 lefcha 1.51 if (check_capabilities(&connpri))
170 lefcha 1.42 continue;
171 lefcha 1.1
172 lefcha 1.49 #ifdef SSL_TLS
173     if (ca->ssl == SSL_DISABLED &&
174 lefcha 1.51 connpri.caps & CAPABILITY_STARTTLS)
175 lefcha 1.52 if (negotiate_tls(&connpri) == RESPONSE_OK)
176 lefcha 1.51 check_capabilities(&connpri);
177 lefcha 1.49 #endif
178    
179 lefcha 1.42 log_info(LOG_ACCOUNT, ca->key);
180 lefcha 1.1
181 lefcha 1.42 if (r != RESPONSE_PREAUTH) {
182     if (ca->passwdattr == PASSWORD_NONE) {
183     printf("Enter password for %s@%s: ",
184     ca->username, ca->server);
185     get_password(ca->password, PASSWORD_LEN);
186     ca->passwdattr = PASSWORD_PLAIN;
187     }
188 lefcha 1.48 #ifdef CRAM_MD5
189 lefcha 1.51 if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5)
190 lefcha 1.52 r = auth_cram_md5(&connpri,
191 lefcha 1.48 ca->username, ca->password);
192     else
193     #endif
194 lefcha 1.51 r = login(&connpri, ca->username,
195 lefcha 1.48 ca->password);
196    
197     if (r == RESPONSE_NO) {
198 lefcha 1.42 error("username %s or password rejected "
199     "at %s\n", ca->username, ca->server);
200     continue;
201     }
202     }
203 lefcha 1.51 check_namespace(&connpri);
204 lefcha 1.42
205     for (cm = ca->mboxes; cm != NULL; cm = cm->next)
206     if (*cm->filters == NULL)
207 lefcha 1.51 mailbox_status(&connpri, cm->name);
208     else if (!select_mailbox(&connpri, cm->name)) {
209 lefcha 1.43 apply_filters(cm->name, cm->filters);
210 lefcha 1.51 close_mailbox(&connpri);
211 lefcha 1.42 }
212 lefcha 1.51 logout(&connpri);
213 lefcha 1.33
214 lefcha 1.51 close_connection(&connpri);
215 lefcha 1.33 }
216 lefcha 1.11
217 lefcha 1.42 /* Fork if in daemon mode. */
218     if (f) {
219     f = 0;
220     pid = fork();
221     switch (pid) {
222     case -1:
223     fatal(ERROR_FORK, "forking; %s\n",
224     strerror(errno));
225     break;
226     case 0:
227     lockfile_create();
228     corefile_disable();
229 lefcha 1.44 flags |= FLAG_DAEMON_MODE;
230 lefcha 1.42 break;
231     default:
232     secmem_clear();
233     close_logfile();
234     exit(0);
235     break;
236     }
237 lefcha 1.46 }
238     if (options & OPTION_DAEMON_MODE &&
239 lefcha 1.54 flags & FLAG_SIGUSR1_RECEIVED) {
240 lefcha 1.55 reread_config(conffile);
241 lefcha 1.46 continue;
242 lefcha 1.42 }
243     if (interval)
244     sleep(interval);
245     } while (options & OPTION_DAEMON_MODE && interval);
246 lefcha 1.17
247 lefcha 1.42 secmem_clear();
248     close_logfile();
249 lefcha 1.33
250 lefcha 1.42 lockfile_remove();
251 lefcha 1.33
252 lefcha 1.55 exit(0);
253 lefcha 1.1 }
254    
255    
256     /*
257 lefcha 1.11 * Print a very brief usage message.
258 lefcha 1.2 */
259 lefcha 1.42 void
260     usage(void)
261 lefcha 1.2 {
262 lefcha 1.42 fprintf(stderr,
263 lefcha 1.29 "usage: imapfilter [-hk"
264 lefcha 1.26 #ifdef ENCRYPTED_PASSWORDS
265     "p"
266 lefcha 1.33 #endif
267 lefcha 1.38 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
268     }
269    
270    
271     /*
272     * Print program's version, and if it is built in, OpenSSL's version number.
273     */
274 lefcha 1.42 void
275     version(void)
276 lefcha 1.38 {
277 lefcha 1.42 fprintf(stderr, "IMAPFilter %s"
278 lefcha 1.53 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
279 lefcha 1.38 ", OpenSSL 0x%8.8lx"
280     #endif
281     "\n", IMAPFILTER_VERSION
282 lefcha 1.53 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS || defined CRAM_MD5
283 lefcha 1.38 ,SSLeay()
284     #endif
285 lefcha 1.42 );
286 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26