]> git.ipfire.org Git - thirdparty/sarg.git/blob - util.c
Detect external commands failures and print the exact command that produced the error...
[thirdparty/sarg.git] / util.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 // #define LEGACY_MY_ATOLL
27 // #define LEGACY_TESTVALIDUSERCHAR
28
29 #include "include/conf.h"
30
31 static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
32 static char mtab2[12][3]={"01","02","03","04","05","06","07","08","09","10","11","12"};
33
34 /*void fgetword(char *word, char *line, int stop)
35 {
36 //VARIANT N1
37 int x;
38
39 for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x];
40 word[x] = '\0';
41
42 //VARIANT N2
43 char *tchar;
44 int difflen;
45
46 tchar = strchr(line, stop);
47 if (tchar == NULL) strcpy(word, line);
48 else
49 {
50 difflen = tchar - line;
51 strncpy(word, line, difflen);
52 word[difflen] = '\0';
53 }
54 }*/
55
56 int getword(char *word, int limit, char *line, int stop)
57 {
58 int x = 0,y;
59
60 /*if(strlen(line) < 3) {
61 word[0]='\0';
62 return(0);
63 }*/
64
65 for(x=0;((line[x]) && (line[x] != stop ));x++) {
66 if(x>=limit) {
67 printf("SARG: getword loop detected after %d bytes.\n",x);
68 printf("SARG: Record=\"%s\"\n",line);
69 printf("SARG: searching for \'x%x\'\n",stop);
70 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
71 if (limit>0) word[limit-1]='\0';
72 //exit(1);
73 return(-1);
74 }
75 word[x] = line[x];
76 }
77
78 word[x] = '\0';
79 if (line[x]) ++x;
80 y=0;
81
82 while((line[y++] = line[x++]));
83 return(0);
84 }
85
86 int getword_multisep(char *word, int limit, char *line, int stop)
87 {
88 int x = 0,y;
89
90 /*if(strlen(line) < 3) {
91 word[0]='\0';
92 return(0);
93 }*/
94
95 for(x=0;((line[x]) && (line[x] != stop ));x++) {
96 if(x>=limit) {
97 printf("SARG: getword_multisep loop detected.\n");
98 printf("SARG: Record=\"%s\"\n",line);
99 printf("SARG: searching for \'x%x\'\n",stop);
100 printf("SARG: searching for \'x%x\'\n",stop);
101 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
102 if (limit>0) word[limit-1]='\0';
103 //exit(1);
104 return(-1);
105 }
106 word[x] = line[x];
107 }
108
109 word[x] = '\0';
110 while (line[x] && line[x]==stop) ++x;
111 y=0;
112
113 while((line[y++] = line[x++]));
114 return(0);
115 }
116
117 #if 0 //this function seems unused
118 char * getword2(char *word, int limit, char *line, int stop)
119 {
120 int x = 0;
121
122 if(strlen(line) < 3) {
123 word[0]='\0';
124 return( line ) ;
125 }
126
127 // printf( "IN Buffer <%s>\n" , line ) ;
128 for(x=0;((line[x]) && (line[x] != stop && limit ));x++ , limit-- ) word[x] = line[x];
129 if( ! limit) {
130 printf("SARG: getword2 loop detected.\n");
131 printf("SARG: Buffer=\"%s\"\n",line);
132 printf("SARG: searching for \'x%x\'\n",stop);
133 printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
134 exit(1);
135 }
136
137 word[x] = '\0';
138 // printf( "Value <%s>\n" , word ) ;
139 // printf( "OUT Buffer <%s>\n" , line+x+1 ) ;
140 return( line + x +1) ;
141 }
142 #endif
143
144 int getword3(char *word, int limit, char *line, int stop)
145 {
146 int x = 0, y = 0;
147
148 for(x=0; x<limit && (line[x] && (line[x] != stop)) ; x++) word[x] = line[x];
149 if(x>=limit) {
150 printf("SARG: getword3 loop detected after %d bytes.\n",x);
151 printf("SARG: Buffer=\"%s\"\n",line);
152 printf("SARG: searching for \'x%x\'\n",stop);
153 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
154 //exit(1);
155 return(-1);
156 }
157 word[x] = '\0';
158 if(line[x]) ++x;
159 while((line[y++] = line[x++]));
160 return(0);
161 }
162
163
164 #ifdef LEGACY_MY_ATOLL
165
166 // BMG (bguillory@email.com)
167 // 3 August 1999
168 long long int my_atoll (const char *nptr)
169 #define MAXLLL 30 //maximum number of digits in long long (a guess)
170 {
171 int offset=0, x;
172 long long int returnval=0;
173 char one_digit[2];
174
175 one_digit[1]='\0';
176
177 // Soak up all the white space
178 while (isspace(nptr[offset])) {
179 offset++;
180 } //while
181
182 //For each character left to right
183 //change the character to a single digit
184 //multiply what we had before by 10 and add the new digit
185 for(x=offset; x<=MAXLLL+offset && isdigit(nptr[x]); x++) {
186 sprintf(one_digit, "%c", nptr[x]); //I don't know how else to do this
187 returnval = (returnval * 10) + atoi(one_digit);
188 } //for
189
190 return returnval;
191
192 } //my_atoll
193
194 #else
195
196 #define MAXLLL 30 //maximum number of digits in long long (a guess)
197 long long int my_atoll (const char *nptr)
198 {
199 long long int returnval=0;
200 char * t = nptr ;
201 int max_digits = MAXLLL ;
202
203 // Soak up all the white space
204 while (isspace( *t )) {
205 t++;
206 } //while
207
208 //For each character left to right
209 //change the character to a single digit
210 //multiply what we had before by 10 and add the new digit
211
212 for( ; --max_digits && isdigit( *t ) ; t++ )
213 {
214 returnval = ( returnval * 10 ) + ( *t - '0' ) ;
215 }
216
217 return returnval;
218
219 } //my_atoll
220
221 #endif
222
223
224 void my_mkdir(char *name)
225 {
226 char w0[255];
227 char w1[255];
228 char w2[255];
229
230 if(strncmp(name,".",1) == 0 || strncmp(name,"/",1) != 0) {
231 fprintf(stderr,"SARG: Invalid path (%s). Please, use absolute paths only.\n",name);
232 fprintf(stderr,"SARG: process aborted.\n");
233 exit(1);
234 }
235
236 strcpy(w0,name);
237 strcpy(w2,"/");
238 if (getword_multisep(w1,sizeof(w1),w0,'/')<0) {
239 printf("SARG: Maybe you have a broken record or garbage in the directory name %s.\n",name);
240 exit(1);
241 }
242 while(strchr(w0,'/')) {
243 if (getword_multisep(w1,sizeof(w1),w0,'/')<0) {
244 printf("SARG: Maybe you have a broken record or garbage in the directory name %s.\n",name);
245 exit(1);
246 }
247 strcat(w2,w1);
248 if(access(w2, R_OK) != 0) {
249 if(mkdir(w2,0755)) {
250 fprintf(stderr,"SARG: mkdir %s %s\n",w2,strerror(errno));
251 fprintf(stderr,"SARG: process aborted.\n");
252 exit(1);
253 }
254 }
255 strcat(w2,"/");
256 }
257 strcat(w2,w0);
258 if(access(w2, R_OK) != 0) {
259 if(mkdir(w2,0755)) {
260 fprintf(stderr,"SARG: mkdir %s %s\n",w2,strerror(errno));
261 fprintf(stderr,"SARG: process aborted.\n");
262 exit(1);
263 }
264 }
265 }
266
267
268 void my_lltoa(unsigned long long int n, char s[], int len)
269 {
270 int i = 0;
271 int x = 0;
272 char ww[50];
273 do {
274 s[i++] = (n % 10) + '0';
275 } while ((n /= 10) > 0);
276 s[i] = '\0';
277 {
278 int c,i,j;
279 for (i = 0, j = strlen(s)-1; i<j; i++, j--)
280 {
281 c = s[i];
282 s[i] = s[j];
283 s[j] = c;
284 }
285 }
286
287 if(len) {
288 bzero(ww,sizeof(ww));
289 i=len-strlen(s)-1;
290 for(x=0; x<=i; x++)
291 ww[x]='0';
292 i=strlen(s);
293 strncat(ww,s,i>sizeof(ww)?sizeof(ww):i);
294 strcpy(s,ww);
295 }
296
297 }
298
299
300 void builddia(char *dia, char *mes, char *ano, char *df, char *wdata)
301 {
302 char ndia[11];
303 char nmes[3];
304 int x;
305
306 if(strlen(dia) < 1) return;
307
308 ndia[0]='\0';
309 nmes[0]='\0';
310
311 for(x=0; x<12; x++) {
312 if(strcmp(mtab1[x],mes) == 0) {
313 strncpy(nmes,mtab2[x],sizeof(nmes)-1);
314 nmes[sizeof(nmes)-1]=0;
315 break;
316 }
317 }
318
319 snprintf(wdata,9,"%s%s%s",ano,nmes,dia);
320
321 if(strncmp(df,"u",1) != 0)
322 snprintf(ndia,sizeof(ndia),"%s/%s/%s",dia,nmes,ano);
323 else
324 snprintf(ndia,sizeof(ndia),"%s/%s/%s",nmes,dia,ano);
325
326 strcpy(dia,ndia);
327
328 }
329
330
331 void buildymd(char *dia, char *mes, char *ano, char *wdata)
332 {
333 char nmes[3];
334 int x;
335
336 nmes[0]='\0';
337
338 for(x=0; x<12; x++) {
339 if(strcmp(mtab1[x],mes) == 0)
340 strcpy(nmes,mtab2[x]);
341 }
342
343 sprintf(wdata,"%s%s%s",ano,nmes,dia);
344
345 }
346
347
348 void conv_month(char *month)
349 {
350 int x;
351
352 for(x=0; x<12; x++) {
353 if(strcmp(mtab1[x],month) == 0)
354 strcpy(month,mtab2[x]);
355 }
356
357 }
358
359
360 void conv_month_name(char *month)
361 {
362 int x;
363
364 for(x=0; x<12; x++) {
365 if(strcmp(mtab2[x],month) == 0)
366 strcpy(month,mtab1[x]);
367 }
368 }
369
370
371 void name_month(char *month,int month_len)
372 {
373 int x, z=atoi(month)-1;
374 char m[255];
375 char w[20];
376
377 strcpy(m,text[133]);
378
379 for(x=0; x<z; x++)
380 if (getword_multisep(w,sizeof(w),m,',')<0) {
381 printf("SARG: Maybe you have a broken record or garbage in the names of the months.\n");
382 exit(1);
383 }
384 if (getword_multisep(month,month_len,m,',')<0) {
385 printf("SARG: Maybe you have a broken record or garbage in the name of the months.\n");
386 exit(1);
387 }
388 }
389
390
391 void fixper(char *tbuf, char *period, char *duntil)
392 {
393
394 char warea[50];
395 char dia[5], mes[5], ano[5];
396 int x;
397
398 warea[0]='\0';
399
400 strncpy(dia,duntil+6,2);
401 dia[2]='\0';
402 strncpy(mes,duntil+4,2);
403 mes[2]='\0';
404 strncpy(ano,duntil,4);
405 ano[4]='\0';
406
407 for(x=0; x<12; x++) {
408 if(strcmp(mtab2[x],mes) == 0)
409 strcpy(mes,mtab1[x]);
410 }
411
412 if(strcmp(df,"e") == 0)
413 sprintf(warea,"%s%s%s",dia,mes,ano);
414 if(strcmp(df,"u") == 0)
415 sprintf(warea,"%s%s%s",ano,mes,dia);
416
417 strcat(period,warea);
418 }
419
420
421 void debuga(char *msg)
422 {
423 fprintf(stderr, "SARG: %s\n",msg);
424
425 }
426
427
428 void debugaz(char *head, char *msg)
429 {
430 fprintf(stderr, "SARG: (util) %s=%s\n",head, msg);
431
432 }
433
434
435 void fixip(char *ip)
436 {
437 char n1[MAXLEN], n2[MAXLEN], n3[MAXLEN];
438 char wip[MAXLEN];
439 char sep[2]=".";
440 int iflag=0;
441
442 strcpy(wip,ip);
443
444 if(strstr(ip,".") != 0) {
445 strcpy(sep,"_");
446 iflag++;
447 }
448
449 if(iflag) {
450 if (getword(n1,sizeof(n1),wip,'.')<0 || getword(n2,sizeof(n2),wip,'.')<0 || getword(n3,sizeof(n3),wip,'.')<0) {
451 printf("SARG: Maybe you have a broken record or garbage in the IP address.\n");
452 exit(1);
453 }
454 } else {
455 if (getword(n1,sizeof(n1),wip,'_')<0 || getword(n2,sizeof(n2),wip,'_')<0 || getword(n3,sizeof(n3),wip,'_')<0) {
456 printf("SARG: Maybe you have a broken record or garbage in the IP address.\n");
457 exit(1);
458 }
459 }
460 ip[0]='\0';
461 sprintf(ip,"%s%s%s%s%s%s%s",n1,sep,n2,sep,n3,sep,wip);
462
463 }
464
465
466 char *fixnum(long long int value, int n)
467 #define MAXLEN 1024
468 {
469 char num[MAXLEN];
470 char buf[MAXLEN * 2];
471 char *pbuf;
472 static char ret[MAXLEN * 2];
473 char *pret;
474 register int i, j, k;
475 static char abbrev[30];
476
477 my_lltoa(value, num, 0);
478
479 if(strcmp(DisplayedValues,"abbreviation") == 0) {
480 if(strlen(num) <= 3)
481 sprintf(abbrev,"%s",num);
482 if(strlen(num) == 4 || strlen(num) == 7 || strlen(num) == 10 || strlen(num) == 13) {
483 snprintf(abbrev,2,"%s",num);
484 strncat(abbrev,".",1);
485 strncat(abbrev,num+1,2);
486 if(!n) return(abbrev);
487 if(strlen(num) == 4)
488 strncat(abbrev,"K",1);
489 else if(strlen(num) == 7)
490 strncat(abbrev,"M",1);
491 else if(strlen(num) == 10)
492 strncat(abbrev,"G",1);
493 else if(strlen(num) == 13)
494 strncat(abbrev,"T",1);
495 }
496 if(strlen(num) == 5 || strlen(num) == 8 || strlen(num) == 11 || strlen(num) == 14) {
497 snprintf(abbrev,3,"%s",num);
498 strncat(abbrev,".",1);
499 strncat(abbrev,num+2,2);
500 if(!n) return(abbrev);
501 if(strlen(num) == 5)
502 strncat(abbrev,"K",1);
503 else if(strlen(num) == 8)
504 strncat(abbrev,"M",1);
505 else if(strlen(num) == 11)
506 strncat(abbrev,"G",1);
507 else if(strlen(num) == 14)
508 strncat(abbrev,"T",1);
509 }
510 if(strlen(num) == 6 || strlen(num) == 9 || strlen(num) == 12 || strlen(num) == 15) {
511 snprintf(abbrev,4,"%s",num);
512 strncat(abbrev,".",1);
513 strncat(abbrev,num+3,2);
514 if(!n) return(abbrev);
515 if(strlen(num) == 6)
516 strncat(abbrev,"K",1);
517 else if(strlen(num) == 9)
518 strncat(abbrev,"M",1);
519 else if(strlen(num) == 12)
520 strncat(abbrev,"G",1);
521 else if(strlen(num) == 15)
522 strncat(abbrev,"T",1);
523 }
524
525 return(abbrev);
526 }
527
528 bzero(buf, MAXLEN*2);
529
530 pbuf = buf;
531 pret = ret;
532 k = 0;
533
534 for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) {
535 if ( k == 2 && i != 0 ) {
536 k = 0;
537 pbuf[j++] = num[i];
538 if(strcmp(UseComma,"yes") == 0)
539 pbuf[j++] = ',';
540 else pbuf[j++] = '.';
541 continue;
542 }
543 pbuf[j] = num[i];
544 j++;
545 k++;
546 }
547
548 pret[0]='\0';
549
550 for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++)
551 pret[j] = pbuf[i];
552
553 pret[j] = '\0';
554
555 return pret;
556 }
557
558
559 char *fixnum2(long long int value, int n)
560 #define MAXLEN 1024
561 {
562 char num[MAXLEN];
563 char buf[MAXLEN * 2];
564 char *pbuf;
565 static char ret[MAXLEN * 2];
566 char *pret;
567 register int i, j, k;
568 static char abbrev[30];
569
570 my_lltoa(value, num, 0);
571 bzero(buf, MAXLEN*2);
572
573 pbuf = buf;
574 pret = ret;
575 k = 0;
576
577 for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) {
578 if ( k == 2 && i != 0 ) {
579 k = 0;
580 pbuf[j++] = num[i];
581 if(strcmp(UseComma,"yes") == 0)
582 pbuf[j++] = ',';
583 else pbuf[j++] = '.';
584 continue;
585 }
586 pbuf[j] = num[i];
587 j++;
588 k++;
589 }
590
591 pret[0]='\0';
592
593 for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++)
594 pret[j] = pbuf[i];
595
596 pret[j] = '\0';
597
598 return pret;
599 }
600
601
602
603 void buildhref(char * href)
604 {
605 char whref[MAXLEN];
606
607 if(strcmp(href,"./") == 0){
608 href[0]='\0';
609 strcat(href,"<a href='");
610 return;
611 }
612
613 href[strlen(href)-1]='\0';
614 sprintf(whref,"%s",strrchr(href,'/'));
615
616 strcpy(href,"<a href='");
617 strcat(href,whref);
618 strcat(href,"/");
619
620 return;
621
622 }
623
624
625 char *buildtime(long long int elap)
626 {
627
628 int num = elap / 1000;
629 int hor = 0;
630 int min = 0;
631 int sec = 0;
632 static char buf[12];
633
634 buf[0]='\0';
635
636 hor=num / 3600;
637 min=(num % 3600) / 60;
638 sec=num % 60;
639 sprintf(buf,"%02d:%02d:%02d",hor,min,sec);
640
641 return(buf);
642
643 }
644
645
646 void obtdate(char *dirname, char *name, char *data)
647 {
648
649 FILE *fp_in;
650 char wdir[MAXLEN];
651
652 sprintf(wdir,"%s%s/sarg-date",dirname,name);
653 if ((fp_in = fopen(wdir, "r")) == 0) {
654 sprintf(wdir,"%s%s/date",dirname,name);
655 if ((fp_in = fopen(wdir, "r")) == 0) {
656 data[0]='\0';
657 return;
658 }
659 }
660
661 fgets(data,80,fp_in);
662 fclose(fp_in);
663 data[strlen(data)-1]='\0';
664
665 return;
666
667 }
668
669
670 void obtuser(char *dirname, char *name, char *tuser)
671 {
672
673 FILE *fp_in;
674 char wdir[MAXLEN];
675
676 sprintf(wdir,"%s%s/sarg-users",dirname,name);
677 if((fp_in=fopen(wdir,"r"))==NULL) {
678 sprintf(wdir,"%s%s/users",dirname,name);
679 if((fp_in=fopen(wdir,"r"))==NULL) {
680 tuser[0]='\0';
681 return;
682 }
683 }
684
685 fgets(tuser,20,fp_in);
686 tuser[strlen(tuser)-1]='\0';
687 fclose(fp_in);
688
689 return;
690
691 }
692
693
694 void obttotal(char *dirname, char *name, char *tbytes, char *tuser, char *media)
695 {
696
697 FILE *fp_in;
698 char wdir[MAXLEN];
699 long long int med=0;
700 long long int wtuser=0;
701
702 sprintf(wdir,"%s%s/sarg-general",dirname,name);
703 if ((fp_in = fopen(wdir, "r")) == 0) {
704 sprintf(wdir,"%s%s/general",dirname,name);
705 if ((fp_in = fopen(wdir, "r")) == 0) {
706 tbytes=0;
707 return;
708 }
709 }
710
711 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
712 if (getword_multisep(warea,sizeof(warea),buf,' ')<0) {
713 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir);
714 exit(1);
715 }
716 if(strcmp(warea,"TOTAL") != 0)
717 continue;
718 if (getword_multisep(warea,sizeof(warea),buf,' ')<0) {
719 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir);
720 exit(1);
721 }
722 if (getword_multisep(warea,sizeof(warea),buf,' ')<0) {
723 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir);
724 exit(1);
725 }
726 twork=my_atoll(warea);
727 sprintf(tbytes,"%s",fixnum(twork,1));
728 }
729 fclose(fp_in);
730
731 if(tuser[0] == '\0') {
732 wtuser=0;
733 sprintf(media,"%s","0");
734 return;
735 }
736
737 wtuser=my_atoll(tuser);
738 med=my_atoll(warea) / wtuser;
739 sprintf(media,"%s",fixnum(med,1));
740
741 return;
742
743 }
744
745
746 //void gperiod(char *dirname, const char *period)
747 void gperiod()
748 {
749
750 FILE *fp_ou;
751
752 char wdirname[MAXLEN];
753
754 strcpy(wdirname,dirname);
755 strcat(wdirname,"/");
756 strcat(wdirname,"sarg-period");
757
758 if((fp_ou=fopen(wdirname,"w"))==NULL){
759 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
760 exit(1);
761 }
762
763 fputs(period,fp_ou);
764 fclose(fp_ou);
765
766 if(debug)
767 debuga((char *)text[50]);
768
769 return;
770
771 }
772
773 void vrfydir(char *dir, char *per1, char *addr, char *site, char *us, char *form)
774 {
775 FILE *img_in, *img_ou;
776 int num=1, count=0;
777 int c;
778 char wdir[MAXLEN];
779 char per2[MAXLEN];
780 char dirname2[MAXLEN];
781 char images[512];
782 DIR *dirp;
783 struct dirent *direntp;
784 int cstatus;
785
786 if(strcmp(IndexTree,"date") == 0) {
787 bzero(y1,5);
788 bzero(y2,5);
789 bzero(d1,3);
790 bzero(d2,3);
791 bzero(m1,4);
792 bzero(m2,4);
793 if(strncmp(df,"u",1) == 0) {
794 strncpy(y1,period,4);
795 strncpy(y2,period+10,4);
796 strncpy(m1,period+4,3);
797 strncpy(m2,period+14,3);
798 strncpy(d1,period+7,2);
799 strncpy(d2,period+17,2);
800 } else if(strncmp(df,"e",1) == 0) {
801 strncpy(d1,period+0,2);
802 strncpy(d2,period+10,2);
803 strncpy(m1,period+2,3);
804 strncpy(m2,period+12,3);
805 strncpy(y1,period+5,4);
806 strncpy(y2,period+15,4);
807 }
808 conv_month(m1);
809 conv_month(m2);
810
811 sprintf(wdir,"%s%s",outdir,y1);
812 if(strcmp(y1,y2) != 0) {
813 strncat(wdir,"-",1);
814 strncat(wdir,y2,strlen(y2));
815 }
816 if(access(wdir, R_OK) != 0)
817 my_mkdir(wdir);
818
819 strncat(wdir,"/",1);
820 strncat(wdir,m1,strlen(m1));
821 if(strcmp(m1,m2) != 0) {
822 strncat(wdir,"-",1);
823 strncat(wdir,m2,strlen(m2));
824 }
825 if(access(wdir, R_OK) != 0)
826 my_mkdir(wdir);
827
828 strncat(wdir,"/",1);
829 strncat(wdir,d1,strlen(d1));
830 if(strcmp(d1,d2) != 0) {
831 strncat(wdir,"-",1);
832 strncat(wdir,d2,strlen(d2));
833 }
834 } else
835 sprintf(wdir,"%s",dir);
836
837 if(strlen(us) > 0) {
838 strcat(wdir,"-");
839 strcat(wdir,us);
840 }
841 if(strlen(addr) > 0) {
842 strcat(wdir,"-");
843 strcat(wdir,addr);
844 }
845 if(strlen(site) > 0) {
846 strcat(wdir,"-");
847 strcat(wdir,site);
848 }
849
850 if(strcmp(dirname,wdir) != 0)
851 strcpy(dirname,wdir);
852
853 if(strcmp(IndexTree,"date") != 0) {
854 strcpy(dirname2,dirname);
855 if(strcmp(OverwriteReport,"no") == 0) {
856 while(num) {
857 if(access(wdir,R_OK) == 0) {
858 sprintf(wdir,"%s.%d",dirname,num);
859 sprintf(per2,"%s.%d",per1,num);
860 num++;
861 count++;
862 } else
863 break;
864 }
865
866 if(count > 0) {
867 if(debug)
868 fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname2,text[52],wdir);
869 rename(dirname2,wdir);
870 }
871 } else {
872 if(access(dir,R_OK) == 0) {
873 sprintf(csort,"rm -r %s",dir);
874 system(csort);
875 }
876 }
877 my_mkdir(dirname);
878 } else {
879 strcpy(dirname2,wdir);
880 if(strcmp(OverwriteReport,"no") == 0) {
881 while(num) {
882 if(access(wdir,R_OK) == 0) {
883 sprintf(wdir,"%s.%d",dirname2,num);
884 sprintf(per2,"%s.%d",per1,num);
885 num++;
886 count++;
887 } else
888 break;
889 }
890
891 if(count > 0) {
892 if(debug)
893 fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname2,text[52],wdir);
894 rename(dirname2,wdir);
895 strcpy(dirname2,wdir);
896 }
897 } else {
898 if(access(wdir,R_OK) == 0) {
899 sprintf(csort,"rm -r %s",wdir);
900 system(csort);
901 }
902 }
903
904 if(access(wdir, R_OK) != 0)
905 my_mkdir(wdir);
906 }
907
908 strcpy(dirname2,wdir);
909 sprintf(images,"%simages",outdir);
910 mkdir(images,0755);
911
912 sprintf(wdir,"date \"+%%a %%b %%d %%H:%%M:%%S %%Z %%Y\" >%s/sarg-date",dirname);
913 cstatus=system(wdir);
914 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
915 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
916 fprintf(stderr, "SARG: command: %s\n",wdir);
917 exit(1);
918 }
919
920 sprintf(per2,"%s/images",SYSCONFDIR);
921
922 dirp = opendir(per2);
923 if(dirp==NULL) {
924 fprintf(stderr, "SARG: (util) %s %s: %s\n","Can't open directory", per2,strerror(errno));
925 return;
926 }
927 while ((direntp = readdir( dirp )) != NULL ){
928 if(strncmp(direntp->d_name,".",1) == 0)
929 continue;
930 sprintf(val10,"%s/%s",per2,direntp->d_name);
931 sprintf(val11,"%s/%s",images,direntp->d_name);
932 img_in = fopen(val10, "rb");
933 if(img_in!=NULL) {
934 img_ou = fopen(val11, "wb");
935 if(img_ou!=NULL) {
936 while (c!=EOF) {
937 c = fgetc(img_in);
938 if(c==EOF) break;
939 fputc(c,img_ou);
940 }
941 c=0;
942 fclose(img_ou);
943 } else
944 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open/create file", val11, strerror(errno));
945 } else
946 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open file", val10, strerror(errno));
947
948 fclose(img_in);
949 }
950 (void) rewinddir(dirp);
951 (void) closedir(dirp);
952
953 return;
954
955
956 }
957
958
959 void strip_latin(char *line)
960 {
961 char buf[255];
962 char warea[255];
963
964 while(strstr(line,"&") != 0){
965 if (getword_multisep(warea,sizeof(warea),line,'&')<0) {
966 printf("SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities.\n");
967 exit(1);
968 }
969 strncat(warea,line,1);
970 if (getword_multisep(buf,sizeof(buf),line,';')<0) {
971 printf("SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities.\n");
972 exit(1);
973 }
974 strcat(warea,line);
975 strcpy(line,warea);
976 }
977
978 return;
979
980 }
981
982 void zdate(char *ftime, char *DateFormat)
983 {
984
985 time_t t;
986 struct tm *local;
987
988 t = time(NULL);
989 local = localtime(&t);
990 if(strcmp(DateFormat,"u") == 0)
991 strftime(ftime, 127, "%b/%d/%Y %H:%M", local);
992 if(strcmp(DateFormat,"e") == 0)
993 strftime(ftime, 127, "%d/%b/%Y-%H:%M", local);
994 if(strcmp(DateFormat,"w") == 0)
995 strftime(ftime, 127, "%V-%H-%M", local);
996
997 return;
998 }
999
1000
1001 char *fixtime(long int elap)
1002 {
1003
1004 int num = elap / 1000;
1005 int hor = 0;
1006 int min = 0;
1007 int sec = 0;
1008 static char buf[12];
1009
1010 if(strcmp(datetimeby,"bytes") == 0) {
1011 sprintf(buf,"%s",fixnum(elap,1));
1012 return buf;
1013 }
1014
1015 buf[0]='\0';
1016
1017 if(num<1) {
1018 sprintf(buf,"00:00:%02ld",elap);
1019 return buf;
1020 }
1021
1022 hor=num / 3600;
1023 min=(num % 3600) / 60;
1024 sec=num % 60;
1025
1026 sprintf(buf,"%01d:%02d:%02d",hor,min,sec);
1027
1028 if(strcmp(buf,"0:00:00") == 0)
1029 strcpy(buf,"0");
1030
1031 return buf;
1032
1033 }
1034
1035
1036 void date_from(char *date, char *dfrom, char *duntil)
1037 {
1038
1039 char diaf[10];
1040 char mesf[10];
1041 char anof[10];
1042 char diau[10];
1043 char mesu[10];
1044 char anou[10];
1045 static char wdate[50];
1046
1047
1048 strcpy(wdate,date);
1049 if(strstr(wdate,"-") == 0) {
1050 strcat(wdate,"-");
1051 strcat(wdate,date);
1052 strcpy(date,wdate);
1053 }
1054
1055 if (getword(diaf,sizeof(diaf),wdate,'/')<0 || getword(mesf,sizeof(mesf),wdate,'/')<0 || getword(anof,sizeof(anof),wdate,'-')<0 ||
1056 getword(diau,sizeof(diau),wdate,'/')<0 || getword(mesu,sizeof(mesu),wdate,'/')<0 || getword(anou,sizeof(anou),wdate,0)<0) {
1057 printf("SARG: Maybe you have a broken record or garbage in a date.\n");
1058 exit(1);
1059 }
1060
1061 sprintf(dfrom,"%s%s%s",anof,mesf,diaf);
1062 sprintf(duntil,"%s%s%s",anou,mesu,diau);
1063 return;
1064 }
1065
1066
1067 char *strlow(char *string)
1068 {
1069 char *s;
1070
1071 if (string)
1072 {
1073 for (s = string; *s; ++s)
1074 *s = tolower(*s);
1075 }
1076
1077 return string;
1078 }
1079
1080
1081
1082
1083 char *strup(char *string)
1084 {
1085 char *s;
1086
1087 if (string)
1088 {
1089 for (s = string; *s; ++s)
1090 *s = toupper(*s);
1091 }
1092
1093 return string;
1094 }
1095
1096
1097 char *subs(char *str, char *from, char *to)
1098 {
1099 char *tmp;
1100 char *ret;
1101 unsigned int ss, st;
1102
1103 if(strstr(str,from) == 0)
1104 return (char *) str;
1105
1106 ss = strlen(str); st = strlen(to) + 10;
1107
1108 if((ret=(char *) malloc(ss + st))==NULL)
1109 {
1110 fprintf(stderr, "SARG: %s (%d):\n",text[59],ss+st);
1111 exit(1);
1112 }
1113
1114 bzero(ret,ss+st);
1115
1116 tmp = strstr(str, from);
1117 if ( tmp == (char *) NULL )
1118 return (char *) NULL;
1119 strncpy(ret, str, ss - strlen(tmp));
1120 strcat(ret, to);
1121 strcat(ret, (tmp+strlen(from)));
1122 return (char *) ret;
1123 }
1124
1125
1126 void removetmp(char *outdir)
1127 {
1128
1129 FILE *fp_in;
1130 char warea[256];
1131
1132 if(strcmp(RemoveTempFiles,"yes") != 0) {
1133 return;
1134 } else {
1135 if(debug) {
1136 sprintf(msg,"%s: sarg-general, sarg-period",text[82]);
1137 debuga(msg);
1138 }
1139 sprintf(warea,"%s/sarg-general",outdir);
1140 if((fp_in=fopen(warea,"r"))==NULL){
1141 fprintf(stderr, "===SARG: (removetmp) %s: %s\n",text[45],warea);
1142 exit(1);
1143 }
1144 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
1145 if(strncmp(buf,"TOTAL",5) == 0)
1146 break;
1147 }
1148 fclose(fp_in);
1149 if((fp_in=fopen(warea,"w"))==NULL){
1150 fprintf(stderr, "SARG: (removetmp) %s: %s\n",text[45],warea);
1151 exit(1);
1152 }
1153 fputs(buf,fp_in);
1154 fclose(fp_in);
1155 sprintf(warea,"%s/sarg-period",outdir);
1156 unlink(warea);
1157 }
1158
1159 return;
1160 }
1161
1162 void load_excludecodes()
1163 {
1164
1165 FILE *fp_in;
1166 char data[80];
1167
1168 if((fp_in=fopen(ExcludeCodes,"r"))==NULL) {
1169 fprintf(stderr, "SARG: (util) Cannot open file: %s (exclude_codes)\n",ExcludeCodes);
1170 exit(1);
1171 }
1172
1173 while(fgets(data,80,fp_in)!=NULL) {
1174 data[strlen(data)-1]='\0';
1175 strcat(excludecode,data);
1176 strcat(excludecode,";");
1177 excode++;
1178 }
1179
1180 fclose(fp_in);
1181 return;
1182
1183 }
1184
1185 int vercode(char *code)
1186 {
1187 char warea[1024];
1188 char cod[80];
1189 int z;
1190
1191 strcpy(warea,excludecode);
1192 for(z=0; z<=excode-1; z++) {
1193 if (getword_multisep(cod,sizeof(cod),warea,';')<0) {
1194 printf("SARG: Maybe you have a broken record or garbage in a version code.\n");
1195 exit(1);
1196 }
1197 if(strcmp(code,cod) == 0)
1198 return 1;
1199 }
1200 return 0;
1201 }
1202
1203 void fixnone(char *str)
1204 {
1205 if(strstr(str,"\n") != 0)
1206 str[strlen(str)-1]='\0';
1207 if(strcmp(str,"none") == 0)
1208 str[0]='\0';
1209
1210 return;
1211 }
1212
1213 #ifdef LEGACY_TESTVALIDUSERCHAR
1214 int testvaliduserchar(char *user)
1215 {
1216
1217 int x=0;
1218 int y=0;
1219
1220 for (y=0; y<strlen(UserInvalidChar); y++) {
1221 for (x=0; x<strlen(user); x++) {
1222 if(user[x] == UserInvalidChar[y])
1223 return 1;
1224 }
1225 }
1226 return 0;
1227 }
1228 #else
1229 int testvaliduserchar(char *user)
1230 {
1231
1232 char * p_UserInvalidChar = UserInvalidChar ;
1233 char * p_user ;
1234
1235 while( *p_UserInvalidChar ) {
1236 p_user = user ;
1237 while ( *p_user ) {
1238 if( *p_UserInvalidChar == *p_user )
1239 return 1;
1240 p_user++ ;
1241 }
1242 p_UserInvalidChar++ ;
1243 }
1244 return 0;
1245 }
1246 #endif
1247
1248 int compar( const void *a, const void *b )
1249 { if( *(int *)a > *(int *)b ) return 1;
1250 if( *(int *)a < *(int *)b ) return -1;
1251 return 0;
1252 }
1253
1254 int getnumlist( char *buf, numlist *list, const int len, const int maxvalue )
1255 { int i, j, d, flag, r1, r2;
1256 char *pbuf, **bp, *strbufs[ 24 ];
1257
1258 bp = strbufs;
1259 strtok( buf, " \t" );
1260 for( *bp = strtok( NULL, "," ), list->len = 0; *bp; *bp = strtok( NULL, "," ) )
1261 { if( ++bp >= &strbufs[ 24 ] )
1262 break;
1263 list->len++;
1264 }
1265 if( ! list->len )
1266 return -1;
1267 d = 0;
1268 for( i = 0; i < list->len; i++ )
1269 { if( strstr( strbufs[ i ], "-" ) != 0 )
1270 { pbuf = strbufs[ i ];
1271 strtok( pbuf, "-" );
1272 pbuf = strtok( NULL, "\0" );
1273 r1 = atoi( strbufs[ i ] );
1274 if( ( r2 = atoi( pbuf ) ) >= maxvalue || r1 >= r2 )
1275 return -1;
1276 if( i + d + ( r2 - r1 ) + 1 <= len )
1277 { for( j = r1; j <= r2; j++ )
1278 list->list[ i + d++ ] = j;
1279 d--;
1280 }
1281 }
1282 else
1283 if( ( list->list[ i + d ] = atoi( strbufs[ i ] ) ) >= maxvalue )
1284 return 1;
1285 }
1286 list->len += d;
1287 qsort( list->list, list->len, sizeof( int ), compar );
1288 do
1289 { flag = 0;
1290 for( i = 0; i < list->len - 1; i++ )
1291 if( list->list[ i ] == list->list[ i + 1 ] )
1292 { for( j = i + 1; j < list->len; j++ )
1293 list->list[ j - 1 ] = list->list[ j ];
1294 list->len--;
1295 flag = 1;
1296 break;
1297 }
1298 } while( flag );
1299 return 0;
1300 }
1301
1302
1303 void show_info(FILE *fp_ou)
1304 {
1305 if(strcmp(ShowSargInfo,"yes") != 0) return;
1306 zdate(ftime, DateFormat);
1307 fprintf(fp_ou,"<center><table><tr><td><br><br></td><td class=\"info\">%s <a href='%s'><font class=\"info\">%s-%s</font></a> %s %s</td></tr></table></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
1308 }
1309
1310 void show_sarg(FILE *fp_ou, char *ind)
1311 {
1312 if(strcmp(ShowSargLogo,"yes") == 0) fprintf(fp_ou,"<center><table cellpadding=0 cellspacing=0>\n<tr><th class=\"logo\"><a href=\"http://sarg.sourceforge.net\"><img src=\"%s/images/sarg.png\" border=\"0\" align=\"absmiddle\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\"></a>&nbsp;<font class=\"logo\">Squid Analysis Report Generator</font></th></tr>\n<tr><th class=\"title\">&nbsp</th></tr>\n<table>\n",ind);
1313 }
1314
1315 int get_size(char *path, char *file)
1316 {
1317 FILE *fp;
1318 char response[255];
1319
1320 sprintf(cmd,"du -skh %s%s",path,file);
1321 fp = popen(cmd, "r");
1322 fgets(response, 255, fp);
1323 if (getword_multisep(val5,sizeof(val5),response,'\t')<0) {
1324 printf("SARG: Maybe you have a broken record or garbage in your %s command.\n",cmd);
1325 exit(1);
1326 }
1327 pclose(fp);
1328
1329 return (val5);
1330 }
1331
1332
1333 void write_html_header(FILE *fp_ou, char * ind)
1334 {
1335 fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n</head>\n",CharSet);
1336 css(fp_ou);
1337 fprintf(fp_ou,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage);
1338 if(strlen(LogoImage) > 0) fprintf(fp_ou, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText);
1339 show_sarg(fp_ou, ind);
1340 fprintf(fp_ou,"<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"title\">%s</th></tr>\n</table></center>\n<center><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",Title);
1341 }
1342
1343 void baddata()
1344 {
1345 printf("SARG: ------------------------------------------------------------------------------\n");
1346 printf("SARG: MALICIUS CODE DETECTED.\n");
1347 printf("SARG: I think someone is trying to execute arbitrary code in your system using sarg.\n");
1348 printf("SARG: please review your access.log and/or your useragent.log file.\n");
1349 printf("SARG: process stoped. No actions taken.\n");
1350 printf("SARG: ------------------------------------------------------------------------------\n");
1351
1352 system("rm -rf /tmp/sarg");
1353 sprintf(tmp4,"rm -rf %s",dirname);
1354 system(tmp4);
1355 system("rm -rf /tmp/sarg");
1356
1357 exit(1);
1358 }
1359
1360
1361 char url_module(char *url, char *w2)
1362 {
1363 int x, y;
1364 char w[255];
1365
1366 bzero(w, 255);
1367 bzero(w2, 255);
1368 y=0;
1369 for(x=strlen(url)-1; x>=0; x--) {
1370 if(url[x] == '/' || y>255) break;
1371 w[y]=url[x];
1372 y++;
1373 }
1374
1375 y=0;
1376 for(x=strlen(w)-1; x>=0; x--) {
1377 w2[y]=w[x];
1378 y++;
1379 }
1380
1381 return;
1382 }
1383
1384
1385 void write_html_trailer(FILE *fp_ou)
1386 {
1387 fputs("</table></center>\n",fp_ou);
1388 zdate(ftime, DateFormat);
1389 show_info(fp_ou);
1390 fputs("</body>\n</html>\n",fp_ou);
1391 }
1392
1393 void version()
1394 {
1395 printf("SARG Version: %s\n",VERSION);
1396 exit(0);
1397 }