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