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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.40 - (hide annotations)
Mon Feb 3 20:22:01 2003 UTC (21 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.39: +5 -0 lines
File MIME type: text/plain
Added i18n support, variable charset to declare character set to the server.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26