]> git.ipfire.org Git - thirdparty/sarg.git/blob - index.c
Applied an extended version of debian patch: opensuse_5_limit_sprintf.patch
[thirdparty/sarg.git] / index.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27 #include "include/defs.h"
28
29 void make_index(void)
30 {
31
32 FILE *fp_ou, *fp_ou2, *fp_ou3, *fp_tmp, *fp_tmp2, *fp_tmp3;
33 DIR *dirp, *dirp2, *dirp3;
34 struct dirent *direntp;
35 struct dirent *direntp2;
36 struct dirent *direntp3;
37 char html[MAXLEN];
38 char wdir[MAXLEN];
39 char wdir_tmp[MAXLEN];
40 char wdir_tmp2[MAXLEN];
41 char wdir_tmp3[MAXLEN];
42 char newname[512];
43 char month[4];
44 char period[80];
45 char data[80];
46 char tuser[20];
47 char tbytes[20];
48 char media[20];
49 char ftime[128];
50 char day[4], mon[4], year[6], hour[10];
51 char h[3], m[3], s[3];
52 int cstatus;
53 char y1[5], y2[5];
54 char d1[3], d2[3];
55 char m1[4], m2[4];
56
57 if(LastLog[0] != '\0') mklastlog(outdir);
58
59 sprintf(wdir,"%sindex.html",outdir);
60 sprintf(wdir_tmp,"%sindex.unsort",outdir);
61 sprintf(wdir_tmp2,"%sindex.sort",outdir);
62 strcpy(hbc1,"class=\"header\"");
63
64 if(strcmp(Index,"no") == 0) {
65 if(access(wdir, R_OK) == 0) unlink(wdir);
66 return;
67 }
68
69 if(debug) debuga("%s",text[53]);
70
71 // Root dir
72 dirp = opendir(outdir);
73 while ((direntp = readdir( dirp )) != NULL) {
74 if(strcmp(IndexTree,"date") == 0) {
75 if(!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1])) continue;
76 if(strlen(direntp->d_name) > 4) {
77 bzero(y1,5);
78 bzero(y2,5);
79 bzero(m1,4);
80 bzero(m2,4);
81 bzero(d1,3);
82 bzero(d2,3);
83 if(strcmp(df,"u") == 0) {
84 strncpy(y1,direntp->d_name,4);
85 strncpy(m1,direntp->d_name+4,3);
86 strncpy(d1,direntp->d_name+7,2);
87 strncpy(y2,direntp->d_name+10,4);
88 strncpy(m2,direntp->d_name+14,3);
89 strncpy(d2,direntp->d_name+17,2);
90 } else if(strcmp(df,"e") == 0) {
91 strncpy(y1,direntp->d_name+5,4);
92 strncpy(m1,direntp->d_name+2,3);
93 strncpy(d1,direntp->d_name,2);
94 strncpy(y2,direntp->d_name+15,4);
95 strncpy(m2,direntp->d_name+12,3);
96 strncpy(d2,direntp->d_name+10,2);
97 }
98 conv_month(m1);
99 conv_month(m2);
100 }
101 sprintf(val1,"%s%s",outdir,y1);
102 if(access(val1, R_OK) != 0) mkdir(val1,0755);
103 if(strcmp(m1,m2) != 0) sprintf(val2,"%s/%s-%s",val1,m1,m2);
104 else sprintf(val2,"%s/%s",val1,m1);
105 if(access(val2, R_OK) != 0) mkdir(val2,0755);
106 if(strcmp(d1,d2) != 0) sprintf(val3,"%s/%s-%s",val2,d1,d2);
107 else sprintf(val3,"%s/%s",val2,d1);
108 sprintf(val4,"%s%s",outdir,direntp->d_name);
109 rename(val4,val3);
110 sprintf(val5,"%s/images",val2);
111 if(access(val5, R_OK) != 0) {
112 sprintf(val5,"ln -s %simages %s/images",outdir,val2);
113 cstatus=system(val5);
114 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
115 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
116 fprintf(stderr, "SARG: command: %s\n",val5);
117 exit(1);
118 }
119 }
120 } else {
121 if(!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1])) continue;
122 if(strlen(direntp->d_name) == 4) {
123 strcpy(y1,direntp->d_name);
124 sprintf(val1,"%s%s",outdir,direntp->d_name);
125 dirp2 = opendir(val1);
126 while ((direntp2 = readdir( dirp2 )) != NULL) {
127 if(!isdigit(direntp2->d_name[0]) && !isdigit(direntp2->d_name[1])) continue;
128 sprintf(val2,"%s/%s",val1,direntp2->d_name);
129 dirp3 = opendir(val2);
130 while ((direntp3 = readdir( dirp3 )) != NULL) {
131 if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
132 bzero(newname,512);
133 strcpy(warea,direntp2->d_name);
134 if(strstr(warea,"-") != 0) {
135 if (getword_multisep(m1,sizeof(m1),warea,'-')<0) {
136 printf("SARG: Maybe you have a broken record or garbage in your %s directory.\n",val2);
137 exit(1);
138 }
139 strcpy(m2,warea);
140 conv_month_name(m1);
141 conv_month_name(m2);
142 } else {
143 strcpy(m1,warea);
144 conv_month_name(m1);
145 strcpy(m2,m1);
146 }
147 strcpy(warea,direntp3->d_name);
148 if(strstr(warea,"-") != 0) {
149 if (getword_multisep(d1,sizeof(d1),warea,'-')<0) {
150 printf("SARG: Maybe you have a broken record or garbage in your %s directory.\n",val2);
151 exit(1);
152 }
153 strcpy(d2,warea);
154 } else {
155 strcpy(d1,warea);
156 strcpy(d2,warea);
157 }
158 if(strcmp(df,"u") == 0) sprintf(val4,"%s%s%s%s-%s%s%s",outdir,y1,m1,d1,y1,m2,d2);
159 else if(strcmp(df,"e") == 0) sprintf(val4,"%s%s%s%s-%s%s%s",outdir,d1,m1,y1,d2,m2,y1);
160 sprintf(val5,"%s%s/%s/%s",outdir,y1,direntp2->d_name,direntp3->d_name);
161 if(rename(val5,val4)) {
162 fprintf(stderr, "SARG: (index) rename error - %s\n",strerror(errno));
163 exit(1);
164 }
165 }
166 (void)rewinddir( dirp3 );
167 (void)closedir( dirp3 );
168 }
169 (void)rewinddir( dirp2 );
170 (void)closedir( dirp2 );
171 }
172 // sprintf(cmd,"rm -rf %s%s\n",outdir,direntp->d_name);
173 // system(cmd);
174 }
175 }
176 (void)rewinddir( dirp );
177 (void)closedir( dirp );
178
179 if((fp_tmp=fopen(wdir_tmp,"w"))==NULL) {
180 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp);
181 exit(1);
182 }
183
184 dirp = opendir(outdir);
185 while ((direntp = readdir( dirp )) != NULL) {
186 char *p;
187 if(strcmp(IndexTree,"date") == 0) {
188 if(strlen(direntp->d_name) > 4 || (!isdigit(direntp->d_name[0]) && !isdigit(direntp->d_name[1]))) continue;
189 fprintf(fp_tmp,"%s %s\n",direntp->d_name,get_size(outdir,direntp->d_name));
190 continue;
191 } else {
192 if(strstr(direntp->d_name,"-") == 0) continue;
193 bzero(newname, 512);
194 if(strcmp(df,"u") == 0) {
195 strncat(newname,direntp->d_name,4);
196 strncpy(month,direntp->d_name+4,3);
197 } else {
198 strncat(newname,direntp->d_name+5,4);
199 strncpy(month,direntp->d_name+2,3);
200 }
201 month[3]='\0';
202 conv_month(month);
203 strcat(newname,month);
204 if(strcmp(df,"u") == 0) strncat(newname,direntp->d_name+7,2);
205 else strncat(newname,direntp->d_name,2);
206 obtdate(outdir,direntp->d_name,data);
207 obtuser(outdir,direntp->d_name,tuser);
208 obttotal(outdir,direntp->d_name,tbytes,tuser,media);
209 strcpy(html,data);
210 if (getword_multisep(mon,sizeof(mon),html,' ')<0) {
211 printf("SARG: Maybe you have a broken week day in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
212 exit(1);
213 }
214 if (getword_multisep(mon,sizeof(mon),html,' ')<0) {
215 printf("SARG: Maybe you have a broken month in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
216 exit(1);
217 }
218 if (getword_multisep(day,sizeof(day),html,' ')<0) {
219 printf("SARG: Maybe you have a broken day in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
220 exit(1);
221 }
222 if (getword_multisep(hour,sizeof(hour),html,' ')<0) {
223 printf("SARG: Maybe you have a broken time in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
224 exit(1);
225 }
226 p = strchr(html, ' ');
227 if (!p) p = html;
228 if (getword_multisep(year,sizeof(year),html,' ')<0) {
229 printf("SARG: Maybe you have a broken year in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
230 exit(1);
231 }
232 strcpy(html,hour);
233 if (getword_multisep(h,sizeof(h),html,':')<0) {
234 printf("SARG: Maybe you have a broken hour in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
235 exit(1);
236 }
237 if (getword_multisep(m,sizeof(m),html,':')<0) {
238 printf("SARG: Maybe you have a broken minute in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
239 exit(1);
240 }
241 if (getword_multisep(s,sizeof(s),html,0)<0) {
242 printf("SARG: Maybe you have a broken second in your %s%s/sarg-date file.\n",outdir,direntp->d_name);
243 exit(1);
244 }
245 buildymd(day,mon,year,ftime);
246 fprintf(fp_tmp,"%s%s%s%s;%s;%s;%s;%s;%s;%s\n",ftime, h, m, s, direntp->d_name, data, tuser, tbytes, media,newname);
247 continue;
248 }
249 }
250
251 if(fp_tmp) fclose(fp_tmp);
252 if(strcmp(IndexTree,"file") == 0) {
253 (void)rewinddir( dirp );
254 (void)closedir( dirp );
255 }
256
257 if(strcmp(IndexTree,"date") == 0) {
258 if(strcmp(IndexSortOrder,"A") == 0) sprintf(warea,"sort -k 1,1 '%s' -o '%s'", wdir_tmp, wdir_tmp2);
259 else sprintf(warea,"sort -r -k 1,1 '%s' -o '%s'", wdir_tmp, wdir_tmp2);
260 cstatus=system(warea);
261 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
262 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
263 fprintf(stderr, "SARG: sort command: %s\n",warea);
264 exit(1);
265 }
266 if((fp_tmp=fopen(wdir_tmp2,"r"))==NULL) {
267 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
268 fprintf(stderr, "SARG: sort command: %s\n",warea);
269 exit(1);
270 }
271 unlink(wdir_tmp);
272 if((fp_ou=fopen(wdir,"w"))==NULL) {
273 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
274 exit(1);
275 }
276 write_html_header(fp_ou, ".");
277 fprintf(fp_ou,"<tr><th %s>%s</th><th %s>%s</th></tr>\n",hbc1,text[130],hbc1,text[132]);
278 while(fgets(wwork1,MAXLEN,fp_tmp)!=NULL) {
279 if (getword_multisep(tmp4,sizeof(tmp4),wwork1,' ')<0) {
280 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
281 exit(1);
282 }
283 fprintf(fp_ou,"<tr><td class=\"data2\"><a href=\"%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",tmp4,tmp4,wwork1);
284 sprintf(tmp2,"%s%s",outdir,tmp4);
285 sprintf(tmp3,"%s%s/index.unsort",outdir,tmp4);
286 // Year dir
287 if((fp_ou2=fopen(tmp3,"w"))==NULL) {
288 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp3);
289 exit(1);
290 }
291 dirp2 = opendir(tmp2);
292 while ((direntp2 = readdir( dirp2 )) != NULL) {
293 if(!isdigit(direntp2->d_name[0]) && !isdigit(direntp2->d_name[1])) continue;
294 fprintf(fp_ou2,"%s\n",direntp2->d_name);
295 }
296 if(fp_ou2) fclose(fp_ou2);
297 (void)rewinddir(dirp2);
298 (void)closedir(dirp2);
299 sprintf(wdir_tmp3,"%s%s/index.sort",outdir,tmp4);
300 if(strcmp(IndexSortOrder,"A") == 0) sprintf(csort,"sort -n '%s' -o '%s'", tmp3, wdir_tmp3);
301 else sprintf(csort,"sort -n -r '%s' -o '%s'", tmp3, wdir_tmp3);
302 cstatus=system(csort);
303 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
304 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
305 fprintf(stderr, "SARG: sort command: %s\n",csort);
306 exit(1);
307 }
308 unlink(tmp3);
309 if((fp_tmp2=fopen(wdir_tmp3,"r"))==NULL) {
310 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp3);
311 exit(1);
312 }
313 sprintf(tmp3,"%s%s/index.html",outdir,tmp4);
314 if((fp_ou2=fopen(tmp3,"w"))==NULL) {
315 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
316 exit(1);
317 }
318 write_html_header(fp_ou2,"..");
319 fprintf(fp_ou2,"<tr><th %s>%s/%s</th></tr>\n",hbc1,text[130],text[131]);
320 while(fgets(wwork1,MAXLEN,fp_tmp2)!=NULL) {
321 wwork1[strlen(wwork1)-1]='\0';
322 strcpy(tmp5,wwork1);
323 if(strstr(tmp5,"-") != 0) {
324 if (getword_multisep(warea,sizeof(warea),tmp5,'-')<0) {
325 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp3);
326 exit(1);
327 }
328 name_month(warea,sizeof(warea));
329 sprintf(tmp6,"%s-",warea);
330 name_month(tmp5,sizeof(tmp5));
331 sprintf(nmonth,"%s%s",tmp6,tmp5);
332 } else {
333 strcpy(nmonth,tmp5);
334 name_month(nmonth,sizeof(nmonth));
335 }
336 fprintf(fp_ou2,"<tr><td class=\"data2\"><a href=\"%s\">%s %s</a></td></tr>\n",wwork1,tmp4,nmonth);
337
338 sprintf(val1,"%s%s/%s",outdir,tmp4,wwork1);
339 sprintf(tmp5,"%s%s/%s/index.unsort",outdir,tmp4,wwork1);
340 if((fp_ou3=fopen(tmp5,"w"))==NULL) {
341 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp5);
342 exit(1);
343 }
344 // month dir
345 dirp3 = opendir(val1);
346 while ((direntp3 = readdir( dirp3 )) != NULL) {
347 if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
348 fprintf(fp_ou3,"%s\n",direntp3->d_name);
349 }
350 if(fp_ou3) fclose(fp_ou3);
351 (void)rewinddir(dirp3);
352 (void)closedir(dirp3);
353 unlink(wdir_tmp3);
354 sprintf(tmp6,"%s%s/%s/index.sort",outdir,tmp4,wwork1);
355 if(strcmp(IndexSortOrder,"A") == 0) sprintf(csort,"sort -n '%s' -o '%s'", tmp5, tmp6);
356 else sprintf(csort,"sort -n -r '%s' -o '%s'", tmp5, tmp6);
357 cstatus=system(csort);
358 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
359 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
360 fprintf(stderr, "SARG: sort command: %s\n",csort);
361 exit(1);
362 }
363 unlink(tmp5);
364 sprintf(val2,"%s%s/%s/index.html",outdir,tmp4,wwork1);
365 sprintf(val3,"%s/%s",tmp4,wwork1);
366 unlink(val2);
367 if((fp_ou3=fopen(val2,"w"))==NULL) {
368 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],val2);
369 exit(1);
370 }
371 if((fp_tmp3=fopen(tmp6,"r"))==NULL) {
372 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],tmp6);
373 exit(1);
374 }
375 write_html_header(fp_ou3,"../..");
376 fprintf(fp_ou3,"<tr><th %s>%s/%s/%s</th></tr>\n",hbc1,text[130],text[131],text[127]);
377 while(fgets(warea,MAXLEN,fp_tmp3)!=NULL) {
378 warea[strlen(warea)-1]='\0';
379 fprintf(fp_ou3,"<tr><td class=\"data2\"><a href=\"%s\">%s %s %s</a></td></tr>\n",warea,tmp4,nmonth,warea);
380 }
381 if(fp_tmp3) fclose(fp_tmp3);
382 write_html_trailer(fp_ou3);
383 if(fp_ou3) fclose(fp_ou3);
384 unlink(tmp6);
385 }
386 write_html_trailer(fp_ou2);
387 if(fp_ou2) fclose(fp_ou2);
388 }
389 (void)rewinddir(dirp);
390 (void)closedir(dirp);
391 if(fp_tmp) fclose(fp_tmp);
392 unlink(tmp6);
393 unlink(wdir_tmp2);
394 } else {
395 if(strcmp(IndexSortOrder,"A") == 0) sprintf(warea,"sort -t';' -k 7,7 -k 1,1 '%s' -o '%s'", wdir_tmp, wdir_tmp2);
396 else sprintf(warea,"sort -r -t';' -k 7,7 -k 1,1 '%s' -o '%s'", wdir_tmp, wdir_tmp2);
397 cstatus=system(warea);
398 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
399 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
400 fprintf(stderr, "SARG: sort command: %s\n",warea);
401 exit(1);
402 }
403 if((fp_tmp2=fopen(wdir_tmp2,"r"))==NULL) {
404 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir_tmp2);
405 fprintf(stderr, "SARG: sort command: %s\n",warea);
406 exit(1);
407 }
408 unlink(wdir_tmp);
409 if((fp_ou=fopen(wdir,"w"))==NULL) {
410 fprintf(stderr, "SARG: (index) %s: %s\n",text[45],wdir);
411 exit(1);
412 }
413 write_html_header(fp_ou,".");
414 fprintf(fp_ou,"<tr><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th><th %s>%s</th></tr>\n",hbc1,text[101],hbc1,text[102],hbc1,text[103],hbc1,text[93],hbc1,text[96]);
415 while(fgets(buf,MAXLEN,fp_tmp2)!=NULL) {
416 if (getword_multisep(period,sizeof(period),buf,';')<0) {
417 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
418 exit(1);
419 }
420 if (getword_multisep(period,sizeof(period),buf,';')<0) {
421 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
422 exit(1);
423 }
424 if (getword_multisep(data,sizeof(data),buf,';')<0) {
425 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
426 exit(1);
427 }
428 if (getword_multisep(tuser,sizeof(tuser),buf,';')<0) {
429 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
430 exit(1);
431 }
432 if (getword_multisep(tbytes,sizeof(tbytes),buf,';')<0) {
433 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
434 exit(1);
435 }
436 if (getword_multisep(media,sizeof(media),buf,';')<0) {
437 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir_tmp2);
438 exit(1);
439 }
440 fprintf(fp_ou,"<tr><td class=\"data2\"><a href='%s/%s'>%s</a></td><td class=\"data2\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s</td></tr>\n",period,ReplaceIndex,period,data,tuser,tbytes,media);
441 }
442 if(fp_tmp2) fclose(fp_tmp2);
443 unlink(wdir_tmp2);
444 }
445
446 write_html_trailer(fp_ou);
447 if(fp_ou) fclose(fp_ou);
448 }