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