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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.33 - (show annotations)
Fri Jul 26 14:45:28 2002 UTC (21 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.32: +63 -65 lines
File MIME type: text/plain
Stylistic changes - K&R indentation.

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 do {
129 for (ca = accounts; ca; ca = ca->next) {
130
131 if (init_connection(&sockpri, ca->server, ca->port, ca->ssl))
132 continue;
133
134 r = greeting_response(&sockpri);
135
136 if (r == RESPONSE_BYE || check_capabilities(&sockpri))
137 continue;
138
139 #ifdef DEBUG
140 test(&sockpri);
141 #endif
142
143 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 }
155 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 }
168
169 /* 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 }
190 }
191 if (interval)
192 sleep(interval);
193 } while (options & OPTION_DAEMON_MODE && interval);
194
195 secmem_clear();
196 close_logfile();
197
198 lockfile_remove();
199
200 exit(0);
201 }
202
203
204 /*
205 * Print a very brief usage message.
206 */
207 void usage(void)
208 {
209 fprintf(stderr,
210 "usage: imapfilter [-hk"
211 #ifdef ENCRYPTED_PASSWORDS
212 "p"
213 #endif
214 "qv] [-c configfile] [-d interval] [-l logfile]\n");
215 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26