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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45 - (show annotations)
Fri Mar 28 17:04:18 2003 UTC (21 years ago) by lefcha
Branch: MAIN
Changes since 1.44: +5 -4 lines
File MIME type: text/plain
Set LC_ALL instead of LC_CTYPE and check capabilities later.

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_ALL, "");
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 #ifdef DEBUG
165 test(&sockpri);
166 #endif
167
168 if (check_capabilities(&sockpri))
169 continue;
170
171 log_info(LOG_ACCOUNT, ca->key);
172
173 if (r != RESPONSE_PREAUTH) {
174 if (ca->passwdattr == PASSWORD_NONE) {
175 printf("Enter password for %s@%s: ",
176 ca->username, ca->server);
177 get_password(ca->password, PASSWORD_LEN);
178 ca->passwdattr = PASSWORD_PLAIN;
179 }
180 if (login(&sockpri, ca->username,
181 ca->password) == RESPONSE_NO) {
182 error("username %s or password rejected "
183 "at %s\n", ca->username, ca->server);
184 continue;
185 }
186 }
187 check_namespace(&sockpri, &nsppri);
188
189 for (cm = ca->mboxes; cm != NULL; cm = cm->next)
190 if (*cm->filters == NULL)
191 mailbox_status(&sockpri, cm->name,
192 &nsppri);
193 else if (!select_mailbox(&sockpri, cm->name,
194 &nsppri)) {
195 apply_filters(cm->name, cm->filters);
196 close_mailbox(&sockpri);
197 }
198 logout(&sockpri);
199
200 close_connection(&sockpri);
201 }
202
203 /* Fork if in daemon mode. */
204 if (f) {
205 f = 0;
206 pid = fork();
207 switch (pid) {
208 case -1:
209 fatal(ERROR_FORK, "forking; %s\n",
210 strerror(errno));
211 break;
212 case 0:
213 #ifdef MEMORY_LOCK
214 secmem_lock();
215 setuid(ruid); /* Capability to regain root
216 * privileges will not be
217 * needed any more. */
218 #endif
219 lockfile_create();
220 corefile_disable();
221 flags |= FLAG_DAEMON_MODE;
222 break;
223 default:
224 secmem_clear();
225 close_logfile();
226 exit(0);
227 break;
228 }
229 }
230 if (interval)
231 sleep(interval);
232 } while (options & OPTION_DAEMON_MODE && interval);
233
234 secmem_clear();
235 close_logfile();
236
237 lockfile_remove();
238
239 exit(0);
240 }
241
242
243 /*
244 * Print a very brief usage message.
245 */
246 void
247 usage(void)
248 {
249 fprintf(stderr,
250 "usage: imapfilter [-hk"
251 #ifdef ENCRYPTED_PASSWORDS
252 "p"
253 #endif
254 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
255 }
256
257
258 /*
259 * Print program's version, and if it is built in, OpenSSL's version number.
260 */
261 void
262 version(void)
263 {
264 fprintf(stderr, "IMAPFilter %s"
265 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
266 ", OpenSSL 0x%8.8lx"
267 #endif
268 "\n", IMAPFILTER_VERSION
269 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
270 ,SSLeay()
271 #endif
272 );
273 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26