1 |
#include <stdio.h> |
#include <stdio.h> |
2 |
|
#include <stdlib.h> |
3 |
#include <string.h> |
#include <string.h> |
4 |
#include <ctype.h> |
#include <ctype.h> |
5 |
#include <limits.h> |
#include <limits.h> |
6 |
|
#include <sys/types.h> /* For POSIX.1-2001 non-conformant systems. */ |
7 |
|
#include <regex.h> |
8 |
|
|
9 |
#include "config.h" |
#include "config.h" |
10 |
#include "imapfilter.h" |
#include "imapfilter.h" |
12 |
#include "struct.h" |
#include "struct.h" |
13 |
|
|
14 |
|
|
15 |
|
extern options_t opts; |
16 |
extern unsigned int flags; |
extern unsigned int flags; |
17 |
|
|
18 |
account_t *accounts = NULL; /* First node of accounts linked list. */ |
account_t *accounts = NULL; /* First node of accounts linked list. */ |
19 |
mboxgrp_t *mboxgrps = NULL; /* First node of mailbox-groups tree. */ |
mboxgrp_t *mboxgrps = NULL; /* First node of mailbox-groups tree. */ |
20 |
|
|
21 |
static account_t *cur_acct = NULL; /* Current account. */ |
account_t *curacct = NULL; /* Current account. */ |
22 |
|
|
23 |
|
|
24 |
void init_account(account_t * node); |
void init_account(account_t * a); |
25 |
|
|
26 |
void init_mboxgrp(mboxgrp_t * node); |
void init_mboxgrp(mboxgrp_t * g); |
27 |
void process_mboxgrp(mboxgrp_t * node, char *mboxs); |
void process_mboxgrp(mboxgrp_t * g, char *mboxs); |
28 |
|
|
29 |
void init_mbox(mbox_t * node); |
void init_mbox(mbox_t * m); |
30 |
mbox_t *set_mbox(char *name); |
mbox_t *set_mbox(char *name); |
31 |
|
|
32 |
int string_decode(char *str); |
int string_decode(char *str); |
36 |
* Set new account's variables to safe values. |
* Set new account's variables to safe values. |
37 |
*/ |
*/ |
38 |
void |
void |
39 |
init_account(account_t * node) |
init_account(account_t * a) |
40 |
{ |
{ |
41 |
node->next = NULL; |
|
42 |
node->key[0] = node->server[0] = '\0'; |
a->next = NULL; |
43 |
node->username[0] = node->password[0] = '\0'; |
a->key[0] = '\0'; |
44 |
node->passwdattr = PASSWORD_NONE; |
a->server[0] = '\0'; |
45 |
node->port = 143; |
a->user[0] = '\0'; |
46 |
node->ssl = SSL_DISABLED; |
a->pass[0] = '\0'; |
47 |
node->mboxes = NULL; |
a->pass_attr = PASS_ATTR_NONE; |
48 |
|
a->port = 143; |
49 |
|
a->ssl = SSL_DISABLED; |
50 |
|
a->mboxes = NULL; |
51 |
} |
} |
52 |
|
|
53 |
|
|
60 |
{ |
{ |
61 |
int n; |
int n; |
62 |
char p[6]; |
char p[6]; |
63 |
account_t *node; |
account_t *a; |
64 |
|
|
65 |
node = (account_t *) xmalloc(sizeof(account_t)); |
a = (account_t *) xmalloc(sizeof(account_t)); |
66 |
node->password = (char *)smalloc(PASSWORD_LEN); |
a->pass = (char *)smalloc(PASS_LEN); |
67 |
|
|
68 |
init_account(node); |
init_account(a); |
69 |
|
|
70 |
strncat(node->key, line + m[1].rm_so, |
strncat(a->key, line + m[1].rm_so, min(m[1].rm_eo - m[1].rm_so, |
71 |
min(m[1].rm_eo - m[1].rm_so, KEY_LEN - 1)); |
KEY_LEN - 1)); |
72 |
|
|
73 |
#ifdef DEBUG |
debug("account: '%s'\n", a->key); |
|
fprintf(stderr, "debug: ACCOUNT: '%s'\n", node->key); |
|
|
#endif |
|
74 |
|
|
75 |
if (m[3].rm_so != -1) { |
if (m[3].rm_so != -1) |
76 |
strncat(node->username, line + m[3].rm_so, |
strncat(a->user, line + m[3].rm_so, |
77 |
min(m[3].rm_eo - m[3].rm_so, USERNAME_LEN - 1)); |
min(m[3].rm_eo - m[3].rm_so, USER_LEN - 1)); |
78 |
} else { |
else |
79 |
strncat(node->username, line + m[5].rm_so, |
strncat(a->user, line + m[5].rm_so, |
80 |
min(m[5].rm_eo - m[5].rm_so, USERNAME_LEN - 1)); |
min(m[5].rm_eo - m[5].rm_so, USER_LEN - 1)); |
|
} |
|
|
if (strchr(node->username, '%')) |
|
|
if (string_decode(node->username)) |
|
|
return ERROR_CONFIG_PARSE; |
|
81 |
|
|
82 |
#ifdef DEBUG |
if (strchr(a->user, '%')) |
83 |
fprintf(stderr, "debug: USERNAME: '%s'\n", node->username); |
if (string_decode(a->user)) |
84 |
#endif |
return ERROR_PARSER; |
85 |
|
|
86 |
|
debug("username: '%s'\n", a->user); |
87 |
|
|
88 |
if (m[4].rm_so != -1) { |
if (m[4].rm_so != -1) { |
89 |
strncat(node->password, line + m[4].rm_so, |
strncat(a->pass, line + m[4].rm_so, |
90 |
min(m[4].rm_eo - m[4].rm_so, PASSWORD_LEN - 1)); |
min(m[4].rm_eo - m[4].rm_so, PASS_LEN - 1)); |
91 |
if (strchr(node->password, '%')) |
if (strchr(a->pass, '%')) |
92 |
if (string_decode(node->password)) |
if (string_decode(a->pass)) |
93 |
return ERROR_CONFIG_PARSE; |
return ERROR_PARSER; |
94 |
node->passwdattr = PASSWORD_PLAIN; |
a->pass_attr = PASS_ATTR_PLAIN; |
95 |
|
if (opts.debug == 1) |
96 |
|
debug("password: *\n"); |
97 |
|
else |
98 |
|
debug("password: '%s'\n", a->pass); |
99 |
} else |
} else |
100 |
flags |= FLAG_BLANK_PASSWORD; |
flags |= FLAG_BLANKPASS; |
|
|
|
|
#ifdef DEBUG |
|
|
fprintf(stderr, "debug: PASSWORD: '%s'\n", node->password); |
|
|
#endif |
|
101 |
|
|
102 |
strncat(node->server, line + m[6].rm_so, |
strncat(a->server, line + m[6].rm_so, min(m[6].rm_eo - m[6].rm_so, |
103 |
min(m[6].rm_eo - m[6].rm_so, SERVER_LEN - 1)); |
SERVER_LEN - 1)); |
104 |
|
|
105 |
#ifdef DEBUG |
debug("server: '%s'\n", a->server); |
|
fprintf(stderr, "debug: SERVER: '%s'\n", node->server); |
|
|
#endif |
|
106 |
|
|
107 |
if (m[7].rm_so != -1) { |
if (m[7].rm_so != -1) { |
108 |
n = min(m[7].rm_eo - m[7].rm_so - 1, 5); |
n = min(m[7].rm_eo - m[7].rm_so - 1, 5); |
109 |
xstrncpy(p, line + m[7].rm_so + 1, n); |
xstrncpy(p, line + m[7].rm_so + 1, n); |
110 |
p[n] = '\0'; |
p[n] = '\0'; |
111 |
node->port = strtoul(p, NULL, 10); |
a->port = strtoul(p, NULL, 10); |
112 |
#ifdef DEBUG |
debug("port: %d\n", a->port); |
|
fprintf(stderr, "debug: PORT: %d\n", node->port); |
|
|
#endif |
|
113 |
} |
} |
114 |
if (m[8].rm_so != -1) { |
if (m[8].rm_so != -1) { |
115 |
if (m[7].rm_so == -1) |
if (m[7].rm_so == -1) |
116 |
node->port = 993; |
a->port = 993; |
117 |
|
|
118 |
if (strcasestr(line + m[8].rm_so, "SSL2")) |
if (strcasestr(line + m[8].rm_so, "SSL2")) |
119 |
node->ssl = SSL_SSL_V2; |
a->ssl = SSL_SSL_V2; |
120 |
else if (strcasestr(line + m[8].rm_so, "SSL3")) |
else if (strcasestr(line + m[8].rm_so, "SSL3")) |
121 |
node->ssl = SSL_SSL_V3; |
a->ssl = SSL_SSL_V3; |
122 |
else |
else |
123 |
node->ssl = SSL_TLS_V1; |
a->ssl = SSL_TLS_V1; |
124 |
} |
} |
125 |
APPEND_LINKED_LIST(accounts, node, account); |
APPEND_LINKED_LIST(accounts, a, account); |
126 |
cur_acct = node; |
curacct = a; |
127 |
|
|
128 |
return 0; |
return 0; |
129 |
} |
} |
139 |
account_t *a; |
account_t *a; |
140 |
|
|
141 |
for (a = accounts; a != NULL; a = a->next) |
for (a = accounts; a != NULL; a = a->next) |
142 |
if (a->passwdattr == PASSWORD_NONE && |
if (a->pass_attr == PASS_ATTR_NONE && |
143 |
!strcmp(a->server, serv) && !strcmp(a->username, user)) { |
!strcmp(a->server, serv) && !strcmp(a->user, user)) { |
144 |
a->passwdattr = PASSWORD_ENCRYPTED; |
a->pass_attr = PASS_ATTR_CRYPT; |
145 |
return a->password; |
return a->pass; |
146 |
} |
} |
147 |
return NULL; |
return NULL; |
148 |
} |
} |
153 |
* Set new mailbox-group's variables to safe values. |
* Set new mailbox-group's variables to safe values. |
154 |
*/ |
*/ |
155 |
void |
void |
156 |
init_mboxgrp(mboxgrp_t * node) |
init_mboxgrp(mboxgrp_t * g) |
157 |
{ |
{ |
158 |
node->left = node->right = NULL; |
|
159 |
node->key[0] = '\0'; |
g->left = g->right = NULL; |
160 |
node->mboxes[0] = NULL; |
g->key[0] = '\0'; |
161 |
|
g->mboxes[0] = NULL; |
162 |
} |
} |
163 |
|
|
164 |
|
|
169 |
int |
int |
170 |
set_mboxgrp(char *line, regmatch_t * m) |
set_mboxgrp(char *line, regmatch_t * m) |
171 |
{ |
{ |
172 |
mboxgrp_t *node; |
mboxgrp_t *g; |
173 |
char mboxs[LINE_MAX]; |
char mboxs[LINE_MAX]; |
174 |
|
|
175 |
mboxs[0] = '\0'; |
mboxs[0] = '\0'; |
176 |
|
|
177 |
if (accounts == NULL) |
if (accounts == NULL) |
178 |
return ERROR_CONFIG_PARSE; |
return ERROR_PARSER; |
179 |
|
|
180 |
node = (mboxgrp_t *) xmalloc(sizeof(mboxgrp_t)); |
g = (mboxgrp_t *) xmalloc(sizeof(mboxgrp_t)); |
181 |
|
|
182 |
init_mboxgrp(node); |
init_mboxgrp(g); |
183 |
|
|
184 |
strncat(node->key, line + m[1].rm_so, |
strncat(g->key, line + m[1].rm_so, min(m[1].rm_eo - m[1].rm_so, |
185 |
min(m[1].rm_eo - m[1].rm_so, KEY_LEN - 1)); |
KEY_LEN - 1)); |
186 |
|
|
187 |
#ifdef DEBUG |
debug("folder: '%s'\n", g->key); |
|
fprintf(stderr, "debug: FOLDER: '%s'\n", node->key); |
|
|
#endif |
|
188 |
|
|
189 |
strncat(mboxs, line + m[2].rm_so, |
strncat(mboxs, line + m[2].rm_so, min(m[2].rm_eo - m[2].rm_so, |
190 |
min(m[2].rm_eo - m[2].rm_so, LINE_MAX - 1)); |
LINE_MAX - 1)); |
191 |
|
|
192 |
process_mboxgrp(node, mboxs); |
process_mboxgrp(g, mboxs); |
193 |
|
|
194 |
INSERT_TREE(mboxgrps, node, mboxgrp); |
INSERT_TREE(mboxgrps, g, mboxgrp); |
195 |
|
|
196 |
return 0; |
return 0; |
197 |
} |
} |
198 |
|
|
199 |
|
|
200 |
/* |
/* |
201 |
* Calls set_mbox() in order to create mailboxes that are part of |
* Calls set_mbox() in order to create mailboxes that are part of the |
202 |
* the mailbox-group. |
* mailbox-group. |
203 |
*/ |
*/ |
204 |
void |
void |
205 |
process_mboxgrp(mboxgrp_t * node, char *mboxs) |
process_mboxgrp(mboxgrp_t * g, char *mboxs) |
206 |
{ |
{ |
207 |
unsigned int i; |
unsigned int i; |
208 |
char *tok; |
char *tok; |
211 |
|
|
212 |
tok = strtok_r(mboxs, ",", &mboxs); |
tok = strtok_r(mboxs, ",", &mboxs); |
213 |
while (i < MBOXGRP_MBOXES_MAX - 1 && tok != NULL) { |
while (i < MBOXGRP_MBOXES_MAX - 1 && tok != NULL) { |
214 |
node->mboxes[i] = (mbox_t *) set_mbox(tok); |
g->mboxes[i] = (mbox_t *) set_mbox(tok); |
215 |
node->mboxes[++i] = NULL; |
g->mboxes[++i] = NULL; |
216 |
|
|
217 |
tok = strtok_r(NULL, ",", &mboxs); |
tok = strtok_r(NULL, ",", &mboxs); |
218 |
} |
} |
223 |
* Set new mailbox's variables to safe values. |
* Set new mailbox's variables to safe values. |
224 |
*/ |
*/ |
225 |
void |
void |
226 |
init_mbox(mbox_t * node) |
init_mbox(mbox_t * m) |
227 |
{ |
{ |
228 |
node->next = NULL; |
|
229 |
node->name[0] = '\0'; |
m->next = NULL; |
230 |
node->filters[0] = NULL; |
m->name[0] = '\0'; |
231 |
|
m->filters[0] = NULL; |
232 |
} |
} |
233 |
|
|
234 |
|
|
238 |
mbox_t * |
mbox_t * |
239 |
set_mbox(char *name) |
set_mbox(char *name) |
240 |
{ |
{ |
241 |
char s[MBOX_NAME_LEN]; |
char s[MBOX_LEN]; |
242 |
mbox_t *node, *m; |
mbox_t *m, *am; |
243 |
|
|
244 |
*s = '\0'; |
*s = '\0'; |
245 |
|
|
246 |
if (*name == '"' && *(name + strlen(name) - 1) == '"') |
if (*name == '"' && *(name + strlen(name) - 1) == '"') |
247 |
strncat(s, name + 1, min(strlen(name) - 2, MBOX_NAME_LEN - 1)); |
strncat(s, name + 1, min(strlen(name) - 2, MBOX_LEN - 1)); |
248 |
else |
else |
249 |
strncat(s, name, min(strlen(name), MBOX_NAME_LEN - 1)); |
strncat(s, name, min(strlen(name), MBOX_LEN - 1)); |
250 |
|
|
251 |
for (m = cur_acct->mboxes; m != NULL; m = m->next) |
for (am = curacct->mboxes; am != NULL; am = am->next) |
252 |
if (!strcmp(m->name, s)) |
if (!strcmp(am->name, s)) |
253 |
return m; |
return am; |
254 |
|
|
255 |
node = (mbox_t *) xmalloc(sizeof(mbox_t)); |
m = (mbox_t *) xmalloc(sizeof(mbox_t)); |
256 |
|
|
257 |
init_mbox(node); |
init_mbox(m); |
258 |
|
|
259 |
strncat(node->name, s, MBOX_NAME_LEN - 1); |
strncat(m->name, s, MBOX_LEN - 1); |
260 |
|
|
261 |
#ifdef DEBUG |
debug("mbox: '%s'\n", m->name); |
|
fprintf(stderr, "debug: MBOX: '%s'\n", node->name); |
|
|
#endif |
|
262 |
|
|
263 |
APPEND_LINKED_LIST(cur_acct->mboxes, node, mbox); |
APPEND_LINKED_LIST(curacct->mboxes, m, mbox); |
264 |
|
|
265 |
return node; |
return m; |
266 |
} |
} |
267 |
|
|
268 |
|
|
283 |
if (*c == '%') { |
if (*c == '%') { |
284 |
if (!isxdigit((unsigned char)(*(c + 1))) || |
if (!isxdigit((unsigned char)(*(c + 1))) || |
285 |
!isxdigit((unsigned char)(*(c + 2)))) |
!isxdigit((unsigned char)(*(c + 2)))) |
286 |
return ERROR_CONFIG_PARSE; |
return ERROR_PARSER; |
287 |
|
|
288 |
xstrncpy(hex, ++c, 2); |
xstrncpy(hex, ++c, 2); |
289 |
hex[2] = '\0'; |
hex[2] = '\0'; |
290 |
|
|
291 |
if (!isprint((unsigned char)(*str = (char)strtoul(hex, |
if (!isprint((unsigned char)(*str = (char)strtoul(hex, |
292 |
NULL, 16)))) |
NULL, 16)))) |
293 |
return ERROR_CONFIG_PARSE; |
return ERROR_PARSER; |
294 |
|
|
295 |
str++; |
str++; |
296 |
c += 2; |
c += 2; |
310 |
char * |
char * |
311 |
apply_namespace(char *mbox, char *prefix, char delim) |
apply_namespace(char *mbox, char *prefix, char delim) |
312 |
{ |
{ |
313 |
static char m[MBOX_NAME_LEN]; |
static char m[MBOX_LEN]; |
314 |
char *c; |
char *c; |
315 |
|
|
316 |
if ((prefix[0] == '\0' && delim == '\0') || |
if ((prefix[0] == '\0' && delim == '\0') || |
319 |
return mbox; |
return mbox; |
320 |
|
|
321 |
m[0] = '\0'; |
m[0] = '\0'; |
322 |
strncat(m, prefix, MBOX_NAME_LEN - 1); |
strncat(m, prefix, NAMESPACE_PREFIX_LEN - 1); |
323 |
strncat(m, mbox, MBOX_NAME_LEN - strlen(m) - 1); |
strncat(m, mbox, MBOX_LEN - strlen(m) - 1); |
324 |
|
|
325 |
c = m; |
c = m; |
326 |
while ((c = strchr(c, '/'))) |
while ((c = strchr(c, '/'))) |
327 |
*(c++) = delim; |
*(c++) = delim; |
328 |
|
|
329 |
#ifdef DEBUG |
debug("mailbox: '%s'\n", m); |
|
fprintf(stderr, "debug: MAILBOX: '%s'\n", m); |
|
|
#endif |
|
330 |
|
|
331 |
return m; |
return m; |
332 |
} |
} |