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

Diff of /imapfilter/file.c

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

revision 1.37 by lefcha, Thu Jun 20 08:48:48 2002 UTC revision 1.38 by lefcha, Sat Jul 13 14:11:08 2002 UTC
# Line 23  extern long timeout; Line 23  extern long timeout;
23  extern char *home;  extern char *home;
24    
25  #ifdef ENCRYPTED_PASSWORDS  #ifdef ENCRYPTED_PASSWORDS
26  char *passphr;                  /* Master password to access the  char *passphr;                  /* Master password to access the passwords
27                                     passwords file. */                                     file. */
28  #endif  #endif
29    
30    
# Line 38  int read_config(char *cfg) Line 38  int read_config(char *cfg)
38      char *c = NULL;      char *c = NULL;
39    
40      if (!cfg) {      if (!cfg) {
41          cfg = c = (char *) xmalloc(PATH_MAX * sizeof(char));          cfg = c = (char *)xmalloc(PATH_MAX * sizeof(char));
42          snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc");          snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc");
43      }      }
   
44  #ifdef DEBUG  #ifdef DEBUG
45      printf("debug: configuration file: '%s'\n", cfg);      printf("debug: configuration file: '%s'\n", cfg);
46  #endif  #endif
# Line 130  int parse_config(FILE * fd) Line 129  int parse_config(FILE * fd)
129          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE)"          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE)"
130          "[[:blank:]]*=[[:blank:]]*([[:print:]]+)\n$",          "[[:blank:]]*=[[:blank:]]*([[:print:]]+)\n$",
131    
132          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(HEADERS|NAMESPACE)"          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(HEADERS|NAMESPACE|EXPUNGE)"
133          "[[:blank:]]*=[[:blank:]]*(YES|NO)[[:blank:]]*\n$",          "[[:blank:]]*=[[:blank:]]*(YES|NO)[[:blank:]]*\n$",
134    
135          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(DAEMON|TIMEOUT)"          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(DAEMON|TIMEOUT)"
# Line 198  void set_options(char *line, regmatch_t Line 197  void set_options(char *line, regmatch_t
197              options |= OPTION_NAMESPACE;              options |= OPTION_NAMESPACE;
198          else          else
199              options &= ~(OPTION_NAMESPACE);              options &= ~(OPTION_NAMESPACE);
200        } else if (!strncasecmp(line + match[2].rm_so, "expunge", 7)) {
201            if (!strncasecmp(line + match[3].rm_so, "yes", 3))
202                options |= OPTION_EXPUNGE;
203            else
204                options &= ~(OPTION_EXPUNGE);
205      } else if (!strncasecmp(line + match[2].rm_so, "timeout", 7)) {      } else if (!strncasecmp(line + match[2].rm_so, "timeout", 7)) {
206          errno = 0;          errno = 0;
207          timeout = strtol(line + match[3].rm_so, NULL, 10);          timeout = strtol(line + match[3].rm_so, NULL, 10);
208          if (errno)          if (errno)
209              timeout = 0;              timeout = 0;
210      } else if (!strncasecmp(line + match[2].rm_so, "daemon", 6) &&      } else if (!strncasecmp(line + match[2].rm_so, "daemon", 6) &&
211                 !(options & OPTION_DAEMON_MODE)) {                 !(options & OPTION_DAEMON_MODE)) {
212          options |= OPTION_DAEMON_MODE;          options |= OPTION_DAEMON_MODE;
213          errno = 0;          errno = 0;
# Line 222  int read_passwords(void) Line 226  int read_passwords(void)
226  {  {
227      FILE *fd;      FILE *fd;
228      char pwfile[PATH_MAX];      char pwfile[PATH_MAX];
229        
230      if (!(flags & FLAG_BLANK_PASSWORD))      if (!(flags & FLAG_BLANK_PASSWORD))
231          return ERROR_CONFIG_PARSE;          return ERROR_CONFIG_PARSE;
232        
233      passphr = (char *) smalloc(PASSPHRASE_LEN);      passphr = (char *)smalloc(PASSPHRASE_LEN);
234      *passphr = 0;      *passphr = 0;
235    
236      snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");      snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");
237  #ifdef DEBUG  #ifdef DEBUG
238      printf("debug: passwords file: '%s'\n", pwfile);      printf("debug: passwords file: '%s'\n", pwfile);
239  #endif  #endif
240        
241      if (!exists_file(pwfile))      if (!exists_file(pwfile))
242          return ERROR_FILE_OPEN;          return ERROR_FILE_OPEN;
243        
244  #ifdef CHECK_PERMISSIONS  #ifdef CHECK_PERMISSIONS
245      check_file_perms(pwfile, S_IRUSR | S_IWUSR);      check_file_perms(pwfile, S_IRUSR | S_IWUSR);
246  #endif  #endif
# Line 275  int parse_passwords(FILE * fd) Line 279  int parse_passwords(FILE * fd)
279          printf("Enter master passphrase: ");          printf("Enter master passphrase: ");
280          get_password(passphr, PASSPHRASE_LEN);          get_password(passphr, PASSPHRASE_LEN);
281      } while ((r = decrypt_passwords(&buf, fd)) && --t);      } while ((r = decrypt_passwords(&buf, fd)) && --t);
282        
283      if (!t)      if (!t)
284          return ERROR_PASSPHRASE;          return ERROR_PASSPHRASE;
285    
286      c = cp = sstrdup(buf);      c = cp = sstrdup(buf);
287        
288      regcomp(&creg, reg, REG_EXTENDED | REG_ICASE);      regcomp(&creg, reg, REG_EXTENDED | REG_ICASE);
289    
290      while ((line = strsep(&c, "\n")) &&      while ((line = strsep(&c, "\n")) &&
291             !regexec(&creg, line, 4, match, 0)) {             !regexec(&creg, line, 4, match, 0)) {
292          user[0] = serv[0] = 0;          user[0] = serv[0] = 0;
293            
294          strncat(serv, line + match[1].rm_so,          strncat(serv, line + match[1].rm_so,
295                  min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1));                  min(match[1].rm_eo - match[1].rm_so, SERVER_LEN - 1));
296          strncat(user, line + match[2].rm_so,          strncat(user, line + match[2].rm_so,
297                  min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1));                  min(match[2].rm_eo - match[2].rm_so, USERNAME_LEN - 1));
298            
299          if ((pe = (char *) find_password(user, serv)))          if ((pe = (char *)find_password(user, serv)))
300              strncat(pe, line + match[3].rm_so,              strncat(pe, line + match[3].rm_so,
301                      min(match[3].rm_eo - match[3].rm_so, PASSWORD_LEN - 1));                      min(match[3].rm_eo - match[3].rm_so, PASSWORD_LEN - 1));
302      }      }
303        
304      regfree(&creg);      regfree(&creg);
305      sfree(cp);        sfree(cp);
306      sfree(buf);      sfree(buf);
307    
308      return 0;      return 0;
# Line 314  int store_passwords(account_t * accts[]) Line 318  int store_passwords(account_t * accts[])
318      FILE *fd;      FILE *fd;
319    
320      snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");      snprintf(pwfile, PATH_MAX, "%s/%s", home, ".imapfilter/passwords");
321        
322      create_file(pwfile, S_IRUSR | S_IWUSR);      create_file(pwfile, S_IRUSR | S_IWUSR);
323    
324      fd = fopen(pwfile, "w");      fd = fopen(pwfile, "w");
# Line 322  int store_passwords(account_t * accts[]) Line 326  int store_passwords(account_t * accts[])
326      if (!fd)      if (!fd)
327          fatal(ERROR_FILE_OPEN, "imapfilter: opening passwords file %s; %s\n",          fatal(ERROR_FILE_OPEN, "imapfilter: opening passwords file %s; %s\n",
328                pwfile, strerror(errno));                pwfile, strerror(errno));
329        
330      encrypt_passwords(fd, accts);      encrypt_passwords(fd, accts);
331        
332      fclose(fd);      fclose(fd);
333    
334      return 0;      return 0;
335  }  }
336    
337  #endif  #endif
338    
339    
# Line 338  int store_passwords(account_t * accts[]) Line 343  int store_passwords(account_t * accts[])
343  int create_homedir(void)  int create_homedir(void)
344  {  {
345      char *hdn = ".imapfilter";      char *hdn = ".imapfilter";
346        
347      if (home)      if (home)
348          if (chdir(home))          if (chdir(home))
349              error("imapfilter: could not change directory; %s\n",              error("imapfilter: could not change directory; %s\n",
# Line 359  int create_homedir(void) Line 364  int create_homedir(void)
364  int exists_file(char *fname)  int exists_file(char *fname)
365  {  {
366      struct stat fs;      struct stat fs;
367        
368      if (access(fname, F_OK))      if (access(fname, F_OK))
369          return 0;          return 0;
370        
371      stat(fname, &fs);      stat(fname, &fs);
372      if (!S_ISREG(fs.st_mode)) {      if (!S_ISREG(fs.st_mode)) {
373          error("imapfilter: file %s not a regular file\n", fname);          error("imapfilter: file %s not a regular file\n", fname);
374          return ERROR_FILE_OPEN;          return ERROR_FILE_OPEN;
375      }      }
       
376      return 1;      return 1;
377  }  }
378    
379    
380  /*  /*
381   * Check if a directory exists.   * Check if a directory exists.
382   */   */
383  int exists_dir(char *dname)  int exists_dir(char *dname)
384  {  {
385      struct stat ds;      struct stat ds;
386        
387      if (access(dname, F_OK))      if (access(dname, F_OK))
388          return 0;          return 0;
389        
390      stat(dname, &ds);      stat(dname, &ds);
391      if (!S_ISDIR(ds.st_mode)) {      if (!S_ISDIR(ds.st_mode)) {
392          error("imapfilter: file %s not a directory\n", dname);          error("imapfilter: file %s not a directory\n", dname);
393          return ERROR_FILE_OPEN;          return ERROR_FILE_OPEN;
394      }      }
       
395      return 1;      return 1;
396  }  }
397    
# Line 399  int exists_dir(char *dname) Line 402  int exists_dir(char *dname)
402  int create_file(char *fname, mode_t mode)  int create_file(char *fname, mode_t mode)
403  {  {
404      int fd = 0;      int fd = 0;
405        
406      if (!exists_file(fname)) {      if (!exists_file(fname)) {
407          fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode);          fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode);
408          if (fd == -1) {          if (fd == -1) {
# Line 409  int create_file(char *fname, mode_t mode Line 412  int create_file(char *fname, mode_t mode
412          }          }
413          close(fd);          close(fd);
414      }      }
   
415      return 0;      return 0;
416  }  }
417    
# Line 421  int create_file(char *fname, mode_t mode Line 423  int create_file(char *fname, mode_t mode
423  int check_file_perms(char *fname, mode_t mode)  int check_file_perms(char *fname, mode_t mode)
424  {  {
425      struct stat fs;      struct stat fs;
426        
427      if (stat(fname, &fs)) {      if (stat(fname, &fs)) {
428          error("imapfilter: getting file %s status; %s\n", fname,          error("imapfilter: getting file %s status; %s\n", fname,
429                strerror(errno));                strerror(errno));
# Line 465  int check_dir_perms(char *dname, mode_t Line 467  int check_dir_perms(char *dname, mode_t
467      }      }
468      return 0;      return 0;
469  }  }
 #endif  
   
   
470    
471    #endif

Legend:
Removed from v.1.37  
changed lines
  Added in v.1.38

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26