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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.52 - (show annotations)
Thu Jul 31 15:53:19 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.51: +12 -6 lines
File MIME type: text/plain
Broke up program files and created some new header files.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26