]>
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"
30 #include "include/defs.h"
32 static char mtab1
[ 12 ][ 4 ]={ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" };
33 static char mtab2
[ 12 ][ 3 ]={ "01" , "02" , "03" , "04" , "05" , "06" , "07" , "08" , "09" , "10" , "11" , "12" };
35 /*void fgetword(char *word, char *line, int stop)
40 for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x];
47 tchar = strchr(line, stop);
48 if (tchar == NULL) strcpy(word, line);
51 difflen = tchar - line;
52 strncpy(word, line, difflen);
57 int getword ( char * word
, int limit
, char * line
, int stop
)
61 /*if(strlen(line) < 3) {
66 for ( x
= 0 ;(( line
[ x
]) && ( line
[ x
] != stop
)); x
++) {
68 printf ( "SARG: getword loop detected after %d bytes. \n " , x
);
69 printf ( "SARG: Record= \" %s \"\n " , line
);
70 printf ( "SARG: searching for \' x%x \'\n " , stop
);
71 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
72 if ( limit
> 0 ) word
[ limit
- 1 ]= '\0' ;
83 while (( line
[ y
++] = line
[ x
++]));
87 int getword_multisep ( char * word
, int limit
, char * line
, int stop
)
91 /*if(strlen(line) < 3) {
96 for ( x
= 0 ;(( line
[ x
]) && ( line
[ x
] != stop
)); x
++) {
98 printf ( "SARG: getword_multisep loop detected. \n " );
99 printf ( "SARG: Record= \" %s \"\n " , line
);
100 printf ( "SARG: searching for \' x%x \'\n " , stop
);
101 printf ( "SARG: searching for \' x%x \'\n " , stop
);
102 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
103 if ( limit
> 0 ) word
[ limit
- 1 ]= '\0' ;
111 while ( line
[ x
] && line
[ x
]== stop
) ++ x
;
114 while (( line
[ y
++] = line
[ x
++]));
118 #if 0 //this function seems unused
119 char * getword2 ( char * word
, int limit
, char * line
, int stop
)
123 if ( strlen ( line
) < 3 ) {
128 // printf( "IN Buffer <%s>\n" , line ) ;
129 for ( x
= 0 ;(( line
[ x
]) && ( line
[ x
] != stop
&& limit
)); x
++ , limit
-- ) word
[ x
] = line
[ x
];
131 printf ( "SARG: getword2 loop detected. \n " );
132 printf ( "SARG: Buffer= \" %s \"\n " , line
);
133 printf ( "SARG: searching for \' x%x \'\n " , stop
);
134 printf ( "SARG: Maybe you have a broken record or garbage in your access.log file. \n " );
139 // printf( "Value <%s>\n" , word ) ;
140 // printf( "OUT Buffer <%s>\n" , line+x+1 ) ;
141 return ( line
+ x
+ 1 ) ;
145 int getword3 ( char * word
, int limit
, char * line
, int stop
)
149 for ( x
= 0 ; x
< limit
&& ( line
[ x
] && ( line
[ x
] != stop
)) ; x
++) word
[ x
] = line
[ x
];
151 printf ( "SARG: getword3 loop detected after %d bytes. \n " , x
);
152 printf ( "SARG: Buffer= \" %s \"\n " , line
);
153 printf ( "SARG: searching for \' x%x \'\n " , stop
);
154 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
160 while (( line
[ y
++] = line
[ x
++]));
165 #ifdef LEGACY_MY_ATOLL
167 // BMG (bguillory@email.com)
169 long long int my_atoll ( const char * nptr
)
170 #define MAXLLL 30 //maximum number of digits in long long (a guess)
173 long long int returnval
= 0 ;
178 // Soak up all the white space
179 while ( isspace ( nptr
[ offset
])) {
183 //For each character left to right
184 //change the character to a single digit
185 //multiply what we had before by 10 and add the new digit
186 for ( x
= offset
; x
<= MAXLLL
+ offset
&& isdigit ( nptr
[ x
]); x
++) {
187 sprintf ( one_digit
, "%c" , nptr
[ x
]); //I don't know how else to do this
188 returnval
= ( returnval
* 10 ) + atoi ( one_digit
);
197 #define MAXLLL 30 //maximum number of digits in long long (a guess)
198 long long int my_atoll ( const char * nptr
)
200 long long int returnval
= 0 ;
201 const char * t
= nptr
;
202 int max_digits
= MAXLLL
;
204 // Soak up all the white space
205 while ( isspace ( * t
)) {
209 //For each character left to right
210 //change the character to a single digit
211 //multiply what we had before by 10 and add the new digit
213 for ( ; -- max_digits
&& isdigit ( * t
) ; t
++ )
215 returnval
= ( returnval
* 10 ) + ( * t
- '0' ) ;
225 void my_mkdir ( const char * name
)
231 if ( strncmp ( name
, "." , 1 ) == 0 || strncmp ( name
, "/" , 1 ) != 0 ) {
232 fprintf ( stderr
, "SARG: Invalid path (%s). Please, use absolute paths only. \n " , name
);
233 fprintf ( stderr
, "SARG: process aborted. \n " );
239 if ( getword_multisep ( w1
, sizeof ( w1
), w0
, '/' )< 0 ) {
240 printf ( "SARG: Maybe you have a broken record or garbage in the directory name %s. \n " , name
);
243 while ( strchr ( w0
, '/' )) {
244 if ( getword_multisep ( w1
, sizeof ( w1
), w0
, '/' )< 0 ) {
245 printf ( "SARG: Maybe you have a broken record or garbage in the directory name %s. \n " , name
);
249 if ( access ( w2
, R_OK
) != 0 ) {
251 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w2
, strerror ( errno
));
252 fprintf ( stderr
, "SARG: process aborted. \n " );
259 if ( access ( w2
, R_OK
) != 0 ) {
261 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w2
, strerror ( errno
));
262 fprintf ( stderr
, "SARG: process aborted. \n " );
269 void my_lltoa ( unsigned long long int n
, char s
[], int len
)
275 s
[ i
++] = ( n
% 10 ) + '0' ;
276 } while (( n
/= 10 ) > 0 );
280 for ( i
= 0 , j
= strlen ( s
)- 1 ; i
< j
; i
++, j
--)
289 bzero ( ww
, sizeof ( ww
));
294 strncat ( ww
, s
, i
> sizeof ( ww
)? sizeof ( ww
): i
);
301 void builddia ( char * dia
, const char * mes
, const char * ano
, const char * df
, char * wdata
)
307 if ( strlen ( dia
) < 1 ) return ;
312 for ( x
= 0 ; x
< 12 ; x
++) {
313 if ( strcmp ( mtab1
[ x
], mes
) == 0 ) {
314 strncpy ( nmes
, mtab2
[ x
], sizeof ( nmes
)- 1 );
315 nmes
[ sizeof ( nmes
)- 1 ]= 0 ;
320 snprintf ( wdata
, 9 , "%s%s%s" , ano
, nmes
, dia
);
322 if ( strncmp ( df
, "u" , 1 ) != 0 )
323 snprintf ( ndia
, sizeof ( ndia
), "%s/%s/%s" , dia
, nmes
, ano
);
325 snprintf ( ndia
, sizeof ( ndia
), "%s/%s/%s" , nmes
, dia
, ano
);
332 void buildymd ( const char * dia
, const char * mes
, const char * ano
, char * wdata
)
339 for ( x
= 0 ; x
< 12 ; x
++) {
340 if ( strcmp ( mtab1
[ x
], mes
) == 0 )
341 strcpy ( nmes
, mtab2
[ x
]);
344 sprintf ( wdata
, "%s%s%s" , ano
, nmes
, dia
);
349 void conv_month ( char * month
)
353 for ( x
= 0 ; x
< 12 ; x
++) {
354 if ( strcmp ( mtab1
[ x
], month
) == 0 )
355 strcpy ( month
, mtab2
[ x
]);
361 void conv_month_name ( char * month
)
365 for ( x
= 0 ; x
< 12 ; x
++) {
366 if ( strcmp ( mtab2
[ x
], month
) == 0 )
367 strcpy ( month
, mtab1
[ x
]);
372 void name_month ( char * month
, int month_len
)
374 int x
, z
= atoi ( month
)- 1 ;
381 if ( getword_multisep ( w
, sizeof ( w
), m
, ',' )< 0 ) {
382 printf ( "SARG: Maybe you have a broken record or garbage in the names of the months. \n " );
385 if ( getword_multisep ( month
, month_len
, m
, ',' )< 0 ) {
386 printf ( "SARG: Maybe you have a broken record or garbage in the name of the months. \n " );
392 void fixper ( char * tbuf
, char * period
, const char * duntil
)
396 char dia
[ 5 ], mes
[ 5 ], ano
[ 5 ];
401 strncpy ( dia
, duntil
+ 6 , 2 );
403 strncpy ( mes
, duntil
+ 4 , 2 );
405 strncpy ( ano
, duntil
, 4 );
408 for ( x
= 0 ; x
< 12 ; x
++) {
409 if ( strcmp ( mtab2
[ x
], mes
) == 0 ) {
410 strcpy ( mes
, mtab1
[ x
]);
415 if ( strcmp ( df
, "e" ) == 0 )
416 sprintf ( warea
, "%s%s%s" , dia
, mes
, ano
);
417 if ( strcmp ( df
, "u" ) == 0 )
418 sprintf ( warea
, "%s%s%s" , ano
, mes
, dia
);
420 strcat ( period
, warea
);
424 void debuga ( const char * msg
)
426 fprintf ( stderr
, "SARG: %s \n " , msg
);
431 void debugaz ( const char * head
, const char * msg
)
433 fprintf ( stderr
, "SARG: (util) %s=%s \n " , head
, msg
);
440 char n1
[ MAXLEN
], n2
[ MAXLEN
], n3
[ MAXLEN
];
447 if ( strstr ( ip
, "." ) != 0 ) {
453 if ( getword ( n1
, sizeof ( n1
), wip
, '.' )< 0 || getword ( n2
, sizeof ( n2
), wip
, '.' )< 0 || getword ( n3
, sizeof ( n3
), wip
, '.' )< 0 ) {
454 printf ( "SARG: Maybe you have a broken record or garbage in the IP address. \n " );
458 if ( getword ( n1
, sizeof ( n1
), wip
, '_' )< 0 || getword ( n2
, sizeof ( n2
), wip
, '_' )< 0 || getword ( n3
, sizeof ( n3
), wip
, '_' )< 0 ) {
459 printf ( "SARG: Maybe you have a broken record or garbage in the IP address. \n " );
464 sprintf ( ip
, "%s%s%s%s%s%s%s" , n1
, sep
, n2
, sep
, n3
, sep
, wip
);
469 char * fixnum ( long long int value
, int n
)
471 #define MAXLEN_FIXNUM 1024
472 char num
[ MAXLEN_FIXNUM
];
473 char buf
[ MAXLEN_FIXNUM
* 2 ];
475 static char ret
[ MAXLEN_FIXNUM
* 2 ];
477 register int i
, j
, k
;
478 static char abbrev
[ 30 ];
480 my_lltoa ( value
, num
, 0 );
482 if ( strcmp ( DisplayedValues
, "abbreviation" ) == 0 ) {
484 sprintf ( abbrev
, "%s" , num
);
485 if ( strlen ( num
) == 4 || strlen ( num
) == 7 || strlen ( num
) == 10 || strlen ( num
) == 13 ) {
486 snprintf ( abbrev
, 2 , "%s" , num
);
487 strncat ( abbrev
, "." , 1 );
488 strncat ( abbrev
, num
+ 1 , 2 );
489 if (! n
) return ( abbrev
);
491 strncat ( abbrev
, "K" , 1 );
492 else if ( strlen ( num
) == 7 )
493 strncat ( abbrev
, "M" , 1 );
494 else if ( strlen ( num
) == 10 )
495 strncat ( abbrev
, "G" , 1 );
496 else if ( strlen ( num
) == 13 )
497 strncat ( abbrev
, "T" , 1 );
499 if ( strlen ( num
) == 5 || strlen ( num
) == 8 || strlen ( num
) == 11 || strlen ( num
) == 14 ) {
500 snprintf ( abbrev
, 3 , "%s" , num
);
501 strncat ( abbrev
, "." , 1 );
502 strncat ( abbrev
, num
+ 2 , 2 );
503 if (! n
) return ( abbrev
);
505 strncat ( abbrev
, "K" , 1 );
506 else if ( strlen ( num
) == 8 )
507 strncat ( abbrev
, "M" , 1 );
508 else if ( strlen ( num
) == 11 )
509 strncat ( abbrev
, "G" , 1 );
510 else if ( strlen ( num
) == 14 )
511 strncat ( abbrev
, "T" , 1 );
513 if ( strlen ( num
) == 6 || strlen ( num
) == 9 || strlen ( num
) == 12 || strlen ( num
) == 15 ) {
514 snprintf ( abbrev
, 4 , "%s" , num
);
515 strncat ( abbrev
, "." , 1 );
516 strncat ( abbrev
, num
+ 3 , 2 );
517 if (! n
) return ( abbrev
);
519 strncat ( abbrev
, "K" , 1 );
520 else if ( strlen ( num
) == 9 )
521 strncat ( abbrev
, "M" , 1 );
522 else if ( strlen ( num
) == 12 )
523 strncat ( abbrev
, "G" , 1 );
524 else if ( strlen ( num
) == 15 )
525 strncat ( abbrev
, "T" , 1 );
531 bzero ( buf
, MAXLEN_FIXNUM
* 2 );
537 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
538 if ( k
== 2 && i
!= 0 ) {
541 if ( strcmp ( UseComma
, "yes" ) == 0 )
543 else pbuf
[ j
++] = '.' ;
553 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
562 char * fixnum2 ( long long int value
, int n
)
564 #define MAXLEN_FIXNUM2 1024
565 char num
[ MAXLEN_FIXNUM2
];
566 char buf
[ MAXLEN_FIXNUM2
* 2 ];
568 static char ret
[ MAXLEN_FIXNUM2
* 2 ];
570 register int i
, j
, k
;
572 my_lltoa ( value
, num
, 0 );
573 bzero ( buf
, MAXLEN_FIXNUM2
* 2 );
579 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
580 if ( k
== 2 && i
!= 0 ) {
583 if ( strcmp ( UseComma
, "yes" ) == 0 )
585 else pbuf
[ j
++] = '.' ;
595 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
605 void buildhref ( char * href
)
609 if ( strcmp ( href
, "./" ) == 0 ){
611 strcat ( href
, "<a href='" );
615 href
[ strlen ( href
)- 1 ]= '\0' ;
616 sprintf ( whref
, "%s" , strrchr ( href
, '/' ));
618 strcpy ( href
, "<a href='" );
627 char * buildtime ( long long int elap
)
630 int num
= elap
/ 1000 ;
639 min
=( num
% 3600 ) / 60 ;
641 sprintf ( buf
, "%02d:%02d:%02d" , hor
, min
, sec
);
648 void obtdate ( const char * dirname
, const char * name
, char * data
)
654 sprintf ( wdir
, "%s%s/sarg-date" , dirname
, name
);
655 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
656 sprintf ( wdir
, "%s%s/date" , dirname
, name
);
657 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
663 fgets ( data
, 80 , fp_in
);
665 data
[ strlen ( data
)- 1 ]= '\0' ;
672 void obtuser ( const char * dirname
, const char * name
, char * tuser
)
678 sprintf ( wdir
, "%s%s/sarg-users" , dirname
, name
);
679 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
680 sprintf ( wdir
, "%s%s/users" , dirname
, name
);
681 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
687 fgets ( tuser
, 20 , fp_in
);
688 tuser
[ strlen ( tuser
)- 1 ]= '\0' ;
696 void obttotal ( const char * dirname
, const char * name
, char * tbytes
, char * tuser
, char * media
)
702 long long int wtuser
= 0 ;
704 sprintf ( wdir
, "%s%s/sarg-general" , dirname
, name
);
705 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
706 sprintf ( wdir
, "%s%s/general" , dirname
, name
);
707 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
713 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
714 if ( getword_multisep ( warea
, sizeof ( warea
), buf
, ' ' )< 0 ) {
715 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
718 if ( strcmp ( warea
, "TOTAL" ) != 0 )
720 if ( getword_multisep ( warea
, sizeof ( warea
), buf
, ' ' )< 0 ) {
721 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
724 if ( getword_multisep ( warea
, sizeof ( warea
), buf
, ' ' )< 0 ) {
725 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
728 twork
= my_atoll ( warea
);
729 sprintf ( tbytes
, "%s" , fixnum ( twork
, 1 ));
733 if ( tuser
[ 0 ] == '\0' ) {
735 sprintf ( media
, "%s" , "0" );
739 wtuser
= my_atoll ( tuser
);
740 med
= my_atoll ( warea
) / wtuser
;
741 sprintf ( media
, "%s" , fixnum ( med
, 1 ));
748 //void gperiod(char *dirname, const char *period)
754 char wdirname
[ MAXLEN
];
756 strcpy ( wdirname
, dirname
);
757 strcat ( wdirname
, "/" );
758 strcat ( wdirname
, "sarg-period" );
760 if (( fp_ou
= fopen ( wdirname
, "w" ))== NULL
){
761 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
769 debuga (( char *) text
[ 50 ]);
775 void vrfydir ( const char * dir
, const char * per1
, const char * addr
, const char * site
, const char * us
, const char * form
)
777 FILE * img_in
, * img_ou
;
782 char dirname2
[ MAXLEN
];
785 struct dirent
* direntp
;
791 if ( strcmp ( IndexTree
, "date" ) == 0 ) {
798 if ( strncmp ( df
, "u" , 1 ) == 0 ) {
799 strncpy ( y1
, period
, 4 );
800 strncpy ( y2
, period
+ 10 , 4 );
801 strncpy ( m1
, period
+ 4 , 3 );
802 strncpy ( m2
, period
+ 14 , 3 );
803 strncpy ( d1
, period
+ 7 , 2 );
804 strncpy ( d2
, period
+ 17 , 2 );
805 } else if ( strncmp ( df
, "e" , 1 ) == 0 ) {
806 strncpy ( d1
, period
+ 0 , 2 );
807 strncpy ( d2
, period
+ 10 , 2 );
808 strncpy ( m1
, period
+ 2 , 3 );
809 strncpy ( m2
, period
+ 12 , 3 );
810 strncpy ( y1
, period
+ 5 , 4 );
811 strncpy ( y2
, period
+ 15 , 4 );
816 sprintf ( wdir
, "%s%s" , outdir
, y1
);
817 if ( strcmp ( y1
, y2
) != 0 ) {
819 strncat ( wdir
, y2
, strlen ( y2
));
821 if ( access ( wdir
, R_OK
) != 0 )
825 strncat ( wdir
, m1
, strlen ( m1
));
826 if ( strcmp ( m1
, m2
) != 0 ) {
828 strncat ( wdir
, m2
, strlen ( m2
));
830 if ( access ( wdir
, R_OK
) != 0 )
834 strncat ( wdir
, d1
, strlen ( d1
));
835 if ( strcmp ( d1
, d2
) != 0 ) {
837 strncat ( wdir
, d2
, strlen ( d2
));
846 if ( strlen ( addr
) > 0 ) {
850 if ( strlen ( site
) > 0 ) {
855 if ( strcmp ( dirname
, wdir
) != 0 )
856 strcpy ( dirname
, wdir
);
858 if ( strcmp ( IndexTree
, "date" ) != 0 ) {
859 strcpy ( dirname2
, dirname
);
860 if ( strcmp ( OverwriteReport
, "no" ) == 0 ) {
862 if ( access ( wdir
, R_OK
) == 0 ) {
863 sprintf ( wdir
, "%s.%d" , dirname
, num
);
864 sprintf ( per2
, "%s.%d" , per1
, num
);
873 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
874 rename ( dirname2
, wdir
);
877 if ( access ( dir
, R_OK
) == 0 ) {
878 sprintf ( csort
, "rm -r %s" , dir
);
884 strcpy ( dirname2
, wdir
);
885 if ( strcmp ( OverwriteReport
, "no" ) == 0 ) {
887 if ( access ( wdir
, R_OK
) == 0 ) {
888 sprintf ( wdir
, "%s.%d" , dirname2
, num
);
889 sprintf ( per2
, "%s.%d" , per1
, num
);
898 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
899 rename ( dirname2
, wdir
);
900 strcpy ( dirname2
, wdir
);
903 if ( access ( wdir
, R_OK
) == 0 ) {
904 sprintf ( csort
, "rm -r %s" , wdir
);
909 if ( access ( wdir
, R_OK
) != 0 )
913 strcpy ( dirname2
, wdir
);
914 sprintf ( images
, "%simages" , outdir
);
917 sprintf ( wdir
, "date \" +%%a %%b %%d %%H:%%M:%%S %%Z %%Y \" >%s/sarg-date" , dirname
);
918 cstatus
= system ( wdir
);
919 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
920 fprintf ( stderr
, "SARG: command return status %d \n " , WEXITSTATUS ( cstatus
));
921 fprintf ( stderr
, "SARG: command: %s \n " , wdir
);
925 sprintf ( per2
, "%s/images" , SYSCONFDIR
);
927 dirp
= opendir ( per2
);
929 fprintf ( stderr
, "SARG: (util) %s %s: %s \n " , "Can't open directory" , per2
, strerror ( errno
));
932 while (( direntp
= readdir ( dirp
)) != NULL
){
933 if ( strncmp ( direntp
-> d_name
, "." , 1 ) == 0 )
935 sprintf ( val10
, "%s/%s" , per2
, direntp
-> d_name
);
936 sprintf ( val11
, "%s/%s" , images
, direntp
-> d_name
);
937 img_in
= fopen ( val10
, "rb" );
939 img_ou
= fopen ( val11
, "wb" );
949 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open/create file" , val11
, strerror ( errno
));
951 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open file" , val10
, strerror ( errno
));
955 ( void ) rewinddir ( dirp
);
956 ( void ) closedir ( dirp
);
964 void strip_latin ( char * line
)
969 while ( strstr ( line
, "&" ) != 0 ){
970 if ( getword_multisep ( warea
, sizeof ( warea
), line
, '&' )< 0 ) {
971 printf ( "SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities. \n " );
974 strncat ( warea
, line
, 1 );
975 if ( getword_multisep ( buf
, sizeof ( buf
), line
, ';' )< 0 ) {
976 printf ( "SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities. \n " );
987 void zdate ( char * ftime
, const char * DateFormat
)
994 local
= localtime (& t
);
995 if ( strcmp ( DateFormat
, "u" ) == 0 )
996 strftime ( ftime
, 127 , "%b/%d/%Y %H:%M" , local
);
997 if ( strcmp ( DateFormat
, "e" ) == 0 )
998 strftime ( ftime
, 127 , "%d/%b/%Y-%H:%M" , local
);
999 if ( strcmp ( DateFormat
, "w" ) == 0 )
1000 strftime ( ftime
, 127 , "%V-%H-%M" , local
);
1006 char * fixtime ( long int elap
)
1009 int num
= elap
/ 1000 ;
1013 static char buf
[ 12 ];
1015 if ( strcmp ( datetimeby
, "bytes" ) == 0 ) {
1016 sprintf ( buf
, "%s" , fixnum ( elap
, 1 ));
1023 sprintf ( buf
, "00:00:%02ld" , elap
);
1028 min
=( num
% 3600 ) / 60 ;
1031 sprintf ( buf
, "%01d:%02d:%02d" , hor
, min
, sec
);
1033 if ( strcmp ( buf
, "0:00:00" ) == 0 )
1041 void date_from ( char * date
, char * dfrom
, char * duntil
)
1050 static char wdate
[ 50 ];
1054 if ( strstr ( wdate
, "-" ) == 0 ) {
1060 if ( getword ( diaf
, sizeof ( diaf
), wdate
, '/' )< 0 || getword ( mesf
, sizeof ( mesf
), wdate
, '/' )< 0 || getword ( anof
, sizeof ( anof
), wdate
, '-' )< 0 ||
1061 getword ( diau
, sizeof ( diau
), wdate
, '/' )< 0 || getword ( mesu
, sizeof ( mesu
), wdate
, '/' )< 0 || getword ( anou
, sizeof ( anou
), wdate
, 0 )< 0 ) {
1062 printf ( "SARG: Maybe you have a broken record or garbage in a date. \n " );
1066 sprintf ( dfrom
, "%s%s%s" , anof
, mesf
, diaf
);
1067 sprintf ( duntil
, "%s%s%s" , anou
, mesu
, diau
);
1072 char * strlow ( char * string
)
1078 for ( s
= string
; * s
; ++ s
)
1088 char * strup ( char * string
)
1094 for ( s
= string
; * s
; ++ s
)
1102 char * subs ( char * str
, char * from
, char * to
)
1106 unsigned int ss
, st
;
1108 if ( strstr ( str
, from
) == 0 )
1109 return ( char *) str
;
1111 ss
= strlen ( str
); st
= strlen ( to
) + 10 ;
1113 if (( ret
=( char *) malloc ( ss
+ st
))== NULL
)
1115 fprintf ( stderr
, "SARG: %s (%d): \n " , text
[ 59 ], ss
+ st
);
1121 tmp
= strstr ( str
, from
);
1122 if ( tmp
== ( char *) NULL
)
1123 return ( char *) NULL
;
1124 strncpy ( ret
, str
, ss
- strlen ( tmp
));
1126 strcat ( ret
, ( tmp
+ strlen ( from
)));
1127 return ( char *) ret
;
1131 void removetmp ( const char * outdir
)
1137 if ( strcmp ( RemoveTempFiles
, "yes" ) != 0 )
1141 sprintf ( msg
, "%s: sarg-general, sarg-period" , text
[ 82 ]);
1144 sprintf ( warea
, "%s/sarg-general" , outdir
);
1145 if (( fp_in
= fopen ( warea
, "r" ))== NULL
){
1146 fprintf ( stderr
, "===SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1149 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
1150 if ( strncmp ( buf
, "TOTAL" , 5 ) == 0 )
1154 if (( fp_in
= fopen ( warea
, "w" ))== NULL
){
1155 fprintf ( stderr
, "SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1160 if ( snprintf ( warea
, sizeof ( warea
), "%s/sarg-period" , outdir
)>= sizeof ( warea
)) {
1161 fprintf ( stderr
, "SARG: (removetmp) directory too long to remove: %s/sarg-period \n " , outdir
);
1169 void load_excludecodes ( void )
1175 if (( fp_in
= fopen ( ExcludeCodes
, "r" ))== NULL
) {
1176 fprintf ( stderr
, "SARG: (util) Cannot open file: %s (exclude_codes) \n " , ExcludeCodes
);
1180 while ( fgets ( data
, sizeof ( data
), fp_in
)!= NULL
) {
1181 data
[ strlen ( data
)- 1 ]= '\0' ;
1182 strcat ( excludecode
, data
);
1183 strcat ( excludecode
, ";" );
1192 int vercode ( const char * code
)
1198 strcpy ( warea
, excludecode
);
1199 for ( z
= 0 ; z
<= excode
- 1 ; z
++) {
1200 if ( getword_multisep ( cod
, sizeof ( cod
), warea
, ';' )< 0 ) {
1201 printf ( "SARG: Maybe you have a broken record or garbage in a version code. \n " );
1204 if ( strcmp ( code
, cod
) == 0 )
1210 void fixnone ( char * str
)
1214 eol
= strchr ( str
, ' \n ' );
1216 if ( strcmp ( str
, "none" ) == 0 )
1222 #ifdef LEGACY_TESTVALIDUSERCHAR
1223 int testvaliduserchar ( const char * user
)
1229 for ( y
= 0 ; y
< strlen ( UserInvalidChar
); y
++) {
1230 for ( x
= 0 ; x
< strlen ( user
); x
++) {
1231 if ( user
[ x
] == UserInvalidChar
[ y
])
1238 int testvaliduserchar ( const char * user
)
1241 char * p_UserInvalidChar
= UserInvalidChar
;
1242 const char * p_user
;
1244 while ( * p_UserInvalidChar
) {
1247 if ( * p_UserInvalidChar
== * p_user
)
1251 p_UserInvalidChar
++ ;
1257 int compar ( const void * a
, const void * b
)
1258 { if ( *( int *) a
> *( int *) b
) return 1 ;
1259 if ( *( int *) a
< *( int *) b
) return - 1 ;
1263 int getnumlist ( char * buf
, numlist
* list
, const int len
, const int maxvalue
)
1264 { int i
, j
, d
, flag
, r1
, r2
;
1265 char * pbuf
, ** bp
, * strbufs
[ 24 ];
1268 strtok ( buf
, " \t " );
1269 for ( * bp
= strtok ( NULL
, "," ), list
-> len
= 0 ; * bp
; * bp
= strtok ( NULL
, "," ) )
1270 { if ( ++ bp
>= & strbufs
[ 24 ] )
1277 for ( i
= 0 ; i
< list
-> len
; i
++ )
1278 { if ( strstr ( strbufs
[ i
], "-" ) != 0 )
1279 { pbuf
= strbufs
[ i
];
1280 strtok ( pbuf
, "-" );
1281 pbuf
= strtok ( NULL
, "\0" );
1282 r1
= atoi ( strbufs
[ i
] );
1283 if ( ( r2
= atoi ( pbuf
) ) >= maxvalue
|| r1
>= r2
)
1285 if ( i
+ d
+ ( r2
- r1
) + 1 <= len
)
1286 { for ( j
= r1
; j
<= r2
; j
++ )
1287 list
-> list
[ i
+ d
++ ] = j
;
1292 if ( ( list
-> list
[ i
+ d
] = atoi ( strbufs
[ i
] ) ) >= maxvalue
)
1296 qsort ( list
-> list
, list
-> len
, sizeof ( int ), compar
);
1299 for ( i
= 0 ; i
< list
-> len
- 1 ; i
++ )
1300 if ( list
-> list
[ i
] == list
-> list
[ i
+ 1 ] )
1301 { for ( j
= i
+ 1 ; j
< list
-> len
; j
++ )
1302 list
-> list
[ j
- 1 ] = list
-> list
[ j
];
1312 void show_info ( FILE * fp_ou
)
1314 if ( strcmp ( ShowSargInfo
, "yes" ) != 0 ) return ;
1315 zdate ( ftime
, DateFormat
);
1316 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
);
1319 void show_sarg ( FILE * fp_ou
, const char * ind
)
1321 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
);
1324 char * get_size ( const char * path
, const char * file
)
1330 if ( snprintf ( cmd
, sizeof ( cmd
), "du -skh %s%s" , path
, file
)>= sizeof ( cmd
)) {
1331 printf ( "SARG: Cannot get disk space because the path %s%s is too long. \n " , path
, file
);
1334 fp
= popen ( cmd
, "r" );
1335 fgets ( response
, sizeof ( response
), fp
);
1336 if ( getword_multisep ( val5
, sizeof ( val5
), response
, ' \t ' )< 0 ) {
1337 printf ( "SARG: Maybe the command %s failed. \n " , cmd
);
1346 void write_html_header ( FILE * fp_ou
, const char * ind
)
1348 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
);
1350 fprintf ( fp_ou
, "<body style= \" font-family:%s;font-size:%s;background-color:%s;background-image:url(%s) \" > \n " , FontFace
, TitleFontSize
, BgColor
, BgImage
);
1351 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
);
1352 show_sarg ( fp_ou
, ind
);
1353 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
);
1358 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1359 printf ( "SARG: MALICIUS CODE DETECTED. \n " );
1360 printf ( "SARG: I think someone is trying to execute arbitrary code in your system using sarg. \n " );
1361 printf ( "SARG: please review your access.log and/or your useragent.log file. \n " );
1362 printf ( "SARG: process stoped. No actions taken. \n " );
1363 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1365 system ( "rm -rf /tmp/sarg" );
1366 sprintf ( tmp4
, "rm -rf %s" , dirname
);
1368 system ( "rm -rf /tmp/sarg" );
1374 void url_module ( char * url
, char * w2
)
1382 for ( x
= strlen ( url
)- 1 ; x
>= 0 ; x
--) {
1383 if ( url
[ x
] == '/' || y
> 255 ) break ;
1389 for ( x
= strlen ( w
)- 1 ; x
>= 0 ; x
--) {
1396 void write_html_trailer ( FILE * fp_ou
)
1398 fputs ( "</table></center> \n " , fp_ou
);
1399 zdate ( ftime
, DateFormat
);
1401 fputs ( "</body> \n </html> \n " , fp_ou
);
1406 printf ( "SARG Version: %s \n " , VERSION
);
1410 char * get_param_value ( const char * param
, char * line
)
1414 while (* line
== ' ' || * line
== ' \t ' ) line
++;
1416 if ( strncasecmp ( line
, param
, plen
)) return ( NULL
);
1417 if ( line
[ plen
]!= ' ' && line
[ plen
]!= ' \t ' ) return ( NULL
);
1419 while (* line
== ' ' || * line
== ' \t ' ) line
++;