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

Annotation of /imapfilter/account.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations)
Fri Feb 13 12:17:15 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
Changes since 1.8: +100 -97 lines
File MIME type: text/plain
Stylistic changes.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26