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

Contents of /imapfilter/account.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations)
Fri Feb 13 13:18:37 2004 UTC (16 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.9: +3 -0 lines
File MIME type: text/plain
Insert an empty line if the function has no local variables.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26