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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45 - (hide 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 lefcha 1.1 #include <stdio.h>
2 lefcha 1.16 #include <stdlib.h>
3 lefcha 1.1 #include <unistd.h>
4     #include <string.h>
5 lefcha 1.7 #include <limits.h>
6 lefcha 1.11 #include <errno.h>
7 lefcha 1.35 #include <setjmp.h>
8 lefcha 1.40 #include <locale.h>
9 lefcha 1.1
10 lefcha 1.39 #include "config.h"
11     #include "imapfilter.h"
12     #include "data.h"
13    
14 lefcha 1.38 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
15     #include <openssl/crypto.h>
16     #endif
17 lefcha 1.1
18    
19 lefcha 1.32 extern int sockpri;
20 lefcha 1.7 extern account_t *accounts;
21 lefcha 1.11 extern filter_t *filters;
22 lefcha 1.32 extern namesp_t nsppri;
23 lefcha 1.7
24 lefcha 1.10 unsigned int options; /* Program options. */
25 lefcha 1.29 unsigned int flags = 0; /* Program flags. */
26 lefcha 1.24 unsigned int capabilities; /* Capabilities of mail server. */
27 lefcha 1.29 unsigned int interval = 0; /* Poll at the specified interval. */
28 lefcha 1.41
29     char authmech[AUTH_MECH_LEN]; /* Authentication mechanisms. */
30 lefcha 1.5 char logfile[PATH_MAX]; /* Log file. */
31 lefcha 1.25 char *home = NULL; /* User's home directory. */
32 lefcha 1.40 char charset[CHARSET_LEN]; /* Charset for IMAP SEARCH requests. */
33 lefcha 1.42
34 lefcha 1.36 #ifdef MEMORY_LOCK
35 lefcha 1.26 uid_t ruid, euid; /* Real and effective UID. */
36 lefcha 1.42
37 lefcha 1.37 #endif
38 lefcha 1.35 jmp_buf acctloop;
39 lefcha 1.1
40 lefcha 1.29
41 lefcha 1.2 /*
42 lefcha 1.8 * In the beginning there was main()...
43 lefcha 1.2 */
44 lefcha 1.42 int
45     main(int argc, char *argv[])
46 lefcha 1.1 {
47 lefcha 1.42 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 lefcha 1.33
53 lefcha 1.36 #ifdef MEMORY_LOCK
54 lefcha 1.42 ruid = getuid();
55     euid = geteuid();
56     seteuid(ruid); /* Drop root privileges. */
57 lefcha 1.36 #endif
58 lefcha 1.28
59 lefcha 1.45 setlocale(LC_ALL, "");
60 lefcha 1.40
61 lefcha 1.42 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 lefcha 1.33
68 lefcha 1.42 while ((c = getopt(argc, argv, "c:d:hkl:"
69 lefcha 1.29 #ifdef ENCRYPTED_PASSWORDS
70 lefcha 1.42 "p"
71 lefcha 1.25 #endif
72 lefcha 1.42 "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 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
95 lefcha 1.42 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 lefcha 1.1 }
117 lefcha 1.32
118 lefcha 1.42 create_homedir();
119 lefcha 1.33
120 lefcha 1.42 lockfile_check();
121     lockfile_create();
122 lefcha 1.28
123 lefcha 1.42 corefile_disable();
124 lefcha 1.33
125 lefcha 1.42 tty_store();
126     catch_signals();
127 lefcha 1.24
128 lefcha 1.42 read_config(confile);
129 lefcha 1.33
130 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
131 lefcha 1.42 read_passwords();
132 lefcha 1.33
133 lefcha 1.42 if ((options & OPTION_PASSWORD_EDITOR)) {
134     password_editor();
135 lefcha 1.33
136 lefcha 1.42 secmem_clear();
137     lockfile_remove();
138 lefcha 1.33
139 lefcha 1.42 exit(0);
140     }
141 lefcha 1.25 #endif
142 lefcha 1.33
143 lefcha 1.42 open_logfile();
144 lefcha 1.32
145 lefcha 1.42 init_vbuf();
146 lefcha 1.32
147 lefcha 1.42 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 lefcha 1.7
155 lefcha 1.42 if (setjmp(acctloop))
156     continue;
157 lefcha 1.35
158 lefcha 1.42 if (init_connection(&sockpri, ca->server, ca->port,
159     ca->ssl))
160     continue;
161 lefcha 1.21
162 lefcha 1.42 r = greeting_response(&sockpri);
163 lefcha 1.18
164 lefcha 1.45 #ifdef DEBUG
165     test(&sockpri);
166     #endif
167    
168 lefcha 1.42 if (check_capabilities(&sockpri))
169     continue;
170 lefcha 1.1
171 lefcha 1.42 log_info(LOG_ACCOUNT, ca->key);
172 lefcha 1.1
173 lefcha 1.42 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 lefcha 1.43 apply_filters(cm->name, cm->filters);
196 lefcha 1.42 close_mailbox(&sockpri);
197     }
198     logout(&sockpri);
199 lefcha 1.33
200 lefcha 1.42 close_connection(&sockpri);
201 lefcha 1.33 }
202 lefcha 1.11
203 lefcha 1.42 /* 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 lefcha 1.36 #ifdef MEMORY_LOCK
214 lefcha 1.42 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 lefcha 1.44 flags |= FLAG_DAEMON_MODE;
222 lefcha 1.42 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 lefcha 1.17
234 lefcha 1.42 secmem_clear();
235     close_logfile();
236 lefcha 1.33
237 lefcha 1.42 lockfile_remove();
238 lefcha 1.33
239 lefcha 1.42 exit(0);
240 lefcha 1.1 }
241    
242    
243     /*
244 lefcha 1.11 * Print a very brief usage message.
245 lefcha 1.2 */
246 lefcha 1.42 void
247     usage(void)
248 lefcha 1.2 {
249 lefcha 1.42 fprintf(stderr,
250 lefcha 1.29 "usage: imapfilter [-hk"
251 lefcha 1.26 #ifdef ENCRYPTED_PASSWORDS
252     "p"
253 lefcha 1.33 #endif
254 lefcha 1.38 "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 lefcha 1.42 void
262     version(void)
263 lefcha 1.38 {
264 lefcha 1.42 fprintf(stderr, "IMAPFilter %s"
265 lefcha 1.38 #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 lefcha 1.42 );
273 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26