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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34.2.3 - (hide 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 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.34.2.1 #include <setjmp.h>
8 lefcha 1.1
9 lefcha 1.34.2.3 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
10     #include <openssl/crypto.h>
11     #endif
12    
13 lefcha 1.1 #include "config.h"
14     #include "imapfilter.h"
15 lefcha 1.11 #include "data.h"
16 lefcha 1.1
17    
18 lefcha 1.32 extern int sockpri;
19 lefcha 1.7 extern account_t *accounts;
20 lefcha 1.11 extern filter_t *filters;
21 lefcha 1.32 extern namesp_t nsppri;
22 lefcha 1.7
23 lefcha 1.10 unsigned int options; /* Program options. */
24 lefcha 1.29 unsigned int flags = 0; /* Program flags. */
25 lefcha 1.24 unsigned int capabilities; /* Capabilities of mail server. */
26 lefcha 1.29 unsigned int interval = 0; /* Poll at the specified interval. */
27 lefcha 1.5 char logfile[PATH_MAX]; /* Log file. */
28 lefcha 1.25 char *home = NULL; /* User's home directory. */
29 lefcha 1.34.2.2 #ifdef MEMORY_LOCK
30 lefcha 1.26 uid_t ruid, euid; /* Real and effective UID. */
31 lefcha 1.34.2.2 #endif
32 lefcha 1.34.2.1 jmp_buf acctloop;
33 lefcha 1.1
34 lefcha 1.29
35 lefcha 1.2 /*
36 lefcha 1.8 * In the beginning there was main()...
37 lefcha 1.2 */
38 lefcha 1.16 int main(int argc, char *argv[])
39 lefcha 1.1 {
40 lefcha 1.29 int c, r, f = 0;
41     pid_t pid;
42 lefcha 1.11 char *confile = NULL; /* Configuration file. */
43     account_t *ca; /* Current account. */
44     mbox_t *cm; /* Current mailbox. */
45 lefcha 1.33
46 lefcha 1.34.2.2 #ifdef MEMORY_LOCK
47 lefcha 1.26 ruid = getuid();
48     euid = geteuid();
49     seteuid(ruid); /* Drop root privileges. */
50 lefcha 1.34.2.2 #endif
51 lefcha 1.28
52     home = getenv("HOME");
53 lefcha 1.34 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE | OPTION_WARNING);
54 lefcha 1.12 *logfile = 0;
55 lefcha 1.33
56 lefcha 1.29 while ((c = getopt(argc, argv, "c:d:hkl:"
57     #ifdef ENCRYPTED_PASSWORDS
58     "p"
59 lefcha 1.25 #endif
60 lefcha 1.34.2.3 "qvV")) != -1) {
61 lefcha 1.1 switch (c) {
62 lefcha 1.3 case 'c':
63 lefcha 1.11 confile = optarg;
64 lefcha 1.3 break;
65 lefcha 1.29 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 lefcha 1.2 case 'h':
73     usage();
74 lefcha 1.12 exit(ERROR_UNDEFINED);
75 lefcha 1.2 break;
76 lefcha 1.29 case 'k':
77     kill_imapfilter();
78     break;
79 lefcha 1.1 case 'l':
80 lefcha 1.12 strncat(logfile, optarg, PATH_MAX - 1);
81 lefcha 1.1 break;
82 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
83     case 'p':
84     options |= OPTION_PASSWORD_EDITOR;
85     break;
86     #endif
87 lefcha 1.1 case 'q':
88 lefcha 1.11 options &= OPTION_DETAILS_CLEAR;
89 lefcha 1.15 options |= OPTION_DETAILS_QUIET;
90 lefcha 1.1 break;
91     case 'v':
92 lefcha 1.11 options &= OPTION_DETAILS_CLEAR;
93     options |= OPTION_DETAILS_VERBOSE;
94 lefcha 1.1 break;
95 lefcha 1.34.2.3 case 'V':
96     version();
97     exit(ERROR_UNDEFINED);
98     break;
99 lefcha 1.1 default:
100 lefcha 1.2 usage();
101 lefcha 1.12 exit(ERROR_UNDEFINED);
102 lefcha 1.1 break;
103     }
104     }
105 lefcha 1.32
106     create_homedir();
107 lefcha 1.33
108 lefcha 1.28 lockfile_check();
109     lockfile_create();
110    
111     corefile_disable();
112 lefcha 1.33
113 lefcha 1.26 tty_store();
114 lefcha 1.24 catch_signals();
115    
116 lefcha 1.12 read_config(confile);
117 lefcha 1.33
118 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
119     read_passwords();
120 lefcha 1.33
121 lefcha 1.32 if ((options & OPTION_PASSWORD_EDITOR)) {
122 lefcha 1.25 password_editor();
123 lefcha 1.33
124 lefcha 1.32 secmem_clear();
125     lockfile_remove();
126 lefcha 1.33
127 lefcha 1.32 exit(0);
128     }
129 lefcha 1.25 #endif
130 lefcha 1.33
131 lefcha 1.32 open_logfile();
132    
133     init_vbuf();
134    
135 lefcha 1.29 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 lefcha 1.7
143 lefcha 1.34.2.1 if (setjmp(acctloop))
144     continue;
145    
146 lefcha 1.33 if (init_connection(&sockpri, ca->server, ca->port, ca->ssl))
147     continue;
148 lefcha 1.21
149 lefcha 1.33 r = greeting_response(&sockpri);
150 lefcha 1.18
151 lefcha 1.34.2.1 if (check_capabilities(&sockpri))
152 lefcha 1.33 continue;
153 lefcha 1.1
154     #ifdef DEBUG
155 lefcha 1.33 test(&sockpri);
156 lefcha 1.1 #endif
157    
158 lefcha 1.33 if (r != RESPONSE_PREAUTH) {
159     if (ca->passwdattr == PASSWORD_NONE) {
160 lefcha 1.34.2.1 printf("Enter password for %s@%s: ", ca->username,
161     ca->server);
162 lefcha 1.33 get_password(ca->password, PASSWORD_LEN);
163     ca->passwdattr = PASSWORD_PLAIN;
164     }
165 lefcha 1.34.2.1 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 lefcha 1.33 continue;
170     }
171 lefcha 1.30 }
172 lefcha 1.33 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 lefcha 1.24 }
185 lefcha 1.11
186 lefcha 1.33 /* 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 lefcha 1.34.2.2 #ifdef MEMORY_LOCK
196 lefcha 1.33 secmem_lock();
197     setuid(ruid); /* Capability to regain root privileges will
198     not be needed any more. */
199 lefcha 1.34.2.2 #endif
200 lefcha 1.33 lockfile_create();
201     corefile_disable();
202     break;
203     default:
204     secmem_clear();
205     close_logfile();
206     exit(0);
207     break;
208 lefcha 1.14 }
209 lefcha 1.29 }
210 lefcha 1.33 if (interval)
211     sleep(interval);
212 lefcha 1.29 } while (options & OPTION_DAEMON_MODE && interval);
213 lefcha 1.17
214 lefcha 1.26 secmem_clear();
215 lefcha 1.1 close_logfile();
216 lefcha 1.33
217 lefcha 1.28 lockfile_remove();
218 lefcha 1.33
219 lefcha 1.1 exit(0);
220     }
221    
222    
223     /*
224 lefcha 1.11 * Print a very brief usage message.
225 lefcha 1.2 */
226     void usage(void)
227     {
228 lefcha 1.25 fprintf(stderr,
229 lefcha 1.29 "usage: imapfilter [-hk"
230 lefcha 1.26 #ifdef ENCRYPTED_PASSWORDS
231     "p"
232 lefcha 1.33 #endif
233 lefcha 1.34.2.3 "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 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26