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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (hide annotations)
Tue Sep 24 18:49:34 2002 UTC (21 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.35: +5 -0 lines
File MIME type: text/plain
Option to disable memory locking during compilation.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26