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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.29 - (show annotations)
Thu Jan 31 17:08:13 2002 UTC (22 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.28: +59 -14 lines
File MIME type: text/plain
Added daemon mode.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26