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