]>
git.ipfire.org Git - thirdparty/sarg.git/blob - util.c
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 #if USE_GETWORD_BACKTRACE
45 static void getword_backtrace(void)
51 n
=backtrace(buffer
,sizeof(buffer
)/sizeof(buffer
[0]));
53 calls
=backtrace_symbols(buffer
,n
);
55 debuga(_("getword backtrace:\n"));
56 for (i
=0 ; i
<n
; i
++) {
57 fprintf(stderr
,"SARG: %d:%s\n",i
+1,calls
[i
]);
62 #endif //USE_GETWORD_BACKTRACE
64 void getword_start(struct getwordstruct
*gwarea
, const char *line
)
66 gwarea
->beginning
=line
;
71 void getword_restart(struct getwordstruct
*gwarea
)
73 if (gwarea
->modified
) {
74 debuga(_("Cannot parse again the line as it was modified\n"));
77 gwarea
->current
=gwarea
->beginning
;
80 int getword(char *word
, int limit
, struct getwordstruct
*gwarea
, char stop
)
84 for(x
=0;((gwarea
->current
[x
]) && (gwarea
->current
[x
] != stop
));x
++) {
86 debuga(_("End of word not found in getword after %d bytes.\n"),x
);
87 debuga(_("Line=\"%s\"\n"),gwarea
->beginning
);
88 debuga(_("Record=\"%s\"\n"),gwarea
->current
);
89 debuga(_("searching for \'x%x\'\n"),stop
);
90 //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
91 word
[(limit
>0) ? limit
-1 : 0]='\0';
92 #if USE_GETWORD_BACKTRACE
97 word
[x
] = gwarea
->current
[x
];
101 if (gwarea
->current
[x
]) ++x
;
106 int getword_limit(char *word
, int limit
, struct getwordstruct
*gwarea
, char stop
)
111 for(x
=0; x
<limit
&& gwarea
->current
[x
] && gwarea
->current
[x
] != stop
;x
++) {
112 word
[x
] = gwarea
->current
[x
];
116 while (*gwarea
->current
&& *gwarea
->current
!= stop
) gwarea
->current
++;
117 if (*gwarea
->current
) ++gwarea
->current
;
121 int getword_multisep(char *word
, int limit
, struct getwordstruct
*gwarea
, char stop
)
125 for(x
=0;((gwarea
->current
[x
]) && (gwarea
->current
[x
] != stop
));x
++) {
127 debuga(_("End of word not found in getword_multisep after %d bytes.\n"),x
);
128 debuga(_("Line=\"%s\"\n"),gwarea
->beginning
);
129 debuga(_("Record=\"%s\"\n"),gwarea
->current
);
130 debuga(_("searching for \'x%x\'\n"),stop
);
131 //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
132 if (limit
>0) word
[limit
-1]='\0';
133 #if USE_GETWORD_BACKTRACE
136 //exit(EXIT_FAILURE);
139 word
[x
] = gwarea
->current
[x
];
143 while (gwarea
->current
[x
] && gwarea
->current
[x
]==stop
) ++x
;
148 int getword_skip(int limit
, struct getwordstruct
*gwarea
, char stop
)
152 for(x
=0;(gwarea
->current
[x
] && (gwarea
->current
[x
] != stop
));x
++) {
154 debuga(_("End of word not found in getword_skip after %d bytes.\n"),x
);
155 debuga(_("Line=\"%s\"\n"),gwarea
->beginning
);
156 debuga(_("Record=\"%s\"\n"),gwarea
->current
);
157 debuga(_("searching for \'x%x\'\n"),stop
);
158 //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
159 #if USE_GETWORD_BACKTRACE
166 if (gwarea
->current
[x
]) ++x
;
171 int getword_atoll(long long int *number
, struct getwordstruct
*gwarea
, char stop
)
177 if (gwarea
->current
[0] == '-') {
180 } else if (gwarea
->current
[0] == '+') {
184 for(x
=0;isdigit(gwarea
->current
[x
]);x
++) {
185 digit
=gwarea
->current
[x
]-'0';
186 if (*number
>= (LLONG_MAX
-digit
)/10) {
187 debuga(_("Integer overflow detected in getword_atoll in line %s\n"),gwarea
->beginning
);
190 *number
=(*number
* 10) + digit
;
192 if(gwarea
->current
[x
] && gwarea
->current
[x
]!=stop
) {
193 debuga(_("End of number not found in getword_atoll after %d bytes.\n"),x
);
194 debuga(_("Line=\"%s\"\n"),gwarea
->beginning
);
195 debuga(_("Record=\"%s\"\n"),gwarea
->current
);
196 debuga(_("searching for \'x%x\'\n"),stop
);
197 //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
198 #if USE_GETWORD_BACKTRACE
205 if (gwarea
->current
[x
]) ++x
;
210 int getword_atoi(int *number
, struct getwordstruct
*gwarea
, char stop
)
216 if (gwarea
->current
[0] == '-') {
219 } else if (gwarea
->current
[0] == '+') {
223 for(x
=0;isdigit(gwarea
->current
[x
]);x
++) {
224 digit
=gwarea
->current
[x
]-'0';
225 if (*number
> (INT_MAX
-digit
)/10) {
226 debuga(_("Integer overflow detected in getword_atoi in line %s\n"),gwarea
->beginning
);
229 *number
=(*number
* 10) + digit
;
231 if(gwarea
->current
[x
] && gwarea
->current
[x
]!=stop
) {
232 debuga(_("End of number not found in getword_atoi after %d bytes.\n"),x
);
233 debuga(_("Line=\"%s\"\n"),gwarea
->beginning
);
234 debuga(_("Record=\"%s\"\n"),gwarea
->current
);
235 debuga(_("searching for \'x%x\'\n"),stop
);
236 //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
237 #if USE_GETWORD_BACKTRACE
244 if (gwarea
->current
[x
]) ++x
;
249 int getword_atol(long int *number
, struct getwordstruct
*gwarea
, char stop
)
255 if (gwarea
->current
[0] == '-') {
258 } else if (gwarea
->current
[0] == '+') {
262 for(x
=0;isdigit(gwarea
->current
[x
]);x
++) {
263 digit
=gwarea
->current
[x
]-'0';
264 if (*number
> (LONG_MAX
-digit
)/10) {
265 debuga(_("Integer overflow detected in getword_atol in line %s\n"),gwarea
->beginning
);
268 *number
=(*number
* 10) + digit
;
270 if(gwarea
->current
[x
] && gwarea
->current
[x
]!=stop
) {
271 debuga(_("End of number not found in getword_atol after %ld bytes.\n"),x
);
272 debuga(_("Line=\"%s\"\n"),gwarea
->beginning
);
273 debuga(_("Record=\"%s\"\n"),gwarea
->current
);
274 debuga(_("searching for \'x%x\'\n"),stop
);
275 //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
276 #if USE_GETWORD_BACKTRACE
283 if (gwarea
->current
[x
]) ++x
;
288 int getword_atolu(unsigned long int *number
, struct getwordstruct
*gwarea
, char stop
)
293 if (gwarea
->current
[0] == '-') {
294 debuga(_("getword_atolu got a negative number.\n"));
295 debuga(_("Line=\"%s\"\n"),gwarea
->beginning
);
296 debuga(_("Record=\"%s\"\n"),gwarea
->current
);
299 if (gwarea
->current
[0] == '+') {
303 for(x
=0;isdigit(gwarea
->current
[x
]);x
++) {
304 digit
=gwarea
->current
[x
]-'0';
305 if (*number
> (ULONG_MAX
-digit
)/10) {
306 debuga(_("Integer overflow detected in getword_atolu in line %s\n"),gwarea
->beginning
);
309 *number
=(*number
* 10) + digit
;
311 if(gwarea
->current
[x
] && gwarea
->current
[x
]!=stop
) {
312 debuga(_("End of number not found in getword_atolu after %ld bytes.\n"),x
);
313 debuga(_("Line=\"%s\"\n"),gwarea
->beginning
);
314 debuga(_("Record=\"%s\"\n"),gwarea
->current
);
315 debuga(_("searching for \'x%x\'\n"),stop
);
316 //debuga(_("Maybe you have a broken record or garbage in your access.log file.\n"));
317 #if USE_GETWORD_BACKTRACE
323 if (gwarea
->current
[x
]) ++x
;
329 int getword_ptr(char *orig_line
,char **word
, struct getwordstruct
*gwarea
, char stop
)
332 \note Why pass the original buffer to the function ? Because we must modify it to
333 insert the terminating ASCII zero for the word we return and that's not compatible
334 with getword_restart(). Moreover, getword_start() sometime works on constant strings
335 so this function require the original buffer to detect any missuse.
341 if (orig_line
&& orig_line
!=gwarea
->beginning
) {
342 debuga(_("Invalid buffer passed to getword_ptr\n"));
346 start
=(gwarea
->current
-gwarea
->beginning
);
347 if (word
&& orig_line
) *word
=orig_line
+start
;
348 for(x
=0;((gwarea
->current
[x
]) && (gwarea
->current
[x
] != stop
));x
++);
349 sep
=(gwarea
->current
[x
]!='\0');
350 if (word
&& orig_line
) orig_line
[start
+x
] = '\0';
357 #define MAXLLL 30 //!< Maximum number of digits in long long (a guess).
358 long long int my_atoll (const char *nptr
)
360 long long int returnval
=0LL;
361 int max_digits
= MAXLLL
;
363 // Soak up all the white space
364 while (isspace( *nptr
)) {
368 //For each character left to right
369 //change the character to a single digit
370 //multiply what we had before by 10 and add the new digit
372 while (--max_digits
&& isdigit( *nptr
))
374 returnval
= ( returnval
* 10 ) + ( *nptr
++ - '0' ) ;
380 int is_absolute(const char *path
)
382 if (*path
=='/') return(1);
384 if (isalpha(path
[0]) && path
[1]==':') return(1);
389 void my_mkdir(const char *name
)
395 if(!is_absolute(name
)) {
396 debuga(_("Invalid path (%s). Please, use absolute paths only.\n"),name
);
401 for (i
=0 ; name
[i
] ; i
++) {
403 debuga(_("directory name too long: %s\n"),name
);
406 if (chars
>0 && name
[i
] == '/') {
408 if (access(w0
, R_OK
) != 0) {
409 if (mkdir(w0
,0755)) {
410 debuga(_("Cannot create directory %s - %s\n"),w0
,strerror(errno
));
415 if (name
[i
] != '/') chars
++;
419 if (access(name
, R_OK
) != 0) {
420 if (mkdir(name
,0755)) {
421 debuga(_("Cannot create directory %s - %s\n"),name
,strerror(errno
));
428 void my_lltoa(unsigned long long int n
, char *s
, int ssize
, int len
)
437 debuga(_("The requested number of digits passed to my_lltoa (%d) is bigger than the output buffer size (%d)\n"),len
,ssize
);
442 s
[slen
++] = (n
% 10) + '0';
443 } while ((n
/= 10) > 0 && slen
<ssize
);
446 for (i
= 0, j
= slen
-1; i
<j
; i
++, j
--) {
454 for(j
=slen
; j
>=0; j
--)
461 int month2num(const char *month
)
465 for(m
=0 ; m
<12 && strcmp(mtab1
[m
],month
) != 0; m
++);
469 int builddia(int day
, int month
, int year
)
471 return(year
*10000+month
*100+day
);
477 \param date1 The first date to compare.
478 \param date2 The second date to compare.
480 \retval -1 If date1<date2.
481 \retval 0 If date1==date2.
482 \retval 1 if date1>date2.
484 int compare_date(struct tm
*date1
,struct tm
*date2
)
486 if (date1
->tm_year
<date2
->tm_year
) return(-1);
487 if (date1
->tm_year
>date2
->tm_year
) return(1);
488 if (date1
->tm_mon
<date2
->tm_mon
) return(-1);
489 if (date1
->tm_mon
>date2
->tm_mon
) return(1);
490 if (date1
->tm_mday
<date2
->tm_mday
) return(-1);
491 if (date1
->tm_mday
>date2
->tm_mday
) return(1);
492 if (date1
->tm_hour
<date2
->tm_hour
) return(-1);
493 if (date1
->tm_hour
>date2
->tm_hour
) return(1);
494 if (date1
->tm_min
<date2
->tm_min
) return(-1);
495 if (date1
->tm_min
>date2
->tm_min
) return(1);
496 if (date1
->tm_sec
<date2
->tm_sec
) return(-1);
497 if (date1
->tm_sec
>date2
->tm_sec
) return(1);
501 void buildymd(const char *dia
, const char *mes
, const char *ano
, char *wdata
)
506 sprintf(wdata
,"%04d%02d%02d",atoi(ano
),nmes
+1,atoi(dia
));
510 int conv_month(const char *month
)
514 for(x
=0; x
<12 && strncmp(mtab1
[x
],month
,3)!=0; x
++);
519 const char *conv_month_name(int month
)
523 if (month
<1 || month
>12) {
524 snprintf(str
,sizeof(str
),"%03d",month
);
527 return(mtab1
[month
-1]);
531 void name_month(char *month
,int month_len
)
533 int x
, z
=atoi(month
)-1;
536 struct getwordstruct gwarea
;
538 strcpy(m
,_("January,February,March,April,May,June,July,August,September,October,November,December"));
539 getword_start(&gwarea
,m
);
542 if (getword_multisep(w
,sizeof(w
),&gwarea
,',')<0) {
543 debuga(_("The internal list of month names is invalid. Please report this bug to the translator.\n"));
546 if (getword_multisep(month
,month_len
,&gwarea
,',')<0) {
547 debuga(_("The internal list of month names is invalid. Please report this bug to the translator.\n"));
554 Write a debug message to stderr. The message is prefixed by "SARG:" to identify its origin.
556 \param msg The printf like message to format.
557 \param ... The arguments to format in the message.
559 void debuga(const char *msg
,...)
563 fputs(_("SARG: "),stderr
);
565 vfprintf(stderr
,msg
,ap
);
571 Write a debug message to stderr. The message is prefixed by "SARG: (info)".
573 \param msg The printf like message to format.
574 \param ... The arguments to format in the message.
576 void debugaz(const char *msg
,...)
580 fputs(_("SARG: (info) "),stderr
);
582 vfprintf(stderr
,msg
,ap
);
587 char *fixnum(long long int value
, int n
)
589 #define MAXLEN_FIXNUM 256
590 char num
[MAXLEN_FIXNUM
]="";
591 char buf
[MAXLEN_FIXNUM
* 2];
593 static char ret
[MAXLEN_FIXNUM
* 2];
595 register int i
, j
, k
;
597 static char abbrev
[30]="";
599 my_lltoa(value
, num
, sizeof(num
), 0);
601 if(DisplayedValues
==DISPLAY_ABBREV
) {
602 numlen
= strlen(num
);
605 else if (numlen
%3 == 1) {
607 abbrev
[1]=(UseComma
) ? ',' : '.';
612 else if (numlen
%3 == 2) {
615 abbrev
[2]=(UseComma
) ? ',' : '.';
620 else if (numlen
%3 == 0) {
624 abbrev
[3]=(UseComma
) ? ',' : '.';
633 else if (numlen
<= 6)
635 else if (numlen
<= 9)
637 else if (numlen
<= 12)
639 else if (numlen
<= 15)
641 else if (numlen
>= 18)
643 else if (numlen
<= 21)
645 else if (numlen
<= 24)
647 else if (numlen
<= 27)
650 strcat(abbrev
,"???");
655 bzero(buf
, MAXLEN_FIXNUM
*2);
661 for ( i
= strlen(num
) - 1, j
= 0 ; i
> -1; i
--) {
662 if ( k
== 2 && i
!= 0 ) {
665 pbuf
[j
++] = (UseComma
) ? ',' : '.';
675 for ( i
= strlen(pbuf
) - 1, j
= 0 ; i
> -1; i
--, j
++)
684 char *fixnum2(long long int value
, int n
)
686 #define MAXLEN_FIXNUM2 1024
687 char num
[MAXLEN_FIXNUM2
];
688 char buf
[MAXLEN_FIXNUM2
* 2];
690 static char ret
[MAXLEN_FIXNUM2
* 2];
692 register int i
, j
, k
;
694 my_lltoa(value
, num
, sizeof(num
), 0);
695 bzero(buf
, MAXLEN_FIXNUM2
*2);
701 for ( i
= strlen(num
) - 1, j
= 0 ; i
> -1; i
--) {
702 if ( k
== 2 && i
!= 0 ) {
705 pbuf
[j
++] = (UseComma
) ? ',' : '.';
715 for ( i
= strlen(pbuf
) - 1, j
= 0 ; i
> -1; i
--, j
++)
724 char *buildtime(long long int elap
)
726 int num
= elap
/ 1000;
735 min
=(num
% 3600) / 60;
737 sprintf(buf
,"%02d:%02d:%02d",hor
,min
,sec
);
744 Get the date stored in the <tt>sarg-date</tt> file of a directory with the connection data.
746 \param dirname The directory to look for the connection directory.
747 \param name The name of the directory whose <tt>sarg-date</tt> file must be read.
748 \param data The buffer to store the content of the file. It must be more than 80
752 \retval -1 File not found.
754 int obtdate(const char *dirname
, const char *name
, char *data
)
759 sprintf(wdir
,"%s%s/sarg-date",dirname
,name
);
760 if ((fp_in
= fopen(wdir
, "rt")) == 0) {
761 sprintf(wdir
,"%s%s/date",dirname
,name
);
762 if ((fp_in
= fopen(wdir
, "rt")) == 0) {
768 if (!fgets(data
,80,fp_in
)) {
769 debuga(_("Failed to read the date in %s\n"),wdir
);
779 void formatdate(char *date
,int date_size
,int year
,int month
,int day
,int hour
,int minute
,int second
,int dst
)
785 memset(<m
,0,sizeof(ltm
));
786 if (year
>=1900) ltm
.tm_year
=year
-1900;
787 if (month
>=1 && month
<=12) ltm
.tm_mon
=month
-1;
788 if (day
>=1 && day
<=31) ltm
.tm_mday
=day
;
789 if (hour
>=0 && hour
<24) ltm
.tm_hour
=hour
;
790 if (minute
>=0 && minute
<60) ltm
.tm_min
=minute
;
791 if (second
>=0 && second
<60) ltm
.tm_sec
=second
;
793 unixtime
=mktime(<m
); //fill the missing entries
794 fulltm
=localtime(&unixtime
);
795 //strftime(date,date_size,"%a %b %d %H:%M:%S %Z %Y",fulltm);
796 strftime(date
,date_size
,"%c",fulltm
);
800 void computedate(int year
,int month
,int day
,struct tm
*t
)
802 memset(t
,0,sizeof(*t
));
803 t
->tm_year
=year
-1900;
809 int obtuser(const char *dirname
, const char *name
)
816 sprintf(wdir
,"%s%s/sarg-users",dirname
,name
);
817 if((fp_in
=fopen(wdir
,"r"))==NULL
) {
818 sprintf(wdir
,"%s%s/users",dirname
,name
);
819 if((fp_in
=fopen(wdir
,"r"))==NULL
) {
824 if (!fgets(tuser
,sizeof(tuser
),fp_in
)) {
825 debuga(_("Failed to read the number of users in %s\n"),wdir
);
835 void obttotal(const char *dirname
, const char *name
, int nuser
, long long int *tbytes
, long long int *media
)
840 char user
[MAX_USER_LEN
];
842 struct getwordstruct gwarea
;
848 sprintf(wdir
,"%s%s/sarg-general",dirname
,name
);
849 if ((fp_in
= fopen(wdir
, "r")) == 0) {
850 sprintf(wdir
,"%s%s/general",dirname
,name
);
851 if ((fp_in
= fopen(wdir
, "r")) == 0) {
856 if ((line
=longline_create())==NULL
) {
857 debuga(_("Not enough memory to read the file %s\n"),wdir
);
861 while((buf
=longline_read(fp_in
,line
))!=NULL
) {
862 if (strncmp(buf
,"TOTAL\t",6) == 0)
864 else if (strncmp(buf
,"TOTAL ",6) == 0)
868 getword_start(&gwarea
,buf
);
869 if (getword(user
,sizeof(user
),&gwarea
,sep
)<0) {
870 debuga(_("There is a invalid user in file %s\n"),wdir
);
873 if(strcmp(user
,"TOTAL") != 0)
875 if (getword_skip(MAXLEN
,&gwarea
,sep
)<0) {
876 debuga(_("There a broken total number of access in file %s\n"),wdir
);
879 if (getword_atoll(tbytes
,&gwarea
,sep
)<0) {
880 debuga(_("There is a broken number of bytes in file %s\n"),wdir
);
886 longline_destroy(&line
);
891 *media
=*tbytes
/ nuser
;
895 int getperiod_fromsarglog(const char *arqtt
,struct periodstruct
*period
)
898 int day0
, month0
, year0
, hour0
, minute0
;
899 int day1
, month1
, year1
, hour1
, minute1
;
902 memset(period
,0,sizeof(*period
));
905 while((str
=strstr(str
,"sarg-"))!=NULL
) {
907 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
908 day0
=(str
[0]-'0')*10+(str
[1]-'0');
909 if (day0
<1 || day0
>31) continue;
911 month0
=(str
[0]-'0')*10+(str
[1]-'0')-1;
912 if (month0
<0 || month0
>11) continue;
915 for (i
=0 ; isdigit(str
[i
]) && i
<4 ; i
++) year0
=year0
*10+(str
[i
]-'0');
918 if (str
[0]!='_') continue;
921 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
922 hour0
=(str
[0]-'0')*10+(str
[1]-'0');
924 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
925 minute0
=(str
[0]-'0')*10+(str
[1]-'0');
928 if (*str
!= '-') continue;
931 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
932 day1
=(str
[0]-'0')*10+(str
[1]-'0');
933 if (day1
<1 || day1
>31) continue;
935 month1
=(str
[0]-'0')*10+(str
[1]-'0')-1;
936 if (month1
<0 || month1
>11) continue;
939 for (i
=0 ; isdigit(str
[i
]) && i
<4 ; i
++) year1
=year1
*10+(str
[i
]-'0');
943 if (str
[0]!='_') continue;
946 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
947 hour1
=(str
[0]-'0')*10+(str
[1]-'0');
949 if (!isdigit(str
[0]) || !isdigit(str
[1])) continue;
950 minute1
=(str
[0]-'0')*10+(str
[1]-'0');
953 period
->start
.tm_mday
=day0
;
954 period
->start
.tm_mon
=month0
;
955 period
->start
.tm_year
=year0
-1900;
956 period
->start
.tm_hour
=hour0
;
957 period
->start
.tm_min
=minute0
;
958 period
->end
.tm_mday
=day1
;
959 period
->end
.tm_mon
=month1
;
960 period
->end
.tm_year
=year1
-1900;
961 period
->end
.tm_hour
=hour1
;
962 period
->end
.tm_min
=minute1
;
968 void getperiod_fromrange(struct periodstruct
*period
,int dfrom
,int duntil
)
970 memset(&period
->start
,0,sizeof(period
->start
));
971 period
->start
.tm_mday
=dfrom
%100;
972 period
->start
.tm_mon
=(dfrom
/100)%100-1;
973 period
->start
.tm_year
=(dfrom
/10000)-1900;
975 memset(&period
->end
,0,sizeof(period
->end
));
976 period
->end
.tm_mday
=duntil
%100;
977 period
->end
.tm_mon
=(duntil
/100)%100-1;
978 period
->end
.tm_year
=(duntil
/10000)-1900;
982 Update the \a main period to encompass the period in \a candidate.
984 void getperiod_merge(struct periodstruct
*main
,struct periodstruct
*candidate
)
989 mdate
=(main
->start
.tm_year
)*10000+(main
->start
.tm_mon
)*100+main
->start
.tm_mday
;
990 cdate
=(candidate
->start
.tm_year
)*10000+(candidate
->start
.tm_mon
)*100+candidate
->start
.tm_mday
;
991 if (cdate
<mdate
) memcpy(&main
->start
,&candidate
->start
,sizeof(struct tm
));
993 mdate
=(main
->end
.tm_year
)*10000+(main
->end
.tm_mon
)*100+main
->end
.tm_mday
;
994 cdate
=(candidate
->end
.tm_year
)*10000+(candidate
->end
.tm_mon
)*100+candidate
->end
.tm_mday
;
995 if (cdate
>mdate
) memcpy(&main
->end
,&candidate
->end
,sizeof(struct tm
));
998 int getperiod_buildtext(struct periodstruct
*period
)
1002 char text1
[40], text2
[40];
1005 i
=strftime(text1
, sizeof(text1
), "%Y %b %d", &period
->start
);
1006 } else if(df
=='e') {
1007 i
=strftime(text1
, sizeof(text1
), "%d %b %Y", &period
->start
);
1008 } else /*if (df=='w')*/ {
1009 IndexTree
=INDEX_TREE_FILE
;
1010 i
=strftime(text1
, sizeof(text1
), "%Y.%U", &period
->start
);
1012 if (i
== 0) return(-1);
1014 range
=(period
->start
.tm_year
!=period
->end
.tm_year
||
1015 period
->start
.tm_mon
!=period
->end
.tm_mon
||
1016 period
->start
.tm_mday
!=period
->end
.tm_mday
);
1019 i
=strftime(text2
, sizeof(text2
)-i
, "%Y %b %d", &period
->end
);
1020 } else if (df
=='e') {
1021 i
=strftime(text2
, sizeof(text2
)-i
, "%d %b %Y", &period
->end
);
1023 i
=strftime(text2
, sizeof(text2
)-i
, "%Y.%U", &period
->end
);
1025 if (i
== 0) return(-1);
1029 snprintf(period
->text
,sizeof(period
->text
),"%s-%s",text1
,text2
);
1030 snprintf(period
->html
,sizeof(period
->html
),"%s—%s",text1
,text2
);
1032 safe_strcpy(period
->text
,text1
,sizeof(period
->text
));
1033 safe_strcpy(period
->html
,text1
,sizeof(period
->html
));
1038 static void copy_images(void)
1040 FILE *img_in
, *img_ou
;
1042 char imgdir
[MAXLEN
];
1043 char srcfile
[MAXLEN
];
1044 char dstfile
[MAXLEN
];
1046 struct dirent
*direntp
;
1047 char buffer
[MAXLEN
];
1051 if (snprintf(images
,sizeof(images
),"%simages",outdir
)>=sizeof(images
)) {
1052 debuga(_("Cannot copy images to target directory %simages\n"),outdir
);
1055 if (access(images
,R_OK
)!=0) {
1056 if (mkdir(images
,0755)) {
1057 debuga(_("Cannot create directory %s - %s\n"),images
,strerror(errno
));
1062 strcpy(imgdir
,IMAGEDIR
);
1063 dirp
= opendir(imgdir
);
1065 debuga(_("(util) Can't open directory %s: %s\n"),imgdir
,strerror(errno
));
1068 while ((direntp
= readdir( dirp
)) != NULL
){
1069 if(direntp
->d_name
[0]=='.')
1071 sprintf(srcfile
,"%s/%s",imgdir
,direntp
->d_name
);
1072 if (stat(srcfile
,&info
)) {
1073 debuga(_("Cannot stat \"%s\" - %s\n"),srcfile
,strerror(errno
));
1076 if (S_ISREG(info
.st_mode
)) {
1077 sprintf(dstfile
,"%s/%s",images
,direntp
->d_name
);
1078 img_in
= fopen(srcfile
, "rb");
1080 img_ou
= fopen(dstfile
, "wb");
1082 while ((nread
= fread(buffer
,1,sizeof(buffer
),img_in
))>0) {
1083 if (fwrite(buffer
,1,nread
,img_ou
)!=nread
) {
1084 debuga(_("Failed to copy image %s to %s\n"),srcfile
,dstfile
);
1088 if (fclose(img_ou
)==EOF
) {
1089 debuga(_("Error while copying image %s: %s\n"),dstfile
,strerror(errno
));
1093 fprintf(stderr
,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open/create file", dstfile
, strerror(errno
));
1096 fprintf(stderr
,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open file", srcfile
, strerror(errno
));
1099 (void) closedir(dirp
);
1104 int vrfydir(const struct periodstruct
*per1
, const char *addr
, const char *site
, const char *us
, const char *form
)
1109 char dirname2
[MAXLEN
];
1117 strcpy(wdir
,outdir
);
1119 y1
=per1
->start
.tm_year
+1900;
1120 y2
=per1
->end
.tm_year
+1900;
1121 m1
=per1
->start
.tm_mon
+1;
1122 m2
=per1
->end
.tm_mon
+1;
1123 d1
=per1
->start
.tm_mday
;
1124 d2
=per1
->end
.tm_mday
;
1125 if(IndexTree
== INDEX_TREE_DATE
) {
1126 wlen
+=sprintf(wdir
+wlen
,"%04d",y1
);
1127 if(y1
!=y2
) wlen
+=sprintf(wdir
+wlen
,"-%04d",y2
);
1128 if(access(wdir
, R_OK
) != 0)
1131 wlen
+=sprintf(wdir
+wlen
,"/%02d",m1
);
1132 if(m1
!= m2
) wlen
+=sprintf(wdir
+wlen
,"-%02d",m2
);
1133 if(access(wdir
, R_OK
) != 0)
1136 wlen
+=sprintf(wdir
+wlen
,"/%02d",d1
);
1137 if(d1
!=d2
) wlen
+=sprintf(wdir
+wlen
,"-%02d",d2
);
1140 wlen
=snprintf(wdir
+wlen
,sizeof(wdir
)-wlen
,"%04d%s%02d-%04d%s%02d",y1
,
1141 conv_month_name(m1
),d1
,y2
,conv_month_name(m2
),d2
);
1142 } else if (df
== 'e') {
1143 wlen
=snprintf(wdir
+wlen
,sizeof(wdir
)-wlen
,"%02d%s%04d-%02d%s%04d",d1
,
1144 conv_month_name(m1
),y1
,d2
,conv_month_name(m2
),y2
);
1145 } else if (df
== 'w') {
1146 wlen2
=strftime(wdir
+wlen
, sizeof(wdir
)-wlen
, "%Y.%U", &per1
->start
);
1147 if (wlen2
==0) return(-1);
1153 struct userinfostruct
*uinfo
=userinfo_find_from_id(us
);
1156 strcat(wdir
,uinfo
->filename
);
1159 if(addr
[0] != '\0') {
1163 if(site
[0] != '\0') {
1168 strcpy(outdirname
,wdir
);
1170 if(IndexTree
!= INDEX_TREE_DATE
) {
1171 if(!OverwriteReport
) {
1173 if(access(wdir
,R_OK
) == 0) {
1174 sprintf(wdir
,"%s.%d",outdirname
,num
);
1183 debuga(_("File %s already exists, moved to %s\n"),outdirname
,wdir
);
1184 rename(outdirname
,wdir
);
1187 if(access(outdirname
,R_OK
) == 0) {
1188 unlinkdir(outdirname
,1);
1191 my_mkdir(outdirname
);
1193 strcpy(dirname2
,wdir
);
1194 if(!OverwriteReport
) {
1196 if(access(wdir
,R_OK
) == 0) {
1197 sprintf(wdir
,"%s.%d",dirname2
,num
);
1206 debuga(_("File %s already exists, moved to %s\n"),dirname2
,wdir
);
1207 rename(dirname2
,wdir
);
1208 strcpy(dirname2
,wdir
);
1211 if(access(wdir
,R_OK
) == 0) {
1216 if(access(wdir
, R_OK
) != 0)
1220 strcpy(dirname2
,wdir
);
1222 sprintf(wdir
,"%s/sarg-date",outdirname
);
1223 if ((fp_ou
= fopen(wdir
, "wt")) == 0) {
1224 debuga(_("cannot open %s for writing: %s\n"),wdir
,strerror(errno
));
1229 //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime));
1230 loctm
=localtime(&curtime
);
1231 strftime(wdir
,sizeof(wdir
),"%Y-%m-%d %H:%M:%S",loctm
);
1232 if (fprintf(fp_ou
,"%s %d\n",wdir
,loctm
->tm_isdst
)<0) {
1233 debuga(_("Failed to write the date in %s\n"),wdir
);
1237 if (fclose(fp_ou
)==EOF
) {
1238 debuga(_("Failed to write the date in %s: %s\n"),wdir
,strerror(errno
));
1247 Copy a string without overflowing the buffer. The copied string
1248 is properly terminated by an ASCII zero.
1250 \param dest The destination buffer.
1251 \param src The source buffer.
1252 \param length The size of the destination buffer. The program is aborted
1253 if the length is negative or zero.
1255 void safe_strcpy(char *dest
,const char *src
,int length
)
1258 debuga(_("Invalid buffer length passed to the function to safely copy a string\n"));
1261 strncpy(dest
,src
,length
-1);
1262 dest
[length
-1]='\0';
1265 void strip_latin(char *line
)
1272 for (i
=0;line
[i
];i
++){
1274 if (line
[i
]==';') skip
=0;
1286 void zdate(char *ftime
,int ftimesize
, char DateFormat
)
1292 local
= localtime(&t
);
1293 if (DateFormat
=='u')
1294 strftime(ftime
, ftimesize
, "%b/%d/%Y %H:%M", local
);
1295 else if (DateFormat
=='e')
1296 strftime(ftime
, ftimesize
, "%d/%b/%Y-%H:%M", local
);
1297 else if (DateFormat
=='w')
1298 strftime(ftime
, ftimesize
, "%W-%H-%M", local
);
1303 char *fixtime(long long int elap
)
1305 int num
= elap
/ 1000;
1309 static char buf
[12];
1312 min
=(num
% 3600) / 60;
1315 if(hor
==0 && min
==0 && sec
==0)
1318 sprintf(buf
,"%d:%02d:%02d",hor
,min
,sec
);
1324 void date_from(char *date
, int *dfrom
, int *duntil
)
1333 if (isdigit(date
[0])) {
1336 if (sscanf(date
,"%d/%d/%d%n",&d0
,&m0
,&y0
,&next
)!=3 || y0
<100 || m0
<1 || m0
>12 || d0
<1 || d0
>31 || next
<0) {
1337 debuga(_("The date passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
1340 if (date
[next
]=='-') {
1341 if (sscanf(date
+next
+1,"%d/%d/%d",&d1
,&m1
,&y1
)!=3 || y1
<100 || m1
<1 || m1
>12 || d1
<1 || d1
>31) {
1342 debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
1345 } else if (date
[next
]!='\0') {
1346 debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n"));
1356 struct tm
*Date0
,Date1
;
1358 if (time(&Today
)==(time_t)-1) {
1359 debuga(_("Failed to get the current time\n"));
1362 if (sscanf(date
,"day-%d",&i
)==1) {
1364 debuga(_("Invalid number of days in -d parameter\n"));
1368 Date0
=localtime(&Today
);
1370 debuga(_("Cannot convert local time: %s\n"),strerror(errno
));
1373 y0
=y1
=Date0
->tm_year
+1900;
1374 m0
=m1
=Date0
->tm_mon
+1;
1375 d0
=d1
=Date0
->tm_mday
;
1376 } else if (sscanf(date
,"week-%d",&i
)==1) {
1378 There is no portable way to find the first day of the week even though the
1379 information is available in the locale. nl_langinfo has the unofficial
1380 parameters _NL_TIME_FIRST_WEEKDAY and _NL_TIME_WEEK_1STDAY but they are
1381 undocumented as is their return value and it is discouraged to use them.
1382 Beside, nl_langinfo isn't available on windows and the first day of the
1383 week isn't available at all on that system.
1385 const int FirstWeekDay
=1;
1389 debuga(_("Invalid number of weeks in -d parameter\n"));
1392 Date0
=localtime(&Today
);
1394 debuga(_("Cannot convert local time: %s\n"),strerror(errno
));
1397 WeekBegin
=Today
-((Date0
->tm_wday
-FirstWeekDay
+7)%7)*24*60*60;
1398 WeekBegin
-=i
*7*24*60*60;
1399 Date0
=localtime(&WeekBegin
);
1401 debuga(_("Cannot convert local time: %s\n"),strerror(errno
));
1404 y0
=Date0
->tm_year
+1900;
1407 WeekBegin
+=6*24*60*60;
1408 Date0
=localtime(&WeekBegin
);
1410 debuga(_("Cannot convert local time: %s\n"),strerror(errno
));
1413 y1
=Date0
->tm_year
+1900;
1416 } else if (sscanf(date
,"month-%d",&i
)==1) {
1418 debuga(_("Invalid number of months in -d parameter\n"));
1421 Date0
=localtime(&Today
);
1423 debuga(_("Cannot convert local time: %s\n"),strerror(errno
));
1426 if (Date0
->tm_mon
<i
%12) {
1427 y0
=Date0
->tm_year
+1900-i
/12-1;
1428 m0
=(Date0
->tm_mon
+12-i
%12)%12+1;
1431 y0
=Date0
->tm_year
+1900-i
/12;
1432 m0
=Date0
->tm_mon
-i
%12+1;
1435 memcpy(&Date1
,Date0
,sizeof(struct tm
));
1440 Date1
.tm_year
=y0
-1900;
1443 Date1
.tm_year
=y0
-1900+1;
1447 Date0
=localtime(&t1
);
1448 y1
=Date0
->tm_year
+1900;
1452 debuga(_("Invalid date range passed on command line\n"));
1457 *dfrom
=y0
*10000+m0
*100+d0
;
1458 *duntil
=y1
*10000+m1
*100+d1
;
1459 sprintf(date
,"%02d/%02d/%04d-%02d/%02d/%04d",d0
,m0
,y0
,d1
,m1
,y1
);
1464 char *strlow(char *string
)
1470 for (s
= string
; *s
; ++s
)
1480 char *strup(char *string
)
1486 for (s
= string
; *s
; ++s
)
1494 void removetmp(const char *outdir
)
1499 if(!RemoveTempFiles
)
1503 debuga(_("Purging temporary file sarg-general\n"));
1505 if (snprintf(filename
,sizeof(filename
),"%s/sarg-general",outdir
)>=sizeof(filename
)) {
1506 debuga(_("(removetmp) directory too long to remove %s/sarg-period\n"),outdir
);
1509 if((fp_gen
=fopen(filename
,"w"))==NULL
){
1510 debuga(_("(removetmp) Cannot open file %s: %s\n"),filename
,strerror(errno
));
1513 totalger(fp_gen
,filename
);
1514 if (fclose(fp_gen
)==EOF
) {
1515 debuga(_("Failed to close %s after writing the total line: %s\n"),filename
,strerror(errno
));
1520 void load_excludecodes(const char *ExcludeCodes
)
1528 if(ExcludeCodes
[0] == '\0')
1531 if((fp_in
=fopen(ExcludeCodes
,"r"))==NULL
) {
1532 debuga(_("(util) Cannot open file %s (exclude_codes): %s\n"),ExcludeCodes
,strerror(errno
));
1536 if (fseek(fp_in
, 0, SEEK_END
)==-1) {
1537 debuga(_("Failed to move till the end of the excluded codes file %s: %s\n"),ExcludeCodes
,strerror(errno
));
1540 MemSize
= ftell(fp_in
);
1542 debuga(_("Cannot get the size of file %s\n"),ExcludeCodes
);
1545 if (fseek(fp_in
, 0, SEEK_SET
)==-1) {
1546 debuga(_("Failed to rewind the excluded codes file %s: %s\n"),ExcludeCodes
,strerror(errno
));
1551 if((excludecode
=(char *) malloc(MemSize
))==NULL
) {
1552 debuga(_("malloc error (%ld)\n"),MemSize
);
1555 memset(excludecode
,0,MemSize
);
1558 while(fgets(data
,sizeof(data
),fp_in
)!=NULL
) {
1559 if (data
[0]=='#') continue;
1560 for (i
=strlen(data
)-1 ; i
>=0 && (unsigned char)data
[i
]<=' ' ; i
--) data
[i
]='\0';
1562 if (Stored
+i
+2>=MemSize
) {
1563 debuga(_("Too many codes to exclude in file %s\n"),ExcludeCodes
);
1566 strcat(excludecode
,data
);
1567 strcat(excludecode
,";");
1575 void free_excludecodes(void)
1583 int vercode(const char *code
)
1588 if (excludecode
&& excludecode
[0]!='\0') {
1592 if (strncmp(code
,cod
,clen
)==0 && cod
[clen
]==';')
1594 cod
=strchr(cod
,';');
1601 void fixnone(char *str
)
1605 for (i
=strlen(str
)-1 ; i
>=0 && (unsigned char)str
[i
]<=' ' ; i
--);
1606 if(i
==3 && strncmp(str
,"none",4) == 0)
1612 void fixendofline(char *str
)
1616 for (i
=strlen(str
)-1 ; i
>=0 && (unsigned char)str
[i
]<=' ' ; i
--) str
[i
]=0;
1619 #ifdef LEGACY_TESTVALIDUSERCHAR
1620 int testvaliduserchar(const char *user
)
1625 for (y
=0; y
<strlen(UserInvalidChar
); y
++) {
1626 for (x
=0; x
<strlen(user
); x
++) {
1627 if(user
[x
] == UserInvalidChar
[y
])
1634 int testvaliduserchar(const char *user
)
1636 char * p_UserInvalidChar
= UserInvalidChar
;
1637 const char * p_user
;
1639 while( *p_UserInvalidChar
) {
1642 if( *p_UserInvalidChar
== *p_user
)
1646 p_UserInvalidChar
++ ;
1652 int compar( const void *a
, const void *b
)
1654 if( *(int *)a
> *(int *)b
) return 1;
1655 if( *(int *)a
< *(int *)b
) return -1;
1659 int getnumlist( char *buf
, numlist
*list
, const int len
, const int maxvalue
)
1661 int i
, j
, d
, flag
, r1
, r2
;
1662 char *pbuf
, **bp
, *strbufs
[ 24 ];
1665 strtok( buf
, " \t" );
1666 for( *bp
= strtok( NULL
, "," ), list
->len
= 0; *bp
; *bp
= strtok( NULL
, "," ) ) {
1667 if( ++bp
>= &strbufs
[ 24 ] )
1674 for( i
= 0; i
< list
->len
; i
++ ) {
1675 if( strchr( strbufs
[ i
], '-' ) != 0 ) {
1676 pbuf
= strbufs
[ i
];
1677 strtok( pbuf
, "-" );
1678 pbuf
= strtok( NULL
, "\0" );
1679 r1
= atoi( strbufs
[ i
] );
1680 if( ( r2
= atoi( pbuf
) ) >= maxvalue
|| r1
>= r2
)
1682 if( i
+ d
+ ( r2
- r1
) + 1 <= len
) {
1683 for( j
= r1
; j
<= r2
; j
++ )
1684 list
->list
[ i
+ d
++ ] = j
;
1689 if( ( list
->list
[ i
+ d
] = atoi( strbufs
[ i
] ) ) >= maxvalue
)
1693 qsort( list
->list
, list
->len
, sizeof( int ), compar
);
1696 for( i
= 0; i
< list
->len
- 1; i
++ )
1697 if( list
->list
[ i
] == list
->list
[ i
+ 1 ] ) {
1698 for( j
= i
+ 1; j
< list
->len
; j
++ )
1699 list
->list
[ j
- 1 ] = list
->list
[ j
];
1709 char *get_size(const char *path
, const char *file
)
1712 static char response
[255];
1716 if (snprintf(cmd
,sizeof(cmd
),"du -skh %s%s",path
,file
)>=sizeof(cmd
)) {
1717 debuga(_("Cannot get disk space because the path %s%s is too long\n"),path
,file
);
1720 if ((fp
= popen(cmd
, "r")) == NULL
) {
1721 debuga(_("Cannot get disk space with command %s\n"),cmd
);
1724 if (!fgets(response
, sizeof(response
), fp
)) {
1725 debuga(_("Cannot get disk size with command %s\n"),cmd
);
1728 ptr
=strchr(response
,'\t');
1730 debuga(_("The command %s failed\n"),cmd
);
1739 void show_info(FILE *fp_ou
)
1743 if(!ShowSargInfo
) return;
1744 zdate(ftime
, sizeof(ftime
), df
);
1745 fputs("<div class=\"info\">",fp_ou
);
1746 fprintf(fp_ou
,_("Generated by <a href='%s'>%s-%s</a> on %s"),URL
,PGM
,VERSION
,ftime
);
1747 fputs("</div>\n",fp_ou
);
1750 void show_sarg(FILE *fp_ou
, int depth
)
1754 if(!ShowSargLogo
) return;
1755 fputs("<div class=\"logo\"><a href=\"http://sarg.sourceforge.net\"><img src=\"",fp_ou
);
1756 for (i
=0 ; i
<depth
; i
++)
1758 fputs("images/sarg.png\" title=\"SARG, Squid Analysis Report Generator. Logo by Osamu Matsuzaki\" alt=\"Sarg\"></a> Squid Analysis Report Generator</div>\n",fp_ou
);
1761 void write_logo_image(FILE *fp_ou
)
1763 if(LogoImage
[0]!='\0')
1764 fprintf(fp_ou
, "<div class=\"logo\"><img src=\"%s\" width=\"%s\" height=\"%s\" alt=\"Logo\"> %s</div>\n",LogoImage
,Width
,Height
,LogoText
);
1767 void write_html_head(FILE *fp_ou
, int depth
, const char *page_title
,int javascript
)
1771 fputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n",fp_ou
);
1772 fprintf(fp_ou
, "<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet
);
1773 if (page_title
) fprintf(fp_ou
,"<title>%s</title>\n",page_title
);
1775 if ((javascript
& HTML_JS_SORTTABLE
)!=0 && SortTableJs
[0]) {
1776 fputs("<script type=\"text/javascript\" src=\"",fp_ou
);
1777 if (strncmp(SortTableJs
,"../",3)==0) {
1778 for (i
=0 ; i
<depth
; i
++) fputs("../",fp_ou
);
1780 fputs(SortTableJs
,fp_ou
);
1781 fputs("\"></script>\n",fp_ou
);
1783 fputs("</head>\n<body>\n",fp_ou
);
1786 void write_html_header(FILE *fp_ou
, int depth
, const char *page_title
,int javascript
)
1788 write_html_head(fp_ou
,depth
,page_title
,javascript
);
1789 write_logo_image(fp_ou
);
1790 show_sarg(fp_ou
, depth
);
1791 fprintf(fp_ou
,"<div class=\"title\"><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"title_c\">%s</th></tr>\n",Title
);
1794 void close_html_header(FILE *fp_ou
)
1796 fputs("</table></div>\n",fp_ou
);
1799 int write_html_trailer(FILE *fp_ou
)
1802 if (fputs("</body>\n</html>\n",fp_ou
)==EOF
) return(-1);
1806 void output_html_string(FILE *fp_ou
,const char *str
,int maxlen
)
1810 while (*str
&& (maxlen
<=0 || i
<maxlen
)) {
1813 fputs("&",fp_ou
);
1816 fputs("<",fp_ou
);
1819 fputs(">",fp_ou
);
1822 fputs(""",fp_ou
);
1825 fputs("'",fp_ou
);
1833 if (maxlen
>0 && i
>=maxlen
)
1834 fputs("…",fp_ou
);
1837 void output_html_url(FILE *fp_ou
,const char *url
)
1841 fputs("&",fp_ou
);
1849 Write a host name inside an A tag of a HTML file. If the host name starts
1850 with a star, it is assumed to be an alias that cannot be put inside a link
1851 so the A tag is not written around the host name.
1853 \param fp_ou The handle of the HTML file.
1854 \param url The host to display in the HTML file.
1855 \param maxlen The maximum number of characters to print into the host name.
1857 void output_html_link(FILE *fp_ou
,const char *url
,int maxlen
)
1859 if (url
[0]==ALIAS_PREFIX
) {
1860 // this is an alias, no need for a A tag
1861 output_html_string(fp_ou
,url
+1,100);
1863 if (skip_scheme(url
)==url
)
1864 fputs("<a href=\"http://",fp_ou
);//no scheme in the url, assume http:// to make the link clickable
1866 fputs("<a href=\"",fp_ou
);//the scheme is in the url, no need to add one
1867 output_html_url(fp_ou
,url
);
1869 output_html_string(fp_ou
,url
,100);
1870 fputs("</a>",fp_ou
);
1874 void url_module(const char *url
, char *w2
)
1880 for(x
=strlen(url
)-1; x
>=0; x
--) {
1881 if(url
[x
] == '/' || y
>=sizeof(w
)-1) break;
1890 for(y
=y
-1; y
>=0; y
--) {
1897 Mangle an URL to produce a part that can be used as an anchor in
1898 a html <a name=""> tag.
1900 \param url The URL to mangle.
1901 \param anchor The buffer to write the mangled URL.
1902 \param size The size of the buffer.
1904 void url_to_anchor(const char *url
,char *anchor
,int size
)
1910 for (i
=0 ; url
[i
] && url
[i
]!='/' && url
[i
]!='?' ; i
++);
1917 // only keep really safe characters
1923 if(isalnum(url
[i
]) || url
[i
]=='-' || url
[i
]=='_' || url
[i
]=='.') {
1927 if (!skip
) anchor
[--j
]='_';
1945 printf(_("SARG Version: %s\n"),VERSION
);
1949 char *get_param_value(const char *param
,char *line
)
1953 while (*line
==' ' || *line
=='\t') line
++;
1955 if (strncasecmp(line
,param
,plen
)) return(NULL
);
1956 if (line
[plen
]!=' ' && line
[plen
]!='\t') return(NULL
);
1958 while (*line
==' ' || *line
=='\t') line
++;
1962 void unlinkdir(const char *dir
,bool contentonly
)
1966 struct dirent
*direntp
;
1972 while ((direntp
= readdir(dirp
)) != NULL
) {
1973 if (direntp
->d_name
[0] == '.' && (direntp
->d_name
[1] == '\0' ||
1974 (direntp
->d_name
[1] == '.' && direntp
->d_name
[2] == '\0')))
1976 if (snprintf(dname
,sizeof(dname
),"%s/%s",dir
,direntp
->d_name
)>=sizeof(dname
)) {
1977 debuga(_("directory name to delete too long: %s/%s\n"),dir
,direntp
->d_name
);
1981 err
=lstat(dname
,&st
);
1983 err
=stat(dname
,&st
);
1986 debuga(_("cannot stat %s\n"),dname
);
1989 if (S_ISREG(st
.st_mode
)) {
1990 if (unlink(dname
)) {
1991 debuga(_("Cannot delete \"%s\": %s\n"),dname
,strerror(errno
));
1994 } else if (S_ISDIR(st
.st_mode
)) {
1997 debuga(_("unknown path type %s\n"),dname
);
2004 debuga(_("Cannot delete \"%s\": %s\n"),dir
,strerror(errno
));
2011 Delete every file from the temporary directory where sarg is told to store its
2014 As any stray file left over by a previous run would be included in the report, we
2015 must delete every file from the temporary directory before we start processing the logs.
2017 But the temporary directory is given by the user either in the configuration file or
2018 on the command line. We check that the user didn't give a wrong directory by looking
2019 at the files stored in the directory. If a single file is not one of ours, we abort.
2021 \param dir The temporary directory to purge.
2023 void emptytmpdir(const char *dir
)
2027 struct dirent
*direntp
;
2033 static const char *TmpExt
[]=
2048 // make sure the temporary directory contains only our files
2049 while ((direntp
= readdir(dirp
)) != NULL
) {
2050 if (direntp
->d_name
[0] == '.' && (direntp
->d_name
[1] == '\0' ||
2051 (direntp
->d_name
[1] == '.' && direntp
->d_name
[2] == '\0')))
2054 // is it one of our files
2055 dlen
=strlen(direntp
->d_name
);
2056 for (i
=sizeof(TmpExt
)/sizeof(TmpExt
[0])-1 ; i
>=0 ; i
--) {
2057 elen
=strlen(TmpExt
[i
]);
2058 if (dlen
>=elen
&& strcasecmp(direntp
->d_name
+dlen
-elen
,TmpExt
[i
])==0) break;
2061 debuga(_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
2062 "Please check the temporary directory you gave to sarg. Adjust the path to a safe "
2063 "directory or manually delete the content of \"%s\"\n"),direntp
->d_name
,dir
,dir
);
2067 if (snprintf(dname
,sizeof(dname
),"%s/%s",dir
,direntp
->d_name
)>=sizeof(dname
)) {
2068 debuga(_("directory name to delete too long: %s/%s\n"),dir
,direntp
->d_name
);
2073 err
=lstat(dname
,&st
);
2075 err
=stat(dname
,&st
);
2078 debuga(_("cannot stat \"%s\"\n"),dname
);
2081 if (S_ISDIR(st
.st_mode
)) {
2083 } else if (!S_ISREG(st
.st_mode
)) {
2084 debuga(_("Unknown path type for \"%s\". Check temporary directory\n"),dname
);
2090 // now delete our files
2091 while ((direntp
= readdir(dirp
)) != NULL
) {
2092 if (direntp
->d_name
[0] == '.' && (direntp
->d_name
[1] == '\0' ||
2093 (direntp
->d_name
[1] == '.' && direntp
->d_name
[2] == '\0')))
2096 // is it one of our files
2097 dlen
=strlen(direntp
->d_name
);
2098 for (i
=sizeof(TmpExt
)/sizeof(TmpExt
[0])-1 ; i
>=0 ; i
--) {
2099 elen
=strlen(TmpExt
[i
]);
2100 if (dlen
>=elen
&& strcasecmp(direntp
->d_name
+dlen
-elen
,TmpExt
[i
])==0) break;
2103 debuga(_("Unknown file \"%s\" found in temporary directory \"%s\". It is not one of our files. "
2104 "Please check the temporary directory you gave to sarg. Adjust the path to a safe "
2105 "directory or manually delete the content of \"%s\"\n"),direntp
->d_name
,dir
,dir
);
2109 if (snprintf(dname
,sizeof(dname
),"%s/%s",dir
,direntp
->d_name
)>=sizeof(dname
)) {
2110 debuga(_("directory name to delete too long: %s/%s\n"),dir
,direntp
->d_name
);
2114 err
=lstat(dname
,&st
);
2116 err
=stat(dname
,&st
);
2119 debuga(_("cannot stat \"%s\"\n"),dname
);
2122 if (S_ISREG(st
.st_mode
)) {
2123 if (unlink(dname
)) {
2124 debuga(_("Cannot delete \"%s\": %s\n"),dname
,strerror(errno
));
2128 debuga(_("unknown path type %s\n"),dname
);
2135 Extract an url, IPv4 or IPv6 from a buffer. The IP addresses may end with a
2138 \param buf The buffer to parse.
2139 \param text A pointer to set to the beginning of the string pattern. No terminating zero is inserted.
2140 The pointer may be NULL.
2141 \param ipv4 A 4 bytes buffer to store the bytes of the IPv4 address.
2142 \param ipv6 A 8 short integers buffer to store the values of the IPv6 address.
2143 \param nbits The number of prefix bits for an IP address.
2144 \param next The content of the line after the extracted address.
2146 \retval 3 The pattern is a IPv6 address.
2147 \retval 2 The pattern is a IPv4 address.
2148 \retval 1 The patter is a string.
2149 \retval 0 Empty pattern.
2151 int extract_address_mask(const char *buf
,const char **text
,unsigned char *ipv4
,unsigned short int *ipv6
,int *nbits
,const char **next
)
2156 unsigned int value4
, value6
;
2157 unsigned short int addr
[8];
2167 // skip leading spaces and tabs
2168 while (*buf
&& (*buf
==' ' || *buf
=='\t')) buf
++;
2170 // find out the nature of the pattern
2171 ip_size
=0x60 | 0x04;
2182 for (i
=0 ; (unsigned char)buf
[i
]>' ' && buf
[i
]!='/' && buf
[i
]!='?' && (!bracket
|| buf
[i
]!=']') && ip_size
; i
++) {
2183 if (ip_size
& 0x04) {
2184 if (isdigit(buf
[i
])) {
2186 port_num
=port_num
*10+(buf
[i
]-'0');
2187 if (port_num
>65535) ip_size
&=~0x04;
2189 value4
=value4
*10+(buf
[i
]-'0');
2190 if (value4
>0xFFU
) ip_size
&=~0x04;
2192 } else if (buf
[i
]=='.' && addr_len
<4) {
2193 addr
[addr_len
++]=(unsigned short)(value4
& 0xFFU
);
2195 } else if (!port
&& buf
[i
]==':') {
2201 if (ip_size
& 0x60) {
2202 if (isdigit(buf
[i
])) {
2203 value6
=(value6
<<4)+(buf
[i
]-'0');
2205 if (value6
>0xFFFFU
) ip_size
&=~0x60;
2206 } else if (toupper(buf
[i
])>='A' && toupper(buf
[i
])<='F') {
2207 value6
=(value6
<<4)+(toupper(buf
[i
])-'A'+10);
2209 if (value6
>0xFFFFU
) ip_size
&=~0x60;
2210 } else if (buf
[i
]==':' && addr_len
<8) {
2211 if (nibble6_len
>0) {
2212 addr
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
2216 if (buf
[i
+1]==':') {
2225 if (i
==0) return(0);
2226 if (ip_size
& 0x04) {
2230 addr
[addr_len
++]=(unsigned short)(value4
& 0xFFU
);
2232 if (ip_size
& 0x60) {
2233 if (pad_pos
<0 && addr_len
!=7) {
2235 } else if (pad_pos
>=0 && addr_len
>=7)
2237 else if (nibble6_len
>0)
2238 addr
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
2243 if (bracket
) (*text
)--;
2245 while ((unsigned char)buf
[i
]>' ') i
++;
2246 if (next
) *next
=buf
+i
;
2249 max_mask
=(ip_size
& 0x04) ? 4*8 : 8*16;
2253 while (isdigit(buf
[i
])) i
++;
2254 if (mask
<0 || mask
>max_mask
) mask
=max_mask
;
2257 if (ip_size
& 0x60 && bracket
&& buf
[i
]==']') i
++;
2258 if (next
) *next
=buf
+i
;
2259 if (ip_size
& 0x04) {
2260 if (nbits
) *nbits
=mask
;
2261 for (i
=0 ; i
<addr_len
; i
++)
2262 ipv4
[i
]=(unsigned char)addr
[i
];
2267 if (nbits
) *nbits
=mask
;
2272 ipv6
[j
++]=(unsigned short int)addr
[i
++];
2274 while (j
<pad_pos
+pad_len
)
2278 ipv6
[j
++]=(unsigned short int)addr
[i
++];