]>
git.ipfire.org Git - thirdparty/sarg.git/blob - util.c
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
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.
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.
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.
26 // #define LEGACY_MY_ATOLL
27 // #define LEGACY_TESTVALIDUSERCHAR
29 #include "include/conf.h"
31 static char mtab1
[ 12 ][ 4 ]={ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" };
32 static char mtab2
[ 12 ][ 3 ]={ "01" , "02" , "03" , "04" , "05" , "06" , "07" , "08" , "09" , "10" , "11" , "12" };
34 /*void fgetword(char *word, char *line, int stop)
39 for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x];
46 tchar = strchr(line, stop);
47 if (tchar == NULL) strcpy(word, line);
50 difflen = tchar - line;
51 strncpy(word, line, difflen);
56 void getword ( char * word
, char * line
, int stop
)
64 if ( strlen ( line
) < 3 ) {
69 for ( x
= 0 ; line
[ x
] && ( line
[ x
] != stop
) && x
< limit
; x
++) word
[ x
] = line
[ x
];
71 printf ( "SARG: getword loop detected. \n " );
72 //printf("SARG: Record=\"%s\"\n",wline);
73 printf ( "SARG: searching for \' x%x \'\n " , stop
);
74 printf ( "SARG: Maybe you have a broken record or garbage in your access.log file. \n " );
83 while (( line
[ y
++] = line
[ x
++]));
86 char * getword2 ( char * word
, char * line
, int stop
)
91 if ( strlen ( line
) < 3 ) {
96 // printf( "IN Buffer <%s>\n" , line ) ;
97 for ( x
= 0 ;(( line
[ x
]) && ( line
[ x
] != stop
&& limit
)); x
++ , limit
-- ) word
[ x
] = line
[ x
];
99 printf ( "SARG: getword2 loop detected. \n " );
100 printf ( "SARG: Buffer= \" %s \"\n " , line
);
101 printf ( "SARG: searching for \' x%x \'\n " , stop
);
102 printf ( "SARG: Maybe you have a broken record or garbage in your access.log file. \n " );
107 // printf( "Value <%s>\n" , word ) ;
108 // printf( "OUT Buffer <%s>\n" , line+x+1 ) ;
109 return ( line
+ x
+ 1 ) ;
112 void getword3 ( char * word
, char * line
, int stop
)
116 for ( x
= 0 ;( line
[ x
] && ( line
[ x
] != stop
)); x
++) word
[ x
] = line
[ x
];
119 while (( line
[ y
++] = line
[ x
++]));
123 #ifdef LEGACY_MY_ATOLL
125 // BMG (bguillory@email.com)
127 long long int my_atoll ( const char * nptr
)
128 #define MAXLLL 30 //maximum number of digits in long long (a guess)
131 long long int returnval
= 0 ;
136 // Soak up all the white space
137 while ( isspace ( nptr
[ offset
])) {
141 //For each character left to right
142 //change the character to a single digit
143 //multiply what we had before by 10 and add the new digit
144 for ( x
= offset
; x
<= MAXLLL
+ offset
&& isdigit ( nptr
[ x
]); x
++) {
145 sprintf ( one_digit
, "%c" , nptr
[ x
]); //I don't know how else to do this
146 returnval
= ( returnval
* 10 ) + atoi ( one_digit
);
155 #define MAXLLL 30 //maximum number of digits in long long (a guess)
156 long long int my_atoll ( const char * nptr
)
158 long long int returnval
= 0 ;
160 int max_digits
= MAXLLL
;
162 // Soak up all the white space
163 while ( isspace ( * t
)) {
167 //For each character left to right
168 //change the character to a single digit
169 //multiply what we had before by 10 and add the new digit
171 for ( ; -- max_digits
&& isdigit ( * t
) ; t
++ )
173 returnval
= ( returnval
* 10 ) + ( * t
- '0' ) ;
183 void my_mkdir ( char * name
)
189 if ( strncmp ( name
, "." , 1 ) == 0 || strncmp ( name
, "/" , 1 ) != 0 ) {
190 fprintf ( stderr
, "SARG: Ivalid path (%s). Please, use absolute paths only. \n " , name
);
191 fprintf ( stderr
, "SARG: process aborted. \n " );
198 while ( strstr ( w0
, "/" ) != 0 ) {
201 if ( access ( w2
, R_OK
) != 0 ) {
203 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w2
, strerror ( errno
));
204 fprintf ( stderr
, "SARG: process aborted. \n " );
211 if ( access ( w2
, R_OK
) != 0 ) {
213 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w2
, strerror ( errno
));
214 fprintf ( stderr
, "SARG: process aborted. \n " );
221 void my_lltoa ( unsigned long long int n
, char s
[], int len
)
227 s
[ i
++] = ( n
% 10 ) + '0' ;
228 } while (( n
/= 10 ) > 0 );
232 for ( i
= 0 , j
= strlen ( s
)- 1 ; i
< j
; i
++, j
--)
241 bzero ( ww
, sizeof ( ww
));
246 strncat ( ww
, s
, i
> sizeof ( ww
)? sizeof ( ww
): i
);
253 void builddia ( char * dia
, char * mes
, char * ano
, char * df
, char * wdata
)
259 if ( strlen ( dia
) < 1 ) return ;
264 for ( x
= 0 ; x
< 12 ; x
++) {
265 if ( strcmp ( mtab1
[ x
], mes
) == 0 ) {
266 strncpy ( nmes
, mtab2
[ x
], sizeof ( nmes
)- 1 );
267 nmes
[ sizeof ( nmes
)- 1 ]= 0 ;
272 snprintf ( wdata
, 9 , "%s%s%s" , ano
, nmes
, dia
);
274 if ( strncmp ( df
, "u" , 1 ) != 0 )
275 snprintf ( ndia
, sizeof ( ndia
), "%s/%s/%s" , dia
, nmes
, ano
);
277 snprintf ( ndia
, sizeof ( ndia
), "%s/%s/%s" , nmes
, dia
, ano
);
284 void buildymd ( char * dia
, char * mes
, char * ano
, char * wdata
)
291 for ( x
= 0 ; x
< 12 ; x
++) {
292 if ( strcmp ( mtab1
[ x
], mes
) == 0 )
293 strcpy ( nmes
, mtab2
[ x
]);
296 sprintf ( wdata
, "%s%s%s" , ano
, nmes
, dia
);
301 void conv_month ( char * month
)
305 for ( x
= 0 ; x
< 12 ; x
++) {
306 if ( strcmp ( mtab1
[ x
], month
) == 0 )
307 strcpy ( month
, mtab2
[ x
]);
313 void conv_month_name ( char * month
)
317 for ( x
= 0 ; x
< 12 ; x
++) {
318 if ( strcmp ( mtab2
[ x
], month
) == 0 )
319 strcpy ( month
, mtab1
[ x
]);
324 void name_month ( char * month
)
326 int x
, z
= atoi ( month
)- 1 ;
334 getword ( month
, m
, ',' );
338 void fixper ( char * tbuf
, char * period
, char * duntil
)
342 char dia
[ 5 ], mes
[ 5 ], ano
[ 5 ];
347 strncpy ( dia
, duntil
+ 6 , 2 );
349 strncpy ( mes
, duntil
+ 4 , 2 );
351 strncpy ( ano
, duntil
, 4 );
354 for ( x
= 0 ; x
< 12 ; x
++) {
355 if ( strcmp ( mtab2
[ x
], mes
) == 0 )
356 strcpy ( mes
, mtab1
[ x
]);
359 if ( strcmp ( df
, "e" ) == 0 )
360 sprintf ( warea
, "%s%s%s" , dia
, mes
, ano
);
361 if ( strcmp ( df
, "u" ) == 0 )
362 sprintf ( warea
, "%s%s%s" , ano
, mes
, dia
);
364 strcat ( period
, warea
);
368 void debuga ( char * msg
)
370 fprintf ( stderr
, "SARG: %s \n " , msg
);
375 void debugaz ( char * head
, char * msg
)
377 fprintf ( stderr
, "SARG: (util) %s=%s \n " , head
, msg
);
384 char n1
[ MAXLEN
], n2
[ MAXLEN
], n3
[ MAXLEN
];
391 if ( strstr ( ip
, "." ) != 0 ) {
406 sprintf ( ip
, "%s%s%s%s%s%s%s" , n1
, sep
, n2
, sep
, n3
, sep
, wip
);
411 char * fixnum ( long long int value
, int n
)
415 char buf
[ MAXLEN
* 2 ];
417 static char ret
[ MAXLEN
* 2 ];
419 register int i
, j
, k
;
420 static char abbrev
[ 30 ];
422 my_lltoa ( value
, num
, 0 );
424 if ( strcmp ( DisplayedValues
, "abbreviation" ) == 0 ) {
426 sprintf ( abbrev
, "%s" , num
);
427 if ( strlen ( num
) == 4 || strlen ( num
) == 7 || strlen ( num
) == 10 || strlen ( num
) == 13 ) {
428 snprintf ( abbrev
, 2 , "%s" , num
);
429 strncat ( abbrev
, "." , 1 );
430 strncat ( abbrev
, num
+ 1 , 2 );
431 if (! n
) return ( abbrev
);
433 strncat ( abbrev
, "K" , 1 );
434 else if ( strlen ( num
) == 7 )
435 strncat ( abbrev
, "M" , 1 );
436 else if ( strlen ( num
) == 10 )
437 strncat ( abbrev
, "G" , 1 );
438 else if ( strlen ( num
) == 13 )
439 strncat ( abbrev
, "T" , 1 );
441 if ( strlen ( num
) == 5 || strlen ( num
) == 8 || strlen ( num
) == 11 || strlen ( num
) == 14 ) {
442 snprintf ( abbrev
, 3 , "%s" , num
);
443 strncat ( abbrev
, "." , 1 );
444 strncat ( abbrev
, num
+ 2 , 2 );
445 if (! n
) return ( abbrev
);
447 strncat ( abbrev
, "K" , 1 );
448 else if ( strlen ( num
) == 8 )
449 strncat ( abbrev
, "M" , 1 );
450 else if ( strlen ( num
) == 11 )
451 strncat ( abbrev
, "G" , 1 );
452 else if ( strlen ( num
) == 14 )
453 strncat ( abbrev
, "T" , 1 );
455 if ( strlen ( num
) == 6 || strlen ( num
) == 9 || strlen ( num
) == 12 || strlen ( num
) == 15 ) {
456 snprintf ( abbrev
, 4 , "%s" , num
);
457 strncat ( abbrev
, "." , 1 );
458 strncat ( abbrev
, num
+ 3 , 2 );
459 if (! n
) return ( abbrev
);
461 strncat ( abbrev
, "K" , 1 );
462 else if ( strlen ( num
) == 9 )
463 strncat ( abbrev
, "M" , 1 );
464 else if ( strlen ( num
) == 12 )
465 strncat ( abbrev
, "G" , 1 );
466 else if ( strlen ( num
) == 15 )
467 strncat ( abbrev
, "T" , 1 );
473 bzero ( buf
, MAXLEN
* 2 );
479 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
480 if ( k
== 2 && i
!= 0 ) {
483 if ( strcmp ( UseComma
, "yes" ) == 0 )
485 else pbuf
[ j
++] = '.' ;
495 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
504 char * fixnum2 ( long long int value
, int n
)
508 char buf
[ MAXLEN
* 2 ];
510 static char ret
[ MAXLEN
* 2 ];
512 register int i
, j
, k
;
513 static char abbrev
[ 30 ];
515 my_lltoa ( value
, num
, 0 );
516 bzero ( buf
, MAXLEN
* 2 );
522 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
523 if ( k
== 2 && i
!= 0 ) {
526 if ( strcmp ( UseComma
, "yes" ) == 0 )
528 else pbuf
[ j
++] = '.' ;
538 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
548 void buildhref ( char * href
)
552 if ( strcmp ( href
, "./" ) == 0 ){
554 strcat ( href
, "<a href='" );
558 href
[ strlen ( href
)- 1 ]= '\0' ;
559 sprintf ( whref
, "%s" , strrchr ( href
, '/' ));
561 strcpy ( href
, "<a href='" );
570 char * buildtime ( long long int elap
)
573 int num
= elap
/ 1000 ;
582 min
=( num
% 3600 ) / 60 ;
584 sprintf ( buf
, "%02d:%02d:%02d" , hor
, min
, sec
);
591 void obtdate ( char * dirname
, char * name
, char * data
)
597 sprintf ( wdir
, "%s%s/sarg-date" , dirname
, name
);
598 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
599 sprintf ( wdir
, "%s%s/date" , dirname
, name
);
600 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
606 fgets ( data
, 80 , fp_in
);
608 data
[ strlen ( data
)- 1 ]= '\0' ;
615 void obtuser ( char * dirname
, char * name
, char * tuser
)
621 sprintf ( wdir
, "%s%s/sarg-users" , dirname
, name
);
622 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
623 sprintf ( wdir
, "%s%s/users" , dirname
, name
);
624 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
630 fgets ( tuser
, 20 , fp_in
);
631 tuser
[ strlen ( tuser
)- 1 ]= '\0' ;
639 void obttotal ( char * dirname
, char * name
, char * tbytes
, char * tuser
, char * media
)
645 long long int wtuser
= 0 ;
647 sprintf ( wdir
, "%s%s/sarg-general" , dirname
, name
);
648 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
649 sprintf ( wdir
, "%s%s/general" , dirname
, name
);
650 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
656 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
657 getword ( warea
, buf
, ' ' );
658 if ( strcmp ( warea
, "TOTAL" ) != 0 )
660 getword ( warea
, buf
, ' ' );
661 getword ( warea
, buf
, ' ' );
662 twork
= my_atoll ( warea
);
663 sprintf ( tbytes
, "%s" , fixnum ( twork
, 1 ));
667 if ( tuser
[ 0 ] == '\0' ) {
669 sprintf ( media
, "%s" , "0" );
673 wtuser
= my_atoll ( tuser
);
674 med
= my_atoll ( warea
) / wtuser
;
675 sprintf ( media
, "%s" , fixnum ( med
, 1 ));
682 //void gperiod(char *dirname, const char *period)
688 char wdirname
[ MAXLEN
];
690 strcpy ( wdirname
, dirname
);
691 strcat ( wdirname
, "/" );
692 strcat ( wdirname
, "sarg-period" );
694 if (( fp_ou
= fopen ( wdirname
, "w" ))== NULL
){
695 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
703 debuga (( char *) text
[ 50 ]);
709 void vrfydir ( char * dir
, char * per1
, char * addr
, char * site
, char * us
, char * form
)
711 FILE * img_in
, * img_ou
;
716 char dirname2
[ MAXLEN
];
719 struct dirent
* direntp
;
721 if ( strcmp ( IndexTree
, "date" ) == 0 ) {
728 if ( strncmp ( df
, "u" , 1 ) == 0 ) {
729 strncpy ( y1
, period
, 4 );
730 strncpy ( y2
, period
+ 10 , 4 );
731 strncpy ( m1
, period
+ 4 , 3 );
732 strncpy ( m2
, period
+ 14 , 3 );
733 strncpy ( d1
, period
+ 7 , 2 );
734 strncpy ( d2
, period
+ 17 , 2 );
735 } else if ( strncmp ( df
, "e" , 1 ) == 0 ) {
736 strncpy ( d1
, period
+ 0 , 2 );
737 strncpy ( d2
, period
+ 10 , 2 );
738 strncpy ( m1
, period
+ 2 , 3 );
739 strncpy ( m2
, period
+ 12 , 3 );
740 strncpy ( y1
, period
+ 5 , 4 );
741 strncpy ( y2
, period
+ 15 , 4 );
746 sprintf ( wdir
, "%s%s" , outdir
, y1
);
747 if ( strcmp ( y1
, y2
) != 0 ) {
749 strncat ( wdir
, y2
, strlen ( y2
));
751 if ( access ( wdir
, R_OK
) != 0 )
755 strncat ( wdir
, m1
, strlen ( m1
));
756 if ( strcmp ( m1
, m2
) != 0 ) {
758 strncat ( wdir
, m2
, strlen ( m2
));
760 if ( access ( wdir
, R_OK
) != 0 )
764 strncat ( wdir
, d1
, strlen ( d1
));
765 if ( strcmp ( d1
, d2
) != 0 ) {
767 strncat ( wdir
, d2
, strlen ( d2
));
770 sprintf ( wdir
, "%s" , dir
);
776 if ( strlen ( addr
) > 0 ) {
780 if ( strlen ( site
) > 0 ) {
785 if ( strcmp ( dirname
, wdir
) != 0 )
786 strcpy ( dirname
, wdir
);
788 if ( strcmp ( IndexTree
, "date" ) != 0 ) {
789 strcpy ( dirname2
, dirname
);
790 if ( strcmp ( OverwriteReport
, "no" ) == 0 ) {
792 if ( access ( wdir
, R_OK
) == 0 ) {
793 sprintf ( wdir
, "%s.%d" , dirname
, num
);
794 sprintf ( per2
, "%s.%d" , per1
, num
);
803 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
804 rename ( dirname2
, wdir
);
807 if ( access ( dir
, R_OK
) == 0 ) {
808 sprintf ( csort
, "rm -r %s" , dir
);
814 strcpy ( dirname2
, wdir
);
815 if ( strcmp ( OverwriteReport
, "no" ) == 0 ) {
817 if ( access ( wdir
, R_OK
) == 0 ) {
818 sprintf ( wdir
, "%s.%d" , dirname2
, num
);
819 sprintf ( per2
, "%s.%d" , per1
, num
);
828 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
829 rename ( dirname2
, wdir
);
830 strcpy ( dirname2
, wdir
);
833 if ( access ( wdir
, R_OK
) == 0 ) {
834 sprintf ( csort
, "rm -r %s" , wdir
);
839 if ( access ( wdir
, R_OK
) != 0 )
843 strcpy ( dirname2
, wdir
);
844 sprintf ( images
, "%simages" , outdir
);
847 sprintf ( wdir
, "date >%s/%s" , dirname
, "sarg-date" );
850 sprintf ( per2
, "%s/images" , SYSCONFDIR
);
852 dirp
= opendir ( per2
);
854 fprintf ( stderr
, "SARG: (util) %s %s: %s \n " , "Can't open directory" , per2
, strerror ( errno
));
857 while (( direntp
= readdir ( dirp
)) != NULL
){
858 if ( strncmp ( direntp
-> d_name
, "." , 1 ) == 0 )
860 sprintf ( val10
, "%s/%s" , per2
, direntp
-> d_name
);
861 sprintf ( val11
, "%s/%s" , images
, direntp
-> d_name
);
862 img_in
= fopen ( val10
, "rb" );
864 img_ou
= fopen ( val11
, "wb" );
874 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open/create file" , val11
, strerror ( errno
));
876 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open file" , val10
, strerror ( errno
));
880 ( void ) rewinddir ( dirp
);
881 ( void ) closedir ( dirp
);
889 void strip_latin ( char * line
)
894 while ( strstr ( line
, "&" ) != 0 ){
895 getword ( warea
, line
, '&' );
896 strncat ( warea
, line
, 1 );
897 getword ( buf
, line
, ';' );
906 void zdate ( char * ftime
, char * DateFormat
)
913 local
= localtime (& t
);
914 if ( strcmp ( DateFormat
, "u" ) == 0 )
915 strftime ( ftime
, 127 , "%b/%d/%Y %H:%M" , local
);
916 if ( strcmp ( DateFormat
, "e" ) == 0 )
917 strftime ( ftime
, 127 , "%d/%b/%Y-%H:%M" , local
);
918 if ( strcmp ( DateFormat
, "w" ) == 0 )
919 strftime ( ftime
, 127 , "%V-%H-%M" , local
);
925 char * fixtime ( long int elap
)
928 int num
= elap
/ 1000 ;
934 if ( strcmp ( datetimeby
, "bytes" ) == 0 ) {
935 sprintf ( buf
, "%s" , fixnum ( elap
, 1 ));
942 sprintf ( buf
, "00:00:%02ld" , elap
);
947 min
=( num
% 3600 ) / 60 ;
950 sprintf ( buf
, "%01d:%02d:%02d" , hor
, min
, sec
);
952 if ( strcmp ( buf
, "0:00:00" ) == 0 )
960 void date_from ( char * date
, char * dfrom
, char * duntil
)
969 static char wdate
[ 50 ];
973 if ( strstr ( wdate
, "-" ) == 0 ) {
979 getword ( diaf
, wdate
, '/' );
980 getword ( mesf
, wdate
, '/' );
981 getword ( anof
, wdate
, '-' );
982 getword ( diau
, wdate
, '/' );
983 getword ( mesu
, wdate
, '/' );
986 sprintf ( dfrom
, "%s%s%s" , anof
, mesf
, diaf
);
987 sprintf ( duntil
, "%s%s%s" , anou
, mesu
, diau
);
992 char * strlow ( char * string
)
998 for ( s
= string
; * s
; ++ s
)
1008 char * strup ( char * string
)
1014 for ( s
= string
; * s
; ++ s
)
1022 char * subs ( char * str
, char * from
, char * to
)
1026 unsigned int ss
, st
;
1028 if ( strstr ( str
, from
) == 0 )
1029 return ( char *) str
;
1031 ss
= strlen ( str
); st
= strlen ( to
) + 10 ;
1033 if (( ret
=( char *) malloc ( ss
+ st
))== NULL
)
1035 fprintf ( stderr
, "SARG: %s (%d): \n " , text
[ 59 ], ss
+ st
);
1041 tmp
= strstr ( str
, from
);
1042 if ( tmp
== ( char *) NULL
)
1043 return ( char *) NULL
;
1044 strncpy ( ret
, str
, ss
- strlen ( tmp
));
1046 strcat ( ret
, ( tmp
+ strlen ( from
)));
1047 return ( char *) ret
;
1051 void removetmp ( char * outdir
)
1057 if ( strcmp ( RemoveTempFiles
, "yes" ) != 0 ) {
1061 sprintf ( msg
, "%s: sarg-general, sarg-period" , text
[ 82 ]);
1064 sprintf ( warea
, "%s/sarg-general" , outdir
);
1065 if (( fp_in
= fopen ( warea
, "r" ))== NULL
){
1066 fprintf ( stderr
, "===SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1069 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
1070 if ( strncmp ( buf
, "TOTAL" , 5 ) == 0 )
1074 if (( fp_in
= fopen ( warea
, "w" ))== NULL
){
1075 fprintf ( stderr
, "SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1080 sprintf ( warea
, "%s/sarg-period" , outdir
);
1087 void load_excludecodes ()
1093 if (( fp_in
= fopen ( ExcludeCodes
, "r" ))== NULL
) {
1094 fprintf ( stderr
, "SARG: (util) Cannot open file: %s (exclude_codes) \n " , ExcludeCodes
);
1098 while ( fgets ( data
, 80 , fp_in
)!= NULL
) {
1099 data
[ strlen ( data
)- 1 ]= '\0' ;
1100 strcat ( excludecode
, data
);
1101 strcat ( excludecode
, ";" );
1110 int vercode ( char * code
)
1116 strcpy ( warea
, excludecode
);
1117 for ( z
= 0 ; z
<= excode
- 1 ; z
++) {
1118 getword ( cod
, warea
, ';' );
1119 if ( strcmp ( code
, cod
) == 0 )
1125 void fixnone ( char * str
)
1127 if ( strstr ( str
, " \n " ) != 0 )
1128 str
[ strlen ( str
)- 1 ]= '\0' ;
1129 if ( strcmp ( str
, "none" ) == 0 )
1135 #ifdef LEGACY_TESTVALIDUSERCHAR
1136 int testvaliduserchar ( char * user
)
1142 for ( y
= 0 ; y
< strlen ( UserInvalidChar
); y
++) {
1143 for ( x
= 0 ; x
< strlen ( user
); x
++) {
1144 if ( user
[ x
] == UserInvalidChar
[ y
])
1151 int testvaliduserchar ( char * user
)
1154 char * p_UserInvalidChar
= UserInvalidChar
;
1157 while ( * p_UserInvalidChar
) {
1160 if ( * p_UserInvalidChar
== * p_user
)
1164 p_UserInvalidChar
++ ;
1170 int compar ( const void * a
, const void * b
)
1171 { if ( *( int *) a
> *( int *) b
) return 1 ;
1172 if ( *( int *) a
< *( int *) b
) return - 1 ;
1176 int getnumlist ( char * buf
, numlist
* list
, const int len
, const int maxvalue
)
1177 { int i
, j
, d
, flag
, r1
, r2
;
1178 char * pbuf
, ** bp
, * strbufs
[ 24 ];
1181 strtok ( buf
, " \t " );
1182 for ( * bp
= strtok ( NULL
, "," ), list
-> len
= 0 ; * bp
; * bp
= strtok ( NULL
, "," ) )
1183 { if ( ++ bp
>= & strbufs
[ 24 ] )
1190 for ( i
= 0 ; i
< list
-> len
; i
++ )
1191 { if ( strstr ( strbufs
[ i
], "-" ) != 0 )
1192 { pbuf
= strbufs
[ i
];
1193 strtok ( pbuf
, "-" );
1194 pbuf
= strtok ( NULL
, "\0" );
1195 r1
= atoi ( strbufs
[ i
] );
1196 if ( ( r2
= atoi ( pbuf
) ) >= maxvalue
|| r1
>= r2
)
1198 if ( i
+ d
+ ( r2
- r1
) + 1 <= len
)
1199 { for ( j
= r1
; j
<= r2
; j
++ )
1200 list
-> list
[ i
+ d
++ ] = j
;
1205 if ( ( list
-> list
[ i
+ d
] = atoi ( strbufs
[ i
] ) ) >= maxvalue
)
1209 qsort ( list
-> list
, list
-> len
, sizeof ( int ), compar
);
1212 for ( i
= 0 ; i
< list
-> len
- 1 ; i
++ )
1213 if ( list
-> list
[ i
] == list
-> list
[ i
+ 1 ] )
1214 { for ( j
= i
+ 1 ; j
< list
-> len
; j
++ )
1215 list
-> list
[ j
- 1 ] = list
-> list
[ j
];
1225 void show_info ( FILE * fp_ou
)
1227 if ( strcmp ( ShowSargInfo
, "yes" ) != 0 ) return ;
1228 zdate ( ftime
, DateFormat
);
1229 fprintf ( fp_ou
, "<center><table><tr><td><br><br></td><td class= \" info \" >%s <a href='%s'><font class= \" info \" >%s-%s</font></a> %s %s</td></tr></table></center> \n " , text
[ 108 ], URL
, PGM
, VERSION
, text
[ 109 ], ftime
);
1232 void show_sarg ( FILE * fp_ou
, char * ind
)
1234 if ( strcmp ( ShowSargLogo
, "yes" ) == 0 ) fprintf ( fp_ou
, "<center><table cellpadding=0 cellspacing=0> \n <tr><th class= \" logo \" ><a href= \" http://sarg.sourceforge.net \" ><img src= \" %s/images/sarg.png \" border= \" 0 \" align= \" absmiddle \" title= \" SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki \" ></a> <font class= \" logo \" >Squid Analysis Report Generator</font></th></tr> \n <tr><th class= \" title \" > </th></tr> \n <table> \n " , ind
);
1237 get_size ( char * path
, char * file
)
1242 sprintf ( cmd
, "du -skh %s%s" , path
, file
);
1243 fp
= popen ( cmd
, "r" );
1244 fgets ( response
, 255 , fp
);
1245 getword ( val5
, response
, ' \t ' );
1252 void write_html_header ( FILE * fp_ou
, char * ind
)
1254 fprintf ( fp_ou
, "<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN \" \" http://www.w3.org/TR/html4/loose.dtd \" > \n <html> \n <head> \n <meta http-equiv= \" Content-Type \" content= \" text/html; charset=%s \" > \n </head> \n " , CharSet
);
1256 fprintf ( fp_ou
, "<body style= \" font-family:%s;font-size:%s;background-color:%s;background-image:url(%s) \" > \n " , FontFace
, TitleFontSize
, BgColor
, BgImage
);
1257 if ( strlen ( LogoImage
) > 0 ) fprintf ( fp_ou
, "<center><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n <tr><th class= \" logo \" ><img src='%s' border=0 align=absmiddle width=%s height=%s> %s</th></tr> \n <tr><td height= \" 5 \" ></td></tr> \n </table> \n " , LogoImage
, Width
, Height
, LogoText
);
1258 show_sarg ( fp_ou
, ind
);
1259 fprintf ( fp_ou
, "<center><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n <tr><th class= \" title \" >%s</th></tr> \n </table></center> \n <center><table cellpadding= \" 1 \" cellspacing= \" 2 \" > \n <tr><td></td><td></td></tr> \n " , Title
);
1264 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1265 printf ( "SARG: MALICIUS CODE DETECTED. \n " );
1266 printf ( "SARG: I think someone is trying to execute arbitrary code in your system using sarg. \n " );
1267 printf ( "SARG: please review your access.log and/or your useragent.log file. \n " );
1268 printf ( "SARG: process stoped. No actions taken. \n " );
1269 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1271 system ( "rm -rf /tmp/sarg" );
1272 sprintf ( tmp4
, "rm -rf %s" , dirname
);
1274 system ( "rm -rf /tmp/sarg" );
1280 char url_module ( char * url
, char * w2
)
1288 for ( x
= strlen ( url
)- 1 ; x
>= 0 ; x
--) {
1289 if ( url
[ x
] == '/' || y
> 255 ) break ;
1295 for ( x
= strlen ( w
)- 1 ; x
>= 0 ; x
--) {
1304 void write_html_trailer ( FILE * fp_ou
)
1306 fputs ( "</table></center> \n " , fp_ou
);
1307 zdate ( ftime
, DateFormat
);
1309 fputs ( "</body> \n </html> \n " , fp_ou
);
1314 printf ( "SARG Version: %s \n " , VERSION
);