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