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

Diff of /imapfilter/file.c

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

revision 1.27 by lefcha, Sun Nov 18 13:07:14 2001 UTC revision 1.28 by lefcha, Sat Dec 8 14:39:10 2001 UTC
# Line 1  Line 1 
1  #include <stdio.h>  #include <stdio.h>
2    #include <unistd.h>
3  #include <errno.h>  #include <errno.h>
4  #include <sys/types.h>  #include <sys/types.h>
5  #include <regex.h>  #include <regex.h>
# Line 6  Line 7 
7  #include <stdlib.h>  #include <stdlib.h>
8  #include <limits.h>  #include <limits.h>
9  #include <sys/stat.h>  #include <sys/stat.h>
10    #include <fcntl.h>
11    
12  #include "config.h"  #include "config.h"
13  #include "imapfilter.h"  #include "imapfilter.h"
# Line 14  Line 16 
16    
17  extern char logfile[PATH_MAX];  extern char logfile[PATH_MAX];
18  extern unsigned int options;  extern unsigned int options;
19    extern long timeout;
20    
21  long timeout = 0;               /* Server non-response timeout in seconds. */  
22    /*
23     * Create $HOME/.imapfilter directory.
24     */
25    int create_homedir(void)
26    {
27        char *hdname = ".imapfilter";
28        char *home = NULL;
29    
30        home = getenv("HOME");
31    
32        if (home)
33            if (chdir(home))
34                error("imapfilter: could not change directory; %s\n",
35                      strerror(errno));
36        
37        create_dir(hdname, S_IRUSR | S_IWUSR | S_IXUSR);
38    
39        return 0;
40    }
41    
42    
43    /*
44     * Create a file.
45     */
46    int create_file(char *fname, mode_t mode)
47    {
48        int fd;
49        struct stat fs;
50    
51        if (access(fname, F_OK)) {
52            fd = creat(fname, mode);
53            if (fd == -1) {
54                error("imapfilter: could not create file %s; %s\n", fname,
55                      strerror(errno));
56                return ERROR_FILE_OPEN;
57            }
58            close(fd);
59        } else {
60            stat(fname, &fs);
61            if (!S_ISREG(fs.st_mode)) {
62                error("imapfilter: file %s not a regular file\n", fname);
63                return ERROR_FILE_OPEN;
64            }
65        }
66    
67        return 0;
68    }
69    
70    
71    /*
72     * Create a directory.
73     */
74    int create_dir(char *dname, mode_t mode)
75    {
76        struct stat ds;
77        
78        if (access(dname, F_OK)) {
79            if (mkdir(dname, mode))
80                error("imapfilter: could not create directory %s; %s\n", dname,
81                      strerror(errno));
82        } else {
83            stat(dname, &ds);
84            if (!S_ISDIR(ds.st_mode))
85                error("imapfilter: file %s not a directory\n", dname);
86        }
87        
88        return 0;
89    }
90    
91    
92    #ifdef CHECK_PERMISSIONS
93    /*
94     * Check the permissions of a file.
95     */
96    int check_file_perms(char *fname, mode_t mode)
97    {
98        struct stat fs;
99    
100        if (stat(fname, &fs)) {
101            error("imapfilter: getting file %s status; %s\n", fname,
102                  strerror(errno));
103            return ERROR_TRIVIAL;
104        }
105        if (!S_ISREG(fs.st_mode)) {
106            error("imapfilter: file %s not a regular file\n", fname);
107            return ERROR_TRIVIAL;
108        }
109        if ((fs.st_mode & 00777) != mode) {
110            error("imapfilter: warning: improper file %s permissions\n"
111                  "imapfilter: warning: file's mode should be %o not %o\n",
112                  fname, mode, fs.st_mode & 00777);
113            return ERROR_TRIVIAL;
114        }
115        
116        return 0;
117    }
118    
119    
120    /*
121     * Check the permissions of a directory.
122     */
123    int check_dir_perms(char *dname, mode_t mode)
124    {
125        struct stat ds;
126        
127        if(stat(dname, &ds)) {
128            error("imapfilter: getting file %s status; %s\n", dname,
129                  strerror(errno));
130            return ERROR_TRIVIAL;
131        }
132        if (!S_ISDIR(ds.st_mode)) {
133            error("imapfilter: file %s not a directory\n", dname);
134            return ERROR_TRIVIAL;
135        }
136        if ((ds.st_mode & 00777) != mode) {
137            error("imapfilter: warning: improper dir %s permissions\n"
138                  "imapfilter: warning: file's mode should be %o not %o\n",
139                  dname, mode, ds.st_mode & 00777);
140            return ERROR_TRIVIAL;
141        }
142        
143        return 0;
144    }
145    #endif
146    
147    
148  /*  /*
# Line 39  int read_config(char *cfg) Line 166  int read_config(char *cfg)
166      printf("debug: configuration file: '%s'\n", cfg);      printf("debug: configuration file: '%s'\n", cfg);
167  #endif  #endif
168  #ifdef CHECK_PERMISSIONS  #ifdef CHECK_PERMISSIONS
169      check_permissions(cfg);      check_file_perms(cfg, S_IRUSR | S_IWUSR);
170  #endif  #endif
171      fp = fopen(cfg, "r");      fp = fopen(cfg, "r");
172    
# Line 64  int read_config(char *cfg) Line 191  int read_config(char *cfg)
191  }  }
192    
193    
 #ifdef CHECK_PERMISSIONS  
 /*  
  * Check the permissions of the configuration file.  
  */  
 int check_permissions(char *cfg)  
 {  
     struct stat fs;  
   
     if (stat(cfg, &fs)) {  
         error("imapfilter: getting file %s status; %s\n", cfg,  
               strerror(errno));  
         return ERROR_TRIVIAL;  
     }  
     if (!S_ISREG(fs.st_mode)) {  
         error("imapfilter: file %s not a regular file\n", cfg);  
         return ERROR_TRIVIAL;  
     }  
     if ((fs.st_mode & 00777) != (S_IRUSR | S_IWUSR)) {  
         error("imapfilter: warning: improper config file %s permissions\n"  
               "imapfilter: warning: file's mode should be 600 not %o\n",  
               cfg, fs.st_mode & 00777);  
         return ERROR_TRIVIAL;  
     }  
     return 0;  
 }  
 #endif  
   
194  /*  /*
195   * Parse configuration file.   * Parse configuration file.
196   */   */
# Line 100  int parse_config(FILE * fp) Line 200  int parse_config(FILE * fp)
200      unsigned int row = 0;      unsigned int row = 0;
201      char line[LINE_MAX];      char line[LINE_MAX];
202      regex_t creg[13];      regex_t creg[13];
203      regmatch_t match[7];      regmatch_t match[8];
204      const char *reg[13] = {      const char *reg[13] = {
205          "^([[:blank:]]*\n|#.*\n)$",          "^([[:blank:]]*\n|#.*\n)$",
206  #ifndef SSL_TLS  #ifndef SSL_TLS
207          "^[[:blank:]]*ACCOUNT[[:blank:]]+([[:graph:]]*):([[:graph:]]*)@([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*\n$",          "^[[:blank:]]*ACCOUNT[[:blank:]]+(([[:graph:]]+):([[:graph:]]*)|([[:graph:]]+))@([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*\n$",
208  #else  #else
209          "^[[:blank:]]*ACCOUNT[[:blank:]]+([[:graph:]]*):([[:graph:]]*)@([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*([[:blank:]]SSL|[[:blank:]]SSL2|[[:blank:]]SSL3|[[:blank:]]TLS1)?[[:blank:]]*\n$",          "^[[:blank:]]*ACCOUNT[[:blank:]]+(([[:graph:]]+):([[:graph:]]*)|([[:graph:]]+))@([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*([[:blank:]]SSL|[[:blank:]]SSL2|[[:blank:]]SSL3|[[:blank:]]TLS1)?[[:blank:]]*\n$",
210  #endif  #endif
211          "^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+([[:graph:]]+)[[:blank:]]*\n$",          "^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+([[:graph:]]+)[[:blank:]]*\n$",
212          "^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$",          "^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$",
# Line 117  int parse_config(FILE * fp) Line 217  int parse_config(FILE * fp)
217          "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(LARGER|SMALLER|OLDER|NEWER)[[:blank:]]+([[:digit:]]+)[[:blank:]]*\n$",          "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(LARGER|SMALLER|OLDER|NEWER)[[:blank:]]+([[:digit:]]+)[[:blank:]]*\n$",
218          "^[[:blank:]]*JOB[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]*\n$",          "^[[:blank:]]*JOB[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]*\n$",
219          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE)[[:blank:]]*=[[:blank:]]*([[:print:]]+)\n$",          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(LOGFILE)[[:blank:]]*=[[:blank:]]*([[:print:]]+)\n$",
220          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(HEADERS)[[:blank:]]*=[[:blank:]]*(YES|NO)[[:blank:]]*\n$",          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(HEADERS|NAMESPACE)[[:blank:]]*=[[:blank:]]*(YES|NO)[[:blank:]]*\n$",
221          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(TIMEOUT)[[:blank:]]*=[[:blank:]]*(-?[[:digit:]]+)\n$"          "^[[:blank:]]*(SET[[:blank:]])?[[:blank:]]*(TIMEOUT)[[:blank:]]*=[[:blank:]]*(-?[[:digit:]]+)\n$"
222      };      };
223    
# Line 129  int parse_config(FILE * fp) Line 229  int parse_config(FILE * fp)
229          if (!regexec(&creg[0], line, 0, match, 0))          if (!regexec(&creg[0], line, 0, match, 0))
230              continue;              continue;
231  #ifndef SSL_TLS  #ifndef SSL_TLS
232          else if (!regexec(&creg[1], line, 5, match, 0))          else if (!regexec(&creg[1], line, 7, match, 0))
233  #else  #else
234          else if (!regexec(&creg[1], line, 6, match, 0))          else if (!regexec(&creg[1], line, 8, match, 0))
235  #endif  #endif
236              set_account(line, match);              set_account(line, match);
237          else if (!regexec(&creg[2], line, 3, match, 0))          else if (!regexec(&creg[2], line, 3, match, 0))
# Line 181  void set_options(char *line, regmatch_t Line 281  void set_options(char *line, regmatch_t
281              options |= OPTION_HEADERS;              options |= OPTION_HEADERS;
282          else          else
283              options &= ~(OPTION_HEADERS);              options &= ~(OPTION_HEADERS);
284        } else if (!strncasecmp(line + match[2].rm_so, "namespace", 9)) {
285            if (!strncasecmp(line + match[3].rm_so, "yes", 3))
286                options |= OPTION_NAMESPACE;
287            else
288                options &= ~(OPTION_NAMESPACE);
289      } else if (!strncasecmp(line + match[2].rm_so, "timeout", 7)) {      } else if (!strncasecmp(line + match[2].rm_so, "timeout", 7)) {
290          errno = 0;          errno = 0;
291          timeout = strtol(line + match[3].rm_so, NULL, 10);          timeout = strtol(line + match[3].rm_so, NULL, 10);

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.28

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26