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

Contents of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.28 - (show annotations)
Wed Jan 30 19:19:00 2002 UTC (22 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.27: +9 -4 lines
File MIME type: text/plain
Added lockfile to prevent many imapfilters from running simultaneously.

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 capabilities; /* Capabilities of mail server. */
18 char logfile[PATH_MAX]; /* Log file. */
19 char *home = NULL; /* User's home directory. */
20 uid_t ruid, euid; /* Real and effective UID. */
21
22
23 /*
24 * In the beginning there was main()...
25 */
26 int main(int argc, char *argv[])
27 {
28 int c, r;
29 char *confile = NULL; /* Configuration file. */
30 account_t *ca; /* Current account. */
31 mbox_t *cm; /* Current mailbox. */
32
33 ruid = getuid();
34 euid = geteuid();
35 seteuid(ruid); /* Drop root privileges. */
36
37 home = getenv("HOME");
38 options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE);
39 *logfile = 0;
40
41 #ifndef ENCRYPTED_PASSWORDS
42 while ((c = getopt(argc, argv, "c:hl:qv")) != -1) {
43 #else
44 while ((c = getopt(argc, argv, "c:hl:pqv")) != -1) {
45 #endif
46 switch (c) {
47 case 'c':
48 confile = optarg;
49 break;
50 case 'h':
51 usage();
52 exit(ERROR_UNDEFINED);
53 break;
54 case 'l':
55 strncat(logfile, optarg, PATH_MAX - 1);
56 break;
57 #ifdef ENCRYPTED_PASSWORDS
58 case 'p':
59 options |= OPTION_PASSWORD_EDITOR;
60 break;
61 #endif
62 case 'q':
63 options &= OPTION_DETAILS_CLEAR;
64 options |= OPTION_DETAILS_QUIET;
65 break;
66 case 'v':
67 options &= OPTION_DETAILS_CLEAR;
68 options |= OPTION_DETAILS_VERBOSE;
69 break;
70 default:
71 usage();
72 exit(ERROR_UNDEFINED);
73 break;
74 }
75 }
76
77 lockfile_check();
78 lockfile_create();
79
80 corefile_disable();
81
82 tty_store();
83 catch_signals();
84
85 create_homedir();
86
87 read_config(confile);
88 open_logfile();
89
90 #ifdef ENCRYPTED_PASSWORDS
91 read_passwords();
92
93 if ((options & OPTION_PASSWORD_EDITOR))
94 password_editor();
95 #endif
96
97 setuid(ruid); /* Capability to regain root privileges
98 will not be needed any more. */
99
100
101 for (ca = accounts; ca; ca = ca->next) {
102
103 #ifndef SSL_TLS
104 if (init_connection(ca->server, ca->port))
105 #else
106 if (init_connection(ca->server, ca->port, ca->ssl))
107 #endif
108 continue;
109
110 r = greeting_response();
111
112 if (r == RESPONSE_BYE || check_capabilities())
113 continue;
114
115 #ifdef DEBUG
116 test();
117 #endif
118
119 if (r != RESPONSE_PREAUTH) {
120 if (ca->passwdattr == PASSWORD_NONE) {
121 printf("Enter password for %s@%s: ", ca->username, ca->server);
122 get_password(ca->password, PASSWORD_LEN);
123 }
124 if (login(ca->username, ca->password))
125 continue;
126 }
127 check_namespace();
128
129 for (cm = ca->mboxes; cm; cm = cm->next)
130 if (!*cm->filters)
131 mailbox_status(cm->name);
132 else if (!select_mailbox(cm->name)) {
133 apply_filters(cm->filters);
134 close_mailbox();
135 }
136 logout();
137
138 close_connection();
139 }
140
141 secmem_clear();
142 close_logfile();
143
144 lockfile_remove();
145
146 exit(0);
147 }
148
149
150 /*
151 * Print a very brief usage message.
152 */
153 void usage(void)
154 {
155 fprintf(stderr,
156 "usage: imapfilter [-h"
157 #ifdef ENCRYPTED_PASSWORDS
158 "p"
159 #endif
160 "qv] [-c configfile] [-l logfile]\n");
161 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26