]> git.ipfire.org Git - thirdparty/sarg.git/blob - util.c
Protect the creation of the index against invalid directories.
[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 #include "include/defs.h"
31
32 #if defined(HAVE_BACKTRACE)
33 #define USE_GETWORD_BACKTRACE 1
34 #else
35 #define USE_GETWORD_BACKTRACE 0
36 #endif
37
38 static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
39
40 //! The list of the HTTP codes to exclude from the report.
41 static char *excludecode=NULL;
42
43 /*void fgetword(char *word, char *line, int stop)
44 {
45 //VARIANT N1
46 int x;
47
48 for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x];
49 word[x] = '\0';
50
51 //VARIANT N2
52 char *tchar;
53 int difflen;
54
55 tchar = strchr(line, stop);
56 if (tchar == NULL) strcpy(word, line);
57 else
58 {
59 difflen = tchar - line;
60 strncpy(word, line, difflen);
61 word[difflen] = '\0';
62 }
63 }*/
64
65 #if USE_GETWORD_BACKTRACE
66 static void getword_backtrace(void)
67 {
68 void *buffer[5];
69 int i, n;
70 char **calls;
71
72 n=backtrace(buffer,sizeof(buffer)/sizeof(buffer[0]));
73 if (n<=0) return;
74 calls=backtrace_symbols(buffer,n);
75 if (calls) {
76 fprintf(stderr,"SARG: getword backtrace:\n");
77 for (i=0 ; i<n ; i++) {
78 fprintf(stderr,"SARG: %d:%s\n",i+1,calls[i]);
79 }
80 free(calls);
81 }
82 }
83 #endif //USE_GETWORD_BACKTRACE
84
85 void getword_start(struct getwordstruct *gwarea, const char *line)
86 {
87 gwarea->beginning=line;
88 gwarea->current=line;
89 }
90
91 void getword_restart(struct getwordstruct *gwarea)
92 {
93 gwarea->current=gwarea->beginning;
94 }
95
96 int getword(char *word, int limit, struct getwordstruct *gwarea, int stop)
97 {
98 int x;
99
100 for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) {
101 if(x>=limit) {
102 printf("SARG: getword loop detected after %d bytes.\n",x);
103 printf("SARG: Line=\"%s\"\n",gwarea->beginning);
104 printf("SARG: Record=\"%s\"\n",gwarea->current);
105 printf("SARG: searching for \'x%x\'\n",stop);
106 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
107 word[(limit>0) ? limit-1 : 0]='\0';
108 #if USE_GETWORD_BACKTRACE
109 getword_backtrace();
110 #endif
111 return(-1);
112 }
113 word[x] = gwarea->current[x];
114 }
115
116 word[x] = '\0';
117 if (gwarea->current[x]) ++x;
118 gwarea->current+=x;
119 return(0);
120 }
121
122 int getword_multisep(char *word, int limit, struct getwordstruct *gwarea, int stop)
123 {
124 int x;
125
126 for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) {
127 if(x>=limit) {
128 printf("SARG: getword_multisep loop detected.\n");
129 printf("SARG: Line=\"%s\"\n",gwarea->beginning);
130 printf("SARG: Record=\"%s\"\n",gwarea->current);
131 printf("SARG: searching for \'x%x\'\n",stop);
132 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
133 if (limit>0) word[limit-1]='\0';
134 #if USE_GETWORD_BACKTRACE
135 getword_backtrace();
136 #endif
137 //exit(1);
138 return(-1);
139 }
140 word[x] = gwarea->current[x];
141 }
142
143 word[x] = '\0';
144 while (gwarea->current[x] && gwarea->current[x]==stop) ++x;
145 gwarea->current+=x;
146 return(0);
147 }
148
149 int getword_skip(int limit, struct getwordstruct *gwarea, int stop)
150 {
151 int x;
152
153 for(x=0;(gwarea->current[x] && (gwarea->current[x] != stop ));x++) {
154 if(x>=limit) {
155 printf("SARG: getword_skip loop detected after %d bytes.\n",x);
156 printf("SARG: Line=\"%s\"\n",gwarea->beginning);
157 printf("SARG: Record=\"%s\"\n",gwarea->current);
158 printf("SARG: searching for \'x%x\'\n",stop);
159 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
160 #if USE_GETWORD_BACKTRACE
161 getword_backtrace();
162 #endif
163 return(-1);
164 }
165 }
166
167 if (gwarea->current[x]) ++x;
168 gwarea->current+=x;
169 return(0);
170 }
171
172 int getword_atoll(long long int *number, struct getwordstruct *gwarea, int stop)
173 {
174 int x;
175 int sign=+1;
176
177 if (gwarea->current[0] == '-') {
178 gwarea->current++;
179 sign=-1;
180 } else if (gwarea->current[0] == '+') {
181 gwarea->current++;
182 }
183 *number=0LL;
184 for(x=0;isdigit(gwarea->current[x]);x++) {
185 *number=(*number * 10) + gwarea->current[x]-'0';
186 }
187 if(gwarea->current[x] && gwarea->current[x]!=stop) {
188 printf("SARG: getword_atoll loop detected after %d bytes.\n",x);
189 printf("SARG: Line=\"%s\"\n",gwarea->beginning);
190 printf("SARG: Record=\"%s\"\n",gwarea->current);
191 printf("SARG: searching for \'x%x\'\n",stop);
192 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
193 #if USE_GETWORD_BACKTRACE
194 getword_backtrace();
195 #endif
196 return(-1);
197 }
198 *number*=sign;
199
200 if (gwarea->current[x]) ++x;
201 gwarea->current+=x;
202 return(0);
203 }
204
205
206 #define MAXLLL 30 //!< Maximum number of digits in long long (a guess).
207 long long int my_atoll (const char *nptr)
208 {
209 long long int returnval=0LL;
210 int max_digits = MAXLLL ;
211
212 // Soak up all the white space
213 while (isspace( *nptr )) {
214 nptr++;
215 }
216
217 //For each character left to right
218 //change the character to a single digit
219 //multiply what we had before by 10 and add the new digit
220
221 while (--max_digits && isdigit( *nptr ))
222 {
223 returnval = ( returnval * 10 ) + ( *nptr++ - '0' ) ;
224 }
225
226 return returnval;
227 }
228
229 int is_absolute(const char *path)
230 {
231 if (*path=='/') return(1);
232 #ifdef WINDOWS
233 if (isalpha(path[0]) && path[1]==':') return(1);
234 #endif
235 return(0);
236 }
237
238 void my_mkdir(const char *name)
239 {
240 char w0[MAXLEN];
241 int i;
242 int chars;
243
244 if(!is_absolute(name)) {
245 fprintf(stderr,"SARG: Invalid path (%s). Please, use absolute paths only.\n",name);
246 fprintf(stderr,"SARG: process aborted.\n");
247 exit(1);
248 }
249
250 chars=0;
251 for (i=0 ; name[i] ; i++) {
252 if (i>=sizeof(w0)) {
253 fprintf(stderr,"SARG: directory name too long %s\n",name);
254 exit(1);
255 }
256 if (chars>0 && name[i] == '/') {
257 w0[i] = '\0';
258 if(access(w0, R_OK) != 0) {
259 if(mkdir(w0,0755)) {
260 fprintf(stderr,"SARG: mkdir %s %s\n",w0,strerror(errno));
261 fprintf(stderr,"SARG: process aborted.\n");
262 exit(1);
263 }
264 }
265 }
266 if (name[i] != '/') chars++;
267 w0[i] = name[i];
268 }
269
270 if(access(name, R_OK) != 0) {
271 if(mkdir(name,0755)) {
272 fprintf(stderr,"SARG: mkdir %s %s\n",name,strerror(errno));
273 fprintf(stderr,"SARG: process aborted.\n");
274 exit(1);
275 }
276 }
277 }
278
279
280 void my_lltoa(unsigned long long int n, char s[], int len)
281 {
282 int i;
283 int slen = 0;
284 int j;
285 char c;
286
287 do {
288 s[slen++] = (n % 10) + '0';
289 } while ((n /= 10) > 0);
290 s[slen] = '\0';
291
292 for (i = 0, j = slen-1; i<j; i++, j--) {
293 c = s[i];
294 s[i] = s[j];
295 s[j] = c;
296 }
297
298 if(len>slen) {
299 i=len-slen;
300 for(j=slen; j>=0; j--)
301 s[j+i]=s[j];
302 for(j=0 ; j<i ; j++)
303 s[j]='0';
304 }
305 }
306
307
308 void builddia(char *dia, const char *mes, const char *ano, const char *df, char *wdata)
309 {
310 char ndia[11];
311 int nmes;
312
313 if(strlen(dia) < 1) return;
314
315 for(nmes=0; nmes<12; nmes++) {
316 if(strcmp(mtab1[nmes],mes) == 0) {
317 break;
318 }
319 }
320 nmes++;
321
322 snprintf(wdata,9,"%s%02d%s",ano,nmes,dia);
323
324 if(df[0]!='u')
325 snprintf(ndia,sizeof(ndia),"%s/%02d/%s",dia,nmes,ano);
326 else
327 snprintf(ndia,sizeof(ndia),"%02d/%s/%s",nmes,dia,ano);
328
329 strcpy(dia,ndia);
330
331 }
332
333
334 void buildymd(const char *dia, const char *mes, const char *ano, char *wdata)
335 {
336 int nmes;
337
338 for(nmes=0; nmes<12; nmes++) {
339 if(strcmp(mtab1[nmes],mes) == 0)
340 break;
341 }
342
343 sprintf(wdata,"%s%02d%s",ano,nmes+1,dia);
344
345 }
346
347
348 void conv_month(char *month)
349 {
350 int x;
351
352 for(x=0; x<12 && strcmp(mtab1[x],month)!=0; x++);
353 sprintf(month,"%02d",x+1);
354 }
355
356
357 void conv_month_name(char *month)
358 {
359 int x;
360
361 x=atoi(month);
362 if (x>=1 && x<=12)
363 strcpy(month,mtab1[x-1]);
364 }
365
366
367 void name_month(char *month,int month_len)
368 {
369 int x, z=atoi(month)-1;
370 char m[255];
371 char w[20];
372 struct getwordstruct gwarea;
373
374 strcpy(m,text[133]);
375 getword_start(&gwarea,m);
376
377 for(x=0; x<z; x++)
378 if (getword_multisep(w,sizeof(w),&gwarea,',')<0) {
379 printf("SARG: Maybe you have a broken record or garbage in the names of the months.\n");
380 exit(1);
381 }
382 if (getword_multisep(month,month_len,&gwarea,',')<0) {
383 printf("SARG: Maybe you have a broken record or garbage in the name of the months.\n");
384 exit(1);
385 }
386 }
387
388
389 void fixper(char *tbuf, char *period, const char *duntil)
390 {
391 char warea[50];
392 char dia[5], mes[5], ano[5];
393 int x;
394
395 strncpy(dia,duntil+6,2);
396 dia[2]='\0';
397 strncpy(mes,duntil+4,2);
398 mes[2]='\0';
399 strncpy(ano,duntil,4);
400 ano[4]='\0';
401
402 x=atoi(mes);
403 if (x>=1 && x<=12)
404 strcpy(mes,mtab1[x-1]);
405
406 if(strcmp(df,"e") == 0)
407 sprintf(warea,"%s%s%s",dia,mes,ano);
408 else if(strcmp(df,"u") == 0)
409 sprintf(warea,"%s%s%s",ano,mes,dia);
410 else
411 warea[0]='\0';
412
413 strcat(period,warea);
414 }
415
416
417 void debuga(const char *msg,...)
418 {
419 va_list ap;
420
421 fputs("SARG: ",stderr);
422 va_start(ap,msg);
423 vfprintf(stderr,msg,ap);
424 va_end(ap);
425 fputc('\n',stderr);
426 }
427
428
429 void debugaz(const char *head, const char *msg)
430 {
431 fprintf(stderr, "SARG: (util) %s=%s\n",head, msg);
432
433 }
434
435
436 void fixip(char *ip)
437 {
438 int i;
439 char sep='_';
440 char search=0;
441 int nrepl=0;
442
443 for (i=0; ip[i]; i++) {
444 if (ip[i]=='.') {
445 search='.';
446 sep='_';
447 break;
448 }
449 if (ip[i]=='_') {
450 search='_';
451 sep='.';
452 break;
453 }
454 }
455 for (; ip[i]; i++) {
456 if (ip[i]==search) {
457 ip[i]=sep;
458 nrepl++;
459 if (nrepl>=3) break;
460 }
461 }
462 }
463
464
465 char *fixnum(long long int value, int n)
466 {
467 #define MAXLEN_FIXNUM 1024
468 char num[MAXLEN_FIXNUM]="";
469 char buf[MAXLEN_FIXNUM * 2];
470 char *pbuf;
471 static char ret[MAXLEN_FIXNUM * 2];
472 char *pret;
473 register int i, j, k;
474 int numlen;
475 static char abbrev[30];
476
477 my_lltoa(value, num, 0);
478
479 if(strcmp(DisplayedValues,"abbreviation") == 0) {
480 numlen = strlen(num);
481 if(numlen <= 3)
482 sprintf(abbrev,"%s",num);
483 if(numlen == 4 || numlen == 7 || numlen == 10 || numlen == 13) {
484 snprintf(abbrev,2,"%s",num);
485 strncat(abbrev,".",1);
486 strncat(abbrev,num+1,2);
487 if(!n) return(abbrev);
488 if(numlen == 4)
489 strncat(abbrev,"K",1);
490 else if(numlen == 7)
491 strncat(abbrev,"M",1);
492 else if(numlen == 10)
493 strncat(abbrev,"G",1);
494 else if(numlen == 13)
495 strncat(abbrev,"T",1);
496 }
497 if(numlen == 5 || numlen == 8 || numlen == 11 || numlen == 14) {
498 snprintf(abbrev,3,"%s",num);
499 strncat(abbrev,".",1);
500 strncat(abbrev,num+2,2);
501 if(!n) return(abbrev);
502 if(numlen == 5)
503 strncat(abbrev,"K",1);
504 else if(numlen == 8)
505 strncat(abbrev,"M",1);
506 else if(numlen == 11)
507 strncat(abbrev,"G",1);
508 else if(numlen == 14)
509 strncat(abbrev,"T",1);
510 }
511 if(numlen == 6 || numlen == 9 || numlen == 12 || numlen == 15) {
512 snprintf(abbrev,4,"%s",num);
513 strncat(abbrev,".",1);
514 strncat(abbrev,num+3,2);
515 if(!n) return(abbrev);
516 if(numlen == 6)
517 strncat(abbrev,"K",1);
518 else if(numlen == 9)
519 strncat(abbrev,"M",1);
520 else if(numlen == 12)
521 strncat(abbrev,"G",1);
522 else if(numlen == 15)
523 strncat(abbrev,"T",1);
524 }
525
526 return(abbrev);
527 }
528
529 bzero(buf, MAXLEN_FIXNUM*2);
530
531 pbuf = buf;
532 pret = ret;
533 k = 0;
534
535 for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) {
536 if ( k == 2 && i != 0 ) {
537 k = 0;
538 pbuf[j++] = num[i];
539 pbuf[j++] = (UseComma) ? ',' : '.';
540 continue;
541 }
542 pbuf[j] = num[i];
543 j++;
544 k++;
545 }
546
547 pret[0]='\0';
548
549 for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++)
550 pret[j] = pbuf[i];
551
552 pret[j] = '\0';
553
554 return pret;
555 }
556
557
558 char *fixnum2(long long int value, int n)
559 {
560 #define MAXLEN_FIXNUM2 1024
561 char num[MAXLEN_FIXNUM2];
562 char buf[MAXLEN_FIXNUM2 * 2];
563 char *pbuf;
564 static char ret[MAXLEN_FIXNUM2 * 2];
565 char *pret;
566 register int i, j, k;
567
568 my_lltoa(value, num, 0);
569 bzero(buf, MAXLEN_FIXNUM2*2);
570
571 pbuf = buf;
572 pret = ret;
573 k = 0;
574
575 for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) {
576 if ( k == 2 && i != 0 ) {
577 k = 0;
578 pbuf[j++] = num[i];
579 pbuf[j++] = (UseComma) ? ',' : '.';
580 continue;
581 }
582 pbuf[j] = num[i];
583 j++;
584 k++;
585 }
586
587 pret[0]='\0';
588
589 for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++)
590 pret[j] = pbuf[i];
591
592 pret[j] = '\0';
593
594 return pret;
595 }
596
597
598
599 void buildhref(char * href)
600 {
601 char whref[MAXLEN];
602
603 if(strcmp(href,"./") == 0){
604 href[0]='\0';
605 strcat(href,"<a href='");
606 return;
607 }
608
609 href[strlen(href)-1]='\0';
610 sprintf(whref,"%s",strrchr(href,'/'));
611
612 strcpy(href,"<a href='");
613 strcat(href,whref);
614 strcat(href,"/");
615
616 return;
617
618 }
619
620
621 char *buildtime(long long int elap)
622 {
623
624 int num = elap / 1000;
625 int hor = 0;
626 int min = 0;
627 int sec = 0;
628 static char buf[12];
629
630 buf[0]='\0';
631
632 hor=num / 3600;
633 min=(num % 3600) / 60;
634 sec=num % 60;
635 sprintf(buf,"%02d:%02d:%02d",hor,min,sec);
636
637 return(buf);
638
639 }
640
641
642 void obtdate(const char *dirname, const char *name, char *data)
643 {
644
645 FILE *fp_in;
646 char wdir[MAXLEN];
647
648 sprintf(wdir,"%s%s/sarg-date",dirname,name);
649 if ((fp_in = fopen(wdir, "rt")) == 0) {
650 sprintf(wdir,"%s%s/date",dirname,name);
651 if ((fp_in = fopen(wdir, "rt")) == 0) {
652 data[0]='\0';
653 return;
654 }
655 }
656
657 if (!fgets(data,80,fp_in)) {
658 fprintf(stderr,"Failed to read the date in %s\n",wdir);
659 exit(1);
660 }
661 fclose(fp_in);
662 fixendofline(data);
663
664 return;
665
666 }
667
668
669 void formatdate(char *date,int date_size,int year,int month,int day,int hour,int minute,int second,int dst)
670 {
671 struct tm ltm;
672 time_t unixtime;
673 struct tm *fulltm;
674
675 memset(&ltm,0,sizeof(ltm));
676 if (year>=1900) ltm.tm_year=year-1900;
677 if (month>=1 && month<=12) ltm.tm_mon=month-1;
678 if (day>=1 && day<=31) ltm.tm_mday=day;
679 if (hour>=0 && hour<24) ltm.tm_hour=hour;
680 if (minute>=0 && minute<60) ltm.tm_min=minute;
681 if (second>=0 && second<60) ltm.tm_sec=second;
682 ltm.tm_isdst=dst;
683 unixtime=mktime(&ltm); //fill the missing entries
684 fulltm=localtime(&unixtime);
685 //strftime(date,date_size,"%a %b %d %H:%M:%S %Z %Y",fulltm);
686 strftime(date,date_size,"%c",fulltm);
687 }
688
689
690 void obtuser(const char *dirname, const char *name, char *tuser)
691 {
692
693 FILE *fp_in;
694 char wdir[MAXLEN];
695
696 sprintf(wdir,"%s%s/sarg-users",dirname,name);
697 if((fp_in=fopen(wdir,"r"))==NULL) {
698 sprintf(wdir,"%s%s/users",dirname,name);
699 if((fp_in=fopen(wdir,"r"))==NULL) {
700 tuser[0]='\0';
701 return;
702 }
703 }
704
705 if (!fgets(tuser,20,fp_in)) {
706 fprintf(stderr,"Failed to read the user in %s\n",wdir);
707 exit(1);
708 }
709 fclose(fp_in);
710 fixendofline(tuser);
711
712 return;
713
714 }
715
716
717 void obttotal(const char *dirname, const char *name, char *tbytes, const char *tuser, char *media)
718 {
719 FILE *fp_in;
720 char buf[MAXLEN];
721 char wdir[MAXLEN];
722 char warea[MAXLEN];
723 long long int med=0;
724 long long int wtuser=0;
725 long long int twork=0;
726 struct getwordstruct gwarea;
727
728 sprintf(wdir,"%s%s/sarg-general",dirname,name);
729 if ((fp_in = fopen(wdir, "r")) == 0) {
730 sprintf(wdir,"%s%s/general",dirname,name);
731 if ((fp_in = fopen(wdir, "r")) == 0) {
732 tbytes[0]='\0';
733 media[0]='\0';
734 return;
735 }
736 }
737
738 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
739 getword_start(&gwarea,buf);
740 if (getword(warea,sizeof(warea),&gwarea,'\t')<0) {
741 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir);
742 exit(1);
743 }
744 if(strcmp(warea,"TOTAL") != 0)
745 continue;
746 if (getword_skip(MAXLEN,&gwarea,'\t')<0) {
747 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir);
748 exit(1);
749 }
750 if (getword_atoll(&twork,&gwarea,'\t')<0) {
751 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdir);
752 exit(1);
753 }
754 strcpy(tbytes,fixnum(twork,1));
755 break;
756 }
757 fclose(fp_in);
758
759 wtuser=my_atoll(tuser);
760 if(wtuser == 0) {
761 strcpy(media,"0");
762 return;
763 }
764
765 med=twork / wtuser;
766 strcpy(media,fixnum(med,1));
767
768 return;
769 }
770
771
772 void gperiod(const char *dirname, const char *period)
773 {
774
775 FILE *fp_ou;
776 char wdirname[MAXLEN];
777
778 strcpy(wdirname,dirname);
779 strcat(wdirname,"/sarg-period");
780
781 if((fp_ou=fopen(wdirname,"w"))==NULL){
782 fprintf(stderr, "SARG: (report) %s: %s\n",text[45],wdirname);
783 exit(1);
784 }
785
786 fputs(period,fp_ou);
787 fclose(fp_ou);
788
789 if(debug)
790 debuga("%s",(char *)text[50]);
791
792 return;
793
794 }
795
796 static void copy_images(void)
797 {
798 FILE *img_in, *img_ou;
799 char images[512];
800 char imgdir[MAXLEN];
801 char srcfile[MAXLEN];
802 char dstfile[MAXLEN];
803 DIR *dirp;
804 struct dirent *direntp;
805 char buffer[MAXLEN];
806 size_t nread;
807 struct stat info;
808
809 if (snprintf(images,sizeof(images),"%simages",outdir)>=sizeof(images)) {
810 fprintf(stderr,"SARG: Cannot copy images to target directory %simages\n",outdir);
811 exit(1);
812 }
813 if (access(images,R_OK)!=0) {
814 mkdir(images,0755);
815 }
816
817 strcpy(imgdir,IMAGEDIR);
818 dirp = opendir(imgdir);
819 if(dirp==NULL) {
820 fprintf(stderr, "SARG: (util) %s %s: %s\n","Can't open directory", imgdir,strerror(errno));
821 return;
822 }
823 while ((direntp = readdir( dirp )) != NULL ){
824 if(direntp->d_name[0]=='.')
825 continue;
826 sprintf(srcfile,"%s/%s",imgdir,direntp->d_name);
827 if (stat(srcfile,&info)) {
828 fprintf(stderr,"SARG: Cannot stat \"%s\" - %s\n",srcfile,strerror(errno));
829 continue;
830 }
831 if (S_ISREG(info.st_mode)) {
832 sprintf(dstfile,"%s/%s",images,direntp->d_name);
833 img_in = fopen(srcfile, "rb");
834 if(img_in!=NULL) {
835 img_ou = fopen(dstfile, "wb");
836 if(img_ou!=NULL) {
837 while ((nread = fread(buffer,1,sizeof(buffer),img_in))>0) {
838 fwrite(buffer,1,nread,img_ou);
839 }
840 fclose(img_ou);
841 } else
842 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open/create file", dstfile, strerror(errno));
843 fclose(img_in);
844 } else
845 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open file", srcfile, strerror(errno));
846 }
847 }
848 (void) closedir(dirp);
849
850 return;
851 }
852
853 void vrfydir(const char *per1, const char *addr, const char *site, const char *us, const char *form)
854 {
855 FILE *fp_ou;
856 int num=1, count=0;
857 char wdir[MAXLEN];
858 char per2[MAXLEN];
859 char dirname2[MAXLEN];
860 char y1[5], y2[5];
861 char d1[3], d2[3];
862 char m1[8], m2[8];
863 time_t curtime;
864 struct tm *loctm;
865 int cstatus;
866
867 if(strcmp(IndexTree,"date") == 0) {
868 bzero(y1,sizeof(y1));
869 bzero(y2,sizeof(y2));
870 bzero(d1,sizeof(d1));
871 bzero(d2,sizeof(d2));
872 bzero(m1,sizeof(m1));
873 bzero(m2,sizeof(m2));
874 if(strncmp(df,"u",1) == 0) {
875 strncpy(y1,period,4);
876 strncpy(y2,period+10,4);
877 strncpy(m1,period+4,3);
878 strncpy(m2,period+14,3);
879 strncpy(d1,period+7,2);
880 strncpy(d2,period+17,2);
881 } else if(strncmp(df,"e",1) == 0) {
882 strncpy(d1,period+0,2);
883 strncpy(d2,period+10,2);
884 strncpy(m1,period+2,3);
885 strncpy(m2,period+12,3);
886 strncpy(y1,period+5,4);
887 strncpy(y2,period+15,4);
888 }
889 conv_month(m1);
890 conv_month(m2);
891
892 sprintf(wdir,"%s%s",outdir,y1);
893 if(strcmp(y1,y2) != 0) {
894 strcat(wdir,"-");
895 strcat(wdir,y2);
896 }
897 if(access(wdir, R_OK) != 0)
898 my_mkdir(wdir);
899
900 strcat(wdir,"/");
901 strcat(wdir,m1);
902 if(strcmp(m1,m2) != 0) {
903 strcat(wdir,"-");
904 strcat(wdir,m2);
905 }
906 if(access(wdir, R_OK) != 0)
907 my_mkdir(wdir);
908
909 strcat(wdir,"/");
910 strcat(wdir,d1);
911 if(strcmp(d1,d2) != 0) {
912 strcat(wdir,"-");
913 strcat(wdir,d2);
914 }
915 } else {
916 sprintf(wdir, "%s%s", outdir, per1);
917 }
918
919 if(us[0] != '\0') {
920 strcat(wdir,"-");
921 strcat(wdir,us);
922 }
923 if(addr[0] != '\0') {
924 strcat(wdir,"-");
925 strcat(wdir,addr);
926 }
927 if(site[0] != '\0') {
928 strcat(wdir,"-");
929 strcat(wdir,site);
930 }
931
932 if(strcmp(dirname,wdir) != 0)
933 strcpy(dirname,wdir);
934
935 if(strcmp(IndexTree,"date") != 0) {
936 if(!OverwriteReport) {
937 while(num) {
938 if(access(wdir,R_OK) == 0) {
939 sprintf(wdir,"%s.%d",dirname,num);
940 sprintf(per2,"%s.%d",per1,num);
941 num++;
942 count++;
943 } else
944 break;
945 }
946
947 if(count > 0) {
948 if(debug)
949 fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname,text[52],wdir);
950 rename(dirname,wdir);
951 }
952 } else {
953 if(access(wdir,R_OK) == 0) {
954 sprintf(csort,"rm -r \"%s\"",wdir);
955 cstatus=system(csort);
956 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
957 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
958 fprintf(stderr, "SARG: command: %s\n",csort);
959 exit(1);
960 }
961 }
962 }
963 my_mkdir(dirname);
964 } else {
965 strcpy(dirname2,wdir);
966 if(!OverwriteReport) {
967 while(num) {
968 if(access(wdir,R_OK) == 0) {
969 sprintf(wdir,"%s.%d",dirname2,num);
970 sprintf(per2,"%s.%d",per1,num);
971 num++;
972 count++;
973 } else
974 break;
975 }
976
977 if(count > 0) {
978 if(debug)
979 fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname2,text[52],wdir);
980 rename(dirname2,wdir);
981 strcpy(dirname2,wdir);
982 }
983 } else {
984 if(access(wdir,R_OK) == 0) {
985 sprintf(csort,"rm -r \"%s\"",wdir);
986 cstatus=system(csort);
987 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
988 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
989 fprintf(stderr, "SARG: command: %s\n",csort);
990 exit(1);
991 }
992 }
993 }
994
995 if(access(wdir, R_OK) != 0)
996 my_mkdir(wdir);
997 }
998
999 strcpy(dirname2,wdir);
1000
1001 sprintf(wdir,"%s/sarg-date",dirname);
1002 if ((fp_ou = fopen(wdir, "wt")) == 0) {
1003 fprintf(stderr, "SARG: cannot open %s for writing\n",wdir);
1004 perror("SARG:");
1005 exit(1);
1006 }
1007 time(&curtime);
1008 //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime));
1009 loctm=localtime(&curtime);
1010 strftime(wdir,sizeof(wdir),"%Y-%m-%d %H:%M:%S",loctm);
1011 fprintf(fp_ou,"%s %d\n",wdir,loctm->tm_isdst);
1012 fclose(fp_ou);
1013
1014 copy_images();
1015 }
1016
1017 void strip_latin(char *line)
1018 {
1019 int i,j;
1020 int skip;
1021
1022 j=0;
1023 skip=0;
1024 for (i=0;line[i];i++){
1025 if (skip){
1026 if (line[i]==';') skip=0;
1027 } else {
1028 if (line[i]=='&')
1029 skip=1;
1030 else
1031 line[j++]=line[i];
1032 }
1033 }
1034 line[j]='\0';
1035 return;
1036
1037 }
1038
1039 void zdate(char *ftime,int ftimesize, const char *DateFormat)
1040 {
1041
1042 time_t t;
1043 struct tm *local;
1044
1045 t = time(NULL);
1046 local = localtime(&t);
1047 if(strcmp(DateFormat,"u") == 0)
1048 strftime(ftime, ftimesize, "%b/%d/%Y %H:%M", local);
1049 if(strcmp(DateFormat,"e") == 0)
1050 strftime(ftime, ftimesize, "%d/%b/%Y-%H:%M", local);
1051 if(strcmp(DateFormat,"w") == 0)
1052 strftime(ftime, ftimesize, "%V-%H-%M", local);
1053 return;
1054 }
1055
1056
1057 char *fixtime(long int elap)
1058 {
1059
1060 int num = elap / 1000;
1061 int hor = 0;
1062 int min = 0;
1063 int sec = 0;
1064 static char buf[12];
1065
1066 if(strcmp(datetimeby,"bytes") == 0) {
1067 strcpy(buf,fixnum(elap,1));
1068 return buf;
1069 }
1070
1071 if(num<1) {
1072 sprintf(buf,"00:00:%02ld",elap);
1073 return buf;
1074 }
1075
1076 hor=num / 3600;
1077 min=(num % 3600) / 60;
1078 sec=num % 60;
1079
1080 if(hor==0 && min==0 && sec==0)
1081 strcpy(buf,"0");
1082 else
1083 sprintf(buf,"%01d:%02d:%02d",hor,min,sec);
1084
1085 return buf;
1086
1087 }
1088
1089
1090 void date_from(char *date, char *dfrom, char *duntil)
1091 {
1092
1093 int diaf;
1094 int mesf;
1095 int anof;
1096 int diau;
1097 int mesu;
1098 int anou;
1099 char wdate[50];
1100
1101 strncpy(wdate,date,sizeof(wdate)-1);
1102 wdate[sizeof(wdate)-1]='\0';
1103 if(strchr(wdate,'-') == NULL) {
1104 if (strlen(wdate)*2+1>=sizeof(wdate)) {
1105 fprintf(stderr,"SARG: Invalid date range passed as argument.\n");
1106 exit(1);
1107 }
1108 strcat(wdate,"-");
1109 strcat(wdate,date);
1110 strcpy(date,wdate);
1111 }
1112
1113 if (sscanf(wdate,"%d/%d/%d-%d/%d/%d",&diaf,&mesf,&anof,&diau,&mesu,&anou)!=6) {
1114 fprintf(stderr,"SARG: The date range passed as argument is not formated as dd/mm/yyyy-dd/mm/yyyy.\n");
1115 exit(1);
1116 }
1117
1118 sprintf(dfrom,"%04d%02d%02d",anof,mesf,diaf);
1119 sprintf(duntil,"%04d%02d%02d",anou,mesu,diau);
1120 return;
1121 }
1122
1123
1124 char *strlow(char *string)
1125 {
1126 char *s;
1127
1128 if (string)
1129 {
1130 for (s = string; *s; ++s)
1131 *s = tolower(*s);
1132 }
1133
1134 return string;
1135 }
1136
1137
1138
1139
1140 char *strup(char *string)
1141 {
1142 char *s;
1143
1144 if (string)
1145 {
1146 for (s = string; *s; ++s)
1147 *s = toupper(*s);
1148 }
1149
1150 return string;
1151 }
1152
1153
1154 void subs(char *str, int size, char *from, char *to)
1155 {
1156 char *tmp;
1157 int i;
1158 int ss, st, sf;
1159 int len;
1160 int shift;
1161
1162 tmp = strstr(str, from);
1163 if(tmp == NULL)
1164 return;
1165
1166 ss = strlen(str);
1167 sf = strlen(from);
1168 st = strlen(to);
1169 shift=st-sf;
1170
1171 if (shift>0) {
1172 if (ss+shift>=size){
1173 fprintf(stderr,"SARG: Cannot replace %s by %s in %s and fit within %d bytes\n",from,to,str,size);
1174 exit(1);
1175 }
1176 for (i=strlen(tmp) ; i>=sf ; i--)
1177 tmp[i+shift]=tmp[i];
1178 } else if (shift<0) {
1179 len=strlen(tmp);
1180 for (i=sf ; i<=len ; i++)
1181 tmp[i+shift]=tmp[i];
1182 }
1183 memcpy(tmp, to, st);
1184 return;
1185 }
1186
1187
1188 void removetmp(const char *outdir)
1189 {
1190
1191 FILE *fp_in;
1192 char warea[256];
1193
1194 if(!RemoveTempFiles)
1195 return;
1196
1197 if(debug) {
1198 debuga("%s: sarg-general, sarg-period",text[82]);
1199 }
1200 if (snprintf(warea,sizeof(warea),"%s/sarg-general",outdir)>=sizeof(warea)) {
1201 fprintf(stderr, "SARG: (removetmp) directory too long to remove: %s/sarg-period\n",outdir);
1202 exit(1);
1203 }
1204 if((fp_in=fopen(warea,"r"))==NULL){
1205 fprintf(stderr, "===SARG: (removetmp) %s: %s\n",text[45],warea);
1206 exit(1);
1207 }
1208 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
1209 if(strncmp(buf,"TOTAL",5) == 0)
1210 break;
1211 }
1212 fclose(fp_in);
1213 if((fp_in=fopen(warea,"w"))==NULL){
1214 fprintf(stderr, "SARG: (removetmp) %s: %s\n",text[45],warea);
1215 exit(1);
1216 }
1217 fputs(buf,fp_in);
1218 fclose(fp_in);
1219 if (snprintf(warea,sizeof(warea),"%s/sarg-period",outdir)>=sizeof(warea)) {
1220 fprintf(stderr, "SARG: (removetmp) directory too long to remove: %s/sarg-period\n",outdir);
1221 exit(1);
1222 }
1223 unlink(warea);
1224
1225 return;
1226 }
1227
1228 void load_excludecodes(const char *ExcludeCodes)
1229 {
1230
1231 FILE *fp_in;
1232 char data[80];
1233 int i;
1234
1235 if(ExcludeCodes[0] == '\0')
1236 return;
1237
1238 if((excludecode=(char *) malloc(1024))==NULL) {
1239 fprintf(stderr, "SARG: %s (1024):\n",text[59]);
1240 exit(1);
1241 }
1242 bzero(excludecode,1024);
1243
1244 if((fp_in=fopen(ExcludeCodes,"r"))==NULL) {
1245 fprintf(stderr, "SARG: (util) Cannot open file: %s (exclude_codes)\n",ExcludeCodes);
1246 exit(1);
1247 }
1248
1249 while(fgets(data,sizeof(data),fp_in)!=NULL) {
1250 for (i=strlen(data)-1 ; i>=0 && (unsigned char)data[i]<=' ' ; i--) data[i]=0;
1251 if (i<0) continue;
1252 strcat(excludecode,data);
1253 strcat(excludecode,";");
1254 }
1255
1256 fclose(fp_in);
1257 return;
1258
1259 }
1260
1261 void free_excludecodes(void)
1262 {
1263 if (excludecode) {
1264 free(excludecode);
1265 excludecode=NULL;
1266 }
1267 }
1268
1269 int vercode(const char *code)
1270 {
1271 char *cod;
1272 int clen;
1273
1274 if (excludecode && excludecode[0]!=0) {
1275 clen=strlen(code);
1276 for (cod=excludecode ; cod ; cod=strchr(cod+1,';')) {
1277 if (strncmp(code,cod,clen)==0 && cod[clen]==';')
1278 return 1;
1279 }
1280 }
1281 return 0;
1282 }
1283
1284 void fixnone(char *str)
1285 {
1286 int i;
1287
1288 for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--);
1289 if(i==3 && strncmp(str,"none",4) == 0)
1290 str[0]='\0';
1291
1292 return;
1293 }
1294
1295 void fixendofline(char *str)
1296 {
1297 int i;
1298
1299 for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--) str[i]=0;
1300 }
1301
1302 #ifdef LEGACY_TESTVALIDUSERCHAR
1303 int testvaliduserchar(const char *user)
1304 {
1305
1306 int x=0;
1307 int y=0;
1308
1309 for (y=0; y<strlen(UserInvalidChar); y++) {
1310 for (x=0; x<strlen(user); x++) {
1311 if(user[x] == UserInvalidChar[y])
1312 return 1;
1313 }
1314 }
1315 return 0;
1316 }
1317 #else
1318 int testvaliduserchar(const char *user)
1319 {
1320
1321 char * p_UserInvalidChar = UserInvalidChar ;
1322 const char * p_user ;
1323
1324 while( *p_UserInvalidChar ) {
1325 p_user = user ;
1326 while ( *p_user ) {
1327 if( *p_UserInvalidChar == *p_user )
1328 return 1;
1329 p_user++ ;
1330 }
1331 p_UserInvalidChar++ ;
1332 }
1333 return 0;
1334 }
1335 #endif
1336
1337 int compar( const void *a, const void *b )
1338 { if( *(int *)a > *(int *)b ) return 1;
1339 if( *(int *)a < *(int *)b ) return -1;
1340 return 0;
1341 }
1342
1343 int getnumlist( char *buf, numlist *list, const int len, const int maxvalue )
1344 {
1345 int i, j, d, flag, r1, r2;
1346 char *pbuf, **bp, *strbufs[ 24 ];
1347
1348 bp = strbufs;
1349 strtok( buf, " \t" );
1350 for( *bp = strtok( NULL, "," ), list->len = 0; *bp; *bp = strtok( NULL, "," ) ) {
1351 if( ++bp >= &strbufs[ 24 ] )
1352 break;
1353 list->len++;
1354 }
1355 if( ! list->len )
1356 return -1;
1357 d = 0;
1358 for( i = 0; i < list->len; i++ ) {
1359 if( strchr( strbufs[ i ], '-' ) != 0 ) {
1360 pbuf = strbufs[ i ];
1361 strtok( pbuf, "-" );
1362 pbuf = strtok( NULL, "\0" );
1363 r1 = atoi( strbufs[ i ] );
1364 if( ( r2 = atoi( pbuf ) ) >= maxvalue || r1 >= r2 )
1365 return -1;
1366 if( i + d + ( r2 - r1 ) + 1 <= len ) {
1367 for( j = r1; j <= r2; j++ )
1368 list->list[ i + d++ ] = j;
1369 d--;
1370 }
1371 }
1372 else
1373 if( ( list->list[ i + d ] = atoi( strbufs[ i ] ) ) >= maxvalue )
1374 return 1;
1375 }
1376 list->len += d;
1377 qsort( list->list, list->len, sizeof( int ), compar );
1378 do {
1379 flag = 0;
1380 for( i = 0; i < list->len - 1; i++ )
1381 if( list->list[ i ] == list->list[ i + 1 ] ) {
1382 for( j = i + 1; j < list->len; j++ )
1383 list->list[ j - 1 ] = list->list[ j ];
1384 list->len--;
1385 flag = 1;
1386 break;
1387 }
1388 } while( flag );
1389 return 0;
1390 }
1391
1392
1393 char *get_size(const char *path, const char *file)
1394 {
1395 FILE *fp;
1396 static char response[255];
1397 char cmd[255];
1398 char *ptr;
1399
1400 if (snprintf(cmd,sizeof(cmd),"du -skh %s%s",path,file)>=sizeof(cmd)) {
1401 printf("SARG: Cannot get disk space because the path %s%s is too long.\n",path,file);
1402 exit(1);
1403 }
1404 fp = popen(cmd, "r");
1405 if (!fgets(response, sizeof(response), fp)) {
1406 fprintf(stderr,"SARG: Cannot get disk size with command %s",cmd);
1407 exit(1);
1408 }
1409 ptr=strchr(response,'\t');
1410 if (ptr==NULL) {
1411 fprintf(stderr,"SARG: The command %s failed.\n",cmd);
1412 exit(1);
1413 }
1414 pclose(fp);
1415 *ptr='\0';
1416
1417 return (response);
1418 }
1419
1420 void show_info(FILE *fp_ou)
1421 {
1422 char ftime[127];
1423
1424 if(!ShowSargInfo) return;
1425 zdate(ftime, sizeof(ftime), DateFormat);
1426 fprintf(fp_ou,"<div align=\"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></div>\n",text[108],URL,PGM,VERSION,text[109],ftime);
1427 }
1428
1429 void show_sarg(FILE *fp_ou, const char *ind)
1430 {
1431 if(ShowSargLogo) fprintf(fp_ou,"<div align=\"center\"><table class=\"logo\">\n<tr><th><a href=\"http://sarg.sourceforge.net\"><img src=\"%s/images/sarg.png\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\" alt=\"Sarg\"></a>&nbsp;Squid Analysis Report Generator</th></tr>\n<tr><th class=\"title\">&nbsp</th></tr>\n</table></div>\n",ind);
1432 }
1433
1434 void write_logo_image(FILE *fp_ou)
1435 {
1436 if(LogoImage[0]!='\0')
1437 fprintf(fp_ou, "<div align=\"center\"><table class=\"logo\">\n<tr><th><img src=\"%s\" width=\"%s\" height=\"%s\" alt=\"Logo\">&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n</div>\n",LogoImage,Width,Height,LogoText);
1438 }
1439
1440 void write_html_header(FILE *fp_ou, const char * ind)
1441 {
1442 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",CharSet);
1443 css(fp_ou);
1444 fprintf(fp_ou,"</head>\n<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage);
1445 write_logo_image(fp_ou);
1446 show_sarg(fp_ou, ind);
1447 fprintf(fp_ou,"<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"title\">%s</th></tr>\n</table></div>\n<div align=\"center\"><table cellpadding=\"1\" cellspacing=\"2\">\n<tr><td></td><td></td></tr>\n",Title);
1448 }
1449
1450 void output_html_string(FILE *fp_ou,const char *str)
1451 {
1452
1453 while (*str) {
1454 switch (*str) {
1455 case '&':
1456 fputs("&amp;",fp_ou);
1457 break;
1458 case '<':
1459 fputs("&lt;",fp_ou);
1460 break;
1461 case '>':
1462 fputs("&gt;",fp_ou);
1463 break;
1464 case '"':
1465 fputs("&quot;",fp_ou);
1466 break;
1467 case '\'':
1468 fputs("&#39;",fp_ou);
1469 break;
1470 default:
1471 fputc(*str,fp_ou);
1472 }
1473 str++;
1474 }
1475 }
1476
1477 void baddata(void)
1478 {
1479 char cmd[1024];
1480 int cstatus;
1481
1482 printf("SARG: ------------------------------------------------------------------------------\n");
1483 printf("SARG: MALICIUS CODE DETECTED.\n");
1484 printf("SARG: I think someone is trying to execute arbitrary code in your system using sarg.\n");
1485 printf("SARG: please review your access.log and/or your useragent.log file.\n");
1486 printf("SARG: process stoped. No actions taken.\n");
1487 printf("SARG: ------------------------------------------------------------------------------\n");
1488
1489 if (snprintf(cmd,sizeof(cmd),"rm -rf \"%s/sarg\"",tmp)>=sizeof(cmd)) {
1490 fprintf(stderr,"SARG: temporary directory too long: %s/sarg\n",tmp);
1491 exit(1);
1492 }
1493 cstatus=system(cmd);
1494 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
1495 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
1496 fprintf(stderr, "SARG: command: %s\n",cmd);
1497 exit(1);
1498 }
1499 if (snprintf(cmd,sizeof(cmd),"rm -rf \"%s\"",dirname)>=sizeof(cmd)) {
1500 fprintf(stderr,"SARG: directory to delete too long: %s\n",dirname);
1501 exit(1);
1502 }
1503 cstatus=system(cmd);
1504 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
1505 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
1506 fprintf(stderr, "SARG: command: %s\n",cmd);
1507 exit(1);
1508 }
1509
1510 exit(1);
1511 }
1512
1513
1514 void url_module(const char *url, char *w2)
1515 {
1516 int x, y;
1517 char w[255];
1518
1519 y=0;
1520 for(x=strlen(url)-1; x>=0; x--) {
1521 if(url[x] == '/' || y>=sizeof(w)-1) break;
1522 w[y++]=url[x];
1523 }
1524 if (x<0) {
1525 w2[0]='\0';
1526 return;
1527 }
1528
1529 x=0;
1530 for(y=y-1; y>=0; y--) {
1531 w2[x++]=w[y];
1532 }
1533 w2[x]='\0';
1534 }
1535
1536
1537 void write_html_trailer(FILE *fp_ou)
1538 {
1539 char ftime[128];
1540
1541 fputs("</table></div>\n",fp_ou);
1542 zdate(ftime, sizeof(ftime), DateFormat);
1543 show_info(fp_ou);
1544 fputs("</body>\n</html>\n",fp_ou);
1545 }
1546
1547 void version(void)
1548 {
1549 printf("SARG Version: %s\n",VERSION);
1550 exit(0);
1551 }
1552
1553 char *get_param_value(const char *param,char *line)
1554 {
1555 int plen;
1556
1557 while (*line==' ' || *line=='\t') line++;
1558 plen=strlen(param);
1559 if (strncasecmp(line,param,plen)) return(NULL);
1560 if (line[plen]!=' ' && line[plen]!='\t') return(NULL);
1561 line+=plen;
1562 while (*line==' ' || *line=='\t') line++;
1563 return(line);
1564 }
1565
1566 void read_usertab(const char *UserTabFile)
1567 {
1568 FILE *fp_usr;
1569 long int nreg;
1570 char buf[MAXLEN];
1571 int z2;
1572 int z1;
1573
1574 if (UserTabFile[0] == '\0') return;
1575 if(debug)
1576 debuga("%s: %s",text[86],UserTabFile);
1577 if((fp_usr=fopen(UserTabFile,"r"))==NULL) {
1578 fprintf(stderr, "SARG: (log) %s: %s - %s\n",text[45],UserTabFile,strerror(errno));
1579 exit(1);
1580 }
1581 fseek(fp_usr, 0, SEEK_END);
1582 nreg = ftell(fp_usr);
1583 if (nreg<0) {
1584 fprintf(stderr,"SARG: Cannot get the size of file %s",UserTabFile);
1585 exit(1);
1586 }
1587 nreg += 100;
1588 fseek(fp_usr, 0, SEEK_SET);
1589 if((userfile=(char *) malloc(nreg))==NULL){
1590 fprintf(stderr, "SARG ERROR: %s",text[87]);
1591 exit(1);
1592 }
1593 userfile[0]='\t';
1594 z2=1;
1595 while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
1596 if (buf[0]=='#') continue;
1597 fixendofline(buf);
1598 z1=0;
1599 while(buf[z1] && (unsigned char)buf[z1]>' ') {
1600 if (z2+3>=nreg) { //need at least 3 additional bytes for the minimum string "\n\t\0"
1601 fprintf(stderr,"SARG: The list of the users is too long in your %s file.\n",UserTabFile);
1602 exit(1);
1603 }
1604 userfile[z2++]=buf[z1++];
1605 }
1606 while(buf[z1] && (unsigned char)buf[z1]<=' ') z1++;
1607 userfile[z2++]='\n';
1608 while(buf[z1] && (unsigned char)buf[z1]>' ') {
1609 if (z2+2>=nreg) { //need at least 2 additional bytes for "\t\0"
1610 fprintf(stderr,"SARG: The list of the users is too long in your %s file.\n",UserTabFile);
1611 exit(1);
1612 }
1613 userfile[z2++]=buf[z1++];
1614 }
1615 userfile[z2++]='\t';
1616 }
1617 userfile[z2]='\0';
1618 fclose(fp_usr);
1619 }
1620
1621 void get_usertab_name(const char *user,char *name,int namelen)
1622 {
1623 char warea[MAXLEN];
1624 char *str;
1625
1626 namelen--;
1627 if(UserTabFile[0] == '\0') {
1628 strncpy(name,user,namelen);
1629 name[namelen]=0;
1630 } else {
1631 sprintf(warea,"\t%s\n",user);
1632 if((str=(char *) strstr(userfile,warea)) == (char *) NULL ) {
1633 strncpy(name,user,namelen);
1634 name[namelen]=0;
1635 } else {
1636 str=strchr(str+1,'\n');
1637 str++;
1638 for(z1=0; *str != '\t' && z1<namelen ; z1++) {
1639 name[z1]=*str++;
1640 }
1641 name[z1]=0;
1642 }
1643 }
1644 }