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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (hide annotations)
Fri Jul 26 18:18:42 2002 UTC (21 years, 8 months ago) by lefcha
Branch: MAIN
CVS Tags: release-0_8
Branch point for: release-0_8-patches
Changes since 1.33: +1 -1 lines
File MIME type: text/plain
New variable about mlock() warning.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26