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