1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <string.h> |
4 |
#include <ctype.h> |
5 |
#include <limits.h> |
6 |
#include <sys/types.h> /* IEEE Std 1003.1-2001 non-conformance. */ |
7 |
#include <regex.h> |
8 |
|
9 |
#include "config.h" |
10 |
#include "imapfilter.h" |
11 |
#include "account.h" |
12 |
#include "struct.h" |
13 |
|
14 |
|
15 |
extern unsigned int flags; |
16 |
|
17 |
account_t *accounts = NULL; /* First node of accounts linked list. */ |
18 |
mboxgrp_t *mboxgrps = NULL; /* First node of mailbox-groups tree. */ |
19 |
|
20 |
account_t *cur_acct = NULL; /* Current account. */ |
21 |
|
22 |
|
23 |
void init_account(account_t * node); |
24 |
|
25 |
void init_mboxgrp(mboxgrp_t * node); |
26 |
void process_mboxgrp(mboxgrp_t * node, char *mboxs); |
27 |
|
28 |
void init_mbox(mbox_t * node); |
29 |
mbox_t *set_mbox(char *name); |
30 |
|
31 |
int string_decode(char *str); |
32 |
|
33 |
|
34 |
/* |
35 |
* Set new account's variables to safe values. |
36 |
*/ |
37 |
void |
38 |
init_account(account_t * node) |
39 |
{ |
40 |
node->next = NULL; |
41 |
node->key[0] = node->server[0] = '\0'; |
42 |
node->username[0] = node->password[0] = '\0'; |
43 |
node->passwdattr = PASSWORD_NONE; |
44 |
node->port = 143; |
45 |
node->ssl = SSL_DISABLED; |
46 |
node->mboxes = NULL; |
47 |
} |
48 |
|
49 |
|
50 |
/* |
51 |
* A new account entry was declared. Create it and set it's variables |
52 |
* accordingly. |
53 |
*/ |
54 |
int |
55 |
set_account(char *line, regmatch_t * m) |
56 |
{ |
57 |
int n; |
58 |
char p[6]; |
59 |
account_t *node; |
60 |
|
61 |
node = (account_t *) xmalloc(sizeof(account_t)); |
62 |
node->password = (char *)smalloc(PASSWORD_LEN); |
63 |
|
64 |
init_account(node); |
65 |
|
66 |
strncat(node->key, line + m[1].rm_so, |
67 |
min(m[1].rm_eo - m[1].rm_so, KEY_LEN - 1)); |
68 |
|
69 |
debug("ACCOUNT: '%s'\n", node->key); |
70 |
|
71 |
if (m[3].rm_so != -1) { |
72 |
strncat(node->username, line + m[3].rm_so, |
73 |
min(m[3].rm_eo - m[3].rm_so, USERNAME_LEN - 1)); |
74 |
} else { |
75 |
strncat(node->username, line + m[5].rm_so, |
76 |
min(m[5].rm_eo - m[5].rm_so, USERNAME_LEN - 1)); |
77 |
} |
78 |
if (strchr(node->username, '%')) |
79 |
if (string_decode(node->username)) |
80 |
return ERROR_CONFIG_PARSE; |
81 |
|
82 |
debug("USERNAME: '%s'\n", node->username); |
83 |
|
84 |
if (m[4].rm_so != -1) { |
85 |
strncat(node->password, line + m[4].rm_so, |
86 |
min(m[4].rm_eo - m[4].rm_so, PASSWORD_LEN - 1)); |
87 |
if (strchr(node->password, '%')) |
88 |
if (string_decode(node->password)) |
89 |
return ERROR_CONFIG_PARSE; |
90 |
node->passwdattr = PASSWORD_PLAIN; |
91 |
debug("PASSWORD: ''\n"); |
92 |
} else |
93 |
flags |= FLAG_BLANK_PASSWORD; |
94 |
|
95 |
strncat(node->server, line + m[6].rm_so, |
96 |
min(m[6].rm_eo - m[6].rm_so, SERVER_LEN - 1)); |
97 |
|
98 |
debug("SERVER: '%s'\n", node->server); |
99 |
|
100 |
if (m[7].rm_so != -1) { |
101 |
n = min(m[7].rm_eo - m[7].rm_so - 1, 5); |
102 |
xstrncpy(p, line + m[7].rm_so + 1, n); |
103 |
p[n] = '\0'; |
104 |
node->port = strtoul(p, NULL, 10); |
105 |
debug("PORT: %d\n", node->port); |
106 |
} |
107 |
if (m[8].rm_so != -1) { |
108 |
if (m[7].rm_so == -1) |
109 |
node->port = 993; |
110 |
|
111 |
if (strcasestr(line + m[8].rm_so, "SSL2")) |
112 |
node->ssl = SSL_SSL_V2; |
113 |
else if (strcasestr(line + m[8].rm_so, "SSL3")) |
114 |
node->ssl = SSL_SSL_V3; |
115 |
else |
116 |
node->ssl = SSL_TLS_V1; |
117 |
} |
118 |
APPEND_LINKED_LIST(accounts, node, account); |
119 |
cur_acct = node; |
120 |
|
121 |
return 0; |
122 |
} |
123 |
|
124 |
|
125 |
#ifdef ENCRYPTED_PASSWORDS |
126 |
/* |
127 |
* Find accounts without a password (candicates for password encryption). |
128 |
*/ |
129 |
char * |
130 |
find_password(char *user, char *serv) |
131 |
{ |
132 |
account_t *a; |
133 |
|
134 |
for (a = accounts; a != NULL; a = a->next) |
135 |
if (a->passwdattr == PASSWORD_NONE && |
136 |
!strcmp(a->server, serv) && !strcmp(a->username, user)) { |
137 |
a->passwdattr = PASSWORD_ENCRYPTED; |
138 |
return a->password; |
139 |
} |
140 |
return NULL; |
141 |
} |
142 |
#endif |
143 |
|
144 |
|
145 |
/* |
146 |
* Set new mailbox-group's variables to safe values. |
147 |
*/ |
148 |
void |
149 |
init_mboxgrp(mboxgrp_t * node) |
150 |
{ |
151 |
node->left = node->right = NULL; |
152 |
node->key[0] = '\0'; |
153 |
node->mboxes[0] = NULL; |
154 |
} |
155 |
|
156 |
|
157 |
/* |
158 |
* A new mailbox-group entry was declared. Create it and set it's variables |
159 |
* accordingly. |
160 |
*/ |
161 |
int |
162 |
set_mboxgrp(char *line, regmatch_t * m) |
163 |
{ |
164 |
mboxgrp_t *node; |
165 |
char mboxs[LINE_MAX]; |
166 |
|
167 |
mboxs[0] = '\0'; |
168 |
|
169 |
if (accounts == NULL) |
170 |
return ERROR_CONFIG_PARSE; |
171 |
|
172 |
node = (mboxgrp_t *) xmalloc(sizeof(mboxgrp_t)); |
173 |
|
174 |
init_mboxgrp(node); |
175 |
|
176 |
strncat(node->key, line + m[1].rm_so, |
177 |
min(m[1].rm_eo - m[1].rm_so, KEY_LEN - 1)); |
178 |
|
179 |
debug("FOLDER: '%s'\n", node->key); |
180 |
|
181 |
strncat(mboxs, line + m[2].rm_so, |
182 |
min(m[2].rm_eo - m[2].rm_so, LINE_MAX - 1)); |
183 |
|
184 |
process_mboxgrp(node, mboxs); |
185 |
|
186 |
INSERT_TREE(mboxgrps, node, mboxgrp); |
187 |
|
188 |
return 0; |
189 |
} |
190 |
|
191 |
|
192 |
/* |
193 |
* Calls set_mbox() in order to create mailboxes that are part of |
194 |
* the mailbox-group. |
195 |
*/ |
196 |
void |
197 |
process_mboxgrp(mboxgrp_t * node, char *mboxs) |
198 |
{ |
199 |
unsigned int i; |
200 |
char *tok; |
201 |
|
202 |
i = 0; |
203 |
|
204 |
tok = strtok_r(mboxs, ",", &mboxs); |
205 |
while (i < MBOXGRP_MBOXES_MAX - 1 && tok != NULL) { |
206 |
node->mboxes[i] = (mbox_t *) set_mbox(tok); |
207 |
node->mboxes[++i] = NULL; |
208 |
|
209 |
tok = strtok_r(NULL, ",", &mboxs); |
210 |
} |
211 |
} |
212 |
|
213 |
|
214 |
/* |
215 |
* Set new mailbox's variables to safe values. |
216 |
*/ |
217 |
void |
218 |
init_mbox(mbox_t * node) |
219 |
{ |
220 |
node->next = NULL; |
221 |
node->name[0] = '\0'; |
222 |
node->filters[0] = NULL; |
223 |
} |
224 |
|
225 |
|
226 |
/* |
227 |
* A new mailbox was declared, create it and set it's variables accordingly. |
228 |
*/ |
229 |
mbox_t * |
230 |
set_mbox(char *name) |
231 |
{ |
232 |
char s[MBOX_NAME_LEN]; |
233 |
mbox_t *node, *m; |
234 |
|
235 |
*s = '\0'; |
236 |
|
237 |
if (*name == '"' && *(name + strlen(name) - 1) == '"') |
238 |
strncat(s, name + 1, min(strlen(name) - 2, MBOX_NAME_LEN - 1)); |
239 |
else |
240 |
strncat(s, name, min(strlen(name), MBOX_NAME_LEN - 1)); |
241 |
|
242 |
for (m = cur_acct->mboxes; m != NULL; m = m->next) |
243 |
if (!strcmp(m->name, s)) |
244 |
return m; |
245 |
|
246 |
node = (mbox_t *) xmalloc(sizeof(mbox_t)); |
247 |
|
248 |
init_mbox(node); |
249 |
|
250 |
strncat(node->name, s, MBOX_NAME_LEN - 1); |
251 |
|
252 |
debug("MBOX: '%s'\n", node->name); |
253 |
|
254 |
APPEND_LINKED_LIST(cur_acct->mboxes, node, mbox); |
255 |
|
256 |
return node; |
257 |
} |
258 |
|
259 |
|
260 |
/* |
261 |
* Decode the character triplet, consisting of the character '%' |
262 |
* followed by two hexadecimal digits to its corresponding ASCII |
263 |
* character (described in Section 2.2 of RFC 1738). |
264 |
*/ |
265 |
int |
266 |
string_decode(char *str) |
267 |
{ |
268 |
char *c; |
269 |
char hex[3]; |
270 |
|
271 |
c = str; |
272 |
|
273 |
while (*c != '\0') { |
274 |
if (*c == '%') { |
275 |
if (!isxdigit((unsigned char)(*(c + 1))) || |
276 |
!isxdigit((unsigned char)(*(c + 2)))) |
277 |
return ERROR_CONFIG_PARSE; |
278 |
|
279 |
xstrncpy(hex, ++c, 2); |
280 |
hex[2] = '\0'; |
281 |
|
282 |
if (!isprint((unsigned char)(*str = (char)strtoul(hex, |
283 |
NULL, 16)))) |
284 |
return ERROR_CONFIG_PARSE; |
285 |
|
286 |
str++; |
287 |
c += 2; |
288 |
} else |
289 |
*(str++) = *(c++); |
290 |
} |
291 |
*str = '\0'; |
292 |
|
293 |
return 0; |
294 |
} |
295 |
|
296 |
|
297 |
/* |
298 |
* Convert the names of personal mailboxes using the namespace specified |
299 |
* by the mail server. |
300 |
*/ |
301 |
char * |
302 |
apply_namespace(char *mbox, char *prefix, char delim) |
303 |
{ |
304 |
static char m[MBOX_NAME_LEN]; |
305 |
char *c; |
306 |
|
307 |
if ((prefix[0] == '\0' && delim == '\0') || |
308 |
(prefix[0] == '\0' && delim == '/') || |
309 |
!strcasecmp(mbox, "INBOX")) |
310 |
return mbox; |
311 |
|
312 |
m[0] = '\0'; |
313 |
strncat(m, prefix, MBOX_NAME_LEN - 1); |
314 |
strncat(m, mbox, MBOX_NAME_LEN - strlen(m) - 1); |
315 |
|
316 |
c = m; |
317 |
while ((c = strchr(c, '/'))) |
318 |
*(c++) = delim; |
319 |
|
320 |
debug("MAILBOX: '%s'\n", m); |
321 |
|
322 |
return m; |
323 |
} |