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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.47 - (show annotations)
Tue May 20 12:17:52 2003 UTC (20 years, 11 months ago) by lefcha
Branch: MAIN
Changes since 1.46: +3 -17 lines
File MIME type: text/plain
Removed memory lock functionality.

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
24 unsigned int options; /* Program options. */
25 unsigned int flags = 0; /* Program flags. */
26 unsigned int capabilities; /* Capabilities of mail server. */
27 unsigned int interval = 0; /* Poll at the specified interval. */
28
29 char authmech[AUTH_MECH_LEN]; /* Authentication mechanisms. */
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 /*
38 * IMAPFilter: an IMAP mail filtering utility.
39 */
40 int
41 main(int argc, char *argv[])
42 {
43 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
49 setlocale(LC_ALL, "");
50
51 f = 0;
52 home = getenv("HOME");
53 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE);
54 *charset = 0;
55 *logfile = 0;
56 confile = NULL;
57
58 while ((c = getopt(argc, argv, "c:d:hkl:"
59 #ifdef ENCRYPTED_PASSWORDS
60 "p"
61 #endif
62 "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 #ifdef ENCRYPTED_PASSWORDS
85 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 }
107
108 create_homedir();
109
110 lockfile_check();
111 lockfile_create();
112
113 #ifndef DEBUG
114 corefile_disable();
115 #endif
116
117 tty_store();
118 catch_signals();
119
120 read_config(confile);
121
122 #ifdef ENCRYPTED_PASSWORDS
123 read_passwords();
124
125 if ((options & OPTION_PASSWORD_EDITOR)) {
126 password_editor();
127
128 secmem_clear();
129 lockfile_remove();
130
131 exit(0);
132 }
133 #endif
134
135 open_logfile();
136
137 init_vbuf();
138
139 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
147 if (setjmp(acctloop))
148 continue;
149
150 if (init_connection(&sockpri, ca->server, ca->port,
151 ca->ssl))
152 continue;
153
154 r = greeting_response(&sockpri);
155
156 #ifdef DEBUG
157 test(&sockpri);
158 #endif
159
160 if (check_capabilities(&sockpri))
161 continue;
162
163 log_info(LOG_ACCOUNT, ca->key);
164
165 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 if (login(&sockpri, ca->username,
173 ca->password) == RESPONSE_NO) {
174 error("username %s or password rejected "
175 "at %s\n", ca->username, ca->server);
176 continue;
177 }
178 }
179 check_namespace(&sockpri, &nsppri);
180
181 for (cm = ca->mboxes; cm != NULL; cm = cm->next)
182 if (*cm->filters == NULL)
183 mailbox_status(&sockpri, cm->name,
184 &nsppri);
185 else if (!select_mailbox(&sockpri, cm->name,
186 &nsppri)) {
187 apply_filters(cm->name, cm->filters);
188 close_mailbox(&sockpri);
189 }
190 logout(&sockpri);
191
192 close_connection(&sockpri);
193 }
194
195 /* Fork if in daemon mode. */
196 if (f) {
197 f = 0;
198 pid = fork();
199 switch (pid) {
200 case -1:
201 fatal(ERROR_FORK, "forking; %s\n",
202 strerror(errno));
203 break;
204 case 0:
205 lockfile_create();
206 corefile_disable();
207 flags |= FLAG_DAEMON_MODE;
208 break;
209 default:
210 secmem_clear();
211 close_logfile();
212 exit(0);
213 break;
214 }
215 }
216 if (options & OPTION_DAEMON_MODE &&
217 flags & FLAG_SIGHUP_RECEIVED) {
218 reread_config(confile);
219 continue;
220 }
221 if (interval)
222 sleep(interval);
223 } while (options & OPTION_DAEMON_MODE && interval);
224
225 secmem_clear();
226 close_logfile();
227
228 lockfile_remove();
229
230 exit(0);
231 }
232
233
234 /*
235 * Print a very brief usage message.
236 */
237 void
238 usage(void)
239 {
240 fprintf(stderr,
241 "usage: imapfilter [-hk"
242 #ifdef ENCRYPTED_PASSWORDS
243 "p"
244 #endif
245 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
246 }
247
248
249 /*
250 * Print program's version, and if it is built in, OpenSSL's version number.
251 */
252 void
253 version(void)
254 {
255 fprintf(stderr, "IMAPFilter %s"
256 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
257 ", OpenSSL 0x%8.8lx"
258 #endif
259 "\n", IMAPFILTER_VERSION
260 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
261 ,SSLeay()
262 #endif
263 );
264 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26