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

Annotation of /imapfilter/imapfilter.c

Parent Directory Parent Directory | Revision Log Revision Log


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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26