]>
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 int getword ( char * word
, int limit
, char * line
, int stop
)
60 /*if(strlen(line) < 3) {
65 for ( x
= 0 ;(( line
[ x
]) && ( line
[ x
] != stop
)); x
++) {
67 printf ( "SARG: getword loop detected after %d bytes. \n " , x
);
68 printf ( "SARG: Record= \" %s \"\n " , line
);
69 printf ( "SARG: searching for \' x%x \'\n " , stop
);
70 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
71 if ( limit
> 0 ) word
[ limit
- 1 ]= '\0' ;
82 while (( line
[ y
++] = line
[ x
++]));
86 int getword_multisep ( char * word
, int limit
, char * line
, int stop
)
90 /*if(strlen(line) < 3) {
95 for ( x
= 0 ;(( line
[ x
]) && ( line
[ x
] != stop
)); x
++) {
97 printf ( "SARG: getword_multisep loop detected. \n " );
98 printf ( "SARG: Record= \" %s \"\n " , line
);
99 printf ( "SARG: searching for \' x%x \'\n " , stop
);
100 printf ( "SARG: searching for \' x%x \'\n " , stop
);
101 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
102 if ( limit
> 0 ) word
[ limit
- 1 ]= '\0' ;
110 while ( line
[ x
] && line
[ x
]== stop
) ++ x
;
113 while (( line
[ y
++] = line
[ x
++]));
117 #if 0 //this function seems unused
118 char * getword2 ( char * word
, int limit
, char * line
, int stop
)
122 if ( strlen ( line
) < 3 ) {
127 // printf( "IN Buffer <%s>\n" , line ) ;
128 for ( x
= 0 ;(( line
[ x
]) && ( line
[ x
] != stop
&& limit
)); x
++ , limit
-- ) word
[ x
] = line
[ x
];
130 printf ( "SARG: getword2 loop detected. \n " );
131 printf ( "SARG: Buffer= \" %s \"\n " , line
);
132 printf ( "SARG: searching for \' x%x \'\n " , stop
);
133 printf ( "SARG: Maybe you have a broken record or garbage in your access.log file. \n " );
138 // printf( "Value <%s>\n" , word ) ;
139 // printf( "OUT Buffer <%s>\n" , line+x+1 ) ;
140 return ( line
+ x
+ 1 ) ;
144 int getword3 ( char * word
, int limit
, char * line
, int stop
)
148 for ( x
= 0 ; x
< limit
&& ( line
[ x
] && ( line
[ x
] != stop
)) ; x
++) word
[ x
] = line
[ x
];
150 printf ( "SARG: getword3 loop detected after %d bytes. \n " , x
);
151 printf ( "SARG: Buffer= \" %s \"\n " , line
);
152 printf ( "SARG: searching for \' x%x \'\n " , stop
);
153 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
159 while (( line
[ y
++] = line
[ x
++]));
164 #ifdef LEGACY_MY_ATOLL
166 // BMG (bguillory@email.com)
168 long long int my_atoll ( const char * nptr
)
169 #define MAXLLL 30 //maximum number of digits in long long (a guess)
172 long long int returnval
= 0 ;
177 // Soak up all the white space
178 while ( isspace ( nptr
[ offset
])) {
182 //For each character left to right
183 //change the character to a single digit
184 //multiply what we had before by 10 and add the new digit
185 for ( x
= offset
; x
<= MAXLLL
+ offset
&& isdigit ( nptr
[ x
]); x
++) {
186 sprintf ( one_digit
, "%c" , nptr
[ x
]); //I don't know how else to do this
187 returnval
= ( returnval
* 10 ) + atoi ( one_digit
);
196 #define MAXLLL 30 //maximum number of digits in long long (a guess)
197 long long int my_atoll ( const char * nptr
)
199 long long int returnval
= 0 ;
201 int max_digits
= MAXLLL
;
203 // Soak up all the white space
204 while ( isspace ( * t
)) {
208 //For each character left to right
209 //change the character to a single digit
210 //multiply what we had before by 10 and add the new digit
212 for ( ; -- max_digits
&& isdigit ( * t
) ; t
++ )
214 returnval
= ( returnval
* 10 ) + ( * t
- '0' ) ;
224 void my_mkdir ( char * name
)
230 if ( strncmp ( name
, "." , 1 ) == 0 || strncmp ( name
, "/" , 1 ) != 0 ) {
231 fprintf ( stderr
, "SARG: Invalid path (%s). Please, use absolute paths only. \n " , name
);
232 fprintf ( stderr
, "SARG: process aborted. \n " );
238 if ( getword_multisep ( w1
, sizeof ( w1
), w0
, '/' )< 0 ) {
239 printf ( "SARG: Maybe you have a broken record or garbage in the directory name %s. \n " , name
);
242 while ( strchr ( w0
, '/' )) {
243 if ( getword_multisep ( w1
, sizeof ( w1
), w0
, '/' )< 0 ) {
244 printf ( "SARG: Maybe you have a broken record or garbage in the directory name %s. \n " , name
);
248 if ( access ( w2
, R_OK
) != 0 ) {
250 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w2
, strerror ( errno
));
251 fprintf ( stderr
, "SARG: process aborted. \n " );
258 if ( access ( w2
, R_OK
) != 0 ) {
260 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w2
, strerror ( errno
));
261 fprintf ( stderr
, "SARG: process aborted. \n " );
268 void my_lltoa ( unsigned long long int n
, char s
[], int len
)
274 s
[ i
++] = ( n
% 10 ) + '0' ;
275 } while (( n
/= 10 ) > 0 );
279 for ( i
= 0 , j
= strlen ( s
)- 1 ; i
< j
; i
++, j
--)
288 bzero ( ww
, sizeof ( ww
));
293 strncat ( ww
, s
, i
> sizeof ( ww
)? sizeof ( ww
): i
);
300 void builddia ( char * dia
, char * mes
, char * ano
, char * df
, char * wdata
)
306 if ( strlen ( dia
) < 1 ) return ;
311 for ( x
= 0 ; x
< 12 ; x
++) {
312 if ( strcmp ( mtab1
[ x
], mes
) == 0 ) {
313 strncpy ( nmes
, mtab2
[ x
], sizeof ( nmes
)- 1 );
314 nmes
[ sizeof ( nmes
)- 1 ]= 0 ;
319 snprintf ( wdata
, 9 , "%s%s%s" , ano
, nmes
, dia
);
321 if ( strncmp ( df
, "u" , 1 ) != 0 )
322 snprintf ( ndia
, sizeof ( ndia
), "%s/%s/%s" , dia
, nmes
, ano
);
324 snprintf ( ndia
, sizeof ( ndia
), "%s/%s/%s" , nmes
, dia
, ano
);
331 void buildymd ( char * dia
, char * mes
, char * ano
, char * wdata
)
338 for ( x
= 0 ; x
< 12 ; x
++) {
339 if ( strcmp ( mtab1
[ x
], mes
) == 0 )
340 strcpy ( nmes
, mtab2
[ x
]);
343 sprintf ( wdata
, "%s%s%s" , ano
, nmes
, dia
);
348 void conv_month ( char * month
)
352 for ( x
= 0 ; x
< 12 ; x
++) {
353 if ( strcmp ( mtab1
[ x
], month
) == 0 )
354 strcpy ( month
, mtab2
[ x
]);
360 void conv_month_name ( char * month
)
364 for ( x
= 0 ; x
< 12 ; x
++) {
365 if ( strcmp ( mtab2
[ x
], month
) == 0 )
366 strcpy ( month
, mtab1
[ x
]);
371 void name_month ( char * month
, int month_len
)
373 int x
, z
= atoi ( month
)- 1 ;
380 if ( getword_multisep ( w
, sizeof ( w
), m
, ',' )< 0 ) {
381 printf ( "SARG: Maybe you have a broken record or garbage in the names of the months. \n " );
384 if ( getword_multisep ( month
, month_len
, m
, ',' )< 0 ) {
385 printf ( "SARG: Maybe you have a broken record or garbage in the name of the months. \n " );
391 void fixper ( char * tbuf
, char * period
, char * duntil
)
395 char dia
[ 5 ], mes
[ 5 ], ano
[ 5 ];
400 strncpy ( dia
, duntil
+ 6 , 2 );
402 strncpy ( mes
, duntil
+ 4 , 2 );
404 strncpy ( ano
, duntil
, 4 );
407 for ( x
= 0 ; x
< 12 ; x
++) {
408 if ( strcmp ( mtab2
[ x
], mes
) == 0 )
409 strcpy ( mes
, mtab1
[ x
]);
412 if ( strcmp ( df
, "e" ) == 0 )
413 sprintf ( warea
, "%s%s%s" , dia
, mes
, ano
);
414 if ( strcmp ( df
, "u" ) == 0 )
415 sprintf ( warea
, "%s%s%s" , ano
, mes
, dia
);
417 strcat ( period
, warea
);
421 void debuga ( char * msg
)
423 fprintf ( stderr
, "SARG: %s \n " , msg
);
428 void debugaz ( char * head
, char * msg
)
430 fprintf ( stderr
, "SARG: (util) %s=%s \n " , head
, msg
);
437 char n1
[ MAXLEN
], n2
[ MAXLEN
], n3
[ MAXLEN
];
444 if ( strstr ( ip
, "." ) != 0 ) {
450 if ( getword ( n1
, sizeof ( n1
), wip
, '.' )< 0 || getword ( n2
, sizeof ( n2
), wip
, '.' )< 0 || getword ( n3
, sizeof ( n3
), wip
, '.' )< 0 ) {
451 printf ( "SARG: Maybe you have a broken record or garbage in the IP address. \n " );
455 if ( getword ( n1
, sizeof ( n1
), wip
, '_' )< 0 || getword ( n2
, sizeof ( n2
), wip
, '_' )< 0 || getword ( n3
, sizeof ( n3
), wip
, '_' )< 0 ) {
456 printf ( "SARG: Maybe you have a broken record or garbage in the IP address. \n " );
461 sprintf ( ip
, "%s%s%s%s%s%s%s" , n1
, sep
, n2
, sep
, n3
, sep
, wip
);
466 char * fixnum ( long long int value
, int n
)
470 char buf
[ MAXLEN
* 2 ];
472 static char ret
[ MAXLEN
* 2 ];
474 register int i
, j
, k
;
475 static char abbrev
[ 30 ];
477 my_lltoa ( value
, num
, 0 );
479 if ( strcmp ( DisplayedValues
, "abbreviation" ) == 0 ) {
481 sprintf ( abbrev
, "%s" , num
);
482 if ( strlen ( num
) == 4 || strlen ( num
) == 7 || strlen ( num
) == 10 || strlen ( num
) == 13 ) {
483 snprintf ( abbrev
, 2 , "%s" , num
);
484 strncat ( abbrev
, "." , 1 );
485 strncat ( abbrev
, num
+ 1 , 2 );
486 if (! n
) return ( abbrev
);
488 strncat ( abbrev
, "K" , 1 );
489 else if ( strlen ( num
) == 7 )
490 strncat ( abbrev
, "M" , 1 );
491 else if ( strlen ( num
) == 10 )
492 strncat ( abbrev
, "G" , 1 );
493 else if ( strlen ( num
) == 13 )
494 strncat ( abbrev
, "T" , 1 );
496 if ( strlen ( num
) == 5 || strlen ( num
) == 8 || strlen ( num
) == 11 || strlen ( num
) == 14 ) {
497 snprintf ( abbrev
, 3 , "%s" , num
);
498 strncat ( abbrev
, "." , 1 );
499 strncat ( abbrev
, num
+ 2 , 2 );
500 if (! n
) return ( abbrev
);
502 strncat ( abbrev
, "K" , 1 );
503 else if ( strlen ( num
) == 8 )
504 strncat ( abbrev
, "M" , 1 );
505 else if ( strlen ( num
) == 11 )
506 strncat ( abbrev
, "G" , 1 );
507 else if ( strlen ( num
) == 14 )
508 strncat ( abbrev
, "T" , 1 );
510 if ( strlen ( num
) == 6 || strlen ( num
) == 9 || strlen ( num
) == 12 || strlen ( num
) == 15 ) {
511 snprintf ( abbrev
, 4 , "%s" , num
);
512 strncat ( abbrev
, "." , 1 );
513 strncat ( abbrev
, num
+ 3 , 2 );
514 if (! n
) return ( abbrev
);
516 strncat ( abbrev
, "K" , 1 );
517 else if ( strlen ( num
) == 9 )
518 strncat ( abbrev
, "M" , 1 );
519 else if ( strlen ( num
) == 12 )
520 strncat ( abbrev
, "G" , 1 );
521 else if ( strlen ( num
) == 15 )
522 strncat ( abbrev
, "T" , 1 );
528 bzero ( buf
, MAXLEN
* 2 );
534 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
535 if ( k
== 2 && i
!= 0 ) {
538 if ( strcmp ( UseComma
, "yes" ) == 0 )
540 else pbuf
[ j
++] = '.' ;
550 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
559 char * fixnum2 ( long long int value
, int n
)
563 char buf
[ MAXLEN
* 2 ];
565 static char ret
[ MAXLEN
* 2 ];
567 register int i
, j
, k
;
568 static char abbrev
[ 30 ];
570 my_lltoa ( value
, num
, 0 );
571 bzero ( buf
, MAXLEN
* 2 );
577 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
578 if ( k
== 2 && i
!= 0 ) {
581 if ( strcmp ( UseComma
, "yes" ) == 0 )
583 else pbuf
[ j
++] = '.' ;
593 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
603 void buildhref ( char * href
)
607 if ( strcmp ( href
, "./" ) == 0 ){
609 strcat ( href
, "<a href='" );
613 href
[ strlen ( href
)- 1 ]= '\0' ;
614 sprintf ( whref
, "%s" , strrchr ( href
, '/' ));
616 strcpy ( href
, "<a href='" );
625 char * buildtime ( long long int elap
)
628 int num
= elap
/ 1000 ;
637 min
=( num
% 3600 ) / 60 ;
639 sprintf ( buf
, "%02d:%02d:%02d" , hor
, min
, sec
);
646 void obtdate ( char * dirname
, char * name
, char * data
)
652 sprintf ( wdir
, "%s%s/sarg-date" , dirname
, name
);
653 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
654 sprintf ( wdir
, "%s%s/date" , dirname
, name
);
655 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
661 fgets ( data
, 80 , fp_in
);
663 data
[ strlen ( data
)- 1 ]= '\0' ;
670 void obtuser ( char * dirname
, char * name
, char * tuser
)
676 sprintf ( wdir
, "%s%s/sarg-users" , dirname
, name
);
677 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
678 sprintf ( wdir
, "%s%s/users" , dirname
, name
);
679 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
685 fgets ( tuser
, 20 , fp_in
);
686 tuser
[ strlen ( tuser
)- 1 ]= '\0' ;
694 void obttotal ( char * dirname
, char * name
, char * tbytes
, char * tuser
, char * media
)
700 long long int wtuser
= 0 ;
702 sprintf ( wdir
, "%s%s/sarg-general" , dirname
, name
);
703 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
704 sprintf ( wdir
, "%s%s/general" , dirname
, name
);
705 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
711 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
712 if ( getword_multisep ( warea
, sizeof ( warea
), buf
, ' ' )< 0 ) {
713 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
716 if ( strcmp ( warea
, "TOTAL" ) != 0 )
718 if ( getword_multisep ( warea
, sizeof ( warea
), buf
, ' ' )< 0 ) {
719 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
722 if ( getword_multisep ( warea
, sizeof ( warea
), buf
, ' ' )< 0 ) {
723 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
726 twork
= my_atoll ( warea
);
727 sprintf ( tbytes
, "%s" , fixnum ( twork
, 1 ));
731 if ( tuser
[ 0 ] == '\0' ) {
733 sprintf ( media
, "%s" , "0" );
737 wtuser
= my_atoll ( tuser
);
738 med
= my_atoll ( warea
) / wtuser
;
739 sprintf ( media
, "%s" , fixnum ( med
, 1 ));
746 //void gperiod(char *dirname, const char *period)
752 char wdirname
[ MAXLEN
];
754 strcpy ( wdirname
, dirname
);
755 strcat ( wdirname
, "/" );
756 strcat ( wdirname
, "sarg-period" );
758 if (( fp_ou
= fopen ( wdirname
, "w" ))== NULL
){
759 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
767 debuga (( char *) text
[ 50 ]);
773 void vrfydir ( char * dir
, char * per1
, char * addr
, char * site
, char * us
, char * form
)
775 FILE * img_in
, * img_ou
;
780 char dirname2
[ MAXLEN
];
783 struct dirent
* direntp
;
786 if ( strcmp ( IndexTree
, "date" ) == 0 ) {
793 if ( strncmp ( df
, "u" , 1 ) == 0 ) {
794 strncpy ( y1
, period
, 4 );
795 strncpy ( y2
, period
+ 10 , 4 );
796 strncpy ( m1
, period
+ 4 , 3 );
797 strncpy ( m2
, period
+ 14 , 3 );
798 strncpy ( d1
, period
+ 7 , 2 );
799 strncpy ( d2
, period
+ 17 , 2 );
800 } else if ( strncmp ( df
, "e" , 1 ) == 0 ) {
801 strncpy ( d1
, period
+ 0 , 2 );
802 strncpy ( d2
, period
+ 10 , 2 );
803 strncpy ( m1
, period
+ 2 , 3 );
804 strncpy ( m2
, period
+ 12 , 3 );
805 strncpy ( y1
, period
+ 5 , 4 );
806 strncpy ( y2
, period
+ 15 , 4 );
811 sprintf ( wdir
, "%s%s" , outdir
, y1
);
812 if ( strcmp ( y1
, y2
) != 0 ) {
814 strncat ( wdir
, y2
, strlen ( y2
));
816 if ( access ( wdir
, R_OK
) != 0 )
820 strncat ( wdir
, m1
, strlen ( m1
));
821 if ( strcmp ( m1
, m2
) != 0 ) {
823 strncat ( wdir
, m2
, strlen ( m2
));
825 if ( access ( wdir
, R_OK
) != 0 )
829 strncat ( wdir
, d1
, strlen ( d1
));
830 if ( strcmp ( d1
, d2
) != 0 ) {
832 strncat ( wdir
, d2
, strlen ( d2
));
835 sprintf ( wdir
, "%s" , dir
);
841 if ( strlen ( addr
) > 0 ) {
845 if ( strlen ( site
) > 0 ) {
850 if ( strcmp ( dirname
, wdir
) != 0 )
851 strcpy ( dirname
, wdir
);
853 if ( strcmp ( IndexTree
, "date" ) != 0 ) {
854 strcpy ( dirname2
, dirname
);
855 if ( strcmp ( OverwriteReport
, "no" ) == 0 ) {
857 if ( access ( wdir
, R_OK
) == 0 ) {
858 sprintf ( wdir
, "%s.%d" , dirname
, num
);
859 sprintf ( per2
, "%s.%d" , per1
, num
);
868 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
869 rename ( dirname2
, wdir
);
872 if ( access ( dir
, R_OK
) == 0 ) {
873 sprintf ( csort
, "rm -r %s" , dir
);
879 strcpy ( dirname2
, wdir
);
880 if ( strcmp ( OverwriteReport
, "no" ) == 0 ) {
882 if ( access ( wdir
, R_OK
) == 0 ) {
883 sprintf ( wdir
, "%s.%d" , dirname2
, num
);
884 sprintf ( per2
, "%s.%d" , per1
, num
);
893 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
894 rename ( dirname2
, wdir
);
895 strcpy ( dirname2
, wdir
);
898 if ( access ( wdir
, R_OK
) == 0 ) {
899 sprintf ( csort
, "rm -r %s" , wdir
);
904 if ( access ( wdir
, R_OK
) != 0 )
908 strcpy ( dirname2
, wdir
);
909 sprintf ( images
, "%simages" , outdir
);
912 sprintf ( wdir
, "date \" +%%a %%b %%d %%H:%%M:%%S %%Z %%Y \" >%s/sarg-date" , dirname
);
913 cstatus
= system ( wdir
);
914 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
915 fprintf ( stderr
, "SARG: command return status %d \n " , WEXITSTATUS ( cstatus
));
916 fprintf ( stderr
, "SARG: command: %s \n " , wdir
);
920 sprintf ( per2
, "%s/images" , SYSCONFDIR
);
922 dirp
= opendir ( per2
);
924 fprintf ( stderr
, "SARG: (util) %s %s: %s \n " , "Can't open directory" , per2
, strerror ( errno
));
927 while (( direntp
= readdir ( dirp
)) != NULL
){
928 if ( strncmp ( direntp
-> d_name
, "." , 1 ) == 0 )
930 sprintf ( val10
, "%s/%s" , per2
, direntp
-> d_name
);
931 sprintf ( val11
, "%s/%s" , images
, direntp
-> d_name
);
932 img_in
= fopen ( val10
, "rb" );
934 img_ou
= fopen ( val11
, "wb" );
944 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open/create file" , val11
, strerror ( errno
));
946 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open file" , val10
, strerror ( errno
));
950 ( void ) rewinddir ( dirp
);
951 ( void ) closedir ( dirp
);
959 void strip_latin ( char * line
)
964 while ( strstr ( line
, "&" ) != 0 ){
965 if ( getword_multisep ( warea
, sizeof ( warea
), line
, '&' )< 0 ) {
966 printf ( "SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities. \n " );
969 strncat ( warea
, line
, 1 );
970 if ( getword_multisep ( buf
, sizeof ( buf
), line
, ';' )< 0 ) {
971 printf ( "SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities. \n " );
982 void zdate ( char * ftime
, char * DateFormat
)
989 local
= localtime (& t
);
990 if ( strcmp ( DateFormat
, "u" ) == 0 )
991 strftime ( ftime
, 127 , "%b/%d/%Y %H:%M" , local
);
992 if ( strcmp ( DateFormat
, "e" ) == 0 )
993 strftime ( ftime
, 127 , "%d/%b/%Y-%H:%M" , local
);
994 if ( strcmp ( DateFormat
, "w" ) == 0 )
995 strftime ( ftime
, 127 , "%V-%H-%M" , local
);
1001 char * fixtime ( long int elap
)
1004 int num
= elap
/ 1000 ;
1008 static char buf
[ 12 ];
1010 if ( strcmp ( datetimeby
, "bytes" ) == 0 ) {
1011 sprintf ( buf
, "%s" , fixnum ( elap
, 1 ));
1018 sprintf ( buf
, "00:00:%02ld" , elap
);
1023 min
=( num
% 3600 ) / 60 ;
1026 sprintf ( buf
, "%01d:%02d:%02d" , hor
, min
, sec
);
1028 if ( strcmp ( buf
, "0:00:00" ) == 0 )
1036 void date_from ( char * date
, char * dfrom
, char * duntil
)
1045 static char wdate
[ 50 ];
1049 if ( strstr ( wdate
, "-" ) == 0 ) {
1055 if ( getword ( diaf
, sizeof ( diaf
), wdate
, '/' )< 0 || getword ( mesf
, sizeof ( mesf
), wdate
, '/' )< 0 || getword ( anof
, sizeof ( anof
), wdate
, '-' )< 0 ||
1056 getword ( diau
, sizeof ( diau
), wdate
, '/' )< 0 || getword ( mesu
, sizeof ( mesu
), wdate
, '/' )< 0 || getword ( anou
, sizeof ( anou
), wdate
, 0 )< 0 ) {
1057 printf ( "SARG: Maybe you have a broken record or garbage in a date. \n " );
1061 sprintf ( dfrom
, "%s%s%s" , anof
, mesf
, diaf
);
1062 sprintf ( duntil
, "%s%s%s" , anou
, mesu
, diau
);
1067 char * strlow ( char * string
)
1073 for ( s
= string
; * s
; ++ s
)
1083 char * strup ( char * string
)
1089 for ( s
= string
; * s
; ++ s
)
1097 char * subs ( char * str
, char * from
, char * to
)
1101 unsigned int ss
, st
;
1103 if ( strstr ( str
, from
) == 0 )
1104 return ( char *) str
;
1106 ss
= strlen ( str
); st
= strlen ( to
) + 10 ;
1108 if (( ret
=( char *) malloc ( ss
+ st
))== NULL
)
1110 fprintf ( stderr
, "SARG: %s (%d): \n " , text
[ 59 ], ss
+ st
);
1116 tmp
= strstr ( str
, from
);
1117 if ( tmp
== ( char *) NULL
)
1118 return ( char *) NULL
;
1119 strncpy ( ret
, str
, ss
- strlen ( tmp
));
1121 strcat ( ret
, ( tmp
+ strlen ( from
)));
1122 return ( char *) ret
;
1126 void removetmp ( char * outdir
)
1132 if ( strcmp ( RemoveTempFiles
, "yes" ) != 0 ) {
1136 sprintf ( msg
, "%s: sarg-general, sarg-period" , text
[ 82 ]);
1139 sprintf ( warea
, "%s/sarg-general" , outdir
);
1140 if (( fp_in
= fopen ( warea
, "r" ))== NULL
){
1141 fprintf ( stderr
, "===SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1144 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
1145 if ( strncmp ( buf
, "TOTAL" , 5 ) == 0 )
1149 if (( fp_in
= fopen ( warea
, "w" ))== NULL
){
1150 fprintf ( stderr
, "SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1155 sprintf ( warea
, "%s/sarg-period" , outdir
);
1162 void load_excludecodes ()
1168 if (( fp_in
= fopen ( ExcludeCodes
, "r" ))== NULL
) {
1169 fprintf ( stderr
, "SARG: (util) Cannot open file: %s (exclude_codes) \n " , ExcludeCodes
);
1173 while ( fgets ( data
, 80 , fp_in
)!= NULL
) {
1174 data
[ strlen ( data
)- 1 ]= '\0' ;
1175 strcat ( excludecode
, data
);
1176 strcat ( excludecode
, ";" );
1185 int vercode ( char * code
)
1191 strcpy ( warea
, excludecode
);
1192 for ( z
= 0 ; z
<= excode
- 1 ; z
++) {
1193 if ( getword_multisep ( cod
, sizeof ( cod
), warea
, ';' )< 0 ) {
1194 printf ( "SARG: Maybe you have a broken record or garbage in a version code. \n " );
1197 if ( strcmp ( code
, cod
) == 0 )
1203 void fixnone ( char * str
)
1205 if ( strstr ( str
, " \n " ) != 0 )
1206 str
[ strlen ( str
)- 1 ]= '\0' ;
1207 if ( strcmp ( str
, "none" ) == 0 )
1213 #ifdef LEGACY_TESTVALIDUSERCHAR
1214 int testvaliduserchar ( char * user
)
1220 for ( y
= 0 ; y
< strlen ( UserInvalidChar
); y
++) {
1221 for ( x
= 0 ; x
< strlen ( user
); x
++) {
1222 if ( user
[ x
] == UserInvalidChar
[ y
])
1229 int testvaliduserchar ( char * user
)
1232 char * p_UserInvalidChar
= UserInvalidChar
;
1235 while ( * p_UserInvalidChar
) {
1238 if ( * p_UserInvalidChar
== * p_user
)
1242 p_UserInvalidChar
++ ;
1248 int compar ( const void * a
, const void * b
)
1249 { if ( *( int *) a
> *( int *) b
) return 1 ;
1250 if ( *( int *) a
< *( int *) b
) return - 1 ;
1254 int getnumlist ( char * buf
, numlist
* list
, const int len
, const int maxvalue
)
1255 { int i
, j
, d
, flag
, r1
, r2
;
1256 char * pbuf
, ** bp
, * strbufs
[ 24 ];
1259 strtok ( buf
, " \t " );
1260 for ( * bp
= strtok ( NULL
, "," ), list
-> len
= 0 ; * bp
; * bp
= strtok ( NULL
, "," ) )
1261 { if ( ++ bp
>= & strbufs
[ 24 ] )
1268 for ( i
= 0 ; i
< list
-> len
; i
++ )
1269 { if ( strstr ( strbufs
[ i
], "-" ) != 0 )
1270 { pbuf
= strbufs
[ i
];
1271 strtok ( pbuf
, "-" );
1272 pbuf
= strtok ( NULL
, "\0" );
1273 r1
= atoi ( strbufs
[ i
] );
1274 if ( ( r2
= atoi ( pbuf
) ) >= maxvalue
|| r1
>= r2
)
1276 if ( i
+ d
+ ( r2
- r1
) + 1 <= len
)
1277 { for ( j
= r1
; j
<= r2
; j
++ )
1278 list
-> list
[ i
+ d
++ ] = j
;
1283 if ( ( list
-> list
[ i
+ d
] = atoi ( strbufs
[ i
] ) ) >= maxvalue
)
1287 qsort ( list
-> list
, list
-> len
, sizeof ( int ), compar
);
1290 for ( i
= 0 ; i
< list
-> len
- 1 ; i
++ )
1291 if ( list
-> list
[ i
] == list
-> list
[ i
+ 1 ] )
1292 { for ( j
= i
+ 1 ; j
< list
-> len
; j
++ )
1293 list
-> list
[ j
- 1 ] = list
-> list
[ j
];
1303 void show_info ( FILE * fp_ou
)
1305 if ( strcmp ( ShowSargInfo
, "yes" ) != 0 ) return ;
1306 zdate ( ftime
, DateFormat
);
1307 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
);
1310 void show_sarg ( FILE * fp_ou
, char * ind
)
1312 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
);
1315 int get_size ( char * path
, char * file
)
1320 sprintf ( cmd
, "du -skh %s%s" , path
, file
);
1321 fp
= popen ( cmd
, "r" );
1322 fgets ( response
, 255 , fp
);
1323 if ( getword_multisep ( val5
, sizeof ( val5
), response
, ' \t ' )< 0 ) {
1324 printf ( "SARG: Maybe you have a broken record or garbage in your %s command. \n " , cmd
);
1333 void write_html_header ( FILE * fp_ou
, char * ind
)
1335 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
);
1337 fprintf ( fp_ou
, "<body style= \" font-family:%s;font-size:%s;background-color:%s;background-image:url(%s) \" > \n " , FontFace
, TitleFontSize
, BgColor
, BgImage
);
1338 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
);
1339 show_sarg ( fp_ou
, ind
);
1340 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
);
1345 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1346 printf ( "SARG: MALICIUS CODE DETECTED. \n " );
1347 printf ( "SARG: I think someone is trying to execute arbitrary code in your system using sarg. \n " );
1348 printf ( "SARG: please review your access.log and/or your useragent.log file. \n " );
1349 printf ( "SARG: process stoped. No actions taken. \n " );
1350 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1352 system ( "rm -rf /tmp/sarg" );
1353 sprintf ( tmp4
, "rm -rf %s" , dirname
);
1355 system ( "rm -rf /tmp/sarg" );
1361 char url_module ( char * url
, char * w2
)
1369 for ( x
= strlen ( url
)- 1 ; x
>= 0 ; x
--) {
1370 if ( url
[ x
] == '/' || y
> 255 ) break ;
1376 for ( x
= strlen ( w
)- 1 ; x
>= 0 ; x
--) {
1385 void write_html_trailer ( FILE * fp_ou
)
1387 fputs ( "</table></center> \n " , fp_ou
);
1388 zdate ( ftime
, DateFormat
);
1390 fputs ( "</body> \n </html> \n " , fp_ou
);
1395 printf ( "SARG Version: %s \n " , VERSION
);