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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.50 - (show annotations)
Sun Jul 27 15:54:49 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.49: +3 -1 lines
File MIME type: text/plain
Use new *_buffer() routines for input/output buffers.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26