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