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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34.2.3 - (show annotations)
Sat Nov 9 14:20:21 2002 UTC (21 years, 4 months ago) by lefcha
Branch: release-0_8-patches
Changes since 1.34.2.2: +27 -2 lines
File MIME type: text/plain
Added option to display program's version.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26