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

Annotation of /imapfilter/account.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Mon Feb 9 17:34:56 2004 UTC (16 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.5: +8 -24 lines
File MIME type: text/plain
Move DEBUG from compilation #define variable to runtime command line option.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26