]>
git.ipfire.org Git - thirdparty/sarg.git/blob - util.c
a69968caba4d7786f32405753e1b18c868d6852e
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" };
34 //! The list of the HTTP codes to exclude from the report.
35 static char * excludecode
= NULL
;
37 /*void fgetword(char *word, char *line, int stop)
42 for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x];
49 tchar = strchr(line, stop);
50 if (tchar == NULL) strcpy(word, line);
53 difflen = tchar - line;
54 strncpy(word, line, difflen);
59 int getword ( char * word
, int limit
, char * line
, int stop
)
63 /*if(strlen(line) < 3) {
68 for ( x
= 0 ;(( line
[ x
]) && ( line
[ x
] != stop
)); x
++) {
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");
74 if ( limit
> 0 ) word
[ limit
- 1 ]= '\0' ;
85 while (( line
[ y
++] = line
[ x
++]));
89 int getword_multisep ( char * word
, int limit
, char * line
, int stop
)
93 for ( x
= 0 ;(( line
[ x
]) && ( line
[ x
] != stop
)); x
++) {
95 printf ( "SARG: getword_multisep loop detected. \n " );
96 printf ( "SARG: Record= \" %s \"\n " , line
);
97 printf ( "SARG: searching for \' x%x \'\n " , stop
);
98 printf ( "SARG: searching for \' x%x \'\n " , stop
);
99 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
100 if ( limit
> 0 ) word
[ limit
- 1 ]= '\0' ;
108 while ( line
[ x
] && line
[ x
]== stop
) ++ x
;
111 while (( line
[ y
++] = line
[ x
++]));
115 int getword3 ( char * word
, int limit
, char * line
, int stop
)
119 for ( x
= 0 ; x
< limit
&& ( line
[ x
] && ( line
[ x
] != stop
)) ; x
++) word
[ x
] = line
[ x
];
121 printf ( "SARG: getword3 loop detected after %d bytes. \n " , x
);
122 printf ( "SARG: Buffer= \" %s \"\n " , line
);
123 printf ( "SARG: searching for \' x%x \'\n " , stop
);
124 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
130 while (( line
[ y
++] = line
[ x
++]));
135 #ifdef LEGACY_MY_ATOLL
137 // BMG (bguillory@email.com)
139 long long int my_atoll ( const char * nptr
)
140 #define MAXLLL 30 //maximum number of digits in long long (a guess)
143 long long int returnval
= 0 ;
148 // Soak up all the white space
149 while ( isspace ( nptr
[ offset
])) {
153 //For each character left to right
154 //change the character to a single digit
155 //multiply what we had before by 10 and add the new digit
156 for ( x
= offset
; x
<= MAXLLL
+ offset
&& isdigit ( nptr
[ x
]); x
++) {
157 sprintf ( one_digit
, "%c" , nptr
[ x
]); //I don't know how else to do this
158 returnval
= ( returnval
* 10 ) + atoi ( one_digit
);
167 #define MAXLLL 30 //!< Maximum number of digits in long long (a guess).
168 long long int my_atoll ( const char * nptr
)
170 long long int returnval
= 0 ;
171 const char * t
= nptr
;
172 int max_digits
= MAXLLL
;
174 // Soak up all the white space
175 while ( isspace ( * t
)) {
179 //For each character left to right
180 //change the character to a single digit
181 //multiply what we had before by 10 and add the new digit
183 for ( ; -- max_digits
&& isdigit ( * t
) ; t
++ )
185 returnval
= ( returnval
* 10 ) + ( * t
- '0' ) ;
195 void my_mkdir ( const char * name
)
201 if ( strncmp ( name
, "." , 1 ) == 0 || strncmp ( name
, "/" , 1 ) != 0 ) {
202 fprintf ( stderr
, "SARG: Invalid path (%s). Please, use absolute paths only. \n " , name
);
203 fprintf ( stderr
, "SARG: process aborted. \n " );
209 if ( getword_multisep ( w1
, sizeof ( w1
), w0
, '/' )< 0 ) {
210 printf ( "SARG: Maybe you have a broken record or garbage in the directory name %s. \n " , name
);
213 while ( strchr ( w0
, '/' )) {
214 if ( getword_multisep ( w1
, sizeof ( w1
), w0
, '/' )< 0 ) {
215 printf ( "SARG: Maybe you have a broken record or garbage in the directory name %s. \n " , name
);
219 if ( access ( w2
, R_OK
) != 0 ) {
221 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w2
, strerror ( errno
));
222 fprintf ( stderr
, "SARG: process aborted. \n " );
229 if ( access ( w2
, R_OK
) != 0 ) {
231 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w2
, strerror ( errno
));
232 fprintf ( stderr
, "SARG: process aborted. \n " );
239 void my_lltoa ( unsigned long long int n
, char s
[], int len
)
245 s
[ i
++] = ( n
% 10 ) + '0' ;
246 } while (( n
/= 10 ) > 0 );
250 for ( i
= 0 , j
= strlen ( s
)- 1 ; i
< j
; i
++, j
--)
259 bzero ( ww
, sizeof ( ww
));
264 strncat ( ww
, s
, i
> sizeof ( ww
)? sizeof ( ww
): i
);
271 void builddia ( char * dia
, const char * mes
, const char * ano
, const char * df
, char * wdata
)
276 if ( strlen ( dia
) < 1 ) return ;
278 for ( nmes
= 0 ; nmes
< 12 ; nmes
++) {
279 if ( strcmp ( mtab1
[ nmes
], mes
) == 0 ) {
285 snprintf ( wdata
, 9 , "%s%02d%s" , ano
, nmes
, dia
);
288 snprintf ( ndia
, sizeof ( ndia
), "%s/%02d/%s" , dia
, nmes
, ano
);
290 snprintf ( ndia
, sizeof ( ndia
), "%02d/%s/%s" , nmes
, dia
, ano
);
297 void buildymd ( const char * dia
, const char * mes
, const char * ano
, char * wdata
)
301 for ( nmes
= 0 ; nmes
< 12 ; nmes
++) {
302 if ( strcmp ( mtab1
[ nmes
], mes
) == 0 )
306 sprintf ( wdata
, "%s%02d%s" , ano
, nmes
+ 1 , dia
);
311 void conv_month ( char * month
)
315 for ( x
= 0 ; x
< 12 && strcmp ( mtab1
[ x
], month
)!= 0 ; x
++);
316 sprintf ( month
, "%02d" , x
+ 1 );
321 void conv_month_name ( char * month
)
327 strcpy ( month
, mtab1
[ x
- 1 ]);
331 void name_month ( char * month
, int month_len
)
333 int x
, z
= atoi ( month
)- 1 ;
340 if ( getword_multisep ( w
, sizeof ( w
), m
, ',' )< 0 ) {
341 printf ( "SARG: Maybe you have a broken record or garbage in the names of the months. \n " );
344 if ( getword_multisep ( month
, month_len
, m
, ',' )< 0 ) {
345 printf ( "SARG: Maybe you have a broken record or garbage in the name of the months. \n " );
351 void fixper ( char * tbuf
, char * period
, const char * duntil
)
355 char dia
[ 5 ], mes
[ 5 ], ano
[ 5 ];
360 strncpy ( dia
, duntil
+ 6 , 2 );
362 strncpy ( mes
, duntil
+ 4 , 2 );
364 strncpy ( ano
, duntil
, 4 );
369 strcpy ( mes
, mtab1
[ x
- 1 ]);
371 if ( strcmp ( df
, "e" ) == 0 )
372 sprintf ( warea
, "%s%s%s" , dia
, mes
, ano
);
373 if ( strcmp ( df
, "u" ) == 0 )
374 sprintf ( warea
, "%s%s%s" , mes
, dia
, ano
);
376 strcat ( period
, warea
);
380 void debuga ( const char * msg
,...)
384 fputs ( "SARG: " , stderr
);
386 vfprintf ( stderr
, msg
, ap
);
392 void debugaz ( const char * head
, const char * msg
)
394 fprintf ( stderr
, "SARG: (util) %s=%s \n " , head
, msg
);
406 for ( i
= 0 ; ip
[ i
]; i
++) {
428 char * fixnum ( long long int value
, int n
)
430 #define MAXLEN_FIXNUM 1024
431 char num
[ MAXLEN_FIXNUM
];
432 char buf
[ MAXLEN_FIXNUM
* 2 ];
434 static char ret
[ MAXLEN_FIXNUM
* 2 ];
436 register int i
, j
, k
;
437 static char abbrev
[ 30 ];
439 my_lltoa ( value
, num
, 0 );
441 if ( strcmp ( DisplayedValues
, "abbreviation" ) == 0 ) {
443 sprintf ( abbrev
, "%s" , num
);
444 if ( strlen ( num
) == 4 || strlen ( num
) == 7 || strlen ( num
) == 10 || strlen ( num
) == 13 ) {
445 snprintf ( abbrev
, 2 , "%s" , num
);
446 strncat ( abbrev
, "." , 1 );
447 strncat ( abbrev
, num
+ 1 , 2 );
448 if (! n
) return ( abbrev
);
450 strncat ( abbrev
, "K" , 1 );
451 else if ( strlen ( num
) == 7 )
452 strncat ( abbrev
, "M" , 1 );
453 else if ( strlen ( num
) == 10 )
454 strncat ( abbrev
, "G" , 1 );
455 else if ( strlen ( num
) == 13 )
456 strncat ( abbrev
, "T" , 1 );
458 if ( strlen ( num
) == 5 || strlen ( num
) == 8 || strlen ( num
) == 11 || strlen ( num
) == 14 ) {
459 snprintf ( abbrev
, 3 , "%s" , num
);
460 strncat ( abbrev
, "." , 1 );
461 strncat ( abbrev
, num
+ 2 , 2 );
462 if (! n
) return ( abbrev
);
464 strncat ( abbrev
, "K" , 1 );
465 else if ( strlen ( num
) == 8 )
466 strncat ( abbrev
, "M" , 1 );
467 else if ( strlen ( num
) == 11 )
468 strncat ( abbrev
, "G" , 1 );
469 else if ( strlen ( num
) == 14 )
470 strncat ( abbrev
, "T" , 1 );
472 if ( strlen ( num
) == 6 || strlen ( num
) == 9 || strlen ( num
) == 12 || strlen ( num
) == 15 ) {
473 snprintf ( abbrev
, 4 , "%s" , num
);
474 strncat ( abbrev
, "." , 1 );
475 strncat ( abbrev
, num
+ 3 , 2 );
476 if (! n
) return ( abbrev
);
478 strncat ( abbrev
, "K" , 1 );
479 else if ( strlen ( num
) == 9 )
480 strncat ( abbrev
, "M" , 1 );
481 else if ( strlen ( num
) == 12 )
482 strncat ( abbrev
, "G" , 1 );
483 else if ( strlen ( num
) == 15 )
484 strncat ( abbrev
, "T" , 1 );
490 bzero ( buf
, MAXLEN_FIXNUM
* 2 );
496 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
497 if ( k
== 2 && i
!= 0 ) {
500 if ( strcmp ( UseComma
, "yes" ) == 0 )
502 else pbuf
[ j
++] = '.' ;
512 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
521 char * fixnum2 ( long long int value
, int n
)
523 #define MAXLEN_FIXNUM2 1024
524 char num
[ MAXLEN_FIXNUM2
];
525 char buf
[ MAXLEN_FIXNUM2
* 2 ];
527 static char ret
[ MAXLEN_FIXNUM2
* 2 ];
529 register int i
, j
, k
;
531 my_lltoa ( value
, num
, 0 );
532 bzero ( buf
, MAXLEN_FIXNUM2
* 2 );
538 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
539 if ( k
== 2 && i
!= 0 ) {
542 if ( strcmp ( UseComma
, "yes" ) == 0 )
544 else pbuf
[ j
++] = '.' ;
554 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
564 void buildhref ( char * href
)
568 if ( strcmp ( href
, "./" ) == 0 ){
570 strcat ( href
, "<a href='" );
574 href
[ strlen ( href
)- 1 ]= '\0' ;
575 sprintf ( whref
, "%s" , strrchr ( href
, '/' ));
577 strcpy ( href
, "<a href='" );
586 char * buildtime ( long long int elap
)
589 int num
= elap
/ 1000 ;
598 min
=( num
% 3600 ) / 60 ;
600 sprintf ( buf
, "%02d:%02d:%02d" , hor
, min
, sec
);
607 void obtdate ( const char * dirname
, const char * name
, char * data
)
613 sprintf ( wdir
, "%s%s/sarg-date" , dirname
, name
);
614 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
615 sprintf ( wdir
, "%s%s/date" , dirname
, name
);
616 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
622 fgets ( data
, 80 , fp_in
);
624 data
[ strlen ( data
)- 1 ]= '\0' ;
631 void obtuser ( const char * dirname
, const char * name
, char * tuser
)
637 sprintf ( wdir
, "%s%s/sarg-users" , dirname
, name
);
638 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
639 sprintf ( wdir
, "%s%s/users" , dirname
, name
);
640 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
646 fgets ( tuser
, 20 , fp_in
);
647 tuser
[ strlen ( tuser
)- 1 ]= '\0' ;
655 void obttotal ( const char * dirname
, const char * name
, char * tbytes
, char * tuser
, char * media
)
661 long long int wtuser
= 0 ;
664 sprintf ( wdir
, "%s%s/sarg-general" , dirname
, name
);
665 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
666 sprintf ( wdir
, "%s%s/general" , dirname
, name
);
667 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
673 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
674 if ( getword_multisep ( warea
, sizeof ( warea
), buf
, ' ' )< 0 ) {
675 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
678 if ( strcmp ( warea
, "TOTAL" ) != 0 )
680 if ( getword_multisep ( warea
, sizeof ( warea
), buf
, ' ' )< 0 ) {
681 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
684 if ( getword_multisep ( warea
, sizeof ( warea
), buf
, ' ' )< 0 ) {
685 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
688 twork
= my_atoll ( warea
);
689 strcpy ( tbytes
, fixnum ( twork
, 1 ));
693 wtuser
= my_atoll ( tuser
);
699 med
= my_atoll ( warea
) / wtuser
;
700 strcpy ( media
, fixnum ( med
, 1 ));
707 void gperiod ( const char * dirname
, const char * period
)
711 char wdirname
[ MAXLEN
];
713 strcpy ( wdirname
, dirname
);
714 strcat ( wdirname
, "/" );
715 strcat ( wdirname
, "sarg-period" );
717 if (( fp_ou
= fopen ( wdirname
, "w" ))== NULL
){
718 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
726 debuga ( "%s" ,( char *) text
[ 50 ]);
732 void vrfydir ( const char * dir
, const char * per1
, const char * addr
, const char * site
, const char * us
, const char * form
)
734 FILE * img_in
, * img_ou
;
739 char dirname2
[ MAXLEN
];
742 struct dirent
* direntp
;
748 if ( strcmp ( IndexTree
, "date" ) == 0 ) {
755 if ( strncmp ( df
, "u" , 1 ) == 0 ) {
756 strncpy ( y1
, period
, 4 );
757 strncpy ( y2
, period
+ 10 , 4 );
758 strncpy ( m1
, period
+ 4 , 3 );
759 strncpy ( m2
, period
+ 14 , 3 );
760 strncpy ( d1
, period
+ 7 , 2 );
761 strncpy ( d2
, period
+ 17 , 2 );
762 } else if ( strncmp ( df
, "e" , 1 ) == 0 ) {
763 strncpy ( d1
, period
+ 0 , 2 );
764 strncpy ( d2
, period
+ 10 , 2 );
765 strncpy ( m1
, period
+ 2 , 3 );
766 strncpy ( m2
, period
+ 12 , 3 );
767 strncpy ( y1
, period
+ 5 , 4 );
768 strncpy ( y2
, period
+ 15 , 4 );
773 sprintf ( wdir
, "%s%s" , outdir
, y1
);
774 if ( strcmp ( y1
, y2
) != 0 ) {
776 strncat ( wdir
, y2
, strlen ( y2
));
778 if ( access ( wdir
, R_OK
) != 0 )
782 strncat ( wdir
, m1
, strlen ( m1
));
783 if ( strcmp ( m1
, m2
) != 0 ) {
785 strncat ( wdir
, m2
, strlen ( m2
));
787 if ( access ( wdir
, R_OK
) != 0 )
791 strncat ( wdir
, d1
, strlen ( d1
));
792 if ( strcmp ( d1
, d2
) != 0 ) {
794 strncat ( wdir
, d2
, strlen ( d2
));
803 if ( strlen ( addr
) > 0 ) {
807 if ( strlen ( site
) > 0 ) {
812 if ( strcmp ( dirname
, wdir
) != 0 )
813 strcpy ( dirname
, wdir
);
815 if ( strcmp ( IndexTree
, "date" ) != 0 ) {
816 strcpy ( dirname2
, dirname
);
817 if ( strcmp ( OverwriteReport
, "no" ) == 0 ) {
819 if ( access ( wdir
, R_OK
) == 0 ) {
820 sprintf ( wdir
, "%s.%d" , dirname
, num
);
821 sprintf ( per2
, "%s.%d" , per1
, num
);
830 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
831 rename ( dirname2
, wdir
);
834 if ( access ( dir
, R_OK
) == 0 ) {
835 sprintf ( csort
, "rm -r %s" , dir
);
841 strcpy ( dirname2
, wdir
);
842 if ( strcmp ( OverwriteReport
, "no" ) == 0 ) {
844 if ( access ( wdir
, R_OK
) == 0 ) {
845 sprintf ( wdir
, "%s.%d" , dirname2
, num
);
846 sprintf ( per2
, "%s.%d" , per1
, num
);
855 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
856 rename ( dirname2
, wdir
);
857 strcpy ( dirname2
, wdir
);
860 if ( access ( wdir
, R_OK
) == 0 ) {
861 sprintf ( csort
, "rm -r %s" , wdir
);
866 if ( access ( wdir
, R_OK
) != 0 )
870 strcpy ( dirname2
, wdir
);
871 sprintf ( images
, "%simages" , outdir
);
874 sprintf ( wdir
, "date \" +%%a %%b %%d %%H:%%M:%%S %%Z %%Y \" >%s/sarg-date" , dirname
);
875 cstatus
= system ( wdir
);
876 if (! WIFEXITED ( cstatus
) || WEXITSTATUS ( cstatus
)) {
877 fprintf ( stderr
, "SARG: command return status %d \n " , WEXITSTATUS ( cstatus
));
878 fprintf ( stderr
, "SARG: command: %s \n " , wdir
);
882 sprintf ( per2
, "%s/images" , SYSCONFDIR
);
884 dirp
= opendir ( per2
);
886 fprintf ( stderr
, "SARG: (util) %s %s: %s \n " , "Can't open directory" , per2
, strerror ( errno
));
889 while (( direntp
= readdir ( dirp
)) != NULL
){
890 if ( strncmp ( direntp
-> d_name
, "." , 1 ) == 0 )
892 sprintf ( val10
, "%s/%s" , per2
, direntp
-> d_name
);
893 sprintf ( val11
, "%s/%s" , images
, direntp
-> d_name
);
894 img_in
= fopen ( val10
, "rb" );
896 img_ou
= fopen ( val11
, "wb" );
898 while (( c
= fgetc ( img_in
))!= EOF
) {
903 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open/create file" , val11
, strerror ( errno
));
906 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open file" , val10
, strerror ( errno
));
908 ( void ) rewinddir ( dirp
);
909 ( void ) closedir ( dirp
);
917 void strip_latin ( char * line
)
922 while ( strstr ( line
, "&" ) != 0 ){
923 if ( getword_multisep ( warea
, sizeof ( warea
), line
, '&' )< 0 ) {
924 printf ( "SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities. \n " );
927 strncat ( warea
, line
, 1 );
928 if ( getword_multisep ( buf
, sizeof ( buf
), line
, ';' )< 0 ) {
929 printf ( "SARG: Maybe you have a broken record or garbage in a line to strip from HTML entities. \n " );
940 void zdate ( char * ftime
, const char * DateFormat
)
947 local
= localtime (& t
);
948 if ( strcmp ( DateFormat
, "u" ) == 0 )
949 strftime ( ftime
, 127 , "%b/%d/%Y %H:%M" , local
);
950 if ( strcmp ( DateFormat
, "e" ) == 0 )
951 strftime ( ftime
, 127 , "%d/%b/%Y-%H:%M" , local
);
952 if ( strcmp ( DateFormat
, "w" ) == 0 )
953 strftime ( ftime
, 127 , "%V-%H-%M" , local
);
959 char * fixtime ( long int elap
)
962 int num
= elap
/ 1000 ;
968 if ( strcmp ( datetimeby
, "bytes" ) == 0 ) {
969 strcpy ( buf
, fixnum ( elap
, 1 ));
974 sprintf ( buf
, "00:00:%02ld" , elap
);
979 min
=( num
% 3600 ) / 60 ;
982 if ( hor
== 0 && min
== 0 && sec
== 0 )
985 sprintf ( buf
, "%01d:%02d:%02d" , hor
, min
, sec
);
992 void date_from ( char * date
, char * dfrom
, char * duntil
)
1001 static char wdate
[ 50 ];
1005 if ( strchr ( wdate
, '-' ) == NULL
) {
1011 if ( getword ( diaf
, sizeof ( diaf
), wdate
, '/' )< 0 || getword ( mesf
, sizeof ( mesf
), wdate
, '/' )< 0 || getword ( anof
, sizeof ( anof
), wdate
, '-' )< 0 ||
1012 getword ( diau
, sizeof ( diau
), wdate
, '/' )< 0 || getword ( mesu
, sizeof ( mesu
), wdate
, '/' )< 0 || getword ( anou
, sizeof ( anou
), wdate
, 0 )< 0 ) {
1013 printf ( "SARG: Maybe you have a broken record or garbage in a date. \n " );
1017 sprintf ( dfrom
, "%s%s%s" , anof
, mesf
, diaf
);
1018 sprintf ( duntil
, "%s%s%s" , anou
, mesu
, diau
);
1023 char * strlow ( char * string
)
1029 for ( s
= string
; * s
; ++ s
)
1039 char * strup ( char * string
)
1045 for ( s
= string
; * s
; ++ s
)
1053 void subs ( char * str
, int size
, char * from
, char * to
)
1061 tmp
= strstr ( str
, from
);
1071 if ( ss
+ shift
>= size
){
1072 fprintf ( stderr
, "SARG: Cannot replace %s by %s in %s and fit within %d bytes \n " , from
, to
, str
, size
);
1075 for ( i
= strlen ( tmp
) ; i
>= sf
; i
--)
1076 tmp
[ i
+ shift
]= tmp
[ i
];
1077 } else if ( shift
< 0 ) {
1079 for ( i
= sf
; i
<= len
; i
++)
1080 tmp
[ i
+ shift
]= tmp
[ i
];
1082 memcpy ( tmp
, to
, st
);
1087 void removetmp ( const char * outdir
)
1093 if ( strcmp ( RemoveTempFiles
, "yes" ) != 0 )
1097 debuga ( "%s: sarg-general, sarg-period" , text
[ 82 ]);
1099 if ( snprintf ( warea
, sizeof ( warea
), "%s/sarg-general" , outdir
)>= sizeof ( warea
)) {
1100 fprintf ( stderr
, "SARG: (removetmp) directory too long to remove: %s/sarg-period \n " , outdir
);
1103 if (( fp_in
= fopen ( warea
, "r" ))== NULL
){
1104 fprintf ( stderr
, "===SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1107 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
1108 if ( strncmp ( buf
, "TOTAL" , 5 ) == 0 )
1112 if (( fp_in
= fopen ( warea
, "w" ))== NULL
){
1113 fprintf ( stderr
, "SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1118 if ( snprintf ( warea
, sizeof ( warea
), "%s/sarg-period" , outdir
)>= sizeof ( warea
)) {
1119 fprintf ( stderr
, "SARG: (removetmp) directory too long to remove: %s/sarg-period \n " , outdir
);
1127 void load_excludecodes ( const char * ExcludeCodes
)
1134 if ( ExcludeCodes
[ 0 ] == '\0' )
1137 if (( excludecode
=( char *) malloc ( 1024 ))== NULL
) {
1138 fprintf ( stderr
, "SARG: %s (1024): \n " , text
[ 59 ]);
1141 bzero ( excludecode
, 1024 );
1143 if (( fp_in
= fopen ( ExcludeCodes
, "r" ))== NULL
) {
1144 fprintf ( stderr
, "SARG: (util) Cannot open file: %s (exclude_codes) \n " , ExcludeCodes
);
1148 while ( fgets ( data
, sizeof ( data
), fp_in
)!= NULL
) {
1149 for ( i
= strlen ( data
)- 1 ; i
>= 0 && ( unsigned char ) data
[ i
]<= ' ' ; i
--) data
[ i
]= 0 ;
1151 strcat ( excludecode
, data
);
1152 strcat ( excludecode
, ";" );
1160 void free_excludecodes ( void )
1168 int vercode ( const char * code
)
1173 if ( excludecode
&& excludecode
[ 0 ]!= 0 ) {
1175 for ( cod
= excludecode
; cod
; cod
= strchr ( cod
+ 1 , ';' )) {
1176 if ( strncmp ( code
, cod
, clen
)== 0 && cod
[ clen
]== ';' )
1183 void fixnone ( char * str
)
1187 for ( i
= strlen ( str
)- 1 ; i
>= 0 && ( unsigned char ) str
[ i
]<= ' ' ; i
--);
1188 if ( i
== 3 && strncmp ( str
, "none" , 4 ) == 0 )
1194 #ifdef LEGACY_TESTVALIDUSERCHAR
1195 int testvaliduserchar ( const char * user
)
1201 for ( y
= 0 ; y
< strlen ( UserInvalidChar
); y
++) {
1202 for ( x
= 0 ; x
< strlen ( user
); x
++) {
1203 if ( user
[ x
] == UserInvalidChar
[ y
])
1210 int testvaliduserchar ( const char * user
)
1213 char * p_UserInvalidChar
= UserInvalidChar
;
1214 const char * p_user
;
1216 while ( * p_UserInvalidChar
) {
1219 if ( * p_UserInvalidChar
== * p_user
)
1223 p_UserInvalidChar
++ ;
1229 int compar ( const void * a
, const void * b
)
1230 { if ( *( int *) a
> *( int *) b
) return 1 ;
1231 if ( *( int *) a
< *( int *) b
) return - 1 ;
1235 int getnumlist ( char * buf
, numlist
* list
, const int len
, const int maxvalue
)
1237 int i
, j
, d
, flag
, r1
, r2
;
1238 char * pbuf
, ** bp
, * strbufs
[ 24 ];
1241 strtok ( buf
, " \t " );
1242 for ( * bp
= strtok ( NULL
, "," ), list
-> len
= 0 ; * bp
; * bp
= strtok ( NULL
, "," ) ) {
1243 if ( ++ bp
>= & strbufs
[ 24 ] )
1250 for ( i
= 0 ; i
< list
-> len
; i
++ ) {
1251 if ( strchr ( strbufs
[ i
], '-' ) != 0 ) {
1252 pbuf
= strbufs
[ i
];
1253 strtok ( pbuf
, "-" );
1254 pbuf
= strtok ( NULL
, "\0" );
1255 r1
= atoi ( strbufs
[ i
] );
1256 if ( ( r2
= atoi ( pbuf
) ) >= maxvalue
|| r1
>= r2
)
1258 if ( i
+ d
+ ( r2
- r1
) + 1 <= len
) {
1259 for ( j
= r1
; j
<= r2
; j
++ )
1260 list
-> list
[ i
+ d
++ ] = j
;
1265 if ( ( list
-> list
[ i
+ d
] = atoi ( strbufs
[ i
] ) ) >= maxvalue
)
1269 qsort ( list
-> list
, list
-> len
, sizeof ( int ), compar
);
1272 for ( i
= 0 ; i
< list
-> len
- 1 ; i
++ )
1273 if ( list
-> list
[ i
] == list
-> list
[ i
+ 1 ] ) {
1274 for ( j
= i
+ 1 ; j
< list
-> len
; j
++ )
1275 list
-> list
[ j
- 1 ] = list
-> list
[ j
];
1285 void show_info ( FILE * fp_ou
)
1289 if ( strcmp ( ShowSargInfo
, "yes" ) != 0 ) return ;
1290 zdate ( ftime
, DateFormat
);
1291 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
);
1294 void show_sarg ( FILE * fp_ou
, const char * ind
)
1296 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
);
1299 char * get_size ( const char * path
, const char * file
)
1305 if ( snprintf ( cmd
, sizeof ( cmd
), "du -skh %s%s" , path
, file
)>= sizeof ( cmd
)) {
1306 printf ( "SARG: Cannot get disk space because the path %s%s is too long. \n " , path
, file
);
1309 fp
= popen ( cmd
, "r" );
1310 fgets ( response
, sizeof ( response
), fp
);
1311 if ( getword_multisep ( val5
, sizeof ( val5
), response
, ' \t ' )< 0 ) {
1312 printf ( "SARG: Maybe the command %s failed. \n " , cmd
);
1321 void write_html_header ( FILE * fp_ou
, const char * ind
)
1323 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
);
1325 fprintf ( fp_ou
, "<body style= \" font-family:%s;font-size:%s;background-color:%s;background-image:url(%s) \" > \n " , FontFace
, TitleFontSize
, BgColor
, BgImage
);
1326 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
);
1327 show_sarg ( fp_ou
, ind
);
1328 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
);
1333 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1334 printf ( "SARG: MALICIUS CODE DETECTED. \n " );
1335 printf ( "SARG: I think someone is trying to execute arbitrary code in your system using sarg. \n " );
1336 printf ( "SARG: please review your access.log and/or your useragent.log file. \n " );
1337 printf ( "SARG: process stoped. No actions taken. \n " );
1338 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1340 system ( "rm -rf /tmp/sarg" );
1341 sprintf ( tmp4
, "rm -rf %s" , dirname
);
1343 system ( "rm -rf /tmp/sarg" );
1349 void url_module ( const char * url
, char * w2
)
1355 for ( x
= strlen ( url
)- 1 ; x
>= 0 ; x
--) {
1356 if ( url
[ x
] == '/' || y
>= sizeof ( w
)- 1 ) break ;
1361 for ( y
= y
- 1 ; y
>= 0 ; y
--) {
1368 void write_html_trailer ( FILE * fp_ou
)
1370 fputs ( "</table></center> \n " , fp_ou
);
1371 zdate ( ftime
, DateFormat
);
1373 fputs ( "</body> \n </html> \n " , fp_ou
);
1378 printf ( "SARG Version: %s \n " , VERSION
);
1382 char * get_param_value ( const char * param
, char * line
)
1386 while (* line
== ' ' || * line
== ' \t ' ) line
++;
1388 if ( strncasecmp ( line
, param
, plen
)) return ( NULL
);
1389 if ( line
[ plen
]!= ' ' && line
[ plen
]!= ' \t ' ) return ( NULL
);
1391 while (* line
== ' ' || * line
== ' \t ' ) line
++;
1395 void read_usertab ( const char * UserTabFile
)
1405 if ( UserTabFile
[ 0 ] != '\0' ) {
1407 debuga ( "%s: %s" , text
[ 86 ], UserTabFile
);
1409 if (( fp_usr
= fopen ( UserTabFile
, "r" ))== NULL
) {
1410 fprintf ( stderr
, "SARG: (log) %s: %s - %s \n " , text
[ 45 ], UserTabFile
, strerror ( errno
));
1413 fseek ( fp_usr
, 0 , SEEK_END
);
1414 nreg
= ftell ( fp_usr
)+ 100 ;
1415 fseek ( fp_usr
, 0 , SEEK_SET
);
1416 if (( userfile
=( char *) malloc ( nreg
))== NULL
){
1417 fprintf ( stderr
, "SARG ERROR: %s" , text
[ 87 ]);
1420 strcpy ( userfile
, " \t " );
1422 while ( fgets ( buf
, sizeof ( buf
), fp_usr
)!= NULL
) {
1423 if ( buf
[ 0 ]== '#' ) continue ;
1424 for ( i
= strlen ( buf
)- 1 ; i
>= 0 && ( unsigned char ) buf
[ i
]<= ' ' ; i
--) buf
[ i
]= 0 ;
1425 if ( getword_multisep ( bufy
, sizeof ( bufy
), buf
, ' ' )< 0 ) {
1426 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , UserTabFile
);
1429 if ( z2
+ strlen ( bufy
)+ strlen ( buf
)+ 3 >= nreg
) {
1430 printf ( "SARG: The list of the users is too long in your %s file. \n " , UserTabFile
);
1433 for ( z1
= 0 ; bufy
[ z1
]; z1
++)
1434 userfile
[ z2
++]= bufy
[ z1
];
1435 userfile
[ z2
++]= ' \n ' ;
1436 for ( z1
= 0 ; buf
[ z1
]; z1
++)
1437 userfile
[ z2
++]= buf
[ z1
];
1438 userfile
[ z2
++]= ' \t ' ;
1445 void get_usertab_name ( const char * user
, char * name
, int namelen
)
1451 if ( UserTabFile
[ 0 ] == '\0' ) {
1452 strncpy ( name
, user
, namelen
);
1455 sprintf ( warea
, " \t %s \n " , user
);
1456 if (( str
=( char *) strstr ( userfile
, warea
)) == ( char *) NULL
) {
1457 strncpy ( name
, user
, namelen
);
1460 str
= strchr ( str
+ 1 , ' \n ' );
1462 for ( z1
= 0 ; * str
!= ' \t ' && z1
< namelen
; z1
++) {
1470 int is_download_suffix ( const char * url
)
1478 if ( DownloadSuffix
[ 0 ]== 0 ) return ( 0 );
1480 urllen
= strlen ( url
)- 1 ;
1481 for ( i
= 0 ; i
< sizeof ( suffix
)- 1 && i
< urllen
; i
++) {
1482 if ( url
[ urllen
- i
]== '.' ) {
1483 if ( i
== 0 ) return ( 0 ); //detect a single trailing dot
1484 strcpy ( suffix
, url
+ urllen
- i
+ 1 );
1485 suflen
= strlen ( suffix
);
1486 for ( str
= DownloadSuffix
; str
; str
= strchr ( str
, ',' )) {
1487 if (* str
== ',' ) str
++;
1488 if ( strncasecmp ( str
, suffix
, suflen
)== 0 && ( str
[ suflen
]== ',' || str
[ suflen
]== 0 ))