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