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