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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.44 - (show annotations)
Sat Mar 22 15:26:22 2003 UTC (21 years ago) by lefcha
Branch: MAIN
Changes since 1.43: +1 -0 lines
File MIME type: text/plain
Flag set when in daemon mode.

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 #ifdef MEMORY_LOCK
35 uid_t ruid, euid; /* Real and effective UID. */
36
37 #endif
38 jmp_buf acctloop;
39
40
41 /*
42 * In the beginning there was main()...
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 #ifdef MEMORY_LOCK
54 ruid = getuid();
55 euid = geteuid();
56 seteuid(ruid); /* Drop root privileges. */
57 #endif
58
59 setlocale(LC_CTYPE, "");
60
61 f = 0;
62 home = getenv("HOME");
63 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_WARNING);
64 *charset = 0;
65 *logfile = 0;
66 confile = NULL;
67
68 while ((c = getopt(argc, argv, "c:d:hkl:"
69 #ifdef ENCRYPTED_PASSWORDS
70 "p"
71 #endif
72 "qvV")) != -1) {
73 switch (c) {
74 case 'c':
75 confile = optarg;
76 break;
77 case 'd':
78 options |= OPTION_DAEMON_MODE;
79 errno = 0;
80 interval = strtoul(optarg, NULL, 10);
81 if (errno)
82 interval = 0;
83 break;
84 case 'h':
85 usage();
86 exit(ERROR_UNDEFINED);
87 break;
88 case 'k':
89 kill_imapfilter();
90 break;
91 case 'l':
92 strncat(logfile, optarg, PATH_MAX - 1);
93 break;
94 #ifdef ENCRYPTED_PASSWORDS
95 case 'p':
96 options |= OPTION_PASSWORD_EDITOR;
97 break;
98 #endif
99 case 'q':
100 options &= OPTION_DETAILS_CLEAR;
101 options |= OPTION_DETAILS_QUIET;
102 break;
103 case 'v':
104 options &= OPTION_DETAILS_CLEAR;
105 options |= OPTION_DETAILS_VERBOSE;
106 break;
107 case 'V':
108 version();
109 exit(ERROR_UNDEFINED);
110 break;
111 default:
112 usage();
113 exit(ERROR_UNDEFINED);
114 break;
115 }
116 }
117
118 create_homedir();
119
120 lockfile_check();
121 lockfile_create();
122
123 corefile_disable();
124
125 tty_store();
126 catch_signals();
127
128 read_config(confile);
129
130 #ifdef ENCRYPTED_PASSWORDS
131 read_passwords();
132
133 if ((options & OPTION_PASSWORD_EDITOR)) {
134 password_editor();
135
136 secmem_clear();
137 lockfile_remove();
138
139 exit(0);
140 }
141 #endif
142
143 open_logfile();
144
145 init_vbuf();
146
147 if (options & OPTION_DAEMON_MODE) {
148 f = 1;
149 options &= OPTION_DETAILS_CLEAR;
150 options |= OPTION_DETAILS_QUIET;
151 }
152 do {
153 for (ca = accounts; ca != NULL; ca = ca->next) {
154
155 if (setjmp(acctloop))
156 continue;
157
158 if (init_connection(&sockpri, ca->server, ca->port,
159 ca->ssl))
160 continue;
161
162 r = greeting_response(&sockpri);
163
164 if (check_capabilities(&sockpri))
165 continue;
166
167 #ifdef DEBUG
168 test(&sockpri);
169 #endif
170 log_info(LOG_ACCOUNT, ca->key);
171
172 if (r != RESPONSE_PREAUTH) {
173 if (ca->passwdattr == PASSWORD_NONE) {
174 printf("Enter password for %s@%s: ",
175 ca->username, ca->server);
176 get_password(ca->password, PASSWORD_LEN);
177 ca->passwdattr = PASSWORD_PLAIN;
178 }
179 if (login(&sockpri, ca->username,
180 ca->password) == RESPONSE_NO) {
181 error("username %s or password rejected "
182 "at %s\n", ca->username, ca->server);
183 continue;
184 }
185 }
186 check_namespace(&sockpri, &nsppri);
187
188 for (cm = ca->mboxes; cm != NULL; cm = cm->next)
189 if (*cm->filters == NULL)
190 mailbox_status(&sockpri, cm->name,
191 &nsppri);
192 else if (!select_mailbox(&sockpri, cm->name,
193 &nsppri)) {
194 apply_filters(cm->name, cm->filters);
195 close_mailbox(&sockpri);
196 }
197 logout(&sockpri);
198
199 close_connection(&sockpri);
200 }
201
202 /* Fork if in daemon mode. */
203 if (f) {
204 f = 0;
205 pid = fork();
206 switch (pid) {
207 case -1:
208 fatal(ERROR_FORK, "forking; %s\n",
209 strerror(errno));
210 break;
211 case 0:
212 #ifdef MEMORY_LOCK
213 secmem_lock();
214 setuid(ruid); /* Capability to regain root
215 * privileges will not be
216 * needed any more. */
217 #endif
218 lockfile_create();
219 corefile_disable();
220 flags |= FLAG_DAEMON_MODE;
221 break;
222 default:
223 secmem_clear();
224 close_logfile();
225 exit(0);
226 break;
227 }
228 }
229 if (interval)
230 sleep(interval);
231 } while (options & OPTION_DAEMON_MODE && interval);
232
233 secmem_clear();
234 close_logfile();
235
236 lockfile_remove();
237
238 exit(0);
239 }
240
241
242 /*
243 * Print a very brief usage message.
244 */
245 void
246 usage(void)
247 {
248 fprintf(stderr,
249 "usage: imapfilter [-hk"
250 #ifdef ENCRYPTED_PASSWORDS
251 "p"
252 #endif
253 "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 void
261 version(void)
262 {
263 fprintf(stderr, "IMAPFilter %s"
264 #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 );
272 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26