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 |
lefcha |
1.12 |
|
36 |
lefcha |
1.26 |
seteuid(ruid); /* Drop root privileges. */ |
37 |
|
|
|
38 |
|
|
corefile_disable(); |
39 |
|
|
|
40 |
lefcha |
1.24 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE); |
41 |
lefcha |
1.12 |
*logfile = 0; |
42 |
lefcha |
1.25 |
home = getenv("HOME"); |
43 |
lefcha |
1.26 |
|
44 |
lefcha |
1.25 |
#ifndef ENCRYPTED_PASSWORDS |
45 |
lefcha |
1.11 |
while ((c = getopt(argc, argv, "c:hl:qv")) != -1) { |
46 |
lefcha |
1.25 |
#else |
47 |
|
|
while ((c = getopt(argc, argv, "c:hl:pqv")) != -1) { |
48 |
|
|
#endif |
49 |
lefcha |
1.1 |
switch (c) { |
50 |
lefcha |
1.3 |
case 'c': |
51 |
lefcha |
1.11 |
confile = optarg; |
52 |
lefcha |
1.3 |
break; |
53 |
lefcha |
1.2 |
case 'h': |
54 |
|
|
usage(); |
55 |
lefcha |
1.12 |
exit(ERROR_UNDEFINED); |
56 |
lefcha |
1.2 |
break; |
57 |
lefcha |
1.1 |
case 'l': |
58 |
lefcha |
1.12 |
strncat(logfile, optarg, PATH_MAX - 1); |
59 |
lefcha |
1.1 |
break; |
60 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
61 |
|
|
case 'p': |
62 |
|
|
options |= OPTION_PASSWORD_EDITOR; |
63 |
|
|
break; |
64 |
|
|
#endif |
65 |
lefcha |
1.1 |
case 'q': |
66 |
lefcha |
1.11 |
options &= OPTION_DETAILS_CLEAR; |
67 |
lefcha |
1.15 |
options |= OPTION_DETAILS_QUIET; |
68 |
lefcha |
1.1 |
break; |
69 |
|
|
case 'v': |
70 |
lefcha |
1.11 |
options &= OPTION_DETAILS_CLEAR; |
71 |
|
|
options |= OPTION_DETAILS_VERBOSE; |
72 |
lefcha |
1.1 |
break; |
73 |
|
|
default: |
74 |
lefcha |
1.2 |
usage(); |
75 |
lefcha |
1.12 |
exit(ERROR_UNDEFINED); |
76 |
lefcha |
1.1 |
break; |
77 |
|
|
} |
78 |
|
|
} |
79 |
lefcha |
1.13 |
|
80 |
lefcha |
1.26 |
tty_store(); |
81 |
lefcha |
1.24 |
catch_signals(); |
82 |
|
|
|
83 |
lefcha |
1.25 |
create_homedir(); |
84 |
|
|
|
85 |
lefcha |
1.12 |
read_config(confile); |
86 |
|
|
open_logfile(); |
87 |
lefcha |
1.21 |
|
88 |
lefcha |
1.25 |
#ifdef ENCRYPTED_PASSWORDS |
89 |
|
|
read_passwords(); |
90 |
|
|
|
91 |
|
|
if ((options & OPTION_PASSWORD_EDITOR)) |
92 |
|
|
password_editor(); |
93 |
|
|
#endif |
94 |
lefcha |
1.26 |
|
95 |
|
|
setuid(ruid); /* Capability to regain root privileges |
96 |
|
|
will not be needed any more. */ |
97 |
|
|
|
98 |
lefcha |
1.25 |
|
99 |
lefcha |
1.7 |
for (ca = accounts; ca; ca = ca->next) { |
100 |
|
|
|
101 |
lefcha |
1.16 |
#ifndef SSL_TLS |
102 |
lefcha |
1.19 |
if (init_connection(ca->server, ca->port)) |
103 |
lefcha |
1.16 |
#else |
104 |
lefcha |
1.19 |
if (init_connection(ca->server, ca->port, ca->ssl)) |
105 |
lefcha |
1.16 |
#endif |
106 |
lefcha |
1.19 |
continue; |
107 |
lefcha |
1.21 |
|
108 |
lefcha |
1.18 |
r = greeting_response(); |
109 |
|
|
|
110 |
|
|
if (r == RESPONSE_BYE || check_capabilities()) |
111 |
lefcha |
1.15 |
continue; |
112 |
lefcha |
1.1 |
|
113 |
|
|
#ifdef DEBUG |
114 |
lefcha |
1.11 |
test(); |
115 |
lefcha |
1.1 |
#endif |
116 |
|
|
|
117 |
lefcha |
1.24 |
if (r != RESPONSE_PREAUTH) { |
118 |
lefcha |
1.25 |
if (ca->passwdattr == PASSWORD_NONE) { |
119 |
|
|
printf("Enter password for %s@%s: ", ca->username, ca->server); |
120 |
|
|
get_password(ca->password, PASSWORD_LEN); |
121 |
|
|
} |
122 |
lefcha |
1.24 |
if (login(ca->username, ca->password)) |
123 |
|
|
continue; |
124 |
|
|
} |
125 |
|
|
check_namespace(); |
126 |
lefcha |
1.11 |
|
127 |
lefcha |
1.14 |
for (cm = ca->mboxes; cm; cm = cm->next) |
128 |
lefcha |
1.15 |
if (!*cm->filters) |
129 |
|
|
mailbox_status(cm->name); |
130 |
|
|
else if (!select_mailbox(cm->name)) { |
131 |
|
|
apply_filters(cm->filters); |
132 |
lefcha |
1.14 |
close_mailbox(); |
133 |
|
|
} |
134 |
lefcha |
1.11 |
logout(); |
135 |
lefcha |
1.1 |
|
136 |
lefcha |
1.5 |
close_connection(); |
137 |
lefcha |
1.22 |
} |
138 |
lefcha |
1.17 |
|
139 |
lefcha |
1.26 |
secmem_clear(); |
140 |
lefcha |
1.1 |
close_logfile(); |
141 |
lefcha |
1.26 |
corefile_restore(); |
142 |
lefcha |
1.1 |
|
143 |
|
|
exit(0); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
|
147 |
|
|
/* |
148 |
lefcha |
1.11 |
* Print a very brief usage message. |
149 |
lefcha |
1.2 |
*/ |
150 |
|
|
void usage(void) |
151 |
|
|
{ |
152 |
lefcha |
1.25 |
fprintf(stderr, |
153 |
lefcha |
1.26 |
"usage: imapfilter [-h" |
154 |
|
|
#ifdef ENCRYPTED_PASSWORDS |
155 |
|
|
"p" |
156 |
|
|
#endif |
157 |
|
|
"qv] [-c configfile] [-l logfile]\n"); |
158 |
lefcha |
1.1 |
} |