1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <ctype.h>
11
12 #include "loadconfig.h"
13 #include "Data.h"
14 #include "Dir.h"
15 #include "Parser.h"
16
17
loadconfig(char * cfgfile)18 void loadconfig(char *cfgfile) /* load isoqlog configuration file */
19 {
20 FILE *fd = NULL;
21 char buf[BUFSIZE];
22 char keyword[KEYSIZE];
23 char value[VALSIZE];
24 int lenbuf = 0;
25 char *cp1 = NULL, *cp2 = NULL;
26 char *variables[] = { "Invalid",
27 "outputdir",
28 "logtype",
29 "logstore",
30 "domainsfile",
31 "langfile",
32 "htmldir",
33 "hostname",
34 "maxsender",
35 "maxreceiver",
36 "maxtotal",
37 "maxbyte"
38 };
39
40 int i, j, key, line, keyword_nums = sizeof(variables)/sizeof(char *);
41
42
43 if ((fd = fopen(cfgfile, "r")) == NULL) {
44 fprintf(stderr, "loadconfig: cannot open isoqlog configuration file %s, exiting...\n", cfgfile);
45 exit(-1);
46 }
47 line = 0;
48 while ((fgets(buf, BUFSIZE, fd)) != NULL) {
49 line++;
50 if (buf[0] == '#')
51 continue;
52 if ((lenbuf = strlen(buf)) <= 1)
53 continue;
54 cp1 = buf;
55 cp2 = keyword;
56 j = 0;
57 while (isspace((int)*cp1) && ((cp1 - buf) < lenbuf))
58 cp1++;
59 while(isgraph((int)*cp1) && *cp1 != '=' && (j++ < KEYSIZE - 1) && (cp1 - buf) < lenbuf)
60 *cp2++ = *cp1++;
61 *cp2 = '\0';
62 cp2 = value;
63 while ((*cp1 != '\0') && (*cp1 !='\n') && (*cp1 !='=') && ((cp1 - buf) < lenbuf))
64 cp1++;
65 cp1++;
66 while (isspace((int)*cp1) && ((cp1 - buf) < lenbuf))
67 cp1++;
68 if (*cp1 == '"')
69 cp1++;
70 j = 0;
71 while ((*cp1 != '\0') && (*cp1 !='\n') && (*cp1 !='"') && (j++ < VALSIZE - 1) && ((cp1 - buf) < lenbuf))
72 *cp2++ = *cp1++;
73 *cp2-- = '\0';
74 if (keyword[0] =='\0' || value[0] =='\0')
75 continue;
76 key = 0;
77 for (i = 0; i < keyword_nums; i++) {
78 if ((strncmp(keyword, variables[i], KEYSIZE)) == 0) {
79 key = i;
80 break;
81 }
82 }
83
84 switch(key) {
85 case 0:
86 fprintf(stderr, "Illegal Keyword: %s\n", keyword);
87 break;
88 case 1:
89 strncpy(outputdir, value, VALSIZE);
90 break;
91 case 2:
92 strncpy(logtype, value, VALSIZE);
93 break;
94 case 3:
95 strncpy(logstore, value, VALSIZE);
96 break;
97 case 4:
98 strncpy(domainsfile, value, VALSIZE);
99 break;
100 case 5:
101 strncpy(langfile, value, VALSIZE);
102 break;
103 case 6:
104 strncpy(htmldir, value, VALSIZE);
105 break;
106 case 7:
107 strncpy(hostname, value, VALSIZE);
108 break;
109 case 8:
110 maxsender = atoi(value);
111 break;
112 case 9:
113 maxreceiver = atoi(value);
114 break;
115 case 10:
116 maxtotal = atoi(value);
117 break;
118 case 11:
119 maxbyte = atoi(value);
120 break;
121 }
122 }
123 fclose(fd);
124 }
125
readconfig(char * cfgfile)126 void readconfig(char *cfgfile)
127 {
128 FILE *fd = NULL;
129 char buf[1024];
130 int count = 0; /*counter for domains */
131 struct stat statbuf;
132 char *dir;
133 struct tm *t;
134
135 time(&today);
136 if ((t = localtime(&today)) == NULL) {
137 fprintf(stderr, "localtime failed: %s\n", strerror(errno));
138 return;
139 }
140
141 cur_year = t->tm_year + 1900;
142 cur_month = t->tm_mon + 1;
143 cur_day = t->tm_mday ;
144 cur_hour = t->tm_hour;
145 cur_min = t->tm_min;
146 cur_sec = t->tm_sec;
147 printf("Year: %d Month: %d\n", cur_year, cur_month);
148
149 loadconfig(cfgfile);
150 printf("outputdir:%s\n", outputdir);
151 printf("htmldir:%s\n", htmldir);
152 printf("logtype:%s\n", logtype);
153 printf("logstore:%s\n", logstore);
154 printf("langfile:%s\n", langfile);
155 printf("maxsender:%d\n", maxsender);
156 printf("maxreceiver:%d\n", maxreceiver);
157 printf("maxtotal:%d\n", maxtotal);
158 printf("maxbyte:%d\n", maxbyte);
159
160 if ((strlen(hostname)) == 0 )
161 gethostname(hostname, VALSIZE);
162 printf("hostname: %s\n", hostname);
163
164 if ((strncasecmp(logtype, "qmail-multilog", VALSIZE) != 0) && (strncasecmp(logtype, "qmail-syslog", VALSIZE) != 0)
165 && (strncasecmp(logtype, "sendmail", VALSIZE) != 0) && (strncasecmp(logtype, "postfix", VALSIZE) != 0)
166 && (strcasecmp(logtype, "exim") != 0)) {
167 printf("Invalid logtype: %.128s\naccepted logytpes are: "
168 "qmail-multilog, qmail-syslog, sendmail, postfix or exim\n", logtype);
169 exit(-1);
170 }
171 if ((strncasecmp(logtype, "qmail-multilog", VALSIZE)) == 0) {
172 if ((stat(logstore, &statbuf)) == 0) {
173 if((S_ISDIR(statbuf.st_mode)) == 0) {
174 fprintf(stderr, "You are using: %s log type "
175 "logstore: %s must be a directory\n", logtype, logstore);
176 exit(-1);
177 }
178 }
179 else {
180 fprintf(stderr, "You are using: %s log type "
181 "logstore: %s directory does not exist!\n", logtype, logstore);
182 exit(-1);
183 }
184 }
185 else {
186 if ((stat(logstore, &statbuf)) == 0) {
187 if((S_ISREG(statbuf.st_mode)) == 0) {
188 fprintf(stderr, "You are using: %s log type "
189 "logstore: %s must be a regular file\n", logtype, logstore);
190 exit(-1);
191 }
192 }
193 else {
194 fprintf(stderr, "You are using: %s log type "
195 "logstore: %s file does not exist!\n", logtype, logstore);
196 exit(-1);
197 }
198 }
199 if ((strlen(outputdir)) == 0 ) {
200 fprintf(stderr, "You must define output directory");
201 exit(-1);
202 }
203 if ((strlen(domainsfile)) == 0 ) {
204 fprintf(stderr, "You must define domainsfile");
205 exit(-1);
206 }
207 createdir(outputdir);
208 if ((fd = fopen(domainsfile, "r")) == NULL) {
209 fprintf(stderr, "domainsfile: %s could not be opened\n", domainsfile);
210 exit(-1);
211 }
212 while ((fgets(buf, 1024, fd)) != NULL) {
213 printf("Domains %s", buf);
214 removespaces(buf, strlen(buf));
215 if (strlen(buf) == 0)
216 ;
217 else {
218 lowercase(buf, strlen(buf)); /* lowercase domains */
219 addDomain(buf);
220 dir = malloc((strlen(outputdir) + strlen(buf) + 100) * sizeof(char));
221 sprintf(dir, "%s/%s", outputdir, buf);
222 createdir(dir); /* create domain directory */
223 sprintf(dir, "%s/%s/%d", outputdir, buf, cur_year);
224 createdir(dir); /* create year dir under domain directory */
225 sprintf(dir, "%s/%s/%d/%d", outputdir, buf, cur_year, cur_month);
226 createdir(dir); /* create month directory */
227 count++;
228 }
229 }
230 if (count == 0) {
231 fprintf(stderr, "Empty domains file. you must add at least one domain\n");
232 exit(-1);
233 }
234 sprintf(dir, "%s/%s", outputdir, "general");
235 createdir(dir); /* create domain directory */
236 sprintf(dir, "%s/%s/%d", outputdir, "general" , cur_year);
237 createdir(dir); /* create year dir under domain directory */
238
239 sprintf(dir, "%s/%s/%d/%d", outputdir, "general", cur_year, cur_month);
240 createdir(dir); /* create month directory */
241
242 free(dir);
243 /* close domains file */
244 fclose(fd);
245 }
246
247 int
removespaces(char * buf,int len)248 removespaces(char *buf, int len)
249 {
250 char *cp = buf;
251 char *sv = buf;
252
253 for (; *buf != '\0' && *buf != '\r' && *buf != '\n' && ((buf - sv) < len); buf++)
254 if (*buf == ' ')
255 continue;
256 else
257 *cp++ = *buf;
258 *cp = 0x0;
259 return cp - sv;
260 }
261