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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.52 - (hide annotations)
Thu Jul 31 15:53:19 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.51: +12 -6 lines
File MIME type: text/plain
Broke up program files and created some new header files.

1 lefcha 1.1 #include <stdio.h>
2 lefcha 1.52 #include <string.h>
3 lefcha 1.1 #include <unistd.h>
4 lefcha 1.52 #include <errno.h>
5 lefcha 1.7 #include <limits.h>
6 lefcha 1.35 #include <setjmp.h>
7 lefcha 1.40 #include <locale.h>
8 lefcha 1.1
9 lefcha 1.39 #include "config.h"
10     #include "imapfilter.h"
11 lefcha 1.52 #include "version.h"
12     #include "account.h"
13     #include "filter.h"
14     #include "buffer.h"
15 lefcha 1.39
16 lefcha 1.38 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
17     #include <openssl/crypto.h>
18     #endif
19 lefcha 1.1
20    
21 lefcha 1.7 extern account_t *accounts;
22 lefcha 1.11 extern filter_t *filters;
23 lefcha 1.50 extern buffer_t ibuf, obuf;
24 lefcha 1.7
25 lefcha 1.10 unsigned int options; /* Program options. */
26 lefcha 1.29 unsigned int flags = 0; /* Program flags. */
27     unsigned int interval = 0; /* Poll at the specified interval. */
28 lefcha 1.51 conn_t connpri, connaux; /* Primary and auxiliary IMAP connection. */
29 lefcha 1.41
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.46 jmp_buf acctloop; /* Non-local exit in case of network error. */
35 lefcha 1.1
36 lefcha 1.29
37 lefcha 1.52 void usage(void);
38     void version(void);
39    
40    
41 lefcha 1.2 /*
42 lefcha 1.46 * IMAPFilter: an IMAP mail filtering utility.
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.45 setlocale(LC_ALL, "");
54 lefcha 1.40
55 lefcha 1.42 f = 0;
56     home = getenv("HOME");
57 lefcha 1.47 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE);
58 lefcha 1.42 *charset = 0;
59     *logfile = 0;
60     confile = NULL;
61 lefcha 1.33
62 lefcha 1.42 while ((c = getopt(argc, argv, "c:d:hkl:"
63 lefcha 1.29 #ifdef ENCRYPTED_PASSWORDS
64 lefcha 1.42 "p"
65 lefcha 1.25 #endif
66 lefcha 1.42 "qvV")) != -1) {
67     switch (c) {
68     case 'c':
69     confile = optarg;
70     break;
71     case 'd':
72     options |= OPTION_DAEMON_MODE;
73     errno = 0;
74     interval = strtoul(optarg, NULL, 10);
75     if (errno)
76     interval = 0;
77     break;
78     case 'h':
79     usage();
80     exit(ERROR_UNDEFINED);
81     break;
82     case 'k':
83     kill_imapfilter();
84     break;
85     case 'l':
86     strncat(logfile, optarg, PATH_MAX - 1);
87     break;
88 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
89 lefcha 1.42 case 'p':
90     options |= OPTION_PASSWORD_EDITOR;
91     break;
92     #endif
93     case 'q':
94     options &= OPTION_DETAILS_CLEAR;
95     options |= OPTION_DETAILS_QUIET;
96     break;
97     case 'v':
98     options &= OPTION_DETAILS_CLEAR;
99     options |= OPTION_DETAILS_VERBOSE;
100     break;
101     case 'V':
102     version();
103     exit(ERROR_UNDEFINED);
104     break;
105     default:
106     usage();
107     exit(ERROR_UNDEFINED);
108     break;
109     }
110 lefcha 1.1 }
111 lefcha 1.32
112 lefcha 1.42 create_homedir();
113 lefcha 1.33
114 lefcha 1.42 lockfile_check();
115     lockfile_create();
116 lefcha 1.28
117 lefcha 1.47 #ifndef DEBUG
118 lefcha 1.42 corefile_disable();
119 lefcha 1.47 #endif
120 lefcha 1.33
121 lefcha 1.42 tty_store();
122     catch_signals();
123 lefcha 1.24
124 lefcha 1.42 read_config(confile);
125 lefcha 1.33
126 lefcha 1.25 #ifdef ENCRYPTED_PASSWORDS
127 lefcha 1.42 read_passwords();
128 lefcha 1.33
129 lefcha 1.42 if ((options & OPTION_PASSWORD_EDITOR)) {
130     password_editor();
131 lefcha 1.33
132 lefcha 1.42 secmem_clear();
133     lockfile_remove();
134 lefcha 1.33
135 lefcha 1.42 exit(0);
136     }
137 lefcha 1.25 #endif
138 lefcha 1.33
139 lefcha 1.42 open_logfile();
140 lefcha 1.32
141 lefcha 1.50 init_buffer(&ibuf);
142     init_buffer(&obuf);
143 lefcha 1.32
144 lefcha 1.42 if (options & OPTION_DAEMON_MODE) {
145     f = 1;
146     options &= OPTION_DETAILS_CLEAR;
147     options |= OPTION_DETAILS_QUIET;
148     }
149     do {
150     for (ca = accounts; ca != NULL; ca = ca->next) {
151 lefcha 1.7
152 lefcha 1.42 if (setjmp(acctloop))
153     continue;
154 lefcha 1.35
155 lefcha 1.51 if (init_connection(&connpri, ca->server, ca->port,
156 lefcha 1.42 ca->ssl))
157     continue;
158 lefcha 1.21
159 lefcha 1.51 r = greeting_response(&connpri);
160 lefcha 1.18
161 lefcha 1.45 #ifdef DEBUG
162 lefcha 1.51 test(&connpri);
163 lefcha 1.45 #endif
164    
165 lefcha 1.51 if (check_capabilities(&connpri))
166 lefcha 1.42 continue;
167 lefcha 1.1
168 lefcha 1.49 #ifdef SSL_TLS
169     if (ca->ssl == SSL_DISABLED &&
170 lefcha 1.51 connpri.caps & CAPABILITY_STARTTLS)
171 lefcha 1.52 if (negotiate_tls(&connpri) == RESPONSE_OK)
172 lefcha 1.51 check_capabilities(&connpri);
173 lefcha 1.49 #endif
174    
175 lefcha 1.42 log_info(LOG_ACCOUNT, ca->key);
176 lefcha 1.1
177 lefcha 1.42 if (r != RESPONSE_PREAUTH) {
178     if (ca->passwdattr == PASSWORD_NONE) {
179     printf("Enter password for %s@%s: ",
180     ca->username, ca->server);
181     get_password(ca->password, PASSWORD_LEN);
182     ca->passwdattr = PASSWORD_PLAIN;
183     }
184 lefcha 1.48 #ifdef CRAM_MD5
185 lefcha 1.51 if (connpri.caps & CAPABILITY_AUTH_CRAM_MD5)
186 lefcha 1.52 r = auth_cram_md5(&connpri,
187 lefcha 1.48 ca->username, ca->password);
188     else
189     #endif
190 lefcha 1.51 r = login(&connpri, ca->username,
191 lefcha 1.48 ca->password);
192    
193     if (r == RESPONSE_NO) {
194 lefcha 1.42 error("username %s or password rejected "
195     "at %s\n", ca->username, ca->server);
196     continue;
197     }
198     }
199 lefcha 1.51 check_namespace(&connpri);
200 lefcha 1.42
201     for (cm = ca->mboxes; cm != NULL; cm = cm->next)
202     if (*cm->filters == NULL)
203 lefcha 1.51 mailbox_status(&connpri, cm->name);
204     else if (!select_mailbox(&connpri, cm->name)) {
205 lefcha 1.43 apply_filters(cm->name, cm->filters);
206 lefcha 1.51 close_mailbox(&connpri);
207 lefcha 1.42 }
208 lefcha 1.51 logout(&connpri);
209 lefcha 1.33
210 lefcha 1.51 close_connection(&connpri);
211 lefcha 1.33 }
212 lefcha 1.11
213 lefcha 1.42 /* Fork if in daemon mode. */
214     if (f) {
215     f = 0;
216     pid = fork();
217     switch (pid) {
218     case -1:
219     fatal(ERROR_FORK, "forking; %s\n",
220     strerror(errno));
221     break;
222     case 0:
223     lockfile_create();
224     corefile_disable();
225 lefcha 1.44 flags |= FLAG_DAEMON_MODE;
226 lefcha 1.42 break;
227     default:
228     secmem_clear();
229     close_logfile();
230     exit(0);
231     break;
232     }
233 lefcha 1.46 }
234     if (options & OPTION_DAEMON_MODE &&
235     flags & FLAG_SIGHUP_RECEIVED) {
236     reread_config(confile);
237     continue;
238 lefcha 1.42 }
239     if (interval)
240     sleep(interval);
241     } while (options & OPTION_DAEMON_MODE && interval);
242 lefcha 1.17
243 lefcha 1.42 secmem_clear();
244     close_logfile();
245 lefcha 1.33
246 lefcha 1.42 lockfile_remove();
247 lefcha 1.33
248 lefcha 1.48 return 0;
249 lefcha 1.1 }
250    
251    
252     /*
253 lefcha 1.11 * Print a very brief usage message.
254 lefcha 1.2 */
255 lefcha 1.42 void
256     usage(void)
257 lefcha 1.2 {
258 lefcha 1.42 fprintf(stderr,
259 lefcha 1.29 "usage: imapfilter [-hk"
260 lefcha 1.26 #ifdef ENCRYPTED_PASSWORDS
261     "p"
262 lefcha 1.33 #endif
263 lefcha 1.38 "qvV] [-c configfile] [-d interval] [-l logfile]\n");
264     }
265    
266    
267     /*
268     * Print program's version, and if it is built in, OpenSSL's version number.
269     */
270 lefcha 1.42 void
271     version(void)
272 lefcha 1.38 {
273 lefcha 1.42 fprintf(stderr, "IMAPFilter %s"
274 lefcha 1.38 #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
275     ", OpenSSL 0x%8.8lx"
276     #endif
277     "\n", IMAPFILTER_VERSION
278     #if defined SSL_TLS || defined ENCRYPTED_PASSWORDS
279     ,SSLeay()
280     #endif
281 lefcha 1.42 );
282 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26