35 |
char *confile = NULL; /* Configuration file. */ |
char *confile = NULL; /* Configuration file. */ |
36 |
account_t *ca; /* Current account. */ |
account_t *ca; /* Current account. */ |
37 |
mbox_t *cm; /* Current mailbox. */ |
mbox_t *cm; /* Current mailbox. */ |
38 |
|
|
39 |
ruid = getuid(); |
ruid = getuid(); |
40 |
euid = geteuid(); |
euid = geteuid(); |
41 |
seteuid(ruid); /* Drop root privileges. */ |
seteuid(ruid); /* Drop root privileges. */ |
43 |
home = getenv("HOME"); |
home = getenv("HOME"); |
44 |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE); |
options = (OPTION_DETAILS_NORMAL | OPTION_NAMESPACE); |
45 |
*logfile = 0; |
*logfile = 0; |
46 |
|
|
47 |
while ((c = getopt(argc, argv, "c:d:hkl:" |
while ((c = getopt(argc, argv, "c:d:hkl:" |
48 |
#ifdef ENCRYPTED_PASSWORDS |
#ifdef ENCRYPTED_PASSWORDS |
49 |
"p" |
"p" |
91 |
} |
} |
92 |
|
|
93 |
create_homedir(); |
create_homedir(); |
94 |
|
|
95 |
lockfile_check(); |
lockfile_check(); |
96 |
lockfile_create(); |
lockfile_create(); |
97 |
|
|
98 |
corefile_disable(); |
corefile_disable(); |
99 |
|
|
100 |
tty_store(); |
tty_store(); |
101 |
catch_signals(); |
catch_signals(); |
102 |
|
|
103 |
read_config(confile); |
read_config(confile); |
104 |
|
|
105 |
#ifdef ENCRYPTED_PASSWORDS |
#ifdef ENCRYPTED_PASSWORDS |
106 |
read_passwords(); |
read_passwords(); |
107 |
|
|
108 |
if ((options & OPTION_PASSWORD_EDITOR)) { |
if ((options & OPTION_PASSWORD_EDITOR)) { |
109 |
password_editor(); |
password_editor(); |
110 |
|
|
111 |
secmem_clear(); |
secmem_clear(); |
112 |
lockfile_remove(); |
lockfile_remove(); |
113 |
|
|
114 |
exit(0); |
exit(0); |
115 |
} |
} |
116 |
#endif |
#endif |
117 |
|
|
118 |
open_logfile(); |
open_logfile(); |
119 |
|
|
120 |
init_vbuf(); |
init_vbuf(); |
125 |
options &= OPTION_DETAILS_CLEAR; |
options &= OPTION_DETAILS_CLEAR; |
126 |
options |= OPTION_DETAILS_QUIET; |
options |= OPTION_DETAILS_QUIET; |
127 |
} |
} |
|
|
|
128 |
do { |
do { |
129 |
for (ca = accounts; ca; ca = ca->next) { |
for (ca = accounts; ca; ca = ca->next) { |
130 |
|
|
131 |
if (init_connection(&sockpri, ca->server, ca->port, ca->ssl)) |
if (init_connection(&sockpri, ca->server, ca->port, ca->ssl)) |
132 |
continue; |
continue; |
133 |
|
|
134 |
r = greeting_response(&sockpri); |
r = greeting_response(&sockpri); |
135 |
|
|
136 |
if (r == RESPONSE_BYE || check_capabilities(&sockpri)) |
if (r == RESPONSE_BYE || check_capabilities(&sockpri)) |
137 |
continue; |
continue; |
138 |
|
|
139 |
#ifdef DEBUG |
#ifdef DEBUG |
140 |
test(&sockpri); |
test(&sockpri); |
141 |
#endif |
#endif |
142 |
|
|
143 |
if (r != RESPONSE_PREAUTH) { |
if (r != RESPONSE_PREAUTH) { |
144 |
if (ca->passwdattr == PASSWORD_NONE) { |
if (ca->passwdattr == PASSWORD_NONE) { |
145 |
printf("Enter password for %s@%s: ", ca->username, ca->server); |
printf("Enter password for %s@%s: ", ca->username, ca->server); |
146 |
get_password(ca->password, PASSWORD_LEN); |
get_password(ca->password, PASSWORD_LEN); |
147 |
ca->passwdattr = PASSWORD_PLAIN; |
ca->passwdattr = PASSWORD_PLAIN; |
148 |
} |
} |
149 |
if (login(&sockpri, ca->username, ca->password) == RESPONSE_NO) { |
if (login(&sockpri, ca->username, ca->password) == RESPONSE_NO) { |
150 |
error("imapfilter: username %s or password rejected at %s\n", |
error("imapfilter: username %s or password rejected at %s\n", |
151 |
ca->username, ca->server); |
ca->username, ca->server); |
152 |
continue; |
continue; |
153 |
|
} |
154 |
} |
} |
155 |
} |
check_namespace(&sockpri, &nsppri); |
|
check_namespace(&sockpri, &nsppri); |
|
156 |
|
|
157 |
for (cm = ca->mboxes; cm; cm = cm->next) |
for (cm = ca->mboxes; cm; cm = cm->next) |
158 |
if (!*cm->filters) |
if (!*cm->filters) |
159 |
mailbox_status(&sockpri, cm->name, &nsppri); |
mailbox_status(&sockpri, cm->name, &nsppri); |
160 |
else if (!select_mailbox(&sockpri, cm->name, &nsppri)) { |
else if (!select_mailbox(&sockpri, cm->name, &nsppri)) { |
161 |
apply_filters(cm->filters); |
apply_filters(cm->filters); |
162 |
close_mailbox(&sockpri); |
close_mailbox(&sockpri); |
163 |
} |
} |
164 |
logout(&sockpri); |
logout(&sockpri); |
165 |
|
|
166 |
close_connection(&sockpri); |
close_connection(&sockpri); |
|
} |
|
|
|
|
|
/* Fork if in daemon mode. */ |
|
|
if (f) { |
|
|
f = 0; |
|
|
pid = fork(); |
|
|
switch (pid) { |
|
|
case -1: |
|
|
fatal(ERROR_FORK, "imapfilter: forking; %s\n", strerror(errno)); |
|
|
break; |
|
|
case 0: |
|
|
secmem_lock(); |
|
|
setuid(ruid); /* Capability to regain root privileges |
|
|
will not be needed any more. */ |
|
|
lockfile_create(); |
|
|
corefile_disable(); |
|
|
break; |
|
|
default: |
|
|
secmem_clear(); |
|
|
close_logfile(); |
|
|
exit(0); |
|
|
break; |
|
167 |
} |
} |
|
} |
|
168 |
|
|
169 |
if (interval) |
/* Fork if in daemon mode. */ |
170 |
sleep(interval); |
if (f) { |
171 |
|
f = 0; |
172 |
|
pid = fork(); |
173 |
|
switch (pid) { |
174 |
|
case -1: |
175 |
|
fatal(ERROR_FORK, "imapfilter: forking; %s\n", strerror(errno)); |
176 |
|
break; |
177 |
|
case 0: |
178 |
|
secmem_lock(); |
179 |
|
setuid(ruid); /* Capability to regain root privileges will |
180 |
|
not be needed any more. */ |
181 |
|
lockfile_create(); |
182 |
|
corefile_disable(); |
183 |
|
break; |
184 |
|
default: |
185 |
|
secmem_clear(); |
186 |
|
close_logfile(); |
187 |
|
exit(0); |
188 |
|
break; |
189 |
|
} |
190 |
|
} |
191 |
|
if (interval) |
192 |
|
sleep(interval); |
193 |
} while (options & OPTION_DAEMON_MODE && interval); |
} while (options & OPTION_DAEMON_MODE && interval); |
194 |
|
|
195 |
secmem_clear(); |
secmem_clear(); |
196 |
close_logfile(); |
close_logfile(); |
197 |
|
|
198 |
lockfile_remove(); |
lockfile_remove(); |
199 |
|
|
200 |
exit(0); |
exit(0); |
201 |
} |
} |
202 |
|
|
210 |
"usage: imapfilter [-hk" |
"usage: imapfilter [-hk" |
211 |
#ifdef ENCRYPTED_PASSWORDS |
#ifdef ENCRYPTED_PASSWORDS |
212 |
"p" |
"p" |
213 |
#endif |
#endif |
214 |
"qv] [-c configfile] [-d interval] [-l logfile]\n"); |
"qv] [-c configfile] [-d interval] [-l logfile]\n"); |
215 |
} |
} |