/[imapfilter]/imapfilter/account.c
ViewVC logotype

Contents of /imapfilter/account.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Sun Aug 3 16:01:53 2003 UTC (20 years, 7 months ago) by lefcha
Branch: MAIN
CVS Tags: release-0_9
Branch point for: release-0_9-patches
Changes since 1.2: +1 -0 lines
File MIME type: text/plain
Added missing stdlib.h header.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26