]>
git.ipfire.org Git - thirdparty/sarg.git/blob - util.c
998e8dfa21764287f0b06ec3ce69d347808b4547
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
27 // #define LEGACY_MY_ATOLL
28 // #define LEGACY_TESTVALIDUSERCHAR
30 #include "include/conf.h"
31 #include "include/defs.h"
33 #if defined(HAVE_BACKTRACE)
34 #define USE_GETWORD_BACKTRACE 1
36 #define USE_GETWORD_BACKTRACE 0
39 static char mtab1
[ 12 ][ 4 ]={ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" };
41 //! The list of the HTTP codes to exclude from the report.
42 static char * excludecode
= NULL
;
44 /*void fgetword(char *word, char *line, int stop)
49 for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x];
56 tchar = strchr(line, stop);
57 if (tchar == NULL) strcpy(word, line);
60 difflen = tchar - line;
61 strncpy(word, line, difflen);
66 #if USE_GETWORD_BACKTRACE
67 static void getword_backtrace ( void )
73 n
= backtrace ( buffer
, sizeof ( buffer
)/ sizeof ( buffer
[ 0 ]));
75 calls
= backtrace_symbols ( buffer
, n
);
77 fprintf ( stderr
, "SARG: getword backtrace: \n " );
78 for ( i
= 0 ; i
< n
; i
++) {
79 fprintf ( stderr
, "SARG: %d:%s \n " , i
+ 1 , calls
[ i
]);
84 #endif //USE_GETWORD_BACKTRACE
86 void getword_start ( struct getwordstruct
* gwarea
, const char * line
)
88 gwarea
-> beginning
= line
;
92 void getword_restart ( struct getwordstruct
* gwarea
)
94 gwarea
-> current
= gwarea
-> beginning
;
97 int getword ( char * word
, int limit
, struct getwordstruct
* gwarea
, int stop
)
101 for ( x
= 0 ;(( gwarea
-> current
[ x
]) && ( gwarea
-> current
[ x
] != stop
)); x
++) {
103 printf ( "SARG: getword loop detected after %d bytes. \n " , x
);
104 printf ( "SARG: Line= \" %s \"\n " , gwarea
-> beginning
);
105 printf ( "SARG: Record= \" %s \"\n " , gwarea
-> current
);
106 printf ( "SARG: searching for \' x%x \'\n " , stop
);
107 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
108 word
[( limit
> 0 ) ? limit
- 1 : 0 ]= '\0' ;
109 #if USE_GETWORD_BACKTRACE
114 word
[ x
] = gwarea
-> current
[ x
];
118 if ( gwarea
-> current
[ x
]) ++ x
;
123 int getword_multisep ( char * word
, int limit
, struct getwordstruct
* gwarea
, int stop
)
127 for ( x
= 0 ;(( gwarea
-> current
[ x
]) && ( gwarea
-> current
[ x
] != stop
)); x
++) {
129 printf ( "SARG: getword_multisep loop detected. \n " );
130 printf ( "SARG: Line= \" %s \"\n " , gwarea
-> beginning
);
131 printf ( "SARG: Record= \" %s \"\n " , gwarea
-> current
);
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");
134 if ( limit
> 0 ) word
[ limit
- 1 ]= '\0' ;
135 #if USE_GETWORD_BACKTRACE
141 word
[ x
] = gwarea
-> current
[ x
];
145 while ( gwarea
-> current
[ x
] && gwarea
-> current
[ x
]== stop
) ++ x
;
150 int getword_skip ( int limit
, struct getwordstruct
* gwarea
, int stop
)
154 for ( x
= 0 ;( gwarea
-> current
[ x
] && ( gwarea
-> current
[ x
] != stop
)); x
++) {
156 printf ( "SARG: getword_skip loop detected after %d bytes. \n " , x
);
157 printf ( "SARG: Line= \" %s \"\n " , gwarea
-> beginning
);
158 printf ( "SARG: Record= \" %s \"\n " , gwarea
-> current
);
159 printf ( "SARG: searching for \' x%x \'\n " , stop
);
160 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
161 #if USE_GETWORD_BACKTRACE
168 if ( gwarea
-> current
[ x
]) ++ x
;
173 int getword_atoll ( long long int * number
, struct getwordstruct
* gwarea
, int stop
)
178 if ( gwarea
-> current
[ 0 ] == '-' ) {
181 } else if ( gwarea
-> current
[ 0 ] == '+' ) {
185 for ( x
= 0 ; isdigit ( gwarea
-> current
[ x
]); x
++) {
186 * number
=(* number
* 10 ) + gwarea
-> current
[ x
]- '0' ;
188 if ( gwarea
-> current
[ x
] && gwarea
-> current
[ x
]!= stop
) {
189 printf ( "SARG: getword_atoll loop detected after %d bytes. \n " , x
);
190 printf ( "SARG: Line= \" %s \"\n " , gwarea
-> beginning
);
191 printf ( "SARG: Record= \" %s \"\n " , gwarea
-> current
);
192 printf ( "SARG: searching for \' x%x \'\n " , stop
);
193 //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n");
194 #if USE_GETWORD_BACKTRACE
201 if ( gwarea
-> current
[ x
]) ++ x
;
207 #define MAXLLL 30 //!< Maximum number of digits in long long (a guess).
208 long long int my_atoll ( const char * nptr
)
210 long long int returnval
= 0LL ;
211 int max_digits
= MAXLLL
;
213 // Soak up all the white space
214 while ( isspace ( * nptr
)) {
218 //For each character left to right
219 //change the character to a single digit
220 //multiply what we had before by 10 and add the new digit
222 while (-- max_digits
&& isdigit ( * nptr
))
224 returnval
= ( returnval
* 10 ) + ( * nptr
++ - '0' ) ;
230 int is_absolute ( const char * path
)
232 if (* path
== '/' ) return ( 1 );
234 if ( isalpha ( path
[ 0 ]) && path
[ 1 ]== ':' ) return ( 1 );
239 void my_mkdir ( const char * name
)
245 if (! is_absolute ( name
)) {
246 fprintf ( stderr
, "SARG: Invalid path (%s). Please, use absolute paths only. \n " , name
);
247 fprintf ( stderr
, "SARG: process aborted. \n " );
252 for ( i
= 0 ; name
[ i
] ; i
++) {
254 fprintf ( stderr
, "SARG: directory name too long %s \n " , name
);
257 if ( chars
> 0 && name
[ i
] == '/' ) {
259 if ( access ( w0
, R_OK
) != 0 ) {
261 fprintf ( stderr
, "SARG: mkdir %s %s \n " , w0
, strerror ( errno
));
262 fprintf ( stderr
, "SARG: process aborted. \n " );
267 if ( name
[ i
] != '/' ) chars
++;
271 if ( access ( name
, R_OK
) != 0 ) {
272 if ( mkdir ( name
, 0755 )) {
273 fprintf ( stderr
, "SARG: mkdir %s %s \n " , name
, strerror ( errno
));
274 fprintf ( stderr
, "SARG: process aborted. \n " );
281 void my_lltoa ( unsigned long long int n
, char s
[], int len
)
289 s
[ slen
++] = ( n
% 10 ) + '0' ;
290 } while (( n
/= 10 ) > 0 );
293 for ( i
= 0 , j
= slen
- 1 ; i
< j
; i
++, j
--) {
301 for ( j
= slen
; j
>= 0 ; j
--)
309 void builddia ( char * dia
, const char * mes
, const char * ano
, const char * df
, char * wdata
)
314 if ( strlen ( dia
) < 1 ) return ;
316 for ( nmes
= 0 ; nmes
< 12 ; nmes
++) {
317 if ( strcmp ( mtab1
[ nmes
], mes
) == 0 ) {
323 snprintf ( wdata
, 9 , "%s%02d%s" , ano
, nmes
, dia
);
326 snprintf ( ndia
, sizeof ( ndia
), "%s/%02d/%s" , dia
, nmes
, ano
);
328 snprintf ( ndia
, sizeof ( ndia
), "%02d/%s/%s" , nmes
, dia
, ano
);
335 void buildymd ( const char * dia
, const char * mes
, const char * ano
, char * wdata
)
339 for ( nmes
= 0 ; nmes
< 12 ; nmes
++) {
340 if ( strcmp ( mtab1
[ nmes
], mes
) == 0 )
344 sprintf ( wdata
, "%04d%02d%02d" , atoi ( ano
), nmes
+ 1 , atoi ( dia
));
349 void conv_month ( char * month
)
353 for ( x
= 0 ; x
< 12 && strcmp ( mtab1
[ x
], month
)!= 0 ; x
++);
354 sprintf ( month
, "%02d" , x
+ 1 );
358 void conv_month_name ( char * month
)
364 strcpy ( month
, mtab1
[ x
- 1 ]);
368 void name_month ( char * month
, int month_len
)
370 int x
, z
= atoi ( month
)- 1 ;
373 struct getwordstruct gwarea
;
376 getword_start (& gwarea
, m
);
379 if ( getword_multisep ( w
, sizeof ( w
),& gwarea
, ',' )< 0 ) {
380 printf ( "SARG: Maybe you have a broken record or garbage in the names of the months. \n " );
383 if ( getword_multisep ( month
, month_len
,& gwarea
, ',' )< 0 ) {
384 printf ( "SARG: Maybe you have a broken record or garbage in the name of the months. \n " );
390 void fixper ( char * tbuf
, char * period
, const char * duntil
)
393 char dia
[ 5 ], mes
[ 5 ], ano
[ 5 ];
396 strncpy ( dia
, duntil
+ 6 , 2 );
398 strncpy ( mes
, duntil
+ 4 , 2 );
400 strncpy ( ano
, duntil
, 4 );
405 strcpy ( mes
, mtab1
[ x
- 1 ]);
407 if ( strcmp ( df
, "e" ) == 0 )
408 sprintf ( warea
, "%s%s%s" , dia
, mes
, ano
);
409 else if ( strcmp ( df
, "u" ) == 0 )
410 sprintf ( warea
, "%s%s%s" , ano
, mes
, dia
);
414 strcat ( period
, warea
);
418 void debuga ( const char * msg
,...)
422 fputs ( "SARG: " , stderr
);
424 vfprintf ( stderr
, msg
, ap
);
430 void debugaz ( const char * head
, const char * msg
)
432 fprintf ( stderr
, "SARG: (util) %s=%s \n " , head
, msg
);
444 for ( i
= 0 ; ip
[ i
]; i
++) {
466 char * fixnum ( long long int value
, int n
)
468 #define MAXLEN_FIXNUM 1024
469 char num
[ MAXLEN_FIXNUM
]= "" ;
470 char buf
[ MAXLEN_FIXNUM
* 2 ];
472 static char ret
[ MAXLEN_FIXNUM
* 2 ];
474 register int i
, j
, k
;
476 static char abbrev
[ 30 ];
478 my_lltoa ( value
, num
, 0 );
480 if ( strcmp ( DisplayedValues
, "abbreviation" ) == 0 ) {
481 numlen
= strlen ( num
);
483 sprintf ( abbrev
, "%s" , num
);
484 if ( numlen
== 4 || numlen
== 7 || numlen
== 10 || numlen
== 13 ) {
485 snprintf ( abbrev
, 2 , "%s" , num
);
486 strncat ( abbrev
, "." , 1 );
487 strncat ( abbrev
, num
+ 1 , 2 );
488 if (! n
) return ( abbrev
);
490 strncat ( abbrev
, "K" , 1 );
492 strncat ( abbrev
, "M" , 1 );
493 else if ( numlen
== 10 )
494 strncat ( abbrev
, "G" , 1 );
495 else if ( numlen
== 13 )
496 strncat ( abbrev
, "T" , 1 );
498 if ( numlen
== 5 || numlen
== 8 || numlen
== 11 || numlen
== 14 ) {
499 snprintf ( abbrev
, 3 , "%s" , num
);
500 strncat ( abbrev
, "." , 1 );
501 strncat ( abbrev
, num
+ 2 , 2 );
502 if (! n
) return ( abbrev
);
504 strncat ( abbrev
, "K" , 1 );
506 strncat ( abbrev
, "M" , 1 );
507 else if ( numlen
== 11 )
508 strncat ( abbrev
, "G" , 1 );
509 else if ( numlen
== 14 )
510 strncat ( abbrev
, "T" , 1 );
512 if ( numlen
== 6 || numlen
== 9 || numlen
== 12 || numlen
== 15 ) {
513 snprintf ( abbrev
, 4 , "%s" , num
);
514 strncat ( abbrev
, "." , 1 );
515 strncat ( abbrev
, num
+ 3 , 2 );
516 if (! n
) return ( abbrev
);
518 strncat ( abbrev
, "K" , 1 );
520 strncat ( abbrev
, "M" , 1 );
521 else if ( numlen
== 12 )
522 strncat ( abbrev
, "G" , 1 );
523 else if ( numlen
== 15 )
524 strncat ( abbrev
, "T" , 1 );
530 bzero ( buf
, MAXLEN_FIXNUM
* 2 );
536 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
537 if ( k
== 2 && i
!= 0 ) {
540 pbuf
[ j
++] = ( UseComma
) ? ',' : '.' ;
550 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
559 char * fixnum2 ( long long int value
, int n
)
561 #define MAXLEN_FIXNUM2 1024
562 char num
[ MAXLEN_FIXNUM2
];
563 char buf
[ MAXLEN_FIXNUM2
* 2 ];
565 static char ret
[ MAXLEN_FIXNUM2
* 2 ];
567 register int i
, j
, k
;
569 my_lltoa ( value
, num
, 0 );
570 bzero ( buf
, MAXLEN_FIXNUM2
* 2 );
576 for ( i
= strlen ( num
) - 1 , j
= 0 ; i
> - 1 ; i
--) {
577 if ( k
== 2 && i
!= 0 ) {
580 pbuf
[ j
++] = ( UseComma
) ? ',' : '.' ;
590 for ( i
= strlen ( pbuf
) - 1 , j
= 0 ; i
> - 1 ; i
--, j
++)
600 void buildhref ( char * href
)
604 if ( strcmp ( href
, "./" ) == 0 ){
606 strcat ( href
, "<a href='" );
610 href
[ strlen ( href
)- 1 ]= '\0' ;
611 sprintf ( whref
, "%s" , strrchr ( href
, '/' ));
613 strcpy ( href
, "<a href='" );
622 char * buildtime ( long long int elap
)
625 int num
= elap
/ 1000 ;
634 min
=( num
% 3600 ) / 60 ;
636 sprintf ( buf
, "%02d:%02d:%02d" , hor
, min
, sec
);
643 void obtdate ( const char * dirname
, const char * name
, char * data
)
649 sprintf ( wdir
, "%s%s/sarg-date" , dirname
, name
);
650 if (( fp_in
= fopen ( wdir
, "rt" )) == 0 ) {
651 sprintf ( wdir
, "%s%s/date" , dirname
, name
);
652 if (( fp_in
= fopen ( wdir
, "rt" )) == 0 ) {
658 if (! fgets ( data
, 80 , fp_in
)) {
659 fprintf ( stderr
, "Failed to read the date in %s \n " , wdir
);
670 void formatdate ( char * date
, int date_size
, int year
, int month
, int day
, int hour
, int minute
, int second
, int dst
)
676 memset (& ltm
, 0 , sizeof ( ltm
));
677 if ( year
>= 1900 ) ltm
. tm_year
= year
- 1900 ;
678 if ( month
>= 1 && month
<= 12 ) ltm
. tm_mon
= month
- 1 ;
679 if ( day
>= 1 && day
<= 31 ) ltm
. tm_mday
= day
;
680 if ( hour
>= 0 && hour
< 24 ) ltm
. tm_hour
= hour
;
681 if ( minute
>= 0 && minute
< 60 ) ltm
. tm_min
= minute
;
682 if ( second
>= 0 && second
< 60 ) ltm
. tm_sec
= second
;
684 unixtime
= mktime (& ltm
); //fill the missing entries
685 fulltm
= localtime (& unixtime
);
686 //strftime(date,date_size,"%a %b %d %H:%M:%S %Z %Y",fulltm);
687 strftime ( date
, date_size
, "%c" , fulltm
);
691 void obtuser ( const char * dirname
, const char * name
, char * tuser
)
697 sprintf ( wdir
, "%s%s/sarg-users" , dirname
, name
);
698 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
699 sprintf ( wdir
, "%s%s/users" , dirname
, name
);
700 if (( fp_in
= fopen ( wdir
, "r" ))== NULL
) {
706 if (! fgets ( tuser
, 20 , fp_in
)) {
707 fprintf ( stderr
, "Failed to read the user in %s \n " , wdir
);
718 void obttotal ( const char * dirname
, const char * name
, char * tbytes
, const char * tuser
, char * media
)
726 long long int wtuser
= 0 ;
727 long long int twork
= 0 ;
728 struct getwordstruct gwarea
;
734 sprintf ( wdir
, "%s%s/sarg-general" , dirname
, name
);
735 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
736 sprintf ( wdir
, "%s%s/general" , dirname
, name
);
737 if (( fp_in
= fopen ( wdir
, "r" )) == 0 ) {
742 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
743 if ( strncmp ( buf
, "TOTAL \t " , 6 ) == 0 )
745 else if ( strncmp ( buf
, "TOTAL " , 6 ) == 0 )
749 getword_start (& gwarea
, buf
);
750 if ( getword ( warea
, sizeof ( warea
),& gwarea
, sep
)< 0 ) {
751 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
754 if ( strcmp ( warea
, "TOTAL" ) != 0 )
756 if ( getword_skip ( MAXLEN
,& gwarea
, sep
)< 0 ) {
757 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
760 if ( getword_atoll (& twork
,& gwarea
, sep
)< 0 ) {
761 printf ( "SARG: Maybe you have a broken record or garbage in your %s file. \n " , wdir
);
764 strcpy ( tbytes
, fixnum ( twork
, 1 ));
769 wtuser
= my_atoll ( tuser
);
776 strcpy ( media
, fixnum ( med
, 1 ));
782 void gperiod ( const char * dirname
, const char * period
)
786 char wdirname
[ MAXLEN
];
788 strcpy ( wdirname
, dirname
);
789 strcat ( wdirname
, "/sarg-period" );
791 if (( fp_ou
= fopen ( wdirname
, "w" ))== NULL
){
792 fprintf ( stderr
, "SARG: (report) %s: %s \n " , text
[ 45 ], wdirname
);
800 debuga ( "%s" ,( char *) text
[ 50 ]);
806 static void copy_images ( void )
808 FILE * img_in
, * img_ou
;
811 char srcfile
[ MAXLEN
];
812 char dstfile
[ MAXLEN
];
814 struct dirent
* direntp
;
819 if ( snprintf ( images
, sizeof ( images
), "%simages" , outdir
)>= sizeof ( images
)) {
820 fprintf ( stderr
, "SARG: Cannot copy images to target directory %simages \n " , outdir
);
823 if ( access ( images
, R_OK
)!= 0 ) {
827 strcpy ( imgdir
, IMAGEDIR
);
828 dirp
= opendir ( imgdir
);
830 fprintf ( stderr
, "SARG: (util) %s %s: %s \n " , "Can't open directory" , imgdir
, strerror ( errno
));
833 while (( direntp
= readdir ( dirp
)) != NULL
){
834 if ( direntp
-> d_name
[ 0 ]== '.' )
836 sprintf ( srcfile
, "%s/%s" , imgdir
, direntp
-> d_name
);
837 if ( stat ( srcfile
,& info
)) {
838 fprintf ( stderr
, "SARG: Cannot stat \" %s \" - %s \n " , srcfile
, strerror ( errno
));
841 if ( S_ISREG ( info
. st_mode
)) {
842 sprintf ( dstfile
, "%s/%s" , images
, direntp
-> d_name
);
843 img_in
= fopen ( srcfile
, "rb" );
845 img_ou
= fopen ( dstfile
, "wb" );
847 while (( nread
= fread ( buffer
, 1 , sizeof ( buffer
), img_in
))> 0 ) {
848 if ( fwrite ( buffer
, 1 , nread
, img_ou
)!= nread
) {
849 fprintf ( stderr
, "SARG: Failed to copy image %s to %s \n " , srcfile
, dstfile
);
855 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open/create file" , dstfile
, strerror ( errno
));
858 fprintf ( stderr
, "SARG: (util): %s %s: %s \n " , text
[ 45 ]? text
[ 45 ]: "Can't open file" , srcfile
, strerror ( errno
));
861 ( void ) closedir ( dirp
);
866 void vrfydir ( const char * per1
, const char * addr
, const char * site
, const char * us
, const char * form
)
872 char dirname2
[ MAXLEN
];
879 if ( IndexTree
== INDEX_TREE_DATE
) {
880 bzero ( y1
, sizeof ( y1
));
881 bzero ( y2
, sizeof ( y2
));
882 bzero ( d1
, sizeof ( d1
));
883 bzero ( d2
, sizeof ( d2
));
884 bzero ( m1
, sizeof ( m1
));
885 bzero ( m2
, sizeof ( m2
));
886 if ( strncmp ( df
, "u" , 1 ) == 0 ) {
887 strncpy ( y1
, period
, 4 );
888 strncpy ( y2
, period
+ 10 , 4 );
889 strncpy ( m1
, period
+ 4 , 3 );
890 strncpy ( m2
, period
+ 14 , 3 );
891 strncpy ( d1
, period
+ 7 , 2 );
892 strncpy ( d2
, period
+ 17 , 2 );
893 } else if ( strncmp ( df
, "e" , 1 ) == 0 ) {
894 strncpy ( d1
, period
+ 0 , 2 );
895 strncpy ( d2
, period
+ 10 , 2 );
896 strncpy ( m1
, period
+ 2 , 3 );
897 strncpy ( m2
, period
+ 12 , 3 );
898 strncpy ( y1
, period
+ 5 , 4 );
899 strncpy ( y2
, period
+ 15 , 4 );
904 sprintf ( wdir
, "%s%s" , outdir
, y1
);
905 if ( strcmp ( y1
, y2
) != 0 ) {
909 if ( access ( wdir
, R_OK
) != 0 )
914 if ( strcmp ( m1
, m2
) != 0 ) {
918 if ( access ( wdir
, R_OK
) != 0 )
923 if ( strcmp ( d1
, d2
) != 0 ) {
928 sprintf ( wdir
, "%s%s" , outdir
, per1
);
935 if ( addr
[ 0 ] != '\0' ) {
939 if ( site
[ 0 ] != '\0' ) {
944 strcpy ( dirname
, wdir
);
946 if ( IndexTree
!= INDEX_TREE_DATE
) {
947 if (! OverwriteReport
) {
949 if ( access ( wdir
, R_OK
) == 0 ) {
950 sprintf ( wdir
, "%s.%d" , dirname
, num
);
951 sprintf ( per2
, "%s.%d" , per1
, num
);
960 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname
, text
[ 52 ], wdir
);
961 rename ( dirname
, wdir
);
964 if ( access ( dirname
, R_OK
) == 0 ) {
965 unlinkdir ( dirname
, 1 );
970 strcpy ( dirname2
, wdir
);
971 if (! OverwriteReport
) {
973 if ( access ( wdir
, R_OK
) == 0 ) {
974 sprintf ( wdir
, "%s.%d" , dirname2
, num
);
975 sprintf ( per2
, "%s.%d" , per1
, num
);
984 fprintf ( stderr
, "SARG: %s: %s %s %s \n " , text
[ 51 ], dirname2
, text
[ 52 ], wdir
);
985 rename ( dirname2
, wdir
);
986 strcpy ( dirname2
, wdir
);
989 if ( access ( wdir
, R_OK
) == 0 ) {
994 if ( access ( wdir
, R_OK
) != 0 )
998 strcpy ( dirname2
, wdir
);
1000 sprintf ( wdir
, "%s/sarg-date" , dirname
);
1001 if (( fp_ou
= fopen ( wdir
, "wt" )) == 0 ) {
1002 fprintf ( stderr
, "SARG: cannot open %s for writing \n " , wdir
);
1007 //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime));
1008 loctm
= localtime (& curtime
);
1009 strftime ( wdir
, sizeof ( wdir
), "%Y-%m-%d %H:%M:%S" , loctm
);
1010 fprintf ( fp_ou
, "%s %d \n " , wdir
, loctm
-> tm_isdst
);
1016 void strip_latin ( char * line
)
1023 for ( i
= 0 ; line
[ i
]; i
++){
1025 if ( line
[ i
]== ';' ) skip
= 0 ;
1038 void zdate ( char * ftime
, int ftimesize
, const char * DateFormat
)
1045 local
= localtime (& t
);
1046 if ( strcmp ( DateFormat
, "u" ) == 0 )
1047 strftime ( ftime
, ftimesize
, "%b/%d/%Y %H:%M" , local
);
1048 if ( strcmp ( DateFormat
, "e" ) == 0 )
1049 strftime ( ftime
, ftimesize
, "%d/%b/%Y-%H:%M" , local
);
1050 if ( strcmp ( DateFormat
, "w" ) == 0 )
1051 strftime ( ftime
, ftimesize
, "%V-%H-%M" , local
);
1056 char * fixtime ( long int elap
)
1059 int num
= elap
/ 1000 ;
1063 static char buf
[ 12 ];
1065 if ( strcmp ( datetimeby
, "bytes" ) == 0 ) {
1066 strcpy ( buf
, fixnum ( elap
, 1 ));
1071 sprintf ( buf
, "00:00:%02ld" , elap
);
1076 min
=( num
% 3600 ) / 60 ;
1079 if ( hor
== 0 && min
== 0 && sec
== 0 )
1082 sprintf ( buf
, "%01d:%02d:%02d" , hor
, min
, sec
);
1089 void date_from ( char * date
, char * dfrom
, char * duntil
)
1100 strncpy ( wdate
, date
, sizeof ( wdate
)- 1 );
1101 wdate
[ sizeof ( wdate
)- 1 ]= '\0' ;
1102 if ( strchr ( wdate
, '-' ) == NULL
) {
1103 if ( strlen ( wdate
)* 2 + 1 >= sizeof ( wdate
)) {
1104 fprintf ( stderr
, "SARG: Invalid date range passed as argument. \n " );
1112 if ( sscanf ( wdate
, "%d/%d/%d-%d/%d/%d" ,& diaf
,& mesf
,& anof
,& diau
,& mesu
,& anou
)!= 6 ) {
1113 fprintf ( stderr
, "SARG: The date range passed as argument is not formated as dd/mm/yyyy-dd/mm/yyyy. \n " );
1117 sprintf ( dfrom
, "%04d%02d%02d" , anof
, mesf
, diaf
);
1118 sprintf ( duntil
, "%04d%02d%02d" , anou
, mesu
, diau
);
1123 char * strlow ( char * string
)
1129 for ( s
= string
; * s
; ++ s
)
1139 char * strup ( char * string
)
1145 for ( s
= string
; * s
; ++ s
)
1153 void subs ( char * str
, int size
, char * from
, char * to
)
1161 tmp
= strstr ( str
, from
);
1171 if ( ss
+ shift
>= size
){
1172 fprintf ( stderr
, "SARG: Cannot replace %s by %s in %s and fit within %d bytes \n " , from
, to
, str
, size
);
1175 for ( i
= strlen ( tmp
) ; i
>= sf
; i
--)
1176 tmp
[ i
+ shift
]= tmp
[ i
];
1177 } else if ( shift
< 0 ) {
1179 for ( i
= sf
; i
<= len
; i
++)
1180 tmp
[ i
+ shift
]= tmp
[ i
];
1182 memcpy ( tmp
, to
, st
);
1187 void removetmp ( const char * outdir
)
1193 if (! RemoveTempFiles
)
1197 debuga ( "%s: sarg-general, sarg-period" , text
[ 82 ]);
1199 if ( snprintf ( warea
, sizeof ( warea
), "%s/sarg-general" , outdir
)>= sizeof ( warea
)) {
1200 fprintf ( stderr
, "SARG: (removetmp) directory too long to remove: %s/sarg-period \n " , outdir
);
1203 if (( fp_in
= fopen ( warea
, "r" ))== NULL
){
1204 fprintf ( stderr
, "===SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1207 while ( fgets ( buf
, sizeof ( buf
), fp_in
)!= NULL
) {
1208 if ( strncmp ( buf
, "TOTAL" , 5 ) == 0 )
1212 if (( fp_in
= fopen ( warea
, "w" ))== NULL
){
1213 fprintf ( stderr
, "SARG: (removetmp) %s: %s \n " , text
[ 45 ], warea
);
1218 if ( snprintf ( warea
, sizeof ( warea
), "%s/sarg-period" , outdir
)>= sizeof ( warea
)) {
1219 fprintf ( stderr
, "SARG: (removetmp) directory too long to remove: %s/sarg-period \n " , outdir
);
1227 void load_excludecodes ( const char * ExcludeCodes
)
1234 if ( ExcludeCodes
[ 0 ] == '\0' )
1237 if (( excludecode
=( char *) malloc ( 1024 ))== NULL
) {
1238 fprintf ( stderr
, "SARG: %s (1024): \n " , text
[ 59 ]);
1241 bzero ( excludecode
, 1024 );
1243 if (( fp_in
= fopen ( ExcludeCodes
, "r" ))== NULL
) {
1244 fprintf ( stderr
, "SARG: (util) Cannot open file: %s (exclude_codes) \n " , ExcludeCodes
);
1248 while ( fgets ( data
, sizeof ( data
), fp_in
)!= NULL
) {
1249 for ( i
= strlen ( data
)- 1 ; i
>= 0 && ( unsigned char ) data
[ i
]<= ' ' ; i
--) data
[ i
]= 0 ;
1251 strcat ( excludecode
, data
);
1252 strcat ( excludecode
, ";" );
1260 void free_excludecodes ( void )
1268 int vercode ( const char * code
)
1273 if ( excludecode
&& excludecode
[ 0 ]!= 0 ) {
1275 for ( cod
= excludecode
; cod
; cod
= strchr ( cod
+ 1 , ';' )) {
1276 if ( strncmp ( code
, cod
, clen
)== 0 && cod
[ clen
]== ';' )
1283 void fixnone ( char * str
)
1287 for ( i
= strlen ( str
)- 1 ; i
>= 0 && ( unsigned char ) str
[ i
]<= ' ' ; i
--);
1288 if ( i
== 3 && strncmp ( str
, "none" , 4 ) == 0 )
1294 void fixendofline ( char * str
)
1298 for ( i
= strlen ( str
)- 1 ; i
>= 0 && ( unsigned char ) str
[ i
]<= ' ' ; i
--) str
[ i
]= 0 ;
1301 #ifdef LEGACY_TESTVALIDUSERCHAR
1302 int testvaliduserchar ( const char * user
)
1308 for ( y
= 0 ; y
< strlen ( UserInvalidChar
); y
++) {
1309 for ( x
= 0 ; x
< strlen ( user
); x
++) {
1310 if ( user
[ x
] == UserInvalidChar
[ y
])
1317 int testvaliduserchar ( const char * user
)
1320 char * p_UserInvalidChar
= UserInvalidChar
;
1321 const char * p_user
;
1323 while ( * p_UserInvalidChar
) {
1326 if ( * p_UserInvalidChar
== * p_user
)
1330 p_UserInvalidChar
++ ;
1336 int compar ( const void * a
, const void * b
)
1337 { if ( *( int *) a
> *( int *) b
) return 1 ;
1338 if ( *( int *) a
< *( int *) b
) return - 1 ;
1342 int getnumlist ( char * buf
, numlist
* list
, const int len
, const int maxvalue
)
1344 int i
, j
, d
, flag
, r1
, r2
;
1345 char * pbuf
, ** bp
, * strbufs
[ 24 ];
1348 strtok ( buf
, " \t " );
1349 for ( * bp
= strtok ( NULL
, "," ), list
-> len
= 0 ; * bp
; * bp
= strtok ( NULL
, "," ) ) {
1350 if ( ++ bp
>= & strbufs
[ 24 ] )
1357 for ( i
= 0 ; i
< list
-> len
; i
++ ) {
1358 if ( strchr ( strbufs
[ i
], '-' ) != 0 ) {
1359 pbuf
= strbufs
[ i
];
1360 strtok ( pbuf
, "-" );
1361 pbuf
= strtok ( NULL
, "\0" );
1362 r1
= atoi ( strbufs
[ i
] );
1363 if ( ( r2
= atoi ( pbuf
) ) >= maxvalue
|| r1
>= r2
)
1365 if ( i
+ d
+ ( r2
- r1
) + 1 <= len
) {
1366 for ( j
= r1
; j
<= r2
; j
++ )
1367 list
-> list
[ i
+ d
++ ] = j
;
1372 if ( ( list
-> list
[ i
+ d
] = atoi ( strbufs
[ i
] ) ) >= maxvalue
)
1376 qsort ( list
-> list
, list
-> len
, sizeof ( int ), compar
);
1379 for ( i
= 0 ; i
< list
-> len
- 1 ; i
++ )
1380 if ( list
-> list
[ i
] == list
-> list
[ i
+ 1 ] ) {
1381 for ( j
= i
+ 1 ; j
< list
-> len
; j
++ )
1382 list
-> list
[ j
- 1 ] = list
-> list
[ j
];
1392 char * get_size ( const char * path
, const char * file
)
1395 static char response
[ 255 ];
1399 if ( snprintf ( cmd
, sizeof ( cmd
), "du -skh %s%s" , path
, file
)>= sizeof ( cmd
)) {
1400 printf ( "SARG: Cannot get disk space because the path %s%s is too long. \n " , path
, file
);
1403 fp
= popen ( cmd
, "r" );
1404 if (! fgets ( response
, sizeof ( response
), fp
)) {
1405 fprintf ( stderr
, "SARG: Cannot get disk size with command %s" , cmd
);
1408 ptr
= strchr ( response
, ' \t ' );
1410 fprintf ( stderr
, "SARG: The command %s failed. \n " , cmd
);
1419 void show_info ( FILE * fp_ou
)
1423 if (! ShowSargInfo
) return ;
1424 zdate ( ftime
, sizeof ( ftime
), DateFormat
);
1425 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
);
1428 void show_sarg ( FILE * fp_ou
, const char * ind
)
1430 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
);
1433 void write_logo_image ( FILE * fp_ou
)
1435 if ( LogoImage
[ 0 ]!= '\0' )
1436 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
);
1439 void write_html_header ( FILE * fp_ou
, const char * ind
)
1441 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
);
1443 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
);
1444 write_logo_image ( fp_ou
);
1445 show_sarg ( fp_ou
, ind
);
1446 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
);
1449 void output_html_string ( FILE * fp_ou
, const char * str
)
1455 fputs ( "&" , fp_ou
);
1458 fputs ( "<" , fp_ou
);
1461 fputs ( ">" , fp_ou
);
1464 fputs ( """ , fp_ou
);
1467 fputs ( "'" , fp_ou
);
1480 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1481 printf ( "SARG: MALICIUS CODE DETECTED. \n " );
1482 printf ( "SARG: I think someone is trying to execute arbitrary code in your system using sarg. \n " );
1483 printf ( "SARG: please review your access.log and/or your useragent.log file. \n " );
1484 printf ( "SARG: process stoped. No actions taken. \n " );
1485 printf ( "SARG: ------------------------------------------------------------------------------ \n " );
1487 if ( snprintf ( dir
, sizeof ( dir
), "%s/sarg" , tmp
)>= sizeof ( dir
)) {
1488 fprintf ( stderr
, "SARG: temporary directory too long: %s/sarg \n " , tmp
);
1492 unlinkdir ( dirname
, 0 );
1498 void url_module ( const char * url
, char * w2
)
1504 for ( x
= strlen ( url
)- 1 ; x
>= 0 ; x
--) {
1505 if ( url
[ x
] == '/' || y
>= sizeof ( w
)- 1 ) break ;
1514 for ( y
= y
- 1 ; y
>= 0 ; y
--) {
1521 void write_html_trailer ( FILE * fp_ou
)
1525 fputs ( "</table></div> \n " , fp_ou
);
1526 zdate ( ftime
, sizeof ( ftime
), DateFormat
);
1528 fputs ( "</body> \n </html> \n " , fp_ou
);
1533 printf ( "SARG Version: %s \n " , VERSION
);
1537 char * get_param_value ( const char * param
, char * line
)
1541 while (* line
== ' ' || * line
== ' \t ' ) line
++;
1543 if ( strncasecmp ( line
, param
, plen
)) return ( NULL
);
1544 if ( line
[ plen
]!= ' ' && line
[ plen
]!= ' \t ' ) return ( NULL
);
1546 while (* line
== ' ' || * line
== ' \t ' ) line
++;
1550 void unlinkdir ( const char * dir
, int contentonly
)
1554 struct dirent
* direntp
;
1560 while (( direntp
= readdir ( dirp
)) != NULL
) {
1561 if ( direntp
-> d_name
[ 0 ] == '.' && ( direntp
-> d_name
[ 1 ] == '\0' ||
1562 ( direntp
-> d_name
[ 1 ] == '.' && direntp
-> d_name
[ 2 ] == '\0' )))
1564 if ( snprintf ( dname
, sizeof ( dname
), "%s/%s" , dir
, direntp
-> d_name
)>= sizeof ( dname
)) {
1565 fprintf ( stderr
, "SARG: directory name to delete too long: %s/%s \n " , dir
, direntp
-> d_name
);
1569 err
= lstat ( dname
,& st
);
1571 err
= stat ( dname
,& st
);
1574 fprintf ( stderr
, "SARG: cannot stat %s \n " , dname
);
1577 if ( S_ISREG ( st
. st_mode
)) {
1578 if ( unlink ( dname
)) {
1579 fprintf ( stderr
, "SARG: cannot delete %s - %s \n " , dname
, strerror ( errno
));
1582 } else if ( S_ISDIR ( st
. st_mode
)) {
1585 fprintf ( stderr
, "SARG: unknown path type %s \n " , dname
);
1592 fprintf ( stderr
, "SARG: cannot delete %s - %s \n " , dir
, strerror ( errno
));