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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.32 - (show annotations)
Tue Jun 18 21:16:34 2002 UTC (21 years, 9 months ago) by lefcha
Branch: MAIN
Changes since 1.31: +29 -17 lines
File MIME type: text/plain
Changes to reflect rcopy/rmove capability.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26