]> git.ipfire.org Git - thirdparty/sarg.git/blob - util.c
Index created using an internal sort algorithm instead of a system call
[thirdparty/sarg.git] / util.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2010
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,"%04d%02d%02d",atoi(ano),nmes+1,atoi(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 if (fwrite(buffer,1,nread,img_ou)!=nread) {
839 fprintf(stderr,"SARG: Failed to copy image %s to %s\n",srcfile,dstfile);
840 break;
841 }
842 }
843 fclose(img_ou);
844 } else
845 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open/create file", dstfile, strerror(errno));
846 fclose(img_in);
847 } else
848 fprintf(stderr,"SARG: (util): %s %s: %s\n", text[45]?text[45]:"Can't open file", srcfile, strerror(errno));
849 }
850 }
851 (void) closedir(dirp);
852
853 return;
854 }
855
856 void vrfydir(const char *per1, const char *addr, const char *site, const char *us, const char *form)
857 {
858 FILE *fp_ou;
859 int num=1, count=0;
860 char wdir[MAXLEN];
861 char per2[MAXLEN];
862 char dirname2[MAXLEN];
863 char y1[5], y2[5];
864 char d1[3], d2[3];
865 char m1[8], m2[8];
866 time_t curtime;
867 struct tm *loctm;
868
869 if(strcmp(IndexTree,"date") == 0) {
870 bzero(y1,sizeof(y1));
871 bzero(y2,sizeof(y2));
872 bzero(d1,sizeof(d1));
873 bzero(d2,sizeof(d2));
874 bzero(m1,sizeof(m1));
875 bzero(m2,sizeof(m2));
876 if(strncmp(df,"u",1) == 0) {
877 strncpy(y1,period,4);
878 strncpy(y2,period+10,4);
879 strncpy(m1,period+4,3);
880 strncpy(m2,period+14,3);
881 strncpy(d1,period+7,2);
882 strncpy(d2,period+17,2);
883 } else if(strncmp(df,"e",1) == 0) {
884 strncpy(d1,period+0,2);
885 strncpy(d2,period+10,2);
886 strncpy(m1,period+2,3);
887 strncpy(m2,period+12,3);
888 strncpy(y1,period+5,4);
889 strncpy(y2,period+15,4);
890 }
891 conv_month(m1);
892 conv_month(m2);
893
894 sprintf(wdir,"%s%s",outdir,y1);
895 if(strcmp(y1,y2) != 0) {
896 strcat(wdir,"-");
897 strcat(wdir,y2);
898 }
899 if(access(wdir, R_OK) != 0)
900 my_mkdir(wdir);
901
902 strcat(wdir,"/");
903 strcat(wdir,m1);
904 if(strcmp(m1,m2) != 0) {
905 strcat(wdir,"-");
906 strcat(wdir,m2);
907 }
908 if(access(wdir, R_OK) != 0)
909 my_mkdir(wdir);
910
911 strcat(wdir,"/");
912 strcat(wdir,d1);
913 if(strcmp(d1,d2) != 0) {
914 strcat(wdir,"-");
915 strcat(wdir,d2);
916 }
917 } else {
918 sprintf(wdir, "%s%s", outdir, per1);
919 }
920
921 if(us[0] != '\0') {
922 strcat(wdir,"-");
923 strcat(wdir,us);
924 }
925 if(addr[0] != '\0') {
926 strcat(wdir,"-");
927 strcat(wdir,addr);
928 }
929 if(site[0] != '\0') {
930 strcat(wdir,"-");
931 strcat(wdir,site);
932 }
933
934 strcpy(dirname,wdir);
935
936 if(strcmp(IndexTree,"date") != 0) {
937 if(!OverwriteReport) {
938 while(num) {
939 if(access(wdir,R_OK) == 0) {
940 sprintf(wdir,"%s.%d",dirname,num);
941 sprintf(per2,"%s.%d",per1,num);
942 num++;
943 count++;
944 } else
945 break;
946 }
947
948 if(count > 0) {
949 if(debug)
950 fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname,text[52],wdir);
951 rename(dirname,wdir);
952 }
953 } else {
954 if(access(dirname,R_OK) == 0) {
955 unlinkdir(dirname,1);
956 }
957 }
958 my_mkdir(dirname);
959 } else {
960 strcpy(dirname2,wdir);
961 if(!OverwriteReport) {
962 while(num) {
963 if(access(wdir,R_OK) == 0) {
964 sprintf(wdir,"%s.%d",dirname2,num);
965 sprintf(per2,"%s.%d",per1,num);
966 num++;
967 count++;
968 } else
969 break;
970 }
971
972 if(count > 0) {
973 if(debug)
974 fprintf(stderr, "SARG: %s: %s %s %s\n",text[51],dirname2,text[52],wdir);
975 rename(dirname2,wdir);
976 strcpy(dirname2,wdir);
977 }
978 } else {
979 if(access(wdir,R_OK) == 0) {
980 unlinkdir(wdir,1);
981 }
982 }
983
984 if(access(wdir, R_OK) != 0)
985 my_mkdir(wdir);
986 }
987
988 strcpy(dirname2,wdir);
989
990 sprintf(wdir,"%s/sarg-date",dirname);
991 if ((fp_ou = fopen(wdir, "wt")) == 0) {
992 fprintf(stderr, "SARG: cannot open %s for writing\n",wdir);
993 perror("SARG:");
994 exit(1);
995 }
996 time(&curtime);
997 //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime));
998 loctm=localtime(&curtime);
999 strftime(wdir,sizeof(wdir),"%Y-%m-%d %H:%M:%S",loctm);
1000 fprintf(fp_ou,"%s %d\n",wdir,loctm->tm_isdst);
1001 fclose(fp_ou);
1002
1003 copy_images();
1004 }
1005
1006 void strip_latin(char *line)
1007 {
1008 int i,j;
1009 int skip;
1010
1011 j=0;
1012 skip=0;
1013 for (i=0;line[i];i++){
1014 if (skip){
1015 if (line[i]==';') skip=0;
1016 } else {
1017 if (line[i]=='&')
1018 skip=1;
1019 else
1020 line[j++]=line[i];
1021 }
1022 }
1023 line[j]='\0';
1024 return;
1025
1026 }
1027
1028 void zdate(char *ftime,int ftimesize, const char *DateFormat)
1029 {
1030
1031 time_t t;
1032 struct tm *local;
1033
1034 t = time(NULL);
1035 local = localtime(&t);
1036 if(strcmp(DateFormat,"u") == 0)
1037 strftime(ftime, ftimesize, "%b/%d/%Y %H:%M", local);
1038 if(strcmp(DateFormat,"e") == 0)
1039 strftime(ftime, ftimesize, "%d/%b/%Y-%H:%M", local);
1040 if(strcmp(DateFormat,"w") == 0)
1041 strftime(ftime, ftimesize, "%V-%H-%M", local);
1042 return;
1043 }
1044
1045
1046 char *fixtime(long int elap)
1047 {
1048
1049 int num = elap / 1000;
1050 int hor = 0;
1051 int min = 0;
1052 int sec = 0;
1053 static char buf[12];
1054
1055 if(strcmp(datetimeby,"bytes") == 0) {
1056 strcpy(buf,fixnum(elap,1));
1057 return buf;
1058 }
1059
1060 if(num<1) {
1061 sprintf(buf,"00:00:%02ld",elap);
1062 return buf;
1063 }
1064
1065 hor=num / 3600;
1066 min=(num % 3600) / 60;
1067 sec=num % 60;
1068
1069 if(hor==0 && min==0 && sec==0)
1070 strcpy(buf,"0");
1071 else
1072 sprintf(buf,"%01d:%02d:%02d",hor,min,sec);
1073
1074 return buf;
1075
1076 }
1077
1078
1079 void date_from(char *date, char *dfrom, char *duntil)
1080 {
1081
1082 int diaf;
1083 int mesf;
1084 int anof;
1085 int diau;
1086 int mesu;
1087 int anou;
1088 char wdate[50];
1089
1090 strncpy(wdate,date,sizeof(wdate)-1);
1091 wdate[sizeof(wdate)-1]='\0';
1092 if(strchr(wdate,'-') == NULL) {
1093 if (strlen(wdate)*2+1>=sizeof(wdate)) {
1094 fprintf(stderr,"SARG: Invalid date range passed as argument.\n");
1095 exit(1);
1096 }
1097 strcat(wdate,"-");
1098 strcat(wdate,date);
1099 strcpy(date,wdate);
1100 }
1101
1102 if (sscanf(wdate,"%d/%d/%d-%d/%d/%d",&diaf,&mesf,&anof,&diau,&mesu,&anou)!=6) {
1103 fprintf(stderr,"SARG: The date range passed as argument is not formated as dd/mm/yyyy-dd/mm/yyyy.\n");
1104 exit(1);
1105 }
1106
1107 sprintf(dfrom,"%04d%02d%02d",anof,mesf,diaf);
1108 sprintf(duntil,"%04d%02d%02d",anou,mesu,diau);
1109 return;
1110 }
1111
1112
1113 char *strlow(char *string)
1114 {
1115 char *s;
1116
1117 if (string)
1118 {
1119 for (s = string; *s; ++s)
1120 *s = tolower(*s);
1121 }
1122
1123 return string;
1124 }
1125
1126
1127
1128
1129 char *strup(char *string)
1130 {
1131 char *s;
1132
1133 if (string)
1134 {
1135 for (s = string; *s; ++s)
1136 *s = toupper(*s);
1137 }
1138
1139 return string;
1140 }
1141
1142
1143 void subs(char *str, int size, char *from, char *to)
1144 {
1145 char *tmp;
1146 int i;
1147 int ss, st, sf;
1148 int len;
1149 int shift;
1150
1151 tmp = strstr(str, from);
1152 if(tmp == NULL)
1153 return;
1154
1155 ss = strlen(str);
1156 sf = strlen(from);
1157 st = strlen(to);
1158 shift=st-sf;
1159
1160 if (shift>0) {
1161 if (ss+shift>=size){
1162 fprintf(stderr,"SARG: Cannot replace %s by %s in %s and fit within %d bytes\n",from,to,str,size);
1163 exit(1);
1164 }
1165 for (i=strlen(tmp) ; i>=sf ; i--)
1166 tmp[i+shift]=tmp[i];
1167 } else if (shift<0) {
1168 len=strlen(tmp);
1169 for (i=sf ; i<=len ; i++)
1170 tmp[i+shift]=tmp[i];
1171 }
1172 memcpy(tmp, to, st);
1173 return;
1174 }
1175
1176
1177 void removetmp(const char *outdir)
1178 {
1179
1180 FILE *fp_in;
1181 char warea[256];
1182
1183 if(!RemoveTempFiles)
1184 return;
1185
1186 if(debug) {
1187 debuga("%s: sarg-general, sarg-period",text[82]);
1188 }
1189 if (snprintf(warea,sizeof(warea),"%s/sarg-general",outdir)>=sizeof(warea)) {
1190 fprintf(stderr, "SARG: (removetmp) directory too long to remove: %s/sarg-period\n",outdir);
1191 exit(1);
1192 }
1193 if((fp_in=fopen(warea,"r"))==NULL){
1194 fprintf(stderr, "===SARG: (removetmp) %s: %s\n",text[45],warea);
1195 exit(1);
1196 }
1197 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
1198 if(strncmp(buf,"TOTAL",5) == 0)
1199 break;
1200 }
1201 fclose(fp_in);
1202 if((fp_in=fopen(warea,"w"))==NULL){
1203 fprintf(stderr, "SARG: (removetmp) %s: %s\n",text[45],warea);
1204 exit(1);
1205 }
1206 fputs(buf,fp_in);
1207 fclose(fp_in);
1208 if (snprintf(warea,sizeof(warea),"%s/sarg-period",outdir)>=sizeof(warea)) {
1209 fprintf(stderr, "SARG: (removetmp) directory too long to remove: %s/sarg-period\n",outdir);
1210 exit(1);
1211 }
1212 unlink(warea);
1213
1214 return;
1215 }
1216
1217 void load_excludecodes(const char *ExcludeCodes)
1218 {
1219
1220 FILE *fp_in;
1221 char data[80];
1222 int i;
1223
1224 if(ExcludeCodes[0] == '\0')
1225 return;
1226
1227 if((excludecode=(char *) malloc(1024))==NULL) {
1228 fprintf(stderr, "SARG: %s (1024):\n",text[59]);
1229 exit(1);
1230 }
1231 bzero(excludecode,1024);
1232
1233 if((fp_in=fopen(ExcludeCodes,"r"))==NULL) {
1234 fprintf(stderr, "SARG: (util) Cannot open file: %s (exclude_codes)\n",ExcludeCodes);
1235 exit(1);
1236 }
1237
1238 while(fgets(data,sizeof(data),fp_in)!=NULL) {
1239 for (i=strlen(data)-1 ; i>=0 && (unsigned char)data[i]<=' ' ; i--) data[i]=0;
1240 if (i<0) continue;
1241 strcat(excludecode,data);
1242 strcat(excludecode,";");
1243 }
1244
1245 fclose(fp_in);
1246 return;
1247
1248 }
1249
1250 void free_excludecodes(void)
1251 {
1252 if (excludecode) {
1253 free(excludecode);
1254 excludecode=NULL;
1255 }
1256 }
1257
1258 int vercode(const char *code)
1259 {
1260 char *cod;
1261 int clen;
1262
1263 if (excludecode && excludecode[0]!=0) {
1264 clen=strlen(code);
1265 for (cod=excludecode ; cod ; cod=strchr(cod+1,';')) {
1266 if (strncmp(code,cod,clen)==0 && cod[clen]==';')
1267 return 1;
1268 }
1269 }
1270 return 0;
1271 }
1272
1273 void fixnone(char *str)
1274 {
1275 int i;
1276
1277 for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--);
1278 if(i==3 && strncmp(str,"none",4) == 0)
1279 str[0]='\0';
1280
1281 return;
1282 }
1283
1284 void fixendofline(char *str)
1285 {
1286 int i;
1287
1288 for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--) str[i]=0;
1289 }
1290
1291 #ifdef LEGACY_TESTVALIDUSERCHAR
1292 int testvaliduserchar(const char *user)
1293 {
1294
1295 int x=0;
1296 int y=0;
1297
1298 for (y=0; y<strlen(UserInvalidChar); y++) {
1299 for (x=0; x<strlen(user); x++) {
1300 if(user[x] == UserInvalidChar[y])
1301 return 1;
1302 }
1303 }
1304 return 0;
1305 }
1306 #else
1307 int testvaliduserchar(const char *user)
1308 {
1309
1310 char * p_UserInvalidChar = UserInvalidChar ;
1311 const char * p_user ;
1312
1313 while( *p_UserInvalidChar ) {
1314 p_user = user ;
1315 while ( *p_user ) {
1316 if( *p_UserInvalidChar == *p_user )
1317 return 1;
1318 p_user++ ;
1319 }
1320 p_UserInvalidChar++ ;
1321 }
1322 return 0;
1323 }
1324 #endif
1325
1326 int compar( const void *a, const void *b )
1327 { if( *(int *)a > *(int *)b ) return 1;
1328 if( *(int *)a < *(int *)b ) return -1;
1329 return 0;
1330 }
1331
1332 int getnumlist( char *buf, numlist *list, const int len, const int maxvalue )
1333 {
1334 int i, j, d, flag, r1, r2;
1335 char *pbuf, **bp, *strbufs[ 24 ];
1336
1337 bp = strbufs;
1338 strtok( buf, " \t" );
1339 for( *bp = strtok( NULL, "," ), list->len = 0; *bp; *bp = strtok( NULL, "," ) ) {
1340 if( ++bp >= &strbufs[ 24 ] )
1341 break;
1342 list->len++;
1343 }
1344 if( ! list->len )
1345 return -1;
1346 d = 0;
1347 for( i = 0; i < list->len; i++ ) {
1348 if( strchr( strbufs[ i ], '-' ) != 0 ) {
1349 pbuf = strbufs[ i ];
1350 strtok( pbuf, "-" );
1351 pbuf = strtok( NULL, "\0" );
1352 r1 = atoi( strbufs[ i ] );
1353 if( ( r2 = atoi( pbuf ) ) >= maxvalue || r1 >= r2 )
1354 return -1;
1355 if( i + d + ( r2 - r1 ) + 1 <= len ) {
1356 for( j = r1; j <= r2; j++ )
1357 list->list[ i + d++ ] = j;
1358 d--;
1359 }
1360 }
1361 else
1362 if( ( list->list[ i + d ] = atoi( strbufs[ i ] ) ) >= maxvalue )
1363 return 1;
1364 }
1365 list->len += d;
1366 qsort( list->list, list->len, sizeof( int ), compar );
1367 do {
1368 flag = 0;
1369 for( i = 0; i < list->len - 1; i++ )
1370 if( list->list[ i ] == list->list[ i + 1 ] ) {
1371 for( j = i + 1; j < list->len; j++ )
1372 list->list[ j - 1 ] = list->list[ j ];
1373 list->len--;
1374 flag = 1;
1375 break;
1376 }
1377 } while( flag );
1378 return 0;
1379 }
1380
1381
1382 char *get_size(const char *path, const char *file)
1383 {
1384 FILE *fp;
1385 static char response[255];
1386 char cmd[255];
1387 char *ptr;
1388
1389 if (snprintf(cmd,sizeof(cmd),"du -skh %s%s",path,file)>=sizeof(cmd)) {
1390 printf("SARG: Cannot get disk space because the path %s%s is too long.\n",path,file);
1391 exit(1);
1392 }
1393 fp = popen(cmd, "r");
1394 if (!fgets(response, sizeof(response), fp)) {
1395 fprintf(stderr,"SARG: Cannot get disk size with command %s",cmd);
1396 exit(1);
1397 }
1398 ptr=strchr(response,'\t');
1399 if (ptr==NULL) {
1400 fprintf(stderr,"SARG: The command %s failed.\n",cmd);
1401 exit(1);
1402 }
1403 pclose(fp);
1404 *ptr='\0';
1405
1406 return (response);
1407 }
1408
1409 void show_info(FILE *fp_ou)
1410 {
1411 char ftime[127];
1412
1413 if(!ShowSargInfo) return;
1414 zdate(ftime, sizeof(ftime), DateFormat);
1415 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);
1416 }
1417
1418 void show_sarg(FILE *fp_ou, const char *ind)
1419 {
1420 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);
1421 }
1422
1423 void write_logo_image(FILE *fp_ou)
1424 {
1425 if(LogoImage[0]!='\0')
1426 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);
1427 }
1428
1429 void write_html_header(FILE *fp_ou, const char * ind)
1430 {
1431 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);
1432 css(fp_ou);
1433 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);
1434 write_logo_image(fp_ou);
1435 show_sarg(fp_ou, ind);
1436 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);
1437 }
1438
1439 void output_html_string(FILE *fp_ou,const char *str)
1440 {
1441
1442 while (*str) {
1443 switch (*str) {
1444 case '&':
1445 fputs("&amp;",fp_ou);
1446 break;
1447 case '<':
1448 fputs("&lt;",fp_ou);
1449 break;
1450 case '>':
1451 fputs("&gt;",fp_ou);
1452 break;
1453 case '"':
1454 fputs("&quot;",fp_ou);
1455 break;
1456 case '\'':
1457 fputs("&#39;",fp_ou);
1458 break;
1459 default:
1460 fputc(*str,fp_ou);
1461 }
1462 str++;
1463 }
1464 }
1465
1466 void baddata(void)
1467 {
1468 char dir[1024];
1469
1470 printf("SARG: ------------------------------------------------------------------------------\n");
1471 printf("SARG: MALICIUS CODE DETECTED.\n");
1472 printf("SARG: I think someone is trying to execute arbitrary code in your system using sarg.\n");
1473 printf("SARG: please review your access.log and/or your useragent.log file.\n");
1474 printf("SARG: process stoped. No actions taken.\n");
1475 printf("SARG: ------------------------------------------------------------------------------\n");
1476
1477 if (snprintf(dir,sizeof(dir),"%s/sarg",tmp)>=sizeof(dir)) {
1478 fprintf(stderr,"SARG: temporary directory too long: %s/sarg\n",tmp);
1479 exit(1);
1480 }
1481 unlinkdir(dir,0);
1482 unlinkdir(dirname,0);
1483
1484 exit(1);
1485 }
1486
1487
1488 void url_module(const char *url, char *w2)
1489 {
1490 int x, y;
1491 char w[255];
1492
1493 y=0;
1494 for(x=strlen(url)-1; x>=0; x--) {
1495 if(url[x] == '/' || y>=sizeof(w)-1) break;
1496 w[y++]=url[x];
1497 }
1498 if (x<0) {
1499 w2[0]='\0';
1500 return;
1501 }
1502
1503 x=0;
1504 for(y=y-1; y>=0; y--) {
1505 w2[x++]=w[y];
1506 }
1507 w2[x]='\0';
1508 }
1509
1510
1511 void write_html_trailer(FILE *fp_ou)
1512 {
1513 char ftime[128];
1514
1515 fputs("</table></div>\n",fp_ou);
1516 zdate(ftime, sizeof(ftime), DateFormat);
1517 show_info(fp_ou);
1518 fputs("</body>\n</html>\n",fp_ou);
1519 }
1520
1521 void version(void)
1522 {
1523 printf("SARG Version: %s\n",VERSION);
1524 exit(0);
1525 }
1526
1527 char *get_param_value(const char *param,char *line)
1528 {
1529 int plen;
1530
1531 while (*line==' ' || *line=='\t') line++;
1532 plen=strlen(param);
1533 if (strncasecmp(line,param,plen)) return(NULL);
1534 if (line[plen]!=' ' && line[plen]!='\t') return(NULL);
1535 line+=plen;
1536 while (*line==' ' || *line=='\t') line++;
1537 return(line);
1538 }
1539
1540 void read_usertab(const char *UserTabFile)
1541 {
1542 FILE *fp_usr;
1543 long int nreg;
1544 char buf[MAXLEN];
1545 int z2;
1546 int z1;
1547
1548 if (UserTabFile[0] == '\0') return;
1549 if(debug)
1550 debuga("%s: %s",text[86],UserTabFile);
1551 if((fp_usr=fopen(UserTabFile,"r"))==NULL) {
1552 fprintf(stderr, "SARG: (log) %s: %s - %s\n",text[45],UserTabFile,strerror(errno));
1553 exit(1);
1554 }
1555 fseek(fp_usr, 0, SEEK_END);
1556 nreg = ftell(fp_usr);
1557 if (nreg<0) {
1558 fprintf(stderr,"SARG: Cannot get the size of file %s",UserTabFile);
1559 exit(1);
1560 }
1561 nreg += 100;
1562 fseek(fp_usr, 0, SEEK_SET);
1563 if((userfile=(char *) malloc(nreg))==NULL){
1564 fprintf(stderr, "SARG ERROR: %s",text[87]);
1565 exit(1);
1566 }
1567 userfile[0]='\t';
1568 z2=1;
1569 while(fgets(buf,sizeof(buf),fp_usr)!=NULL) {
1570 if (buf[0]=='#') continue;
1571 fixendofline(buf);
1572 z1=0;
1573 while(buf[z1] && (unsigned char)buf[z1]>' ') {
1574 if (z2+3>=nreg) { //need at least 3 additional bytes for the minimum string "\n\t\0"
1575 fprintf(stderr,"SARG: The list of the users is too long in your %s file.\n",UserTabFile);
1576 exit(1);
1577 }
1578 userfile[z2++]=buf[z1++];
1579 }
1580 while(buf[z1] && (unsigned char)buf[z1]<=' ') z1++;
1581 userfile[z2++]='\n';
1582 while(buf[z1] && (unsigned char)buf[z1]>' ') {
1583 if (z2+2>=nreg) { //need at least 2 additional bytes for "\t\0"
1584 fprintf(stderr,"SARG: The list of the users is too long in your %s file.\n",UserTabFile);
1585 exit(1);
1586 }
1587 userfile[z2++]=buf[z1++];
1588 }
1589 userfile[z2++]='\t';
1590 }
1591 userfile[z2]='\0';
1592 fclose(fp_usr);
1593 }
1594
1595 void get_usertab_name(const char *user,char *name,int namelen)
1596 {
1597 char warea[MAXLEN];
1598 char *str;
1599
1600 namelen--;
1601 if(UserTabFile[0] == '\0') {
1602 strncpy(name,user,namelen);
1603 name[namelen]=0;
1604 } else {
1605 sprintf(warea,"\t%s\n",user);
1606 if((str=(char *) strstr(userfile,warea)) == (char *) NULL ) {
1607 strncpy(name,user,namelen);
1608 name[namelen]=0;
1609 } else {
1610 str=strchr(str+1,'\n');
1611 str++;
1612 for(z1=0; *str != '\t' && z1<namelen ; z1++) {
1613 name[z1]=*str++;
1614 }
1615 name[z1]=0;
1616 }
1617 }
1618 }
1619
1620 void unlinkdir(const char *dir,int contentonly)
1621 {
1622 struct stat st;
1623 DIR *dirp;
1624 struct dirent *direntp;
1625 char dname[MAXLEN];
1626
1627 dirp=opendir(dir);
1628 if (!dirp) return;
1629 while ((direntp = readdir(dirp)) != NULL) {
1630 if (direntp->d_name[0] == '.' && (direntp->d_name[1] == '\0' ||
1631 (direntp->d_name[1] == '.' && direntp->d_name[2] == '\0')))
1632 continue;
1633 if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) {
1634 fprintf(stderr,"SARG: directory name to delete too long: %s/%s\n",dir,direntp->d_name);
1635 exit(1);
1636 }
1637 if (lstat(dname,&st)) {
1638 fprintf(stderr,"SARG: cannot stat %s\n",dname);
1639 exit(1);
1640 }
1641 if (S_ISREG(st.st_mode)) {
1642 if (unlink(dname)) {
1643 fprintf(stderr,"SARG: cannot delete %s - %s\n",dname,strerror(errno));
1644 exit(1);
1645 }
1646 } else if (S_ISDIR(st.st_mode)) {
1647 unlinkdir(dname,1);
1648 } else {
1649 fprintf(stderr,"SARG: unknown path type %s\n",dname);
1650 }
1651 }
1652 closedir(dirp);
1653
1654 if (!contentonly) {
1655 if (rmdir(dir)) {
1656 fprintf(stderr,"SARG: cannot delete %s - %s\n",dir,strerror(errno));
1657 exit(1);
1658 }
1659 }
1660 }