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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34.2.2 - (show annotations)
Tue Sep 24 18:40:38 2002 UTC (21 years, 6 months ago) by lefcha
Branch: release-0_8-patches
Changes since 1.34.2.1: +6 -0 lines
File MIME type: text/plain
Option to disable memory locking during compilation.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26