]>
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 #if defined(HAVE_BACKTRACE)
33 #define USE_GETWORD_BACKTRACE 1
35 #define USE_GETWORD_BACKTRACE 0
38 static char mtab1
[ 12 ][ 4 ]={ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" };
40 //! The list of the HTTP codes to exclude from the report.
41 static char * excludecode
= NULL
;
43 /*void fgetword(char *word, char *line, int stop)
48 for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x];
55 tchar = strchr(line, stop);
56 if (tchar == NULL) strcpy(word, line);
59 difflen = tchar - line;
60 strncpy(word, line, difflen);
65 #if USE_GETWORD_BACKTRACE
66 static void getword_backtrace ( void )
72 n
= backtrace ( buffer
, sizeof ( buffer
)/ sizeof ( buffer
[ 0 ]));
74 calls
= backtrace_symbols ( buffer
, n
);
76 fprintf ( stderr
, "SARG: getword backtrace: \n " );
77 for ( i
= 0 ; i
< n
; i
++) {
78 fprintf ( stderr
, "SARG: %d:%s \n " , i
+ 1 , calls
[ i
]);
83 #endif //USE_GETWORD_BACKTRACE
85 void getword_start ( struct getwordstruct
* gwarea
, const char * line
)
87 gwarea
-> beginning
= line
;
91 void getword_restart ( struct getwordstruct
* gwarea
)
93 gwarea
-> current
= gwarea
-> beginning
;
96 int getword ( char * word
, int limit
, struct getwordstruct
* gwarea
, int stop
)
100 for ( x
= 0 ;(( gwarea
-> current
[ x
]) && ( gwarea
-> current
[ x
] != stop
)); x
++) {
102 printf ( "SARG: getword loop detected after %d bytes. \n " , x
);
103 printf ( "SARG: Line= \" %s \"\n " , gwarea
-> beginning
);
104 printf ( "SARG: Record= \" %s \"\n " , gwarea
-> current
);
105 printf ( "SARG: searching for \' x%x \'\n " , stop
);
106 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
107 word
[( limit
> 0 ) ? limit
- 1 : 0 ]= '\0' ;
108 #if USE_GETWORD_BACKTRACE
113 word
[ x
] = gwarea
-> current
[ x
];
117 if ( gwarea
-> current
[ x
]) ++ x
;
122 int getword_multisep ( char * word
, int limit
, struct getwordstruct
* gwarea
, int stop
)
126 for ( x
= 0 ;(( gwarea
-> current
[ x
]) && ( gwarea
-> current
[ x
] != stop
)); x
++) {
128 printf ( "SARG: getword_multisep loop detected. \n " );
129 printf ( "SARG: Line= \" %s \"\n " , gwarea
-> beginning
);
130 printf ( "SARG: Record= \" %s \"\n " , gwarea
-> current
);
131 printf ( "SARG: searching for \' x%x \'\n " , stop
);
132 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
133 if ( limit
> 0 ) word
[ limit
- 1 ]= '\0' ;
134 #if USE_GETWORD_BACKTRACE
140 word
[ x
] = gwarea
-> current
[ x
];
144 while ( gwarea
-> current
[ x
] && gwarea
-> current
[ x
]== stop
) ++ x
;
149 int getword_skip ( int limit
, struct getwordstruct
* gwarea
, int stop
)
153 for ( x
= 0 ;( gwarea
-> current
[ x
] && ( gwarea
-> current
[ x
] != stop
)); x
++) {
155 printf ( "SARG: getword_skip loop detected after %d bytes. \n " , x
);
156 printf ( "SARG: Line= \" %s \"\n " , gwarea
-> beginning
);
157 printf ( "SARG: Record= \" %s \"\n " , gwarea
-> current
);
158 printf ( "SARG: searching for \' x%x \'\n " , stop
);
159 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
160 #if USE_GETWORD_BACKTRACE
167 if ( gwarea
-> current
[ x
]) ++ x
;
172 int getword_atoll ( long long int * number
, struct getwordstruct
* gwarea
, int stop
)
177 if ( gwarea
-> current
[ 0 ] == '-' ) {
180 } else if ( gwarea
-> current
[ 0 ] == '+' ) {
184 for ( x
= 0 ; isdigit ( gwarea
-> current
[ x
]); x
++) {
185 * number
=(* number
* 10 ) + gwarea
-> current
[ x
]- '0' ;
187 if ( gwarea
-> current
[ x
] && gwarea
-> current
[ x
]!= stop
) {
188 printf ( "SARG: getword_atoll loop detected after %d bytes. \n " , x
);
189 printf ( "SARG: Line= \" %s \"\n " , gwarea
-> beginning
);
190 printf ( "SARG: Record= \" %s \"\n " , gwarea
-> current
);
191 printf ( "SARG: searching for \' x%x \'\n " , stop
);
192 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
193 #if USE_GETWORD_BACKTRACE
200 if ( gwarea
-> current
[ x
]) ++ x
;
206 #define MAXLLL 30 //!< Maximum number of digits in long long (a guess).
207 long long int my_atoll ( const char * nptr
)
209 long long int returnval
= 0LL ;
210 int max_digits
= MAXLLL
;
212 // Soak up all the white space
213 while ( isspace ( * nptr
)) {
217 //For each character left to right
218 //change the character to a single digit
219 //multiply what we had before by 10 and add the new digit
221 while (-- max_digits
&& isdigit ( * nptr
))
223 returnval
= ( returnval
* 10 ) + ( * nptr
++ - '0' ) ;
229 int is_absolute ( const char * path
)
231 if (* path
== '/' ) return ( 1 );
233 if ( isalpha ( path
[ 0 ]) && path
[ 1 ]== ':' ) return ( 1 );
238 void my_mkdir ( const char * name
)
244 if (! is_absolute ( name
)) {
245 fprintf ( stderr
, "SARG: Invalid path (%s). Please, use absolute paths only. \n " , name
);
246 fprintf ( stderr
, "SARG: process aborted. \n " );
251 for ( i
= 0 ; name
[ i
] ; i
++) {
253 fprintf ( stderr
, "SARG: directory name too long %s \n " , name
);
256 if ( chars
> 0 && name
[ i
] == '/' ) {
258 if ( access ( w0
, R_OK
) != 0 ) {
260 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w0
, strerror ( errno
));
261 fprintf ( stderr
, "SARG: process aborted. \n " );
266 if ( name
[ i
] != '/' ) chars
++;
270 if ( access ( name
, R_OK
) != 0 ) {
271 if ( mkdir ( name
, 0755 )) {
272 fprintf ( stderr
, "SARG: mkdir %s %s \n " , name
, strerror ( errno
));
273 fprintf ( stderr
, "SARG: process aborted. \n " );
280 void my_lltoa ( unsigned long long int n
, char s
[], int len
)
288 s
[ slen
++] = ( n
% 10 ) + '0' ;
289 } while (( n
/= 10 ) > 0 );
292 for ( i
= 0 , j
= slen
- 1 ; i
< j
; i
++, j
--) {
300 for ( j
= slen
; j
>= 0 ; j
--)
308 void builddia ( char * dia
, const char * mes
, const char * ano
, const char * df
, char * wdata
)
313 if ( strlen ( dia
) < 1 ) return ;
315 for ( nmes
= 0 ; nmes
< 12 ; nmes
++) {
316 if ( strcmp ( mtab1
[ nmes
], mes
) == 0 ) {
322 snprintf ( wdata
, 9 , "%s%02d%s" , ano
, nmes
, dia
);
325 snprintf ( ndia
, sizeof ( ndia
), "%s/%02d/%s" , dia
, nmes
, ano
);
327 snprintf ( ndia
, sizeof ( ndia
), "%02d/%s/%s" , nmes
, dia
, ano
);
334 void buildymd ( const char * dia
, const char * mes
, const char * ano
, char * wdata
)
338 for ( nmes
= 0 ; nmes
< 12 ; nmes
++) {
339 if ( strcmp ( mtab1
[ nmes
], mes
) == 0 )
343 sprintf ( wdata
, "%04d%02d%02d" , atoi ( ano
), nmes
+ 1 , atoi ( dia
));
348 void conv_month ( char * month
)
352 for ( x
= 0 ; x
< 12 && strcmp ( mtab1
[ x
], month
)!= 0 ; x
++);
353 sprintf ( month
, "%02d" , x
+ 1 );
357 void conv_month_name ( char * month
)
363 strcpy ( month
, mtab1
[ x
- 1 ]);
367 void name_month ( char * month
, int month_len
)
369 int x
, z
= atoi ( month
)- 1 ;
372 struct getwordstruct gwarea
;
375 getword_start (& gwarea
, m
);
378 if ( getword_multisep ( w
, sizeof ( w
),& gwarea
, ',' )< 0 ) {
379 printf ( "SARG: Maybe you have a broken record or garbage in the names of the months. \n " );
382 if ( getword_multisep ( month
, month_len
,& gwarea
, ',' )< 0 ) {
383 printf ( "SARG: Maybe you have a broken record or garbage in the name of the months. \n " );
389 void fixper ( char * tbuf
, char * period
, const char * duntil
)
392 char dia
[ 5 ], mes
[ 5 ], ano
[ 5 ];
395 strncpy ( dia
, duntil
+ 6 , 2 );
397 strncpy ( mes
, duntil
+ 4 , 2 );
399 strncpy ( ano
, duntil
, 4 );
404 strcpy ( mes
, mtab1
[ x
- 1 ]);
406 if ( strcmp ( df
, "e" ) == 0 )
407 sprintf ( warea
, "%s%s%s" , dia
, mes
, ano
);
408 else if ( strcmp ( df
, "u" ) == 0 )
409 sprintf ( warea
, "%s%s%s" , ano
, mes
, dia
);
413 strcat ( period
, warea
);
417 void debuga ( const char * msg
,...)
421 fputs ( "SARG: " , stderr
);
423 vfprintf ( stderr
, msg
, ap
);
429 void debugaz ( const char * head
, const char * msg
)
431 fprintf ( stderr
, "SARG: (util) %s=%s \n " , head
, msg
);
443 for ( i
= 0 ; ip
[ i
]; i
++) {
465 char * fixnum ( long long int value
, int n
)
467 #define MAXLEN_FIXNUM 1024
468 char num
[ MAXLEN_FIXNUM
]= "" ;
469 char buf
[ MAXLEN_FIXNUM
* 2 ];
471 static char ret
[ MAXLEN_FIXNUM
* 2 ];
473 register int i
, j
, k
;
475 static char abbrev
[ 30 ];
477 my_lltoa ( value
, num
, 0 );
479 if ( strcmp ( DisplayedValues
, "abbreviation" ) == 0 ) {
480 numlen
= strlen ( num
);
482 sprintf ( abbrev
, "%s" , num
);
483 if ( numlen
== 4 || numlen
== 7 || numlen
== 10 || numlen
== 13 ) {
484 snprintf ( abbrev
, 2 , "%s" , num
);
485 strncat ( abbrev
, "." , 1 );
486 strncat ( abbrev
, num
+ 1 , 2 );
487 if (! n
) return ( abbrev
);
489 strncat ( abbrev
, "K" , 1 );
491 strncat ( abbrev
, "M" , 1 );
492 else if ( numlen
== 10 )
493 strncat ( abbrev
, "G" , 1 );
494 else if ( numlen
== 13 )
495 strncat ( abbrev
, "T" , 1 );
497 if ( numlen
== 5 || numlen
== 8 || numlen
== 11 || numlen
== 14 ) {
498 snprintf ( abbrev
, 3 , "%s" , num
);
499 strncat ( abbrev
, "." , 1 );
500 strncat ( abbrev
, num
+ 2 , 2 );
501 if (! n
) return ( abbrev
);
503 strncat ( abbrev
, "K" , 1 );
505 strncat ( abbrev
, "M" , 1 );
506 else if ( numlen
== 11 )
507 strncat ( abbrev
, "G" , 1 );
508 else if ( numlen
== 14 )
509 strncat ( abbrev
, "T" , 1 );
511 if ( numlen
== 6 || numlen
== 9 || numlen
== 12 || numlen
== 15 ) {
512 snprintf ( abbrev
, 4 , "%s" , num
);
513 strncat ( abbrev
, "." , 1 );
514 strncat ( abbrev
, num
+ 3 , 2 );
515 if (! n
) return ( abbrev
);
517 strncat ( abbrev
, "K" , 1 );
519 strncat ( abbrev
, "M" , 1 );
520 else if ( numlen
== 12 )
521 strncat ( abbrev
, "G" , 1 );
522 else if ( numlen
== 15 )
523 strncat ( abbrev
, "T" , 1 );
529 bzero ( buf
, MAXLEN_FIXNUM
* 2 );
535 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
536 if ( k
== 2 && i
!= 0 ) {
539 pbuf
[ j
++] = ( UseComma
) ? ',' : '.' ;
549 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
558 char * fixnum2 ( long long int value
, int n
)
560 #define MAXLEN_FIXNUM2 1024
561 char num
[ MAXLEN_FIXNUM2
];
562 char buf
[ MAXLEN_FIXNUM2
* 2 ];
564 static char ret
[ MAXLEN_FIXNUM2
* 2 ];
566 register int i
, j
, k
;
568 my_lltoa ( value
, num
, 0 );
569 bzero ( buf
, MAXLEN_FIXNUM2
* 2 );
575 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
576 if ( k
== 2 && i
!= 0 ) {
579 pbuf
[ j
++] = ( UseComma
) ? ',' : '.' ;
589 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
599 void buildhref ( char * href
)
603 if ( strcmp ( href
, "./" ) == 0 ){
605 strcat ( href
, "<a href='" );
609 href
[ strlen ( href
)- 1 ]= '\0' ;
610 sprintf ( whref
, "%s" , strrchr ( href
, '/' ));
612 strcpy ( href
, "<a href='" );
621 char * buildtime ( long long int elap
)
624 int num
= elap
/ 1000 ;
633 min
=( num
% 3600 ) / 60 ;
635 sprintf ( buf
, "%02d:%02d:%02d" , hor
, min
, sec
);
642 void obtdate ( const char * dirname
, const char * name
, char * data
)
648 sprintf ( wdir
, "%s%s/sarg-date" , dirname
, name
);
649 if (( fp_in
= fopen ( wdir
, "rt" )) == 0 ) {
650 sprintf ( wdir
, "%s%s/date" , dirname
, name
);
651 if (( fp_in
= fopen ( wdir
, "rt" )) == 0 ) {
657 if (! fgets ( data
, 80 , fp_in
)) {
658 fprintf ( stderr
, "Failed to read the date in %s \n " , wdir
);
669 void formatdate ( char * date
, int date_size
, int year
, int month
, int day
, int hour
, int minute
, int second
, int dst
)
675 memset (& ltm
, 0 , sizeof ( ltm
));
676 if ( year
>= 1900 ) ltm
. tm_year
= year
- 1900 ;
677 if ( month
>= 1 && month
<= 12 ) ltm
. tm_mon
= month
- 1 ;
678 if ( day
>= 1 && day
<= 31 ) ltm
. tm_mday
= day
;
679 if ( hour
>= 0 && hour
< 24 ) ltm
. tm_hour
= hour
;
680 if ( minute
>= 0 && minute
< 60 ) ltm
. tm_min
= minute
;
681 if ( second
>= 0 && second
< 60 ) ltm
. tm_sec
= second
;
683 unixtime
= mktime (& ltm
); //fill the missing entries
684 fulltm
= localtime (& unixtime
);
685 //strftime(date,date_size,"%a %b %d %H:%M:%S %Z %Y",fulltm);
686 strftime ( date
, date_size
, "%c" , fulltm
);
690 void obtuser ( const char * dirname
, const char * name
, char * tuser
)
696 sprintf ( wdir
, "%s%s/sarg-users" , dirname
, name
);
697 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
698 sprintf ( wdir
, "%s%s/users" , dirname
, name
);
699 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
705 if (! fgets ( tuser
, 20 , fp_in
)) {
706 fprintf ( stderr
, "Failed to read the user in %s \n " , wdir
);
717 void obttotal ( const char * dirname
, const char * name
, char * tbytes
, const char * tuser
, char * media
)
725 long long int wtuser
= 0 ;
726 long long int twork
= 0 ;
727 struct getwordstruct gwarea
;
733 sprintf ( wdir
, "%s%s/sarg-general" , dirname
, name
);
734 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
735 sprintf ( wdir
, "%s%s/general" , dirname
, name
);
736 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
741 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
742 if ( strncmp ( buf
, "TOTAL \t " , 6 ) == 0 )
744 else if ( strncmp ( buf
, "TOTAL " , 6 ) == 0 )
748 getword_start (& gwarea
, buf
);
749 if ( getword ( warea
, sizeof ( warea
),& gwarea
, sep
)< 0 ) {
750 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
753 if ( strcmp ( warea
, "TOTAL" ) != 0 )
755 if ( getword_skip ( MAXLEN
,& gwarea
, sep
)< 0 ) {
756 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
759 if ( getword_atoll (& twork
,& gwarea
, sep
)< 0 ) {
760 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
763 strcpy ( tbytes
, fixnum ( twork
, 1 ));
768 wtuser
= my_atoll ( tuser
);
775 strcpy ( media
, fixnum ( med
, 1 ));
781 void gperiod ( const char * dirname
, const char * period
)
785 char wdirname
[ MAXLEN
];
787 strcpy ( wdirname
, dirname
);
788 strcat ( wdirname
, "/sarg-period" );
790 if (( fp_ou
= fopen ( wdirname
, "w" ))== NULL
){
791 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
799 debuga ( "%s" ,( char *) text
[ 50 ]);
805 static void copy_images ( void )
807 FILE * img_in
, * img_ou
;
810 char srcfile
[ MAXLEN
];
811 char dstfile
[ MAXLEN
];
813 struct dirent
* direntp
;
818 if ( snprintf ( images
, sizeof ( images
), "%simages" , outdir
)>= sizeof ( images
)) {
819 fprintf ( stderr
, "SARG: Cannot copy images to target directory %simages \n " , outdir
);
822 if ( access ( images
, R_OK
)!= 0 ) {
826 strcpy ( imgdir
, IMAGEDIR
);
827 dirp
= opendir ( imgdir
);
829 fprintf ( stderr
, "SARG: (util) %s %s: %s \n " , "Can't open directory" , imgdir
, strerror ( errno
));
832 while (( direntp
= readdir ( dirp
)) != NULL
){
833 if ( direntp
-> d_name
[ 0 ]== '.' )
835 sprintf ( srcfile
, "%s/%s" , imgdir
, direntp
-> d_name
);
836 if ( stat ( srcfile
,& info
)) {
837 fprintf ( stderr
, "SARG: Cannot stat \" %s \" - %s \n " , srcfile
, strerror ( errno
));
840 if ( S_ISREG ( info
. st_mode
)) {
841 sprintf ( dstfile
, "%s/%s" , images
, direntp
-> d_name
);
842 img_in
= fopen ( srcfile
, "rb" );
844 img_ou
= fopen ( dstfile
, "wb" );
846 while (( nread
= fread ( buffer
, 1 , sizeof ( buffer
), img_in
))> 0 ) {
847 if ( fwrite ( buffer
, 1 , nread
, img_ou
)!= nread
) {
848 fprintf ( stderr
, "SARG: Failed to copy image %s to %s \n " , srcfile
, dstfile
);
854 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open/create file" , dstfile
, strerror ( errno
));
857 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open file" , srcfile
, strerror ( errno
));
860 ( void ) closedir ( dirp
);
865 void vrfydir ( const char * per1
, const char * addr
, const char * site
, const char * us
, const char * form
)
871 char dirname2
[ MAXLEN
];
878 if ( IndexTree
== INDEX_TREE_DATE
) {
879 bzero ( y1
, sizeof ( y1
));
880 bzero ( y2
, sizeof ( y2
));
881 bzero ( d1
, sizeof ( d1
));
882 bzero ( d2
, sizeof ( d2
));
883 bzero ( m1
, sizeof ( m1
));
884 bzero ( m2
, sizeof ( m2
));
885 if ( strncmp ( df
, "u" , 1 ) == 0 ) {
886 strncpy ( y1
, period
, 4 );
887 strncpy ( y2
, period
+ 10 , 4 );
888 strncpy ( m1
, period
+ 4 , 3 );
889 strncpy ( m2
, period
+ 14 , 3 );
890 strncpy ( d1
, period
+ 7 , 2 );
891 strncpy ( d2
, period
+ 17 , 2 );
892 } else if ( strncmp ( df
, "e" , 1 ) == 0 ) {
893 strncpy ( d1
, period
+ 0 , 2 );
894 strncpy ( d2
, period
+ 10 , 2 );
895 strncpy ( m1
, period
+ 2 , 3 );
896 strncpy ( m2
, period
+ 12 , 3 );
897 strncpy ( y1
, period
+ 5 , 4 );
898 strncpy ( y2
, period
+ 15 , 4 );
903 sprintf ( wdir
, "%s%s" , outdir
, y1
);
904 if ( strcmp ( y1
, y2
) != 0 ) {
908 if ( access ( wdir
, R_OK
) != 0 )
913 if ( strcmp ( m1
, m2
) != 0 ) {
917 if ( access ( wdir
, R_OK
) != 0 )
922 if ( strcmp ( d1
, d2
) != 0 ) {
927 sprintf ( wdir
, "%s%s" , outdir
, per1
);
934 if ( addr
[ 0 ] != '\0' ) {
938 if ( site
[ 0 ] != '\0' ) {
943 strcpy ( dirname
, wdir
);
945 if ( IndexTree
!= INDEX_TREE_DATE
) {
946 if (! OverwriteReport
) {
948 if ( access ( wdir
, R_OK
) == 0 ) {
949 sprintf ( wdir
, "%s.%d" , dirname
, num
);
950 sprintf ( per2
, "%s.%d" , per1
, num
);
959 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname
, text
[ 52 ], wdir
);
960 rename ( dirname
, wdir
);
963 if ( access ( dirname
, R_OK
) == 0 ) {
964 unlinkdir ( dirname
, 1 );
969 strcpy ( dirname2
, wdir
);
970 if (! OverwriteReport
) {
972 if ( access ( wdir
, R_OK
) == 0 ) {
973 sprintf ( wdir
, "%s.%d" , dirname2
, num
);
974 sprintf ( per2
, "%s.%d" , per1
, num
);
983 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
984 rename ( dirname2
, wdir
);
985 strcpy ( dirname2
, wdir
);
988 if ( access ( wdir
, R_OK
) == 0 ) {
993 if ( access ( wdir
, R_OK
) != 0 )
997 strcpy ( dirname2
, wdir
);
999 sprintf ( wdir
, "%s/sarg-date" , dirname
);
1000 if (( fp_ou
= fopen ( wdir
, "wt" )) == 0 ) {
1001 fprintf ( stderr
, "SARG: cannot open %s for writing \n " , wdir
);
1006 //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime));
1007 loctm
= localtime (& curtime
);
1008 strftime ( wdir
, sizeof ( wdir
), "%Y-%m-%d %H:%M:%S" , loctm
);
1009 fprintf ( fp_ou
, "%s %d \n " , wdir
, loctm
-> tm_isdst
);
1015 void strip_latin ( char * line
)
1022 for ( i
= 0 ; line
[ i
]; i
++){
1024 if ( line
[ i
]== ';' ) skip
= 0 ;
1037 void zdate ( char * ftime
, int ftimesize
, const char * DateFormat
)
1044 local
= localtime (& t
);
1045 if ( strcmp ( DateFormat
, "u" ) == 0 )
1046 strftime ( ftime
, ftimesize
, "%b/%d/%Y %H:%M" , local
);
1047 if ( strcmp ( DateFormat
, "e" ) == 0 )
1048 strftime ( ftime
, ftimesize
, "%d/%b/%Y-%H:%M" , local
);
1049 if ( strcmp ( DateFormat
, "w" ) == 0 )
1050 strftime ( ftime
, ftimesize
, "%V-%H-%M" , local
);
1055 char * fixtime ( long int elap
)
1058 int num
= elap
/ 1000 ;
1062 static char buf
[ 12 ];
1064 if ( strcmp ( datetimeby
, "bytes" ) == 0 ) {
1065 strcpy ( buf
, fixnum ( elap
, 1 ));
1070 sprintf ( buf
, "00:00:%02ld" , elap
);
1075 min
=( num
% 3600 ) / 60 ;
1078 if ( hor
== 0 && min
== 0 && sec
== 0 )
1081 sprintf ( buf
, "%01d:%02d:%02d" , hor
, min
, sec
);
1088 void date_from ( char * date
, char * dfrom
, char * duntil
)
1099 strncpy ( wdate
, date
, sizeof ( wdate
)- 1 );
1100 wdate
[ sizeof ( wdate
)- 1 ]= '\0' ;
1101 if ( strchr ( wdate
, '-' ) == NULL
) {
1102 if ( strlen ( wdate
)* 2 + 1 >= sizeof ( wdate
)) {
1103 fprintf ( stderr
, "SARG: Invalid date range passed as argument. \n " );
1111 if ( sscanf ( wdate
, "%d/%d/%d-%d/%d/%d" ,& diaf
,& mesf
,& anof
,& diau
,& mesu
,& anou
)!= 6 ) {
1112 fprintf ( stderr
, "SARG: The date range passed as argument is not formated as dd/mm/yyyy-dd/mm/yyyy. \n " );
1116 sprintf ( dfrom
, "%04d%02d%02d" , anof
, mesf
, diaf
);
1117 sprintf ( duntil
, "%04d%02d%02d" , anou
, mesu
, diau
);
1122 char * strlow ( char * string
)
1128 for ( s
= string
; * s
; ++ s
)
1138 char * strup ( char * string
)
1144 for ( s
= string
; * s
; ++ s
)
1152 void subs ( char * str
, int size
, char * from
, char * to
)
1160 tmp
= strstr ( str
, from
);
1170 if ( ss
+ shift
>= size
){
1171 fprintf ( stderr
, "SARG: Cannot replace %s by %s in %s and fit within %d bytes \n " , from
, to
, str
, size
);
1174 for ( i
= strlen ( tmp
) ; i
>= sf
; i
--)
1175 tmp
[ i
+ shift
]= tmp
[ i
];
1176 } else if ( shift
< 0 ) {
1178 for ( i
= sf
; i
<= len
; i
++)
1179 tmp
[ i
+ shift
]= tmp
[ i
];
1181 memcpy ( tmp
, to
, st
);
1186 void removetmp ( const char * outdir
)
1192 if (! RemoveTempFiles
)
1196 debuga ( "%s: sarg-general, sarg-period" , text
[ 82 ]);
1198 if ( snprintf ( warea
, sizeof ( warea
), "%s/sarg-general" , outdir
)>= sizeof ( warea
)) {
1199 fprintf ( stderr
, "SARG: (removetmp) directory too long to remove: %s/sarg-period \n " , outdir
);
1202 if (( fp_in
= fopen ( warea
, "r" ))== NULL
){
1203 fprintf ( stderr
, "===SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1206 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
1207 if ( strncmp ( buf
, "TOTAL" , 5 ) == 0 )
1211 if (( fp_in
= fopen ( warea
, "w" ))== NULL
){
1212 fprintf ( stderr
, "SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1217 if ( snprintf ( warea
, sizeof ( warea
), "%s/sarg-period" , outdir
)>= sizeof ( warea
)) {
1218 fprintf ( stderr
, "SARG: (removetmp) directory too long to remove: %s/sarg-period \n " , outdir
);
1226 void load_excludecodes ( const char * ExcludeCodes
)
1233 if ( ExcludeCodes
[ 0 ] == '\0' )
1236 if (( excludecode
=( char *) malloc ( 1024 ))== NULL
) {
1237 fprintf ( stderr
, "SARG: %s (1024): \n " , text
[ 59 ]);
1240 bzero ( excludecode
, 1024 );
1242 if (( fp_in
= fopen ( ExcludeCodes
, "r" ))== NULL
) {
1243 fprintf ( stderr
, "SARG: (util) Cannot open file: %s (exclude_codes) \n " , ExcludeCodes
);
1247 while ( fgets ( data
, sizeof ( data
), fp_in
)!= NULL
) {
1248 for ( i
= strlen ( data
)- 1 ; i
>= 0 && ( unsigned char ) data
[ i
]<= ' ' ; i
--) data
[ i
]= 0 ;
1250 strcat ( excludecode
, data
);
1251 strcat ( excludecode
, ";" );
1259 void free_excludecodes ( void )
1267 int vercode ( const char * code
)
1272 if ( excludecode
&& excludecode
[ 0 ]!= 0 ) {
1274 for ( cod
= excludecode
; cod
; cod
= strchr ( cod
+ 1 , ';' )) {
1275 if ( strncmp ( code
, cod
, clen
)== 0 && cod
[ clen
]== ';' )
1282 void fixnone ( char * str
)
1286 for ( i
= strlen ( str
)- 1 ; i
>= 0 && ( unsigned char ) str
[ i
]<= ' ' ; i
--);
1287 if ( i
== 3 && strncmp ( str
, "none" , 4 ) == 0 )
1293 void fixendofline ( char * str
)
1297 for ( i
= strlen ( str
)- 1 ; i
>= 0 && ( unsigned char ) str
[ i
]<= ' ' ; i
--) str
[ i
]= 0 ;
1300 #ifdef LEGACY_TESTVALIDUSERCHAR
1301 int testvaliduserchar ( const char * user
)
1307 for ( y
= 0 ; y
< strlen ( UserInvalidChar
); y
++) {
1308 for ( x
= 0 ; x
< strlen ( user
); x
++) {
1309 if ( user
[ x
] == UserInvalidChar
[ y
])
1316 int testvaliduserchar ( const char * user
)
1319 char * p_UserInvalidChar
= UserInvalidChar
;
1320 const char * p_user
;
1322 while ( * p_UserInvalidChar
) {
1325 if ( * p_UserInvalidChar
== * p_user
)
1329 p_UserInvalidChar
++ ;
1335 int compar ( const void * a
, const void * b
)
1336 { if ( *( int *) a
> *( int *) b
) return 1 ;
1337 if ( *( int *) a
< *( int *) b
) return - 1 ;
1341 int getnumlist ( char * buf
, numlist
* list
, const int len
, const int maxvalue
)
1343 int i
, j
, d
, flag
, r1
, r2
;
1344 char * pbuf
, ** bp
, * strbufs
[ 24 ];
1347 strtok ( buf
, " \t " );
1348 for ( * bp
= strtok ( NULL
, "," ), list
-> len
= 0 ; * bp
; * bp
= strtok ( NULL
, "," ) ) {
1349 if ( ++ bp
>= & strbufs
[ 24 ] )
1356 for ( i
= 0 ; i
< list
-> len
; i
++ ) {
1357 if ( strchr ( strbufs
[ i
], '-' ) != 0 ) {
1358 pbuf
= strbufs
[ i
];
1359 strtok ( pbuf
, "-" );
1360 pbuf
= strtok ( NULL
, "\0" );
1361 r1
= atoi ( strbufs
[ i
] );
1362 if ( ( r2
= atoi ( pbuf
) ) >= maxvalue
|| r1
>= r2
)
1364 if ( i
+ d
+ ( r2
- r1
) + 1 <= len
) {
1365 for ( j
= r1
; j
<= r2
; j
++ )
1366 list
-> list
[ i
+ d
++ ] = j
;
1371 if ( ( list
-> list
[ i
+ d
] = atoi ( strbufs
[ i
] ) ) >= maxvalue
)
1375 qsort ( list
-> list
, list
-> len
, sizeof ( int ), compar
);
1378 for ( i
= 0 ; i
< list
-> len
- 1 ; i
++ )
1379 if ( list
-> list
[ i
] == list
-> list
[ i
+ 1 ] ) {
1380 for ( j
= i
+ 1 ; j
< list
-> len
; j
++ )
1381 list
-> list
[ j
- 1 ] = list
-> list
[ j
];
1391 char * get_size ( const char * path
, const char * file
)
1394 static char response
[ 255 ];
1398 if ( snprintf ( cmd
, sizeof ( cmd
), "du -skh %s%s" , path
, file
)>= sizeof ( cmd
)) {
1399 printf ( "SARG: Cannot get disk space because the path %s%s is too long. \n " , path
, file
);
1402 fp
= popen ( cmd
, "r" );
1403 if (! fgets ( response
, sizeof ( response
), fp
)) {
1404 fprintf ( stderr
, "SARG: Cannot get disk size with command %s" , cmd
);
1407 ptr
= strchr ( response
, ' \t ' );
1409 fprintf ( stderr
, "SARG: The command %s failed. \n " , cmd
);
1418 void show_info ( FILE * fp_ou
)
1422 if (! ShowSargInfo
) return ;
1423 zdate ( ftime
, sizeof ( ftime
), DateFormat
);
1424 fprintf ( fp_ou
, "<div align= \" center \" ><table><tr><td><br><br></td><td class= \" info \" >%s <a href='%s'><font class= \" info \" >%s-%s</font></a> %s %s</td></tr></table></div> \n " , text
[ 108 ], URL
, PGM
, VERSION
, text
[ 109 ], ftime
);
1427 void show_sarg ( FILE * fp_ou
, const char * ind
)
1429 if ( ShowSargLogo
) fprintf ( fp_ou
, "<div align= \" center \" ><table class= \" logo \" > \n <tr><th><a href= \" http://sarg.sourceforge.net \" ><img src= \" %s/images/sarg.png \" title= \" SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki \" alt= \" Sarg \" ></a> Squid Analysis Report Generator</th></tr> \n <tr><th class= \" title \" > </th></tr> \n </table></div> \n " , ind
);
1432 void write_logo_image ( FILE * fp_ou
)
1434 if ( LogoImage
[ 0 ]!= '\0' )
1435 fprintf ( fp_ou
, "<div align= \" center \" ><table class= \" logo \" > \n <tr><th><img src= \" %s \" width= \" %s \" height= \" %s \" alt= \" Logo \" > %s</th></tr> \n <tr><td height= \" 5 \" ></td></tr> \n </table> \n </div> \n " , LogoImage
, Width
, Height
, LogoText
);
1438 void write_html_header ( FILE * fp_ou
, const char * ind
)
1440 fprintf ( fp_ou
, "<!DOCTYPE HTML PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN \" \" http://www.w3.org/TR/html4/loose.dtd \" > \n <html> \n <head> \n <meta http-equiv= \" Content-Type \" content= \" text/html; charset=%s \" > \n " , CharSet
);
1442 fprintf ( fp_ou
, "</head> \n <body style= \" font-family:%s;font-size:%s;background-color:%s;background-image:url(%s) \" > \n " , FontFace
, TitleFontSize
, BgColor
, BgImage
);
1443 write_logo_image ( fp_ou
);
1444 show_sarg ( fp_ou
, ind
);
1445 fprintf ( fp_ou
, "<div align= \" center \" ><table cellpadding= \" 0 \" cellspacing= \" 0 \" > \n <tr><th class= \" title \" >%s</th></tr> \n </table></div> \n <div align= \" center \" ><table cellpadding= \" 1 \" cellspacing= \" 2 \" > \n <tr><td></td><td></td></tr> \n " , Title
);
1448 void output_html_string ( FILE * fp_ou
, const char * str
)
1454 fputs ( "&" , fp_ou
);
1457 fputs ( "<" , fp_ou
);
1460 fputs ( ">" , fp_ou
);
1463 fputs ( """ , fp_ou
);
1466 fputs ( "'" , fp_ou
);
1479 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1480 printf ( "SARG: MALICIUS CODE DETECTED. \n " );
1481 printf ( "SARG: I think someone is trying to execute arbitrary code in your system using sarg. \n " );
1482 printf ( "SARG: please review your access.log and/or your useragent.log file. \n " );
1483 printf ( "SARG: process stoped. No actions taken. \n " );
1484 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1486 if ( snprintf ( dir
, sizeof ( dir
), "%s/sarg" , tmp
)>= sizeof ( dir
)) {
1487 fprintf ( stderr
, "SARG: temporary directory too long: %s/sarg \n " , tmp
);
1491 unlinkdir ( dirname
, 0 );
1497 void url_module ( const char * url
, char * w2
)
1503 for ( x
= strlen ( url
)- 1 ; x
>= 0 ; x
--) {
1504 if ( url
[ x
] == '/' || y
>= sizeof ( w
)- 1 ) break ;
1513 for ( y
= y
- 1 ; y
>= 0 ; y
--) {
1520 void write_html_trailer ( FILE * fp_ou
)
1524 fputs ( "</table></div> \n " , fp_ou
);
1525 zdate ( ftime
, sizeof ( ftime
), DateFormat
);
1527 fputs ( "</body> \n </html> \n " , fp_ou
);
1532 printf ( "SARG Version: %s \n " , VERSION
);
1536 char * get_param_value ( const char * param
, char * line
)
1540 while (* line
== ' ' || * line
== ' \t ' ) line
++;
1542 if ( strncasecmp ( line
, param
, plen
)) return ( NULL
);
1543 if ( line
[ plen
]!= ' ' && line
[ plen
]!= ' \t ' ) return ( NULL
);
1545 while (* line
== ' ' || * line
== ' \t ' ) line
++;
1549 void unlinkdir ( const char * dir
, int contentonly
)
1553 struct dirent
* direntp
;
1559 while (( direntp
= readdir ( dirp
)) != NULL
) {
1560 if ( direntp
-> d_name
[ 0 ] == '.' && ( direntp
-> d_name
[ 1 ] == '\0' ||
1561 ( direntp
-> d_name
[ 1 ] == '.' && direntp
-> d_name
[ 2 ] == '\0' )))
1563 if ( snprintf ( dname
, sizeof ( dname
), "%s/%s" , dir
, direntp
-> d_name
)>= sizeof ( dname
)) {
1564 fprintf ( stderr
, "SARG: directory name to delete too long: %s/%s \n " , dir
, direntp
-> d_name
);
1568 err
= lstat ( dname
,& st
);
1570 err
= stat ( dname
,& st
);
1573 fprintf ( stderr
, "SARG: cannot stat %s \n " , dname
);
1576 if ( S_ISREG ( st
. st_mode
)) {
1577 if ( unlink ( dname
)) {
1578 fprintf ( stderr
, "SARG: cannot delete %s - %s \n " , dname
, strerror ( errno
));
1581 } else if ( S_ISDIR ( st
. st_mode
)) {
1584 fprintf ( stderr
, "SARG: unknown path type %s \n " , dname
);
1591 fprintf ( stderr
, "SARG: cannot delete %s - %s \n " , dir
, strerror ( errno
));