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

Diff of /imapfilter/account.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by lefcha, Fri Aug 8 00:18:45 2003 UTC revision 1.9 by lefcha, Fri Feb 13 12:17:15 2004 UTC
# Line 3  Line 3 
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>          /* IEEE Std 1003.1-2001 transitional. */
7  #include <regex.h>  #include <regex.h>
8    
9  #include "config.h"  #include "config.h"
# Line 11  Line 12 
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  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);
# Line 34  int string_decode(char *str); Line 36  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;          a->next = NULL;
42          node->key[0] = node->server[0] = '\0';          a->key[0] = '\0';
43          node->username[0] = node->password[0] = '\0';          a->server[0] = '\0';
44          node->passwdattr = PASSWORD_NONE;          a->user[0] = '\0';
45          node->port = 143;          a->pass[0] = '\0';
46          node->ssl = SSL_DISABLED;          a->pass_attr = PASS_ATTR_NONE;
47          node->mboxes = NULL;          a->port = 143;
48            a->ssl = SSL_DISABLED;
49            a->mboxes = NULL;
50  }  }
51    
52    
# Line 55  set_account(char *line, regmatch_t * m) Line 59  set_account(char *line, regmatch_t * m)
59  {  {
60          int n;          int n;
61          char p[6];          char p[6];
62          account_t *node;          account_t *a;
63    
64          node = (account_t *) xmalloc(sizeof(account_t));          a = (account_t *) xmalloc(sizeof(account_t));
65          node->password = (char *)smalloc(PASSWORD_LEN);          a->pass = (char *)smalloc(PASS_LEN);
66    
67          init_account(node);          init_account(a);
68    
69          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,
70              min(m[1].rm_eo - m[1].rm_so, KEY_LEN - 1));                  KEY_LEN - 1));
71    
72  #ifdef DEBUG          debug("account: '%s'\n", a->key);
         fprintf(stderr, "debug: ACCOUNT: '%s'\n", node->key);  
 #endif  
73    
74          if (m[3].rm_so != -1) {          if (m[3].rm_so != -1)
75                  strncat(node->username, line + m[3].rm_so,                  strncat(a->user, line + m[3].rm_so,
76                      min(m[3].rm_eo - m[3].rm_so, USERNAME_LEN - 1));                      min(m[3].rm_eo - m[3].rm_so, USER_LEN - 1));
77          } else {          else
78                  strncat(node->username, line + m[5].rm_so,                  strncat(a->user, line + m[5].rm_so,
79                      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;  
80    
81  #ifdef DEBUG          if (strchr(a->user, '%'))
82          fprintf(stderr, "debug: USERNAME: '%s'\n", node->username);                  if (string_decode(a->user))
83  #endif                          return ERROR_PARSER;
84    
85            debug("username: '%s'\n", a->user);
86    
87          if (m[4].rm_so != -1) {          if (m[4].rm_so != -1) {
88                  strncat(node->password, line + m[4].rm_so,                  strncat(a->pass, line + m[4].rm_so,
89                      min(m[4].rm_eo - m[4].rm_so, PASSWORD_LEN - 1));                      min(m[4].rm_eo - m[4].rm_so, PASS_LEN - 1));
90                  if (strchr(node->password, '%'))                  if (strchr(a->pass, '%'))
91                          if (string_decode(node->password))                          if (string_decode(a->pass))
92                                  return ERROR_CONFIG_PARSE;                                  return ERROR_PARSER;
93                  node->passwdattr = PASSWORD_PLAIN;                  a->pass_attr = PASS_ATTR_PLAIN;
94                    if (opts.debug == 1)
95                            debug("password: *\n");
96                    else
97                            debug("password: '%s'\n", a->pass);
98          } else          } else
99                  flags |= FLAG_BLANK_PASSWORD;                  flags |= FLAG_BLANKPASS;
100    
101  #ifdef DEBUG          strncat(a->server, line + m[6].rm_so,
         fprintf(stderr, "debug: PASSWORD: '%s'\n", node->password);  
 #endif  
   
         strncat(node->server, line + m[6].rm_so,  
102              min(m[6].rm_eo - m[6].rm_so, SERVER_LEN - 1));              min(m[6].rm_eo - m[6].rm_so, SERVER_LEN - 1));
103    
104  #ifdef DEBUG          debug("server: '%s'\n", a->server);
         fprintf(stderr, "debug: SERVER: '%s'\n", node->server);  
 #endif  
105    
106          if (m[7].rm_so != -1) {          if (m[7].rm_so != -1) {
107                  n = min(m[7].rm_eo - m[7].rm_so - 1, 5);                  n = min(m[7].rm_eo - m[7].rm_so - 1, 5);
108                  xstrncpy(p, line + m[7].rm_so + 1, n);                  xstrncpy(p, line + m[7].rm_so + 1, n);
109                  p[n] = '\0';                  p[n] = '\0';
110                  node->port = strtoul(p, NULL, 10);                  a->port = strtoul(p, NULL, 10);
111  #ifdef DEBUG                  debug("port: %d\n", a->port);
                 fprintf(stderr, "debug: PORT: %d\n", node->port);  
 #endif  
112          }          }
113          if (m[8].rm_so != -1) {          if (m[8].rm_so != -1) {
114                  if (m[7].rm_so == -1)                  if (m[7].rm_so == -1)
115                          node->port = 993;                          a->port = 993;
116    
117                  if (strcasestr(line + m[8].rm_so, "SSL2"))                  if (strcasestr(line + m[8].rm_so, "SSL2"))
118                          node->ssl = SSL_SSL_V2;                          a->ssl = SSL_SSL_V2;
119                  else if (strcasestr(line + m[8].rm_so, "SSL3"))                  else if (strcasestr(line + m[8].rm_so, "SSL3"))
120                          node->ssl = SSL_SSL_V3;                          a->ssl = SSL_SSL_V3;
121                  else                  else
122                          node->ssl = SSL_TLS_V1;                          a->ssl = SSL_TLS_V1;
123          }          }
124          APPEND_LINKED_LIST(accounts, node, account);          APPEND_LINKED_LIST(accounts, a, account);
125          cur_acct = node;          curacct = a;
126    
127          return 0;          return 0;
128  }  }
# Line 142  find_password(char *user, char *serv) Line 138  find_password(char *user, char *serv)
138          account_t *a;          account_t *a;
139    
140          for (a = accounts; a != NULL; a = a->next)          for (a = accounts; a != NULL; a = a->next)
141                  if (a->passwdattr == PASSWORD_NONE &&                  if (a->pass_attr == PASS_ATTR_NONE &&
142                      !strcmp(a->server, serv) && !strcmp(a->username, user)) {                      !strcmp(a->server, serv) &&
143                          a->passwdattr = PASSWORD_ENCRYPTED;                      !strcmp(a->user, user)) {
144                          return a->password;                          a->pass_attr = PASS_ATTR_CRYPT;
145                            return a->pass;
146                  }                  }
147          return NULL;          return NULL;
148  }  }
# Line 156  find_password(char *user, char *serv) Line 153  find_password(char *user, char *serv)
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;          g->left = g->right = NULL;
159          node->key[0] = '\0';          g->key[0] = '\0';
160          node->mboxes[0] = NULL;          g->mboxes[0] = NULL;
161  }  }
162    
163    
# Line 171  init_mboxgrp(mboxgrp_t * node) Line 168  init_mboxgrp(mboxgrp_t * node)
168  int  int
169  set_mboxgrp(char *line, regmatch_t * m)  set_mboxgrp(char *line, regmatch_t * m)
170  {  {
171          mboxgrp_t *node;          mboxgrp_t *g;
172          char mboxs[LINE_MAX];          char mboxs[LINE_MAX];
173    
174          mboxs[0] = '\0';          mboxs[0] = '\0';
175    
176          if (accounts == NULL)          if (accounts == NULL)
177                  return ERROR_CONFIG_PARSE;                  return ERROR_PARSER;
178    
179          node = (mboxgrp_t *) xmalloc(sizeof(mboxgrp_t));          g = (mboxgrp_t *) xmalloc(sizeof(mboxgrp_t));
180    
181          init_mboxgrp(node);          init_mboxgrp(g);
182    
183          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,
184              min(m[1].rm_eo - m[1].rm_so, KEY_LEN - 1));                  KEY_LEN - 1));
185    
186  #ifdef DEBUG          debug("folder: '%s'\n", g->key);
         fprintf(stderr, "debug: FOLDER: '%s'\n", node->key);  
 #endif  
187    
188          strncat(mboxs, line + m[2].rm_so,          strncat(mboxs, line + m[2].rm_so, min(m[2].rm_eo - m[2].rm_so,
189              min(m[2].rm_eo - m[2].rm_so, LINE_MAX - 1));                  LINE_MAX - 1));
190    
191          process_mboxgrp(node, mboxs);          process_mboxgrp(g, mboxs);
192    
193          INSERT_TREE(mboxgrps, node, mboxgrp);          INSERT_TREE(mboxgrps, g, mboxgrp);
194    
195          return 0;          return 0;
196  }  }
# Line 206  set_mboxgrp(char *line, regmatch_t * m) Line 201  set_mboxgrp(char *line, regmatch_t * m)
201   * the mailbox-group.   * the mailbox-group.
202   */   */
203  void  void
204  process_mboxgrp(mboxgrp_t * node, char *mboxs)  process_mboxgrp(mboxgrp_t * g, char *mboxs)
205  {  {
206          unsigned int i;          unsigned int i;
207          char *tok;          char *tok;
# Line 215  process_mboxgrp(mboxgrp_t * node, char * Line 210  process_mboxgrp(mboxgrp_t * node, char *
210    
211          tok = strtok_r(mboxs, ",", &mboxs);          tok = strtok_r(mboxs, ",", &mboxs);
212          while (i < MBOXGRP_MBOXES_MAX - 1 && tok != NULL) {          while (i < MBOXGRP_MBOXES_MAX - 1 && tok != NULL) {
213                  node->mboxes[i] = (mbox_t *) set_mbox(tok);                  g->mboxes[i] = (mbox_t *) set_mbox(tok);
214                  node->mboxes[++i] = NULL;                  g->mboxes[++i] = NULL;
215    
216                  tok = strtok_r(NULL, ",", &mboxs);                  tok = strtok_r(NULL, ",", &mboxs);
217          }          }
# Line 227  process_mboxgrp(mboxgrp_t * node, char * Line 222  process_mboxgrp(mboxgrp_t * node, char *
222   * Set new mailbox's variables to safe values.   * Set new mailbox's variables to safe values.
223   */   */
224  void  void
225  init_mbox(mbox_t * node)  init_mbox(mbox_t * m)
226  {  {
227          node->next = NULL;          m->next = NULL;
228          node->name[0] = '\0';          m->name[0] = '\0';
229          node->filters[0] = NULL;          m->filters[0] = NULL;
230  }  }
231    
232    
# Line 241  init_mbox(mbox_t * node) Line 236  init_mbox(mbox_t * node)
236  mbox_t *  mbox_t *
237  set_mbox(char *name)  set_mbox(char *name)
238  {  {
239          char s[MBOX_NAME_LEN];          char s[MBOX_LEN];
240          mbox_t *node, *m;          mbox_t *m, *am;
241    
242          *s = '\0';          *s = '\0';
243    
244          if (*name == '"' && *(name + strlen(name) - 1) == '"')          if (*name == '"' && *(name + strlen(name) - 1) == '"')
245                  strncat(s, name + 1, min(strlen(name) - 2, MBOX_NAME_LEN - 1));                  strncat(s, name + 1, min(strlen(name) - 2, MBOX_LEN - 1));
246          else          else
247                  strncat(s, name, min(strlen(name), MBOX_NAME_LEN - 1));                  strncat(s, name, min(strlen(name), MBOX_LEN - 1));
248    
249          for (m = cur_acct->mboxes; m != NULL; m = m->next)          for (am = curacct->mboxes; am != NULL; am = am->next)
250                  if (!strcmp(m->name, s))                  if (!strcmp(am->name, s))
251                          return m;                          return am;
252    
253          node = (mbox_t *) xmalloc(sizeof(mbox_t));          m = (mbox_t *) xmalloc(sizeof(mbox_t));
254    
255          init_mbox(node);          init_mbox(m);
256    
257          strncat(node->name, s, MBOX_NAME_LEN - 1);          strncat(m->name, s, MBOX_LEN - 1);
258    
259  #ifdef DEBUG          debug("mbox: '%s'\n", m->name);
         fprintf(stderr, "debug: MBOX: '%s'\n", node->name);  
 #endif  
260    
261          APPEND_LINKED_LIST(cur_acct->mboxes, node, mbox);          APPEND_LINKED_LIST(curacct->mboxes, m, mbox);
262    
263          return node;          return m;
264  }  }
265    
266    
# Line 288  string_decode(char *str) Line 281  string_decode(char *str)
281                  if (*c == '%') {                  if (*c == '%') {
282                          if (!isxdigit((unsigned char)(*(c + 1))) ||                          if (!isxdigit((unsigned char)(*(c + 1))) ||
283                              !isxdigit((unsigned char)(*(c + 2))))                              !isxdigit((unsigned char)(*(c + 2))))
284                                  return ERROR_CONFIG_PARSE;                                  return ERROR_PARSER;
285    
286                          xstrncpy(hex, ++c, 2);                          xstrncpy(hex, ++c, 2);
287                          hex[2] = '\0';                          hex[2] = '\0';
288    
289                          if (!isprint((unsigned char)(*str = (char)strtoul(hex,                          if (!isprint((unsigned char)(*str = (char)strtoul(hex,
290                                          NULL, 16))))                                          NULL, 16))))
291                                  return ERROR_CONFIG_PARSE;                                  return ERROR_PARSER;
292    
293                          str++;                          str++;
294                          c += 2;                          c += 2;
# Line 315  string_decode(char *str) Line 308  string_decode(char *str)
308  char *  char *
309  apply_namespace(char *mbox, char *prefix, char delim)  apply_namespace(char *mbox, char *prefix, char delim)
310  {  {
311          static char m[MBOX_NAME_LEN];          static char m[MBOX_LEN];
312          char *c;          char *c;
313    
314          if ((prefix[0] == '\0' && delim == '\0') ||          if ((prefix[0] == '\0' && delim == '\0') ||
# Line 324  apply_namespace(char *mbox, char *prefix Line 317  apply_namespace(char *mbox, char *prefix
317                  return mbox;                  return mbox;
318    
319          m[0] = '\0';          m[0] = '\0';
320          strncat(m, prefix, MBOX_NAME_LEN - 1);          strncat(m, prefix, NAMESPACE_PREFIX_LEN - 1);
321          strncat(m, mbox, MBOX_NAME_LEN - strlen(m) - 1);          strncat(m, mbox, MBOX_LEN - strlen(m) - 1);
322    
323          c = m;          c = m;
324          while ((c = strchr(c, '/')))          while ((c = strchr(c, '/')))
325                  *(c++) = delim;                  *(c++) = delim;
326    
327  #ifdef DEBUG          debug("mailbox: '%s'\n", m);
         fprintf(stderr, "debug: MAILBOX: '%s'\n", m);  
 #endif  
328    
329          return m;          return m;
330  }  }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.9

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26