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