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

Contents of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (show annotations)
Sat Oct 6 12:28:40 2001 UTC (22 years, 5 months ago) by lefcha
Branch: MAIN
Changes since 1.20: +2 -0 lines
File MIME type: text/plain
Fixed a memory leak.

1 #include <stdio.h>
2 #include <errno.h>
3 #include <sys/types.h>
4 #include <regex.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <limits.h>
8 #include <sys/stat.h>
9
10 #include "config.h"
11 #include "imapfilter.h"
12 #include "data.h"
13
14
15 extern char logfile[PATH_MAX];
16 extern unsigned int options;
17
18
19 /*
20 * Find the path to configuration file, open it and call parse_config().
21 */
22 int read_config(char *cfg)
23 {
24 int r;
25 FILE *fp;
26 char *home = NULL;
27
28 if (!cfg) {
29 cfg = (char *) xmalloc(PATH_MAX * sizeof(char));
30
31 home = getenv("HOME");
32
33 snprintf(cfg, PATH_MAX, "%s/%s", home, ".imapfilterrc");
34 }
35 #ifdef DEBUG
36 printf("debug: configuration file: '%s'\n", cfg);
37 #endif
38 #ifdef CHECK_PERMISSIONS
39 check_permissions(cfg);
40 #endif
41 fp = fopen(cfg, "r");
42
43 if (!fp)
44 fatal(ERROR_FILE_OPEN, "imapfilter: opening config file %s; %s\n",
45 cfg, strerror(errno));
46
47 if ((r = parse_config(fp)))
48 fatal(ERROR_CONFIG_PARSE,
49 "imapfilter: parse error in config file at row %d\n", r);
50
51 fclose(fp);
52
53 free(cfg);
54
55 #ifdef DEBUG
56 printf("debug: options: %0#10x\n", options);
57 #endif
58
59 return 0;
60 }
61
62
63 #ifdef CHECK_PERMISSIONS
64 /*
65 * Check the permissions of the configuration file.
66 */
67 int check_permissions(char *cfg)
68 {
69 struct stat fs;
70
71 if (stat(cfg, &fs)) {
72 error("imapfilter: getting file %s status; %s\n", cfg,
73 strerror(errno));
74 return ERROR_TRIVIAL;
75 }
76 if (!S_ISREG(fs.st_mode)) {
77 error("imapfilter: file %s not a regular file\n", cfg);
78 return ERROR_TRIVIAL;
79 }
80 if ((fs.st_mode & 00777) != (S_IRUSR | S_IWUSR)) {
81 error("imapfilter: warning: improper config file %s permissions\n"
82 "imapfilter: warning: file's mode should be 600 not %o\n",
83 cfg, fs.st_mode & 00777);
84 return ERROR_TRIVIAL;
85 }
86 return 0;
87 }
88 #endif
89
90 /*
91 * Parse configuration file.
92 */
93 int parse_config(FILE * fp)
94 {
95 int i, r = 0;
96 unsigned int row = 0;
97 char line[LINE_MAX];
98 regex_t creg[12];
99 regmatch_t match[7];
100 const char *reg[12] = {
101 "^([[:blank:]]*\n|#.*\n)$",
102 "^[[:blank:]]*ACCOUNT[[:blank:]]+([[:graph:]]*):([[:graph:]]*)@([[:alnum:].-]+)(:[[:digit:]]+)?[[:blank:]]*\n$",
103 "^[[:blank:]]*FOLDER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]+([[:graph:]]+)[[:blank:]]*\n$",
104 "^[[:blank:]]*FILTER[[:blank:]]+([[:alnum:]_-]+)[[:blank:]]*([[:blank:]]OR|[[:blank:]]AND)?[[:blank:]]*\n$",
105 "^[[:blank:]]*ACTION[[:blank:]]+(DELETE|COPY[[:blank:]]+([[:graph:]]+)|MOVE[[:blank:]]+([[:graph:]]+)|LIST)[[:blank:]]*([[:graph:]]*)[[:blank:]]*\n$",
106 "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(ANSWERED|DELETED|DRAFT|FLAGGED|NEW|OLD|RECENT|SEEN|UNANSWERED|UNDELETED|UNDRAFT|UNFLAGGED|UNSEEN)[[:blank:]]*\n$",
107 "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(BCC|BODY|CC|FROM|SUBJECT|TEXT|TO)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$",
108 "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(HEADER)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]+(\"[[:print:]]*\"|[[:graph:]]+)[[:blank:]]*\n$",
109 "^[[:blank:]]*(MASK[[:blank:]])?[[:blank:]]*(OR[[:blank:]]|AND[[:blank:]])?[[:blank:]]*(NOT[[:blank:]])?[[:blank:]]*(LARGER|SMALLER)[[:blank:]]+([[:digit:]]+)[[:blank:]]*\n$",
110 "^[[:blank:]]*JOB[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]+([[:alnum:],_-]+)[[:blank:]]*\n$",
111 "^[[:blank:]]*(LOGFILE)[[:blank:]]*=[[:blank:]]*([[:print:]]+)\n$",
112 "^[[:blank:]]*(HEADERS)[[:blank:]]*=[[:blank:]]*(YES|NO)[[:blank:]]*\n$"
113 };
114
115 for (i = 0; i < 12; i++)
116 regcomp(&creg[i], reg[i], REG_EXTENDED | REG_ICASE);
117
118 while (fgets(line, LINE_MAX - 1, fp)) {
119 row++;
120 if (!regexec(&creg[0], line, 0, match, 0))
121 continue;
122 else if (!regexec(&creg[1], line, 6, match, 0))
123 set_account(line, match);
124 else if (!regexec(&creg[2], line, 3, match, 0))
125 r = set_mboxgrp(line, match);
126 else if (!regexec(&creg[3], line, 3, match, 0))
127 r = set_filter(line, match);
128 else if (!regexec(&creg[4], line, 5, match, 0))
129 r = set_action(line, match);
130 else if (!regexec(&creg[5], line, 7, match, 0) ||
131 !regexec(&creg[6], line, 7, match, 0) ||
132 !regexec(&creg[7], line, 7, match, 0) ||
133 !regexec(&creg[8], line, 7, match, 0))
134 r = set_mask(line, match);
135 else if (!regexec(&creg[9], line, 3, match, 0))
136 r = set_job(line, match);
137 else if (!regexec(&creg[10], line, 3, match, 0) ||
138 !regexec(&creg[11], line, 3, match, 0))
139 set_options(line, match);
140 else
141 return row;
142
143 if (r == ERROR_CONFIG_PARSE)
144 return row;
145 }
146
147 for (i = 0; i < 12; i++)
148 regfree(&creg[i]);
149
150 destroy_data();
151
152 return 0;
153 }
154
155
156 /*
157 * Set other options found in config file.
158 */
159 void set_options(char *line, regmatch_t * match)
160 {
161 if (!strncasecmp(line + match[1].rm_so, "logfile", 7)) {
162 if (!*logfile)
163 strncat(logfile, line + match[2].rm_so,
164 min((match[2].rm_eo - match[2].rm_so), PATH_MAX - 1));
165 } else {
166 if (!strncasecmp(line + match[2].rm_so, "yes", 3))
167 options |= OPTION_HEADERS;
168 else if (options & OPTION_HEADERS)
169 options ^= OPTION_HEADERS;
170 }
171 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26