1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <ctype.h>
10 
11 #include "loadconfig.h"
12 
13 
14 int wcnt;
15 int bcnt;
16 int pcnt;
17 int enable_subj_filt;
18 
19 char logtype[VALSIZE];
20 char logfile[VALSIZE];
21 char ignorefile[VALSIZE];
22 char highfile[VALSIZE];
23 char badmailfile[VALSIZE];
24 char hostname[VALSIZE];
25 char sysadmin[VALSIZE];
26 char statfile[VALSIZE];
27 char mail_command[VALSIZE];
28 char makemap_command[VALSIZE];
29 char qsheff_rules_file[VALSIZE];
30 char trim_subj_str[VALSIZE];
31 
32 extern int errno;
33 extern int w;
34 extern int b;
35 extern int p;
36 
37 void
loadconfig(const char * cfgfile)38 loadconfig(const char *cfgfile)
39 {
40 	FILE *fd = NULL;
41 	char buf[BUFSIZE];
42 	char keyword[KEYSIZE];
43 	char value[VALSIZE];
44 	char *cp1, *cp2;
45 	char *variables[] = {
46 		"Invalid",
47 		"logtype",
48 		"logfile",
49 		"ignorefile",
50 		"badmailfile",
51 		"hostname",
52 		"sysadmin",
53 		"statfile",
54 		"mail_command",
55 		"makemap_command",
56 		"wcnt",
57 		"bcnt",
58 		"pcnt",
59 		"highfile",
60 		"enable_subj_filt",
61 		"qsheff_rules_file",
62 		"trim_subj_str"
63 	};
64 
65 	int i = 0, j = 0, key = 0, line = 0, lenbuf = 0, keyword_nums = sizeof(variables)/sizeof(char *);
66 
67 	if ((fd = fopen(cfgfile, "r")) == NULL) {
68 		fprintf(stderr, "loadconfig: cannot open spamguard configuration file %s, exiting...\n", cfgfile);
69 		exit(-1);
70 	}
71 
72 	while ((fgets(buf, BUFSIZE, fd)) != NULL) {
73 		line++;
74 		if (buf[0] == '#')
75 			continue;
76 		if ((lenbuf = strlen(buf)) <= 1)
77 			continue;
78 		cp1 = buf;
79 		cp2 = keyword;
80 		while (isspace((int)*cp1) && ((cp1 - buf) < lenbuf))
81 			cp1++;
82 		while (isgraph((int)*cp1) && (*cp1 != '=') && (j++ < KEYSIZE - 1) && ((cp1 - buf) < lenbuf))
83 			*cp2++ = *cp1++;
84 		*cp2 = '\0';
85 		j = 0;
86 		cp2 = value;
87 		while ((*cp1 != '\0') && (*cp1 !='\n') && (*cp1 !='=') && ((cp1 - buf) < lenbuf))
88 			cp1++;
89 		cp1++;
90 		while (isspace((int)*cp1) && ((cp1 - buf) < lenbuf))
91 			cp1++;
92 		if (*cp1 == '"')
93 			cp1++;
94 		while ((*cp1 != '\0') && (*cp1 !='\n') && (*cp1 !='"') && (j++ < VALSIZE - 1) && ((cp1 - buf) < lenbuf))
95 			*cp2++ = *cp1++;
96 		*cp2-- = '\0';
97 		j = 0;
98 		if (keyword[0] =='\0' || value[0] =='\0')
99 			continue;
100 		key = 0;
101 		for (i = 0; i < keyword_nums; i++) {
102 			if ((strncmp(keyword, variables[i], KEYSIZE)) == 0) {
103 				key = i;
104 				break;
105 			}
106 		}
107 
108 		switch(key) {
109 			case 0:
110 				fprintf(stderr, "Illegal Keyword: %s\n", keyword);
111 				break;
112 			case 1:
113 				strncpy(logtype, value, VALSIZE);
114 				break;
115 			case 2:
116 				strncpy(logfile, value, VALSIZE);
117 				break;
118 			case 3:
119 				strncpy(ignorefile, value, VALSIZE);
120 				break;
121 			case 4:
122 				strncpy(badmailfile, value, VALSIZE);
123 				break;
124 			case 5:
125 				strncpy(hostname, value, VALSIZE);
126 				break;
127 			case 6:
128 				strncpy(sysadmin, value, VALSIZE);
129 				break;
130 			case 7:
131 				strncpy(statfile, value, VALSIZE);
132 				break;
133 			case 8:
134 				strncpy(mail_command, value, VALSIZE);
135 				break;
136 			case 9:
137 				strncpy(makemap_command, value, VALSIZE);
138 				break;
139 			case 10:
140 				wcnt = atoi(value);
141 				w = 1;
142 				break;
143 			case 11:
144 				bcnt = atoi(value);
145 				b = 1;
146 				break;
147 			case 12:
148 				pcnt = atoi(value);
149 				p = 1;
150 				break;
151 			case 13:
152 				pcnt = atoi(value);
153 				strncpy(highfile, value, VALSIZE);
154 				break;
155                         case 14:
156                                 enable_subj_filt = atoi(value);
157                                 break;
158                         case 15:
159                                 strncpy(qsheff_rules_file, value, VALSIZE);
160                                 break;
161                         case 16:
162                                 strncpy(trim_subj_str, value, VALSIZE);
163                                 break;
164 		}
165 	}
166 
167 	if (fclose(fd) != 0) {
168 		fprintf(stderr, "File: %s - Line: %d: %s.\n", __FILE__, __LINE__, strerror(errno));
169 		exit(-1);
170 	}
171 }
172 
173 void
readconfig(const char * cfgfile)174 readconfig(const char *cfgfile)
175 {
176 	struct stat statbuf;
177 
178 	loadconfig(cfgfile);
179 
180 	printf("logtype: %s\n", logtype);
181 	printf("logfile: %s\n", logfile);
182 	printf("ignorefile: %s\n", ignorefile);
183 	printf("highfile: %s\n", highfile);
184 	printf("badmailfile: %s\n", badmailfile);
185 	printf("statfile: %s\n", statfile);
186 	printf("warning count: %d\n", wcnt);
187 	printf("block count: %d\n", bcnt);
188 	printf("paranoid count: %d\n", pcnt);
189         printf("qsheff subject filtering: %d\n", enable_subj_filt);
190 	printf("qsheff rules file: %s\n", qsheff_rules_file);
191         printf("trim subject string: %s\n", trim_subj_str);
192 
193 	if ((enable_subj_filt != 0) && (enable_subj_filt != 1) && (enable_subj_filt) != 2) {
194                 printf("enable_subj_filt value must be 0, 1 or 2\n");
195                 exit(-1);
196         }
197 
198 	if (b == 0) {
199 		printf("You must set block count(bcnt) value in spamguard.conf\n");
200 		exit(-1);
201 	}
202 
203 	if (w && b && (wcnt >= bcnt)) {
204 		printf("Error!: warning count value:%d must be smaller than block count value:%d\n", wcnt, bcnt);
205 		exit(-1);
206 	}
207 
208 	if (w && (wcnt >= pcnt)) {
209 		printf("Error!: warning count value:%d must be smaller than paranoid count value:%d\n", wcnt, pcnt);
210 		exit(-1);
211 	}
212 
213 	if (b && (bcnt >= pcnt)) {
214 		printf("Error!: block count value:%d must be smaller than paranoid count value:%d\n", bcnt, pcnt);
215 		exit(-1);
216 	}
217 
218 	if ((strlen(hostname)) <= 0 )
219         	gethostname(hostname, VALSIZE);
220 	        printf("hostname: %s\n", hostname);
221 
222 	if ((strcasecmp(logtype, "qmail") != 0) && (strcasecmp(logtype, "sendmail") != 0)
223 		&& (strcasecmp(logtype, "postfix") != 0)
224 		&& (strcasecmp(logtype, "qsheff") != 0)) {
225 		printf("Invalid logtype: %s\naccepted logytpes are: "
226 		        "qmail, qsheff, sendmail or postfix\n", logtype);
227                 exit(-1);
228 	}
229 
230 	if ((stat(logfile, &statbuf)) == 0) {
231 		if((S_ISREG(statbuf.st_mode)) == 0) {
232 			fprintf(stderr, "You are using: %s log type "
233 					"logfile: %s  must be a regular file\n", logtype, logfile);
234 			exit(-1);
235 		}
236 	}
237 
238 	else {
239 		fprintf(stderr, "You are using: %s log type "
240 				"logfile: %s file does not exist!\n", logtype, logfile);
241 		exit(-1);
242 	}
243 
244 	if ((strlen(ignorefile)) <= 0) {
245 		printf("You must define a ignorefile\n");
246 		exit(-1);
247 	}
248 
249 	if ((strlen(badmailfile)) <= 0) {
250 		printf("You must define a badmailfile\n");
251 		exit(-1);
252 	}
253 
254 	if ((strlen(sysadmin)) <= 0) {
255 		printf("You must define a sysadmin email address\n");
256 		exit(-1);
257 	}
258 
259 	if ((strlen(mail_command)) <= 0) {
260 		printf("You must set mail_command value\n");
261 		exit(-1);
262 	}
263 
264 	if (((strcasecmp(logtype, "sendmail") == 0) || (strcasecmp(logtype, "postfix") == 0)) && (strlen(makemap_command) == 0)){
265 		printf("You are using logtype:%s, you must set makemap_command value\n", logtype);
266 		exit(-1);
267 	}
268 
269         if ((strlen(qsheff_rules_file)) <= 0) {
270                 printf("Wonna enabling qsheff subject filtering? Please specify a valid path for qsheff rules file\n");
271                 exit(-1);
272 	}
273 
274         if ((strlen(trim_subj_str) <= 0) && (enable_subj_filt == 2)) {
275                 printf("Wonna trimming subjects? Please type valid word(s) or change 'enable_subj_filt' value to 0 or 1\n");
276                 exit(-1);
277         }
278 }
279