From 9bd92830048ace91561922e97c89bbdb01ac54f5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Marchal?= Date: Tue, 21 Dec 2010 20:22:24 +0000 Subject: [PATCH] Reindent code with tabs instead of space This is useless except for me as I use tabs with other projects and have to reconfigure my editor each time I switch to sarg. --- auth.c | 62 +- authfail.c | 329 ++-- btree_cache.c | 10 +- charset.c | 28 +- convlog.c | 84 +- css.c | 78 +- dansguardian_log.c | 223 ++- dansguardian_report.c | 263 ++-- datafile.c | 425 +++--- decomp.c | 72 +- denied.c | 291 ++-- download.c | 451 +++--- email.c | 424 +++--- exclude.c | 657 ++++---- getconf.c | 1031 +++++++------ grepday.c | 1326 ++++++++--------- html.c | 1196 +++++++-------- index.c | 1188 +++++++-------- indexonly.c | 40 +- ip2name.c | 124 +- lastlog.c | 193 ++- log.c | 3302 ++++++++++++++++++++--------------------- longline.c | 184 +-- realtime.c | 443 +++--- repday.c | 393 +++-- report.c | 1440 +++++++++--------- siteuser.c | 393 +++-- smartfilter.c | 337 +++-- sort.c | 299 ++-- splitlog.c | 92 +- squidguard_log.c | 648 ++++---- squidguard_report.c | 301 ++-- topsites.c | 472 +++--- topuser.c | 795 +++++----- totday.c | 201 ++- totger.c | 90 +- usage.c | 54 +- useragent.c | 574 ++++--- userinfo.c | 182 +-- usertab.c | 356 ++--- util.c | 2752 +++++++++++++++++----------------- 41 files changed, 10877 insertions(+), 10926 deletions(-) diff --git a/auth.c b/auth.c index 5a54814..42cbe45 100644 --- a/auth.c +++ b/auth.c @@ -29,40 +29,40 @@ void htaccess(const struct userinfostruct *uinfo) { - char htname[MAXLEN]; - char line[MAXLEN]; - FILE *fp_in; - FILE *fp_auth; - size_t i,nread; + char htname[MAXLEN]; + char line[MAXLEN]; + FILE *fp_in; + FILE *fp_auth; + size_t i,nread; - if(!UserAuthentication) - return; + if(!UserAuthentication) + return; - if (snprintf(htname,sizeof(htname),"%s/%s/.htaccess",outdirname,uinfo->filename)>=sizeof(htname)) { - debuga(_("File name too long: %s/%s/.htaccess\n"),outdirname,uinfo->filename); - exit(EXIT_FAILURE); - } - if((fp_auth=fopen(htname,"w"))==NULL) { - debuga(_("(auth) Cannot open file: %s - %s\n"),htname,strerror(errno)); - exit(EXIT_FAILURE); - } + if (snprintf(htname,sizeof(htname),"%s/%s/.htaccess",outdirname,uinfo->filename)>=sizeof(htname)) { + debuga(_("File name too long: %s/%s/.htaccess\n"),outdirname,uinfo->filename); + exit(EXIT_FAILURE); + } + if((fp_auth=fopen(htname,"w"))==NULL) { + debuga(_("(auth) Cannot open file: %s - %s\n"),htname,strerror(errno)); + exit(EXIT_FAILURE); + } - if ((fp_in=fopen(AuthUserTemplateFile,"r"))==NULL) { - debuga(_("(auth) Cannot open template file: %s - %s\n"),AuthUserTemplateFile,strerror(errno)); - exit(EXIT_FAILURE); - } + if ((fp_in=fopen(AuthUserTemplateFile,"r"))==NULL) { + debuga(_("(auth) Cannot open template file: %s - %s\n"),AuthUserTemplateFile,strerror(errno)); + exit(EXIT_FAILURE); + } - while((nread=fread(line,1,sizeof(line),fp_in))!=0) { - for (i=0 ; iid,fp_auth); - i++; - } else { - fputc(line[i],fp_auth); - } - } - fclose(fp_auth); - fclose(fp_in); + while((nread=fread(line,1,sizeof(line),fp_in))!=0) { + for (i=0 ; iid,fp_auth); + i++; + } else { + fputc(line[i],fp_auth); + } + } + fclose(fp_auth); + fclose(fp_in); - return; + return; } diff --git a/authfail.c b/authfail.c index be63469..875053c 100644 --- a/authfail.c +++ b/authfail.c @@ -29,169 +29,168 @@ void authfail_report(void) { - - FILE *fp_in = NULL, *fp_ou = NULL; - - char *buf; - char *url; - char authfail_in[MAXLEN]; - char report[MAXLEN]; - char ip[MAXLEN]; - char oip[MAXLEN]; - char user[MAXLEN]; - char ouser[MAXLEN]; - char ouser2[MAXLEN]; - char data[15]; - char hora[15]; - char tmp4[MAXLEN]; - char csort[MAXLEN]; - int z=0; - int count=0; - int cstatus; - int day,month,year; - bool new_user; - struct getwordstruct gwarea; - longline line; - struct userinfostruct *uinfo; - struct tm t; - - if(DataFile[0] != '\0') return; - - ouser[0]='\0'; - ouser2[0]='\0'; - oip[0]='\0'; - - snprintf(tmp4,sizeof(tmp4),"%s/authfail.log.unsort",tmp); - - if(authfail_count == 0) { - unlink(tmp4); - return; - } - - snprintf(authfail_in,sizeof(authfail_in),"%s/authfail.log",tmp); - snprintf(report,sizeof(report),"%s/authfail.html",outdirname); - - snprintf(csort,sizeof(csort),"sort -b -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"", tmp, authfail_in, tmp4); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - if((fp_in=MY_FOPEN(authfail_in,"r"))==NULL) { - debuga(_("(authfail) Cannot open file %s\n"),authfail_in); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - unlink(tmp4); - - if((fp_ou=MY_FOPEN(report,"w"))==NULL) { - debuga(_("(authfail) Cannot open file %s\n"),report); - exit(EXIT_FAILURE); - } - - write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Authentication Failures"),HTML_JS_NONE); - fputs("",fp_ou); - fprintf(fp_ou,_("Period: %s"),period.html); - fputs("\n",fp_ou); - fprintf(fp_ou,"%s\n",_("Authentication Failures")); - close_html_header(fp_ou); - - fputs("
\n",fp_ou); - fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read file %s\n"),authfail_in); - exit(EXIT_FAILURE); - } - - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(data,sizeof(data),&gwarea,'\t')<0) { - debuga(_("There is a broken date in file %s\n"),authfail_in); - exit(EXIT_FAILURE); - } - if (getword(hora,sizeof(hora),&gwarea,'\t')<0) { - debuga(_("There is a broken time in file %s\n"),authfail_in); - exit(EXIT_FAILURE); - } - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - debuga(_("There is a broken user ID in file %s\n"),authfail_in); - exit(EXIT_FAILURE); - } - if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("There is a broken IP address in file %s\n"),authfail_in); - exit(EXIT_FAILURE); - } - if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("There is a broken url in file %s\n"),authfail_in); - exit(EXIT_FAILURE); - } - if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; - computedate(year,month,day,&t); - strftime(data,sizeof(data),"%x",&t); - - uinfo=userinfo_find_from_id(user); - if (!uinfo) { - debuga(_("Unknown user ID %s in file %s\n"),user,authfail_in); - exit(EXIT_FAILURE); - } - - new_user=false; - if(z == 0) { - strcpy(ouser,user); - strcpy(oip,ip); - z++; - new_user=true; - } else { - if(strcmp(ouser,user) != 0) { - strcpy(ouser,user); - new_user=true; - } - if(strcmp(oip,ip) != 0) { - strcpy(oip,ip); - new_user=true; - } - } - - if(AuthfailReportLimit>0) { - if(strcmp(ouser2,uinfo->label) == 0) { - count++; - } else { - count=1; - strcpy(ouser2,uinfo->label); - } - if(count >= AuthfailReportLimit) - continue; - } - - fputs("",fp_ou); - if (new_user) - fprintf(fp_ou,"",uinfo->label,ip); - else - fputs("",fp_ou); - fprintf(fp_ou,"\n",fp_ou); - } - fclose(fp_in); - longline_destroy(&line); - - fputs("
%s%s%s%s
%s%s%s-%s",data,hora); - if(BlockIt[0]!='\0') { - fprintf(fp_ou," ",fp_ou); - } - fputs("",fp_ou); - output_html_string(fp_ou,url,100); - fputs("
\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),report); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - - unlink(authfail_in); - - return; + FILE *fp_in = NULL, *fp_ou = NULL; + + char *buf; + char *url; + char authfail_in[MAXLEN]; + char report[MAXLEN]; + char ip[MAXLEN]; + char oip[MAXLEN]; + char user[MAXLEN]; + char ouser[MAXLEN]; + char ouser2[MAXLEN]; + char data[15]; + char hora[15]; + char tmp4[MAXLEN]; + char csort[MAXLEN]; + int z=0; + int count=0; + int cstatus; + int day,month,year; + bool new_user; + struct getwordstruct gwarea; + longline line; + struct userinfostruct *uinfo; + struct tm t; + + if(DataFile[0] != '\0') return; + + ouser[0]='\0'; + ouser2[0]='\0'; + oip[0]='\0'; + + snprintf(tmp4,sizeof(tmp4),"%s/authfail.log.unsort",tmp); + + if(authfail_count == 0) { + unlink(tmp4); + return; + } + + snprintf(authfail_in,sizeof(authfail_in),"%s/authfail.log",tmp); + snprintf(report,sizeof(report),"%s/authfail.html",outdirname); + + snprintf(csort,sizeof(csort),"sort -b -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"", tmp, authfail_in, tmp4); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + if((fp_in=MY_FOPEN(authfail_in,"r"))==NULL) { + debuga(_("(authfail) Cannot open file %s\n"),authfail_in); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + unlink(tmp4); + + if((fp_ou=MY_FOPEN(report,"w"))==NULL) { + debuga(_("(authfail) Cannot open file %s\n"),report); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Authentication Failures"),HTML_JS_NONE); + fputs("",fp_ou); + fprintf(fp_ou,_("Period: %s"),period.html); + fputs("\n",fp_ou); + fprintf(fp_ou,"%s\n",_("Authentication Failures")); + close_html_header(fp_ou); + + fputs("
\n",fp_ou); + fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read file %s\n"),authfail_in); + exit(EXIT_FAILURE); + } + + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword(data,sizeof(data),&gwarea,'\t')<0) { + debuga(_("There is a broken date in file %s\n"),authfail_in); + exit(EXIT_FAILURE); + } + if (getword(hora,sizeof(hora),&gwarea,'\t')<0) { + debuga(_("There is a broken time in file %s\n"),authfail_in); + exit(EXIT_FAILURE); + } + if (getword(user,sizeof(user),&gwarea,'\t')<0) { + debuga(_("There is a broken user ID in file %s\n"),authfail_in); + exit(EXIT_FAILURE); + } + if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { + debuga(_("There is a broken IP address in file %s\n"),authfail_in); + exit(EXIT_FAILURE); + } + if (getword_ptr(buf,&url,&gwarea,'\t')<0) { + debuga(_("There is a broken url in file %s\n"),authfail_in); + exit(EXIT_FAILURE); + } + if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; + computedate(year,month,day,&t); + strftime(data,sizeof(data),"%x",&t); + + uinfo=userinfo_find_from_id(user); + if (!uinfo) { + debuga(_("Unknown user ID %s in file %s\n"),user,authfail_in); + exit(EXIT_FAILURE); + } + + new_user=false; + if(z == 0) { + strcpy(ouser,user); + strcpy(oip,ip); + z++; + new_user=true; + } else { + if(strcmp(ouser,user) != 0) { + strcpy(ouser,user); + new_user=true; + } + if(strcmp(oip,ip) != 0) { + strcpy(oip,ip); + new_user=true; + } + } + + if(AuthfailReportLimit>0) { + if(strcmp(ouser2,uinfo->label) == 0) { + count++; + } else { + count=1; + strcpy(ouser2,uinfo->label); + } + if(count >= AuthfailReportLimit) + continue; + } + + fputs("",fp_ou); + if (new_user) + fprintf(fp_ou,"",uinfo->label,ip); + else + fputs("",fp_ou); + fprintf(fp_ou,"\n",fp_ou); + } + fclose(fp_in); + longline_destroy(&line); + + fputs("
%s%s%s%s
%s%s%s-%s",data,hora); + if(BlockIt[0]!='\0') { + fprintf(fp_ou," ",fp_ou); + } + fputs("",fp_ou); + output_html_string(fp_ou,url,100); + fputs("
\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),report); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); + + unlink(authfail_in); + + return; } diff --git a/btree_cache.c b/btree_cache.c index 6eb11f7..5cd15c6 100644 --- a/btree_cache.c +++ b/btree_cache.c @@ -33,9 +33,9 @@ #define AVL_DOUBLE_LEFT_ROTATION 4 struct bt { - char value[64], targetattr[256]; - struct bt *left, *right; - int balanceinfo; + char value[64], targetattr[256]; + struct bt *left, *right; + int balanceinfo; }; struct bt *root_bt = NULL; @@ -223,7 +223,6 @@ void rotate_left(struct bt *node) if (parent->right == tmp) parent->right = node; } - } int get_disbalance_type(struct bt *node) @@ -295,9 +294,7 @@ void init_cache(void) int insert_to_cache(const char *key, const char *value) { - struct bt *root = NULL; - char strict_chars[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strict_chars_ptr; strict_chars_ptr = strict_chars; @@ -317,7 +314,6 @@ int insert_to_cache(const char *key, const char *value) } else return 1; - } char *search_in_cache(const char *key) diff --git a/charset.c b/charset.c index 7c0f44b..ddd92cd 100644 --- a/charset.c +++ b/charset.c @@ -29,18 +29,18 @@ void ccharset(char *CharSet) { - if(strcmp(CharSet,"Latin2") == 0) strcpy(CharSet,"ISO-8859-2"); - else if(strcmp(CharSet,"Latin3") == 0) strcpy(CharSet,"ISO-8859-3"); - else if(strcmp(CharSet,"Latin4") == 0) strcpy(CharSet,"ISO-8859-4"); - else if(strcmp(CharSet,"Cyrillic") == 0) strcpy(CharSet,"ISO-8859-5"); - else if(strcmp(CharSet,"Arabic") == 0) strcpy(CharSet,"ISO-8859-6"); - else if(strcmp(CharSet,"Greek") == 0) strcpy(CharSet,"ISO-8859-7"); - else if(strcmp(CharSet,"Hebrew") == 0) strcpy(CharSet,"ISO-8859-8"); - else if(strcmp(CharSet,"Latin5") == 0) strcpy(CharSet,"ISO-8859-9"); - else if(strcmp(CharSet,"Latin6") == 0) strcpy(CharSet,"ISO-8859-10"); - else if(strcmp(CharSet,"Windows-1251") == 0) strcpy(CharSet,"Windows-1251"); - else if(strcmp(CharSet,"Japan") == 0) strcpy(CharSet,"EUC-JP"); - else if(strcmp(CharSet,"Koi8-r") == 0) strcpy(CharSet,"KOI8-R"); - else if(strcmp(CharSet,"UTF-8") == 0) strcpy(CharSet,"UTF-8"); - return; + if(strcmp(CharSet,"Latin2") == 0) strcpy(CharSet,"ISO-8859-2"); + else if(strcmp(CharSet,"Latin3") == 0) strcpy(CharSet,"ISO-8859-3"); + else if(strcmp(CharSet,"Latin4") == 0) strcpy(CharSet,"ISO-8859-4"); + else if(strcmp(CharSet,"Cyrillic") == 0) strcpy(CharSet,"ISO-8859-5"); + else if(strcmp(CharSet,"Arabic") == 0) strcpy(CharSet,"ISO-8859-6"); + else if(strcmp(CharSet,"Greek") == 0) strcpy(CharSet,"ISO-8859-7"); + else if(strcmp(CharSet,"Hebrew") == 0) strcpy(CharSet,"ISO-8859-8"); + else if(strcmp(CharSet,"Latin5") == 0) strcpy(CharSet,"ISO-8859-9"); + else if(strcmp(CharSet,"Latin6") == 0) strcpy(CharSet,"ISO-8859-10"); + else if(strcmp(CharSet,"Windows-1251") == 0) strcpy(CharSet,"Windows-1251"); + else if(strcmp(CharSet,"Japan") == 0) strcpy(CharSet,"EUC-JP"); + else if(strcmp(CharSet,"Koi8-r") == 0) strcpy(CharSet,"KOI8-R"); + else if(strcmp(CharSet,"UTF-8") == 0) strcpy(CharSet,"UTF-8"); + return; } diff --git a/convlog.c b/convlog.c index 28e1189..512414c 100644 --- a/convlog.c +++ b/convlog.c @@ -29,54 +29,54 @@ void convlog(const char *arq, char *df, int dfrom, int duntil) { - FILE *fp_in; - char *buf; - char data[30]; - char dia[11]; - time_t tt; - int idata=0; - struct tm *t; - struct getwordstruct gwarea; - longline line; + FILE *fp_in; + char *buf; + char data[30]; + char dia[11]; + time_t tt; + int idata=0; + struct tm *t; + struct getwordstruct gwarea; + longline line; - if(arq[0] == '\0') - arq="/var/log/squid/access.log"; + if(arq[0] == '\0') + arq="/var/log/squid/access.log"; - if((fp_in=MY_FOPEN(arq,"r"))==NULL) { - debuga(_("(convlog) Cannot open log file %s - %s\n"),arq,strerror(errno)); - exit(EXIT_FAILURE); - } + if((fp_in=MY_FOPEN(arq,"r"))==NULL) { + debuga(_("(convlog) Cannot open log file %s - %s\n"),arq,strerror(errno)); + exit(EXIT_FAILURE); + } - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the log file %s\n"),arq); - exit(EXIT_FAILURE); - } + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the log file %s\n"),arq); + exit(EXIT_FAILURE); + } - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(data,sizeof(data),&gwarea,' ')<0) { - debuga(_("Maybe you have a broken record or garbage in file %s\n"),arq); - exit(EXIT_FAILURE); - } - tt=atoi(data); - t=localtime(&tt); + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword(data,sizeof(data),&gwarea,' ')<0) { + debuga(_("Maybe you have a broken record or garbage in file %s\n"),arq); + exit(EXIT_FAILURE); + } + tt=atoi(data); + t=localtime(&tt); - if(dfrom) { - idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday; - if(idata < dfrom || idata > duntil) - continue; - } + if(dfrom) { + idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday; + if(idata < dfrom || idata > duntil) + continue; + } - if(df[0]=='e') - strftime(dia, sizeof(dia), "%d/%m/%Y", t); - else - strftime(dia, sizeof(dia), "%m/%d/%Y", t); + if(df[0]=='e') + strftime(dia, sizeof(dia), "%d/%m/%Y", t); + else + strftime(dia, sizeof(dia), "%m/%d/%Y", t); - printf("%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t->tm_min,t->tm_sec,gwarea.current); - } + printf("%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t->tm_min,t->tm_sec,gwarea.current); + } - longline_destroy(&line); - if (fclose(fp_in)==EOF) { - debuga(_("Failed to close file %s - %s\n"),arq,strerror(errno)); - } + longline_destroy(&line); + if (fclose(fp_in)==EOF) { + debuga(_("Failed to close file %s - %s\n"),arq,strerror(errno)); + } } diff --git a/css.c b/css.c index c6e368a..90b94cd 100644 --- a/css.c +++ b/css.c @@ -29,58 +29,58 @@ void css_content(FILE *fp_css) { - fprintf(fp_css,".logo {font-family:Verdana,Tahoma,Arial;font-size:11px;color:%s;text-align:center;vertical-align:middle;border:none;padding:0px;margin-bottom:5px;}\n",LogoTextColor); - fprintf(fp_css,".logo th {padding:0px;}\n"); - fprintf(fp_css,".logo img {vertical-align:middle;padding:0px;border:0px none;}\n"); - fprintf(fp_css,".body {font-family:%s;font-size:%s;color:%s;background-color:%s;background-image:url(%s);}\n",FontFace,TitleFontSize,TxColor,BgColor,BgImage); - fprintf(fp_css,".info {font-family:%s;font-size:10px;text-align:center;margin-top:1em;margin-bottom:1em;}\n", FontFace); - fprintf(fp_css,".info a:link,a:visited {font-family:%s;color:#0000FF;font-size:10px;text-decoration:none;}\n", FontFace); + fprintf(fp_css,".logo {font-family:Verdana,Tahoma,Arial;font-size:11px;color:%s;text-align:center;vertical-align:middle;border:none;padding:0px;margin-bottom:5px;}\n",LogoTextColor); + fprintf(fp_css,".logo th {padding:0px;}\n"); + fprintf(fp_css,".logo img {vertical-align:middle;padding:0px;border:0px none;}\n"); + fprintf(fp_css,".body {font-family:%s;font-size:%s;color:%s;background-color:%s;background-image:url(%s);}\n",FontFace,TitleFontSize,TxColor,BgColor,BgImage); + fprintf(fp_css,".info {font-family:%s;font-size:10px;text-align:center;margin-top:1em;margin-bottom:1em;}\n", FontFace); + fprintf(fp_css,".info a:link,a:visited {font-family:%s;color:#0000FF;font-size:10px;text-decoration:none;}\n", FontFace); - fprintf(fp_css,".title {width:100%%;text-align:center;margin-bottom:1em;}\n"); - fprintf(fp_css,"div.title > table {margin:auto;}\n"); - fprintf(fp_css,".title_c {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:center;}\n",FontFace,TitleFontSize,TiColor,BgColor); - fprintf(fp_css,".title_l {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:left;}\n",FontFace,TitleFontSize,TiColor,BgColor); - fprintf(fp_css,".title_r {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:right;}\n",FontFace,TitleFontSize,TiColor,BgColor); + fprintf(fp_css,".title {width:100%%;text-align:center;margin-bottom:1em;}\n"); + fprintf(fp_css,"div.title > table {margin:auto;}\n"); + fprintf(fp_css,".title_c {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:center;}\n",FontFace,TitleFontSize,TiColor,BgColor); + fprintf(fp_css,".title_l {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:left;}\n",FontFace,TitleFontSize,TiColor,BgColor); + fprintf(fp_css,".title_r {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:right;}\n",FontFace,TitleFontSize,TiColor,BgColor); - fprintf(fp_css,".index {width:100%%;text-align:center;}\n"); - fprintf(fp_css,"div.index > table {margin:auto;}\n"); - fprintf(fp_css,".report {width:100%%;text-align:center;}\n"); - fprintf(fp_css,"div.report > table {margin:auto;}\n"); + fprintf(fp_css,".index {width:100%%;text-align:center;}\n"); + fprintf(fp_css,"div.index > table {margin:auto;}\n"); + fprintf(fp_css,".report {width:100%%;text-align:center;}\n"); + fprintf(fp_css,"div.report > table {margin:auto;}\n"); - fprintf(fp_css,".header_l {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:left;border-right:1px solid #666666;border-bottom:1px solid #666666;}\n",FontFace,HeaderFontSize,HeaderColor,HeaderBgColor); - fprintf(fp_css,".header_r {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:right;border-right:1px solid #666666;border-bottom:1px solid #666666;}\n",FontFace,HeaderFontSize,HeaderColor,HeaderBgColor); - fprintf(fp_css,".header_c {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:center;border-right:1px solid #666666;border-bottom:1px solid #666666;}\n",FontFace,HeaderFontSize,HeaderColor,HeaderBgColor); + fprintf(fp_css,".header_l {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:left;border-right:1px solid #666666;border-bottom:1px solid #666666;}\n",FontFace,HeaderFontSize,HeaderColor,HeaderBgColor); + fprintf(fp_css,".header_r {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:right;border-right:1px solid #666666;border-bottom:1px solid #666666;}\n",FontFace,HeaderFontSize,HeaderColor,HeaderBgColor); + fprintf(fp_css,".header_c {font-family:%s;font-size:%s;color:%s;background-color:%s;text-align:center;border-right:1px solid #666666;border-bottom:1px solid #666666;}\n",FontFace,HeaderFontSize,HeaderColor,HeaderBgColor); - fprintf(fp_css,".data {font-family:%s;color:%s;font-size:%s;background-color:%s;text-align:right;border-right:1px solid #6A5ACD;border-bottom:1px solid #6A5ACD;}\n", FontFace, TxColor, FontSize, TxBgColor); - fprintf(fp_css,".data a:link,a:visited {font-family:%s;color:#0000FF;font-size:%s;background-color:%s;text-align:right;text-decoration:none;}\n", FontFace, FontSize, TxBgColor); + fprintf(fp_css,".data {font-family:%s;color:%s;font-size:%s;background-color:%s;text-align:right;border-right:1px solid #6A5ACD;border-bottom:1px solid #6A5ACD;}\n", FontFace, TxColor, FontSize, TxBgColor); + fprintf(fp_css,".data a:link,a:visited {font-family:%s;color:#0000FF;font-size:%s;background-color:%s;text-align:right;text-decoration:none;}\n", FontFace, FontSize, TxBgColor); - fprintf(fp_css,".data2 {font-family:%s;color:%s;font-size:%s;background-color:%s;text-align:left;border-right:1px solid #6A5ACD;border-bottom:1px solid #6A5ACD;}\n", FontFace, TxColor, FontSize, TxBgColor); - fprintf(fp_css,".data2 a:link,a:visited {font-family:%s;color:#0000FF;font-size:%s;text-align:left;background-color:%s;text-decoration:none;}\n", FontFace, FontSize, TxBgColor); + fprintf(fp_css,".data2 {font-family:%s;color:%s;font-size:%s;background-color:%s;text-align:left;border-right:1px solid #6A5ACD;border-bottom:1px solid #6A5ACD;}\n", FontFace, TxColor, FontSize, TxBgColor); + fprintf(fp_css,".data2 a:link,a:visited {font-family:%s;color:#0000FF;font-size:%s;text-align:left;background-color:%s;text-decoration:none;}\n", FontFace, FontSize, TxBgColor); - fprintf(fp_css,".data3 {font-family:%s;color:%s;font-size:%s;background-color:%s;text-align:center;border-right:1px solid #6A5ACD;border-bottom:1px solid #6A5ACD;}\n", FontFace, TxColor, FontSize, TxBgColor); - fprintf(fp_css,".data3 a:link,a:visited {font-family:%s;color:#0000FF;font-size:%s;text-align:center;background-color:%s;text-decoration:none;}\n", FontFace, FontSize, TxBgColor); + fprintf(fp_css,".data3 {font-family:%s;color:%s;font-size:%s;background-color:%s;text-align:center;border-right:1px solid #6A5ACD;border-bottom:1px solid #6A5ACD;}\n", FontFace, TxColor, FontSize, TxBgColor); + fprintf(fp_css,".data3 a:link,a:visited {font-family:%s;color:#0000FF;font-size:%s;text-align:center;background-color:%s;text-decoration:none;}\n", FontFace, FontSize, TxBgColor); - fprintf(fp_css,".text {font-family:%s;color:%s;font-size:%s;background-color:%s;text-align:right;}\n", FontFace, TxColor, FontSize, TxBgColor); + fprintf(fp_css,".text {font-family:%s;color:%s;font-size:%s;background-color:%s;text-align:right;}\n", FontFace, TxColor, FontSize, TxBgColor); - fprintf(fp_css,".link {font-family:%s;font-size:%s;color:#0000FF;}\n", FontFace, FontSize); - fprintf(fp_css,".link a:link,a:visited {font-family:%s;font-size:%s;color:#0000FF;text-decoration:none;}\n", FontFace, FontSize); + fprintf(fp_css,".link {font-family:%s;font-size:%s;color:#0000FF;}\n", FontFace, FontSize); + fprintf(fp_css,".link a:link,a:visited {font-family:%s;font-size:%s;color:#0000FF;text-decoration:none;}\n", FontFace, FontSize); - fprintf(fp_css,"a > img {border:none;}\n"); + fprintf(fp_css,"a > img {border:none;}\n"); - if (SortTableJs[0]) { - fprintf(fp_css,".sorttable_nosort {cursor:default !important;}\n"); - fprintf(fp_css,".sortable thead th {cursor:pointer;}\n"); - } + if (SortTableJs[0]) { + fprintf(fp_css,".sorttable_nosort {cursor:default !important;}\n"); + fprintf(fp_css,".sortable thead th {cursor:pointer;}\n"); + } } void css(FILE *fp_css) { - if(ExternalCSSFile[0] != '\0') { - fprintf(fp_css,"\n",ExternalCSSFile); - return; - } + if(ExternalCSSFile[0] != '\0') { + fprintf(fp_css,"\n",ExternalCSSFile); + return; + } - fprintf(fp_css,"\n",fp_css); + fprintf(fp_css,"\n",fp_css); } diff --git a/dansguardian_log.c b/dansguardian_log.c index c6d081b..5fa1cca 100644 --- a/dansguardian_log.c +++ b/dansguardian_log.c @@ -29,116 +29,115 @@ void dansguardian_log(void) { - - FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL; - char buf[MAXLEN]; - char guard_in[MAXLEN]; - char guard_ou[MAXLEN]; - char loglocation[MAXLEN] = "/var/log/dansguardian/access.log"; - char year[10], mon[10], day[10]; - char hour[15]; - char user[MAXLEN], code1[255], code2[255]; - char ip[30]; - char wdata[127]; - char *url; - char tmp6[MAXLEN]; - int idata=0; - int cstatus; - int dfrom, duntil; - struct getwordstruct gwarea; - - dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+period.start.tm_mday; - duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.end.tm_mday; - - snprintf(guard_in,sizeof(guard_in),"%s/dansguardian.unsort",tmp); - snprintf(guard_ou,sizeof(guard_ou),"%s/dansguardian.log",tmp); - - if(access(DansGuardianConf, R_OK) != 0) { - debuga(_("Cannot open DansGuardian config file: %s\n"),DansGuardianConf); - exit(EXIT_FAILURE); - } - - if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) { - debuga(_("(dansguardian) Cannot open log file: %s\n"),DansGuardianConf); - exit(EXIT_FAILURE); - } - - if((fp_ou=MY_FOPEN(guard_in,"a"))==NULL) { - debuga(_("(dansguardian) Cannot open log file: %s\n"),guard_in); - exit(EXIT_FAILURE); - } - - while(fgets(buf,sizeof(buf),fp_guard)!=NULL) { - fixendofline(buf); - if(buf[0]=='#') - continue; - if(strstr(buf,"loglocation ") != 0) { - getword_start(&gwarea,buf); - if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),DansGuardianConf); - exit(EXIT_FAILURE); - } - break; - } - } - - if(debug) - debuga(_("Reading DansGuardian log file: %s\n"),loglocation); - - if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) { - debuga(_("(dansguardian) Cannot open log file: %s\n"),loglocation); - exit(EXIT_FAILURE); - } - - while(fgets(buf,sizeof(buf),fp_in) != NULL) { - if(strstr(buf," *DENIED* ") == 0) - continue; - getword_start(&gwarea,buf); - if (getword(year,sizeof(year),&gwarea,'.')<0 || getword(mon,sizeof(mon),&gwarea,'.')<0 || - getword(day,sizeof(day),&gwarea,' ')<0 || getword(hour,sizeof(hour),&gwarea,' ')<0 || - getword(user,sizeof(user),&gwarea,' ')<0 || getword(ip,sizeof(ip),&gwarea,' ')<0 || - getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation); - exit(EXIT_FAILURE); - } - if (getword_ptr(buf,&url,&gwarea,' ')<0) { - debuga(_("Maybe you have a broken url in your %s file\n"),loglocation); - exit(EXIT_FAILURE); - } - if (getword_skip(255,&gwarea,' ')<0 || - getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation); - exit(EXIT_FAILURE); - } - sprintf(wdata,"%s%02d%s",year,atoi(mon),day); - idata = atoi(wdata); - - if(DansguardianFilterOutDate) { - if(idata < dfrom && idata > duntil) - continue; - } - - if (strcmp(user,"-") == 0) { - strcpy(user,ip); - bzero(ip, 30); - } - fprintf(fp_ou,"%s\t%d\t%s\t%s\t%s\t%s\t%s\n",user,idata,hour,ip,url,code1,code2); - dansguardian_count++; - } - - if(fp_in) fclose(fp_in); - if(fp_guard) fclose(fp_guard); - if(fp_ou) fclose(fp_ou); - - if(debug) - debuga(_("Sorting file: %s\n"),guard_ou); - - snprintf(tmp6,sizeof(tmp6),"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou); - cstatus=system(tmp6); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),tmp6); - exit(EXIT_FAILURE); - } - unlink(guard_in); + FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL; + char buf[MAXLEN]; + char guard_in[MAXLEN]; + char guard_ou[MAXLEN]; + char loglocation[MAXLEN] = "/var/log/dansguardian/access.log"; + char year[10], mon[10], day[10]; + char hour[15]; + char user[MAXLEN], code1[255], code2[255]; + char ip[30]; + char wdata[127]; + char *url; + char tmp6[MAXLEN]; + int idata=0; + int cstatus; + int dfrom, duntil; + struct getwordstruct gwarea; + + dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+period.start.tm_mday; + duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.end.tm_mday; + + snprintf(guard_in,sizeof(guard_in),"%s/dansguardian.unsort",tmp); + snprintf(guard_ou,sizeof(guard_ou),"%s/dansguardian.log",tmp); + + if(access(DansGuardianConf, R_OK) != 0) { + debuga(_("Cannot open DansGuardian config file: %s\n"),DansGuardianConf); + exit(EXIT_FAILURE); + } + + if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) { + debuga(_("(dansguardian) Cannot open log file: %s\n"),DansGuardianConf); + exit(EXIT_FAILURE); + } + + if((fp_ou=MY_FOPEN(guard_in,"a"))==NULL) { + debuga(_("(dansguardian) Cannot open log file: %s\n"),guard_in); + exit(EXIT_FAILURE); + } + + while(fgets(buf,sizeof(buf),fp_guard)!=NULL) { + fixendofline(buf); + if(buf[0]=='#') + continue; + if(strstr(buf,"loglocation ") != 0) { + getword_start(&gwarea,buf); + if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),DansGuardianConf); + exit(EXIT_FAILURE); + } + break; + } + } + + if(debug) + debuga(_("Reading DansGuardian log file: %s\n"),loglocation); + + if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) { + debuga(_("(dansguardian) Cannot open log file: %s\n"),loglocation); + exit(EXIT_FAILURE); + } + + while(fgets(buf,sizeof(buf),fp_in) != NULL) { + if(strstr(buf," *DENIED* ") == 0) + continue; + getword_start(&gwarea,buf); + if (getword(year,sizeof(year),&gwarea,'.')<0 || getword(mon,sizeof(mon),&gwarea,'.')<0 || + getword(day,sizeof(day),&gwarea,' ')<0 || getword(hour,sizeof(hour),&gwarea,' ')<0 || + getword(user,sizeof(user),&gwarea,' ')<0 || getword(ip,sizeof(ip),&gwarea,' ')<0 || + getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation); + exit(EXIT_FAILURE); + } + if (getword_ptr(buf,&url,&gwarea,' ')<0) { + debuga(_("Maybe you have a broken url in your %s file\n"),loglocation); + exit(EXIT_FAILURE); + } + if (getword_skip(255,&gwarea,' ')<0 || + getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation); + exit(EXIT_FAILURE); + } + sprintf(wdata,"%s%02d%s",year,atoi(mon),day); + idata = atoi(wdata); + + if(DansguardianFilterOutDate) { + if(idata < dfrom && idata > duntil) + continue; + } + + if (strcmp(user,"-") == 0) { + strcpy(user,ip); + bzero(ip, 30); + } + fprintf(fp_ou,"%s\t%d\t%s\t%s\t%s\t%s\t%s\n",user,idata,hour,ip,url,code1,code2); + dansguardian_count++; + } + + if(fp_in) fclose(fp_in); + if(fp_guard) fclose(fp_guard); + if(fp_ou) fclose(fp_ou); + + if(debug) + debuga(_("Sorting file: %s\n"),guard_ou); + + snprintf(tmp6,sizeof(tmp6),"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou); + cstatus=system(tmp6); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),tmp6); + exit(EXIT_FAILURE); + } + unlink(guard_in); } diff --git a/dansguardian_report.c b/dansguardian_report.c index e1e3b3f..122769e 100644 --- a/dansguardian_report.c +++ b/dansguardian_report.c @@ -29,136 +29,135 @@ void dansguardian_report(void) { - - FILE *fp_in = NULL, *fp_ou = NULL; - - char buf[MAXLEN]; - char *url; - char dansguardian_in[MAXLEN]; - char report[MAXLEN]; - char ip[MAXLEN]; - char rule[255]; - char oip[MAXLEN]; - char user[MAXLEN]; - char ouser[MAXLEN]; - char date[15]; - char date2[15]; - char hour[15]; - char ouser2[255]; - int z=0; - int count=0; - struct getwordstruct gwarea; - - ouser[0]='\0'; - - snprintf(dansguardian_in,sizeof(dansguardian_in),"%s/dansguardian.log",tmp); - if(!dansguardian_count) { - unlink(dansguardian_in); - return; - } - - sprintf(report,"%s/dansguardian.html",outdirname); - - if((fp_in=MY_FOPEN(dansguardian_in,"r"))==NULL) { - debuga(_("(dansguardian_report) Cannot open log file %s\n"),dansguardian_in); - exit(EXIT_FAILURE); - } - - if((fp_ou=MY_FOPEN(report,"w"))==NULL) { - debuga(_("(dansguardian_report) Cannot open log file %s\n"),report); - exit(EXIT_FAILURE); - } - - write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("DansGuardian"),HTML_JS_NONE); - fputs("",fp_ou); - fprintf(fp_ou,_("Period: %s"),period.html); - fputs("\n",fp_ou); - fprintf(fp_ou,"%s\n",_("DansGuardian")); - close_html_header(fp_ou); - - fputs("
\n",fp_ou); - fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"),_("CAUSE")); - - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - getword_start(&gwarea,buf); - if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(date2,sizeof(date2),&gwarea,'\t')<0 || - getword(hour,sizeof(hour),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),dansguardian_in); - exit(EXIT_FAILURE); - } - if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken url in your %s file\n"),dansguardian_in); - exit(EXIT_FAILURE); - } - if (getword(rule,sizeof(rule),&gwarea,'\n')<0) { - debuga(_("Maybe you have a broken rule in your %s file\n"),dansguardian_in); - exit(EXIT_FAILURE); - } - - if(UserIp) - strcpy(user,ip); - - bzero(date, 15); - if(strncmp(df,"u",1) != 0) { - strncpy(date,date2+6,2); - strcat(date,"/"); - strncat(date,date2+4,2); - strcat(date,"/"); - strncat(date,date2,4); - } else { - strncpy(date,date2+4,2); - strcat(date,"/"); - strncat(date,date2+6,2); - strcat(date,"/"); - strncat(date,date2,4); - } - - if(Ip2Name) - ip2name(ip,sizeof(ip)); - - if(!z) { - strcpy(ouser,user); - strcpy(oip,ip); - z++; - } else { - if(strcmp(ouser,user) == 0) - user[0]='\0'; - if(user[0] != '\0') - strcpy(ouser,user); - if(strcmp(oip,ip) == 0) - ip[0]='\0'; - if(ip[0] != '\0') - strcpy(oip,ip); - } - - user_find(name, sizeof(name), user); - - if(DansGuardianReportLimit) { - if(strcmp(ouser2,name) == 0) { - count++; - } else { - count=1; - strcpy(ouser2,name); - } - if(count >= DansGuardianReportLimit) - continue; - } - - fprintf(fp_ou,"\n",rule); - } - fclose(fp_in); - - fputs("
%s%s%s%s%s
%s%s%s-%s",fp_ou); - output_html_string(fp_ou,url,100); - fprintf(fp_ou,"%s
\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),report); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - - unlink(dansguardian_in); - - return; + FILE *fp_in = NULL, *fp_ou = NULL; + + char buf[MAXLEN]; + char *url; + char dansguardian_in[MAXLEN]; + char report[MAXLEN]; + char ip[MAXLEN]; + char rule[255]; + char oip[MAXLEN]; + char user[MAXLEN]; + char ouser[MAXLEN]; + char date[15]; + char date2[15]; + char hour[15]; + char ouser2[255]; + int z=0; + int count=0; + struct getwordstruct gwarea; + + ouser[0]='\0'; + + snprintf(dansguardian_in,sizeof(dansguardian_in),"%s/dansguardian.log",tmp); + if(!dansguardian_count) { + unlink(dansguardian_in); + return; + } + + sprintf(report,"%s/dansguardian.html",outdirname); + + if((fp_in=MY_FOPEN(dansguardian_in,"r"))==NULL) { + debuga(_("(dansguardian_report) Cannot open log file %s\n"),dansguardian_in); + exit(EXIT_FAILURE); + } + + if((fp_ou=MY_FOPEN(report,"w"))==NULL) { + debuga(_("(dansguardian_report) Cannot open log file %s\n"),report); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("DansGuardian"),HTML_JS_NONE); + fputs("",fp_ou); + fprintf(fp_ou,_("Period: %s"),period.html); + fputs("\n",fp_ou); + fprintf(fp_ou,"%s\n",_("DansGuardian")); + close_html_header(fp_ou); + + fputs("
\n",fp_ou); + fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"),_("CAUSE")); + + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + getword_start(&gwarea,buf); + if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(date2,sizeof(date2),&gwarea,'\t')<0 || + getword(hour,sizeof(hour),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),dansguardian_in); + exit(EXIT_FAILURE); + } + if (getword_ptr(buf,&url,&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken url in your %s file\n"),dansguardian_in); + exit(EXIT_FAILURE); + } + if (getword(rule,sizeof(rule),&gwarea,'\n')<0) { + debuga(_("Maybe you have a broken rule in your %s file\n"),dansguardian_in); + exit(EXIT_FAILURE); + } + + if(UserIp) + strcpy(user,ip); + + bzero(date, 15); + if(strncmp(df,"u",1) != 0) { + strncpy(date,date2+6,2); + strcat(date,"/"); + strncat(date,date2+4,2); + strcat(date,"/"); + strncat(date,date2,4); + } else { + strncpy(date,date2+4,2); + strcat(date,"/"); + strncat(date,date2+6,2); + strcat(date,"/"); + strncat(date,date2,4); + } + + if(Ip2Name) + ip2name(ip,sizeof(ip)); + + if(!z) { + strcpy(ouser,user); + strcpy(oip,ip); + z++; + } else { + if(strcmp(ouser,user) == 0) + user[0]='\0'; + if(user[0] != '\0') + strcpy(ouser,user); + if(strcmp(oip,ip) == 0) + ip[0]='\0'; + if(ip[0] != '\0') + strcpy(oip,ip); + } + + user_find(name, sizeof(name), user); + + if(DansGuardianReportLimit) { + if(strcmp(ouser2,name) == 0) { + count++; + } else { + count=1; + strcpy(ouser2,name); + } + if(count >= DansGuardianReportLimit) + continue; + } + + fprintf(fp_ou,"\n",rule); + } + fclose(fp_in); + + fputs("
%s%s%s%s%s
%s%s%s-%s",fp_ou); + output_html_string(fp_ou,url,100); + fprintf(fp_ou,"%s
\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),report); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); + + unlink(dansguardian_in); + + return; } diff --git a/datafile.c b/datafile.c index 8bab494..485f203 100644 --- a/datafile.c +++ b/datafile.c @@ -31,242 +31,241 @@ void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nac void data_file(char *tmp) { + FILE *fp_in, *fp_ou=NULL; - FILE *fp_in, *fp_ou=NULL; + char *buf; + char accdia[11], acchora[9], accip[MAXLEN], *accurl; + char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN]; + char *oldurl; + char olduser[MAXLEN], acccode[50], oldacccode[50]; + char ipantes[MAXLEN], nameantes[MAXLEN]; + char accsmart[MAXLEN]; + char crc2[50]; + char *str; + char tmp3[MAXLEN]; + char user[MAX_USER_LEN]; + char u2[MAX_USER_LEN]; + DIR *dirp; + struct dirent *direntp; + long long int nbytes=0; + long long int nelap=0; + long long int nacc=0; + int rtotal=0; + long long int incache=0; + long long int oucache=0; + long long int accbytes; + long long int accelap; + int dlen; + int new_user; + int same_url; + int url_len; + int ourl_size; + struct getwordstruct gwarea; + struct userinfostruct *uinfo; + longline line; + const char logext[]=".log"; - char *buf; - char accdia[11], acchora[9], accip[MAXLEN], *accurl; - char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN]; - char *oldurl; - char olduser[MAXLEN], acccode[50], oldacccode[50]; - char ipantes[MAXLEN], nameantes[MAXLEN]; - char accsmart[MAXLEN]; - char crc2[50]; - char *str; - char tmp3[MAXLEN]; - char user[MAX_USER_LEN]; - char u2[MAX_USER_LEN]; - DIR *dirp; - struct dirent *direntp; - long long int nbytes=0; - long long int nelap=0; - long long int nacc=0; - int rtotal=0; - long long int incache=0; - long long int oucache=0; - long long int accbytes; - long long int accelap; - int dlen; - int new_user; - int same_url; - int url_len; - int ourl_size; - struct getwordstruct gwarea; - struct userinfostruct *uinfo; - longline line; - const char logext[]=".log"; + ipantes[0]='\0'; + nameantes[0]='\0'; - ipantes[0]='\0'; - nameantes[0]='\0'; + olduser[0]='\0'; + oldurl=NULL; + ourl_size=0; - olduser[0]='\0'; - oldurl=NULL; - ourl_size=0; + if ((dirp = opendir(tmp)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); + exit(EXIT_FAILURE); + } + while ( (direntp = readdir( dirp )) != NULL ) { + dlen=strlen(direntp->d_name)-(sizeof(logext)-1); + if (dlen<=0) continue; + if(strcmp(direntp->d_name+dlen,logext) != 0) + continue; - if ((dirp = opendir(tmp)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); - exit(EXIT_FAILURE); - } - while ( (direntp = readdir( dirp )) != NULL ) { - dlen=strlen(direntp->d_name)-(sizeof(logext)-1); - if (dlen<=0) continue; - if(strcmp(direntp->d_name+dlen,logext) != 0) - continue; + if (dlen>0) { + if (dlen>=sizeof(user)) continue; + strncpy(user,direntp->d_name,dlen); + user[dlen]=0; + } else { + user[0]='\0'; + } - if (dlen>0) { - if (dlen>=sizeof(user)) continue; - strncpy(user,direntp->d_name,dlen); - user[dlen]=0; - } else { - user[0]='\0'; - } + uinfo=userinfo_find_from_file(user); + if (!uinfo) { + debuga(_("Ignoring unknown user file %s\n"),user); + continue; + } + strcpy(u2,uinfo->id); + if(Ip2Name && uinfo->id_is_ip) { + strcpy(ipantes,u2); + ip2name(u2,sizeof(u2)); + strcpy(nameantes,u2); + } + user_find(uinfo->label,MAX_USER_LEN, u2); - uinfo=userinfo_find_from_file(user); - if (!uinfo) { - debuga(_("Ignoring unknown user file %s\n"),user); - continue; - } - strcpy(u2,uinfo->id); - if(Ip2Name && uinfo->id_is_ip) { - strcpy(ipantes,u2); - ip2name(u2,sizeof(u2)); - strcpy(nameantes,u2); - } - user_find(uinfo->label,MAX_USER_LEN, u2); + if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) { + debuga(_("(datafile) directory path too long: %s/%s\n"),tmp,direntp->d_name); + exit(EXIT_FAILURE); + } - if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) { - debuga(_("(datafile) directory path too long: %s/%s\n"),tmp,direntp->d_name); - exit(EXIT_FAILURE); - } + if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){ + debuga(_("(datafile) Cannot open file %s\n"),tmp3); + exit(EXIT_FAILURE); + } - if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){ - debuga(_("(datafile) Cannot open file %s\n"),tmp3); - exit(EXIT_FAILURE); - } + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the downloaded files.\n")); + exit(EXIT_FAILURE); + } - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the downloaded files.\n")); - exit(EXIT_FAILURE); - } + ttopen=0; + new_user=1; + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 || + getword(accip,sizeof(accip),&gwarea,'\t')<0 || + getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 || + getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) { + debuga(_("There is a broken record or garbage in file %s\n"),tmp3); + exit(EXIT_FAILURE); + } + if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) { + debuga(_("There is an invalid smart info in file %s\n"),tmp3); + exit(EXIT_FAILURE); + } - ttopen=0; - new_user=1; - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 || - getword(accip,sizeof(accip),&gwarea,'\t')<0 || - getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 || - getword(acccode,sizeof(acccode),&gwarea,'\t')<0 || getword_atoll(&accelap,&gwarea,'\t')<0) { - debuga(_("There is a broken record or garbage in file %s\n"),tmp3); - exit(EXIT_FAILURE); - } - if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) { - debuga(_("There is an invalid smart info in file %s\n"),tmp3); - exit(EXIT_FAILURE); - } + if(Ip2Name) { + if(strcmp(accip,ipantes) != 0) { + strcpy(ipantes,accip); + ip2name(accip,sizeof(accip)); + strcpy(nameantes,accip); + } + else strcpy(accip,nameantes); + } - if(Ip2Name) { - if(strcmp(accip,ipantes) != 0) { - strcpy(ipantes,accip); - ip2name(accip,sizeof(accip)); - strcpy(nameantes,accip); - } - else strcpy(accip,nameantes); - } + if(!rtotal){ + url_len=strlen(accurl); + if (!oldurl || url_len>=ourl_size) { + ourl_size=url_len+1; + oldurl=realloc(oldurl,ourl_size); + if (!oldurl) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(oldurl,accurl); + strcpy(oldacccode,acccode); + strcpy(oldaccip,accip); + strcpy(oldaccdia,accdia); + strcpy(oldacchora,acchora); + new_user=0; + rtotal++; + } + same_url=(strcmp(oldurl,accurl) == 0); - if(!rtotal){ - url_len=strlen(accurl); - if (!oldurl || url_len>=ourl_size) { - ourl_size=url_len+1; - oldurl=realloc(oldurl,ourl_size); - if (!oldurl) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(oldurl,accurl); - strcpy(oldacccode,acccode); - strcpy(oldaccip,accip); - strcpy(oldaccdia,accdia); - strcpy(oldacchora,acchora); - new_user=0; - rtotal++; - } - same_url=(strcmp(oldurl,accurl) == 0); + if(!same_url || new_user){ + if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){ + debuga(_("(datafile) Cannot open file %s\n"),DataFile); + exit(EXIT_FAILURE); + } + saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache); + nacc=0; + nbytes=0; + nelap=0; + incache=0; + oucache=0; + new_user=0; + } - if(!same_url || new_user){ - if(!fp_ou && (fp_ou=MY_FOPEN(DataFile,"w"))==NULL){ - debuga(_("(datafile) Cannot open file %s\n"),DataFile); - exit(EXIT_FAILURE); - } - saverecs(fp_ou,uinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache); - nacc=0; - nbytes=0; - nelap=0; - incache=0; - oucache=0; - new_user=0; - } + nacc++; + nbytes+=accbytes; + nelap+=accelap; - nacc++; - nbytes+=accbytes; - nelap+=accelap; + strcpy(crc2,acccode); + str=strchr(crc2,'/'); + if (str) *str='\0'; - strcpy(crc2,acccode); - str=strchr(crc2,'/'); - if (str) *str='\0'; + if(strstr(crc2,"MISS") != 0) oucache+=accbytes; + else incache+=accbytes; - if(strstr(crc2,"MISS") != 0) oucache+=accbytes; - else incache+=accbytes; + if (!same_url) { + url_len=strlen(accurl); + if (url_len>=ourl_size) { + ourl_size=url_len+1; + oldurl=realloc(oldurl,ourl_size); + if (!oldurl) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(oldurl,accurl); + } + new_user=0; + strcpy(oldacccode,acccode); + strcpy(oldaccip,accip); + strcpy(oldaccdia,accdia); + strcpy(oldacchora,acchora); + } - if (!same_url) { - url_len=strlen(accurl); - if (url_len>=ourl_size) { - ourl_size=url_len+1; - oldurl=realloc(oldurl,ourl_size); - if (!oldurl) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(oldurl,accurl); - } - new_user=0; - strcpy(oldacccode,acccode); - strcpy(oldaccip,accip); - strcpy(oldaccdia,accdia); - strcpy(oldacchora,acchora); - } + fclose(fp_in); + longline_destroy(&line); + } + (void)closedir( dirp ); + if (oldurl) free(oldurl); + if (fp_ou) fclose(fp_ou); - fclose(fp_in); - longline_destroy(&line); - } - (void)closedir( dirp ); - if (oldurl) free(oldurl); - if (fp_ou) fclose(fp_ou); - - if(debug) - debuga(_("Datafile %s written successfully\n"),DataFile); + if(debug) + debuga(_("Datafile %s written successfully\n"),DataFile); } void saverecs(FILE *fp_ou, const struct userinfostruct *uinfo, long long int nacc, char *url, long long int nbytes, char *ip, char *hora, char *dia, long long int nelap, long long int incache, long long int oucache) { - char val[20]; - char name[512]; + char val[20]; + char name[512]; - if((DataFileFields & DATA_FIELD_USER) != 0) { - fputs(uinfo->label,fp_ou); - fputc(DataFileDelimiter[0],fp_ou); - } - if((DataFileFields & DATA_FIELD_DATE) != 0) { - fputs(dia,fp_ou); - fputc(DataFileDelimiter[0],fp_ou); - } - if((DataFileFields & DATA_FIELD_TIME) != 0) { - fputs(hora,fp_ou); - fputc(DataFileDelimiter[0],fp_ou); - } - if((DataFileFields & DATA_FIELD_URL) != 0) { - url_hostname(url,name,sizeof(name)); - if (DataFileUrl == DATAFILEURL_IP) name2ip(name); - fputs(name,fp_ou); - fputc(DataFileDelimiter[0],fp_ou); - } - if((DataFileFields & DATA_FIELD_CONNECT) != 0) { - my_lltoa(nacc,val,sizeof(val),0); - fputs(val,fp_ou); - fputc(DataFileDelimiter[0],fp_ou); - } - if((DataFileFields & DATA_FIELD_BYTES) != 0) { - my_lltoa(nbytes,val,sizeof(val),0); - fputs(val,fp_ou); - fputc(DataFileDelimiter[0],fp_ou); - } - if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) { - my_lltoa(incache,val,sizeof(val),0); - fputs(val,fp_ou); - fputc(DataFileDelimiter[0],fp_ou); - } - if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) { - my_lltoa(oucache,val,sizeof(val),0); - fputs(val,fp_ou); - fputc(DataFileDelimiter[0],fp_ou); - } - if((DataFileFields & DATA_FIELD_ELAPSED) != 0) { - my_lltoa(nelap,val,sizeof(val),0); - fputs(val,fp_ou); - fputc(DataFileDelimiter[0],fp_ou); - } + if((DataFileFields & DATA_FIELD_USER) != 0) { + fputs(uinfo->label,fp_ou); + fputc(DataFileDelimiter[0],fp_ou); + } + if((DataFileFields & DATA_FIELD_DATE) != 0) { + fputs(dia,fp_ou); + fputc(DataFileDelimiter[0],fp_ou); + } + if((DataFileFields & DATA_FIELD_TIME) != 0) { + fputs(hora,fp_ou); + fputc(DataFileDelimiter[0],fp_ou); + } + if((DataFileFields & DATA_FIELD_URL) != 0) { + url_hostname(url,name,sizeof(name)); + if (DataFileUrl == DATAFILEURL_IP) name2ip(name); + fputs(name,fp_ou); + fputc(DataFileDelimiter[0],fp_ou); + } + if((DataFileFields & DATA_FIELD_CONNECT) != 0) { + my_lltoa(nacc,val,sizeof(val),0); + fputs(val,fp_ou); + fputc(DataFileDelimiter[0],fp_ou); + } + if((DataFileFields & DATA_FIELD_BYTES) != 0) { + my_lltoa(nbytes,val,sizeof(val),0); + fputs(val,fp_ou); + fputc(DataFileDelimiter[0],fp_ou); + } + if((DataFileFields & DATA_FIELD_IN_CACHE) != 0) { + my_lltoa(incache,val,sizeof(val),0); + fputs(val,fp_ou); + fputc(DataFileDelimiter[0],fp_ou); + } + if((DataFileFields & DATA_FIELD_OUT_CACHE) != 0) { + my_lltoa(oucache,val,sizeof(val),0); + fputs(val,fp_ou); + fputc(DataFileDelimiter[0],fp_ou); + } + if((DataFileFields & DATA_FIELD_ELAPSED) != 0) { + my_lltoa(nelap,val,sizeof(val),0); + fputs(val,fp_ou); + fputc(DataFileDelimiter[0],fp_ou); + } - fputc('\n',fp_ou); + fputc('\n',fp_ou); } diff --git a/decomp.c b/decomp.c index d7dd5af..2bbb138 100644 --- a/decomp.c +++ b/decomp.c @@ -29,45 +29,45 @@ FILE *decomp(const char *arq, bool *pipe) { - char cmd[1024]; - int arqlen; + char cmd[1024]; + int arqlen; - if(access(arq, R_OK) != 0) { - debuga(_("File not found: %s\n"),arq); - exit(EXIT_FAILURE); - } + if(access(arq, R_OK) != 0) { + debuga(_("File not found: %s\n"),arq); + exit(EXIT_FAILURE); + } - arqlen=strlen(arq); - if(arqlen>3 && strcmp(arq+arqlen-3,".gz") == 0) { - debuga(_("Decompressing log file \"%s\" with zcat\n"),arq); - if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) { - debuga(_("decompression command too long for log file %s\n"),arq); - exit(EXIT_FAILURE); - } - *pipe=true; - return(popen(cmd,"r")); - } + arqlen=strlen(arq); + if(arqlen>3 && strcmp(arq+arqlen-3,".gz") == 0) { + debuga(_("Decompressing log file \"%s\" with zcat\n"),arq); + if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) { + debuga(_("decompression command too long for log file %s\n"),arq); + exit(EXIT_FAILURE); + } + *pipe=true; + return(popen(cmd,"r")); + } - if(arqlen>4 && strcmp(arq+arqlen-4,".bz2") == 0) { - debuga(_("Decompressing log file \"%s\" with bzcat\n"),arq); - if (snprintf(cmd,sizeof(cmd),"bzcat \"%s\"",arq)>=sizeof(cmd)) { - debuga(_("decompression command too long for log file %s\n"),arq); - exit(EXIT_FAILURE); - } - *pipe=true; - return(popen(cmd,"r")); - } + if(arqlen>4 && strcmp(arq+arqlen-4,".bz2") == 0) { + debuga(_("Decompressing log file \"%s\" with bzcat\n"),arq); + if (snprintf(cmd,sizeof(cmd),"bzcat \"%s\"",arq)>=sizeof(cmd)) { + debuga(_("decompression command too long for log file %s\n"),arq); + exit(EXIT_FAILURE); + } + *pipe=true; + return(popen(cmd,"r")); + } - if(arqlen>2 && strcmp(arq+arqlen-2,".Z") == 0) { - debuga(_("Decompressing log file \"%s\" with zcat\n"),arq); - if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) { - debuga(_("decompression command too long for log file %s\n"),arq); - exit(EXIT_FAILURE); - } - *pipe=true; - return(popen(cmd,"r")); - } + if(arqlen>2 && strcmp(arq+arqlen-2,".Z") == 0) { + debuga(_("Decompressing log file \"%s\" with zcat\n"),arq); + if (snprintf(cmd,sizeof(cmd),"zcat \"%s\"",arq)>=sizeof(cmd)) { + debuga(_("decompression command too long for log file %s\n"),arq); + exit(EXIT_FAILURE); + } + *pipe=true; + return(popen(cmd,"r")); + } - *pipe=false; - return(MY_FOPEN(arq,"r")); + *pipe=false; + return(MY_FOPEN(arq,"r")); } diff --git a/denied.c b/denied.c index 553a622..5d2288e 100644 --- a/denied.c +++ b/denied.c @@ -29,155 +29,154 @@ static void show_ignored_denied(FILE *fp_ou,int count) { - char ignored[80]; + char ignored[80]; - snprintf(ignored,sizeof(ignored),ngettext("%d more denied access not shown here…","%d more denied accesses not shown here…",count),count); - fprintf(fp_ou,"%s\n",ignored); + snprintf(ignored,sizeof(ignored),ngettext("%d more denied access not shown here…","%d more denied accesses not shown here…",count),count); + fprintf(fp_ou,"%s\n",ignored); } void gen_denied_report(void) { - - FILE *fp_in = NULL, *fp_ou = NULL; - - char *buf; - char *url; - char denied_in[MAXLEN]; - char report[MAXLEN]; - char ip[MAXLEN]; - char oip[MAXLEN]; - char user[MAXLEN]; - char ouser[MAXLEN]; - char ouser2[MAXLEN]; - char data[15]; - char hora[15]; - bool z=false; - int count=0; - int day,month,year; - bool new_user; - struct getwordstruct gwarea; - longline line; - struct userinfostruct *uinfo; - struct tm t; - - ouser[0]='\0'; - ouser2[0]='\0'; - - sprintf(denied_in,"%s/denied.log",tmp); - if(!denied_count) { - unlink(denied_in); - return; - } - - sprintf(report,"%s/denied.html",outdirname); - - if((fp_in=MY_FOPEN(denied_in,"r"))==NULL) { - debuga(_("(denied) Cannot open log file %s\n"),denied_in); - exit(EXIT_FAILURE); - } - - if((fp_ou=MY_FOPEN(report,"w"))==NULL) { - debuga(_("(denied) Cannot open log file %s\n"),report); - exit(EXIT_FAILURE); - } - - write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied"),HTML_JS_NONE); - fputs("",fp_ou); - fprintf(fp_ou,_("Period: %s"),period.html); - fputs("\n",fp_ou); - fprintf(fp_ou,"%s\n",_("Denied")); - close_html_header(fp_ou); - - fputs("
\n",fp_ou); - fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the denied accesses\n")); - exit(EXIT_FAILURE); - } - - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || - getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("There is a broken record or garbage in file %s\n"),denied_in); - exit(EXIT_FAILURE); - } - if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("There is a broken url in file %s\n"),denied_in); - exit(EXIT_FAILURE); - } - if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; - computedate(year,month,day,&t); - strftime(data,sizeof(data),"%x",&t); - - uinfo=userinfo_find_from_id(user); - if (!uinfo) { - debuga(_("Unknown user ID %s in file %s\n"),user,denied_in); - exit(EXIT_FAILURE); - } - - new_user=false; - if(!z) { - strcpy(ouser,user); - strcpy(oip,ip); - z=true; - new_user=true; - } else { - if(strcmp(ouser,user) != 0) { - strcpy(ouser,user); - new_user=true; - } - if(strcmp(oip,ip) != 0) { - strcpy(oip,ip); - new_user=true; - } - } - - if(DeniedReportLimit) { - if(strcmp(ouser2,uinfo->label) == 0) { - count++; - } else { - if(count>DeniedReportLimit && DeniedReportLimit>0) - show_ignored_denied(fp_ou,count-DeniedReportLimit); - count=1; - strcpy(ouser2,uinfo->label); - } - if(count > DeniedReportLimit) - continue; - } - - fputs("",fp_ou); - if (new_user) - fprintf(fp_ou,"",uinfo->filename,uinfo->filename,uinfo->label,ip); - else - fputs("",fp_ou); - fprintf(fp_ou,"\n",fp_ou); - } - fclose(fp_in); - longline_destroy(&line); - - if(count>DeniedReportLimit && DeniedReportLimit>0) - show_ignored_denied(fp_ou,count-DeniedReportLimit); - - fputs("
%s%s%s%s
%s%s%s-%s",data,hora); - if(BlockIt[0] != '\0') { - fprintf(fp_ou," ",ImageFile); - } - fputs("http://",fp_ou); - output_html_string(fp_ou,url,100); - fputs("
\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),report); - if (fclose(fp_ou)<0) - debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - - if (unlink(denied_in)==-1) - debuga(_("Failed to delete the file \"%s\" after processing it - %s\n"),denied_in,strerror(errno)); - - return; + FILE *fp_in = NULL, *fp_ou = NULL; + + char *buf; + char *url; + char denied_in[MAXLEN]; + char report[MAXLEN]; + char ip[MAXLEN]; + char oip[MAXLEN]; + char user[MAXLEN]; + char ouser[MAXLEN]; + char ouser2[MAXLEN]; + char data[15]; + char hora[15]; + bool z=false; + int count=0; + int day,month,year; + bool new_user; + struct getwordstruct gwarea; + longline line; + struct userinfostruct *uinfo; + struct tm t; + + ouser[0]='\0'; + ouser2[0]='\0'; + + sprintf(denied_in,"%s/denied.log",tmp); + if(!denied_count) { + unlink(denied_in); + return; + } + + sprintf(report,"%s/denied.html",outdirname); + + if((fp_in=MY_FOPEN(denied_in,"r"))==NULL) { + debuga(_("(denied) Cannot open log file %s\n"),denied_in); + exit(EXIT_FAILURE); + } + + if((fp_ou=MY_FOPEN(report,"w"))==NULL) { + debuga(_("(denied) Cannot open log file %s\n"),report); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Denied"),HTML_JS_NONE); + fputs("",fp_ou); + fprintf(fp_ou,_("Period: %s"),period.html); + fputs("\n",fp_ou); + fprintf(fp_ou,"%s\n",_("Denied")); + close_html_header(fp_ou); + + fputs("
\n",fp_ou); + fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the denied accesses\n")); + exit(EXIT_FAILURE); + } + + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || + getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) { + debuga(_("There is a broken record or garbage in file %s\n"),denied_in); + exit(EXIT_FAILURE); + } + if (getword_ptr(buf,&url,&gwarea,'\t')<0) { + debuga(_("There is a broken url in file %s\n"),denied_in); + exit(EXIT_FAILURE); + } + if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; + computedate(year,month,day,&t); + strftime(data,sizeof(data),"%x",&t); + + uinfo=userinfo_find_from_id(user); + if (!uinfo) { + debuga(_("Unknown user ID %s in file %s\n"),user,denied_in); + exit(EXIT_FAILURE); + } + + new_user=false; + if(!z) { + strcpy(ouser,user); + strcpy(oip,ip); + z=true; + new_user=true; + } else { + if(strcmp(ouser,user) != 0) { + strcpy(ouser,user); + new_user=true; + } + if(strcmp(oip,ip) != 0) { + strcpy(oip,ip); + new_user=true; + } + } + + if(DeniedReportLimit) { + if(strcmp(ouser2,uinfo->label) == 0) { + count++; + } else { + if(count>DeniedReportLimit && DeniedReportLimit>0) + show_ignored_denied(fp_ou,count-DeniedReportLimit); + count=1; + strcpy(ouser2,uinfo->label); + } + if(count > DeniedReportLimit) + continue; + } + + fputs("",fp_ou); + if (new_user) + fprintf(fp_ou,"",uinfo->filename,uinfo->filename,uinfo->label,ip); + else + fputs("",fp_ou); + fprintf(fp_ou,"\n",fp_ou); + } + fclose(fp_in); + longline_destroy(&line); + + if(count>DeniedReportLimit && DeniedReportLimit>0) + show_ignored_denied(fp_ou,count-DeniedReportLimit); + + fputs("
%s%s%s%s
%s%s%s-%s",data,hora); + if(BlockIt[0] != '\0') { + fprintf(fp_ou," ",ImageFile); + } + fputs("http://",fp_ou); + output_html_string(fp_ou,url,100); + fputs("
\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),report); + if (fclose(fp_ou)<0) + debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); + + if (unlink(denied_in)==-1) + debuga(_("Failed to delete the file \"%s\" after processing it - %s\n"),denied_in,strerror(errno)); + + return; } diff --git a/download.c b/download.c index 10739e9..373465d 100644 --- a/download.c +++ b/download.c @@ -33,243 +33,242 @@ static int NDownloadSuffix=0; void download_report(void) { - - FILE *fp_in = NULL, *fp_ou = NULL; - - char *buf; - char *url; - char report_in[MAXLEN]; - char report[MAXLEN]; - char ip[MAXLEN]; - char oip[MAXLEN]; - char user[MAXLEN]; - char ouser[MAXLEN]; - char ouser2[MAXLEN]; - char data[15]; - char hora[15]; - int z=0; - int count=0; - int i; - int day,month,year; - bool new_user; - struct getwordstruct gwarea; - longline line; - struct userinfostruct *uinfo; - struct tm t; - - ouser[0]='\0'; - ouser2[0]='\0'; - - snprintf(report_in,sizeof(report_in),"%s/download.log",tmp); - if(access(report_in, R_OK) != 0) - return; - - snprintf(report,sizeof(report),"%s/download.html",outdirname); - - if((fp_in=MY_FOPEN(report_in,"r"))==NULL) { - debuga(_("(download) Cannot open log file %s\n"),report_in); - exit(EXIT_FAILURE); - } - - if((fp_ou=MY_FOPEN(report,"w"))==NULL) { - debuga(_("(download) Cannot open log file %s\n"),report); - exit(EXIT_FAILURE); - } - - write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloads"),HTML_JS_NONE); - fputs("",fp_ou); - fprintf(fp_ou,_("Period: %s"),period.html); - fputs("\n",fp_ou); - fprintf(fp_ou,"%s\n",_("Downloads")); - close_html_header(fp_ou); - - fputs("
\n",fp_ou); - fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the downloaded files\n")); - exit(EXIT_FAILURE); - } - - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || - getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("There is a broken record or garbage in file %s\n"),report_in); - exit(EXIT_FAILURE); - } - if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("There is a broken url in file %s\n"),report_in); - exit(EXIT_FAILURE); - } - if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; - computedate(year,month,day,&t); - strftime(data,sizeof(data),"%x",&t); - - uinfo=userinfo_find_from_id(user); - if (!uinfo) { - debuga(_("Unknown user ID %s in file %s\n"),user,report_in); - exit(EXIT_FAILURE); - } - new_user=false; - if(!z) { - strcpy(ouser,user); - strcpy(oip,ip); - z++; - new_user=true; - } else { - if(strcmp(ouser,user) != 0) { - strcpy(ouser,user); - new_user=true; - } - if(strcmp(oip,ip) != 0) { - strcpy(oip,ip); - new_user=true; - } - } - - if(DownloadReportLimit) { - if(strcmp(ouser2,uinfo->label) == 0) { - count++; - } else { - count=1; - strcpy(ouser2,uinfo->label); - } - if(count >= DownloadReportLimit) - continue; - } - - for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0; - - fputs("",fp_ou); - if (new_user) - fprintf(fp_ou,"",uinfo->label,ip); - else - fputs("",fp_ou); - fprintf(fp_ou,"\n",fp_ou); - } - fclose(fp_in); - longline_destroy(&line); - - fputs("
%s%s%s%s
%s%s%s-%s",data,hora); - if(BlockIt[0]!='\0') { - fprintf(fp_ou," ",ImageFile); - } - fputs("http://",fp_ou); - output_html_string(fp_ou,url,100); - fputs("
\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),report); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - - unlink(report_in); - - return; + FILE *fp_in = NULL, *fp_ou = NULL; + + char *buf; + char *url; + char report_in[MAXLEN]; + char report[MAXLEN]; + char ip[MAXLEN]; + char oip[MAXLEN]; + char user[MAXLEN]; + char ouser[MAXLEN]; + char ouser2[MAXLEN]; + char data[15]; + char hora[15]; + int z=0; + int count=0; + int i; + int day,month,year; + bool new_user; + struct getwordstruct gwarea; + longline line; + struct userinfostruct *uinfo; + struct tm t; + + ouser[0]='\0'; + ouser2[0]='\0'; + + snprintf(report_in,sizeof(report_in),"%s/download.log",tmp); + if(access(report_in, R_OK) != 0) + return; + + snprintf(report,sizeof(report),"%s/download.html",outdirname); + + if((fp_in=MY_FOPEN(report_in,"r"))==NULL) { + debuga(_("(download) Cannot open log file %s\n"),report_in); + exit(EXIT_FAILURE); + } + + if((fp_ou=MY_FOPEN(report,"w"))==NULL) { + debuga(_("(download) Cannot open log file %s\n"),report); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloads"),HTML_JS_NONE); + fputs("",fp_ou); + fprintf(fp_ou,_("Period: %s"),period.html); + fputs("\n",fp_ou); + fprintf(fp_ou,"%s\n",_("Downloads")); + close_html_header(fp_ou); + + fputs("
\n",fp_ou); + fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the downloaded files\n")); + exit(EXIT_FAILURE); + } + + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || + getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) { + debuga(_("There is a broken record or garbage in file %s\n"),report_in); + exit(EXIT_FAILURE); + } + if (getword_ptr(buf,&url,&gwarea,'\t')<0) { + debuga(_("There is a broken url in file %s\n"),report_in); + exit(EXIT_FAILURE); + } + if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; + computedate(year,month,day,&t); + strftime(data,sizeof(data),"%x",&t); + + uinfo=userinfo_find_from_id(user); + if (!uinfo) { + debuga(_("Unknown user ID %s in file %s\n"),user,report_in); + exit(EXIT_FAILURE); + } + new_user=false; + if(!z) { + strcpy(ouser,user); + strcpy(oip,ip); + z++; + new_user=true; + } else { + if(strcmp(ouser,user) != 0) { + strcpy(ouser,user); + new_user=true; + } + if(strcmp(oip,ip) != 0) { + strcpy(oip,ip); + new_user=true; + } + } + + if(DownloadReportLimit) { + if(strcmp(ouser2,uinfo->label) == 0) { + count++; + } else { + count=1; + strcpy(ouser2,uinfo->label); + } + if(count >= DownloadReportLimit) + continue; + } + + for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0; + + fputs("",fp_ou); + if (new_user) + fprintf(fp_ou,"",uinfo->label,ip); + else + fputs("",fp_ou); + fprintf(fp_ou,"\n",fp_ou); + } + fclose(fp_in); + longline_destroy(&line); + + fputs("
%s%s%s%s
%s%s%s-%s",data,hora); + if(BlockIt[0]!='\0') { + fprintf(fp_ou," ",ImageFile); + } + fputs("http://",fp_ou); + output_html_string(fp_ou,url,100); + fputs("
\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),report); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); + + unlink(report_in); + + return; } void free_download(void) { - if (DownloadSuffix) { - free(DownloadSuffix); - DownloadSuffix=NULL; - } - if (DownloadSuffixIndex) { - free(DownloadSuffixIndex); - DownloadSuffixIndex=NULL; - } - NDownloadSuffix=0; + if (DownloadSuffix) { + free(DownloadSuffix); + DownloadSuffix=NULL; + } + if (DownloadSuffixIndex) { + free(DownloadSuffixIndex); + DownloadSuffixIndex=NULL; + } + NDownloadSuffix=0; } void set_download_suffix(const char *list) { - char *str; - int i, j, k; - int cmp; - - free_download(); - - DownloadSuffix=strdup(list); - if (!DownloadSuffix) { - debuga(_("Download suffix list too long\n")); - exit(EXIT_FAILURE); - } - j = 1; - for (i=0 ; list[i] ; i++) - if (list[i] == ',') j++; - DownloadSuffixIndex=malloc(j*sizeof(char *)); - if (!DownloadSuffixIndex) { - debuga(_("Too many download suffixes\n")); - exit(EXIT_FAILURE); - } - - str = DownloadSuffix; - for (i=0 ; DownloadSuffix[i] ; i++) { - if (DownloadSuffix[i] == ',') { - DownloadSuffix[i] = '\0'; - if (*str) { - cmp = -1; - for (j=0 ; j0 ; j++); - if (cmp != 0) { - for (k=NDownloadSuffix ; k>j ; k--) - DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1]; - NDownloadSuffix++; - DownloadSuffixIndex[j]=str; - } - } - str=DownloadSuffix+i+1; - } - } - - if (*str) { - cmp = -1; - for (j=0 ; j0 ; j++); - if (cmp != 0) { - for (k=NDownloadSuffix ; k>j ; k--) - DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1]; - NDownloadSuffix++; - DownloadSuffixIndex[j]=str; - } - } + char *str; + int i, j, k; + int cmp; + + free_download(); + + DownloadSuffix=strdup(list); + if (!DownloadSuffix) { + debuga(_("Download suffix list too long\n")); + exit(EXIT_FAILURE); + } + j = 1; + for (i=0 ; list[i] ; i++) + if (list[i] == ',') j++; + DownloadSuffixIndex=malloc(j*sizeof(char *)); + if (!DownloadSuffixIndex) { + debuga(_("Too many download suffixes\n")); + exit(EXIT_FAILURE); + } + + str = DownloadSuffix; + for (i=0 ; DownloadSuffix[i] ; i++) { + if (DownloadSuffix[i] == ',') { + DownloadSuffix[i] = '\0'; + if (*str) { + cmp = -1; + for (j=0 ; j0 ; j++); + if (cmp != 0) { + for (k=NDownloadSuffix ; k>j ; k--) + DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1]; + NDownloadSuffix++; + DownloadSuffixIndex[j]=str; + } + } + str=DownloadSuffix+i+1; + } + } + + if (*str) { + cmp = -1; + for (j=0 ; j0 ; j++); + if (cmp != 0) { + for (k=NDownloadSuffix ; k>j ; k--) + DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1]; + NDownloadSuffix++; + DownloadSuffixIndex[j]=str; + } + } } bool is_download_suffix(const char *url) { - int urllen; - int i; - int down, up, center; - const char *suffix; - int cmp; - const int max_suffix=10; - - if (DownloadSuffix == NULL || NDownloadSuffix == 0) return(false); - - urllen=strlen(url)-1; - if (urllen<=0) return(false); - if (url[urllen] == '.') return(false); //reject a single trailing dot - for (i=0 ; i=urllen) return(false); // url is a hostname without any path or file to download - - for (i=0 ; i<=max_suffix && imax_suffix || i>=urllen) return(false); - - suffix=url+urllen-i+1; - down=0; - up=NDownloadSuffix-1; - while (down<=up) { - center=(down+up)/2; - cmp=strcasecmp(suffix,DownloadSuffixIndex[center]); - if (cmp == 0) return(true); - if (cmp < 0) - up = center-1; - else - down = center+1; - } - return(false); + int urllen; + int i; + int down, up, center; + const char *suffix; + int cmp; + const int max_suffix=10; + + if (DownloadSuffix == NULL || NDownloadSuffix == 0) return(false); + + urllen=strlen(url)-1; + if (urllen<=0) return(false); + if (url[urllen] == '.') return(false); //reject a single trailing dot + for (i=0 ; i=urllen) return(false); // url is a hostname without any path or file to download + + for (i=0 ; i<=max_suffix && imax_suffix || i>=urllen) return(false); + + suffix=url+urllen-i+1; + down=0; + up=NDownloadSuffix-1; + while (down<=up) { + center=(down+up)/2; + cmp=strcasecmp(suffix,DownloadSuffixIndex[center]); + if (cmp == 0) return(true); + if (cmp < 0) + up = center-1; + else + down = center+1; + } + return(false); } diff --git a/email.c b/email.c index 4a0ca94..3188b79 100644 --- a/email.c +++ b/email.c @@ -29,241 +29,241 @@ int geramail(const char *dirname, int debug, const char *outdir, const char *email, const char *TempDir) { - FILE *fp_in, *fp_top1, *fp_top2, *fp_top3; - long long int ttnbytes=0, ttnacc=0, tnacc=0; - long long int tnbytes=0, ttnelap=0, tnelap=0; - long long int nacc, nbytes, elap; - long long int avgacc, avgelap; - double perc=0.00; - double perc2=0.00; - int posicao=0; - char olduser[MAX_USER_LEN], csort[MAXLEN]; - char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN]; - char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN]; - char *buf; - char warea[MAXLEN]; - int totuser=0; - time_t t; - struct tm *local; - int cstatus; - struct getwordstruct gwarea; - struct generalitemstruct item; - longline line; - const struct userinfostruct *uinfo; - - snprintf(wger,sizeof(wger),"%s/sarg-general",dirname); - if((fp_in=fopen(wger,"r"))==NULL) { - debuga(_("(email) Cannot open file %s\n"),wger); - exit(EXIT_FAILURE); - } - - snprintf(top1,sizeof(top1),"%s/top",dirname); - if((fp_top1=fopen(top1,"w"))==NULL) { - debuga(_("(email) Cannot open file %s\n"),top1); - exit(EXIT_FAILURE); - } - - snprintf(top2,sizeof(top2),"%s/top.tmp",dirname); - if((fp_top2=fopen(top2,"w"))==NULL) { - debuga(_("(email) Cannot open file %s\n"),top2); - exit(EXIT_FAILURE); - } - - olduser[0]='\0'; - totuser=0; - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read file %s\n"),wger); - exit(EXIT_FAILURE); - } - - while((buf=longline_read(fp_in,line))!=NULL) { - ger_read(buf,&item,wger); - if(item.total) continue; - if(strcmp(olduser,item.user) != 0) - { - totuser++; - - if (olduser[0] != '\0') { + FILE *fp_in, *fp_top1, *fp_top2, *fp_top3; + long long int ttnbytes=0, ttnacc=0, tnacc=0; + long long int tnbytes=0, ttnelap=0, tnelap=0; + long long int nacc, nbytes, elap; + long long int avgacc, avgelap; + double perc=0.00; + double perc2=0.00; + int posicao=0; + char olduser[MAX_USER_LEN], csort[MAXLEN]; + char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAX_USER_LEN]; + char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN]; + char *buf; + char warea[MAXLEN]; + int totuser=0; + time_t t; + struct tm *local; + int cstatus; + struct getwordstruct gwarea; + struct generalitemstruct item; + longline line; + const struct userinfostruct *uinfo; + + snprintf(wger,sizeof(wger),"%s/sarg-general",dirname); + if((fp_in=fopen(wger,"r"))==NULL) { + debuga(_("(email) Cannot open file %s\n"),wger); + exit(EXIT_FAILURE); + } + + snprintf(top1,sizeof(top1),"%s/top",dirname); + if((fp_top1=fopen(top1,"w"))==NULL) { + debuga(_("(email) Cannot open file %s\n"),top1); + exit(EXIT_FAILURE); + } + + snprintf(top2,sizeof(top2),"%s/top.tmp",dirname); + if((fp_top2=fopen(top2,"w"))==NULL) { + debuga(_("(email) Cannot open file %s\n"),top2); + exit(EXIT_FAILURE); + } + + olduser[0]='\0'; + totuser=0; + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read file %s\n"),wger); + exit(EXIT_FAILURE); + } + + while((buf=longline_read(fp_in,line))!=NULL) { + ger_read(buf,&item,wger); + if(item.total) continue; + if(strcmp(olduser,item.user) != 0) + { + totuser++; + + if (olduser[0] != '\0') { #if defined(__FreeBSD__) - fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap); + fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap); #else - fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap); + fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap); #endif - ttnbytes+=tnbytes; - ttnacc+=tnacc; - ttnelap+=tnelap; - } - strcpy(olduser,item.user); - tnbytes=0; - tnacc=0; - tnelap=0; - } - - tnbytes+=item.nbytes; - tnacc+=item.nacc; - tnelap+=item.nelap; - } - fclose(fp_in); - longline_destroy(&line); - - if (olduser[0] != '\0') { + ttnbytes+=tnbytes; + ttnacc+=tnacc; + ttnelap+=tnelap; + } + strcpy(olduser,item.user); + tnbytes=0; + tnacc=0; + tnelap=0; + } + + tnbytes+=item.nbytes; + tnacc+=item.nacc; + tnelap+=item.nelap; + } + fclose(fp_in); + longline_destroy(&line); + + if (olduser[0] != '\0') { #if defined(__FreeBSD__) - fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap); + fprintf(fp_top2,"%s\t%qu\t%qu\t%qu\n",olduser,tnbytes,tnacc,tnelap); #else - fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap); + fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap); #endif - ttnbytes+=tnbytes; - ttnacc+=tnacc; - ttnelap+=tnelap; - } - - fclose(fp_top2); - - sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - unlink(top2); - - if((fp_top1=fopen(top1,"r"))==NULL) { - debuga(_("(email) Cannot open file %s\n"),top1); - exit(EXIT_FAILURE); - } - - snprintf(top3,sizeof(top3),"%s/report",dirname); - if((fp_top3=fopen(top3,"w"))==NULL) { - debuga(_("(email) Cannot open file %s\n"),top3); - exit(EXIT_FAILURE); - } - - strcpy(strip1,_("Squid User Access Report")); - strip_latin(strip1); - fprintf(fp_top3,"%s\n",strip1); - - strcpy(strip1,_("Decreasing Access (bytes)")); - strip_latin(strip1); - fprintf(fp_top3,"%s\n",strip1); - - strcpy(strip1,_("Period")); - strip_latin(strip1); - fprintf(fp_top3,"%s %s\n\n",strip1,period.text); - - strcpy(strip1,_("NUM")); - strip_latin(strip1); - strcpy(strip2,_("USERID")); - strip_latin(strip2); - strcpy(strip3,_("CONNECT")); - strip_latin(strip3); - strcpy(strip4,_("BYTES")); - strip_latin(strip4); - strcpy(strip5,_("ELAPSED TIME")); - strip_latin(strip5); - strcpy(strip6,_("MILLISEC")); - strip_latin(strip6); - strcpy(strip7,_("TIME")); - strip_latin(strip7); - - fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7); - - while(fgets(warea,sizeof(warea),fp_top1)) - { - fixendofline(warea); - getword_start(&gwarea,warea); - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - debuga(_("There is an invalid user ID in file %s\n"),top1); - exit(EXIT_FAILURE); - } - if (getword_atoll(&nbytes,&gwarea,'\t')<0) { - debuga(_("There is an invalid number of bytes in file %s\n"),top1); - exit(EXIT_FAILURE); - } - if (getword_atoll(&nacc,&gwarea,'\t')<0) { - debuga(_("There is an invalid number of access in file %s\n"),top1); - exit(EXIT_FAILURE); - } - if (getword_atoll(&elap,&gwarea,'\0')<0) { - debuga(_("There is an invalid elapsed time in file %s\n"),top1); - exit(EXIT_FAILURE); - } - - uinfo=userinfo_find_from_id(user); - if (!uinfo) { - debuga(_("Unknown user ID %s in file %s\n"),user,top1); - exit(EXIT_FAILURE); - } - - perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0; - perc2=(ttnelap) ? elap * 100. / ttnelap : 0; - - posicao++; + ttnbytes+=tnbytes; + ttnacc+=tnacc; + ttnelap+=tnelap; + } + + fclose(fp_top2); + + sprintf(csort,"sort -n -T \"%s\" -r -k 2,2 -o \"%s\" \"%s\"", TempDir, top1, top2); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + unlink(top2); + + if((fp_top1=fopen(top1,"r"))==NULL) { + debuga(_("(email) Cannot open file %s\n"),top1); + exit(EXIT_FAILURE); + } + + snprintf(top3,sizeof(top3),"%s/report",dirname); + if((fp_top3=fopen(top3,"w"))==NULL) { + debuga(_("(email) Cannot open file %s\n"),top3); + exit(EXIT_FAILURE); + } + + strcpy(strip1,_("Squid User Access Report")); + strip_latin(strip1); + fprintf(fp_top3,"%s\n",strip1); + + strcpy(strip1,_("Decreasing Access (bytes)")); + strip_latin(strip1); + fprintf(fp_top3,"%s\n",strip1); + + strcpy(strip1,_("Period")); + strip_latin(strip1); + fprintf(fp_top3,"%s %s\n\n",strip1,period.text); + + strcpy(strip1,_("NUM")); + strip_latin(strip1); + strcpy(strip2,_("USERID")); + strip_latin(strip2); + strcpy(strip3,_("CONNECT")); + strip_latin(strip3); + strcpy(strip4,_("BYTES")); + strip_latin(strip4); + strcpy(strip5,_("ELAPSED TIME")); + strip_latin(strip5); + strcpy(strip6,_("MILLISEC")); + strip_latin(strip6); + strcpy(strip7,_("TIME")); + strip_latin(strip7); + + fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7); + + while(fgets(warea,sizeof(warea),fp_top1)) + { + fixendofline(warea); + getword_start(&gwarea,warea); + if (getword(user,sizeof(user),&gwarea,'\t')<0) { + debuga(_("There is an invalid user ID in file %s\n"),top1); + exit(EXIT_FAILURE); + } + if (getword_atoll(&nbytes,&gwarea,'\t')<0) { + debuga(_("There is an invalid number of bytes in file %s\n"),top1); + exit(EXIT_FAILURE); + } + if (getword_atoll(&nacc,&gwarea,'\t')<0) { + debuga(_("There is an invalid number of access in file %s\n"),top1); + exit(EXIT_FAILURE); + } + if (getword_atoll(&elap,&gwarea,'\0')<0) { + debuga(_("There is an invalid elapsed time in file %s\n"),top1); + exit(EXIT_FAILURE); + } + + uinfo=userinfo_find_from_id(user); + if (!uinfo) { + debuga(_("Unknown user ID %s in file %s\n"),user,top1); + exit(EXIT_FAILURE); + } + + perc=(ttnbytes) ? nbytes * 100. / ttnbytes : 0; + perc2=(ttnelap) ? elap * 100. / ttnelap : 0; + + posicao++; #if defined(__FreeBSD__) - fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10qu %3.2lf%%\n",posicao,uinfo->label,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); + fprintf(fp_top3,"%7d %20s %8lld %15s %3.2lf%% %10s %10qu %3.2lf%%\n",posicao,uinfo->label,nacc,fixnum(nbytes,1),perc,buildtime(elap),elap,perc2); #else - fprintf(fp_top3,"%7d %20s %8"PRIu64" %15s %3.2lf%% %10s %10"PRIu64" %3.2lf%%\n",posicao,uinfo->label,(uint64_t)nacc,fixnum(nbytes,1),perc,buildtime(elap),(uint64_t)elap,perc2); + fprintf(fp_top3,"%7d %20s %8"PRIu64" %15s %3.2lf%% %10s %10"PRIu64" %3.2lf%%\n",posicao,uinfo->label,(uint64_t)nacc,fixnum(nbytes,1),perc,buildtime(elap),(uint64_t)elap,perc2); #endif - } + } - // output total - fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3); + // output total + fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3); #if defined(__FreeBSD__) - fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); + fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",_("TOTAL")," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap); #else - fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",_("TOTAL")," ",(uint64_t)ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),(uint64_t)ttnelap); + fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",_("TOTAL")," ",(uint64_t)ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),(uint64_t)ttnelap); #endif - // compute and write average - if (totuser>0) { - tnbytes=(totuser) ? ttnbytes / totuser : 0; - avgacc=ttnacc/totuser; - avgelap=ttnelap/totuser; - } else { - tnbytes=0; - avgacc=0; - avgelap=0; - } - - strcpy(strip1,_("AVERAGE")); - strip_latin(strip1); + // compute and write average + if (totuser>0) { + tnbytes=(totuser) ? ttnbytes / totuser : 0; + avgacc=ttnacc/totuser; + avgelap=ttnelap/totuser; + } else { + tnbytes=0; + avgacc=0; + avgelap=0; + } + + strcpy(strip1,_("AVERAGE")); + strip_latin(strip1); #if defined(__FreeBSD__) - fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),avgelap); + fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),avgelap); #else - fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",strip1," ",(uint64_t)avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),(uint64_t)avgelap); + fprintf(fp_top3,"%-7s %20s %8"PRIu64" %15s %8s %9s %10"PRIu64"\n",strip1," ",(uint64_t)avgacc,fixnum(tnbytes,1)," ",buildtime(avgelap),(uint64_t)avgelap); #endif - fclose(fp_top1); - unlink(top1); + fclose(fp_top1); + unlink(top1); - t = time(NULL); - local = localtime(&t); - fprintf(fp_top3, "\n%s\n", asctime(local)); + t = time(NULL); + local = localtime(&t); + fprintf(fp_top3, "\n%s\n", asctime(local)); - fclose(fp_top3); + fclose(fp_top3); - if(strcmp(email,"stdout") == 0) { - if((fp_top3=fopen(top3,"r"))==NULL) { - debuga(_("(email) Cannot open file %s\n"),top3); - exit(EXIT_FAILURE); - } + if(strcmp(email,"stdout") == 0) { + if((fp_top3=fopen(top3,"r"))==NULL) { + debuga(_("(email) Cannot open file %s\n"),top3); + exit(EXIT_FAILURE); + } - while(fgets(warea,sizeof(warea),fp_top3)!=NULL) - fputs(warea,stdout); - } else { - snprintf(warea,sizeof(warea),"%s -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,_("Report"),asctime(local),email,top3); - cstatus=system(warea); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("command: %s\n"),warea); - exit(EXIT_FAILURE); - } - } + while(fgets(warea,sizeof(warea),fp_top3)!=NULL) + fputs(warea,stdout); + } else { + snprintf(warea,sizeof(warea),"%s -s \"SARG %s, %s\" \"%s\" <\"%s\"",MailUtility,_("Report"),asctime(local),email,top3); + cstatus=system(warea); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("command: %s\n"),warea); + exit(EXIT_FAILURE); + } + } - //unlinkdir(TempDir,0); + //unlinkdir(TempDir,0); - return (0); + return (0); } diff --git a/exclude.c b/exclude.c index 915dc15..0ccfb07 100644 --- a/exclude.c +++ b/exclude.c @@ -29,18 +29,18 @@ struct hostip4struct { - //! The IP address. - unsigned long int address; - //! The mask to match the address of the URL. - unsigned long int mask; + //! The IP address. + unsigned long int address; + //! The mask to match the address of the URL. + unsigned long int mask; }; struct hostnamestruct { - //! The URL to match without any leading wildcard. - char *url; - //! The number of dots in the url if a wildcard is present or -1 if the address is complete (no wildcard) - int ndots; + //! The URL to match without any leading wildcard. + char *url; + //! The number of dots in the url if a wildcard is present or -1 if the address is complete (no wildcard) + int ndots; }; static struct hostip4struct *exclude_ip4=NULL; @@ -54,355 +54,354 @@ static char *excludeuser=NULL; static void store_exclude_ip4(unsigned short int *addr,int mask) { - int i; - - if (num_exclude_ip4>=ip4allocated) { - struct hostip4struct *temp; - - ip4allocated+=5; - temp=realloc(exclude_ip4,ip4allocated*sizeof(*temp)); - if (temp==NULL) { - debuga(_("Not enough memory to store the exlcluded IP addresses\n")); - exit(EXIT_FAILURE); - } - exclude_ip4=temp; - } - exclude_ip4[num_exclude_ip4].address=0UL; - for (i=0 ; i<4 ; i++) - exclude_ip4[num_exclude_ip4].address=(exclude_ip4[num_exclude_ip4].address<<8) | (unsigned char)(addr[i] & 0xFFU); - exclude_ip4[num_exclude_ip4].mask=(0xFFFFFFFFUL << (32-mask)); - num_exclude_ip4++; + int i; + + if (num_exclude_ip4>=ip4allocated) { + struct hostip4struct *temp; + + ip4allocated+=5; + temp=realloc(exclude_ip4,ip4allocated*sizeof(*temp)); + if (temp==NULL) { + debuga(_("Not enough memory to store the exlcluded IP addresses\n")); + exit(EXIT_FAILURE); + } + exclude_ip4=temp; + } + exclude_ip4[num_exclude_ip4].address=0UL; + for (i=0 ; i<4 ; i++) + exclude_ip4[num_exclude_ip4].address=(exclude_ip4[num_exclude_ip4].address<<8) | (unsigned char)(addr[i] & 0xFFU); + exclude_ip4[num_exclude_ip4].mask=(0xFFFFFFFFUL << (32-mask)); + num_exclude_ip4++; } static void store_exclude_url(char *url,int length) { - int start; - int i; - int ndots, firstdot; - struct hostnamestruct *item; - - start=0; - ndots=-1; - firstdot=0; - for (i=0 ; i=0) - ndots++; - } - if (start>=length || firstdot) return; - if (start>0) { - url+=start; - length-=start; - } - - if (num_exclude_name>=nameallocated) { - struct hostnamestruct *temp; - - nameallocated+=5; - temp=realloc(exclude_name,nameallocated*sizeof(*temp)); - if (temp==NULL) { - debuga(_("Not enough memory to store the excluded URLs\n")); - exit(EXIT_FAILURE); - } - exclude_name=temp; - } - - item=exclude_name+num_exclude_name; - num_exclude_name++; - item->url=malloc(length+1); - if (!item->url) { - debuga(_("Not enough memory to store the excluded URLs\n")); - exit(EXIT_FAILURE); - } - strncpy(item->url,url,length); - item->url[length]='\0'; - item->ndots=(ndots>0) ? ndots : -1; + int start; + int i; + int ndots, firstdot; + struct hostnamestruct *item; + + start=0; + ndots=-1; + firstdot=0; + for (i=0 ; i=0) + ndots++; + } + if (start>=length || firstdot) return; + if (start>0) { + url+=start; + length-=start; + } + + if (num_exclude_name>=nameallocated) { + struct hostnamestruct *temp; + + nameallocated+=5; + temp=realloc(exclude_name,nameallocated*sizeof(*temp)); + if (temp==NULL) { + debuga(_("Not enough memory to store the excluded URLs\n")); + exit(EXIT_FAILURE); + } + exclude_name=temp; + } + + item=exclude_name+num_exclude_name; + num_exclude_name++; + item->url=malloc(length+1); + if (!item->url) { + debuga(_("Not enough memory to store the excluded URLs\n")); + exit(EXIT_FAILURE); + } + strncpy(item->url,url,length); + item->url[length]='\0'; + item->ndots=(ndots>0) ? ndots : -1; } void gethexclude(const char *hexfile, int debug) { - FILE *fp_ex; - char buf[255]; - int i; - int ip_size; - unsigned int value4, value6; - unsigned short int addr[8]; - int addr_len; - int mask, max_mask; - - if(access(hexfile, R_OK) != 0) { - debuga(_("Cannot open exclude_hosts file: %s - %s\n"),hexfile,strerror(errno)); - exit(EXIT_FAILURE); - } - if(debug) - debuga(_("Loading exclude host file from: %s\n"),hexfile); - - if ((fp_ex = fopen(hexfile, "r")) == NULL) { - debuga(_("(gethexclude) Cannot open file %s - %s\n"),hexfile,strerror(errno)); - exit(EXIT_FAILURE); - } - - while(fgets(buf,sizeof(buf),fp_ex)!=NULL){ - if(buf[0]=='#') - continue; - fixendofline(buf); - ip_size=0x60 | 0x04; - value4=0U; - value6=0U; - addr_len=0; - for (i=0 ; (unsigned char)buf[i]>' ' && buf[i]!='/' ; i++) { - if (ip_size & 0x04) { - if (isdigit(buf[i])) { - value4=value4*10+(buf[i]-'0'); - if (value4>0xFFU) ip_size&=~0x04; - } else if (buf[i]=='.' && addr_len<4) { - addr[addr_len++]=(unsigned short)(value4 & 0xFFU); - value4=0U; - } else { - ip_size&=~0x04; - } - } - if (ip_size & 0x60) { - if (isdigit(buf[i])) { - value6=(value6<<4)+(buf[i]-'0'); - if (value6>0xFFFFU) ip_size&=~0x60; - } else if (toupper(buf[i])>='A' && toupper(buf[i])<='F') { - value6=(value6<<4)+(toupper(buf[i])-'A'+10); - if (value6>0xFFFFU) ip_size&=~0x60; - } else if (buf[i]==':' && addr_len<8) { - addr[addr_len++]=(unsigned short)(value6 & 0xFFFFU); - value6=0U; - } else { - ip_size&=~0x60; - } - } - } - if (i==0) continue; - if (ip_size & 0x04) { - if (addr_len!=3) - ip_size&=~0x04; - else - addr[addr_len++]=(unsigned short)(value4 & 0xFFU); - } - if (ip_size & 0x60) { - if (addr_len>=8) - ip_size&=~0x60; - else - addr[addr_len++]=(unsigned short)(value6 & 0xFFFFU); - } - if (ip_size) { - max_mask=(ip_size & 0x04) ? 4*8 : 8*16; - if (buf[i]=='/') { - mask=atoi(buf+i+1); - if (mask<0 || mask>max_mask) mask=max_mask; - } else - mask=max_mask; - if (ip_size & 0x04) - store_exclude_ip4(addr,mask); - else { - debuga(_("IPv6 addresses are not supported (found in %s)\n"),hexfile); - exit(EXIT_FAILURE); - } - } else { - store_exclude_url(buf,i); - } - } - - fclose(fp_ex); - return; + FILE *fp_ex; + char buf[255]; + int i; + int ip_size; + unsigned int value4, value6; + unsigned short int addr[8]; + int addr_len; + int mask, max_mask; + + if(access(hexfile, R_OK) != 0) { + debuga(_("Cannot open exclude_hosts file: %s - %s\n"),hexfile,strerror(errno)); + exit(EXIT_FAILURE); + } + if(debug) + debuga(_("Loading exclude host file from: %s\n"),hexfile); + + if ((fp_ex = fopen(hexfile, "r")) == NULL) { + debuga(_("(gethexclude) Cannot open file %s - %s\n"),hexfile,strerror(errno)); + exit(EXIT_FAILURE); + } + + while(fgets(buf,sizeof(buf),fp_ex)!=NULL){ + if(buf[0]=='#') + continue; + fixendofline(buf); + ip_size=0x60 | 0x04; + value4=0U; + value6=0U; + addr_len=0; + for (i=0 ; (unsigned char)buf[i]>' ' && buf[i]!='/' ; i++) { + if (ip_size & 0x04) { + if (isdigit(buf[i])) { + value4=value4*10+(buf[i]-'0'); + if (value4>0xFFU) ip_size&=~0x04; + } else if (buf[i]=='.' && addr_len<4) { + addr[addr_len++]=(unsigned short)(value4 & 0xFFU); + value4=0U; + } else { + ip_size&=~0x04; + } + } + if (ip_size & 0x60) { + if (isdigit(buf[i])) { + value6=(value6<<4)+(buf[i]-'0'); + if (value6>0xFFFFU) ip_size&=~0x60; + } else if (toupper(buf[i])>='A' && toupper(buf[i])<='F') { + value6=(value6<<4)+(toupper(buf[i])-'A'+10); + if (value6>0xFFFFU) ip_size&=~0x60; + } else if (buf[i]==':' && addr_len<8) { + addr[addr_len++]=(unsigned short)(value6 & 0xFFFFU); + value6=0U; + } else { + ip_size&=~0x60; + } + } + } + if (i==0) continue; + if (ip_size & 0x04) { + if (addr_len!=3) + ip_size&=~0x04; + else + addr[addr_len++]=(unsigned short)(value4 & 0xFFU); + } + if (ip_size & 0x60) { + if (addr_len>=8) + ip_size&=~0x60; + else + addr[addr_len++]=(unsigned short)(value6 & 0xFFFFU); + } + if (ip_size) { + max_mask=(ip_size & 0x04) ? 4*8 : 8*16; + if (buf[i]=='/') { + mask=atoi(buf+i+1); + if (mask<0 || mask>max_mask) mask=max_mask; + } else + mask=max_mask; + if (ip_size & 0x04) + store_exclude_ip4(addr,mask); + else { + debuga(_("IPv6 addresses are not supported (found in %s)\n"),hexfile); + exit(EXIT_FAILURE); + } + } else { + store_exclude_url(buf,i); + } + } + + fclose(fp_ex); + return; } int vhexclude(const char *url) { - int i, j; - int length; - int ip_size; - unsigned int value4, value6; - unsigned long int addr4; - unsigned short int addr6[8]; - int addr_len; - int dotpos[10]; - int ndots; - - ip_size=0x60 | 0x04; - addr4=0UL; - value4=0U; - value6=0U; - addr_len=0; - for (i=0 ; (unsigned char)url[i]>' ' && url[i]!='/' && url[i]!='?'&& ((ip_size & 0x60)!=0 || url[i]!=':') && ip_size ; i++) { - if (ip_size & 0x04) { - if (isdigit(url[i])) { - value4=value4*10+(url[i]-'0'); - if (value4>0xFFU) ip_size&=~0x04; - } else if (url[i]=='.' && addr_len<4) { - addr_len++; - addr4=(addr4<<8) | (unsigned long int)(value4 & 0xFFU); - value4=0U; - } else { - ip_size&=~0x04; - } - } - if (ip_size & 0x60) { - if (isdigit(url[i])) { - value6=(value6<<4)+(url[i]-'0'); - if (value6>0xFFFFU) ip_size&=~0x60; - } else if (toupper(url[i])>='A' && toupper(url[i])<='F') { - value6=(value6<<4)+(toupper(url[i])-'A'+10); - if (value6>0xFFFFU) ip_size&=~0x60; - } else if (url[i]==':' && addr_len<8) { - addr6[addr_len++]=(unsigned short)(value6 & 0xFFFFU); - value6=0U; - } else { - ip_size&=~0x60; - } - } - } - if ((ip_size & 0x04) && addr_len==3) { - if (exclude_ip4 == NULL) return(1); - addr4=(addr4<<8) | (unsigned long int)(value4 & 0xFFU); - for (i=0 ; i' ' && url[length]!=':' && url[length]!='/' && url[length]!='?' ; length++) - if (url[length]=='.') { - /* - We store the position of each dots of the URL to match it against any - wildcard in the excluded list. The size of dotpos is big enough for the most - ambitious URL but we have a safety mechanism that shift the positions should there be too - many dots in the URL. - */ - if (ndots0) { - for (i=0 ; i0) { - const char *wurl=url; - int len=length; - if (exclude_name[i].ndots<=ndots) { - wurl+=dotpos[ndots-exclude_name[i].ndots]; - len-=dotpos[ndots-exclude_name[i].ndots]; - } - if (strncmp(exclude_name[i].url,wurl,len)==0 && exclude_name[i].url[len]=='\0') return(0); - } else { - if (strncmp(exclude_name[i].url,url,length)==0 && exclude_name[i].url[length]=='\0') return(0); - } - } - } - } - - return(1); + int i, j; + int length; + int ip_size; + unsigned int value4, value6; + unsigned long int addr4; + unsigned short int addr6[8]; + int addr_len; + int dotpos[10]; + int ndots; + + ip_size=0x60 | 0x04; + addr4=0UL; + value4=0U; + value6=0U; + addr_len=0; + for (i=0 ; (unsigned char)url[i]>' ' && url[i]!='/' && url[i]!='?'&& ((ip_size & 0x60)!=0 || url[i]!=':') && ip_size ; i++) { + if (ip_size & 0x04) { + if (isdigit(url[i])) { + value4=value4*10+(url[i]-'0'); + if (value4>0xFFU) ip_size&=~0x04; + } else if (url[i]=='.' && addr_len<4) { + addr_len++; + addr4=(addr4<<8) | (unsigned long int)(value4 & 0xFFU); + value4=0U; + } else { + ip_size&=~0x04; + } + } + if (ip_size & 0x60) { + if (isdigit(url[i])) { + value6=(value6<<4)+(url[i]-'0'); + if (value6>0xFFFFU) ip_size&=~0x60; + } else if (toupper(url[i])>='A' && toupper(url[i])<='F') { + value6=(value6<<4)+(toupper(url[i])-'A'+10); + if (value6>0xFFFFU) ip_size&=~0x60; + } else if (url[i]==':' && addr_len<8) { + addr6[addr_len++]=(unsigned short)(value6 & 0xFFFFU); + value6=0U; + } else { + ip_size&=~0x60; + } + } + } + if ((ip_size & 0x04) && addr_len==3) { + if (exclude_ip4 == NULL) return(1); + addr4=(addr4<<8) | (unsigned long int)(value4 & 0xFFU); + for (i=0 ; i' ' && url[length]!=':' && url[length]!='/' && url[length]!='?' ; length++) + if (url[length]=='.') { + /* + We store the position of each dots of the URL to match it against any + wildcard in the excluded list. The size of dotpos is big enough for the most + ambitious URL but we have a safety mechanism that shift the positions should there be too + many dots in the URL. + */ + if (ndots0) { + for (i=0 ; i0) { + const char *wurl=url; + int len=length; + if (exclude_name[i].ndots<=ndots) { + wurl+=dotpos[ndots-exclude_name[i].ndots]; + len-=dotpos[ndots-exclude_name[i].ndots]; + } + if (strncmp(exclude_name[i].url,wurl,len)==0 && exclude_name[i].url[len]=='\0') return(0); + } else { + if (strncmp(exclude_name[i].url,url,length)==0 && exclude_name[i].url[length]=='\0') return(0); + } + } + } + } + + return(1); } void getuexclude(const char *uexfile, int debug) { - - FILE *fp_ex; - char buf[255]; - long int nreg=0; - - if(debug) - debuga(_("Loading exclude file from: %s\n"),uexfile); - - if ((fp_ex = fopen(uexfile, "r")) == NULL) { - debuga(_("(gethexclude) Cannot open file %s - %s\n"),uexfile,strerror(errno)); - exit(EXIT_FAILURE); - } - - if (fseek(fp_ex, 0, SEEK_END)==-1) { - debuga(_("Failed to move till the end of the excluded users file %s: %s\n"),uexfile,strerror(errno)); - exit(EXIT_FAILURE); - } - nreg = ftell(fp_ex); - if (nreg<0) { - debuga(_("Cannot get the size of file %s\n"),uexfile); - exit(EXIT_FAILURE); - } - nreg += 11; - if (fseek(fp_ex, 0, SEEK_SET)==-1) { - debuga(_("Failed to rewind the excluded users file %s: %s\n"),uexfile,strerror(errno)); - exit(EXIT_FAILURE); - } - - if((excludeuser=(char *) malloc(nreg))==NULL){ - debuga(_("malloc error (%ld bytes required)\n"),nreg); - exit(EXIT_FAILURE); - } - - bzero(excludeuser,nreg); - - while(fgets(buf,sizeof(buf),fp_ex)!=NULL){ - if(strchr(buf,'#') != NULL) - continue; - fixendofline(buf); - strcat(excludeuser,buf); - strcat(excludeuser," "); - } - - strcat(excludeuser,"*END* "); - - fclose(fp_ex); - - return; + FILE *fp_ex; + char buf[255]; + long int nreg=0; + + if(debug) + debuga(_("Loading exclude file from: %s\n"),uexfile); + + if ((fp_ex = fopen(uexfile, "r")) == NULL) { + debuga(_("(gethexclude) Cannot open file %s - %s\n"),uexfile,strerror(errno)); + exit(EXIT_FAILURE); + } + + if (fseek(fp_ex, 0, SEEK_END)==-1) { + debuga(_("Failed to move till the end of the excluded users file %s: %s\n"),uexfile,strerror(errno)); + exit(EXIT_FAILURE); + } + nreg = ftell(fp_ex); + if (nreg<0) { + debuga(_("Cannot get the size of file %s\n"),uexfile); + exit(EXIT_FAILURE); + } + nreg += 11; + if (fseek(fp_ex, 0, SEEK_SET)==-1) { + debuga(_("Failed to rewind the excluded users file %s: %s\n"),uexfile,strerror(errno)); + exit(EXIT_FAILURE); + } + + if((excludeuser=(char *) malloc(nreg))==NULL){ + debuga(_("malloc error (%ld bytes required)\n"),nreg); + exit(EXIT_FAILURE); + } + + bzero(excludeuser,nreg); + + while(fgets(buf,sizeof(buf),fp_ex)!=NULL){ + if(strchr(buf,'#') != NULL) + continue; + fixendofline(buf); + strcat(excludeuser,buf); + strcat(excludeuser," "); + } + + strcat(excludeuser,"*END* "); + + fclose(fp_ex); + + return; } int vuexclude(const char *user) { - const char *wuser; - int len; - - if (excludeuser) { - len=strlen(user); - wuser=excludeuser; - while ((wuser=strstr(wuser,user))!=NULL) { - if (wuser[len]==' ') return(0); - wuser+=len; - } - } - - return(1); + const char *wuser; + int len; + + if (excludeuser) { + len=strlen(user); + wuser=excludeuser; + while ((wuser=strstr(wuser,user))!=NULL) { + if (wuser[len]==' ') return(0); + wuser+=len; + } + } + + return(1); } bool is_indexonly(void) { - if (excludeuser==NULL) return(false); - return(strstr(excludeuser,"indexonly") != NULL); + if (excludeuser==NULL) return(false); + return(strstr(excludeuser,"indexonly") != NULL); } void free_exclude(void) { - int i; - - if (exclude_ip4) { - free(exclude_ip4); - exclude_ip4=NULL; - } - - if (exclude_name) { - for (i=0 ; i' ') return(0); - return(1); + plen=strlen(param); + if (strncmp(buf,param,plen) != 0) return(0); + buf+=plen; + if ((unsigned char)*buf>' ') return(0); + return(1); } static int getparam_string(const char *param,char *buf,char *value,int value_size) { - int plen; - - plen=strlen(param); - if (strncmp(buf,param,plen) != 0) return(0); - buf+=plen; - if ((unsigned char)*buf>' ') return(0); - while (*buf && (unsigned char)*buf<=' ') buf++; - - if (strlen(buf)>=value_size) { - debuga(_("The string value of parameter \"%s\" is too long\n"),param); - exit(EXIT_FAILURE); - } - strcpy(value,buf); - fixnone(value); - return(1); + int plen; + + plen=strlen(param); + if (strncmp(buf,param,plen) != 0) return(0); + buf+=plen; + if ((unsigned char)*buf>' ') return(0); + while (*buf && (unsigned char)*buf<=' ') buf++; + + if (strlen(buf)>=value_size) { + debuga(_("The string value of parameter \"%s\" is too long\n"),param); + exit(EXIT_FAILURE); + } + strcpy(value,buf); + fixnone(value); + return(1); } static int getparam_quoted(const char *param,char *buf,char *value,int value_size) { - int plen; - int i; - - plen=strlen(param); - if (strncmp(buf,param,plen) != 0) return(0); - buf+=plen; - if ((unsigned char)*buf>' ') return(0); - while (*buf && (unsigned char)*buf<=' ') buf++; - - if (*buf != '\"') { - debuga(_("Missing double quote after parameter \"%s\"\n"),param); - exit(EXIT_FAILURE); - } - buf++; - - value_size--; - for (i=0 ; i' ') return(0); + while (*buf && (unsigned char)*buf<=' ') buf++; + + if (*buf != '\"') { + debuga(_("Missing double quote after parameter \"%s\"\n"),param); + exit(EXIT_FAILURE); + } + buf++; + + value_size--; + for (i=0 ; i' ') return(0); - while (*buf && (unsigned char)*buf<=' ') buf++; - - for (i=0 ; i' ' ; i++) - word1[i]=*buf++; - if (i>=word1_size) { - debuga(_("The first word of parameter \"%s\" is more than %d bytes long\n"),param,word1_size-1); - exit(EXIT_FAILURE); - } - if (*buf!=' ') { - debuga(_("Missing second word for parameter \"%s\"\n"),param); - exit(EXIT_FAILURE); - } - word1[i]=0; - - while (*buf && (unsigned char)*buf<=' ') buf++; - - for (i=0 ; i' ' ; i++) - word2[i]=*buf++; - if (i>=word2_size) { - debuga(_("The second word of parameter \"%s\" is more than %d bytes long\n"),param,word2_size-1); - exit(EXIT_FAILURE); - } - word2[i]=0; - - fixnone(word1); - fixnone(word2); - return(1); + int plen; + int i; + + plen=strlen(param); + if (strncmp(buf,param,plen) != 0) return(0); + buf+=plen; + if ((unsigned char)*buf>' ') return(0); + while (*buf && (unsigned char)*buf<=' ') buf++; + + for (i=0 ; i' ' ; i++) + word1[i]=*buf++; + if (i>=word1_size) { + debuga(_("The first word of parameter \"%s\" is more than %d bytes long\n"),param,word1_size-1); + exit(EXIT_FAILURE); + } + if (*buf!=' ') { + debuga(_("Missing second word for parameter \"%s\"\n"),param); + exit(EXIT_FAILURE); + } + word1[i]=0; + + while (*buf && (unsigned char)*buf<=' ') buf++; + + for (i=0 ; i' ' ; i++) + word2[i]=*buf++; + if (i>=word2_size) { + debuga(_("The second word of parameter \"%s\" is more than %d bytes long\n"),param,word2_size-1); + exit(EXIT_FAILURE); + } + word2[i]=0; + + fixnone(word1); + fixnone(word2); + return(1); } static int getparam_int(const char *param,char *buf,int *value) { - int plen; - int next; - - plen=strlen(param); - if (strncmp(buf,param,plen) != 0) return(0); - buf+=plen; - if ((unsigned char)*buf>' ') return(0); - while (*buf && (unsigned char)*buf<=' ') buf++; - - next=0; - if (sscanf(buf,"%d%n",value,&next) != 1 || (unsigned char)buf[next] > ' ') { - debuga(_("The integer value of parameter \"%s\" is invalid\n"),param); - exit(EXIT_FAILURE); - } - return(1); + int plen; + int next; + + plen=strlen(param); + if (strncmp(buf,param,plen) != 0) return(0); + buf+=plen; + if ((unsigned char)*buf>' ') return(0); + while (*buf && (unsigned char)*buf<=' ') buf++; + + next=0; + if (sscanf(buf,"%d%n",value,&next) != 1 || (unsigned char)buf[next] > ' ') { + debuga(_("The integer value of parameter \"%s\" is invalid\n"),param); + exit(EXIT_FAILURE); + } + return(1); } static int getparam_bool(const char *param,char *buf,bool *value) { - int plen; - int i; - const char *bool_str="yes,true,on,1"; - - plen=strlen(param); - if (strncmp(buf,param,plen) != 0) return(0); - buf+=plen; - if ((unsigned char)*buf>' ') return(0); - while (*buf && (unsigned char)*buf<=' ') buf++; - - *value=false; - for ( ; *bool_str ; bool_str+=i) { - for (i=0 ; bool_str[i] && bool_str[i]!=',' ; i++); - if (strncasecmp(bool_str,buf,i)==0) { - *value=true; - break; - } - if (bool_str[i]==',') i++; - } - return(1); + int plen; + int i; + const char *bool_str="yes,true,on,1"; + + plen=strlen(param); + if (strncmp(buf,param,plen) != 0) return(0); + buf+=plen; + if ((unsigned char)*buf>' ') return(0); + while (*buf && (unsigned char)*buf<=' ') buf++; + + *value=false; + for ( ; *bool_str ; bool_str+=i) { + for (i=0 ; bool_str[i] && bool_str[i]!=',' ; i++); + if (strncasecmp(bool_str,buf,i)==0) { + *value=true; + break; + } + if (bool_str[i]==',') i++; + } + return(1); } static int getparam_list(const char *param,struct param_list *options,int noptions,char *buf,unsigned long int *value) { - int plen; - char *str; - int i; - - plen=strlen(param); - if (strncmp(buf,param,plen) != 0) return(0); - buf+=plen; - if ((unsigned char)*buf>' ') return(0); - while (*buf && (unsigned char)*buf<=' ') buf++; - - *value=0UL; - while (*buf) { - str=buf; - while (*str && (unsigned char)*str>' ' && *str!=';') str++; - if (*str) { - *str++='\0'; - while (*str && ((unsigned char)*str<=' ' || *str==';')) str++; - } - for (i=0 ; i=noptions) { - debuga(_("Unknown value \"%s\" for parameter \"%s\"\n"),buf,param); - exit(EXIT_FAILURE); - } - if ((*value & options[i].exclude)!=0) { - debuga(_("Value \"%s\" conflicts with other selected values for parameter \"%s\"\n"),buf,param); - exit(EXIT_FAILURE); - } - *value|=options[i].value; - buf=str; - } - return(1); + int plen; + char *str; + int i; + + plen=strlen(param); + if (strncmp(buf,param,plen) != 0) return(0); + buf+=plen; + if ((unsigned char)*buf>' ') return(0); + while (*buf && (unsigned char)*buf<=' ') buf++; + + *value=0UL; + while (*buf) { + str=buf; + while (*str && (unsigned char)*str>' ' && *str!=';') str++; + if (*str) { + *str++='\0'; + while (*str && ((unsigned char)*str<=' ' || *str==';')) str++; + } + for (i=0 ; i=noptions) { + debuga(_("Unknown value \"%s\" for parameter \"%s\"\n"),buf,param); + exit(EXIT_FAILURE); + } + if ((*value & options[i].exclude)!=0) { + debuga(_("Value \"%s\" conflicts with other selected values for parameter \"%s\"\n"),buf,param); + exit(EXIT_FAILURE); + } + *value|=options[i].value; + buf=str; + } + return(1); } static int getparam_sort(const char *param,struct sort_list *options,int noptions,char *buf,unsigned long int *value) { - int plen; - char *str, *order; - int i; - - plen=strlen(param); - if (strncmp(buf,param,plen) != 0) return(0); - buf+=plen; - if ((unsigned char)*buf>' ') return(0); - while (*buf && (unsigned char)*buf<=' ') buf++; - - str=buf; - order=NULL; - while (*str && (unsigned char)*str>' ') str++; - if (*str) { - *str++='\0'; - while (*str && (unsigned char)*str<=' ') str++; - order=str; - } - for (i=0 ; i=noptions) { - debuga(_("Unknown sort criterion \"%s\" for parameter \"%s\"\n"),buf,param); - exit(EXIT_FAILURE); - } - *value=options[i].value; - - if (order) { - str=order; - while (*str && (unsigned char)*str>' ') str++; - if (*str) { - *str++='\0'; - while (*str && (unsigned char)*str<=' ') str++; - } - if (strcasecmp(order,"reverse")==0 || strcasecmp(order,"D")==0) { - *value|=SORT_REVERSE; - } else if (strcasecmp(order,"normal")!=0 && strcasecmp(order,"A")!=0) { - debuga(_("Unknown sort order \"%s\" for parameter \"%s\"\n"),order,param); - exit(EXIT_FAILURE); - } - } - - buf=str; - return(1); + int plen; + char *str, *order; + int i; + + plen=strlen(param); + if (strncmp(buf,param,plen) != 0) return(0); + buf+=plen; + if ((unsigned char)*buf>' ') return(0); + while (*buf && (unsigned char)*buf<=' ') buf++; + + str=buf; + order=NULL; + while (*str && (unsigned char)*str>' ') str++; + if (*str) { + *str++='\0'; + while (*str && (unsigned char)*str<=' ') str++; + order=str; + } + for (i=0 ; i=noptions) { + debuga(_("Unknown sort criterion \"%s\" for parameter \"%s\"\n"),buf,param); + exit(EXIT_FAILURE); + } + *value=options[i].value; + + if (order) { + str=order; + while (*str && (unsigned char)*str>' ') str++; + if (*str) { + *str++='\0'; + while (*str && (unsigned char)*str<=' ') str++; + } + if (strcasecmp(order,"reverse")==0 || strcasecmp(order,"D")==0) { + *value|=SORT_REVERSE; + } else if (strcasecmp(order,"normal")!=0 && strcasecmp(order,"A")!=0) { + debuga(_("Unknown sort order \"%s\" for parameter \"%s\"\n"),order,param); + exit(EXIT_FAILURE); + } + } + + buf=str; + return(1); } static void parmtest(char *buf) { - char wbuf[2048]; - struct getwordstruct gwarea; + char wbuf[2048]; + struct getwordstruct gwarea; - while (*buf && (unsigned char)*buf<=' ') buf++; + while (*buf && (unsigned char)*buf<=' ') buf++; - if(*buf == '#' || *buf == '\0') - return; + if(*buf == '#' || *buf == '\0') + return; - if(debugz) - printf(_("SARG: TAG: %s\n"),buf); + if(debugz) + printf(_("SARG: TAG: %s\n"),buf); - if (getparam_string("background_color",buf,BgColor,sizeof(BgColor))>0) return; + if (getparam_string("background_color",buf,BgColor,sizeof(BgColor))>0) return; - if (getparam_string("text_color",buf,TxColor,sizeof(TxColor))>0) return; + if (getparam_string("text_color",buf,TxColor,sizeof(TxColor))>0) return; - if (getparam_string("text_bgcolor",buf,TxBgColor,sizeof(TxBgColor))>0) return; + if (getparam_string("text_bgcolor",buf,TxBgColor,sizeof(TxBgColor))>0) return; - if (getparam_string("title_color",buf,TiColor,sizeof(TiColor))>0) return; + if (getparam_string("title_color",buf,TiColor,sizeof(TiColor))>0) return; - if (getparam_string("logo_image",buf,LogoImage,sizeof(LogoImage))>0) return; + if (getparam_string("logo_image",buf,LogoImage,sizeof(LogoImage))>0) return; - if (getparam_quoted("logo_text",buf,LogoText,sizeof(LogoText))>0) return; + if (getparam_quoted("logo_text",buf,LogoText,sizeof(LogoText))>0) return; - if (getparam_string("logo_text_color",buf,LogoTextColor,sizeof(LogoTextColor))>0) return; + if (getparam_string("logo_text_color",buf,LogoTextColor,sizeof(LogoTextColor))>0) return; - if (getparam_string("background_image",buf,BgImage,sizeof(BgImage))>0) return; + if (getparam_string("background_image",buf,BgImage,sizeof(BgImage))>0) return; - if (getparam_bool("show_sarg_info",buf,&ShowSargInfo)>0) return; + if (getparam_bool("show_sarg_info",buf,&ShowSargInfo)>0) return; - if (getparam_bool("show_sarg_logo",buf,&ShowSargLogo)>0) return; + if (getparam_bool("show_sarg_logo",buf,&ShowSargLogo)>0) return; - if (getparam_string("font_face",buf,FontFace,sizeof(FontFace))>0) return; + if (getparam_string("font_face",buf,FontFace,sizeof(FontFace))>0) return; - if (getparam_string("header_color",buf,HeaderColor,sizeof(HeaderColor))>0) return; + if (getparam_string("header_color",buf,HeaderColor,sizeof(HeaderColor))>0) return; - if (getparam_string("header_bgcolor",buf,HeaderBgColor,sizeof(HeaderBgColor))>0) return; + if (getparam_string("header_bgcolor",buf,HeaderBgColor,sizeof(HeaderBgColor))>0) return; - if (getparam_string("font_size",buf,FontSize,sizeof(FontSize))>0) return; + if (getparam_string("font_size",buf,FontSize,sizeof(FontSize))>0) return; - if (getparam_string("header_font_size",buf,HeaderFontSize,sizeof(HeaderFontSize))>0) return; + if (getparam_string("header_font_size",buf,HeaderFontSize,sizeof(HeaderFontSize))>0) return; - if (getparam_string("title_font_size",buf,TitleFontSize,sizeof(TitleFontSize))>0) return; + if (getparam_string("title_font_size",buf,TitleFontSize,sizeof(TitleFontSize))>0) return; - if (getparam_2words("image_size",buf,Width,sizeof(Width),Height,sizeof(Height))>0) return; + if (getparam_2words("image_size",buf,Width,sizeof(Width),Height,sizeof(Height))>0) return; - if (getparam_quoted("title",buf,Title,sizeof(Title))>0) return; + if (getparam_quoted("title",buf,Title,sizeof(Title))>0) return; - if (getparam_bool("resolve_ip",buf,&Ip2Name)>0) return; + if (getparam_bool("resolve_ip",buf,&Ip2Name)>0) return; - if (getparam_bool("user_ip",buf,&UserIp)>0) return; + if (getparam_bool("user_ip",buf,&UserIp)>0) return; - if (getparam_string("max_elapsed",buf,MaxElapsed,sizeof(MaxElapsed))>0) return; + if (getparam_string("max_elapsed",buf,MaxElapsed,sizeof(MaxElapsed))>0) return; - if (is_param("date_format",buf)) { - getword_start(&gwarea,buf); - if (getword_multisep(wbuf,sizeof(wbuf),&gwarea,' ')<0) { - debuga(_("Maybe you have a broken record or garbage in \"date_format\" parameter\n")); - exit(EXIT_FAILURE); - } - strncpy(DateFormat,gwarea.current,1); - fixnone(DateFormat); - return; - } + if (is_param("date_format",buf)) { + getword_start(&gwarea,buf); + if (getword_multisep(wbuf,sizeof(wbuf),&gwarea,' ')<0) { + debuga(_("Maybe you have a broken record or garbage in \"date_format\" parameter\n")); + exit(EXIT_FAILURE); + } + strncpy(DateFormat,gwarea.current,1); + fixnone(DateFormat); + return; + } - if (is_param("hours",buf)) { - if( getnumlist( buf, &hours, 24, 24 ) ) { - debuga(_("Error: Invalid syntax in hours tag!\n")); - exit( 1 ); - } - return; - } + if (is_param("hours",buf)) { + if( getnumlist( buf, &hours, 24, 24 ) ) { + debuga(_("Error: Invalid syntax in hours tag!\n")); + exit( 1 ); + } + return; + } - if (is_param("weekdays",buf)) { - if( getnumlist( buf, &weekdays, 7, 7 ) ) { - debuga(_("Error: Invalid syntax in weekdays tag!\n")); - exit( 1 ); - } - return; - } + if (is_param("weekdays",buf)) { + if( getnumlist( buf, &weekdays, 7, 7 ) ) { + debuga(_("Error: Invalid syntax in weekdays tag!\n")); + exit( 1 ); + } + return; + } - if (getparam_sort("topuser_sort_field",SET_LIST(topuser_sort),buf,&TopuserSort)>0) return; + if (getparam_sort("topuser_sort_field",SET_LIST(topuser_sort),buf,&TopuserSort)>0) return; - if (getparam_sort("user_sort_field",SET_LIST(user_sort),buf,&UserSort)>0) return; + if (getparam_sort("user_sort_field",SET_LIST(user_sort),buf,&UserSort)>0) return; - if (is_param("access_log",buf)>0) { - if (AccessLogFromCmdLine==0) { - if (NAccessLog>=MAXLOGS) { - debuga(_("Too many log files in configuration file\n")); - exit(EXIT_FAILURE); - } - getparam_string("access_log",buf,AccessLog[NAccessLog],MAX_LOG_FILELEN); - NAccessLog++; - } - return; - } + if (is_param("access_log",buf)>0) { + if (AccessLogFromCmdLine==0) { + if (NAccessLog>=MAXLOGS) { + debuga(_("Too many log files in configuration file\n")); + exit(EXIT_FAILURE); + } + getparam_string("access_log",buf,AccessLog[NAccessLog],MAX_LOG_FILELEN); + NAccessLog++; + } + return; + } - if (is_param("redirector_log",buf)>0) { - if (RedirectorLogFromCmdLine==0) { - if (NRedirectorLogs>=MAX_REDIRECTOR_LOGS) { - debuga(_("Too many redirector log files in configuration file\n")); - exit(EXIT_FAILURE); - } - getparam_string("redirector_log",buf,RedirectorLogs[NRedirectorLogs],MAX_REDIRECTOR_FILELEN); - NRedirectorLogs++; - } - return; - } + if (is_param("redirector_log",buf)>0) { + if (RedirectorLogFromCmdLine==0) { + if (NRedirectorLogs>=MAX_REDIRECTOR_LOGS) { + debuga(_("Too many redirector log files in configuration file\n")); + exit(EXIT_FAILURE); + } + getparam_string("redirector_log",buf,RedirectorLogs[NRedirectorLogs],MAX_REDIRECTOR_FILELEN); + NRedirectorLogs++; + } + return; + } - if (getparam_string("useragent_log",buf,UserAgentLog,sizeof(UserAgentLog))>0) return; + if (getparam_string("useragent_log",buf,UserAgentLog,sizeof(UserAgentLog))>0) return; - if (getparam_string("exclude_hosts",buf,ExcludeHosts,sizeof(ExcludeHosts))>0) return; + if (getparam_string("exclude_hosts",buf,ExcludeHosts,sizeof(ExcludeHosts))>0) return; - if (getparam_string("exclude_codes",buf,ExcludeCodes,sizeof(ExcludeCodes))>0) return; + if (getparam_string("exclude_codes",buf,ExcludeCodes,sizeof(ExcludeCodes))>0) return; - if (getparam_string("exclude_users",buf,ExcludeUsers,sizeof(ExcludeUsers))>0) return; + if (getparam_string("exclude_users",buf,ExcludeUsers,sizeof(ExcludeUsers))>0) return; - if (getparam_string("password",buf,PasswdFile,sizeof(PasswdFile))>0) return; + if (getparam_string("password",buf,PasswdFile,sizeof(PasswdFile))>0) return; - if (getparam_string("temporary_dir",buf,TempDir,sizeof(TempDir))>0) return; + if (getparam_string("temporary_dir",buf,TempDir,sizeof(TempDir))>0) return; - if (getparam_list("report_type",SET_LIST(report_type_values),buf,&ReportType)>0) return; + if (getparam_list("report_type",SET_LIST(report_type_values),buf,&ReportType)>0) return; - if (getparam_string("output_dir",buf,OutputDir,sizeof(OutputDir))>0) return; + if (getparam_string("output_dir",buf,OutputDir,sizeof(OutputDir))>0) return; - if (getparam_string("output_email",buf,OutputEmail,sizeof(OutputEmail))>0) return; + if (getparam_string("output_email",buf,OutputEmail,sizeof(OutputEmail))>0) return; - if (getparam_2words("per_user_limit",buf,PerUserLimitFile,sizeof(PerUserLimitFile),wbuf,sizeof(wbuf))>0) { - PerUserLimit=atoi(wbuf); - return; - } + if (getparam_2words("per_user_limit",buf,PerUserLimitFile,sizeof(PerUserLimitFile),wbuf,sizeof(wbuf))>0) { + PerUserLimit=atoi(wbuf); + return; + } - if (getparam_int("lastlog",buf,&LastLog)>0) return; + if (getparam_int("lastlog",buf,&LastLog)>0) return; - if (getparam_bool("remove_temp_files",buf,&RemoveTempFiles)>0) return; + if (getparam_bool("remove_temp_files",buf,&RemoveTempFiles)>0) return; - if (getparam_string("replace_index",buf,ReplaceIndex,sizeof(ReplaceIndex))>0) return; + if (getparam_string("replace_index",buf,ReplaceIndex,sizeof(ReplaceIndex))>0) return; - if (getparam_list("index_tree",SET_LIST(index_tree_values),buf,&IndexTree)>0) return; + if (getparam_list("index_tree",SET_LIST(index_tree_values),buf,&IndexTree)>0) return; - if (getparam_list("index",SET_LIST(index_values),buf,&Index)>0) return; + if (getparam_list("index",SET_LIST(index_values),buf,&Index)>0) return; - if (getparam_bool("overwrite_report",buf,&OverwriteReport)>0) return; + if (getparam_bool("overwrite_report",buf,&OverwriteReport)>0) return; - if (getparam_list("records_without_userid",SET_LIST(recnouser_values),buf,&RecordsWithoutUser)>0) return; + if (getparam_list("records_without_userid",SET_LIST(recnouser_values),buf,&RecordsWithoutUser)>0) return; - if (getparam_bool("use_comma",buf,&UseComma)>0) return; + if (getparam_bool("use_comma",buf,&UseComma)>0) return; - if (getparam_string("mail_utility",buf,MailUtility,sizeof(MailUtility))>0) return; + if (getparam_string("mail_utility",buf,MailUtility,sizeof(MailUtility))>0) return; - if (getparam_int("topsites_num",buf,&TopSitesNum)>0) return; + if (getparam_int("topsites_num",buf,&TopSitesNum)>0) return; - if (getparam_int("topuser_num",buf,&TopUsersNum)>0) return; + if (getparam_int("topuser_num",buf,&TopUsersNum)>0) return; - if (getparam_string("usertab",buf,UserTabFile,sizeof(UserTabFile))>0) return; + if (getparam_string("usertab",buf,UserTabFile,sizeof(UserTabFile))>0) return; - if (getparam_string("index_sort_order",buf,IndexSortOrder,sizeof(IndexSortOrder))>0) return; + if (getparam_string("index_sort_order",buf,IndexSortOrder,sizeof(IndexSortOrder))>0) return; - if (getparam_sort("topsites_sort_order",SET_LIST(topsite_sort),buf,&TopsitesSort)>0) return; + if (getparam_sort("topsites_sort_order",SET_LIST(topsite_sort),buf,&TopsitesSort)>0) return; - if (getparam_bool("long_url",buf,&LongUrl)>0) return; + if (getparam_bool("long_url",buf,&LongUrl)>0) return; - if (getparam_string("dansguardian_conf",buf,DansGuardianConf,sizeof(DansGuardianConf))>0) return; + if (getparam_string("dansguardian_conf",buf,DansGuardianConf,sizeof(DansGuardianConf))>0) return; - if (getparam_string("squidguard_conf",buf,SquidGuardConf,sizeof(SquidGuardConf))>0) return; + if (getparam_string("squidguard_conf",buf,SquidGuardConf,sizeof(SquidGuardConf))>0) return; - if (getparam_list("date_time_by",SET_LIST(datetime_values),buf,&datetimeby)>0) return; + if (getparam_list("date_time_by",SET_LIST(datetime_values),buf,&datetimeby)>0) return; - if (getparam_string("charset",buf,CharSet,sizeof(CharSet))>0) { - ccharset(CharSet); - return; - } + if (getparam_string("charset",buf,CharSet,sizeof(CharSet))>0) { + ccharset(CharSet); + return; + } - if (getparam_quoted("user_invalid_char",buf,UserInvalidChar,sizeof(UserInvalidChar))>0) return; + if (getparam_quoted("user_invalid_char",buf,UserInvalidChar,sizeof(UserInvalidChar))>0) return; - if (getparam_quoted("include_users",buf,IncludeUsers+1,sizeof(IncludeUsers)-2)>0) { - IncludeUsers[0]=':'; - strcat(IncludeUsers,":"); - return; - } + if (getparam_quoted("include_users",buf,IncludeUsers+1,sizeof(IncludeUsers)-2)>0) { + IncludeUsers[0]=':'; + strcat(IncludeUsers,":"); + return; + } - if (getparam_quoted("exclude_string",buf,ExcludeString,sizeof(ExcludeString))>0) return; + if (getparam_quoted("exclude_string",buf,ExcludeString,sizeof(ExcludeString))>0) return; - if (getparam_bool("privacy",buf,&Privacy)>0) return; + if (getparam_bool("privacy",buf,&Privacy)>0) return; - if (getparam_quoted("privacy_string",buf,PrivacyString,sizeof(PrivacyString))>0) return; + if (getparam_quoted("privacy_string",buf,PrivacyString,sizeof(PrivacyString))>0) return; - if (getparam_string("privacy_string_color",buf,PrivacyStringColor,sizeof(PrivacyStringColor))>0) return; + if (getparam_string("privacy_string_color",buf,PrivacyStringColor,sizeof(PrivacyStringColor))>0) return; - if (getparam_bool("show_successful_message",buf,&SuccessfulMsg)>0) return; + if (getparam_bool("show_successful_message",buf,&SuccessfulMsg)>0) return; - if (getparam_bool("show_read_statistics",buf,&ShowReadStatistics)>0) return; + if (getparam_bool("show_read_statistics",buf,&ShowReadStatistics)>0) return; - if (getparam_list("topuser_fields",SET_LIST(topuserfields_values),buf,&TopUserFields)>0) return; + if (getparam_list("topuser_fields",SET_LIST(topuserfields_values),buf,&TopUserFields)>0) return; - if (getparam_bool("bytes_in_sites_users_report",buf,&BytesInSitesUsersReport)>0) return; + if (getparam_bool("bytes_in_sites_users_report",buf,&BytesInSitesUsersReport)>0) return; - if (getparam_list("user_report_fields",SET_LIST(userreportfields_values),buf,&UserReportFields)>0) return; + if (getparam_list("user_report_fields",SET_LIST(userreportfields_values),buf,&UserReportFields)>0) return; - if (getparam_string("datafile",buf,DataFile,sizeof(DataFile))>0) return; + if (getparam_string("datafile",buf,DataFile,sizeof(DataFile))>0) return; - if (getparam_quoted("datafile_delimiter",buf,DataFileDelimiter,sizeof(DataFileDelimiter))>0) return; + if (getparam_quoted("datafile_delimiter",buf,DataFileDelimiter,sizeof(DataFileDelimiter))>0) return; - if (getparam_list("datafile_fields",SET_LIST(data_field_values),buf,&DataFileFields)>0) return; + if (getparam_list("datafile_fields",SET_LIST(data_field_values),buf,&DataFileFields)>0) return; - if (getparam_list("datafile_url",SET_LIST(datafileurl_values),buf,&DataFileUrl)>0) return; + if (getparam_list("datafile_url",SET_LIST(datafileurl_values),buf,&DataFileUrl)>0) return; - if (getparam_string("parsed_output_log",buf,ParsedOutputLog,sizeof(ParsedOutputLog))>0) return; + if (getparam_string("parsed_output_log",buf,ParsedOutputLog,sizeof(ParsedOutputLog))>0) return; - if (getparam_string("parsed_output_log_compress",buf,ParsedOutputLogCompress,sizeof(ParsedOutputLogCompress))>0) return; + if (getparam_string("parsed_output_log_compress",buf,ParsedOutputLogCompress,sizeof(ParsedOutputLogCompress))>0) return; - if (getparam_list("displayed_values",SET_LIST(displayvalue_values),buf,&DisplayedValues)>0) return; + if (getparam_list("displayed_values",SET_LIST(displayvalue_values),buf,&DisplayedValues)>0) return; - if (getparam_int("authfail_report_limit",buf,&AuthfailReportLimit)>0) return; + if (getparam_int("authfail_report_limit",buf,&AuthfailReportLimit)>0) return; - if (getparam_int("denied_report_limit",buf,&DeniedReportLimit)>0) return; + if (getparam_int("denied_report_limit",buf,&DeniedReportLimit)>0) return; - if (getparam_int("siteusers_report_limit",buf,&SiteUsersReportLimit)>0) return; + if (getparam_int("siteusers_report_limit",buf,&SiteUsersReportLimit)>0) return; - if (getparam_int("dansguardian_report_limit",buf,&DansGuardianReportLimit)>0) return; + if (getparam_int("dansguardian_report_limit",buf,&DansGuardianReportLimit)>0) return; - if (getparam_int("squidguard_report_limit",buf,&SquidGuardReportLimit)>0) return; + if (getparam_int("squidguard_report_limit",buf,&SquidGuardReportLimit)>0) return; - if (getparam_int("user_report_limit",buf,&UserReportLimit)>0) return; + if (getparam_int("user_report_limit",buf,&UserReportLimit)>0) return; - if (getparam_int("download_report_limit",buf,&DownloadReportLimit)>0) return; + if (getparam_int("download_report_limit",buf,&DownloadReportLimit)>0) return; - if (getparam_string("www_document_root",buf,wwwDocumentRoot,sizeof(wwwDocumentRoot))>0) return; + if (getparam_string("www_document_root",buf,wwwDocumentRoot,sizeof(wwwDocumentRoot))>0) return; - if (getparam_string("block_it",buf,BlockIt,sizeof(BlockIt))>0) return; + if (getparam_string("block_it",buf,BlockIt,sizeof(BlockIt))>0) return; - if (getparam_string("external_css_file",buf,ExternalCSSFile,sizeof(ExternalCSSFile))>0) return; + if (getparam_string("external_css_file",buf,ExternalCSSFile,sizeof(ExternalCSSFile))>0) return; - if (getparam_bool("user_authentication",buf,&UserAuthentication)>0) return; + if (getparam_bool("user_authentication",buf,&UserAuthentication)>0) return; - if (getparam_string("AuthUserTemplateFile",buf,wbuf,sizeof(wbuf))>0) { - char dir[MAXLEN]; + if (getparam_string("AuthUserTemplateFile",buf,wbuf,sizeof(wbuf))>0) { + char dir[MAXLEN]; - if (is_absolute(wbuf)) { - if (strlen(wbuf)>=sizeof(AuthUserTemplateFile)) { - debuga(_("Template file name is too long in parameter \"AuthUserTemplateFile\"\n")); - exit(EXIT_FAILURE); - } - strcpy(AuthUserTemplateFile,wbuf); - } else { - strcpy(dir,ConfigFile); - if (snprintf(AuthUserTemplateFile,sizeof(AuthUserTemplateFile),"%s/%s",dirname(dir),wbuf)>=sizeof(AuthUserTemplateFile)) { - debuga(_("Template file name is too long in parameter \"AuthUserTemplateFile\"\n")); - exit(EXIT_FAILURE); - } - } - return; - } + if (is_absolute(wbuf)) { + if (strlen(wbuf)>=sizeof(AuthUserTemplateFile)) { + debuga(_("Template file name is too long in parameter \"AuthUserTemplateFile\"\n")); + exit(EXIT_FAILURE); + } + strcpy(AuthUserTemplateFile,wbuf); + } else { + strcpy(dir,ConfigFile); + if (snprintf(AuthUserTemplateFile,sizeof(AuthUserTemplateFile),"%s/%s",dirname(dir),wbuf)>=sizeof(AuthUserTemplateFile)) { + debuga(_("Template file name is too long in parameter \"AuthUserTemplateFile\"\n")); + exit(EXIT_FAILURE); + } + } + return; + } - if (is_param("download_suffix",buf)) { - char warea[MAXLEN]; + if (is_param("download_suffix",buf)) { + char warea[MAXLEN]; - getparam_quoted("download_suffix",buf,warea,sizeof(warea)); - set_download_suffix(warea); - return; - } + getparam_quoted("download_suffix",buf,warea,sizeof(warea)); + set_download_suffix(warea); + return; + } - if (getparam_bool("graphs",buf,&Graphs)>0) return; + if (getparam_bool("graphs",buf,&Graphs)>0) return; - if (getparam_string("graph_days_bytes_bar_color",buf,GraphDaysBytesBarColor,sizeof(GraphDaysBytesBarColor))>0) return; + if (getparam_string("graph_days_bytes_bar_color",buf,GraphDaysBytesBarColor,sizeof(GraphDaysBytesBarColor))>0) return; - if (getparam_string("redirector_log_format",buf,RedirectorLogFormat,sizeof(RedirectorLogFormat))>0) return; - if (getparam_string("squidguard_log_format",buf,RedirectorLogFormat,sizeof(RedirectorLogFormat))>0) { - debuga(_("squidguard_log_format is deprecated and has been replaced by redirector_log_format. Please update your configuration file.\n")); - return; - } + if (getparam_string("redirector_log_format",buf,RedirectorLogFormat,sizeof(RedirectorLogFormat))>0) return; + if (getparam_string("squidguard_log_format",buf,RedirectorLogFormat,sizeof(RedirectorLogFormat))>0) { + debuga(_("squidguard_log_format is deprecated and has been replaced by redirector_log_format. Please update your configuration file.\n")); + return; + } - if (getparam_bool("redirector_filter_out_date",buf,&RedirectorFilterOutDate)>0) return; - if (getparam_bool("redirector_ignore_date",buf,&RedirectorFilterOutDate)>0) { - /* - Due to an old bug in sarg before version 2.3, the option was having the opposite action than implied by the name. - */ - debuga(_("redirector_ignore_date is deprecated and has been replaced by redirector_filter_out_date that does the action implied by its name as opposed to redirector_ignore_date. Please update your configuration file.\n")); - RedirectorFilterOutDate=!RedirectorFilterOutDate; - return; - } - if (getparam_bool("squidguard_ignore_date",buf,&RedirectorFilterOutDate)>0) { - debuga(_("squidguard_ignore_date is deprecated and has been replaced by redirector_filter_out_date that does the action implied by its name as opposed to squidguard_ignore_date. Please update your configuration file.\n")); - RedirectorFilterOutDate=!RedirectorFilterOutDate; - return; - } + if (getparam_bool("redirector_filter_out_date",buf,&RedirectorFilterOutDate)>0) return; + if (getparam_bool("redirector_ignore_date",buf,&RedirectorFilterOutDate)>0) { + /* + Due to an old bug in sarg before version 2.3, the option was having the opposite action than implied by the name. + */ + debuga(_("redirector_ignore_date is deprecated and has been replaced by redirector_filter_out_date that does the action implied by its name as opposed to redirector_ignore_date. Please update your configuration file.\n")); + RedirectorFilterOutDate=!RedirectorFilterOutDate; + return; + } + if (getparam_bool("squidguard_ignore_date",buf,&RedirectorFilterOutDate)>0) { + debuga(_("squidguard_ignore_date is deprecated and has been replaced by redirector_filter_out_date that does the action implied by its name as opposed to squidguard_ignore_date. Please update your configuration file.\n")); + RedirectorFilterOutDate=!RedirectorFilterOutDate; + return; + } - if (getparam_bool("dansguardian_filter_out_date",buf,&DansguardianFilterOutDate)>0) return; - if (getparam_bool("dansguardian_ignore_date",buf,&DansguardianFilterOutDate)>0) { - debuga(_("dansguardian_ignore_date is deprecated and has been replaced by dansguardian_filter_out_date that does the action implied by its name as opposed to dansguardian_ignore_date. Please update your configuration file.\n")); - DansguardianFilterOutDate=!DansguardianFilterOutDate; - return; - } + if (getparam_bool("dansguardian_filter_out_date",buf,&DansguardianFilterOutDate)>0) return; + if (getparam_bool("dansguardian_ignore_date",buf,&DansguardianFilterOutDate)>0) { + debuga(_("dansguardian_ignore_date is deprecated and has been replaced by dansguardian_filter_out_date that does the action implied by its name as opposed to dansguardian_ignore_date. Please update your configuration file.\n")); + DansguardianFilterOutDate=!DansguardianFilterOutDate; + return; + } - if (getparam_string("ulimit",buf,Ulimit,sizeof(Ulimit))>0) return; + if (getparam_string("ulimit",buf,Ulimit,sizeof(Ulimit))>0) return; - if (getparam_list("ntlm_user_format",SET_LIST(ntml_userformat_values),buf,&NtlmUserFormat)>0) return; + if (getparam_list("ntlm_user_format",SET_LIST(ntml_userformat_values),buf,&NtlmUserFormat)>0) return; - if (getparam_string("realtime_types",buf,RealtimeTypes,sizeof(RealtimeTypes))>0) return; + if (getparam_string("realtime_types",buf,RealtimeTypes,sizeof(RealtimeTypes))>0) return; - if (getparam_list("realtime_unauthenticated_records",SET_LIST(realtime_unauth_values),buf,&RealtimeUnauthRec)>0) return; + if (getparam_list("realtime_unauthenticated_records",SET_LIST(realtime_unauth_values),buf,&RealtimeUnauthRec)>0) return; - if (getparam_int("realtime_refresh_time",buf,&realtime_refresh)>0) return; + if (getparam_int("realtime_refresh_time",buf,&realtime_refresh)>0) return; - if (getparam_int("realtime_access_log_lines",buf,&realtime_access_log_lines)>0) return; + if (getparam_int("realtime_access_log_lines",buf,&realtime_access_log_lines)>0) return; - if (getparam_string("LDAPHost",buf,LDAPHost,sizeof(LDAPHost))>0) return; + if (getparam_string("LDAPHost",buf,LDAPHost,sizeof(LDAPHost))>0) return; - if (getparam_int("LDAPPort",buf,&LDAPPort)>0) return; + if (getparam_int("LDAPPort",buf,&LDAPPort)>0) return; - if (getparam_int("LDAPProtocolVersion",buf,&LDAPProtocolVersion)>0) return; + if (getparam_int("LDAPProtocolVersion",buf,&LDAPProtocolVersion)>0) return; - if (getparam_string("LDAPBindDN",buf,LDAPBindDN,sizeof(LDAPBindDN))>0) return; + if (getparam_string("LDAPBindDN",buf,LDAPBindDN,sizeof(LDAPBindDN))>0) return; - if (getparam_string("LDAPBindPW",buf,LDAPBindPW,sizeof(LDAPBindPW))>0) return; + if (getparam_string("LDAPBindPW",buf,LDAPBindPW,sizeof(LDAPBindPW))>0) return; - if (getparam_string("LDAPBaseSearch",buf,LDAPBaseSearch,sizeof(LDAPBaseSearch))>0) return; + if (getparam_string("LDAPBaseSearch",buf,LDAPBaseSearch,sizeof(LDAPBaseSearch))>0) return; - if (getparam_string("LDAPFilterSearch",buf,LDAPFilterSearch,sizeof(LDAPFilterSearch))>0) return; + if (getparam_string("LDAPFilterSearch",buf,LDAPFilterSearch,sizeof(LDAPFilterSearch))>0) return; - if (getparam_string("LDAPTargetAttr",buf,LDAPTargetAttr,sizeof(LDAPTargetAttr))>0) return; + if (getparam_string("LDAPTargetAttr",buf,LDAPTargetAttr,sizeof(LDAPTargetAttr))>0) return; - if (getparam_string("graph_font",buf,GraphFont,sizeof(GraphFont))>0) return; + if (getparam_string("graph_font",buf,GraphFont,sizeof(GraphFont))>0) return; - if (getparam_string("sorttable",buf,SortTableJs,sizeof(SortTableJs))>0) return; + if (getparam_string("sorttable",buf,SortTableJs,sizeof(SortTableJs))>0) return; - if(strstr(buf,"squid24") != 0) { - squid24=true; - return; - } + if(strstr(buf,"squid24") != 0) { + squid24=true; + return; + } - if(strstr(buf,"byte_cost") != 0) { - getword_start(&gwarea,buf); - if (getword_multisep(wbuf,sizeof(wbuf),&gwarea,' ')<0) { - debuga(_("The \"byte_cost\" parameter of the configuration file is invalid\n")); - exit(EXIT_FAILURE); - } - cost=atol(gwarea.current); - if (getword_multisep(wbuf,sizeof(wbuf),&gwarea,' ')<0) { - debuga(_("The \"byte_cost\" parameter of the configuration file is invalid\n")); - exit(EXIT_FAILURE); - } - nocost=my_atoll(gwarea.current); - return; - } + if(strstr(buf,"byte_cost") != 0) { + getword_start(&gwarea,buf); + if (getword_multisep(wbuf,sizeof(wbuf),&gwarea,' ')<0) { + debuga(_("The \"byte_cost\" parameter of the configuration file is invalid\n")); + exit(EXIT_FAILURE); + } + cost=atol(gwarea.current); + if (getword_multisep(wbuf,sizeof(wbuf),&gwarea,' ')<0) { + debuga(_("The \"byte_cost\" parameter of the configuration file is invalid\n")); + exit(EXIT_FAILURE); + } + nocost=my_atoll(gwarea.current); + return; + } - printf(_("SARG: Unknown option %s\n"),buf); + printf(_("SARG: Unknown option %s\n"),buf); } void getconf(void) { + FILE *fp_in; + char buf[MAXLEN]; - FILE *fp_in; - char buf[MAXLEN]; + if(debug) + debuga(_("Loading configuration from %s\n"),ConfigFile); - if(debug) - debuga(_("Loading configuration from %s\n"),ConfigFile); + if ((fp_in = fopen(ConfigFile, "r")) == NULL) { + debuga(_("(getconf) Cannot open file %s\n"),ConfigFile); + exit(EXIT_FAILURE); + } - if ((fp_in = fopen(ConfigFile, "r")) == NULL) { - debuga(_("(getconf) Cannot open file %s\n"),ConfigFile); - exit(EXIT_FAILURE); - } + while (fgets(buf, sizeof(buf), fp_in) != NULL) { + fixendofline(buf); - while (fgets(buf, sizeof(buf), fp_in) != NULL) { - fixendofline(buf); + if(debugm) + printf("SYSCONFDIR %s\n",buf); - if(debugm) - printf("SYSCONFDIR %s\n",buf); + parmtest(buf); - parmtest(buf); + } - } - - fclose(fp_in); - return; + fclose(fp_in); + return; } diff --git a/grepday.c b/grepday.c index 5d09f79..87829a0 100644 --- a/grepday.c +++ b/grepday.c @@ -36,84 +36,84 @@ struct GraphDataStruct { - int lavender; - int darkblue; - int dimgray; - int goldenrod; - int goldenrod2; - int gray; - int silver; - int black; - //! The color of the top side of a graph bar. - int color1; - //! The color of the right side of a graph bar. - int color2; - //! The color of the front side of a graph bar. - int color3; - //! The libgd image we are drawing on. - gdImage *im; - //! An allocated buffer to convert the string into UTF-8. - char *string; - //! The number of bytes allocated for the string buffer. - size_t string_size; - //! The bottom border of the graph. - int BottomGraph; - //! The top border of the graph. - int TopGraph; - //! The left border of the graph. - int LeftGraph; - //! The right border of the graph. - int RightGraph; - //! The height at which the bottom depth border of the graph extends. - int BottomDepth; - //! The distance between two ticks on the horizontal axis. - double XScale; - //! The distance between two ticks on the vertical axis. - double YScale; - //! The exterior length of a tick on the scales. - int TickLength; - //! The distance, in pixels, between two ticks along the Y axis. - int YTickSpace; + int lavender; + int darkblue; + int dimgray; + int goldenrod; + int goldenrod2; + int gray; + int silver; + int black; + //! The color of the top side of a graph bar. + int color1; + //! The color of the right side of a graph bar. + int color2; + //! The color of the front side of a graph bar. + int color3; + //! The libgd image we are drawing on. + gdImage *im; + //! An allocated buffer to convert the string into UTF-8. + char *string; + //! The number of bytes allocated for the string buffer. + size_t string_size; + //! The bottom border of the graph. + int BottomGraph; + //! The top border of the graph. + int TopGraph; + //! The left border of the graph. + int LeftGraph; + //! The right border of the graph. + int RightGraph; + //! The height at which the bottom depth border of the graph extends. + int BottomDepth; + //! The distance between two ticks on the horizontal axis. + double XScale; + //! The distance between two ticks on the vertical axis. + double YScale; + //! The exterior length of a tick on the scales. + int TickLength; + //! The distance, in pixels, between two ticks along the Y axis. + int YTickSpace; }; enum PlotType { - PTG_LinBin, - PTG_LogBin, - PTG_Time, + PTG_LinBin, + PTG_LogBin, + PTG_Time, }; struct PlotStruct { - //! The data points to plot. - long long int *datapoints; - //! The number of points to plot. - int npoints; - //! The minimum data to plot along the Y axis. - long long int ymin; - //! The maximum data to plot along the Y axis. - long long int ymax; - //! The type of Y axis to draw. - enum PlotType ytype; - //! The label to write on the X axis. - const char *XLabel; - //! The label to write on the Y axis. - const char *YLabel; - //! The name of the output PNG file. - const char *pngfile; + //! The data points to plot. + long long int *datapoints; + //! The number of points to plot. + int npoints; + //! The minimum data to plot along the Y axis. + long long int ymin; + //! The maximum data to plot along the Y axis. + long long int ymax; + //! The type of Y axis to draw. + enum PlotType ytype; + //! The label to write on the X axis. + const char *XLabel; + //! The label to write on the Y axis. + const char *YLabel; + //! The name of the output PNG file. + const char *pngfile; }; enum TextRefPos { - TRP_TopLeft, - TRP_TopCenter, - TRP_TopRight, - TRP_BottomLeft, - TRP_BottomCenter, - TRP_BottomRight, - TRP_CenterLeft, - TRP_Center, - TRP_CenterRight, + TRP_TopLeft, + TRP_TopCenter, + TRP_TopRight, + TRP_BottomLeft, + TRP_BottomCenter, + TRP_BottomRight, + TRP_CenterLeft, + TRP_Center, + TRP_CenterRight, }; #ifdef USE_ICONV @@ -122,510 +122,510 @@ iconv_t localtoutf=(iconv_t)-1; #endif static void Sarg_gdImageStringFT (struct GraphDataStruct *gdata, int fg, char *fontlist, - double ptsize, double angle, int x, int y, const char *string,enum TextRefPos RefPos) + double ptsize, double angle, int x, int y, const char *string,enum TextRefPos RefPos) { - char *sstring; - char *retval; - int brect[8]; - int minx,miny,maxx,maxy; - int i; + char *sstring; + char *retval; + int brect[8]; + int minx,miny,maxx,maxy; + int i; #ifdef USE_ICONV - if (localtoutf!=(iconv_t)-1) { - const char *str; - char *sstr; - size_t slen, sslen; - - slen = strlen(string) + 1; // We must include string termination character - sslen = slen * 3; // We assume that the UTF8 string will not be bigger than 3 times the original size. - if (sslen>gdata->string_size) { - sstring = (char *)realloc(gdata->string,sslen); - if (!sstring) { - debuga(_("realloc error (%"PRIu64" bytes required)\n"),(uint64_t)sslen); - exit(EXIT_FAILURE); - } - gdata->string=(char *)sstring; - gdata->string_size=sslen; - } else { - sstring=gdata->string; - sslen=gdata->string_size; - } - - str = string; - sstr = sstring; - if (iconv (localtoutf, (ICONV_CONST char **)&str, &slen, &sstr, &sslen)==-1) { - debuga(_("(grepday) iconv failed to convert string \"%s\" from %s to UTF-8 - %s\n"),string,CharSet,strerror(errno)); - sstring=(char *)string; //show something sensible on the graph - } - } else { - sstring=(char *)string; //show something sensible on the graph - } + if (localtoutf!=(iconv_t)-1) { + const char *str; + char *sstr; + size_t slen, sslen; + + slen = strlen(string) + 1; // We must include string termination character + sslen = slen * 3; // We assume that the UTF8 string will not be bigger than 3 times the original size. + if (sslen>gdata->string_size) { + sstring = (char *)realloc(gdata->string,sslen); + if (!sstring) { + debuga(_("realloc error (%"PRIu64" bytes required)\n"),(uint64_t)sslen); + exit(EXIT_FAILURE); + } + gdata->string=(char *)sstring; + gdata->string_size=sslen; + } else { + sstring=gdata->string; + sslen=gdata->string_size; + } + + str = string; + sstr = sstring; + if (iconv (localtoutf, (ICONV_CONST char **)&str, &slen, &sstr, &sslen)==-1) { + debuga(_("(grepday) iconv failed to convert string \"%s\" from %s to UTF-8 - %s\n"),string,CharSet,strerror(errno)); + sstring=(char *)string; //show something sensible on the graph + } + } else { + sstring=(char *)string; //show something sensible on the graph + } #else - sstring=(char *)string; + sstring=(char *)string; #endif - if (RefPos!=TRP_BottomLeft) { - retval = gdImageStringFTEx (NULL, brect, fg, fontlist, ptsize, angle, 0, 0, sstring, gdFTEX_Unicode); - if (retval) { - debuga(_("libgd failed to calculate the bounding box of the text \"%s\": %s\n"),sstring,retval); - exit(EXIT_FAILURE); - } - /* - From libgd documentation, brect contains this without taking into account the angle: - 0 lower left corner, X position - 1 lower left corner, Y position - 2 lower right corner, X position - 3 lower right corner, Y position - 4 upper right corner, X position - 5 upper right corner, Y position - 6 upper left corner, X position - 7 upper left corner, Y position - */ - minx=maxx=brect[0]; - miny=maxy=brect[1]; - for (i=2 ; i<7 ; i+=2) { - if (minx>brect[i]) minx=brect[i]; - if (maxxbrect[i+1]) miny=brect[i+1]; - if (maxyim, brect, fg, fontlist, ptsize, angle, x, y, sstring, gdFTEX_Unicode); - if (retval) { - debuga(_("libgd failed to render the text \"%s\": %s\n"),sstring,retval); - exit(EXIT_FAILURE); - } + if (RefPos!=TRP_BottomLeft) { + retval = gdImageStringFTEx (NULL, brect, fg, fontlist, ptsize, angle, 0, 0, sstring, gdFTEX_Unicode); + if (retval) { + debuga(_("libgd failed to calculate the bounding box of the text \"%s\": %s\n"),sstring,retval); + exit(EXIT_FAILURE); + } + /* + From libgd documentation, brect contains this without taking into account the angle: + 0 lower left corner, X position + 1 lower left corner, Y position + 2 lower right corner, X position + 3 lower right corner, Y position + 4 upper right corner, X position + 5 upper right corner, Y position + 6 upper left corner, X position + 7 upper left corner, Y position + */ + minx=maxx=brect[0]; + miny=maxy=brect[1]; + for (i=2 ; i<7 ; i+=2) { + if (minx>brect[i]) minx=brect[i]; + if (maxxbrect[i+1]) miny=brect[i+1]; + if (maxyim, brect, fg, fontlist, ptsize, angle, x, y, sstring, gdFTEX_Unicode); + if (retval) { + debuga(_("libgd failed to render the text \"%s\": %s\n"),sstring,retval); + exit(EXIT_FAILURE); + } } static void bar(struct GraphDataStruct *gdata,int x1,double y,const char *label) { - gdPoint points[4]; - int val=0; - int width2; - - val=gdata->BottomGraph+5-(int)(y*gdata->YScale+0.5); - width2=(int)(gdata->XScale/4.); - - // front side of the bar - if (valBottomDepth) - gdImageFilledRectangle(gdata->im, x1-width2, val, x1+width2, gdata->BottomDepth, gdata->color3); - - // top side of the bar - points[0].x = x1-width2+5; - points[0].y = val-5; - points[1].x = x1-width2; - points[1].y = val; - points[2].x = x1+width2; - points[2].y = val; - points[3].x = x1+width2+5; - points[3].y = val-5; - gdImageFilledPolygon(gdata->im, points, 4, gdata->color1); - - gdImageLine(gdata->im, x1+2, val-2, x1+2, val-10, gdata->dimgray); - gdImageFilledRectangle(gdata->im, x1-8, val-20, x1+12, val-10, gdata->goldenrod); - gdImageRectangle(gdata->im, x1-8, val-20, x1+12, val-10, gdata->goldenrod2); - - Sarg_gdImageStringFT(gdata,gdata->black,GraphFont,6,0.0,x1+2,val-12,label,TRP_BottomCenter); - - // lateral side of the bar - if (valBottomDepth) { - points[0].x = x1+width2+5; - points[0].y = val-5; - points[1].x = x1+width2; - points[1].y = val; - points[2].x = x1+width2; - points[2].y = gdata->BottomDepth; - points[3].x = x1+width2+5; - points[3].y = gdata->BottomGraph; - gdImageFilledPolygon(gdata->im, points, 4, gdata->color2); - } - - return; + gdPoint points[4]; + int val=0; + int width2; + + val=gdata->BottomGraph+5-(int)(y*gdata->YScale+0.5); + width2=(int)(gdata->XScale/4.); + + // front side of the bar + if (valBottomDepth) + gdImageFilledRectangle(gdata->im, x1-width2, val, x1+width2, gdata->BottomDepth, gdata->color3); + + // top side of the bar + points[0].x = x1-width2+5; + points[0].y = val-5; + points[1].x = x1-width2; + points[1].y = val; + points[2].x = x1+width2; + points[2].y = val; + points[3].x = x1+width2+5; + points[3].y = val-5; + gdImageFilledPolygon(gdata->im, points, 4, gdata->color1); + + gdImageLine(gdata->im, x1+2, val-2, x1+2, val-10, gdata->dimgray); + gdImageFilledRectangle(gdata->im, x1-8, val-20, x1+12, val-10, gdata->goldenrod); + gdImageRectangle(gdata->im, x1-8, val-20, x1+12, val-10, gdata->goldenrod2); + + Sarg_gdImageStringFT(gdata,gdata->black,GraphFont,6,0.0,x1+2,val-12,label,TRP_BottomCenter); + + // lateral side of the bar + if (valBottomDepth) { + points[0].x = x1+width2+5; + points[0].y = val-5; + points[1].x = x1+width2; + points[1].y = val; + points[2].x = x1+width2; + points[2].y = gdata->BottomDepth; + points[3].x = x1+width2+5; + points[3].y = gdata->BottomGraph; + gdImageFilledPolygon(gdata->im, points, 4, gdata->color2); + } + + return; } static int greport_compute_yaxis(struct PlotStruct *pdata,struct GraphDataStruct *gdata) { - double symin,symax; - double range; - double yscale; - double ymag; - - if (pdata->ymin<0.) { - debuga(_("Minimum for Y scale of the graph is out of range: %"PRId64"\n"),(int64_t)pdata->ymin); - return(-1); - } - if (pdata->ymax<=0.) { - debuga(_("Maximum for Y scale of the graph is out of range: %"PRId64"\n"),(int64_t)pdata->ymax); - return(-1); - } - - switch(pdata->ytype) - { - case PTG_LinBin: - symin=(double)pdata->ymin; - symax=(double)pdata->ymax; - ymag=1000.; - break; - - case PTG_LogBin: - if (pdata->ymin>0.) - symin=log(pdata->ymin); - else - symin=0.; - symax=log(pdata->ymax); - ymag=1000.; - break; - - case PTG_Time: - symin=(double)pdata->ymin; - symax=(double)pdata->ymax; - ymag=1.; - break; - - default: - debuga(_("Unknown type %d for Y axis scale\n"),pdata->ytype); - return(-1); - } - gdata->YTickSpace=10; - - range=symax-symin; - yscale=(double)(gdata->BottomGraph-gdata->TopGraph)/range; - gdata->YScale=yscale; - return(0); + double symin,symax; + double range; + double yscale; + double ymag; + + if (pdata->ymin<0.) { + debuga(_("Minimum for Y scale of the graph is out of range: %"PRId64"\n"),(int64_t)pdata->ymin); + return(-1); + } + if (pdata->ymax<=0.) { + debuga(_("Maximum for Y scale of the graph is out of range: %"PRId64"\n"),(int64_t)pdata->ymax); + return(-1); + } + + switch(pdata->ytype) + { + case PTG_LinBin: + symin=(double)pdata->ymin; + symax=(double)pdata->ymax; + ymag=1000.; + break; + + case PTG_LogBin: + if (pdata->ymin>0.) + symin=log(pdata->ymin); + else + symin=0.; + symax=log(pdata->ymax); + ymag=1000.; + break; + + case PTG_Time: + symin=(double)pdata->ymin; + symax=(double)pdata->ymax; + ymag=1.; + break; + + default: + debuga(_("Unknown type %d for Y axis scale\n"),pdata->ytype); + return(-1); + } + gdata->YTickSpace=10; + + range=symax-symin; + yscale=(double)(gdata->BottomGraph-gdata->TopGraph)/range; + gdata->YScale=yscale; + return(0); } static void greport_formatbin(double yval,int maxdigits,char *string,int slen) { - int len; - char schar[]={'\0','k','M','G','T','P'}; - int scount; - int i; - int ndigits; - - for (scount=0 ; scount=1000. ; scount++) - yval/=1000.; - if (yval<2.) - ndigits=2; - else if (yval<3.) - ndigits=1; - else - ndigits=0; - if (ndigits>maxdigits) ndigits=maxdigits; - len=snprintf(string,slen,"%.*f",ndigits,(float)yval); - if (UseComma) - for (i=0 ; i=1000. ; scount++) + yval/=1000.; + if (yval<2.) + ndigits=2; + else if (yval<3.) + ndigits=1; + else + ndigits=0; + if (ndigits>maxdigits) ndigits=maxdigits; + len=snprintf(string,slen,"%.*f",ndigits,(float)yval); + if (UseComma) + for (i=0 ; iBottomGraph; - yt=gdata->BottomDepth-gdata->BottomGraph; - xexterior=gdata->LeftGraph-10; - xinterior=gdata->LeftGraph; - xtick=gdata->LeftGraph-10-gdata->TickLength; - for(y=y0-gdata->YTickSpace ; y>=gdata->TopGraph ; y-=gdata->YTickSpace) { - gdImageLine(gdata->im, xtick, y+yt, xexterior, y+yt, gdata->dimgray); - gdImageLine(gdata->im, xexterior, y+yt, xinterior, y, gdata->dimgray); - gdImageLine(gdata->im, xinterior, y, gdata->RightGraph, y, gdata->dimgray); - switch (pdata->ytype) - { - case PTG_LinBin: - yval=(double)(y0-y)/gdata->YScale+(double)pdata->ymin; - greport_formatbin(yval,2,YLabel,sizeof(YLabel)); - break; - - case PTG_LogBin: - yval=exp((double)(y0-y)/gdata->YScale+log(pdata->ymin)); - greport_formatbin(yval,2,YLabel,sizeof(YLabel)); - break; - - case PTG_Time: - { - int t; - - yval=(double)(y0-y)/gdata->YScale+(double)pdata->ymin; - t=(int)(yval/60000.+0.5); - snprintf(YLabel,sizeof(YLabel),"%02d:%02d",t/60,t%60); - break; - } - } - Sarg_gdImageStringFT(gdata,gdata->dimgray,GraphFont,7,0.0,xtick,y+yt,YLabel,TRP_CenterRight); - } + double yval; + int y0; + int y; + int yt; + char YLabel[50]; + int xexterior; + int xinterior; + int xtick; + + y0=gdata->BottomGraph; + yt=gdata->BottomDepth-gdata->BottomGraph; + xexterior=gdata->LeftGraph-10; + xinterior=gdata->LeftGraph; + xtick=gdata->LeftGraph-10-gdata->TickLength; + for(y=y0-gdata->YTickSpace ; y>=gdata->TopGraph ; y-=gdata->YTickSpace) { + gdImageLine(gdata->im, xtick, y+yt, xexterior, y+yt, gdata->dimgray); + gdImageLine(gdata->im, xexterior, y+yt, xinterior, y, gdata->dimgray); + gdImageLine(gdata->im, xinterior, y, gdata->RightGraph, y, gdata->dimgray); + switch (pdata->ytype) + { + case PTG_LinBin: + yval=(double)(y0-y)/gdata->YScale+(double)pdata->ymin; + greport_formatbin(yval,2,YLabel,sizeof(YLabel)); + break; + + case PTG_LogBin: + yval=exp((double)(y0-y)/gdata->YScale+log(pdata->ymin)); + greport_formatbin(yval,2,YLabel,sizeof(YLabel)); + break; + + case PTG_Time: + { + int t; + + yval=(double)(y0-y)/gdata->YScale+(double)pdata->ymin; + t=(int)(yval/60000.+0.5); + snprintf(YLabel,sizeof(YLabel),"%02d:%02d",t/60,t%60); + break; + } + } + Sarg_gdImageStringFT(gdata,gdata->dimgray,GraphFont,7,0.0,xtick,y+yt,YLabel,TRP_CenterRight); + } } static void greport_plot(const struct userinfostruct *uinfo,struct PlotStruct *pdata) { - FILE *pngout; - int x, y; - int day; - int x1; - char graph[MAXLEN]; - char s[15]; - char ftime[128]; - time_t t; - struct tm *local; - gdPoint points[4]; - struct GraphDataStruct gdata; - const int ImgXSize=720; - const int ImgYSize=480; - const int LeftMargin=60; - const int RightMargin=20; - const int TopMargin=60; - const int BottomMargin=60; - const int TickLength=3; - const int ZTickLength=5; - double yval; - char blabel[50]; - double logpmin; - - memset(&gdata,0,sizeof(gdata)); - - gdata.im = gdImageCreate(ImgXSize, ImgYSize); - gdata.BottomGraph=ImgYSize-BottomMargin; - gdata.LeftGraph=LeftMargin; - gdata.RightGraph=ImgXSize-RightMargin; - gdata.TopGraph=TopMargin; - gdata.BottomDepth=gdata.BottomGraph+5; - gdata.XScale=(double)(gdata.RightGraph-gdata.LeftGraph)/(pdata->npoints+1); - if (greport_compute_yaxis(pdata,&gdata)<0) return; - gdata.TickLength=TickLength; - - // first allocated color is the background - gdata.lavender = gdImageColorAllocate(gdata.im, 230, 230, 250); - gdata.gray = gdImageColorAllocate(gdata.im, 192, 192, 192); - gdata.silver = gdImageColorAllocate(gdata.im, 211, 211, 211); - gdata.black = gdImageColorAllocate(gdata.im, 0, 0, 0); - gdata.dimgray = gdImageColorAllocate(gdata.im, 105, 105, 105); - gdata.darkblue = gdImageColorAllocate(gdata.im, 0, 0, 139); - gdata.goldenrod = gdImageColorAllocate(gdata.im, 234, 234, 174); - gdata.goldenrod2 = gdImageColorAllocate(gdata.im, 207, 181, 59); - - if(strcmp(GraphDaysBytesBarColor,"orange") == 0) { - gdata.color1 = gdImageColorAllocate(gdata.im, 255, 233, 142); - gdata.color2 = gdImageColorAllocate(gdata.im, 220, 163, 72); - gdata.color3 = gdImageColorAllocate(gdata.im, 255, 198, 107); - } - else if(strcmp(GraphDaysBytesBarColor,"blue") == 0) { - gdata.color1 = gdImageColorAllocate(gdata.im, 62, 80, 167); - gdata.color2 = gdImageColorAllocate(gdata.im, 40, 51, 101); - gdata.color3 = gdImageColorAllocate(gdata.im, 57, 73, 150); - } - else if(strcmp(GraphDaysBytesBarColor,"green") == 0) { - gdata.color1 = gdImageColorAllocate(gdata.im,120,166,129); - gdata.color2 = gdImageColorAllocate(gdata.im,84,113,82); - gdata.color3 = gdImageColorAllocate(gdata.im,158,223,167); - } - else if(strcmp(GraphDaysBytesBarColor,"yellow") == 0) { - gdata.color1 = gdImageColorAllocate(gdata.im,185,185,10); - gdata.color2 = gdImageColorAllocate(gdata.im,111,111,10); - gdata.color3 = gdImageColorAllocate(gdata.im,166,166,10); - } - else if(strcmp(GraphDaysBytesBarColor,"brown") == 0) { - gdata.color1 = gdImageColorAllocate(gdata.im,97,45,27); - gdata.color2 = gdImageColorAllocate(gdata.im,60,30,20); - gdata.color3 = gdImageColorAllocate(gdata.im,88,41,26); - } - else if(strcmp(GraphDaysBytesBarColor,"red") == 0){ - gdata.color1 = gdImageColorAllocate(gdata.im,185,10,10); - gdata.color2 = gdImageColorAllocate(gdata.im,111,10,10); - gdata.color3 = gdImageColorAllocate(gdata.im,166,10,10); - } else { - debuga(_("Unknown color \"%s\" requested for the graph. Using orange instead\n"),GraphDaysBytesBarColor); - gdata.color1 = gdImageColorAllocate(gdata.im, 255, 233, 142); - gdata.color2 = gdImageColorAllocate(gdata.im, 220, 163, 72); - gdata.color3 = gdImageColorAllocate(gdata.im, 255, 198, 107); - } - - // rectangle around the image - gdImageRectangle(gdata.im, 0, 0, ImgXSize-1, ImgYSize-1, gdata.dimgray); - // backtround of the graph - gdImageFilledRectangle(gdata.im, LeftMargin, gdata.TopGraph, gdata.RightGraph, gdata.BottomGraph, gdata.silver); - - // depth of the left Y axis - points[0].x = gdata.LeftGraph-10; - points[0].y = gdata.TopGraph+5; - points[1].x = gdata.LeftGraph-10; - points[1].y = gdata.BottomDepth; - points[2].x = gdata.LeftGraph; - points[2].y = gdata.BottomGraph; - points[3].x = gdata.LeftGraph; - points[3].y = gdata.TopGraph; - gdImageFilledPolygon(gdata.im, points, 4, gdata.gray); - - // depth of the bottom X axis - points[0].x = gdata.LeftGraph; - points[0].y = gdata.BottomGraph; - points[1].x = gdata.LeftGraph-10; - points[1].y = gdata.BottomDepth; - points[2].x = gdata.RightGraph-10; - points[2].y = gdata.BottomDepth; - points[3].x = gdata.RightGraph; - points[3].y = gdata.BottomGraph; - gdImageFilledPolygon(gdata.im, points, 4, gdata.gray); - - // vertical exterior line of the depth - gdImageLine(gdata.im, LeftMargin-10, TopMargin+5, LeftMargin-10, gdata.BottomDepth+ZTickLength, gdata.black); - // horizontal exterior line of the depth - gdImageLine(gdata.im, LeftMargin-10-ZTickLength, gdata.BottomDepth, gdata.RightGraph-10, gdata.BottomDepth, gdata.black); - // diagonal line between the two depths - gdImageLine(gdata.im, LeftMargin-10, gdata.BottomDepth, LeftMargin, gdata.BottomGraph, gdata.black); - // vertical left line of the graph - gdImageLine(gdata.im, LeftMargin, gdata.BottomGraph, LeftMargin, gdata.TopGraph, gdata.black); - // horizontal bottom line of the graph - gdImageLine(gdata.im, LeftMargin, gdata.BottomGraph, gdata.RightGraph, gdata.BottomGraph, gdata.black); - // vertical right line of the graph - gdImageLine(gdata.im, gdata.RightGraph, gdata.TopGraph, gdata.RightGraph, gdata.BottomGraph, gdata.black); - // diagonal line to close the right of the bottom depth - gdImageLine(gdata.im, gdata.RightGraph-10, gdata.BottomDepth, gdata.RightGraph, gdata.BottomGraph, gdata.black); - - // Y axis ticks - greport_draw_yaxis(pdata,&gdata); - - // X axis ticks and labels - for(y=1; y<=pdata->npoints; y++) { - x=gdata.LeftGraph-10+(int)((double)y*gdata.XScale+0.5); - gdImageLine(gdata.im, x, gdata.BottomDepth, x, gdata.BottomDepth+TickLength, gdata.dimgray); - sprintf(s,"%02d",y); - Sarg_gdImageStringFT(&gdata,gdata.dimgray,GraphFont,7,0.0,x,gdata.BottomDepth+TickLength+1,s,TRP_TopCenter); - } - - t = time(NULL); - local = localtime(&t); - if(DateFormat[0]=='u') - strftime(ftime, sizeof(ftime), "%b/%d/%Y %H:%M", local); - if(DateFormat[0]=='e') - strftime(ftime, sizeof(ftime), "%d/%b/%Y-%H:%M", local); - - x=ImgXSize*5/12; - Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,7,0.0,ImgXSize-10,ImgYSize-10,ftime,TRP_BottomRight); - if(ShowSargInfo) Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,10,0.0,x,15,_("SARG, "),TRP_BottomRight); - Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,10,0.0,x,15,Title,TRP_BottomLeft); - sprintf(warea,_("Period: %s"),period.text); - Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,9,0.0,x,27,warea,TRP_BottomLeft); - sprintf(warea,_("User: %s"),uinfo->label); - Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,9,0.0,x,38,warea,TRP_BottomLeft); - - Sarg_gdImageStringFT(&gdata,gdata.black,GraphFont,10,3.141592/2,15,ImgYSize/2,pdata->YLabel,TRP_CenterLeft); - Sarg_gdImageStringFT(&gdata,gdata.black,GraphFont,10,0.0,ImgXSize/2,ImgYSize-20,pdata->XLabel,TRP_BottomCenter); - - logpmin=(pdata->ytype==PTG_LogBin && pdata->ymin>0.) ? log(pdata->ymin) : 0.; - for (day=0 ; daynpoints ; day++) { - if (pdata->datapoints[day]>0) { - x1=gdata.LeftGraph-10+(int)((double)(day+1)*gdata.XScale+0.5); - switch (pdata->ytype) - { - case PTG_LinBin: - yval=(double)pdata->datapoints[day]; - if (yvalymin) - yval=0.; - else if (yval>pdata->ymax) - yval=pdata->ymax; - else - yval-=pdata->ymin; - greport_formatbin(pdata->datapoints[day],1,blabel,sizeof(blabel)); - break; - case PTG_LogBin: - yval=(double)pdata->datapoints[day]; - if (yval<=pdata->ymin) - yval=0.; - else if (yval>pdata->ymax) - yval=log(pdata->ymax)-logpmin; - else - yval=log(yval)-logpmin; - greport_formatbin(pdata->datapoints[day],1,blabel,sizeof(blabel)); - break; - case PTG_Time: - { - int t; - - yval=(double)pdata->datapoints[day]; - if (yvalymin) - yval=0.; - else if (yval>pdata->ymax) - yval=pdata->ymax; - else - yval-=pdata->ymin; - t=(int)(pdata->datapoints[day]/60000.); - snprintf(blabel,sizeof(blabel),"%d:%02d",t/60,t%60); - break; - } - default: - yval=-1.; - break; - } - if (yval>=0.) bar(&gdata,x1,yval,blabel); - } - } - - if (snprintf(graph,sizeof(graph),"%s/%s/%s",outdirname,uinfo->filename,pdata->pngfile)>=sizeof(graph)) { - debuga(_("user name too long for %s/%s/%s\n"),outdirname,uinfo->filename,pdata->pngfile); - exit(EXIT_FAILURE); - } - if((pngout=fopen(graph,"wb"))==NULL) { - debuga(_("(grepday) Cannot open log file %s\n"),graph); - exit(EXIT_FAILURE); - } - gdImagePng(gdata.im, pngout); - fclose(pngout); - gdImageDestroy(gdata.im); - - if (gdata.string) free(gdata.string); + FILE *pngout; + int x, y; + int day; + int x1; + char graph[MAXLEN]; + char s[15]; + char ftime[128]; + time_t t; + struct tm *local; + gdPoint points[4]; + struct GraphDataStruct gdata; + const int ImgXSize=720; + const int ImgYSize=480; + const int LeftMargin=60; + const int RightMargin=20; + const int TopMargin=60; + const int BottomMargin=60; + const int TickLength=3; + const int ZTickLength=5; + double yval; + char blabel[50]; + double logpmin; + + memset(&gdata,0,sizeof(gdata)); + + gdata.im = gdImageCreate(ImgXSize, ImgYSize); + gdata.BottomGraph=ImgYSize-BottomMargin; + gdata.LeftGraph=LeftMargin; + gdata.RightGraph=ImgXSize-RightMargin; + gdata.TopGraph=TopMargin; + gdata.BottomDepth=gdata.BottomGraph+5; + gdata.XScale=(double)(gdata.RightGraph-gdata.LeftGraph)/(pdata->npoints+1); + if (greport_compute_yaxis(pdata,&gdata)<0) return; + gdata.TickLength=TickLength; + + // first allocated color is the background + gdata.lavender = gdImageColorAllocate(gdata.im, 230, 230, 250); + gdata.gray = gdImageColorAllocate(gdata.im, 192, 192, 192); + gdata.silver = gdImageColorAllocate(gdata.im, 211, 211, 211); + gdata.black = gdImageColorAllocate(gdata.im, 0, 0, 0); + gdata.dimgray = gdImageColorAllocate(gdata.im, 105, 105, 105); + gdata.darkblue = gdImageColorAllocate(gdata.im, 0, 0, 139); + gdata.goldenrod = gdImageColorAllocate(gdata.im, 234, 234, 174); + gdata.goldenrod2 = gdImageColorAllocate(gdata.im, 207, 181, 59); + + if(strcmp(GraphDaysBytesBarColor,"orange") == 0) { + gdata.color1 = gdImageColorAllocate(gdata.im, 255, 233, 142); + gdata.color2 = gdImageColorAllocate(gdata.im, 220, 163, 72); + gdata.color3 = gdImageColorAllocate(gdata.im, 255, 198, 107); + } + else if(strcmp(GraphDaysBytesBarColor,"blue") == 0) { + gdata.color1 = gdImageColorAllocate(gdata.im, 62, 80, 167); + gdata.color2 = gdImageColorAllocate(gdata.im, 40, 51, 101); + gdata.color3 = gdImageColorAllocate(gdata.im, 57, 73, 150); + } + else if(strcmp(GraphDaysBytesBarColor,"green") == 0) { + gdata.color1 = gdImageColorAllocate(gdata.im,120,166,129); + gdata.color2 = gdImageColorAllocate(gdata.im,84,113,82); + gdata.color3 = gdImageColorAllocate(gdata.im,158,223,167); + } + else if(strcmp(GraphDaysBytesBarColor,"yellow") == 0) { + gdata.color1 = gdImageColorAllocate(gdata.im,185,185,10); + gdata.color2 = gdImageColorAllocate(gdata.im,111,111,10); + gdata.color3 = gdImageColorAllocate(gdata.im,166,166,10); + } + else if(strcmp(GraphDaysBytesBarColor,"brown") == 0) { + gdata.color1 = gdImageColorAllocate(gdata.im,97,45,27); + gdata.color2 = gdImageColorAllocate(gdata.im,60,30,20); + gdata.color3 = gdImageColorAllocate(gdata.im,88,41,26); + } + else if(strcmp(GraphDaysBytesBarColor,"red") == 0){ + gdata.color1 = gdImageColorAllocate(gdata.im,185,10,10); + gdata.color2 = gdImageColorAllocate(gdata.im,111,10,10); + gdata.color3 = gdImageColorAllocate(gdata.im,166,10,10); + } else { + debuga(_("Unknown color \"%s\" requested for the graph. Using orange instead\n"),GraphDaysBytesBarColor); + gdata.color1 = gdImageColorAllocate(gdata.im, 255, 233, 142); + gdata.color2 = gdImageColorAllocate(gdata.im, 220, 163, 72); + gdata.color3 = gdImageColorAllocate(gdata.im, 255, 198, 107); + } + + // rectangle around the image + gdImageRectangle(gdata.im, 0, 0, ImgXSize-1, ImgYSize-1, gdata.dimgray); + // backtround of the graph + gdImageFilledRectangle(gdata.im, LeftMargin, gdata.TopGraph, gdata.RightGraph, gdata.BottomGraph, gdata.silver); + + // depth of the left Y axis + points[0].x = gdata.LeftGraph-10; + points[0].y = gdata.TopGraph+5; + points[1].x = gdata.LeftGraph-10; + points[1].y = gdata.BottomDepth; + points[2].x = gdata.LeftGraph; + points[2].y = gdata.BottomGraph; + points[3].x = gdata.LeftGraph; + points[3].y = gdata.TopGraph; + gdImageFilledPolygon(gdata.im, points, 4, gdata.gray); + + // depth of the bottom X axis + points[0].x = gdata.LeftGraph; + points[0].y = gdata.BottomGraph; + points[1].x = gdata.LeftGraph-10; + points[1].y = gdata.BottomDepth; + points[2].x = gdata.RightGraph-10; + points[2].y = gdata.BottomDepth; + points[3].x = gdata.RightGraph; + points[3].y = gdata.BottomGraph; + gdImageFilledPolygon(gdata.im, points, 4, gdata.gray); + + // vertical exterior line of the depth + gdImageLine(gdata.im, LeftMargin-10, TopMargin+5, LeftMargin-10, gdata.BottomDepth+ZTickLength, gdata.black); + // horizontal exterior line of the depth + gdImageLine(gdata.im, LeftMargin-10-ZTickLength, gdata.BottomDepth, gdata.RightGraph-10, gdata.BottomDepth, gdata.black); + // diagonal line between the two depths + gdImageLine(gdata.im, LeftMargin-10, gdata.BottomDepth, LeftMargin, gdata.BottomGraph, gdata.black); + // vertical left line of the graph + gdImageLine(gdata.im, LeftMargin, gdata.BottomGraph, LeftMargin, gdata.TopGraph, gdata.black); + // horizontal bottom line of the graph + gdImageLine(gdata.im, LeftMargin, gdata.BottomGraph, gdata.RightGraph, gdata.BottomGraph, gdata.black); + // vertical right line of the graph + gdImageLine(gdata.im, gdata.RightGraph, gdata.TopGraph, gdata.RightGraph, gdata.BottomGraph, gdata.black); + // diagonal line to close the right of the bottom depth + gdImageLine(gdata.im, gdata.RightGraph-10, gdata.BottomDepth, gdata.RightGraph, gdata.BottomGraph, gdata.black); + + // Y axis ticks + greport_draw_yaxis(pdata,&gdata); + + // X axis ticks and labels + for(y=1; y<=pdata->npoints; y++) { + x=gdata.LeftGraph-10+(int)((double)y*gdata.XScale+0.5); + gdImageLine(gdata.im, x, gdata.BottomDepth, x, gdata.BottomDepth+TickLength, gdata.dimgray); + sprintf(s,"%02d",y); + Sarg_gdImageStringFT(&gdata,gdata.dimgray,GraphFont,7,0.0,x,gdata.BottomDepth+TickLength+1,s,TRP_TopCenter); + } + + t = time(NULL); + local = localtime(&t); + if(DateFormat[0]=='u') + strftime(ftime, sizeof(ftime), "%b/%d/%Y %H:%M", local); + if(DateFormat[0]=='e') + strftime(ftime, sizeof(ftime), "%d/%b/%Y-%H:%M", local); + + x=ImgXSize*5/12; + Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,7,0.0,ImgXSize-10,ImgYSize-10,ftime,TRP_BottomRight); + if(ShowSargInfo) Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,10,0.0,x,15,_("SARG, "),TRP_BottomRight); + Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,10,0.0,x,15,Title,TRP_BottomLeft); + sprintf(warea,_("Period: %s"),period.text); + Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,9,0.0,x,27,warea,TRP_BottomLeft); + sprintf(warea,_("User: %s"),uinfo->label); + Sarg_gdImageStringFT(&gdata,gdata.darkblue,GraphFont,9,0.0,x,38,warea,TRP_BottomLeft); + + Sarg_gdImageStringFT(&gdata,gdata.black,GraphFont,10,3.141592/2,15,ImgYSize/2,pdata->YLabel,TRP_CenterLeft); + Sarg_gdImageStringFT(&gdata,gdata.black,GraphFont,10,0.0,ImgXSize/2,ImgYSize-20,pdata->XLabel,TRP_BottomCenter); + + logpmin=(pdata->ytype==PTG_LogBin && pdata->ymin>0.) ? log(pdata->ymin) : 0.; + for (day=0 ; daynpoints ; day++) { + if (pdata->datapoints[day]>0) { + x1=gdata.LeftGraph-10+(int)((double)(day+1)*gdata.XScale+0.5); + switch (pdata->ytype) + { + case PTG_LinBin: + yval=(double)pdata->datapoints[day]; + if (yvalymin) + yval=0.; + else if (yval>pdata->ymax) + yval=pdata->ymax; + else + yval-=pdata->ymin; + greport_formatbin(pdata->datapoints[day],1,blabel,sizeof(blabel)); + break; + case PTG_LogBin: + yval=(double)pdata->datapoints[day]; + if (yval<=pdata->ymin) + yval=0.; + else if (yval>pdata->ymax) + yval=log(pdata->ymax)-logpmin; + else + yval=log(yval)-logpmin; + greport_formatbin(pdata->datapoints[day],1,blabel,sizeof(blabel)); + break; + case PTG_Time: + { + int t; + + yval=(double)pdata->datapoints[day]; + if (yvalymin) + yval=0.; + else if (yval>pdata->ymax) + yval=pdata->ymax; + else + yval-=pdata->ymin; + t=(int)(pdata->datapoints[day]/60000.); + snprintf(blabel,sizeof(blabel),"%d:%02d",t/60,t%60); + break; + } + default: + yval=-1.; + break; + } + if (yval>=0.) bar(&gdata,x1,yval,blabel); + } + } + + if (snprintf(graph,sizeof(graph),"%s/%s/%s",outdirname,uinfo->filename,pdata->pngfile)>=sizeof(graph)) { + debuga(_("user name too long for %s/%s/%s\n"),outdirname,uinfo->filename,pdata->pngfile); + exit(EXIT_FAILURE); + } + if((pngout=fopen(graph,"wb"))==NULL) { + debuga(_("(grepday) Cannot open log file %s\n"),graph); + exit(EXIT_FAILURE); + } + gdImagePng(gdata.im, pngout); + fclose(pngout); + gdImageDestroy(gdata.im); + + if (gdata.string) free(gdata.string); } #endif //HAVE_GD @@ -633,16 +633,16 @@ static void greport_plot(const struct userinfostruct *uinfo,struct PlotStruct *p void greport_prepare(void) { #ifdef HAVE_GD - if(access(GraphFont, R_OK) != 0) { - debuga(_("(grepday) Fontname %s not found\n"),GraphFont); - exit(EXIT_FAILURE); - } + if(access(GraphFont, R_OK) != 0) { + debuga(_("(grepday) Fontname %s not found\n"),GraphFont); + exit(EXIT_FAILURE); + } #ifdef USE_ICONV - localtoutf = iconv_open ("UTF-8", CharSet); - if (localtoutf==(iconv_t)-1) { - debuga(_("(grepday) iconv cannot convert from %s to UTF-8 - %s\n"),CharSet,strerror(errno)); - } + localtoutf = iconv_open ("UTF-8", CharSet); + if (localtoutf==(iconv_t)-1) { + debuga(_("(grepday) iconv cannot convert from %s to UTF-8 - %s\n"),CharSet,strerror(errno)); + } #endif #endif //HAVE_GD @@ -651,126 +651,126 @@ void greport_prepare(void) void greport_day(const struct userinfostruct *uinfo) { #ifdef HAVE_GD - FILE *fp_in, *fp_ou; - char wdirname[MAXLEN]; - char buf[MAXLEN]; - char colsep; - int day; - long long int llday; - long long int bytes; - long long int elap; - long long int bytespoints[31]; - long long int elappoints[31]; - struct getwordstruct gwarea; - struct PlotStruct pdata; - - if (snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename)>=sizeof(wdirname)) { - debuga(_("user name too long for %s/%s.day\n"),tmp,uinfo->filename); - exit(EXIT_FAILURE); - } - if(access(wdirname, R_OK) != 0) { - return; - } - if(!Graphs || GraphFont[0]=='\0') { - unlink(wdirname); - return; - } - - if((fp_in=fopen(wdirname,"r"))==NULL) { - debuga(_("(grepday) Cannot open log file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - - memset(bytespoints,0,sizeof(bytespoints)); - memset(elappoints,0,sizeof(elappoints)); - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - fixendofline(buf); - getword_start(&gwarea,buf); - if (getword_atoll(&llday,&gwarea,'/')<0) { - debuga(_("Invalid date in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - day=(int)llday; - if (day<1 || day>31) continue; - if (getword_skip(20,&gwarea,'\t')<0 || getword_skip(20,&gwarea,'\t')<0) { - debuga(_("Invalid entry in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - if ((datetimeby & DATETIME_BYTE)!=0) { - colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; - if (getword_atoll(&bytes,&gwarea,'\t')<0) { - debuga(_("Invalid number of bytes in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - bytespoints[day-1]+=bytes; - } - if ((datetimeby & DATETIME_ELAP)!=0) { - if (getword_atoll(&elap,&gwarea,'\0')<0) { - debuga(_("Invalid elapsed time in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - elappoints[day-1]+=elap; - } - } - fclose(fp_in); - unlink(wdirname); - - if (snprintf(wdirname,sizeof(wdirname),"%s/%s/graph.html",outdirname,uinfo->filename)>=sizeof(wdirname)) { - debuga(_("user name too long for %s/%s/%s\n"),outdirname,uinfo->filename,"graph.html"); - exit(EXIT_FAILURE); - } - if ((fp_ou=fopen(wdirname,"wt"))==NULL) { - debuga(_("(grepday) Cannot open output file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - write_html_head(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Graph report"),HTML_JS_NONE); - - fputs("\n", fp_ou); - if((datetimeby & DATETIME_BYTE)!=0) { - memset(&pdata,0,sizeof(pdata)); - pdata.datapoints=bytespoints; - pdata.npoints=31; - pdata.XLabel=_("DAYS"); - pdata.ymin=50LL*1000LL; - pdata.ymax=5LL*1000LL*1000LL*1000LL; - pdata.ytype=PTG_LogBin; - pdata.YLabel=_("BYTES"); - pdata.pngfile="graph_day_byte.png"; - greport_plot(uinfo,&pdata); - fprintf(fp_ou,"\n",pdata.pngfile); - } - if((datetimeby & DATETIME_ELAP)!=0) { - memset(&pdata,0,sizeof(pdata)); - pdata.datapoints=elappoints; - pdata.npoints=31; - pdata.XLabel=_("DAYS"); - pdata.ymin=0; - pdata.ymax=86400000; - pdata.ytype=PTG_Time; - pdata.YLabel=_("ELAPSED TIME"); - pdata.pngfile="graph_day_elap.png"; - greport_plot(uinfo,&pdata); - fprintf(fp_ou,"\n",pdata.pngfile); - } - fputs("
\"B\"
\"E\"
\n",fp_ou); - - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),wdirname); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno)); + FILE *fp_in, *fp_ou; + char wdirname[MAXLEN]; + char buf[MAXLEN]; + char colsep; + int day; + long long int llday; + long long int bytes; + long long int elap; + long long int bytespoints[31]; + long long int elappoints[31]; + struct getwordstruct gwarea; + struct PlotStruct pdata; + + if (snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename)>=sizeof(wdirname)) { + debuga(_("user name too long for %s/%s.day\n"),tmp,uinfo->filename); + exit(EXIT_FAILURE); + } + if(access(wdirname, R_OK) != 0) { + return; + } + if(!Graphs || GraphFont[0]=='\0') { + unlink(wdirname); + return; + } + + if((fp_in=fopen(wdirname,"r"))==NULL) { + debuga(_("(grepday) Cannot open log file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + + memset(bytespoints,0,sizeof(bytespoints)); + memset(elappoints,0,sizeof(elappoints)); + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + fixendofline(buf); + getword_start(&gwarea,buf); + if (getword_atoll(&llday,&gwarea,'/')<0) { + debuga(_("Invalid date in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + day=(int)llday; + if (day<1 || day>31) continue; + if (getword_skip(20,&gwarea,'\t')<0 || getword_skip(20,&gwarea,'\t')<0) { + debuga(_("Invalid entry in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + if ((datetimeby & DATETIME_BYTE)!=0) { + colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; + if (getword_atoll(&bytes,&gwarea,'\t')<0) { + debuga(_("Invalid number of bytes in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + bytespoints[day-1]+=bytes; + } + if ((datetimeby & DATETIME_ELAP)!=0) { + if (getword_atoll(&elap,&gwarea,'\0')<0) { + debuga(_("Invalid elapsed time in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + elappoints[day-1]+=elap; + } + } + fclose(fp_in); + unlink(wdirname); + + if (snprintf(wdirname,sizeof(wdirname),"%s/%s/graph.html",outdirname,uinfo->filename)>=sizeof(wdirname)) { + debuga(_("user name too long for %s/%s/%s\n"),outdirname,uinfo->filename,"graph.html"); + exit(EXIT_FAILURE); + } + if ((fp_ou=fopen(wdirname,"wt"))==NULL) { + debuga(_("(grepday) Cannot open output file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + write_html_head(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Graph report"),HTML_JS_NONE); + + fputs("\n", fp_ou); + if((datetimeby & DATETIME_BYTE)!=0) { + memset(&pdata,0,sizeof(pdata)); + pdata.datapoints=bytespoints; + pdata.npoints=31; + pdata.XLabel=_("DAYS"); + pdata.ymin=50LL*1000LL; + pdata.ymax=5LL*1000LL*1000LL*1000LL; + pdata.ytype=PTG_LogBin; + pdata.YLabel=_("BYTES"); + pdata.pngfile="graph_day_byte.png"; + greport_plot(uinfo,&pdata); + fprintf(fp_ou,"\n",pdata.pngfile); + } + if((datetimeby & DATETIME_ELAP)!=0) { + memset(&pdata,0,sizeof(pdata)); + pdata.datapoints=elappoints; + pdata.npoints=31; + pdata.XLabel=_("DAYS"); + pdata.ymin=0; + pdata.ymax=86400000; + pdata.ytype=PTG_Time; + pdata.YLabel=_("ELAPSED TIME"); + pdata.pngfile="graph_day_elap.png"; + greport_plot(uinfo,&pdata); + fprintf(fp_ou,"\n",pdata.pngfile); + } + fputs("
\"B\"
\"E\"
\n",fp_ou); + + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),wdirname); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno)); #endif //HAVE_GD - return; + return; } void greport_cleanup(void) { #ifdef HAVE_GD - gdFontCacheShutdown(); + gdFontCacheShutdown(); #ifdef USE_ICONV - if (localtoutf!=(iconv_t)-1) - iconv_close (localtoutf); + if (localtoutf!=(iconv_t)-1) + iconv_close (localtoutf); #endif #endif //HAVE_GD } diff --git a/html.c b/html.c index bc41751..a6beae5 100644 --- a/html.c +++ b/html.c @@ -29,608 +29,608 @@ void htmlrel(void) { - DIR *dirp; - FILE *fp_in, *fp_ou, *fp_ip, *fp_ip2, *fp_usr; - - struct dirent *direntp; - long long int nnbytes=0, unbytes=0, tnbytes=0, totbytes=0, totbytes2=0; - long long int totelap=0, totelap2=0, nnelap=0, unelap=0, tnelap=0; - long long int incache=0, oucache=0, tnincache=0, tnoucache=0, twork=0; - long long int ltemp; - long long int ntotuser; - long long int userbytes, userelap; - char *buf; - char arqin[MAXLEN], arqou[MAXLEN], arqper[MAXLEN], arqip[MAXLEN]; - char *url, tmsg[50], csort[MAXLEN]; - char user[MAXLEN], duser[MAXLEN]; - char user_ip[MAXLEN], olduserip[MAXLEN], tmp2[MAXLEN], tmp3[MAXLEN]; - char denied_report[255]; - char *str; - char warea[MAXLEN]; - char totuser[8]; - char tmp6[MAXLEN]; - char *user_url; - long long int tnacc=0, ttnacc=0, unacc=0; - double perc=0, perc2=0, ouperc=0, inperc=0; - int count; - int cstatus; - const char txtext[]=".txt"; - const char *sort_field; - const char *sort_order; - int dlen; - char siteind[MAX_TRUNCATED_URL]; - struct getwordstruct gwarea; - longline line,line1; - struct generalitemstruct item; - const struct userinfostruct *uinfo; - - if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; - - strcpy(tmp2,tmp); - strcat(tmp2,"/sargtmp.unsort"); - - strcpy(tmp3,tmp); - strcat(tmp3,"/sargtmp.log"); - - snprintf(arqper,sizeof(arqper),"%s/sarg-general",outdirname); - if ((fp_in = fopen(arqper, "r")) == 0){ - debuga(_("(html2) Cannot open file %s\n"),arqper); - exit(EXIT_FAILURE); - } - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read file %s\n"),arqper); - exit(EXIT_FAILURE); - } - ttnacc=0; - totbytes=0; - totelap=0; - while((buf=longline_read(fp_in,line))!=NULL) { - ger_read(buf,&item,arqper); - if(item.total) { - ttnacc+=item.nacc; - totbytes+=item.nbytes; - totelap+=item.nelap; - } - } - fclose(fp_in); - longline_destroy(&line); - - snprintf(arqper,sizeof(arqper),"%s/sarg-users",outdirname); - if ((fp_in = fopen(arqper, "r")) == 0){ - debuga(_("(html11) Cannot open file %s\n"),arqper); - exit(EXIT_FAILURE); - } - if (!fgets(totuser,sizeof(totuser),fp_in)) { - debuga(_("(html11) read error in %s\n"),arqper); - exit(EXIT_FAILURE); - } - fclose(fp_in); - ntotuser=my_atoll(totuser); - if (ntotuser<=0) ntotuser=1; - - greport_prepare(); - - sort_labels(&sort_field,&sort_order); - - if ((dirp = opendir(tmp)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); - exit(EXIT_FAILURE); - } - while ( (direntp = readdir( dirp )) != NULL ) { - dlen=strlen(direntp->d_name)-(sizeof(txtext)-1); - if (dlen<0) continue; - if(strcmp(direntp->d_name+dlen,txtext) != 0) - continue; - - count=1; - if (dlen>0) { - if (dlen>=sizeof(user)) continue; - strncpy(user,direntp->d_name,dlen); - user[dlen]=0; - } else { - user[0]=0; - } - - uinfo=userinfo_find_from_file(user); - if (!uinfo) { - debuga(_("Unknown user ID %s in directory %s\n"),user,tmp); - exit(EXIT_FAILURE); - } - str=strrchr(user,'.'); - if (str) - strcpy(denied_report,str+1); - else - strcpy(denied_report,user); - - if (snprintf(warea,sizeof(warea),"%s/%s",outdirname,user)>=sizeof(warea)) { - debuga(_("Destination directory too long: %s/%s\n"),outdirname,user); - exit(EXIT_FAILURE); - } - mkdir(warea,0755); - - report_day(uinfo); - greport_day(uinfo); - - if (snprintf(arqin,sizeof(arqin),"%s/%s",tmp,direntp->d_name)>=sizeof(arqin)) { - debuga(_("Input file name too long: %s/%s\n"),tmp,direntp->d_name); - exit(EXIT_FAILURE); - } - if (snprintf(arqou,sizeof(arqou),"%s/%s/%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqou)) { - debuga(_("Output file name too long: %s/%s/%s.html\n"),outdirname,uinfo->filename,uinfo->filename); - exit(EXIT_FAILURE); - } - if (snprintf(duser,sizeof(duser),"%s/%s/denied_%s.html",tmp,direntp->d_name,denied_report)>=sizeof(duser)) { - debuga(_("File name too long: %s/%s/denied_%s.html\n"),tmp,direntp->d_name,denied_report); - exit(EXIT_FAILURE); - } - if(access(duser, R_OK) != 0) - denied_report[0]='\0'; - - if ((fp_in = fopen(arqin, "r")) == 0){ - debuga(_("(html3) Cannot open file %s\n"),arqin); - exit(EXIT_FAILURE); - } - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read file %s\n"),arqin); - exit(EXIT_FAILURE); - } - - tnacc=0; - tnbytes=0; - tnelap=0; - tnincache=0; - tnoucache=0; - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword_atoll(<emp,&gwarea,'\t')<0) { - debuga(_("There is a broken number of access in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - tnacc+=ltemp; - if (getword_atoll(<emp,&gwarea,'\t')<0) { - debuga(_("There is a broken downloaded size in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - tnbytes+=ltemp; - if (getword_ptr(NULL,NULL,&gwarea,'\t')<0) { - debuga(_("There is a broken url in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - if (getword_skip(MAXLEN,&gwarea,'\t')<0) { - debuga(_("There is a broken access code in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - if (getword_atoll(<emp,&gwarea,'\t')<0) { - debuga(_("There is a broken elapsed time in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - tnelap+=ltemp; - if (getword_atoll(<emp,&gwarea,'\t')<0) { - debuga(_("There is a broken in-cache volume in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - tnincache+=ltemp; - if (getword_atoll(<emp,&gwarea,'\n')<0) { - debuga(_("There is a broken out-cache volume in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - tnoucache+=ltemp; - } - - rewind(fp_in); - - if ((fp_ou = fopen(arqou, "w")) == 0){ - debuga(_("(html5) Cannot open file %s\n"),arqou); - exit(EXIT_FAILURE); - } - - write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("User report"),HTML_JS_SORTTABLE); - fprintf(fp_ou,"%s: %s\n",_("Period"),period.html); - fprintf(fp_ou,"%s: %s\n",_("User"),uinfo->label); - fputs("",fp_ou); - fprintf(fp_ou,_("Sort: %s, %s"),sort_field,sort_order); - fputs("\n",fp_ou); - fprintf(fp_ou,"%s\n",_("User report")); - close_html_header(fp_ou); - - fputs("
\n",fp_ou); - - /*! \bug This line lays before the header of the table and is therefore invalid - HTML. It will also break the sorttable script. Beside, I suspect it serves no purpose. - */ - if(denied_report[0]!='\0') { - fprintf(fp_ou,"\n",denied_report,_("SmartFilter"),_("Report")); - } - - fputs("",_("ACCESSED SITE")); - - if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) - fprintf(fp_ou,"",_("CONNECT")); - if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) - fprintf(fp_ou,"",_("BYTES")); - if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) - fprintf(fp_ou,"",_("BYTES")); - if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) - fprintf(fp_ou,"",_("IN-CACHE-OUT")); - if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) - fprintf(fp_ou,"",_("ELAPSED TIME")); - if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) - fprintf(fp_ou,"",_("MILLISEC")); - if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) - fprintf(fp_ou,"",_("TIME")); - - fputs("\n",fp_ou); - - if(debug) { - debuga(_("Making report: %s\n"),uinfo->id); - } - - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword_atoll(&twork,&gwarea,'\t')<0) { - debuga(_("There is a broken number of access in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - if (getword_atoll(&nnbytes,&gwarea,'\t')<0) { - debuga(_("There is a broken number of bytes in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("There is a broken url in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - if (getword(tmsg,sizeof(tmsg),&gwarea,'\t')<0) { - debuga(_("There is a broken access code in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - if (getword_atoll(&nnelap,&gwarea,'\t')<0) { - debuga(_("There is a broken elapsed time in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - if (getword_atoll(&incache,&gwarea,'\t')<0) { - debuga(_("There is a broken in cache column in file %s\n"),arqin); - exit(EXIT_FAILURE); - } - if (getword_atoll(&oucache,&gwarea,'\n')<0) { - debuga(_("There is a broken out of cache column in file %s (%d)\n"),arqin,__LINE__); - exit(EXIT_FAILURE); - } - - if(UserReportLimit<=0 || count<=UserReportLimit) { - fputs("",fp_ou); - - if(IndexTree == INDEX_TREE_DATE) - sprintf(tmp6,"../%s",ImageFile); - else - strcpy(tmp6,"../../images"); - - if((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0) { - url_to_file(url,siteind,sizeof(siteind)); - fprintf(fp_ou,"",uinfo->filename,siteind,tmp6,_("date/time report")); - } else { - fprintf(fp_ou,""); - } - - if(Privacy) - fprintf(fp_ou,"",PrivacyStringColor,PrivacyString); - else { - fputs("",fp_ou); - } - - if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) { - fputs("",fixnum(twork,1)); - } - if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) { - fputs("",fixnum(nnbytes,1)); - } - if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) { - perc=(tnbytes) ? nnbytes * 100. / tnbytes : 0.; - fprintf(fp_ou,"",perc); - } - if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) { - inperc=(nnbytes) ? incache * 100. / nnbytes : 0.; - ouperc=(nnbytes) ? oucache * 100. / nnbytes : 0.; - fprintf(fp_ou,"",inperc,ouperc); - } - if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) { - fputs("",buildtime(nnelap)); - } - if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) { - fputs("",fixnum2(nnelap,1)); - } - if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) { - perc2=(tnelap) ? nnelap * 100. / tnelap : 0.; - fprintf(fp_ou,"",perc2); - } - - if(strncmp(tmsg,"OK",2) != 0) - fprintf(fp_ou,"",_("DENIED")); - - fputs("\n",fp_ou); - count++; - } else if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0) { - url_to_file(url,siteind,sizeof(siteind)); - snprintf(tmp2,sizeof(tmp2),"%s/%s/tt%s-%s.html",outdirname,uinfo->filename,uinfo->filename,siteind); - if (unlink(tmp2)!=0) { - debuga(_("Cannot delete unused file \"%s\" - %s\n"),tmp2,strerror(errno)); - } - } - - if(iprel) { - if (snprintf(arqip,sizeof(arqip),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(arqip)) { - debuga(_("File name too long: %s/%s.ip\n"),tmp,uinfo->filename); - exit(EXIT_FAILURE); - } - - if ((fp_ip = fopen(arqip, "r")) == 0){ - debuga(_("(html6) Cannot open file %s\n"),arqip); - exit(EXIT_FAILURE); - } - - if ((fp_ip2 = MY_FOPEN(tmp2, "a")) == 0){ - debuga(_("(html7) Cannot open file %s\n"),tmp2); - exit(EXIT_FAILURE); - } - - if ((line1=longline_create())==NULL) { - debuga(_("Not enough memory to read file %s\n"),arqip); - exit(EXIT_FAILURE); - } - while((buf=longline_read(fp_ip,line1))!=NULL) { - getword_start(&gwarea,buf); - if (getword(user_ip,sizeof(user_ip),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken user IP in your %s file\n"),tmp3); - exit(EXIT_FAILURE); - } - if (getword_ptr(buf,&user_url,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken url in your %s file\n"),tmp3); - exit(EXIT_FAILURE); - } - if (strncmp(user_url,url,strlen(url))!=0) continue; - if (getword_skip(15,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken day in your %s file\n"),tmp3); - exit(EXIT_FAILURE); - } - if (getword_skip(15,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken time in your %s file\n"),tmp3); - exit(EXIT_FAILURE); - } - if (getword_atoll(&userbytes,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken size in your %s file\n"),tmp3); - exit(EXIT_FAILURE); - } - if (getword_atoll(&userelap,&gwarea,'\0')<0) { - debuga(_("Maybe you have a broken elapsed time in your %s file\n"),tmp3); - exit(EXIT_FAILURE); - } - fprintf(fp_ip2,"%s\t%"PRIu64"\t%"PRIu64"\n",user_ip,(uint64_t)userbytes,(uint64_t)userelap); - } - longline_destroy(&line1); - - fclose(fp_ip); - fclose(fp_ip2); - - sprintf(csort,"sort -n -T \"%s\" -k 1,1 -k 2,2 -o \"%s\" \"%s\"",tmp,tmp3,tmp2); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - if ((fp_ip = MY_FOPEN(tmp3, "r")) == 0) { - debuga(_("(html8) Cannot open file %s\n"),tmp3); - exit(EXIT_FAILURE); - } - - olduserip[0]='\0'; - - if ((line1=longline_create())==NULL) { - debuga(_("Not enough memory to read file %s\n"),arqip); - exit(EXIT_FAILURE); - } - while((buf=longline_read(fp_ip,line1))!=NULL) { - getword_start(&gwarea,buf); - if (getword(user_ip,sizeof(user_ip),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken user IP in your %s file\n"),tmp3); - exit(EXIT_FAILURE); - } - if (getword_atoll(&userbytes,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken size in your %s file\n"),tmp3); - exit(EXIT_FAILURE); - } - if (getword_atoll(&userelap,&gwarea,'\0')<0) { - debuga(_("Maybe you have a broken elapsed time in your %s file\n"),tmp3); - exit(EXIT_FAILURE); - } - if(strcmp(user_ip,olduserip) != 0) { - if (olduserip[0]!='\0') { - fprintf(fp_ou,"",olduserip); - if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) - fputs("",fp_ou); - if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) - fprintf(fp_ou,"",fixnum(unbytes,1)); - if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) - fputs("",fp_ou); - if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) - fputs("",fp_ou); - if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) - fprintf(fp_ou,"",buildtime(unelap)); - if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) - fprintf(fp_ou,"",fixnum2(unelap,1)); - fputs("\n",fp_ou); - } - - strcpy(olduserip,user_ip); - unacc=0; - unbytes=0; - unelap=0; - } - - unbytes+=userbytes; - unelap+=userelap; - } - - fclose(fp_ip); - longline_destroy(&line1); - - unlink(tmp2); - unlink(tmp3); - - if (olduserip[0]!='\0') { - fprintf(fp_ou,"",olduserip); - if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) - fputs("",fp_ou); - if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) - fprintf(fp_ou,"",fixnum(unbytes,1)); - if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) - fputs("",fp_ou); - if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) - fputs("",fp_ou); - if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) - fprintf(fp_ou,"",buildtime(unelap)); - if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) - fprintf(fp_ou,"",fixnum2(unelap,1)); - fputs("\n",fp_ou); - } - } - - unacc=0; - unbytes=0; - unelap=0; - } - - fclose(fp_in); - longline_destroy(&line); - - if(iprel) - unlink(arqip); - unlink(arqin); - - if ((UserReportFields & (USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE)) != 0) - fputs("",fp_ou); - - if((UserReportFields & USERREPORTFIELDS_TOTAL) != 0) { - fprintf(fp_ou,"",_("TOTAL")); - if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) - fprintf(fp_ou,"",fixnum(tnacc,1)); - if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) - fprintf(fp_ou,"",fixnum(tnbytes,1)); - if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) { - perc=(totbytes) ? tnbytes *100. / totbytes :0.; - fprintf(fp_ou,"",perc); - } - if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) { - inperc=(tnbytes) ? tnincache * 100. / tnbytes : 0.; - ouperc=(tnbytes) ? tnoucache * 100. / tnbytes : 0.; - fprintf(fp_ou,"",inperc,ouperc); - } - if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) - fprintf(fp_ou,"",buildtime(tnelap)); - if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) - fprintf(fp_ou,"",fixnum2(tnelap,1)); - if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) { - perc2=(totelap) ? tnelap * 100. / totelap : 0.; - fprintf(fp_ou,"",perc2); - } - fputs("\n",fp_ou); - } - - if(PerUserLimit > 0) { - if(tnbytes > (PerUserLimit*1000000)) { - limit_flag=0; - if(access(PerUserLimitFile, R_OK) == 0) { - if((fp_usr = fopen(PerUserLimitFile, "r")) == 0) { - debuga(_("(html9) Cannot open file %s\n"),PerUserLimitFile); - exit(EXIT_FAILURE); - } - while(fgets(tmp6,sizeof(tmp6),fp_usr)!=NULL) { - fixendofline(tmp6); - if(strcmp(tmp6,uinfo->label) == 0) { - limit_flag=1; - break; - } - } - fclose(fp_usr); - } - - if(!limit_flag) { - if((fp_usr = fopen(PerUserLimitFile, "a")) == 0) { - debuga(_("(html10) Cannot open file %s\n"),PerUserLimitFile); - exit(EXIT_FAILURE); - } - fprintf(fp_usr,"%s\n",uinfo->label); - fclose(fp_usr); - - if(debug) - debuga(_("User %s limit exceeded (%d MB). Added to file %s\n"),uinfo->label,PerUserLimit,PerUserLimitFile); - } - } - } + DIR *dirp; + FILE *fp_in, *fp_ou, *fp_ip, *fp_ip2, *fp_usr; + + struct dirent *direntp; + long long int nnbytes=0, unbytes=0, tnbytes=0, totbytes=0, totbytes2=0; + long long int totelap=0, totelap2=0, nnelap=0, unelap=0, tnelap=0; + long long int incache=0, oucache=0, tnincache=0, tnoucache=0, twork=0; + long long int ltemp; + long long int ntotuser; + long long int userbytes, userelap; + char *buf; + char arqin[MAXLEN], arqou[MAXLEN], arqper[MAXLEN], arqip[MAXLEN]; + char *url, tmsg[50], csort[MAXLEN]; + char user[MAXLEN], duser[MAXLEN]; + char user_ip[MAXLEN], olduserip[MAXLEN], tmp2[MAXLEN], tmp3[MAXLEN]; + char denied_report[255]; + char *str; + char warea[MAXLEN]; + char totuser[8]; + char tmp6[MAXLEN]; + char *user_url; + long long int tnacc=0, ttnacc=0, unacc=0; + double perc=0, perc2=0, ouperc=0, inperc=0; + int count; + int cstatus; + const char txtext[]=".txt"; + const char *sort_field; + const char *sort_order; + int dlen; + char siteind[MAX_TRUNCATED_URL]; + struct getwordstruct gwarea; + longline line,line1; + struct generalitemstruct item; + const struct userinfostruct *uinfo; + + if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; + + strcpy(tmp2,tmp); + strcat(tmp2,"/sargtmp.unsort"); + + strcpy(tmp3,tmp); + strcat(tmp3,"/sargtmp.log"); + + snprintf(arqper,sizeof(arqper),"%s/sarg-general",outdirname); + if ((fp_in = fopen(arqper, "r")) == 0){ + debuga(_("(html2) Cannot open file %s\n"),arqper); + exit(EXIT_FAILURE); + } + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read file %s\n"),arqper); + exit(EXIT_FAILURE); + } + ttnacc=0; + totbytes=0; + totelap=0; + while((buf=longline_read(fp_in,line))!=NULL) { + ger_read(buf,&item,arqper); + if(item.total) { + ttnacc+=item.nacc; + totbytes+=item.nbytes; + totelap+=item.nelap; + } + } + fclose(fp_in); + longline_destroy(&line); + + snprintf(arqper,sizeof(arqper),"%s/sarg-users",outdirname); + if ((fp_in = fopen(arqper, "r")) == 0){ + debuga(_("(html11) Cannot open file %s\n"),arqper); + exit(EXIT_FAILURE); + } + if (!fgets(totuser,sizeof(totuser),fp_in)) { + debuga(_("(html11) read error in %s\n"),arqper); + exit(EXIT_FAILURE); + } + fclose(fp_in); + ntotuser=my_atoll(totuser); + if (ntotuser<=0) ntotuser=1; + + greport_prepare(); + + sort_labels(&sort_field,&sort_order); + + if ((dirp = opendir(tmp)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); + exit(EXIT_FAILURE); + } + while ( (direntp = readdir( dirp )) != NULL ) { + dlen=strlen(direntp->d_name)-(sizeof(txtext)-1); + if (dlen<0) continue; + if(strcmp(direntp->d_name+dlen,txtext) != 0) + continue; + + count=1; + if (dlen>0) { + if (dlen>=sizeof(user)) continue; + strncpy(user,direntp->d_name,dlen); + user[dlen]=0; + } else { + user[0]=0; + } + + uinfo=userinfo_find_from_file(user); + if (!uinfo) { + debuga(_("Unknown user ID %s in directory %s\n"),user,tmp); + exit(EXIT_FAILURE); + } + str=strrchr(user,'.'); + if (str) + strcpy(denied_report,str+1); + else + strcpy(denied_report,user); + + if (snprintf(warea,sizeof(warea),"%s/%s",outdirname,user)>=sizeof(warea)) { + debuga(_("Destination directory too long: %s/%s\n"),outdirname,user); + exit(EXIT_FAILURE); + } + mkdir(warea,0755); + + report_day(uinfo); + greport_day(uinfo); + + if (snprintf(arqin,sizeof(arqin),"%s/%s",tmp,direntp->d_name)>=sizeof(arqin)) { + debuga(_("Input file name too long: %s/%s\n"),tmp,direntp->d_name); + exit(EXIT_FAILURE); + } + if (snprintf(arqou,sizeof(arqou),"%s/%s/%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqou)) { + debuga(_("Output file name too long: %s/%s/%s.html\n"),outdirname,uinfo->filename,uinfo->filename); + exit(EXIT_FAILURE); + } + if (snprintf(duser,sizeof(duser),"%s/%s/denied_%s.html",tmp,direntp->d_name,denied_report)>=sizeof(duser)) { + debuga(_("File name too long: %s/%s/denied_%s.html\n"),tmp,direntp->d_name,denied_report); + exit(EXIT_FAILURE); + } + if(access(duser, R_OK) != 0) + denied_report[0]='\0'; + + if ((fp_in = fopen(arqin, "r")) == 0){ + debuga(_("(html3) Cannot open file %s\n"),arqin); + exit(EXIT_FAILURE); + } + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read file %s\n"),arqin); + exit(EXIT_FAILURE); + } + + tnacc=0; + tnbytes=0; + tnelap=0; + tnincache=0; + tnoucache=0; + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword_atoll(<emp,&gwarea,'\t')<0) { + debuga(_("There is a broken number of access in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + tnacc+=ltemp; + if (getword_atoll(<emp,&gwarea,'\t')<0) { + debuga(_("There is a broken downloaded size in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + tnbytes+=ltemp; + if (getword_ptr(NULL,NULL,&gwarea,'\t')<0) { + debuga(_("There is a broken url in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + if (getword_skip(MAXLEN,&gwarea,'\t')<0) { + debuga(_("There is a broken access code in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + if (getword_atoll(<emp,&gwarea,'\t')<0) { + debuga(_("There is a broken elapsed time in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + tnelap+=ltemp; + if (getword_atoll(<emp,&gwarea,'\t')<0) { + debuga(_("There is a broken in-cache volume in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + tnincache+=ltemp; + if (getword_atoll(<emp,&gwarea,'\n')<0) { + debuga(_("There is a broken out-cache volume in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + tnoucache+=ltemp; + } + + rewind(fp_in); + + if ((fp_ou = fopen(arqou, "w")) == 0){ + debuga(_("(html5) Cannot open file %s\n"),arqou); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("User report"),HTML_JS_SORTTABLE); + fprintf(fp_ou,"\n",_("Period"),period.html); + fprintf(fp_ou,"\n",_("User"),uinfo->label); + fputs("\n",fp_ou); + fprintf(fp_ou,"\n",_("User report")); + close_html_header(fp_ou); + + fputs("
%s %s
%s%s%s%%%s%s%s%s%%%s
\"T\"%s",fp_ou); - if(BlockIt[0]!='\0') { - fprintf(fp_ou," ",tmp6); - } - fputs("",fp_ou); - output_html_string(fp_ou,url,100); - fputs("%s%s%3.2lf%%%3.2lf%%%3.2lf%%%s%s%3.2lf%%%s
%s%s%s%s
%s%s%s%s
%s%s%s%3.2lf%%%3.2lf%%%3.2lf%%%s%s%3.2lf%%
%s: %s
%s: %s
",fp_ou); + fprintf(fp_ou,_("Sort: %s, %s"),sort_field,sort_order); + fputs("
%s
\n",fp_ou); + + /*! \bug This line lays before the header of the table and is therefore invalid + HTML. It will also break the sorttable script. Beside, I suspect it serves no purpose. + */ + if(denied_report[0]!='\0') { + fprintf(fp_ou,"\n",denied_report,_("SmartFilter"),_("Report")); + } + + fputs("",_("ACCESSED SITE")); + + if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) + fprintf(fp_ou,"",_("CONNECT")); + if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) + fprintf(fp_ou,"",_("BYTES")); + if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) + fprintf(fp_ou,"",_("BYTES")); + if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) + fprintf(fp_ou,"",_("IN-CACHE-OUT")); + if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) + fprintf(fp_ou,"",_("ELAPSED TIME")); + if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) + fprintf(fp_ou,"",_("MILLISEC")); + if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) + fprintf(fp_ou,"",_("TIME")); + + fputs("\n",fp_ou); + + if(debug) { + debuga(_("Making report: %s\n"),uinfo->id); + } + + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword_atoll(&twork,&gwarea,'\t')<0) { + debuga(_("There is a broken number of access in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + if (getword_atoll(&nnbytes,&gwarea,'\t')<0) { + debuga(_("There is a broken number of bytes in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + if (getword_ptr(buf,&url,&gwarea,'\t')<0) { + debuga(_("There is a broken url in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + if (getword(tmsg,sizeof(tmsg),&gwarea,'\t')<0) { + debuga(_("There is a broken access code in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + if (getword_atoll(&nnelap,&gwarea,'\t')<0) { + debuga(_("There is a broken elapsed time in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + if (getword_atoll(&incache,&gwarea,'\t')<0) { + debuga(_("There is a broken in cache column in file %s\n"),arqin); + exit(EXIT_FAILURE); + } + if (getword_atoll(&oucache,&gwarea,'\n')<0) { + debuga(_("There is a broken out of cache column in file %s (%d)\n"),arqin,__LINE__); + exit(EXIT_FAILURE); + } + + if(UserReportLimit<=0 || count<=UserReportLimit) { + fputs("",fp_ou); + + if(IndexTree == INDEX_TREE_DATE) + sprintf(tmp6,"../%s",ImageFile); + else + strcpy(tmp6,"../../images"); + + if((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0) { + url_to_file(url,siteind,sizeof(siteind)); + fprintf(fp_ou,"",uinfo->filename,siteind,tmp6,_("date/time report")); + } else { + fprintf(fp_ou,""); + } + + if(Privacy) + fprintf(fp_ou,"",PrivacyStringColor,PrivacyString); + else { + fputs("",fp_ou); + } + + if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) { + fputs("",fixnum(twork,1)); + } + if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) { + fputs("",fixnum(nnbytes,1)); + } + if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) { + perc=(tnbytes) ? nnbytes * 100. / tnbytes : 0.; + fprintf(fp_ou,"",perc); + } + if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) { + inperc=(nnbytes) ? incache * 100. / nnbytes : 0.; + ouperc=(nnbytes) ? oucache * 100. / nnbytes : 0.; + fprintf(fp_ou,"",inperc,ouperc); + } + if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) { + fputs("",buildtime(nnelap)); + } + if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) { + fputs("",fixnum2(nnelap,1)); + } + if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) { + perc2=(tnelap) ? nnelap * 100. / tnelap : 0.; + fprintf(fp_ou,"",perc2); + } + + if(strncmp(tmsg,"OK",2) != 0) + fprintf(fp_ou,"",_("DENIED")); + + fputs("\n",fp_ou); + count++; + } else if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0) { + url_to_file(url,siteind,sizeof(siteind)); + snprintf(tmp2,sizeof(tmp2),"%s/%s/tt%s-%s.html",outdirname,uinfo->filename,uinfo->filename,siteind); + if (unlink(tmp2)!=0) { + debuga(_("Cannot delete unused file \"%s\" - %s\n"),tmp2,strerror(errno)); + } + } + + if(iprel) { + if (snprintf(arqip,sizeof(arqip),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(arqip)) { + debuga(_("File name too long: %s/%s.ip\n"),tmp,uinfo->filename); + exit(EXIT_FAILURE); + } + + if ((fp_ip = fopen(arqip, "r")) == 0){ + debuga(_("(html6) Cannot open file %s\n"),arqip); + exit(EXIT_FAILURE); + } + + if ((fp_ip2 = MY_FOPEN(tmp2, "a")) == 0){ + debuga(_("(html7) Cannot open file %s\n"),tmp2); + exit(EXIT_FAILURE); + } + + if ((line1=longline_create())==NULL) { + debuga(_("Not enough memory to read file %s\n"),arqip); + exit(EXIT_FAILURE); + } + while((buf=longline_read(fp_ip,line1))!=NULL) { + getword_start(&gwarea,buf); + if (getword(user_ip,sizeof(user_ip),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken user IP in your %s file\n"),tmp3); + exit(EXIT_FAILURE); + } + if (getword_ptr(buf,&user_url,&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken url in your %s file\n"),tmp3); + exit(EXIT_FAILURE); + } + if (strncmp(user_url,url,strlen(url))!=0) continue; + if (getword_skip(15,&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken day in your %s file\n"),tmp3); + exit(EXIT_FAILURE); + } + if (getword_skip(15,&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken time in your %s file\n"),tmp3); + exit(EXIT_FAILURE); + } + if (getword_atoll(&userbytes,&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken size in your %s file\n"),tmp3); + exit(EXIT_FAILURE); + } + if (getword_atoll(&userelap,&gwarea,'\0')<0) { + debuga(_("Maybe you have a broken elapsed time in your %s file\n"),tmp3); + exit(EXIT_FAILURE); + } + fprintf(fp_ip2,"%s\t%"PRIu64"\t%"PRIu64"\n",user_ip,(uint64_t)userbytes,(uint64_t)userelap); + } + longline_destroy(&line1); + + fclose(fp_ip); + fclose(fp_ip2); + + sprintf(csort,"sort -n -T \"%s\" -k 1,1 -k 2,2 -o \"%s\" \"%s\"",tmp,tmp3,tmp2); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + if ((fp_ip = MY_FOPEN(tmp3, "r")) == 0) { + debuga(_("(html8) Cannot open file %s\n"),tmp3); + exit(EXIT_FAILURE); + } + + olduserip[0]='\0'; + + if ((line1=longline_create())==NULL) { + debuga(_("Not enough memory to read file %s\n"),arqip); + exit(EXIT_FAILURE); + } + while((buf=longline_read(fp_ip,line1))!=NULL) { + getword_start(&gwarea,buf); + if (getword(user_ip,sizeof(user_ip),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken user IP in your %s file\n"),tmp3); + exit(EXIT_FAILURE); + } + if (getword_atoll(&userbytes,&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken size in your %s file\n"),tmp3); + exit(EXIT_FAILURE); + } + if (getword_atoll(&userelap,&gwarea,'\0')<0) { + debuga(_("Maybe you have a broken elapsed time in your %s file\n"),tmp3); + exit(EXIT_FAILURE); + } + if(strcmp(user_ip,olduserip) != 0) { + if (olduserip[0]!='\0') { + fprintf(fp_ou,"",olduserip); + if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) + fputs("",fp_ou); + if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) + fprintf(fp_ou,"",fixnum(unbytes,1)); + if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) + fputs("",fp_ou); + if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) + fputs("",fp_ou); + if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) + fprintf(fp_ou,"",buildtime(unelap)); + if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) + fprintf(fp_ou,"",fixnum2(unelap,1)); + fputs("\n",fp_ou); + } + + strcpy(olduserip,user_ip); + unacc=0; + unbytes=0; + unelap=0; + } + + unbytes+=userbytes; + unelap+=userelap; + } + + fclose(fp_ip); + longline_destroy(&line1); + + unlink(tmp2); + unlink(tmp3); + + if (olduserip[0]!='\0') { + fprintf(fp_ou,"",olduserip); + if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) + fputs("",fp_ou); + if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) + fprintf(fp_ou,"",fixnum(unbytes,1)); + if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) + fputs("",fp_ou); + if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) + fputs("",fp_ou); + if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) + fprintf(fp_ou,"",buildtime(unelap)); + if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) + fprintf(fp_ou,"",fixnum2(unelap,1)); + fputs("\n",fp_ou); + } + } + + unacc=0; + unbytes=0; + unelap=0; + } + + fclose(fp_in); + longline_destroy(&line); + + if(iprel) + unlink(arqip); + unlink(arqin); + + if ((UserReportFields & (USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE)) != 0) + fputs("",fp_ou); + + if((UserReportFields & USERREPORTFIELDS_TOTAL) != 0) { + fprintf(fp_ou,"",_("TOTAL")); + if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) + fprintf(fp_ou,"",fixnum(tnacc,1)); + if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) + fprintf(fp_ou,"",fixnum(tnbytes,1)); + if((UserReportFields & USERREPORTFIELDS_SETYB) != 0) { + perc=(totbytes) ? tnbytes *100. / totbytes :0.; + fprintf(fp_ou,"",perc); + } + if((UserReportFields & USERREPORTFIELDS_IN_CACHE_OUT) != 0) { + inperc=(tnbytes) ? tnincache * 100. / tnbytes : 0.; + ouperc=(tnbytes) ? tnoucache * 100. / tnbytes : 0.; + fprintf(fp_ou,"",inperc,ouperc); + } + if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) + fprintf(fp_ou,"",buildtime(tnelap)); + if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) + fprintf(fp_ou,"",fixnum2(tnelap,1)); + if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) { + perc2=(totelap) ? tnelap * 100. / totelap : 0.; + fprintf(fp_ou,"",perc2); + } + fputs("\n",fp_ou); + } + + if(PerUserLimit > 0) { + if(tnbytes > (PerUserLimit*1000000)) { + limit_flag=0; + if(access(PerUserLimitFile, R_OK) == 0) { + if((fp_usr = fopen(PerUserLimitFile, "r")) == 0) { + debuga(_("(html9) Cannot open file %s\n"),PerUserLimitFile); + exit(EXIT_FAILURE); + } + while(fgets(tmp6,sizeof(tmp6),fp_usr)!=NULL) { + fixendofline(tmp6); + if(strcmp(tmp6,uinfo->label) == 0) { + limit_flag=1; + break; + } + } + fclose(fp_usr); + } + + if(!limit_flag) { + if((fp_usr = fopen(PerUserLimitFile, "a")) == 0) { + debuga(_("(html10) Cannot open file %s\n"),PerUserLimitFile); + exit(EXIT_FAILURE); + } + fprintf(fp_usr,"%s\n",uinfo->label); + fclose(fp_usr); + + if(debug) + debuga(_("User %s limit exceeded (%d MB). Added to file %s\n"),uinfo->label,PerUserLimit,PerUserLimitFile); + } + } + } // if(indexonly) { // unlink(arqou); // continue; // } - if ((ReportType & REPORT_TYPE_TOPUSERS) != 0 && (UserReportFields & USERREPORTFIELDS_AVERAGE) != 0) { - totbytes2=totbytes/ntotuser; - totelap2=totelap/ntotuser; - - fprintf(fp_ou,"",_("AVERAGE")); - if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) - fprintf(fp_ou,"",fixnum(ttnacc/ntotuser,1)); - if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) - fprintf(fp_ou,"",fixnum(totbytes2,1)); - fprintf(fp_ou,""); - if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) - fprintf(fp_ou,"",buildtime(totelap2)); - if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) - fprintf(fp_ou,"",fixnum2(totelap2,1)); - if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) { - perc2 = (totelap) ? totelap2 * 100. / totelap : 0.; - fprintf(fp_ou,"",perc2); - } - fputs("\n",fp_ou); - } - - if ((UserReportFields & (USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE)) != 0) - fputs("",fp_ou); - - fputs("
%s %s
%s%s%s%%%s%s%s%s%%%s
\"T\"%s",fp_ou); + if(BlockIt[0]!='\0') { + fprintf(fp_ou," ",tmp6); + } + fputs("",fp_ou); + output_html_string(fp_ou,url,100); + fputs("%s%s%3.2lf%%%3.2lf%%%3.2lf%%%s%s%3.2lf%%%s
%s%s%s%s
%s%s%s%s
%s%s%s%3.2lf%%%3.2lf%%%3.2lf%%%s%s%3.2lf%%
%s%s%s%s%s%3.2lf%%
\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),arqou); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),arqou,strerror(errno)); - - htaccess(uinfo); - } - - (void)closedir(dirp); - greport_cleanup(); - - return; + if ((ReportType & REPORT_TYPE_TOPUSERS) != 0 && (UserReportFields & USERREPORTFIELDS_AVERAGE) != 0) { + totbytes2=totbytes/ntotuser; + totelap2=totelap/ntotuser; + + fprintf(fp_ou,"%s",_("AVERAGE")); + if((UserReportFields & USERREPORTFIELDS_CONNECT) != 0) + fprintf(fp_ou,"%s",fixnum(ttnacc/ntotuser,1)); + if((UserReportFields & USERREPORTFIELDS_BYTES) != 0) + fprintf(fp_ou,"%s",fixnum(totbytes2,1)); + fprintf(fp_ou,""); + if((UserReportFields & USERREPORTFIELDS_USED_TIME) != 0) + fprintf(fp_ou,"%s",buildtime(totelap2)); + if((UserReportFields & USERREPORTFIELDS_MILISEC) != 0) + fprintf(fp_ou,"%s",fixnum2(totelap2,1)); + if((UserReportFields & USERREPORTFIELDS_PTIME) != 0) { + perc2 = (totelap) ? totelap2 * 100. / totelap : 0.; + fprintf(fp_ou,"%3.2lf%%",perc2); + } + fputs("\n",fp_ou); + } + + if ((UserReportFields & (USERREPORTFIELDS_TOTAL | USERREPORTFIELDS_AVERAGE)) != 0) + fputs("",fp_ou); + + fputs("\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),arqou); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),arqou,strerror(errno)); + + htaccess(uinfo); + } + + (void)closedir(dirp); + greport_cleanup(); + + return; } diff --git a/index.c b/index.c index 2fc7235..c439b7c 100644 --- a/index.c +++ b/index.c @@ -34,619 +34,619 @@ static void date_index_to_file_index(const char *entry); void make_index(void) { - DIR *dirp; - struct dirent *direntp; - char wdir[MAXLEN]; - - if(LastLog > 0) mklastlog(outdir); - - if(Index == INDEX_NO) { - sprintf(wdir,"%sindex.html",outdir); - if(access(wdir, R_OK) == 0) unlink(wdir); - return; - } - - if(debug) debuga(_("Making index.html\n")); - - // convert any old report hierarchy - if ((dirp = opendir(outdir)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); - exit(EXIT_FAILURE); - } - while ((direntp = readdir( dirp )) != NULL) { - if(isdigit(direntp->d_name[0]) && isdigit(direntp->d_name[1])) { - if(IndexTree == INDEX_TREE_DATE) - file_index_to_date_index(direntp->d_name); - else - date_index_to_file_index(direntp->d_name); - } - } - closedir(dirp); - - if(IndexTree == INDEX_TREE_DATE) { - make_date_index(); - } else { - make_file_index(); - } + DIR *dirp; + struct dirent *direntp; + char wdir[MAXLEN]; + + if(LastLog > 0) mklastlog(outdir); + + if(Index == INDEX_NO) { + sprintf(wdir,"%sindex.html",outdir); + if(access(wdir, R_OK) == 0) unlink(wdir); + return; + } + + if(debug) debuga(_("Making index.html\n")); + + // convert any old report hierarchy + if ((dirp = opendir(outdir)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); + exit(EXIT_FAILURE); + } + while ((direntp = readdir( dirp )) != NULL) { + if(isdigit(direntp->d_name[0]) && isdigit(direntp->d_name[1])) { + if(IndexTree == INDEX_TREE_DATE) + file_index_to_date_index(direntp->d_name); + else + date_index_to_file_index(direntp->d_name); + } + } + closedir(dirp); + + if(IndexTree == INDEX_TREE_DATE) { + make_date_index(); + } else { + make_file_index(); + } } static void make_date_index(void) { - FILE *fp_ou, *fp_ou2, *fp_ou3; - DIR *dirp, *dirp2, *dirp3; - struct dirent *direntp; - struct dirent *direntp2; - struct dirent *direntp3; - char yearindex[MAXLEN]; - char yeardir[MAXLEN]; - char yearnum[10]; - char monthindex[MAXLEN]; - char monthdir[MAXLEN]; - char monthname1[9], monthname2[9]; - char nmonth[30]; - char monthnum[10]; - char dayindex[MAXLEN]; - char daynum[10]; - char title[80]; - int yearsort[150]; - int nyears; - int year; - int monthsort[144]; - int nmonths; - int m1, m2, month; - int daysort[31*31]; - int ndays; - int d1, d2, day; - int i, y, m, d; - int order; - - sprintf(yearindex,"%sindex.html",outdir); - - nyears=0; - if ((dirp = opendir(outdir)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); - exit(EXIT_FAILURE); - } - while ((direntp = readdir( dirp )) != NULL) { - if(strlen(direntp->d_name) > 4 || !isdigit(direntp->d_name[0]) || !isdigit(direntp->d_name[1]) || - !isdigit(direntp->d_name[2]) || !isdigit(direntp->d_name[3])) continue; - year=atoi(direntp->d_name); - if (nyears>=sizeof(yearsort)/sizeof(yearsort[0])) { - /* - If too many years are listed in the directory, we ignore the earliest years. The yearsort array - is big enough to accomodate the most ambitious use of sarg but this safety is added to prevent - a crash should the directory be polluted by other entries. - */ - if (year>yearsort[0]) { - for (i=1 ; iyearsort[i] ; i++) - yearsort[i-1]=yearsort[i]; - yearsort[i-1]=year; - } - } else { - for (i=nyears ; i>0 && year\n\n",fp_ou); - fprintf(fp_ou,"\n",_("YEAR"),_("SIZE")); - for (y=0 ; y0) - year=yearsort[y]; - else - year=yearsort[nyears-1-y]; - sprintf(yearnum,"%04d",year); - fprintf(fp_ou,"\n",yearnum,yearnum,get_size(outdir,yearnum)); - sprintf(yeardir,"%s%s",outdir,yearnum); - // Year dir - nmonths=0; - if ((dirp2 = opendir(yeardir)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),yeardir,strerror(errno)); - exit(EXIT_FAILURE); - } - while ((direntp2 = readdir( dirp2 )) != NULL) { - if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue; - i=-1; - if (sscanf(direntp2->d_name,"%d%n",&m1,&i)!=1 || m1<=0 || m1>12 || i<0) continue; - if (direntp2->d_name[i]=='-') { - if (sscanf(direntp2->d_name+i+1,"%d",&m2)!=1 || m212) continue; - } else if (direntp2->d_name[i]!='\0') { - continue; - } else { - m2=0; - } - if (nmonths>=sizeof(monthsort)/sizeof(monthsort[0])) { - debuga(_("Too many month directories in %s\nSupernumerary entries are ignored\n"),yeardir); - break; - } - month=m1*16+m2; - for (i=nmonths ; i>0 && month
%s%s
%s%s
\n\n",fp_ou2); - fprintf(fp_ou2,"\n",_("YEAR"),_("MONTH")); - for (m=0 ; m0) - month=monthsort[m]; - else - month=monthsort[nmonths-1-m]; - m1=month / 16; - if(month % 16 != 0) { - m2=month % 16; - sprintf(monthnum,"%02d-%02d",m1,m2); - sprintf(monthname1,"%02d",m1); - sprintf(monthname2,"%02d",m2); - name_month(monthname1,sizeof(monthname1)); - name_month(monthname2,sizeof(monthname2)); - sprintf(nmonth,"%s-%s",monthname1,monthname2); - } else { - sprintf(nmonth,"%02d",m1); - sprintf(monthnum,"%02d",m1); - name_month(nmonth,sizeof(nmonth)); - } - fprintf(fp_ou2,"\n",monthnum,yearnum,nmonth); - - sprintf(monthdir,"%s/%s",yeardir,monthnum); - // month dir - ndays=0; - if ((dirp3 = opendir(monthdir)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),monthdir,strerror(errno)); - exit(EXIT_FAILURE); - } - while ((direntp3 = readdir( dirp3 )) != NULL) { - if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue; - i=-1; - if (sscanf(direntp3->d_name,"%d%n",&d1,&i)!=1 || d1<=0 || d1>31 || i<0) continue; - if (direntp3->d_name[i]=='-') { - if (sscanf(direntp3->d_name+i+1,"%d",&d2)!=1 || d231) continue; - } else if (direntp3->d_name[i]!='\0') { - continue; - } else { - d2=0; - } - if (ndays>=sizeof(daysort)/sizeof(daysort[0])) { - debuga(_("Too many day directories in %s\nSupernumerary entries are ignored\n"),monthdir); - break; - } - day=d1*32+d2; - for (i=ndays ; i>0 && day
%s/%s
%s %s
\n\n",fp_ou3); - fprintf(fp_ou3,"\n",_("YEAR"),_("MONTH"),_("DAYS")); - for (d=0 ; d0) - day=daysort[d]; - else - day=daysort[ndays-1-d]; - d1=day / 32; - if(day % 32 != 0) { - d2=day % 32; - sprintf(daynum,"%02d-%02d",d1,d2); - } else { - sprintf(daynum,"%02d",d1); - } - fprintf(fp_ou3,"\n",daynum,yearnum,nmonth,daynum); - } - fputs("
%s/%s/%s
%s %s %s
\n",fp_ou3); - if (write_html_trailer(fp_ou3)<0) - debuga(_("Write error in the index %s\n"),dayindex); - if (fclose(fp_ou3)==EOF) - debuga(_("Failed to close the index file %s - %s\n"),dayindex,strerror(errno)); - } - fputs("\n",fp_ou2); - if (write_html_trailer(fp_ou2)<0) - debuga(_("Write error in the index %s\n"),monthindex); - if (fclose(fp_ou2)==EOF) - debuga(_("Failed to close the index file %s - %s\n"),monthindex,strerror(errno)); - } - - fputs("\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in the index %s\n"),yearindex); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close the index file %s - %s\n"),yearindex,strerror(errno)); + FILE *fp_ou, *fp_ou2, *fp_ou3; + DIR *dirp, *dirp2, *dirp3; + struct dirent *direntp; + struct dirent *direntp2; + struct dirent *direntp3; + char yearindex[MAXLEN]; + char yeardir[MAXLEN]; + char yearnum[10]; + char monthindex[MAXLEN]; + char monthdir[MAXLEN]; + char monthname1[9], monthname2[9]; + char nmonth[30]; + char monthnum[10]; + char dayindex[MAXLEN]; + char daynum[10]; + char title[80]; + int yearsort[150]; + int nyears; + int year; + int monthsort[144]; + int nmonths; + int m1, m2, month; + int daysort[31*31]; + int ndays; + int d1, d2, day; + int i, y, m, d; + int order; + + sprintf(yearindex,"%sindex.html",outdir); + + nyears=0; + if ((dirp = opendir(outdir)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); + exit(EXIT_FAILURE); + } + while ((direntp = readdir( dirp )) != NULL) { + if(strlen(direntp->d_name) > 4 || !isdigit(direntp->d_name[0]) || !isdigit(direntp->d_name[1]) || + !isdigit(direntp->d_name[2]) || !isdigit(direntp->d_name[3])) continue; + year=atoi(direntp->d_name); + if (nyears>=sizeof(yearsort)/sizeof(yearsort[0])) { + /* + If too many years are listed in the directory, we ignore the earliest years. The yearsort array + is big enough to accomodate the most ambitious use of sarg but this safety is added to prevent + a crash should the directory be polluted by other entries. + */ + if (year>yearsort[0]) { + for (i=1 ; iyearsort[i] ; i++) + yearsort[i-1]=yearsort[i]; + yearsort[i-1]=year; + } + } else { + for (i=nyears ; i>0 && year\n\n",fp_ou); + fprintf(fp_ou,"\n",_("YEAR"),_("SIZE")); + for (y=0 ; y0) + year=yearsort[y]; + else + year=yearsort[nyears-1-y]; + sprintf(yearnum,"%04d",year); + fprintf(fp_ou,"\n",yearnum,yearnum,get_size(outdir,yearnum)); + sprintf(yeardir,"%s%s",outdir,yearnum); + // Year dir + nmonths=0; + if ((dirp2 = opendir(yeardir)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),yeardir,strerror(errno)); + exit(EXIT_FAILURE); + } + while ((direntp2 = readdir( dirp2 )) != NULL) { + if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue; + i=-1; + if (sscanf(direntp2->d_name,"%d%n",&m1,&i)!=1 || m1<=0 || m1>12 || i<0) continue; + if (direntp2->d_name[i]=='-') { + if (sscanf(direntp2->d_name+i+1,"%d",&m2)!=1 || m212) continue; + } else if (direntp2->d_name[i]!='\0') { + continue; + } else { + m2=0; + } + if (nmonths>=sizeof(monthsort)/sizeof(monthsort[0])) { + debuga(_("Too many month directories in %s\nSupernumerary entries are ignored\n"),yeardir); + break; + } + month=m1*16+m2; + for (i=nmonths ; i>0 && month
%s%s
%s%s
\n\n",fp_ou2); + fprintf(fp_ou2,"\n",_("YEAR"),_("MONTH")); + for (m=0 ; m0) + month=monthsort[m]; + else + month=monthsort[nmonths-1-m]; + m1=month / 16; + if(month % 16 != 0) { + m2=month % 16; + sprintf(monthnum,"%02d-%02d",m1,m2); + sprintf(monthname1,"%02d",m1); + sprintf(monthname2,"%02d",m2); + name_month(monthname1,sizeof(monthname1)); + name_month(monthname2,sizeof(monthname2)); + sprintf(nmonth,"%s-%s",monthname1,monthname2); + } else { + sprintf(nmonth,"%02d",m1); + sprintf(monthnum,"%02d",m1); + name_month(nmonth,sizeof(nmonth)); + } + fprintf(fp_ou2,"\n",monthnum,yearnum,nmonth); + + sprintf(monthdir,"%s/%s",yeardir,monthnum); + // month dir + ndays=0; + if ((dirp3 = opendir(monthdir)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),monthdir,strerror(errno)); + exit(EXIT_FAILURE); + } + while ((direntp3 = readdir( dirp3 )) != NULL) { + if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue; + i=-1; + if (sscanf(direntp3->d_name,"%d%n",&d1,&i)!=1 || d1<=0 || d1>31 || i<0) continue; + if (direntp3->d_name[i]=='-') { + if (sscanf(direntp3->d_name+i+1,"%d",&d2)!=1 || d231) continue; + } else if (direntp3->d_name[i]!='\0') { + continue; + } else { + d2=0; + } + if (ndays>=sizeof(daysort)/sizeof(daysort[0])) { + debuga(_("Too many day directories in %s\nSupernumerary entries are ignored\n"),monthdir); + break; + } + day=d1*32+d2; + for (i=ndays ; i>0 && day
%s/%s
%s %s
\n\n",fp_ou3); + fprintf(fp_ou3,"\n",_("YEAR"),_("MONTH"),_("DAYS")); + for (d=0 ; d0) + day=daysort[d]; + else + day=daysort[ndays-1-d]; + d1=day / 32; + if(day % 32 != 0) { + d2=day % 32; + sprintf(daynum,"%02d-%02d",d1,d2); + } else { + sprintf(daynum,"%02d",d1); + } + fprintf(fp_ou3,"\n",daynum,yearnum,nmonth,daynum); + } + fputs("
%s/%s/%s
%s %s %s
\n",fp_ou3); + if (write_html_trailer(fp_ou3)<0) + debuga(_("Write error in the index %s\n"),dayindex); + if (fclose(fp_ou3)==EOF) + debuga(_("Failed to close the index file %s - %s\n"),dayindex,strerror(errno)); + } + fputs("\n",fp_ou2); + if (write_html_trailer(fp_ou2)<0) + debuga(_("Write error in the index %s\n"),monthindex); + if (fclose(fp_ou2)==EOF) + debuga(_("Failed to close the index file %s - %s\n"),monthindex,strerror(errno)); + } + + fputs("\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in the index %s\n"),yearindex); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close the index file %s - %s\n"),yearindex,strerror(errno)); } static void make_file_index(void) { - #define MAX_CREATION_DATE 15 - FILE *fp_ou; - DIR *dirp; - struct dirent *direntp; - char wdir[MAXLEN]; - char data[80]; - char ftime[128]; - char day[6], mon[8], year[40], hour[10]; - long long int tbytes; - long long int media; - int iyear, imonth, iday, ihour, iminute, isecond, idst; - int nsort; - int nallocated; - int order; - int i; - int tuser; - struct getwordstruct gwarea; - struct sortstruct - { - int year, month, day, sortnum; - char creationdate[MAX_CREATION_DATE]; - char *dirname; - char date[60]; - } **sortlist, *item, **tempsort; - - sprintf(wdir,"%sindex.html",outdir); - - order=(strcmp(IndexSortOrder,"A") == 0) ? 1 : -1; - - if ((dirp = opendir(outdir)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); - exit(EXIT_FAILURE); - } - - nsort=0; - nallocated=0; - sortlist=NULL; - while ((direntp = readdir( dirp )) != NULL) { - if (strchr(direntp->d_name,'-') == 0) continue; - item=malloc(sizeof(*item)); - if (!item) { - debuga(_("not enough memory to sort the index\n")); - exit(EXIT_FAILURE); - } - if(strcmp(df,"u") == 0) { - item->year=atoi(direntp->d_name); - item->month=conv_month(direntp->d_name+4); - item->day=atoi(direntp->d_name+7); - } else { - item->year=atoi(direntp->d_name+5); - item->month=conv_month(direntp->d_name+2); - item->day=atoi(direntp->d_name); - } - item->sortnum=(item->year*16+item->month)*32+item->day; - obtdate(outdir,direntp->d_name,data); - if (sscanf(data,"%d-%d-%d %d:%d:%d %d",&iyear,&imonth,&iday,&ihour,&iminute,&isecond,&idst)==7) { - formatdate(data,sizeof(data),iyear,imonth,iday,ihour,iminute,isecond,idst); - snprintf(item->creationdate,sizeof(item->creationdate),"%04d%02d%02d%02d%02d%02d",iyear,imonth,iday,ihour,iminute,isecond); - } else { - /* - Old code to parse a date stored by sarg before 2.2.6.1 in the sarg-date file of each report directory. - */ - getword_start(&gwarea,data); - if (getword_skip(16,&gwarea,' ')<0) { - debuga(_("Maybe you have a broken week day in your %s%s/sarg-date file\n"),outdir,direntp->d_name); - exit(EXIT_FAILURE); - } - if (getword_multisep(mon,sizeof(mon),&gwarea,' ')<0) { - debuga(_("Maybe you have a broken month in your %s%s/sarg-date file\n"),outdir,direntp->d_name); - exit(EXIT_FAILURE); - } - if (getword_multisep(day,sizeof(day),&gwarea,' ')<0) { - debuga(_("Maybe you have a broken day in your %s%s/sarg-date file\n"),outdir,direntp->d_name); - exit(EXIT_FAILURE); - } - if (getword_multisep(hour,sizeof(hour),&gwarea,' ')<0) { - debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name); - exit(EXIT_FAILURE); - } - do { - if (getword_multisep(year,sizeof(year),&gwarea,' ')<0) { - debuga(_("Maybe you have a broken year in your %s%s/sarg-date file\n"),outdir,direntp->d_name); - exit(EXIT_FAILURE); - } - } while (year[0] && !isdigit(year[0])); //skip time zone information with spaces until the year is found - if (sscanf(hour,"%d:%d:%d",&ihour,&iminute,&isecond)!=3) { - debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name); - exit(EXIT_FAILURE); - } - buildymd(day,mon,year,ftime); - snprintf(item->creationdate,sizeof(item->creationdate),"%s%02d%02d%02d",ftime, ihour, iminute, isecond); - } - item->dirname=strdup(direntp->d_name); - if (!item->dirname) { - debuga(_("Not enough memory to store the directory name \"%s\" in the index\n"),direntp->d_name); - exit(EXIT_FAILURE); - } - strncpy(item->date,data,sizeof(item->date)); - if (nsort+1>nallocated) { - nallocated+=10; - tempsort=realloc(sortlist,nallocated*sizeof(*item)); - if (!tempsort) { - debuga(_("not enough memory to sort the index\n")); - exit(EXIT_FAILURE); - } - sortlist=tempsort; - } - for (i=nsort ; i>0 ; i--) { - if (item->sortnum>sortlist[i-1]->sortnum) break; - if (item->sortnum==sortlist[i-1]->sortnum) { - if (strcmp(item->creationdate,sortlist[i-1]->creationdate)>=0) break; - } - sortlist[i]=sortlist[i-1]; - } - sortlist[i]=item; - nsort++; - } - - closedir( dirp ); - - if((fp_ou=fopen(wdir,"w"))==NULL) { - debuga(_("(index) Cannot open file %s\n"),wdir); - exit(EXIT_FAILURE); - } - write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nsort),HTML_JS_SORTTABLE); - close_html_header(fp_ou); - fputs("
\n",fp_ou); - fprintf(fp_ou,"\n",_("FILE/PERIOD"),_("CREATION DATE"),_("USERS"),_("BYTES"),_("AVERAGE")); - for (i=0 ; i0) - item=sortlist[i]; - else - item=sortlist[nsort-i-1]; - tuser=obtuser(outdir,item->dirname); - obttotal(outdir,item->dirname,tuser,&tbytes,&media); - fputs("",item->dirname,ReplaceIndex,item->dirname); - fputs("",item->date); - fprintf(fp_ou,"",tuser); - fputs("",fixnum(tbytes,1)); - fputs("\n",fixnum(media,1)); - } - fputs("
%s%s%s%s%s
sortnum); - fprintf(fp_ou,">%screationdate); - fprintf(fp_ou,">%s%d%s%s
\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in the index %s\n"),wdir); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close the index file %s - %s\n"),wdir,strerror(errno)); - - if (sortlist) { - for (i=0 ; idirname); - free(sortlist[i]); - } - free(sortlist); - } + #define MAX_CREATION_DATE 15 + FILE *fp_ou; + DIR *dirp; + struct dirent *direntp; + char wdir[MAXLEN]; + char data[80]; + char ftime[128]; + char day[6], mon[8], year[40], hour[10]; + long long int tbytes; + long long int media; + int iyear, imonth, iday, ihour, iminute, isecond, idst; + int nsort; + int nallocated; + int order; + int i; + int tuser; + struct getwordstruct gwarea; + struct sortstruct + { + int year, month, day, sortnum; + char creationdate[MAX_CREATION_DATE]; + char *dirname; + char date[60]; + } **sortlist, *item, **tempsort; + + sprintf(wdir,"%sindex.html",outdir); + + order=(strcmp(IndexSortOrder,"A") == 0) ? 1 : -1; + + if ((dirp = opendir(outdir)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); + exit(EXIT_FAILURE); + } + + nsort=0; + nallocated=0; + sortlist=NULL; + while ((direntp = readdir( dirp )) != NULL) { + if (strchr(direntp->d_name,'-') == 0) continue; + item=malloc(sizeof(*item)); + if (!item) { + debuga(_("not enough memory to sort the index\n")); + exit(EXIT_FAILURE); + } + if(strcmp(df,"u") == 0) { + item->year=atoi(direntp->d_name); + item->month=conv_month(direntp->d_name+4); + item->day=atoi(direntp->d_name+7); + } else { + item->year=atoi(direntp->d_name+5); + item->month=conv_month(direntp->d_name+2); + item->day=atoi(direntp->d_name); + } + item->sortnum=(item->year*16+item->month)*32+item->day; + obtdate(outdir,direntp->d_name,data); + if (sscanf(data,"%d-%d-%d %d:%d:%d %d",&iyear,&imonth,&iday,&ihour,&iminute,&isecond,&idst)==7) { + formatdate(data,sizeof(data),iyear,imonth,iday,ihour,iminute,isecond,idst); + snprintf(item->creationdate,sizeof(item->creationdate),"%04d%02d%02d%02d%02d%02d",iyear,imonth,iday,ihour,iminute,isecond); + } else { + /* + Old code to parse a date stored by sarg before 2.2.6.1 in the sarg-date file of each report directory. + */ + getword_start(&gwarea,data); + if (getword_skip(16,&gwarea,' ')<0) { + debuga(_("Maybe you have a broken week day in your %s%s/sarg-date file\n"),outdir,direntp->d_name); + exit(EXIT_FAILURE); + } + if (getword_multisep(mon,sizeof(mon),&gwarea,' ')<0) { + debuga(_("Maybe you have a broken month in your %s%s/sarg-date file\n"),outdir,direntp->d_name); + exit(EXIT_FAILURE); + } + if (getword_multisep(day,sizeof(day),&gwarea,' ')<0) { + debuga(_("Maybe you have a broken day in your %s%s/sarg-date file\n"),outdir,direntp->d_name); + exit(EXIT_FAILURE); + } + if (getword_multisep(hour,sizeof(hour),&gwarea,' ')<0) { + debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name); + exit(EXIT_FAILURE); + } + do { + if (getword_multisep(year,sizeof(year),&gwarea,' ')<0) { + debuga(_("Maybe you have a broken year in your %s%s/sarg-date file\n"),outdir,direntp->d_name); + exit(EXIT_FAILURE); + } + } while (year[0] && !isdigit(year[0])); //skip time zone information with spaces until the year is found + if (sscanf(hour,"%d:%d:%d",&ihour,&iminute,&isecond)!=3) { + debuga(_("Maybe you have a broken time in your %s%s/sarg-date file\n"),outdir,direntp->d_name); + exit(EXIT_FAILURE); + } + buildymd(day,mon,year,ftime); + snprintf(item->creationdate,sizeof(item->creationdate),"%s%02d%02d%02d",ftime, ihour, iminute, isecond); + } + item->dirname=strdup(direntp->d_name); + if (!item->dirname) { + debuga(_("Not enough memory to store the directory name \"%s\" in the index\n"),direntp->d_name); + exit(EXIT_FAILURE); + } + strncpy(item->date,data,sizeof(item->date)); + if (nsort+1>nallocated) { + nallocated+=10; + tempsort=realloc(sortlist,nallocated*sizeof(*item)); + if (!tempsort) { + debuga(_("not enough memory to sort the index\n")); + exit(EXIT_FAILURE); + } + sortlist=tempsort; + } + for (i=nsort ; i>0 ; i--) { + if (item->sortnum>sortlist[i-1]->sortnum) break; + if (item->sortnum==sortlist[i-1]->sortnum) { + if (strcmp(item->creationdate,sortlist[i-1]->creationdate)>=0) break; + } + sortlist[i]=sortlist[i-1]; + } + sortlist[i]=item; + nsort++; + } + + closedir( dirp ); + + if((fp_ou=fopen(wdir,"w"))==NULL) { + debuga(_("(index) Cannot open file %s\n"),wdir); + exit(EXIT_FAILURE); + } + write_html_header(fp_ou,0,ngettext("SARG report","SARG reports",nsort),HTML_JS_SORTTABLE); + close_html_header(fp_ou); + fputs("
\n",fp_ou); + fprintf(fp_ou,"\n",_("FILE/PERIOD"),_("CREATION DATE"),_("USERS"),_("BYTES"),_("AVERAGE")); + for (i=0 ; i0) + item=sortlist[i]; + else + item=sortlist[nsort-i-1]; + tuser=obtuser(outdir,item->dirname); + obttotal(outdir,item->dirname,tuser,&tbytes,&media); + fputs("",item->dirname,ReplaceIndex,item->dirname); + fputs("",item->date); + fprintf(fp_ou,"",tuser); + fputs("",fixnum(tbytes,1)); + fputs("\n",fixnum(media,1)); + } + fputs("
%s%s%s%s%s
sortnum); + fprintf(fp_ou,">%screationdate); + fprintf(fp_ou,">%s%d%s%s
\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in the index %s\n"),wdir); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close the index file %s - %s\n"),wdir,strerror(errno)); + + if (sortlist) { + for (i=0 ; idirname); + free(sortlist[i]); + } + free(sortlist); + } } static void file_index_to_date_index(const char *entry) { - int y1, y2, m1, m2, d1, d2; - int i, j; - int ndirlen; - int monthlen; - char sm1[8], sm2[8]; - char olddir[MAXLEN], newdir[MAXLEN]; - - if(strlen(entry) < 19) return; - - y1=0; - y2=0; - memset(sm1,0,sizeof(sm1)); - memset(sm2,0,sizeof(sm2)); - d1=0; - d2=0; - i=0; - if(strcmp(df,"u") == 0) { - for (j=0 ; entry[i] && isdigit(entry[i]) ; j++) - y1=y1*10+(entry[i++]-'0'); - if (j!=4) return; - for (j=0 ; jd_name[0]) || !isdigit(direntp2->d_name[1])) continue; - i=0; - str=direntp2->d_name; - m1=0; - for (j=0 ; j<2 && str[i] && isdigit(str[i]) ; j++) - m1=(m1*10)+(str[i++]-'0'); - if (j>=2) continue; - sm1=conv_month_name(m1); - if (str[i]=='-') { - i++; - m2=0; - for (j=0 ; j<2 && str[i] && isdigit(str[i]) ; j++) - m2=(m2*10)+(str[i++]-'0'); - if (j>=2) continue; - sm2=conv_month_name(m2); - } else if (!str[i]) { - sm2=sm1; - } else { - continue; - } - - sprintf(val1+val1len,"/%s",direntp2->d_name); - dirp3 = opendir(val1); - if (!dirp3) continue; - while ((direntp3 = readdir( dirp3 )) != NULL) { - if(!isdigit(direntp3->d_name[0]) || !isdigit(direntp3->d_name[1])) continue; - i=0; - str=direntp3->d_name; - d1=0; - for (j=0 ; str[i] && isdigit(str[i]) ; j++) - d1=d1*10+(str[i++]-'0'); - if (j!=2) continue; - if (str[i]=='-') { - i++; - d2=0; - for (j=0 ; str[i] && isdigit(str[i]) ; j++) - d2=d2*10+(str[i++]-'0'); - if (j!=2) continue; - } else if (!str[i]) { - d2=d1; - } else { - continue; - } - - if(strcmp(df,"u") == 0) sprintf(newdir,"%s%04d%s%02d-%04d%s%02d",outdir,y1,sm1,d1,y1,sm2,d2); - else if(strcmp(df,"e") == 0) sprintf(newdir,"%s%02d%s%04d-%02d%s%04d",outdir,d1,sm1,y1,d2,sm2,y1); - else continue; - sprintf(olddir,"%s%04d/%s/%s",outdir,y1,direntp2->d_name,direntp3->d_name); - if(rename(olddir,newdir)) { - debuga(_("(index) rename error from \"%s\" to \"%s\" - %s\n"),olddir,newdir,strerror(errno)); - exit(EXIT_FAILURE); - } - } - closedir( dirp3 ); - } - closedir( dirp2 ); - - /*! - \bug The links to the images in the reports are broken after moving the directories - as the the HTML files are not at the right level for the images any more. - */ + int y1, next; + int m1, m2; + int d1, d2; + int val1len; + int i, j; + char val1[MAXLEN]; + const char *sm1, *sm2; + char *str; + char newdir[MAXLEN], olddir[MAXLEN]; + DIR *dirp2, *dirp3; + struct dirent *direntp2; + struct dirent *direntp3; + + if(strlen(entry) != 4) return; + + next=-1; + if (sscanf(entry,"%d%n",&y1,&next)!=1 || next<0 || entry[next]) return; + + val1len=snprintf(val1,sizeof(val1),"%s%s",outdir,entry); + dirp2 = opendir(val1); + if (!dirp2) return; + while ((direntp2 = readdir( dirp2 )) != NULL) { + if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue; + i=0; + str=direntp2->d_name; + m1=0; + for (j=0 ; j<2 && str[i] && isdigit(str[i]) ; j++) + m1=(m1*10)+(str[i++]-'0'); + if (j>=2) continue; + sm1=conv_month_name(m1); + if (str[i]=='-') { + i++; + m2=0; + for (j=0 ; j<2 && str[i] && isdigit(str[i]) ; j++) + m2=(m2*10)+(str[i++]-'0'); + if (j>=2) continue; + sm2=conv_month_name(m2); + } else if (!str[i]) { + sm2=sm1; + } else { + continue; + } + + sprintf(val1+val1len,"/%s",direntp2->d_name); + dirp3 = opendir(val1); + if (!dirp3) continue; + while ((direntp3 = readdir( dirp3 )) != NULL) { + if(!isdigit(direntp3->d_name[0]) || !isdigit(direntp3->d_name[1])) continue; + i=0; + str=direntp3->d_name; + d1=0; + for (j=0 ; str[i] && isdigit(str[i]) ; j++) + d1=d1*10+(str[i++]-'0'); + if (j!=2) continue; + if (str[i]=='-') { + i++; + d2=0; + for (j=0 ; str[i] && isdigit(str[i]) ; j++) + d2=d2*10+(str[i++]-'0'); + if (j!=2) continue; + } else if (!str[i]) { + d2=d1; + } else { + continue; + } + + if(strcmp(df,"u") == 0) sprintf(newdir,"%s%04d%s%02d-%04d%s%02d",outdir,y1,sm1,d1,y1,sm2,d2); + else if(strcmp(df,"e") == 0) sprintf(newdir,"%s%02d%s%04d-%02d%s%04d",outdir,d1,sm1,y1,d2,sm2,y1); + else continue; + sprintf(olddir,"%s%04d/%s/%s",outdir,y1,direntp2->d_name,direntp3->d_name); + if(rename(olddir,newdir)) { + debuga(_("(index) rename error from \"%s\" to \"%s\" - %s\n"),olddir,newdir,strerror(errno)); + exit(EXIT_FAILURE); + } + } + closedir( dirp3 ); + } + closedir( dirp2 ); + + /*! + \bug The links to the images in the reports are broken after moving the directories + as the the HTML files are not at the right level for the images any more. + */ } diff --git a/indexonly.c b/indexonly.c index 0c538a1..8cdf118 100644 --- a/indexonly.c +++ b/indexonly.c @@ -30,28 +30,28 @@ void index_only(const char *dirname,int debug) { - DIR *dirp; - struct dirent *direntp; - char remove[MAXLEN]; + DIR *dirp; + struct dirent *direntp; + char remove[MAXLEN]; - if ((dirp = opendir(dirname)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),dirname,strerror(errno)); - exit(EXIT_FAILURE); - } - while ( (direntp = readdir( dirp )) != NULL ){ - if(strcmp(direntp->d_name,".") == 0 || strcmp(direntp->d_name,"..") == 0 || strcmp(direntp->d_name, "index.html") == 0) - continue; + if ((dirp = opendir(dirname)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),dirname,strerror(errno)); + exit(EXIT_FAILURE); + } + while ( (direntp = readdir( dirp )) != NULL ){ + if(strcmp(direntp->d_name,".") == 0 || strcmp(direntp->d_name,"..") == 0 || strcmp(direntp->d_name, "index.html") == 0) + continue; - if (snprintf(remove,sizeof(remove),"%s/%s",dirname,direntp->d_name)>=sizeof(remove)) { - debuga(_("Name of the file to remove is too long: %s/%s\n"),dirname,direntp->d_name); - continue; - } - if (unlink(remove) == -1) { - debuga(_("Failed to remove the file %s\n"),remove); - } - } + if (snprintf(remove,sizeof(remove),"%s/%s",dirname,direntp->d_name)>=sizeof(remove)) { + debuga(_("Name of the file to remove is too long: %s/%s\n"),dirname,direntp->d_name); + continue; + } + if (unlink(remove) == -1) { + debuga(_("Failed to remove the file %s\n"),remove); + } + } - (void)closedir( dirp ); + (void)closedir( dirp ); - return; + return; } diff --git a/ip2name.c b/ip2name.c index d202198..4ae79bb 100644 --- a/ip2name.c +++ b/ip2name.c @@ -27,89 +27,89 @@ #include "include/conf.h" #include "include/defs.h" #ifdef HAVE_WS2TCPIP_H -#include //define getnameinfo on windows +#include //define getnameinfo on windows #endif void ip2name(char *ip,int ip_len) { #ifdef HAVE_GETNAMEINFO - struct sockaddr_storage sa; - char host[NI_MAXHOST]; - int n1,n2,n3,n4,next=0; - int error; + struct sockaddr_storage sa; + char host[NI_MAXHOST]; + int n1,n2,n3,n4,next=0; + int error; - memset(&sa,0,sizeof(sa)); - if (sscanf(ip,"%d.%d.%d.%d%n",&n1,&n2,&n3,&n4,&next)==4 && ip[next]=='\0') { - struct sockaddr_in *s4=(struct sockaddr_in *)&sa; - if (inet_pton(AF_INET,ip,&s4->sin_addr)!=1) return; - sa.ss_family=AF_INET; - } else { - struct sockaddr_in6 *s6=(struct sockaddr_in6 *)&sa; - if (inet_pton(AF_INET6,ip,&s6->sin6_addr)!=1) return; - sa.ss_family=AF_INET6; - } - error=getnameinfo((struct sockaddr *)&sa,sizeof(sa),host,sizeof(host),NULL,0,0); - if (error==0) - { - strncpy(ip,host,ip_len-1); - ip[ip_len-1]='\0'; - } else { - debuga(_("IP to name resolution (getnameinfo) on IP address %s failed with error %d - %s\n"),ip,error,gai_strerror(error)); - } + memset(&sa,0,sizeof(sa)); + if (sscanf(ip,"%d.%d.%d.%d%n",&n1,&n2,&n3,&n4,&next)==4 && ip[next]=='\0') { + struct sockaddr_in *s4=(struct sockaddr_in *)&sa; + if (inet_pton(AF_INET,ip,&s4->sin_addr)!=1) return; + sa.ss_family=AF_INET; + } else { + struct sockaddr_in6 *s6=(struct sockaddr_in6 *)&sa; + if (inet_pton(AF_INET6,ip,&s6->sin6_addr)!=1) return; + sa.ss_family=AF_INET6; + } + error=getnameinfo((struct sockaddr *)&sa,sizeof(sa),host,sizeof(host),NULL,0,0); + if (error==0) + { + strncpy(ip,host,ip_len-1); + ip[ip_len-1]='\0'; + } else { + debuga(_("IP to name resolution (getnameinfo) on IP address %s failed with error %d - %s\n"),ip,error,gai_strerror(error)); + } #else - struct in_addr addr; - struct hostent *hp; - char **p; + struct in_addr addr; + struct hostent *hp; + char **p; #ifdef HAVE_INET_ATON - if (inet_aton(ip,&addr) == 0) - return; + if (inet_aton(ip,&addr) == 0) + return; #else - addr.s_addr=inet_addr(ip); - if (addr.s_addr==-1) return; + addr.s_addr=inet_addr(ip); + if (addr.s_addr==-1) return; #endif - hp = gethostbyaddr((void *)&addr, sizeof (addr), AF_INET); - if (hp == NULL) - return; + hp = gethostbyaddr((void *)&addr, sizeof (addr), AF_INET); + if (hp == NULL) + return; - for (p = hp->h_addr_list; *p != 0; p++) { - struct in_addr in; + for (p = hp->h_addr_list; *p != 0; p++) { + struct in_addr in; - (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr)); - strncpy(ip,hp->h_name,ip_len-1); - ip[ip_len-1]=0; - } + (void) memcpy(&in.s_addr, *p, sizeof (in.s_addr)); + strncpy(ip,hp->h_name,ip_len-1); + ip[ip_len-1]=0; + } #endif - return; + return; } void name2ip(char *name) { - struct in_addr ia; - struct hostent *hp; - char *port; - char n1[4]; - char n2[4]; - char n3[4]; - char n4[4]; - struct getwordstruct gwarea; + struct in_addr ia; + struct hostent *hp; + char *port; + char n1[4]; + char n2[4]; + char n3[4]; + char n4[4]; + struct getwordstruct gwarea; - port=strchr(name,':'); - if (port) *port=0; + port=strchr(name,':'); + if (port) *port=0; - if((hp=gethostbyname(name))==NULL) - return; + if((hp=gethostbyname(name))==NULL) + return; - memcpy(&ia.s_addr,hp->h_addr_list[0],sizeof(ia.s_addr)); - ia.s_addr=ntohl(ia.s_addr); - getword_start(&gwarea,inet_ntoa(ia)); - if (getword(n4,sizeof(n4),&gwarea,'.')<0 || getword(n3,sizeof(n3),&gwarea,'.')<0 || - getword(n2,sizeof(n2),&gwarea,'.')<0 || getword(n1,sizeof(n1),&gwarea,0)<0) { - printf("SARG: Maybe you have a broken record or garbage in your %s ip address.\n",gwarea.beginning); - exit(EXIT_FAILURE); - } - sprintf(name,"%s.%s.%s.%s",n1,n2,n3,n4); + memcpy(&ia.s_addr,hp->h_addr_list[0],sizeof(ia.s_addr)); + ia.s_addr=ntohl(ia.s_addr); + getword_start(&gwarea,inet_ntoa(ia)); + if (getword(n4,sizeof(n4),&gwarea,'.')<0 || getword(n3,sizeof(n3),&gwarea,'.')<0 || + getword(n2,sizeof(n2),&gwarea,'.')<0 || getword(n1,sizeof(n1),&gwarea,0)<0) { + printf("SARG: Maybe you have a broken record or garbage in your %s ip address.\n",gwarea.beginning); + exit(EXIT_FAILURE); + } + sprintf(name,"%s.%s.%s.%s",n1,n2,n3,n4); - return; + return; } diff --git a/lastlog.c b/lastlog.c index 0a44699..2252baf 100644 --- a/lastlog.c +++ b/lastlog.c @@ -29,101 +29,100 @@ void mklastlog(const char *outdir) { - - FILE *fp_in, *fp_ou; - DIR *dirp; - struct dirent *direntp; - char buf[MAXLEN]; - char temp[MAXLEN]; - char warea[MAXLEN]; - char ftime[128]; - int ftot=0; - time_t t; - struct tm *local; - struct stat statb; - int cstatus; - struct getwordstruct gwarea; - - if(LastLog <= 0) - return; - - snprintf(temp,sizeof(temp),"%slastlog1",outdir); - if((fp_ou=fopen(temp,"w"))==NULL) { - debuga(_("(lastlog) Cannot open temporary file %s\n"),temp); - exit(EXIT_FAILURE); - } - - if ((dirp = opendir(outdir)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); - exit(EXIT_FAILURE); - } - while ((direntp = readdir( dirp )) != NULL ){ - if(strchr(direntp->d_name,'-') == 0) - continue; - - snprintf(warea,sizeof(warea),"%s%s",outdir,direntp->d_name); - if (stat(warea,&statb) == -1) { - debuga(_("Failed to get the creation time of %s\n"),warea); - continue; - } - t=statb.st_ctime; - local = localtime(&t); - strftime(ftime, sizeof(ftime), "%Y%m%d%H%M%S", local); - fprintf(fp_ou,"%s\t%s\n",ftime,direntp->d_name); - ftot++; - } - - closedir( dirp ); - fclose(fp_ou); - - snprintf(buf,sizeof(buf),"sort -n -k 1,1 -o \"%slastlog\" \"%s\"",outdir,temp); - cstatus=system(buf); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),buf); - exit(EXIT_FAILURE); - } - - unlink(temp); - - if(ftot<=LastLog) { - snprintf(temp,sizeof(temp),"%slastlog",outdir); - if(access(temp, R_OK) == 0) - unlink(temp); - return; - } - - ftot-=LastLog; - - snprintf(temp,sizeof(temp),"%slastlog",outdir); - if((fp_in=fopen(temp,"r"))==NULL) { - debuga(_("(lastlog) Cannot open temporary file %s\n"),temp); - exit(EXIT_FAILURE); - } - - while(ftot>0 && fgets(buf,sizeof(buf),fp_in)!=NULL) { - fixendofline(buf); - getword_start(&gwarea,buf); - if (getword(warea,sizeof(warea),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),temp); - exit(EXIT_FAILURE); - } - - if(debug) - debuga(_("Removing old report file %s\n"),gwarea.current); - if (snprintf(temp,sizeof(temp),"%s%s",outdir,gwarea.current)>=sizeof(temp)) { - debuga(_("Directory name too long: %s%s\n"),outdir,gwarea.current); - exit(EXIT_FAILURE); - } - unlinkdir(temp,0); - ftot--; - } - - fclose(fp_in); - snprintf(temp,sizeof(temp),"%slastlog",outdir); - if (unlink(temp) == -1) { - debuga(_("Failed to delete the file %s\n"),temp); - } - - return; + FILE *fp_in, *fp_ou; + DIR *dirp; + struct dirent *direntp; + char buf[MAXLEN]; + char temp[MAXLEN]; + char warea[MAXLEN]; + char ftime[128]; + int ftot=0; + time_t t; + struct tm *local; + struct stat statb; + int cstatus; + struct getwordstruct gwarea; + + if(LastLog <= 0) + return; + + snprintf(temp,sizeof(temp),"%slastlog1",outdir); + if((fp_ou=fopen(temp,"w"))==NULL) { + debuga(_("(lastlog) Cannot open temporary file %s\n"),temp); + exit(EXIT_FAILURE); + } + + if ((dirp = opendir(outdir)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno)); + exit(EXIT_FAILURE); + } + while ((direntp = readdir( dirp )) != NULL ){ + if(strchr(direntp->d_name,'-') == 0) + continue; + + snprintf(warea,sizeof(warea),"%s%s",outdir,direntp->d_name); + if (stat(warea,&statb) == -1) { + debuga(_("Failed to get the creation time of %s\n"),warea); + continue; + } + t=statb.st_ctime; + local = localtime(&t); + strftime(ftime, sizeof(ftime), "%Y%m%d%H%M%S", local); + fprintf(fp_ou,"%s\t%s\n",ftime,direntp->d_name); + ftot++; + } + + closedir( dirp ); + fclose(fp_ou); + + snprintf(buf,sizeof(buf),"sort -n -k 1,1 -o \"%slastlog\" \"%s\"",outdir,temp); + cstatus=system(buf); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),buf); + exit(EXIT_FAILURE); + } + + unlink(temp); + + if(ftot<=LastLog) { + snprintf(temp,sizeof(temp),"%slastlog",outdir); + if(access(temp, R_OK) == 0) + unlink(temp); + return; + } + + ftot-=LastLog; + + snprintf(temp,sizeof(temp),"%slastlog",outdir); + if((fp_in=fopen(temp,"r"))==NULL) { + debuga(_("(lastlog) Cannot open temporary file %s\n"),temp); + exit(EXIT_FAILURE); + } + + while(ftot>0 && fgets(buf,sizeof(buf),fp_in)!=NULL) { + fixendofline(buf); + getword_start(&gwarea,buf); + if (getword(warea,sizeof(warea),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),temp); + exit(EXIT_FAILURE); + } + + if(debug) + debuga(_("Removing old report file %s\n"),gwarea.current); + if (snprintf(temp,sizeof(temp),"%s%s",outdir,gwarea.current)>=sizeof(temp)) { + debuga(_("Directory name too long: %s%s\n"),outdir,gwarea.current); + exit(EXIT_FAILURE); + } + unlinkdir(temp,0); + ftot--; + } + + fclose(fp_in); + snprintf(temp,sizeof(temp),"%slastlog",outdir); + if (unlink(temp) == -1) { + debuga(_("Failed to delete the file %s\n"),temp); + } + + return; } diff --git a/log.c b/log.c index 42eaec6..869dd1f 100644 --- a/log.c +++ b/log.c @@ -36,1708 +36,1706 @@ struct userfilestruct { - struct userfilestruct *next; - struct userinfostruct *user; - FILE *file; + struct userfilestruct *next; + struct userinfostruct *user; + FILE *file; }; /*@null@*/static char *userfile=NULL; numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 }; numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, - 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 }; + 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 }; static void getusers(const char *pwdfile, int debug); int main(int argc,char *argv[]) { - enum isa_col_id { - ISACOL_Ip, - ISACOL_UserName, - ISACOL_Date, - ISACOL_Time, - ISACOL_TimeTaken, - ISACOL_Bytes, - ISACOL_Uri, - ISACOL_Status, - ISACOL_Last //last entry of the list ! - }; - enum InputLogFormat { - ILF_Unknown, - ILF_Squid, - ILF_Common, - ILF_Sarg, - ILF_Isa, - ILF_Last //last entry of the list ! - }; - - FILE *fp_in = NULL, *fp_denied=NULL, *fp_authfail=NULL, *fp_log=NULL; - - char sz_Download_Unsort[ 20000 ] ; - FILE * fp_Download_Unsort = NULL ; - - extern int optind; - extern int optopt; - extern char *optarg; - - char data[255]; - char elap[255]; - char ip[MAXLEN]; - char tam[255]; - char fun[MAXLEN]; - char wuser[MAXLEN]; - char smartfilter[MAXLEN]; - char dia[128]; - char mes[30]; - char ano[30]; - char hora[30]; - char wtemp[MAXLEN]; - char date[255]; - char arq[255]; - char arq_log[255]; - int hm, hmf, hmr; - char hm_str[15]; - char uagent[MAXLEN]; - char hexclude[MAXLEN]; - char csort[MAXLEN]; - int cstatus; - char tbuf2[128]; - char zip[20]; - char *str; - char tmp3[MAXLEN]; - char denied_unsort[MAXLEN]; - char denied_sort[MAXLEN]; - char authfail_unsort[MAXLEN]; - char start_hour[128]; - char end_hour[128]; - char *linebuf; - char hostname[512]; - char *url; - char *urly; - char user[MAX_USER_LEN]; - enum InputLogFormat ilf; - int ilf_count[ILF_Last]; - int ch; - int x; - int errflg=0; - int puser=0; - bool fhost=false; - bool dns=false; - bool fuser=false; - int idata=0; - int mindate=0; - int maxdate=0; - int iarq=0; - int isa_ncols=0,isa_cols[ISACOL_Last]; - int lastlog=-1; - bool from_stdin; - bool from_pipe; - int blen; - int maxopenfiles; - int nopen; - bool id_is_ip; - long totregsl=0; - long totregsg=0; - long totregsx=0; - bool totper=false; - long int max_elapsed=0; - long long int iyear, imonth, iday; - bool realt; - bool userip; - struct tm tt; - struct tm *t; - unsigned long recs1=0UL; - unsigned long recs2=0UL; - int OutputNonZero = REPORT_EVERY_X_LINES ; - bool download_flag=false; - char *download_url=NULL; - struct getwordstruct gwarea; - longline line; - time_t tnum; - struct stat logstat; - struct userinfostruct *uinfo; - struct userfilestruct *first_user_file, *ufile, *ufile1, *prev_ufile; - static int split=0; - static int convert=0; - static int output_css=0; - int option_index; - static struct option long_options[]= - { - {"convert",no_argument,&convert,1}, - {"css",no_argument,&output_css,1}, - {"lastlog",required_argument,NULL,2}, - {"keeplogs",no_argument,NULL,3}, - {"split",no_argument,&split,1}, - {0,0,0,0} - }; + enum isa_col_id { + ISACOL_Ip, + ISACOL_UserName, + ISACOL_Date, + ISACOL_Time, + ISACOL_TimeTaken, + ISACOL_Bytes, + ISACOL_Uri, + ISACOL_Status, + ISACOL_Last //last entry of the list ! + }; + enum InputLogFormat { + ILF_Unknown, + ILF_Squid, + ILF_Common, + ILF_Sarg, + ILF_Isa, + ILF_Last //last entry of the list ! + }; + + FILE *fp_in = NULL, *fp_denied=NULL, *fp_authfail=NULL, *fp_log=NULL; + + char sz_Download_Unsort[ 20000 ] ; + FILE * fp_Download_Unsort = NULL ; + + extern int optind; + extern int optopt; + extern char *optarg; + + char data[255]; + char elap[255]; + char ip[MAXLEN]; + char tam[255]; + char fun[MAXLEN]; + char wuser[MAXLEN]; + char smartfilter[MAXLEN]; + char dia[128]; + char mes[30]; + char ano[30]; + char hora[30]; + char wtemp[MAXLEN]; + char date[255]; + char arq[255]; + char arq_log[255]; + int hm, hmf, hmr; + char hm_str[15]; + char uagent[MAXLEN]; + char hexclude[MAXLEN]; + char csort[MAXLEN]; + int cstatus; + char tbuf2[128]; + char zip[20]; + char *str; + char tmp3[MAXLEN]; + char denied_unsort[MAXLEN]; + char denied_sort[MAXLEN]; + char authfail_unsort[MAXLEN]; + char start_hour[128]; + char end_hour[128]; + char *linebuf; + char hostname[512]; + char *url; + char *urly; + char user[MAX_USER_LEN]; + enum InputLogFormat ilf; + int ilf_count[ILF_Last]; + int ch; + int x; + int errflg=0; + int puser=0; + bool fhost=false; + bool dns=false; + bool fuser=false; + int idata=0; + int mindate=0; + int maxdate=0; + int iarq=0; + int isa_ncols=0,isa_cols[ISACOL_Last]; + int lastlog=-1; + bool from_stdin; + bool from_pipe; + int blen; + int maxopenfiles; + int nopen; + bool id_is_ip; + long totregsl=0; + long totregsg=0; + long totregsx=0; + bool totper=false; + long int max_elapsed=0; + long long int iyear, imonth, iday; + bool realt; + bool userip; + struct tm tt; + struct tm *t; + unsigned long recs1=0UL; + unsigned long recs2=0UL; + int OutputNonZero = REPORT_EVERY_X_LINES ; + bool download_flag=false; + char *download_url=NULL; + struct getwordstruct gwarea; + longline line; + time_t tnum; + struct stat logstat; + struct userinfostruct *uinfo; + struct userfilestruct *first_user_file, *ufile, *ufile1, *prev_ufile; + static int split=0; + static int convert=0; + static int output_css=0; + int option_index; + static struct option long_options[]= + { + {"convert",no_argument,&convert,1}, + {"css",no_argument,&output_css,1}, + {"lastlog",required_argument,NULL,2}, + {"keeplogs",no_argument,NULL,3}, + {"split",no_argument,&split,1}, + {0,0,0,0} + }; #ifdef HAVE_LOCALE_H - setlocale(LC_TIME,""); + setlocale(LC_TIME,""); #endif #if defined(ENABLE_NLS) && defined(HAVE_LOCALE_H) - if (!setlocale (LC_ALL, "")) { - fprintf(stderr,"SARG: Cannot set the locale LC_ALL to the environment variable\n"); - exit(EXIT_FAILURE); - } - if (!bindtextdomain (PACKAGE_NAME, LOCALEDIR)) { - fprintf(stderr,"SARG: Cannot bind to text domain %s in directory %s (%s)\n",PACKAGE_NAME,LOCALEDIR,strerror(errno)); - exit(EXIT_FAILURE); - } - if (!textdomain (PACKAGE_NAME)) { - fprintf(stderr,"SARG: Cannot set gettext domain for %s PACKAGE_NAME (%s)\n",PACKAGE_NAME,strerror(errno)); - exit(EXIT_FAILURE); - } + if (!setlocale (LC_ALL, "")) { + fprintf(stderr,"SARG: Cannot set the locale LC_ALL to the environment variable\n"); + exit(EXIT_FAILURE); + } + if (!bindtextdomain (PACKAGE_NAME, LOCALEDIR)) { + fprintf(stderr,"SARG: Cannot bind to text domain %s in directory %s (%s)\n",PACKAGE_NAME,LOCALEDIR,strerror(errno)); + exit(EXIT_FAILURE); + } + if (!textdomain (PACKAGE_NAME)) { + fprintf(stderr,"SARG: Cannot set gettext domain for %s PACKAGE_NAME (%s)\n",PACKAGE_NAME,strerror(errno)); + exit(EXIT_FAILURE); + } #endif //ENABLE_NLS - BgImage[0]='\0'; - LogoImage[0]='\0'; - LogoText[0]='\0'; - PasswdFile[0]='\0'; - OutputEmail[0]='\0'; - UserAgentLog[0]='\0'; - ExcludeHosts[0]='\0'; - ExcludeUsers[0]='\0'; - ConfigFile[0]='\0'; - code[0]='\0'; - LastLog=0; - ReportType=0UL; - UserTabFile[0]='\0'; - BlockIt[0]='\0'; - ExternalCSSFile[0]='\0'; - RedirectorLogFormat[0]='\0'; - NRedirectorLogs=0; - for (ilf=0 ; ilf=MAXLOGS) { - debuga(_("Too many log files passed on command line with option -l.\n")); - exit(EXIT_FAILURE); - } - if (strlen(optarg)>=MAX_LOG_FILELEN) { - debuga(_("Log file name too long passed on command line with option -l: %s\n"),optarg); - exit(EXIT_FAILURE); - } - strcpy(AccessLog[NAccessLog],optarg); - NAccessLog++; - AccessLogFromCmdLine++; - break; - case 'L': - if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) { - debuga(_("Too many redirector logs passed on command line with option -L.\n")); - exit(EXIT_FAILURE); - } - if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) { - debuga(_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg); - exit(EXIT_FAILURE); - } - strcpy(RedirectorLogs[NRedirectorLogs],optarg); - NRedirectorLogs++; - RedirectorLogFromCmdLine++; - break; - case 'm': - debugm++; - break; - case 'n': - dns=true; - break; - case 'o': - strcpy(outdir,optarg); - break; - case 'p': - userip=true; - break; - case 'r': - realt=true; - break; - case 's': - strcpy(site,optarg); - break; - case 't': - { - int h1,m1,h2,m2; - - if(strstr(optarg,"-") == 0) { - if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) { - debuga(_("Time period passed on the command line with option -t must be MM:SS\n")); - exit(EXIT_FAILURE); - } - hm=h1*100+m1; - hmf=hm; - snprintf(hm_str,sizeof(hm_str),"%02d:%02d",h1,m1); - } else { - if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h2,&m2)!=4) { - debuga(_("Time range passed on the command line with option -t must be MM:SS-MM:SS\n")); - exit(EXIT_FAILURE); - } - hm=h1*100+m1; - hmf=h2*100+m2; - snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2); - } - break; - } - case 'u': - strcpy(us,optarg); - break; - case 'v': - version(); - break; - case 'w': - strcpy(tmp,optarg); - break; - case 'x': - debug++; - break; - case 'y': - langcode++; - break; - case 'z': - debugz++; - break; - /*case ':': - debuga(_("Option -%c require an argument\n"),optopt); - errflg++; - break;*/ - case '?': - usage(argv[0]); - exit(EXIT_FAILURE); - default: - abort(); - } - } - - if (errflg>0) { - usage(argv[0]); - exit(2); - } - - if (optind=MAXLOGS) { - debuga(_("Too many log files passed on command line.\n")); - exit(EXIT_FAILURE); - } - if (strlen(argv[iarq])>=MAX_LOG_FILELEN) { - debuga(_("Log file name too long passed on command line: %s\n"),argv[iarq]); - exit(EXIT_FAILURE); - } - strcpy(AccessLog[NAccessLog],argv[iarq]); - NAccessLog++; - AccessLogFromCmdLine++; - } - } - - if(debug) debuga(_("Init\n")); - - if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg.conf",SYSCONFDIR); - if(access(ConfigFile, R_OK) != 0) { - debuga(_("Cannot open config file: %s - %s\n"),ConfigFile,strerror(errno)); - exit(EXIT_FAILURE); - } - - if(access(ConfigFile, R_OK) == 0) - getconf(); - - if(userip) UserIp=true; - - if(dns) Ip2Name=true; - - if (lastlog>=0) LastLog=lastlog; - - if(realt) { - realtime(); - exit(EXIT_SUCCESS); - } - - if(IndexTree == INDEX_TREE_FILE) - strcpy(ImageFile,"../images"); - else - strcpy(ImageFile,"../../../images"); - - dataonly=0; - if(DataFile[0] != '\0') - dataonly++; - - if(NAccessLog == 0) { - strcpy(AccessLog[0],"/var/log/squid/access.log"); - NAccessLog++; - } - - if(output_css) { - css_content(stdout); - exit(EXIT_SUCCESS); - } - if(split) { - for (iarq=0 ; iarq=MAXLOGS) { + debuga(_("Too many log files passed on command line with option -l.\n")); + exit(EXIT_FAILURE); + } + if (strlen(optarg)>=MAX_LOG_FILELEN) { + debuga(_("Log file name too long passed on command line with option -l: %s\n"),optarg); + exit(EXIT_FAILURE); + } + strcpy(AccessLog[NAccessLog],optarg); + NAccessLog++; + AccessLogFromCmdLine++; + break; + case 'L': + if (NRedirectorLogs>MAX_REDIRECTOR_LOGS) { + debuga(_("Too many redirector logs passed on command line with option -L.\n")); + exit(EXIT_FAILURE); + } + if (strlen(optarg)>=MAX_REDIRECTOR_FILELEN) { + debuga(_("Redirector log file name too long passed on command line with opton -L: %s\n"),optarg); + exit(EXIT_FAILURE); + } + strcpy(RedirectorLogs[NRedirectorLogs],optarg); + NRedirectorLogs++; + RedirectorLogFromCmdLine++; + break; + case 'm': + debugm++; + break; + case 'n': + dns=true; + break; + case 'o': + strcpy(outdir,optarg); + break; + case 'p': + userip=true; + break; + case 'r': + realt=true; + break; + case 's': + strcpy(site,optarg); + break; + case 't': + { + int h1,m1,h2,m2; + + if(strstr(optarg,"-") == 0) { + if(sscanf(optarg,"%d:%d",&h1,&m1)!=2) { + debuga(_("Time period passed on the command line with option -t must be MM:SS\n")); + exit(EXIT_FAILURE); + } + hm=h1*100+m1; + hmf=hm; + snprintf(hm_str,sizeof(hm_str),"%02d:%02d",h1,m1); + } else { + if(sscanf(optarg,"%d:%d-%d:%d",&h1,&m1,&h2,&m2)!=4) { + debuga(_("Time range passed on the command line with option -t must be MM:SS-MM:SS\n")); + exit(EXIT_FAILURE); + } + hm=h1*100+m1; + hmf=h2*100+m2; + snprintf(hm_str,sizeof(hm_str),"%02d:%02d-%02d:%02d",h1,m1,h2,m2); + } + break; + } + case 'u': + strcpy(us,optarg); + break; + case 'v': + version(); + break; + case 'w': + strcpy(tmp,optarg); + break; + case 'x': + debug++; + break; + case 'y': + langcode++; + break; + case 'z': + debugz++; + break; + /*case ':': + debuga(_("Option -%c require an argument\n"),optopt); + errflg++; + break;*/ + case '?': + usage(argv[0]); + exit(EXIT_FAILURE); + default: + abort(); + } + } + + if (errflg>0) { + usage(argv[0]); + exit(2); + } + + if (optind=MAXLOGS) { + debuga(_("Too many log files passed on command line.\n")); + exit(EXIT_FAILURE); + } + if (strlen(argv[iarq])>=MAX_LOG_FILELEN) { + debuga(_("Log file name too long passed on command line: %s\n"),argv[iarq]); + exit(EXIT_FAILURE); + } + strcpy(AccessLog[NAccessLog],argv[iarq]); + NAccessLog++; + AccessLogFromCmdLine++; + } + } + + if(debug) debuga(_("Init\n")); + + if(ConfigFile[0] == '\0') snprintf(ConfigFile,sizeof(ConfigFile),"%s/sarg.conf",SYSCONFDIR); + if(access(ConfigFile, R_OK) != 0) { + debuga(_("Cannot open config file: %s - %s\n"),ConfigFile,strerror(errno)); + exit(EXIT_FAILURE); + } + + if(access(ConfigFile, R_OK) == 0) + getconf(); + + if(userip) UserIp=true; + + if(dns) Ip2Name=true; + + if (lastlog>=0) LastLog=lastlog; + + if(realt) { + realtime(); + exit(EXIT_SUCCESS); + } + + if(IndexTree == INDEX_TREE_FILE) + strcpy(ImageFile,"../images"); + else + strcpy(ImageFile,"../../../images"); + + dataonly=0; + if(DataFile[0] != '\0') + dataonly++; + + if(NAccessLog == 0) { + strcpy(AccessLog[0],"/var/log/squid/access.log"); + NAccessLog++; + } + + if(output_css) { + css_content(stdout); + exit(EXIT_SUCCESS); + } + if(split) { + for (iarq=0 ; iarqtm_year+1900)*10000+(logtime->tm_mon+1)*100+logtime->tm_mday0) { - for (i=0 ; itm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday; - } - } - if (ilf==ILF_Sarg) { - getword_start(&gwarea,linebuf); - if (getword(data,sizeof(data),&gwarea,'\t')<0){ - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword(hora,sizeof(hora),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword_ptr(linebuf,&url,&gwarea,'\t')<0){ - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword(tam,sizeof(tam),&gwarea,'\t')<0){ - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword(code,sizeof(code),&gwarea,'\t')<0){ - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword(elap,sizeof(elap),&gwarea,'\t')<0){ - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword(smartfilter,sizeof(smartfilter),&gwarea,'\0')<0){ - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - getword_start(&gwarea,data); - if (getword_atoll(&iday,&gwarea,'/')<0 || iday<1 || iday>31){ - debuga(_("Maybe you have a broken date in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword_atoll(&imonth,&gwarea,'/')<0 || imonth<1 || imonth>12){ - debuga(_("Maybe you have a broken date in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword_atoll(&iyear,&gwarea,'\0')<0){ - debuga(_("Maybe you have a broken date in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - idata=builddia(iday,imonth,iyear); - computedate(iyear,imonth,iday,&tt); - t=&tt; - } - if (ilf==ILF_Isa) { - if (linebuf[0] == '#') { - int ncols,cols[ISACOL_Last]; - - fixendofline(linebuf); - getword_start(&gwarea,linebuf); - // remove the #Fields: column at the beginning of the line - if (getword_skip(1000,&gwarea,' ')<0){ - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - for (ncols=0 ; ncols=0) { - isa_ncols=ncols; - for (ncols=0 ; ncols=sizeof(ip)) { - debuga(_("Maybe you have a broken IP in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - strcpy(ip,str); - } else if (x==isa_cols[ISACOL_UserName]) { - if (strlen(str)>=sizeof(user)) { - debuga(_("Maybe you have a broken user ID in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - strcpy(user,str); - } else if (x==isa_cols[ISACOL_Date]) { - if (strlen(str)>=sizeof(data)) { - debuga(_("Maybe you have a broken date in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - strcpy(data,str); - } else if (x==isa_cols[ISACOL_Time]) { - if (strlen(str)>=sizeof(hora)) { - debuga(_("Maybe you have a broken time in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - strcpy(hora,str); - } else if (x==isa_cols[ISACOL_TimeTaken]) { - if (strlen(str)>=sizeof(elap)) { - debuga(_("Maybe you have a broken download duration in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - strcpy(elap,str); - } else if (x==isa_cols[ISACOL_Bytes]) { - if (strlen(str)>=sizeof(tam)) { - debuga(_("Maybe you have a broken download size in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - strcpy(tam,str); - } else if (x==isa_cols[ISACOL_Uri]) { - url=str; - } else if (x==isa_cols[ISACOL_Status]) { - if (strlen(str)>=sizeof(code)) { - debuga(_("Maybe you have a broken access code in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - strcpy(code,str); - } - } - - if(strcmp(code,"401") == 0 || strcmp(code,"403") == 0 || strcmp(code,"407") == 0) { - sprintf(val1,"DENIED/%s",code); - strcpy(code,val1); - } - getword_start(&gwarea,data); - if (getword_atoll(&iyear,&gwarea,'-')<0){ - debuga(_("Maybe you have a broken year in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword_atoll(&imonth,&gwarea,'-')<0){ - debuga(_("Maybe you have a broken month in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - if (getword_atoll(&iday,&gwarea,'\0')<0){ - debuga(_("Maybe you have a broken day in your %s file\n"),arq); - exit(EXIT_FAILURE); - } - - idata=builddia(iday,imonth,iyear); - computedate(iyear,imonth,iday,&tt); - t=&tt; - } - if (t==NULL) { - debuga(_("Unknown input log file format\n")); - break; - } - - strftime(dia, sizeof(dia), "%d/%m/%Y", t); - snprintf(hora,sizeof(hora),"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec); - - if(debugm) - printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil); - - if(date[0] != '\0'){ - if(idata < dfrom || idata > duntil) continue; - } - - // Record only hours usage which is required - if (t) { - if( bsearch( &( t -> tm_wday ), weekdays.list, weekdays.len, - sizeof( int ), compar ) == NULL ) - continue; - - if( bsearch( &( t -> tm_hour ), hours.list, hours.len, - sizeof( int ), compar ) == NULL ) - continue; - } - - - if(strlen(user) > MAX_USER_LEN) { - if (debugm) printf(_("User ID too long: %s\n"),user); - totregsx++; - continue; - } - - // include_users - if(IncludeUsers[0] != '\0') { - sprintf(val1,":%s:",user); - if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL ) - continue; - } - - if(vercode(code)) { - if (debugm) printf(_("Excluded code: %s\n"),code); - totregsx++; - continue; - } - - if(testvaliduserchar(user)) - continue; + init_usertab(UserTabFile); + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read a log file\n")); + exit(EXIT_FAILURE); + } + + snprintf(sz_Download_Unsort,sizeof(sz_Download_Unsort),"%s/download.unsort", tmp); + + if(DataFile[0]=='\0') { + if((ReportType & REPORT_TYPE_DENIED) != 0) { + if((fp_denied=MY_FOPEN(denied_unsort,"w"))==NULL) { + debuga(_("(log) Cannot open file: %s - %s\n"),denied_unsort,strerror(errno)); + exit(EXIT_FAILURE); + } + } + + if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) { + if((fp_authfail=MY_FOPEN(authfail_unsort,"w"))==NULL) { + debuga(_("(log) Cannot open file: %s - %s\n"),authfail_unsort,strerror(errno)); + exit(EXIT_FAILURE); + } + } + } + + for (iarq=0 ; iarqtm_year+1900)*10000+(logtime->tm_mon+1)*100+logtime->tm_mday0) { + for (i=0 ; itm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday; + } + } + if (ilf==ILF_Sarg) { + getword_start(&gwarea,linebuf); + if (getword(data,sizeof(data),&gwarea,'\t')<0){ + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword(hora,sizeof(hora),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword(user,sizeof(user),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword_ptr(linebuf,&url,&gwarea,'\t')<0){ + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword(tam,sizeof(tam),&gwarea,'\t')<0){ + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword(code,sizeof(code),&gwarea,'\t')<0){ + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword(elap,sizeof(elap),&gwarea,'\t')<0){ + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword(smartfilter,sizeof(smartfilter),&gwarea,'\0')<0){ + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + getword_start(&gwarea,data); + if (getword_atoll(&iday,&gwarea,'/')<0 || iday<1 || iday>31){ + debuga(_("Maybe you have a broken date in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword_atoll(&imonth,&gwarea,'/')<0 || imonth<1 || imonth>12){ + debuga(_("Maybe you have a broken date in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword_atoll(&iyear,&gwarea,'\0')<0){ + debuga(_("Maybe you have a broken date in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + idata=builddia(iday,imonth,iyear); + computedate(iyear,imonth,iday,&tt); + t=&tt; + } + if (ilf==ILF_Isa) { + if (linebuf[0] == '#') { + int ncols,cols[ISACOL_Last]; + + fixendofline(linebuf); + getword_start(&gwarea,linebuf); + // remove the #Fields: column at the beginning of the line + if (getword_skip(1000,&gwarea,' ')<0){ + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + for (ncols=0 ; ncols=0) { + isa_ncols=ncols; + for (ncols=0 ; ncols=sizeof(ip)) { + debuga(_("Maybe you have a broken IP in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + strcpy(ip,str); + } else if (x==isa_cols[ISACOL_UserName]) { + if (strlen(str)>=sizeof(user)) { + debuga(_("Maybe you have a broken user ID in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + strcpy(user,str); + } else if (x==isa_cols[ISACOL_Date]) { + if (strlen(str)>=sizeof(data)) { + debuga(_("Maybe you have a broken date in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + strcpy(data,str); + } else if (x==isa_cols[ISACOL_Time]) { + if (strlen(str)>=sizeof(hora)) { + debuga(_("Maybe you have a broken time in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + strcpy(hora,str); + } else if (x==isa_cols[ISACOL_TimeTaken]) { + if (strlen(str)>=sizeof(elap)) { + debuga(_("Maybe you have a broken download duration in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + strcpy(elap,str); + } else if (x==isa_cols[ISACOL_Bytes]) { + if (strlen(str)>=sizeof(tam)) { + debuga(_("Maybe you have a broken download size in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + strcpy(tam,str); + } else if (x==isa_cols[ISACOL_Uri]) { + url=str; + } else if (x==isa_cols[ISACOL_Status]) { + if (strlen(str)>=sizeof(code)) { + debuga(_("Maybe you have a broken access code in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + strcpy(code,str); + } + } + + if(strcmp(code,"401") == 0 || strcmp(code,"403") == 0 || strcmp(code,"407") == 0) { + sprintf(val1,"DENIED/%s",code); + strcpy(code,val1); + } + getword_start(&gwarea,data); + if (getword_atoll(&iyear,&gwarea,'-')<0){ + debuga(_("Maybe you have a broken year in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword_atoll(&imonth,&gwarea,'-')<0){ + debuga(_("Maybe you have a broken month in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + if (getword_atoll(&iday,&gwarea,'\0')<0){ + debuga(_("Maybe you have a broken day in your %s file\n"),arq); + exit(EXIT_FAILURE); + } + + idata=builddia(iday,imonth,iyear); + computedate(iyear,imonth,iday,&tt); + t=&tt; + } + if (t==NULL) { + debuga(_("Unknown input log file format\n")); + break; + } + + strftime(dia, sizeof(dia), "%d/%m/%Y", t); + snprintf(hora,sizeof(hora),"%02d:%02d:%02d",t->tm_hour,t->tm_min,t->tm_sec); + + if(debugm) + printf("DATE=%s IDATA=%d DFROM=%d DUNTIL=%d\n",date,idata,dfrom,duntil); + + if(date[0] != '\0'){ + if(idata < dfrom || idata > duntil) continue; + } + + // Record only hours usage which is required + if (t) { + if( bsearch( &( t -> tm_wday ), weekdays.list, weekdays.len, + sizeof( int ), compar ) == NULL ) + continue; + + if( bsearch( &( t -> tm_hour ), hours.list, hours.len, + sizeof( int ), compar ) == NULL ) + continue; + } + + + if(strlen(user) > MAX_USER_LEN) { + if (debugm) printf(_("User ID too long: %s\n"),user); + totregsx++; + continue; + } + + // include_users + if(IncludeUsers[0] != '\0') { + sprintf(val1,":%s:",user); + if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL ) + continue; + } + + if(vercode(code)) { + if (debugm) printf(_("Excluded code: %s\n"),code); + totregsx++; + continue; + } + + if(testvaliduserchar(user)) + continue; #if 0 - if((str = strstr(user,"%20")) != NULL) { - /* - This is a patch introduced to solve bug #1624251 reported at sourceforge but - the side effect is to truncate the name at the first space and merge the reports - of people whose name is identical up to the first space. - - The old code used to truncate the user name at the first % if a %20 was - found anywhere in the string. That means the string could be truncated - at the wrong place if another % occured before the %20. This new code should - avoid that problem and only truncate at the space. There is no bug - report indicating that anybody noticed this. - */ - *str='\0'; - } - - /* - Code prior to 2.2.7 used to replace any %xx by a dot as long as a %5c was - found in the user name. - */ - while((str = strstr(user,"%5c")) != NULL) { - *str='.'; - for (x=3 ; str[x] ; x++) str[x-2]=str[x]; - } + if((str = strstr(user,"%20")) != NULL) { + /* + This is a patch introduced to solve bug #1624251 reported at sourceforge but + the side effect is to truncate the name at the first space and merge the reports + of people whose name is identical up to the first space. + + The old code used to truncate the user name at the first % if a %20 was + found anywhere in the string. That means the string could be truncated + at the wrong place if another % occured before the %20. This new code should + avoid that problem and only truncate at the space. There is no bug + report indicating that anybody noticed this. + */ + *str='\0'; + } + + /* + Code prior to 2.2.7 used to replace any %xx by a dot as long as a %5c was + found in the user name. + */ + while((str = strstr(user,"%5c")) != NULL) { + *str='.'; + for (x=3 ; str[x] ; x++) str[x-2]=str[x]; + } #endif - urly=url; - - if(ilf!=ILF_Sarg) { - /* - The full URL is not saved in sarg log. There is no point in testing the URL to detect - a downloaded file. - */ - download_flag=is_download_suffix(url); - if (download_flag) { - download_url=url; - download_count++; - } - } else - download_flag=false; - - // remove any protocol:// at the beginning of the URL - if ((str = strchr(url,'/')) != NULL && str[1] == '/') { - int i; - - str+=2; - for (i=0 ; str[i] ; i++) - url[i]=str[i]; - url[i]='\0'; - } - - if(!LongUrl) { - url_hostname(url,hostname,sizeof(hostname)); - url=hostname; - } - - if(url[0] == '\0') continue; - - if(addr[0] != '\0'){ - if(strcmp(addr,ip)!=0) continue; - } - if(fhost) { - if(!vhexclude(url)) { - if (debugm) printf(_("Excluded site: %s\n"),url); - totregsx++; - continue; - } - } - - if(hm >= 0 && hmf >= 0) { - hmr=t->tm_hour*100+t->tm_min; - if(hmr < hm || hmr > hmf) continue; - } - - if(site[0] != '\0'){ - if(strstr(url,site)==0) continue; - } - - if(UserIp) { - strcpy(user,ip); - id_is_ip=true; - } else { - id_is_ip=false; - if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) { - if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) { - strcpy(user,ip); - id_is_ip=true; - } - if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE) - continue; - if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY) - strcpy(user,"everybody"); - } else { - strlow(user); - if(NtlmUserFormat == NTLMUSERFORMAT_USER) { - if((str = strchr(user,'_')) != 0) { - strcpy(warea,str+1); - strcpy(user,warea); - } - if((str = strchr(user,'+')) != 0) { - strcpy(warea,str+1); - strcpy(user,warea); - } - } - } - } - - if(us[0] != '\0'){ - if(strcmp(user,us)!=0) continue; - } - - if(puser) { - sprintf(wuser,":%s:",user); - if(strstr(userfile, wuser) == 0) - continue; - } - - if(fuser) { - if(!vuexclude(user)) { - if (debugm) printf(_("Excluded user: %s\n"),user); - totregsx++; - continue; - } - } - - if(strcmp(user,"-") ==0 || strcmp(user," ") ==0 || strcmp(user,"") ==0 || strcmp(user,":") ==0) - continue; - - if(max_elapsed) { - if(atol(elap)>max_elapsed) { - elap[0]='0'; - elap[1]='\0'; - } - } - - if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) { - fixendofline(str); - sprintf(smartfilter,"\"%s\"",str+1); - } else sprintf(smartfilter,"\"\""); - - nopen=0; - prev_ufile=NULL; - for (ufile=first_user_file ; ufile && strcmp(user,ufile->user->id)!=0 ; ufile=ufile->next) { - prev_ufile=ufile; - if (ufile->file) nopen++; - } - if (!ufile) { - ufile=malloc(sizeof(*ufile)); - if (!ufile) { - debuga(_("Not enough memory to store the user %s\n"),user); - exit(EXIT_FAILURE); - } - memset(ufile,0,sizeof(*ufile)); - ufile->next=first_user_file; - first_user_file=ufile; - uinfo=userinfo_create(user); - ufile->user=uinfo; - uinfo->id_is_ip=id_is_ip; - } else { - if (prev_ufile) { - prev_ufile->next=ufile->next; - ufile->next=first_user_file; - first_user_file=ufile; - } - } - - if (ufile->file==NULL) { - if (nopen>=maxopenfiles) { - x=0; - for (ufile1=first_user_file ; ufile1 ; ufile1=ufile1->next) { - if (ufile1->file!=NULL) { - if (x>=maxopenfiles) { - if (fclose(ufile1->file)==EOF) { - debuga(_("Failed to close the log file of user %s - %s\n"),ufile1->user->id,strerror(errno)); - exit(EXIT_FAILURE); - } - ufile1->file=NULL; - } - x++; - } - } - } - if (snprintf (tmp3, sizeof(tmp3), "%s/%s.unsort", tmp, ufile->user->filename)>=sizeof(tmp3)) { - debuga(_("Temporary user file name too long: %s/%s.unsort\n"), tmp, ufile->user->filename); - exit(EXIT_FAILURE); - } - if ((ufile->file = MY_FOPEN (tmp3, "a")) == NULL) { - debuga(_("(log) Cannot open temporary file: %s - %s\n"), tmp3, strerror(errno)); - exit (1); - } - } - - /*if ( strcmp ( user , sz_Last_User ) != 0 ) { - if ( fp_Write_User ) - fclose( fp_Write_User ) ; - sprintf (tmp3, "%s/%s.unsort", tmp, user); - - if ((fp_Write_User = MY_FOPEN (tmp3, "a")) == NULL) { - fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], _("Cannot open temporary file"), tmp3, strerror(errno)); - exit (1); - } - strcpy( sz_Last_User , user ) ; - }*/ - if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,ip,url,tam,code,elap,smartfilter)<=0) { - debuga(_("Write error in the log file of user %s\n"),user); - exit(EXIT_FAILURE); - } - - if(fp_log && ilf!=ILF_Sarg) - fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter); - - totregsg++; - - if(!dataonly && download_flag && download_url && strstr(code,"DENIED") == 0) { - ndownload = 1; - - if ( ! fp_Download_Unsort ) { - if ((fp_Download_Unsort = MY_FOPEN ( sz_Download_Unsort, "a")) == NULL) { - debuga(_("(log) Cannot open temporary file: %s - %s\n"),sz_Download_Unsort, strerror(errno)); - exit (1); - } - } - fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,download_url); - } - - if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) { - if(fp_denied && strstr(code,"DENIED/403") != 0) { - fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly); - denied_count++; - } - if(fp_authfail && (strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0)) { - fprintf(fp_authfail, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly); - authfail_count++; - } - } - - if (ilf!=ILF_Sarg) { - if(!totper || idatamaxdate) { - maxdate=idata; - memcpy(&period.end,t,sizeof(*t)); - } - totper=true; - } - - if(debugm){ - printf("IP=\t%s\n",ip); - printf("USER=\t%s\n",user); - printf("ELAP=\t%s\n",elap); - printf("DATE=\t%s\n",dia); - printf("TIME=\t%s\n",hora); - printf("FUNC=\t%s\n",fun); - printf("URL=\t%s\n",url); - printf("CODE=\t%s\n",code); - printf("LEN=\t%s\n",tam); - } - } - if (!from_stdin) { - if (from_pipe) - pclose(fp_in); - else { - fclose(fp_in); - if( ShowReadStatistics ) - printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs1, (float) 100 ); - } - } - } - - if (debug) - debuga(_(" Records read: %ld, written: %ld, excluded: %ld\n"),totregsl,totregsg,totregsx); - - longline_destroy(&line); - if ( fp_Download_Unsort ) - fclose (fp_Download_Unsort); - - for (ufile=first_user_file ; ufile ; ufile=ufile1) { - ufile1=ufile->next; - if (ufile->file!=NULL) fclose(ufile->file); - free(ufile); - } - - free_download(); - free_excludecodes(); - free_exclude(); - - if(debug) { - int totalcount=0; - - for (ilf=0 ; ilf0 && ilf_count[ILF_Squid]>0) - debuga(_("Log with mixed records format (squid and common log)\n")); - - if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]==0) - debuga(_("Common log format\n")); - - if(ilf_count[ILF_Common]==0 && ilf_count[ILF_Squid]>0) - debuga(_("Squid log format\n")); - - if(ilf_count[ILF_Sarg]>0) - debuga(_("Sarg log format\n")); - - if(totalcount==0 && totregsg) - debuga(_("Log with invalid format\n")); - } - - if(!totregsg){ - debuga(_("No records found\n")); - debuga(_("End\n")); - if(fp_denied) fclose(fp_denied); - if(fp_authfail) fclose(fp_authfail); - userinfo_free(); - if(userfile) free(userfile); - close_usertab(); - exit(EXIT_SUCCESS); - } - - if (date[0]!='\0') { - char date0[30], date1[30]; - - strftime(date0,sizeof(date0),"%d/%m/%Y",&period.start); - strftime(date1,sizeof(date1),"%d/%m/%Y",&period.end); - debuga(_("Period covered by log files: %s-%s\n"),date0,date1); - getperiod_fromrange(&period,dfrom,duntil); - } - if (getperiod_buildtext(&period)<0) { - debuga(_("Failed to build the string representation of the date range\n")); - exit(EXIT_FAILURE); - } - - if(debugz){ - debugaz("data",dia); - debugaz("period",period.text); - } - - if(debug) - debuga(_("Period: %s\n"),period.text); + urly=url; + + if(ilf!=ILF_Sarg) { + /* + The full URL is not saved in sarg log. There is no point in testing the URL to detect + a downloaded file. + */ + download_flag=is_download_suffix(url); + if (download_flag) { + download_url=url; + download_count++; + } + } else + download_flag=false; + + // remove any protocol:// at the beginning of the URL + if ((str = strchr(url,'/')) != NULL && str[1] == '/') { + int i; + + str+=2; + for (i=0 ; str[i] ; i++) + url[i]=str[i]; + url[i]='\0'; + } + + if(!LongUrl) { + url_hostname(url,hostname,sizeof(hostname)); + url=hostname; + } + + if(url[0] == '\0') continue; + + if(addr[0] != '\0'){ + if(strcmp(addr,ip)!=0) continue; + } + if(fhost) { + if(!vhexclude(url)) { + if (debugm) printf(_("Excluded site: %s\n"),url); + totregsx++; + continue; + } + } + + if(hm >= 0 && hmf >= 0) { + hmr=t->tm_hour*100+t->tm_min; + if(hmr < hm || hmr > hmf) continue; + } + + if(site[0] != '\0'){ + if(strstr(url,site)==0) continue; + } + + if(UserIp) { + strcpy(user,ip); + id_is_ip=true; + } else { + id_is_ip=false; + if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) { + if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) { + strcpy(user,ip); + id_is_ip=true; + } + if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE) + continue; + if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY) + strcpy(user,"everybody"); + } else { + strlow(user); + if(NtlmUserFormat == NTLMUSERFORMAT_USER) { + if((str = strchr(user,'_')) != 0) { + strcpy(warea,str+1); + strcpy(user,warea); + } + if((str = strchr(user,'+')) != 0) { + strcpy(warea,str+1); + strcpy(user,warea); + } + } + } + } + + if(us[0] != '\0'){ + if(strcmp(user,us)!=0) continue; + } + + if(puser) { + sprintf(wuser,":%s:",user); + if(strstr(userfile, wuser) == 0) + continue; + } + + if(fuser) { + if(!vuexclude(user)) { + if (debugm) printf(_("Excluded user: %s\n"),user); + totregsx++; + continue; + } + } + + if(strcmp(user,"-") ==0 || strcmp(user," ") ==0 || strcmp(user,"") ==0 || strcmp(user,":") ==0) + continue; + + if(max_elapsed) { + if(atol(elap)>max_elapsed) { + elap[0]='0'; + elap[1]='\0'; + } + } + + if((str=(char *) strstr(linebuf, "[SmartFilter:")) != (char *) NULL ) { + fixendofline(str); + sprintf(smartfilter,"\"%s\"",str+1); + } else sprintf(smartfilter,"\"\""); + + nopen=0; + prev_ufile=NULL; + for (ufile=first_user_file ; ufile && strcmp(user,ufile->user->id)!=0 ; ufile=ufile->next) { + prev_ufile=ufile; + if (ufile->file) nopen++; + } + if (!ufile) { + ufile=malloc(sizeof(*ufile)); + if (!ufile) { + debuga(_("Not enough memory to store the user %s\n"),user); + exit(EXIT_FAILURE); + } + memset(ufile,0,sizeof(*ufile)); + ufile->next=first_user_file; + first_user_file=ufile; + uinfo=userinfo_create(user); + ufile->user=uinfo; + uinfo->id_is_ip=id_is_ip; + } else { + if (prev_ufile) { + prev_ufile->next=ufile->next; + ufile->next=first_user_file; + first_user_file=ufile; + } + } + + if (ufile->file==NULL) { + if (nopen>=maxopenfiles) { + x=0; + for (ufile1=first_user_file ; ufile1 ; ufile1=ufile1->next) { + if (ufile1->file!=NULL) { + if (x>=maxopenfiles) { + if (fclose(ufile1->file)==EOF) { + debuga(_("Failed to close the log file of user %s - %s\n"),ufile1->user->id,strerror(errno)); + exit(EXIT_FAILURE); + } + ufile1->file=NULL; + } + x++; + } + } + } + if (snprintf (tmp3, sizeof(tmp3), "%s/%s.unsort", tmp, ufile->user->filename)>=sizeof(tmp3)) { + debuga(_("Temporary user file name too long: %s/%s.unsort\n"), tmp, ufile->user->filename); + exit(EXIT_FAILURE); + } + if ((ufile->file = MY_FOPEN (tmp3, "a")) == NULL) { + debuga(_("(log) Cannot open temporary file: %s - %s\n"), tmp3, strerror(errno)); + exit (1); + } + } + + /*if ( strcmp ( user , sz_Last_User ) != 0 ) { + if ( fp_Write_User ) + fclose( fp_Write_User ) ; + sprintf (tmp3, "%s/%s.unsort", tmp, user); + + if ((fp_Write_User = MY_FOPEN (tmp3, "a")) == NULL) { + fprintf (stderr, "%s: (log) %s: %s - %s\n", argv[0], _("Cannot open temporary file"), tmp3, strerror(errno)); + exit (1); + } + strcpy( sz_Last_User , user ) ; + }*/ + if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,ip,url,tam,code,elap,smartfilter)<=0) { + debuga(_("Write error in the log file of user %s\n"),user); + exit(EXIT_FAILURE); + } + + if(fp_log && ilf!=ILF_Sarg) + fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,url,tam,code,elap,smartfilter); + + totregsg++; + + if(!dataonly && download_flag && download_url && strstr(code,"DENIED") == 0) { + ndownload = 1; + + if ( ! fp_Download_Unsort ) { + if ((fp_Download_Unsort = MY_FOPEN ( sz_Download_Unsort, "a")) == NULL) { + debuga(_("(log) Cannot open temporary file: %s - %s\n"),sz_Download_Unsort, strerror(errno)); + exit (1); + } + } + fprintf(fp_Download_Unsort,"%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,download_url); + } + + if((ReportType & REPORT_TYPE_DENIED) != 0 || (ReportType & REPORT_TYPE_AUTH_FAILURES) != 0) { + if(fp_denied && strstr(code,"DENIED/403") != 0) { + fprintf(fp_denied, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly); + denied_count++; + } + if(fp_authfail && (strstr(code,"DENIED/401") != 0 || strstr(code,"DENIED/407") != 0)) { + fprintf(fp_authfail, "%s\t%s\t%s\t%s\t%s\n",dia,hora,user,ip,urly); + authfail_count++; + } + } + + if (ilf!=ILF_Sarg) { + if(!totper || idatamaxdate) { + maxdate=idata; + memcpy(&period.end,t,sizeof(*t)); + } + totper=true; + } + + if(debugm){ + printf("IP=\t%s\n",ip); + printf("USER=\t%s\n",user); + printf("ELAP=\t%s\n",elap); + printf("DATE=\t%s\n",dia); + printf("TIME=\t%s\n",hora); + printf("FUNC=\t%s\n",fun); + printf("URL=\t%s\n",url); + printf("CODE=\t%s\n",code); + printf("LEN=\t%s\n",tam); + } + } + if (!from_stdin) { + if (from_pipe) + pclose(fp_in); + else { + fclose(fp_in); + if( ShowReadStatistics ) + printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs1, (float) 100 ); + } + } + } + + if (debug) + debuga(_(" Records read: %ld, written: %ld, excluded: %ld\n"),totregsl,totregsg,totregsx); + + longline_destroy(&line); + if ( fp_Download_Unsort ) + fclose (fp_Download_Unsort); + + for (ufile=first_user_file ; ufile ; ufile=ufile1) { + ufile1=ufile->next; + if (ufile->file!=NULL) fclose(ufile->file); + free(ufile); + } + + free_download(); + free_excludecodes(); + free_exclude(); + + if(debug) { + int totalcount=0; + + for (ilf=0 ; ilf0 && ilf_count[ILF_Squid]>0) + debuga(_("Log with mixed records format (squid and common log)\n")); + + if(ilf_count[ILF_Common]>0 && ilf_count[ILF_Squid]==0) + debuga(_("Common log format\n")); + + if(ilf_count[ILF_Common]==0 && ilf_count[ILF_Squid]>0) + debuga(_("Squid log format\n")); + + if(ilf_count[ILF_Sarg]>0) + debuga(_("Sarg log format\n")); + + if(totalcount==0 && totregsg) + debuga(_("Log with invalid format\n")); + } + + if(!totregsg){ + debuga(_("No records found\n")); + debuga(_("End\n")); + if(fp_denied) fclose(fp_denied); + if(fp_authfail) fclose(fp_authfail); + userinfo_free(); + if(userfile) free(userfile); + close_usertab(); + exit(EXIT_SUCCESS); + } + + if (date[0]!='\0') { + char date0[30], date1[30]; + + strftime(date0,sizeof(date0),"%d/%m/%Y",&period.start); + strftime(date1,sizeof(date1),"%d/%m/%Y",&period.end); + debuga(_("Period covered by log files: %s-%s\n"),date0,date1); + getperiod_fromrange(&period,dfrom,duntil); + } + if (getperiod_buildtext(&period)<0) { + debuga(_("Failed to build the string representation of the date range\n")); + exit(EXIT_FAILURE); + } + + if(debugz){ + debugaz("data",dia); + debugaz("period",period.text); + } + + if(debug) + debuga(_("Period: %s\n"),period.text); // fclose(fp_ou); - if(fp_denied) - fclose(fp_denied); - if(fp_authfail) - fclose(fp_authfail); - - if(fp_log != NULL) { - fclose(fp_log); - strcpy(end_hour,tbuf2); - strftime(val2,sizeof(val2),"%d%m%Y",&period.start); - strftime(val1,sizeof(val1),"%d%m%Y",&period.end); - sprintf(val4,"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,start_hour,val1,end_hour); - if (rename(arq_log,val4)) { - debuga(_("failed to rename %s to %s - %s\n"),arq_log,val4,strerror(errno)); - } else { - strcpy(arq_log,val4); - - if(strcmp(ParsedOutputLogCompress,"nocompress") != 0 && ParsedOutputLogCompress[0] != '\0') { - /* - No double quotes around ParsedOutputLogCompress because it may contain command line options. If double quotes are - necessary around the command name, put them in the configuration file. - */ - sprintf(val1,"%s \"%s\"",ParsedOutputLogCompress,arq_log); - cstatus=system(val1); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("command: %s\n"),val1); - exit(EXIT_FAILURE); - } - } - } - if(debug) - debuga(_("Sarg parsed log saved as %s\n"),arq_log); - } - - if(DataFile[0] == '\0' && (ReportType & REPORT_TYPE_DENIED) != 0) { - sprintf(csort,"sort -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,denied_sort,denied_unsort); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - unlink(denied_unsort); - } - - sort_users_log(tmp, debug); - - if(DataFile[0] != '\0') - data_file(tmp); - else - gerarel(); - - if((ReportType & REPORT_TYPE_DENIED) != 0) - unlink(denied_sort); - - if(strcmp(tmp,"/tmp") != 0) { - unlinkdir(tmp,0); - } - - userinfo_free(); - if(userfile) - free(userfile); - close_usertab(); - - if(debug) - debuga(_("End\n")); - - exit(EXIT_SUCCESS); - + if(fp_denied) + fclose(fp_denied); + if(fp_authfail) + fclose(fp_authfail); + + if(fp_log != NULL) { + fclose(fp_log); + strcpy(end_hour,tbuf2); + strftime(val2,sizeof(val2),"%d%m%Y",&period.start); + strftime(val1,sizeof(val1),"%d%m%Y",&period.end); + sprintf(val4,"%s/sarg-%s_%s-%s_%s.log",ParsedOutputLog,val2,start_hour,val1,end_hour); + if (rename(arq_log,val4)) { + debuga(_("failed to rename %s to %s - %s\n"),arq_log,val4,strerror(errno)); + } else { + strcpy(arq_log,val4); + + if(strcmp(ParsedOutputLogCompress,"nocompress") != 0 && ParsedOutputLogCompress[0] != '\0') { + /* + No double quotes around ParsedOutputLogCompress because it may contain command line options. If double quotes are + necessary around the command name, put them in the configuration file. + */ + sprintf(val1,"%s \"%s\"",ParsedOutputLogCompress,arq_log); + cstatus=system(val1); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("command: %s\n"),val1); + exit(EXIT_FAILURE); + } + } + } + if(debug) + debuga(_("Sarg parsed log saved as %s\n"),arq_log); + } + + if(DataFile[0] == '\0' && (ReportType & REPORT_TYPE_DENIED) != 0) { + sprintf(csort,"sort -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"",tmp,denied_sort,denied_unsort); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + unlink(denied_unsort); + } + + sort_users_log(tmp, debug); + + if(DataFile[0] != '\0') + data_file(tmp); + else + gerarel(); + + if((ReportType & REPORT_TYPE_DENIED) != 0) + unlink(denied_sort); + + if(strcmp(tmp,"/tmp") != 0) { + unlinkdir(tmp,0); + } + + userinfo_free(); + if(userfile) + free(userfile); + close_usertab(); + + if(debug) + debuga(_("End\n")); + + exit(EXIT_SUCCESS); } static void getusers(const char *pwdfile, int debug) { - - FILE *fp_usr; - char buf[255]; - char *str; - long int nreg=0; - - if(debug) - debuga(_("Loading password file from %s\n"),pwdfile); - - if ((fp_usr = fopen(pwdfile, "r")) == NULL) { - debuga(_("(getusers) Cannot open file %s - %s\n"),pwdfile,strerror(errno)); - exit(EXIT_FAILURE); - } - - if (fseek(fp_usr, 0, SEEK_END)==-1) { - debuga(_("Failed to move till the end of the users file %s: %s\n"),pwdfile,strerror(errno)); - exit(EXIT_FAILURE); - } - nreg = ftell(fp_usr); - if (nreg<0) { - debuga(_("Cannot get the size of file %s\n"),pwdfile); - exit(EXIT_FAILURE); - } - nreg = nreg+5000; - if (fseek(fp_usr, 0, SEEK_SET)==-1) { - debuga(_("Failed to rewind the users file %s: %s\n"),pwdfile,strerror(errno)); - exit(EXIT_FAILURE); - } - - if((userfile=(char *) malloc(nreg))==NULL){ - debuga(_("malloc error (%ld)\n"),nreg); - exit(EXIT_FAILURE); - } - - bzero(userfile,nreg); - strcpy(userfile,":"); - - while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { - str=strchr(buf,':'); - if (!str) { - debuga(_("You have an invalid user in your %s file\n"),pwdfile); - exit(EXIT_FAILURE); - } - str[1]='\0'; - strcat(userfile,buf); - } - - fclose(fp_usr); - - return; + FILE *fp_usr; + char buf[255]; + char *str; + long int nreg=0; + + if(debug) + debuga(_("Loading password file from %s\n"),pwdfile); + + if ((fp_usr = fopen(pwdfile, "r")) == NULL) { + debuga(_("(getusers) Cannot open file %s - %s\n"),pwdfile,strerror(errno)); + exit(EXIT_FAILURE); + } + + if (fseek(fp_usr, 0, SEEK_END)==-1) { + debuga(_("Failed to move till the end of the users file %s: %s\n"),pwdfile,strerror(errno)); + exit(EXIT_FAILURE); + } + nreg = ftell(fp_usr); + if (nreg<0) { + debuga(_("Cannot get the size of file %s\n"),pwdfile); + exit(EXIT_FAILURE); + } + nreg = nreg+5000; + if (fseek(fp_usr, 0, SEEK_SET)==-1) { + debuga(_("Failed to rewind the users file %s: %s\n"),pwdfile,strerror(errno)); + exit(EXIT_FAILURE); + } + + if((userfile=(char *) malloc(nreg))==NULL){ + debuga(_("malloc error (%ld)\n"),nreg); + exit(EXIT_FAILURE); + } + + bzero(userfile,nreg); + strcpy(userfile,":"); + + while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { + str=strchr(buf,':'); + if (!str) { + debuga(_("You have an invalid user in your %s file\n"),pwdfile); + exit(EXIT_FAILURE); + } + str[1]='\0'; + strcat(userfile,buf); + } + + fclose(fp_usr); + + return; } diff --git a/longline.c b/longline.c index 9ef816d..07dfc25 100644 --- a/longline.c +++ b/longline.c @@ -31,119 +31,119 @@ struct longlinestruct { - //! The buffer to store the data read from the log file. - char *buffer; - //! The size of the buffer. - size_t size; - //! The number of bytes stored in the buffer. - size_t length; - //! The position of the beginning of the current string. - size_t start; - //! The position of the end of the current string. - size_t end; + //! The buffer to store the data read from the log file. + char *buffer; + //! The size of the buffer. + size_t size; + //! The number of bytes stored in the buffer. + size_t length; + //! The position of the beginning of the current string. + size_t start; + //! The position of the end of the current string. + size_t end; }; longline longline_create(void) { - longline line; + longline line; - line=malloc(sizeof(*line)); - if (line==NULL) return(NULL); - line->size=INITIAL_LINE_BUFFER_SIZE; - line->buffer=malloc(line->size); - if (line->buffer==NULL) { - free(line); - return(NULL); - } - line->start=0; - line->end=0; - line->length=0; - return(line); + line=malloc(sizeof(*line)); + if (line==NULL) return(NULL); + line->size=INITIAL_LINE_BUFFER_SIZE; + line->buffer=malloc(line->size); + if (line->buffer==NULL) { + free(line); + return(NULL); + } + line->start=0; + line->end=0; + line->length=0; + return(line); } void longline_reset(longline line) { - if (line!=NULL) { - line->start=0; - line->end=0; - line->length=0; - } + if (line!=NULL) { + line->start=0; + line->end=0; + line->length=0; + } } char *longline_read(FILE *fp_in,longline line) { - int i; - char *newbuf; - size_t nread; + int i; + char *newbuf; + size_t nread; - if (line==NULL || line->buffer==NULL) return(NULL); + if (line==NULL || line->buffer==NULL) return(NULL); - while (true) { - for (i=line->end ; ilength && (line->buffer[i]=='\n' || line->buffer[i]=='\r') ; i++); - if (ilength) { - line->end=i; - break; - } - nread=(feof(fp_in)!=0) ? 0 : fread(line->buffer,1,line->size,fp_in); - if (nread==0) return(NULL); - line->length=nread; - line->end=0; - } + while (true) { + for (i=line->end ; ilength && (line->buffer[i]=='\n' || line->buffer[i]=='\r') ; i++); + if (ilength) { + line->end=i; + break; + } + nread=(feof(fp_in)!=0) ? 0 : fread(line->buffer,1,line->size,fp_in); + if (nread==0) return(NULL); + line->length=nread; + line->end=0; + } - line->start=line->end; - while (true) { - for (i=line->end ; ilength ; i++) { - if ((unsigned char)line->buffer[i]>=' ') continue; - if (line->buffer[i]=='\n' || line->buffer[i]=='\r') break; - } + line->start=line->end; + while (true) { + for (i=line->end ; ilength ; i++) { + if ((unsigned char)line->buffer[i]>=' ') continue; + if (line->buffer[i]=='\n' || line->buffer[i]=='\r') break; + } - line->end=i; - if (line->endlength) break; + line->end=i; + if (line->endlength) break; - if (line->start>0) { - for (i=line->start ; ilength ; i++) line->buffer[i-line->start]=line->buffer[i]; - line->length-=line->start; - line->end-=line->start; - line->start=0; - } - if (line->length>=line->size) { - line->size+=8192; - newbuf=realloc(line->buffer,line->size); - if (!newbuf) { - debuga(_("Not enough memory to read one more line from the input log file\n")); - exit(EXIT_FAILURE); - } - line->buffer=newbuf; - } - nread=(feof(fp_in)!=0) ? 0 : fread(line->buffer+line->length,1,line->size-line->length,fp_in); - if (nread==0) { - if (line->end<=line->start) return(NULL); - if (line->end>=line->size) { - line->end=line->size; - line->size++; - newbuf=realloc(line->buffer,line->size); - if (!newbuf) { - debuga(_("Not enough memory to read one more line from the input log file\n")); - exit(EXIT_FAILURE); - } - line->buffer=newbuf; - } - line->buffer[line->end]='\0'; - return(line->buffer+line->start); - } - line->length+=nread; - } - line->buffer[line->end++]='\0'; - return(line->buffer+line->start); + if (line->start>0) { + for (i=line->start ; ilength ; i++) line->buffer[i-line->start]=line->buffer[i]; + line->length-=line->start; + line->end-=line->start; + line->start=0; + } + if (line->length>=line->size) { + line->size+=8192; + newbuf=realloc(line->buffer,line->size); + if (!newbuf) { + debuga(_("Not enough memory to read one more line from the input log file\n")); + exit(EXIT_FAILURE); + } + line->buffer=newbuf; + } + nread=(feof(fp_in)!=0) ? 0 : fread(line->buffer+line->length,1,line->size-line->length,fp_in); + if (nread==0) { + if (line->end<=line->start) return(NULL); + if (line->end>=line->size) { + line->end=line->size; + line->size++; + newbuf=realloc(line->buffer,line->size); + if (!newbuf) { + debuga(_("Not enough memory to read one more line from the input log file\n")); + exit(EXIT_FAILURE); + } + line->buffer=newbuf; + } + line->buffer[line->end]='\0'; + return(line->buffer+line->start); + } + line->length+=nread; + } + line->buffer[line->end++]='\0'; + return(line->buffer+line->start); } void longline_destroy(longline *line_ptr) { - longline line; + longline line; - if (line_ptr==NULL || *line_ptr==NULL) return; - line=*line_ptr; - *line_ptr=NULL; - if (line->buffer!=NULL) free(line->buffer); - free(line); + if (line_ptr==NULL || *line_ptr==NULL) return; + line=*line_ptr; + *line_ptr=NULL; + if (line->buffer!=NULL) free(line->buffer); + free(line); } diff --git a/realtime.c b/realtime.c index 9d47fdd..6dc382c 100755 --- a/realtime.c +++ b/realtime.c @@ -34,262 +34,259 @@ static void header(void); void realtime(void) { - - getlog(); - + getlog(); } static void getlog(void) { - FILE *tmp, *fp; - char template1[255]="/var/tmp/sargtpl1.XXXXXX"; - char template2[255]="/var/tmp/sargtpl2.XXXXXX"; - char cmd[512]; - char *buf; - int fd1,fd2; - int cstatus; - longline line; + FILE *tmp, *fp; + char template1[255]="/var/tmp/sargtpl1.XXXXXX"; + char template2[255]="/var/tmp/sargtpl2.XXXXXX"; + char cmd[512]; + char *buf; + int fd1,fd2; + int cstatus; + longline line; - init_usertab(UserTabFile); + init_usertab(UserTabFile); - fd1 = mkstemp(template1); - fd2 = mkstemp(template2); + fd1 = mkstemp(template1); + fd2 = mkstemp(template2); - if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */ - debuga(_("(realtime) mkstemp error - %s\n"),strerror(errno)); - exit(EXIT_FAILURE); - } + if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */ + debuga(_("(realtime) mkstemp error - %s\n"),strerror(errno)); + exit(EXIT_FAILURE); + } - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the log file\n")); - exit(EXIT_FAILURE); - } + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the log file\n")); + exit(EXIT_FAILURE); + } - sprintf(cmd,"tail -%d \"%s\"",realtime_access_log_lines,AccessLog[0]); - fp = popen(cmd, "r"); - if (!fp) { - debuga(_("Failed to get the %d trailing lines of %s: %s\n"),realtime_access_log_lines,AccessLog[0],strerror(errno)); - debuga(_("tail command: %s\n"),cmd); - exit(EXIT_FAILURE); - } - while((buf=longline_read(fp,line)) != NULL ) - if (getdata(buf,tmp)<0) { - debuga(_("Maybe a broken record or garbage was returned by %s\n"),cmd); - exit(EXIT_FAILURE); - } - pclose(fp); - fclose(tmp); - longline_destroy(&line); + sprintf(cmd,"tail -%d \"%s\"",realtime_access_log_lines,AccessLog[0]); + fp = popen(cmd, "r"); + if (!fp) { + debuga(_("Failed to get the %d trailing lines of %s: %s\n"),realtime_access_log_lines,AccessLog[0],strerror(errno)); + debuga(_("tail command: %s\n"),cmd); + exit(EXIT_FAILURE); + } + while((buf=longline_read(fp,line)) != NULL ) + if (getdata(buf,tmp)<0) { + debuga(_("Maybe a broken record or garbage was returned by %s\n"),cmd); + exit(EXIT_FAILURE); + } + pclose(fp); + fclose(tmp); + longline_destroy(&line); - sprintf(cmd,"sort -r -k 1,1 -k 2,2 -o \"%s\" \"%s\"",template2,template1); - cstatus=system(cmd); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),cmd); - exit(EXIT_FAILURE); - } - unlink(template1); - datashow(template2); + sprintf(cmd,"sort -r -k 1,1 -k 2,2 -o \"%s\" \"%s\"",template2,template1); + cstatus=system(cmd); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),cmd); + exit(EXIT_FAILURE); + } + unlink(template1); + datashow(template2); } static int getdata(char *rec, FILE *ftmp) { - time_t tt; - struct tm *t; - char dat[128]; - char tbuf[128]; - char typ[128]; - char warea[MAXLEN]; - char user[MAX_USER_LEN]; - char ip[25]; - char *url; - struct getwordstruct gwarea; + time_t tt; + struct tm *t; + char dat[128]; + char tbuf[128]; + char typ[128]; + char warea[MAXLEN]; + char user[MAX_USER_LEN]; + char ip[25]; + char *url; + struct getwordstruct gwarea; - getword_start(&gwarea,rec); - if (getword(dat,sizeof(dat),&gwarea,' ')<0) { - debuga(_("The time stamp at column 1 is too long\n")); - return(-1); - } - if (getword(warea,sizeof(warea),&gwarea,' ')<0) { - debuga(_("The connection duration at column 2 is too long\n")); - return(-1); - } - while(strcmp(warea,"") == 0 && gwarea.current[0] != '\0') - if (getword(warea,sizeof(warea),&gwarea,' ')<0) { - return(-1); - } - if (getword(ip,sizeof(ip),&gwarea,' ')<0) { - debuga(_("The IP address at column 3 is too long\n")); - return(-1); - } - if (getword_skip(MAXLEN,&gwarea,' ')<0) { - debuga(_("The status at column 4 is too long\n")); - return(-1); - } - if (getword_skip(MAXLEN,&gwarea,' ')<0) { - debuga(_("The size at column 5 is too long\n")); - return(-1); - } - if (getword(typ,sizeof(typ),&gwarea,' ')<0) { - debuga(_("The action at column 6 is too long\n")); - return(-1); - } - if(strncmp(typ,"CONNECT",7) == 0) { - if (getword_ptr(rec,&url,&gwarea,' ')<0) { - debuga(_("The URL at column 7 is too long\n")); - return(-1); - } - if (getword(user,sizeof(user),&gwarea,' ')<0) { - debuga(_("The user ID at column 8 is too long\n")); - return(-1); - } - }else { - if (getword_skip(MAXLEN,&gwarea,'/')<0) { - debuga(_("The URL at column 7 is too long\n")); - return(-1); - } - if (getword_skip(MAXLEN,&gwarea,'/')<0) { - debuga(_("The URL at column 7 is too long\n")); - return(-1); - } - if (getword_ptr(rec,&url,&gwarea,'/')<0) { - debuga(_("The URL at column 7 is too long\n")); - return(-1); - } - if (getword_skip(MAXLEN,&gwarea,' ')<0) { - debuga(_("The data at column 8 is too long\n")); - return(-1); - } - if (getword(user,sizeof(user),&gwarea,' ')<0) { - debuga(_("The user at column 9 is too long\n")); - return(-1); - } - } + getword_start(&gwarea,rec); + if (getword(dat,sizeof(dat),&gwarea,' ')<0) { + debuga(_("The time stamp at column 1 is too long\n")); + return(-1); + } + if (getword(warea,sizeof(warea),&gwarea,' ')<0) { + debuga(_("The connection duration at column 2 is too long\n")); + return(-1); + } + while(strcmp(warea,"") == 0 && gwarea.current[0] != '\0') + if (getword(warea,sizeof(warea),&gwarea,' ')<0) { + return(-1); + } + if (getword(ip,sizeof(ip),&gwarea,' ')<0) { + debuga(_("The IP address at column 3 is too long\n")); + return(-1); + } + if (getword_skip(MAXLEN,&gwarea,' ')<0) { + debuga(_("The status at column 4 is too long\n")); + return(-1); + } + if (getword_skip(MAXLEN,&gwarea,' ')<0) { + debuga(_("The size at column 5 is too long\n")); + return(-1); + } + if (getword(typ,sizeof(typ),&gwarea,' ')<0) { + debuga(_("The action at column 6 is too long\n")); + return(-1); + } + if(strncmp(typ,"CONNECT",7) == 0) { + if (getword_ptr(rec,&url,&gwarea,' ')<0) { + debuga(_("The URL at column 7 is too long\n")); + return(-1); + } + if (getword(user,sizeof(user),&gwarea,' ')<0) { + debuga(_("The user ID at column 8 is too long\n")); + return(-1); + } + }else { + if (getword_skip(MAXLEN,&gwarea,'/')<0) { + debuga(_("The URL at column 7 is too long\n")); + return(-1); + } + if (getword_skip(MAXLEN,&gwarea,'/')<0) { + debuga(_("The URL at column 7 is too long\n")); + return(-1); + } + if (getword_ptr(rec,&url,&gwarea,'/')<0) { + debuga(_("The URL at column 7 is too long\n")); + return(-1); + } + if (getword_skip(MAXLEN,&gwarea,' ')<0) { + debuga(_("The data at column 8 is too long\n")); + return(-1); + } + if (getword(user,sizeof(user),&gwarea,' ')<0) { + debuga(_("The user at column 9 is too long\n")); + return(-1); + } + } - if(strncmp(user,"-",1) == 0 && RealtimeUnauthRec==REALTIME_UNAUTH_REC_IGNORE) - return(0); + if(strncmp(user,"-",1) == 0 && RealtimeUnauthRec==REALTIME_UNAUTH_REC_IGNORE) + return(0); - tt=atoi(dat); - t=localtime(&tt); - if(strncmp(DateFormat,"u",1) == 0) - strftime(tbuf, sizeof(tbuf), "%Y-%m-%d\t%H:%M", t); - else if(strncmp(DateFormat,"e",1) == 0) - strftime(tbuf, sizeof(tbuf), "%d-%m-%Y\t%H:%M", t); + tt=atoi(dat); + t=localtime(&tt); + if(strncmp(DateFormat,"u",1) == 0) + strftime(tbuf, sizeof(tbuf), "%Y-%m-%d\t%H:%M", t); + else if(strncmp(DateFormat,"e",1) == 0) + strftime(tbuf, sizeof(tbuf), "%d-%m-%Y\t%H:%M", t); - fprintf(ftmp,"%s\t%s\t%s\t%s\t%s\n",tbuf,ip,user,url,typ); - return(0); + fprintf(ftmp,"%s\t%s\t%s\t%s\t%s\n",tbuf,ip,user,url,typ); + return(0); } static void datashow(const char *tmp) { - FILE *fin; - char dat[128]; - char tim[128]; - char *buf; - char *url; - char *ourl=NULL; - char ouser[MAX_USER_LEN]=""; - char typ[128]; - char user[MAX_USER_LEN]; - char u2[MAX_USER_LEN]; - char ip[25]; - int url_len; - int ourl_size=0; - struct getwordstruct gwarea; - longline line; - - if((fin=fopen(tmp,"r"))==NULL) { - debuga(_("(realtime) open error %s - %s\n"),tmp,strerror(errno)); - exit(EXIT_FAILURE); - } + FILE *fin; + char dat[128]; + char tim[128]; + char *buf; + char *url; + char *ourl=NULL; + char ouser[MAX_USER_LEN]=""; + char typ[128]; + char user[MAX_USER_LEN]; + char u2[MAX_USER_LEN]; + char ip[25]; + int url_len; + int ourl_size=0; + struct getwordstruct gwarea; + longline line; - header(); + if((fin=fopen(tmp,"r"))==NULL) { + debuga(_("(realtime) open error %s - %s\n"),tmp,strerror(errno)); + exit(EXIT_FAILURE); + } - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the log file\n")); - exit(EXIT_FAILURE); - } + header(); - while((buf=longline_read(fin,line))!=NULL) { - fixendofline(buf); - getword_start(&gwarea,buf); - if (getword(dat,sizeof(dat),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); - exit(EXIT_FAILURE); - } - if (getword(tim,sizeof(tim),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); - exit(EXIT_FAILURE); - } - if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); - exit(EXIT_FAILURE); - } - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); - exit(EXIT_FAILURE); - } - if(strlen(dat) < 3 || strlen(user) < 1) continue; - if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken url in your %s file\n"),tmp); - exit(EXIT_FAILURE); - } - if (getword(typ,sizeof(typ),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); - exit(EXIT_FAILURE); - } - if(strstr(RealtimeTypes,typ) == 0) - continue; + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the log file\n")); + exit(EXIT_FAILURE); + } - if(strcmp(ouser,user) == 0 && ourl && strcmp(ourl,url) == 0) - continue; + while((buf=longline_read(fin,line))!=NULL) { + fixendofline(buf); + getword_start(&gwarea,buf); + if (getword(dat,sizeof(dat),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); + exit(EXIT_FAILURE); + } + if (getword(tim,sizeof(tim),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); + exit(EXIT_FAILURE); + } + if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); + exit(EXIT_FAILURE); + } + if (getword(user,sizeof(user),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); + exit(EXIT_FAILURE); + } + if(strlen(dat) < 3 || strlen(user) < 1) continue; + if (getword_ptr(buf,&url,&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken url in your %s file\n"),tmp); + exit(EXIT_FAILURE); + } + if (getword(typ,sizeof(typ),&gwarea,'\t')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); + exit(EXIT_FAILURE); + } + if(strstr(RealtimeTypes,typ) == 0) + continue; - if(UserIp) - strcpy(user,ip); - strcpy(u2,user); - if(Ip2Name) - ip2name(u2,sizeof(u2)); - user_find(name, sizeof(name), u2); + if(strcmp(ouser,user) == 0 && ourl && strcmp(ourl,url) == 0) + continue; - printf("%s %s%s%s%s%s\n",dat,tim,ip,name,typ,url,url); - strcpy(ouser,user); + if(UserIp) + strcpy(user,ip); + strcpy(u2,user); + if(Ip2Name) + ip2name(u2,sizeof(u2)); + user_find(name, sizeof(name), u2); - url_len=strlen(url); - if (!ourl || url_len>=ourl_size) { - ourl_size=url_len+1; - ourl=realloc(ourl,ourl_size); - if (!ourl) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(ourl,url); - } - longline_destroy(&line); - if (ourl) free(ourl); + printf("%s %s%s%s%s%s\n",dat,tim,ip,name,typ,url,url); + strcpy(ouser,user); - puts("\n\n\n\n"); - fclose(fin); - unlink(tmp); - fflush(NULL); + url_len=strlen(url); + if (!ourl || url_len>=ourl_size) { + ourl_size=url_len+1; + ourl=realloc(ourl,ourl_size); + if (!ourl) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(ourl,url); + } + longline_destroy(&line); + if (ourl) free(ourl); + puts("\n\n\n\n"); + fclose(fin); + unlink(tmp); + fflush(NULL); } static void header(void) { - puts("\n"); - puts("\n"); - puts("\n"); - if(realtime_refresh) - printf(" \n",realtime_refresh,CharSet); - else - printf(" \n",CharSet); - css(stdout); - puts("\n"); - printf("\n",FontFace,TitleFontSize,BgColor,BgImage); - puts("
\n"); - printf("\n",_("Realtime")); - printf("\n",_("Auto refresh"),realtime_refresh); - printf("\n",_("DATE/TIME"),_("IP/NAME"),_("USERID"),_("TYPE"),_("ACCESSED SITE")); + puts("\n"); + puts("\n"); + puts("\n"); + if(realtime_refresh) + printf(" \n",realtime_refresh,CharSet); + else + printf(" \n",CharSet); + css(stdout); + puts("\n"); + printf("\n",FontFace,TitleFontSize,BgColor,BgImage); + puts("
SARG %s
%s: %d s
%s%s%s%s%s
\n"); + printf("\n",_("Realtime")); + printf("\n",_("Auto refresh"),realtime_refresh); + printf("\n",_("DATE/TIME"),_("IP/NAME"),_("USERID"),_("TYPE"),_("ACCESSED SITE")); } diff --git a/repday.c b/repday.c index 2b587d0..c22e79e 100644 --- a/repday.c +++ b/repday.c @@ -31,201 +31,200 @@ extern numlist hours; void report_day(const struct userinfostruct *uinfo) { - - FILE *fp_in, *fp_ou; - - char buf[200]; - char data[20]; - long long int hour; - const char *label; - char arqout[MAXLEN]; - char wdirname[2048]; - char colsep; - int ihour=0; - int day,month,year; - int daylist[MAX_DATETIME_DAYS]; - int daysort[MAX_DATETIME_DAYS]; - int ndaylist; - int daynum; - int dayidx; - long long int bytes; - long long int elap; - long long int tbytes[MAX_DATETIME_DAYS*24]; - long long int telap[MAX_DATETIME_DAYS*24]; - long long int tt; - long long int tttime[24]; - int i, j; - struct getwordstruct gwarea; - struct tm t; - - snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename); - if(access(wdirname, R_OK) != 0) return; - - if (snprintf(arqout,sizeof(arqout),"%s/%s/d%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqout)) { - debuga(_("Output file name too long: %s/%s/d%s.html\n"),outdirname,uinfo->filename,uinfo->filename); - exit(EXIT_FAILURE); - } - - if((fp_in=fopen(wdirname,"r"))==NULL) { - debuga(_("(repday) Cannot open log file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - - memset(tbytes,0,sizeof(tbytes)); - memset(telap,0,sizeof(telap)); - ndaylist=0; - - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - fixendofline(buf); - getword_start(&gwarea,buf); - if (getword(data,sizeof(data),&gwarea,'\t')<0) { - debuga(_("Invalid date in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; - if (day<1 || day>31 || month<1 || month>12 || year>9999) continue; - daynum=(year*10000)+(month*100)+day; - for (dayidx=0 ; dayidx=ndaylist) { - if (dayidx>=sizeof(daylist)/sizeof(*daylist)) { - debuga(_("Too many different dates in %s\n"),wdirname); - exit(EXIT_FAILURE); - } - daylist[ndaylist++]=daynum; - } - - if (getword_atoll(&hour,&gwarea,'\t')<0) { - debuga(_("Invalid time in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - ihour=(int)hour; - i=dayidx*24+ihour; - - if ((datetimeby & DATETIME_BYTE)!=0) { - colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; - if (getword_atoll(&bytes,&gwarea,colsep)<0) { - debuga(_("Invalid number of bytes in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - tbytes[i]+=bytes; - } - if ((datetimeby & DATETIME_ELAP)!=0) { - if (getword_atoll(&elap,&gwarea,'\0')<0) { - debuga(_("Invalid elapsed time in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - telap[i]+=elap; - } - } - fclose(fp_in); - - if((fp_ou=fopen(arqout,"w"))==NULL) { - debuga(_("(repday) Cannot open log file %s\n"),arqout); - exit(EXIT_FAILURE); - } - - write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"),HTML_JS_NONE); - fprintf(fp_ou,"\n",_("Period"),period.html); - fprintf(fp_ou,"\n",_("User"),uinfo->label); - close_html_header(fp_ou); - - dayidx=0; - for (i=0 ; i0 && daynum\n", fp_ou); - fputs("",fp_ou); - for( i = 0; i < hours.len; i++ ) - fprintf(fp_ou, "\n", hours.list[ i ], _("H"), label ); - fprintf(fp_ou, "\n", _("TOTAL"), label ); - - memset(tttime,0,sizeof(tttime)); - for (dayidx=0 ; dayidx\n", data ); - tt=0; - for( i = 0; i < hours.len; i++ ) { - ihour=hours.list[i]; - if (tbytes[daynum*24+ihour]>0) { - fprintf(fp_ou, "\n",fixnum(tbytes[daynum*24+ihour],1)); - tt+=tbytes[daynum*24+ihour]; - tttime[ihour]+=tbytes[daynum*24+ihour]; - } else - fputs("\n",fp_ou); - } - fprintf(fp_ou, "\n",fixnum(tt,1)); - } - - fprintf(fp_ou, "\n", _("TOTAL") ); - tt=0; - for( i = 0; i < hours.len; i++ ) { - if (tttime[i]>0) { - fprintf(fp_ou, "\n",fixnum(tttime[i],1)); - tt+=tttime[i]; - } else - fputs("\n",fp_ou); - } - fprintf(fp_ou, "\n",fixnum(tt,1)); - fputs("
SARG %s
%s: %d s
%s%s%s%s%s
%s: %s
%s: %s
%02d%s
%s
%s
%s
%s%s%s
%s%s%s
\n",fp_ou); - } - - if((datetimeby & DATETIME_ELAP)!=0) { - label=_("H:M:S"); - fputs("\n", fp_ou); - fputs("",fp_ou); - for( i = 0; i < hours.len; i++ ) - fprintf(fp_ou, "\n", hours.list[ i ], _("H"), label ); - fprintf(fp_ou, "\n", _("TOTAL"), label ); - - memset(tttime,0,sizeof(tttime)); - for (dayidx=0 ; dayidx\n", data ); - tt=0; - for( i = 0; i < hours.len; i++ ) { - ihour=hours.list[i]; - if (telap[daynum*24+ihour]>0) { - fprintf(fp_ou, "\n",fixtime(telap[daynum*24+ihour])); - tt+=telap[daynum*24+ihour]; - tttime[ihour]+=telap[daynum*24+ihour]; - } else - fputs("\n",fp_ou); - } - fprintf(fp_ou, "\n",fixtime(tt)); - } - - fprintf(fp_ou, "\n", _("TOTAL") ); - tt=0; - for( i = 0; i < hours.len; i++ ) { - if (tttime[i]>0) { - fprintf(fp_ou, "\n",fixtime(tttime[i])); - tt+=tttime[i]; - } else - fputs("\n",fp_ou); - } - fprintf(fp_ou, "\n",fixtime(tt)); - fputs("
%02d%s
%s
%s
%s
%s%s%s
%s%s%s
\n",fp_ou); - } - - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),arqout); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno)); - return; + FILE *fp_in, *fp_ou; + + char buf[200]; + char data[20]; + long long int hour; + const char *label; + char arqout[MAXLEN]; + char wdirname[2048]; + char colsep; + int ihour=0; + int day,month,year; + int daylist[MAX_DATETIME_DAYS]; + int daysort[MAX_DATETIME_DAYS]; + int ndaylist; + int daynum; + int dayidx; + long long int bytes; + long long int elap; + long long int tbytes[MAX_DATETIME_DAYS*24]; + long long int telap[MAX_DATETIME_DAYS*24]; + long long int tt; + long long int tttime[24]; + int i, j; + struct getwordstruct gwarea; + struct tm t; + + snprintf(wdirname,sizeof(wdirname),"%s/%s.day",tmp,uinfo->filename); + if(access(wdirname, R_OK) != 0) return; + + if (snprintf(arqout,sizeof(arqout),"%s/%s/d%s.html",outdirname,uinfo->filename,uinfo->filename)>=sizeof(arqout)) { + debuga(_("Output file name too long: %s/%s/d%s.html\n"),outdirname,uinfo->filename,uinfo->filename); + exit(EXIT_FAILURE); + } + + if((fp_in=fopen(wdirname,"r"))==NULL) { + debuga(_("(repday) Cannot open log file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + + memset(tbytes,0,sizeof(tbytes)); + memset(telap,0,sizeof(telap)); + ndaylist=0; + + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + fixendofline(buf); + getword_start(&gwarea,buf); + if (getword(data,sizeof(data),&gwarea,'\t')<0) { + debuga(_("Invalid date in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; + if (day<1 || day>31 || month<1 || month>12 || year>9999) continue; + daynum=(year*10000)+(month*100)+day; + for (dayidx=0 ; dayidx=ndaylist) { + if (dayidx>=sizeof(daylist)/sizeof(*daylist)) { + debuga(_("Too many different dates in %s\n"),wdirname); + exit(EXIT_FAILURE); + } + daylist[ndaylist++]=daynum; + } + + if (getword_atoll(&hour,&gwarea,'\t')<0) { + debuga(_("Invalid time in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + ihour=(int)hour; + i=dayidx*24+ihour; + + if ((datetimeby & DATETIME_BYTE)!=0) { + colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; + if (getword_atoll(&bytes,&gwarea,colsep)<0) { + debuga(_("Invalid number of bytes in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + tbytes[i]+=bytes; + } + if ((datetimeby & DATETIME_ELAP)!=0) { + if (getword_atoll(&elap,&gwarea,'\0')<0) { + debuga(_("Invalid elapsed time in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + telap[i]+=elap; + } + } + fclose(fp_in); + + if((fp_ou=fopen(arqout,"w"))==NULL) { + debuga(_("(repday) Cannot open log file %s\n"),arqout); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Day report"),HTML_JS_NONE); + fprintf(fp_ou,"%s: %s\n",_("Period"),period.html); + fprintf(fp_ou,"%s: %s\n",_("User"),uinfo->label); + close_html_header(fp_ou); + + dayidx=0; + for (i=0 ; i0 && daynum\n", fp_ou); + fputs("",fp_ou); + for( i = 0; i < hours.len; i++ ) + fprintf(fp_ou, "%02d%s
%s\n", hours.list[ i ], _("H"), label ); + fprintf(fp_ou, "%s
%s\n", _("TOTAL"), label ); + + memset(tttime,0,sizeof(tttime)); + for (dayidx=0 ; dayidx%s\n", data ); + tt=0; + for( i = 0; i < hours.len; i++ ) { + ihour=hours.list[i]; + if (tbytes[daynum*24+ihour]>0) { + fprintf(fp_ou, "%s\n",fixnum(tbytes[daynum*24+ihour],1)); + tt+=tbytes[daynum*24+ihour]; + tttime[ihour]+=tbytes[daynum*24+ihour]; + } else + fputs("\n",fp_ou); + } + fprintf(fp_ou, "%s\n",fixnum(tt,1)); + } + + fprintf(fp_ou, "%s\n", _("TOTAL") ); + tt=0; + for( i = 0; i < hours.len; i++ ) { + if (tttime[i]>0) { + fprintf(fp_ou, "%s\n",fixnum(tttime[i],1)); + tt+=tttime[i]; + } else + fputs("\n",fp_ou); + } + fprintf(fp_ou, "%s\n",fixnum(tt,1)); + fputs("\n",fp_ou); + } + + if((datetimeby & DATETIME_ELAP)!=0) { + label=_("H:M:S"); + fputs("\n", fp_ou); + fputs("",fp_ou); + for( i = 0; i < hours.len; i++ ) + fprintf(fp_ou, "\n", hours.list[ i ], _("H"), label ); + fprintf(fp_ou, "\n", _("TOTAL"), label ); + + memset(tttime,0,sizeof(tttime)); + for (dayidx=0 ; dayidx\n", data ); + tt=0; + for( i = 0; i < hours.len; i++ ) { + ihour=hours.list[i]; + if (telap[daynum*24+ihour]>0) { + fprintf(fp_ou, "\n",fixtime(telap[daynum*24+ihour])); + tt+=telap[daynum*24+ihour]; + tttime[ihour]+=telap[daynum*24+ihour]; + } else + fputs("\n",fp_ou); + } + fprintf(fp_ou, "\n",fixtime(tt)); + } + + fprintf(fp_ou, "\n", _("TOTAL") ); + tt=0; + for( i = 0; i < hours.len; i++ ) { + if (tttime[i]>0) { + fprintf(fp_ou, "\n",fixtime(tttime[i])); + tt+=tttime[i]; + } else + fputs("\n",fp_ou); + } + fprintf(fp_ou, "\n",fixtime(tt)); + fputs("
%02d%s
%s
%s
%s
%s%s%s
%s%s%s
\n",fp_ou); + } + + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),arqout); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno)); + return; } diff --git a/report.c b/report.c index aa53a3b..499c75c 100644 --- a/report.c +++ b/report.c @@ -39,784 +39,774 @@ static void grava_SmartFilter(const char *dirname, const char *user, const char void gerarel(void) { - - FILE *fp_in; - FILE *fp_gen; - - char *buf; - char accdia[11], acchora[9], accip[MAXLEN], *accurl; - char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN]; - char wdirname[MAXLEN]; - char *oldurl=NULL; - char oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1], user[MAX_USER_LEN]; - char ipantes[MAXLEN], nameantes[MAXLEN]; - char accsmart[MAXLEN]; - char crc2[MAXLEN/2 -1]; - char siteind[MAX_TRUNCATED_URL]; - char arqtt[256]; - char *oldurltt=NULL; - char oldaccdiatt[11],oldacchoratt[9]; - char tmp3[MAXLEN]; - char tmp4[5]; - char u2[MAX_USER_LEN]; - long long int nbytes=0; - long long int nelap=0; - long long int nacc=0; - long long int rtotal=0; - long long int incache=0; - long long int oucache=0; - long long int accbytes, accelap; - char *str; - DIR *dirp; - struct dirent *direntp; - const char logext[]=".log"; - const char *sort_field; - const char *sort_order; - int dlen; - int url_len; - int ourl_size=0; - int ourltt_size=0; - int same_url; - int new_user; - struct getwordstruct gwarea; - longline line; - struct userinfostruct *uinfo,*puinfo; - - ipantes[0]='\0'; - smartfilter=0; - - if (vrfydir(&period, addr, site, us, email)<0) { - debuga(_("Cannot create the output directory name containing the period as part of the name\n")); - exit(EXIT_FAILURE); - } - - if(debugz){ - debugaz("outdirname",outdirname); - } - - if(UserAgentLog[0] != '\0' && email[0] == '\0') useragent(); - - snprintf(wdirname,sizeof(wdirname),"%s/sarg-general",outdirname); - if((fp_gen=MY_FOPEN(wdirname,"w"))==NULL){ - debuga(_("(report) Cannot open file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - - puinfo=NULL; - fp_tt=NULL; - - if ((dirp = opendir(tmp)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); - exit(EXIT_FAILURE); - } - while ((direntp = readdir( dirp )) != NULL ) { - dlen=strlen(direntp->d_name)-(sizeof(logext)-1); - if (dlen<0) continue; - if((strcmp(direntp->d_name+dlen,logext) != 0) || - (strncmp(direntp->d_name,"download.log",12) == 0) || - (strncmp(direntp->d_name,"denied.log",10) == 0) || - (strncmp(direntp->d_name,"authfail.log.unsort",19) == 0)) - continue; - if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) { - debuga(_("(report) directory entry too long: %s/%s\n"),tmp,direntp->d_name); - exit(EXIT_FAILURE); - } - if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){ - debuga(_("(report) Cannot open file %s\n"),tmp); - exit(EXIT_FAILURE); - } - - if (dlen>0) { - if (dlen>=sizeof(user)) continue; - strncpy(user,direntp->d_name,dlen); - user[dlen]=0; - } else { - user[0]='\0'; - } - - uinfo=userinfo_find_from_file(user); - if (!uinfo) { - debuga(_("Ignoring unknown user file %s\n"),user); - continue; - } - strcpy(u2,uinfo->id); - if(Ip2Name && uinfo->id_is_ip) { - strcpy(ipantes,u2); - ip2name(u2,sizeof(u2)); - strcpy(nameantes,u2); - } - user_find(uinfo->label,MAX_USER_LEN, u2); - - maketmp(user,tmp,debug,indexonly); - maketmp_hour(user,tmp,indexonly); - sort_labels(&sort_field,&sort_order); - - ttopen=0; - oldurltt=NULL; - ourltt_size=0; - memset(oldaccdiatt,0,sizeof(oldaccdiatt)); - memset(oldacchoratt,0,sizeof(oldacchoratt)); - new_user=1; - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the downloaded files\n")); - exit(EXIT_FAILURE); - } - - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 || - getword(accip,sizeof(accip),&gwarea,'\t')<0 || - getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 || - getword(acccode,sizeof(acccode),&gwarea,'\t')<0) { - debuga(_("There is a broken record or garbage in file %s\n"),tmp3); - exit(EXIT_FAILURE); - } - if(strncmp(acccode,"TCP_DENIED/407",14) == 0) continue; - if (getword_atoll(&accelap,&gwarea,'\t')<0) { - debuga(_("There is a broken elapsed time in file %s\n"),tmp3); - exit(EXIT_FAILURE); - } - if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) { - debuga(_("There is a broken smart info in file %s\n"),tmp3); - exit(EXIT_FAILURE); - } - - if(accsmart[0] != '\0') { - smartfilter++; - grava_SmartFilter(outdirname,uinfo->id,accip,accdia,acchora,accurl,accsmart); - } - - if(Ip2Name) { - if(strcmp(accip,ipantes) != 0) { - strcpy(ipantes,accip); - ip2name(accip,sizeof(accip)); - strcpy(nameantes,accip); - } else strcpy(accip,nameantes); - } - - gravatmp_hora(outdirname,uinfo,accdia,acchora,accelap,accbytes,indexonly); - - if(iprel){ - gravaporuser(uinfo,outdirname,accurl,accip,accdia,acchora,accbytes,accelap,indexonly); - } - - if(!rtotal){ - url_len=strlen(accurl); - if (!oldurl || url_len>=ourl_size) { - ourl_size=url_len+1; - oldurl=realloc(oldurl,ourl_size); - if (!oldurl) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(oldurl,accurl); - strcpy(oldacccode,acccode); - puinfo=uinfo; - strcpy(oldaccip,accip); - strcpy(oldaccdia,accdia); - strcpy(oldacchora,acchora); - new_user=0; - rtotal++; - } - same_url=(strcmp(oldurl,accurl) == 0); - - if(site[0] != '\0') { - if(new_user){ - if(strstr(oldacccode,"DENIED") != 0) - strcpy(oldmsg,"DENIED"); - else - strcpy(oldmsg,"OK"); - gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache); - gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache); - nacc=0; - nbytes=0; - nelap=0; - incache=0; - oucache=0; - } - } else { - if(!same_url || new_user){ - if(strstr(oldacccode,"DENIED") != 0) - strcpy(oldmsg,"DENIED"); - else - strcpy(oldmsg,"OK"); - gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache); - gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache); - nacc=0; - nbytes=0; - nelap=0; - incache=0; - oucache=0; - } - } - nacc++; - nbytes+=accbytes; - nelap+=accelap; - - if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0 && - (!oldurltt || strcmp(oldurltt,accurl) || strcmp(oldaccdiatt,accdia) || strcmp(oldacchoratt,acchora))) { - - if(!ttopen) { - snprintf(arqtt,sizeof(arqtt),"%s/%s",outdirname,uinfo->filename); - if(access(arqtt, R_OK) != 0) - my_mkdir(arqtt); - url_to_file(accurl,siteind,sizeof(siteind)); - snprintf(arqtt,sizeof(arqtt),"%s/%s/tt%s-%s.html",outdirname,uinfo->filename,uinfo->filename,siteind); - if ((fp_tt = fopen(arqtt, "w")) == 0) { - debuga(_("(report) Cannot open file %s\n"),arqtt); - exit(EXIT_FAILURE); - } - ttopen=1; - - /* - if(Privacy) - sprintf(httplink,"%s", \ - FontSize,PrivacyStringColor,PrivacyString,PrivacyString); - else - sprintf(httplink,"
%s",FontSize,accurl,accurl); - */ - - write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"),HTML_JS_NONE); - fprintf(fp_tt,"%s: %s\n",_("Period"),period.html); - fprintf(fp_tt,"%s: %s\n",_("User"),uinfo->label); - fputs("",fp_tt); - fprintf(fp_tt,_("Sort: %s, %s"),sort_field,sort_order); - fputs("\n",fp_tt); - fprintf(fp_tt,"%s\n",_("User")); - close_html_header(fp_tt); - - fputs("
\n",fp_tt); - strncpy(tmp4,_("DATE/TIME"),4); - tmp4[4]='\0'; - fprintf(fp_tt,"\n",_("ACCESSED SITE"),tmp4,_("DATE/TIME")+5); - } - - fputs("\n",accdia,acchora); - - url_len=strlen(accurl); - if (!oldurltt || url_len>=ourltt_size) { - ourltt_size=url_len+1; - oldurltt=realloc(oldurltt,ourltt_size); - if (!oldurltt) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(oldurltt,accurl); - strcpy(oldaccdiatt,accdia); - strcpy(oldacchoratt,acchora); - } - - strcpy(crc2,acccode); - str=strchr(crc2,'/'); - if (str) *str='\0'; - if(strstr(crc2,"MISS") != 0) - oucache+=accbytes; - else incache+=accbytes; - - if(new_user) { - new_user=0; - day_totalize(tmp,puinfo,indexonly); - } - - puinfo=uinfo; - strcpy(oldacccode,acccode); - strcpy(oldaccip,accip); - if (!same_url) { - url_len=strlen(accurl); - if (url_len>=ourl_size) { - ourl_size=url_len+1; - oldurl=realloc(oldurl,ourl_size); - if (!oldurl) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(oldurl,accurl); - } - strcpy(oldaccdia,accdia); - strcpy(oldacchora,acchora); - - } - fclose(fp_in); - longline_destroy(&line); - if (oldurltt) free(oldurltt); - unlink(tmp3); - } - closedir(dirp); - - if (oldurl) { - if(strstr(oldacccode,"DENIED") != 0) - strcpy(oldmsg,"DENIED"); - else - strcpy(oldmsg,"OK"); - gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache); - gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache); - free(oldurl); - } - fclose(fp_gen); - if (puinfo) day_totalize(tmp,puinfo,indexonly); - - tmpsort(); - - totalger(outdirname, debug, outdir); - - if(email[0] == '\0') { - if((ReportType & REPORT_TYPE_DOWNLOADS) != 0) download_report(); - - if(DansGuardianConf[0] != '\0') { - dansguardian_log(); - } - - squidguard_log(); - - topuser(); - - if((ReportType & REPORT_TYPE_TOPSITES) != 0) topsites(); - - if((ReportType & REPORT_TYPE_SITES_USERS) != 0) siteuser(); - gen_denied_report(); - - authfail_report(); - - if(smartfilter) smartfilter_report(); - - if(DansGuardianConf[0] != '\0') dansguardian_report(); - - squidguard_report(); - - if((ReportType & REPORT_TYPE_USERS_SITES) != 0) htmlrel(); - - make_index(); - - if(SuccessfulMsg) debuga(_("Successful report generated on %s\n"),outdirname); - } else { - geramail(outdirname, debug, outdir, email, tmp); - - if((strcmp(email,"stdout") != 0) && SuccessfulMsg) - debuga(_("Successful report generated and sent to %s\n"),email); - } - - if(indexonly) { - index_only(outdirname, debug); - } - - removetmp(outdirname); - return; + FILE *fp_in; + FILE *fp_gen; + + char *buf; + char accdia[11], acchora[9], accip[MAXLEN], *accurl; + char oldaccdia[11], oldacchora[9], oldaccip[MAXLEN]; + char wdirname[MAXLEN]; + char *oldurl=NULL; + char oldmsg[50], acccode[MAXLEN/2 - 1], oldacccode[MAXLEN/2 - 1], user[MAX_USER_LEN]; + char ipantes[MAXLEN], nameantes[MAXLEN]; + char accsmart[MAXLEN]; + char crc2[MAXLEN/2 -1]; + char siteind[MAX_TRUNCATED_URL]; + char arqtt[256]; + char *oldurltt=NULL; + char oldaccdiatt[11],oldacchoratt[9]; + char tmp3[MAXLEN]; + char tmp4[5]; + char u2[MAX_USER_LEN]; + long long int nbytes=0; + long long int nelap=0; + long long int nacc=0; + long long int rtotal=0; + long long int incache=0; + long long int oucache=0; + long long int accbytes, accelap; + char *str; + DIR *dirp; + struct dirent *direntp; + const char logext[]=".log"; + const char *sort_field; + const char *sort_order; + int dlen; + int url_len; + int ourl_size=0; + int ourltt_size=0; + int same_url; + int new_user; + struct getwordstruct gwarea; + longline line; + struct userinfostruct *uinfo,*puinfo; + + ipantes[0]='\0'; + smartfilter=0; + + if (vrfydir(&period, addr, site, us, email)<0) { + debuga(_("Cannot create the output directory name containing the period as part of the name\n")); + exit(EXIT_FAILURE); + } + + if(debugz){ + debugaz("outdirname",outdirname); + } + + if(UserAgentLog[0] != '\0' && email[0] == '\0') useragent(); + + snprintf(wdirname,sizeof(wdirname),"%s/sarg-general",outdirname); + if((fp_gen=MY_FOPEN(wdirname,"w"))==NULL){ + debuga(_("(report) Cannot open file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + + puinfo=NULL; + fp_tt=NULL; + + if ((dirp = opendir(tmp)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); + exit(EXIT_FAILURE); + } + while ((direntp = readdir( dirp )) != NULL ) { + dlen=strlen(direntp->d_name)-(sizeof(logext)-1); + if (dlen<0) continue; + if((strcmp(direntp->d_name+dlen,logext) != 0) || + (strncmp(direntp->d_name,"download.log",12) == 0) || + (strncmp(direntp->d_name,"denied.log",10) == 0) || + (strncmp(direntp->d_name,"authfail.log.unsort",19) == 0)) + continue; + if (snprintf(tmp3,sizeof(tmp3),"%s/%s",tmp,direntp->d_name)>=sizeof(tmp3)) { + debuga(_("(report) directory entry too long: %s/%s\n"),tmp,direntp->d_name); + exit(EXIT_FAILURE); + } + if((fp_in=MY_FOPEN(tmp3,"r"))==NULL){ + debuga(_("(report) Cannot open file %s\n"),tmp); + exit(EXIT_FAILURE); + } + + if (dlen>0) { + if (dlen>=sizeof(user)) continue; + strncpy(user,direntp->d_name,dlen); + user[dlen]=0; + } else { + user[0]='\0'; + } + + uinfo=userinfo_find_from_file(user); + if (!uinfo) { + debuga(_("Ignoring unknown user file %s\n"),user); + continue; + } + strcpy(u2,uinfo->id); + if(Ip2Name && uinfo->id_is_ip) { + strcpy(ipantes,u2); + ip2name(u2,sizeof(u2)); + strcpy(nameantes,u2); + } + user_find(uinfo->label,MAX_USER_LEN, u2); + + maketmp(user,tmp,debug,indexonly); + maketmp_hour(user,tmp,indexonly); + sort_labels(&sort_field,&sort_order); + + ttopen=0; + oldurltt=NULL; + ourltt_size=0; + memset(oldaccdiatt,0,sizeof(oldaccdiatt)); + memset(oldacchoratt,0,sizeof(oldacchoratt)); + new_user=1; + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the downloaded files\n")); + exit(EXIT_FAILURE); + } + + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword(accdia,sizeof(accdia),&gwarea,'\t')<0 || getword(acchora,sizeof(acchora),&gwarea,'\t')<0 || + getword(accip,sizeof(accip),&gwarea,'\t')<0 || + getword_ptr(buf,&accurl,&gwarea,'\t')<0 || getword_atoll(&accbytes,&gwarea,'\t')<0 || + getword(acccode,sizeof(acccode),&gwarea,'\t')<0) { + debuga(_("There is a broken record or garbage in file %s\n"),tmp3); + exit(EXIT_FAILURE); + } + if(strncmp(acccode,"TCP_DENIED/407",14) == 0) continue; + if (getword_atoll(&accelap,&gwarea,'\t')<0) { + debuga(_("There is a broken elapsed time in file %s\n"),tmp3); + exit(EXIT_FAILURE); + } + if (getword_skip(20000,&gwarea,'"')<0 || getword(accsmart,sizeof(accsmart),&gwarea,'"')<0) { + debuga(_("There is a broken smart info in file %s\n"),tmp3); + exit(EXIT_FAILURE); + } + + if(accsmart[0] != '\0') { + smartfilter++; + grava_SmartFilter(outdirname,uinfo->id,accip,accdia,acchora,accurl,accsmart); + } + + if(Ip2Name) { + if(strcmp(accip,ipantes) != 0) { + strcpy(ipantes,accip); + ip2name(accip,sizeof(accip)); + strcpy(nameantes,accip); + } else strcpy(accip,nameantes); + } + + gravatmp_hora(outdirname,uinfo,accdia,acchora,accelap,accbytes,indexonly); + + if(iprel){ + gravaporuser(uinfo,outdirname,accurl,accip,accdia,acchora,accbytes,accelap,indexonly); + } + + if(!rtotal){ + url_len=strlen(accurl); + if (!oldurl || url_len>=ourl_size) { + ourl_size=url_len+1; + oldurl=realloc(oldurl,ourl_size); + if (!oldurl) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(oldurl,accurl); + strcpy(oldacccode,acccode); + puinfo=uinfo; + strcpy(oldaccip,accip); + strcpy(oldaccdia,accdia); + strcpy(oldacchora,acchora); + new_user=0; + rtotal++; + } + same_url=(strcmp(oldurl,accurl) == 0); + + if(site[0] != '\0') { + if(new_user){ + if(strstr(oldacccode,"DENIED") != 0) + strcpy(oldmsg,"DENIED"); + else + strcpy(oldmsg,"OK"); + gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache); + gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache); + nacc=0; + nbytes=0; + nelap=0; + incache=0; + oucache=0; + } + } else { + if(!same_url || new_user){ + if(strstr(oldacccode,"DENIED") != 0) + strcpy(oldmsg,"DENIED"); + else + strcpy(oldmsg,"OK"); + gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache); + gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache); + nacc=0; + nbytes=0; + nelap=0; + incache=0; + oucache=0; + } + } + nacc++; + nbytes+=accbytes; + nelap+=accelap; + + if ((ReportType & REPORT_TYPE_SITE_USER_TIME_DATE) != 0 && + (!oldurltt || strcmp(oldurltt,accurl) || strcmp(oldaccdiatt,accdia) || strcmp(oldacchoratt,acchora))) { + + if(!ttopen) { + snprintf(arqtt,sizeof(arqtt),"%s/%s",outdirname,uinfo->filename); + if(access(arqtt, R_OK) != 0) + my_mkdir(arqtt); + url_to_file(accurl,siteind,sizeof(siteind)); + snprintf(arqtt,sizeof(arqtt),"%s/%s/tt%s-%s.html",outdirname,uinfo->filename,uinfo->filename,siteind); + if ((fp_tt = fopen(arqtt, "w")) == 0) { + debuga(_("(report) Cannot open file %s\n"),arqtt); + exit(EXIT_FAILURE); + } + ttopen=1; + + /* + if(Privacy) + sprintf(httplink,"%s", \ + FontSize,PrivacyStringColor,PrivacyString,PrivacyString); + else + sprintf(httplink,"%s",FontSize,accurl,accurl); + */ + + write_html_header(fp_tt,(IndexTree == INDEX_TREE_DATE) ? 4 : 2,_("Site access report"),HTML_JS_NONE); + fprintf(fp_tt,"\n",_("Period"),period.html); + fprintf(fp_tt,"\n",_("User"),uinfo->label); + fputs("\n",fp_tt); + fprintf(fp_tt,"\n",_("User")); + close_html_header(fp_tt); + + fputs("
%s%s%s
",fp_tt); - output_html_string(fp_tt,accurl,100); - fprintf(fp_tt,"%s%s
%s: %s
%s: %s
",fp_tt); + fprintf(fp_tt,_("Sort: %s, %s"),sort_field,sort_order); + fputs("
%s
\n",fp_tt); + strncpy(tmp4,_("DATE/TIME"),4); + tmp4[4]='\0'; + fprintf(fp_tt,"\n",_("ACCESSED SITE"),tmp4,_("DATE/TIME")+5); + } + + fputs("\n",accdia,acchora); + + url_len=strlen(accurl); + if (!oldurltt || url_len>=ourltt_size) { + ourltt_size=url_len+1; + oldurltt=realloc(oldurltt,ourltt_size); + if (!oldurltt) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(oldurltt,accurl); + strcpy(oldaccdiatt,accdia); + strcpy(oldacchoratt,acchora); + } + + strcpy(crc2,acccode); + str=strchr(crc2,'/'); + if (str) *str='\0'; + if(strstr(crc2,"MISS") != 0) + oucache+=accbytes; + else incache+=accbytes; + + if(new_user) { + new_user=0; + day_totalize(tmp,puinfo,indexonly); + } + + puinfo=uinfo; + strcpy(oldacccode,acccode); + strcpy(oldaccip,accip); + if (!same_url) { + url_len=strlen(accurl); + if (url_len>=ourl_size) { + ourl_size=url_len+1; + oldurl=realloc(oldurl,ourl_size); + if (!oldurl) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(oldurl,accurl); + } + strcpy(oldaccdia,accdia); + strcpy(oldacchora,acchora); + + } + fclose(fp_in); + longline_destroy(&line); + if (oldurltt) free(oldurltt); + unlink(tmp3); + } + closedir(dirp); + + if (oldurl) { + if(strstr(oldacccode,"DENIED") != 0) + strcpy(oldmsg,"DENIED"); + else + strcpy(oldmsg,"OK"); + gravatmp(puinfo,oldurl,nacc,nbytes,oldmsg,nelap,indexonly,incache,oucache); + gravager(fp_gen,puinfo,nacc,oldurl,nbytes,oldaccip,oldacchora,oldaccdia,nelap,incache,oucache); + free(oldurl); + } + fclose(fp_gen); + if (puinfo) day_totalize(tmp,puinfo,indexonly); + + tmpsort(); + + totalger(outdirname, debug, outdir); + + if(email[0] == '\0') { + if((ReportType & REPORT_TYPE_DOWNLOADS) != 0) download_report(); + + if(DansGuardianConf[0] != '\0') { + dansguardian_log(); + } + + squidguard_log(); + + topuser(); + + if((ReportType & REPORT_TYPE_TOPSITES) != 0) topsites(); + + if((ReportType & REPORT_TYPE_SITES_USERS) != 0) siteuser(); + gen_denied_report(); + + authfail_report(); + + if(smartfilter) smartfilter_report(); + + if(DansGuardianConf[0] != '\0') dansguardian_report(); + + squidguard_report(); + + if((ReportType & REPORT_TYPE_USERS_SITES) != 0) htmlrel(); + + make_index(); + + if(SuccessfulMsg) debuga(_("Successful report generated on %s\n"),outdirname); + } else { + geramail(outdirname, debug, outdir, email, tmp); + + if((strcmp(email,"stdout") != 0) && SuccessfulMsg) + debuga(_("Successful report generated and sent to %s\n"),email); + } + + if(indexonly) { + index_only(outdirname, debug); + } + + removetmp(outdirname); + return; } static void maketmp(const char *user, const char *dirname, int debug, int indexonly) { + FILE *fp_ou; + char wdirname[MAXLEN]; - FILE *fp_ou; - - char wdirname[MAXLEN]; + if(indexonly) return; + if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; - if(indexonly) return; - if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; + if(debug) debuga(_("Making file: %s/%s\n"),tmp,user); + if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,user)>=sizeof(wdirname)) { + debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,user); + exit(EXIT_FAILURE); + } - if(debug) debuga(_("Making file: %s/%s\n"),tmp,user); - if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,user)>=sizeof(wdirname)) { - debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,user); - exit(EXIT_FAILURE); - } + if((fp_ou=fopen(wdirname,"w"))==NULL){ + debuga(_("(report) Cannot open file %s\n"),wdirname); + exit(EXIT_FAILURE); + } - if((fp_ou=fopen(wdirname,"w"))==NULL){ - debuga(_("(report) Cannot open file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - - fclose(fp_ou); - return; + fclose(fp_ou); + return; } static void maketmp_hour(const char *user, const char *dirname, int indexonly) { + FILE *fp_ou; + char wdirname[MAXLEN]; - FILE *fp_ou; - - char wdirname[MAXLEN]; + if(indexonly) return; + if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; + if(datetimeby==0) return; - if(indexonly) return; - if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; - if(datetimeby==0) return; + if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,user)>=sizeof(wdirname)) { + debuga(_("Temporary file name too long: %s/%s.htmp\n"),tmp,user); + exit(EXIT_FAILURE); + } - if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,user)>=sizeof(wdirname)) { - debuga(_("Temporary file name too long: %s/%s.htmp\n"),tmp,user); - exit(EXIT_FAILURE); - } + if((fp_ou=fopen(wdirname,"w"))==NULL){ + debuga(_("(report-1) Cannot open file %s - %s\n"),wdirname,strerror(errno)); + exit(EXIT_FAILURE); + } - if((fp_ou=fopen(wdirname,"w"))==NULL){ - debuga(_("(report-1) Cannot open file %s - %s\n"),wdirname,strerror(errno)); - exit(EXIT_FAILURE); - } - - fclose(fp_ou); - return; + fclose(fp_ou); + return; } static void gravatmp(const struct userinfostruct *uinfo, const char *oldurl, long long int nacc, long long int nbytes, const char *oldmsg, long long int nelap, int indexonly, long long int incache, long long int oucache) { - FILE *fp_ou; - char wdirname[MAXLEN]; - - if(indexonly) return; - if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; - - if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(wdirname)) { - debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,uinfo->filename); - exit(EXIT_FAILURE); - } - - if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){ - debuga(_("(report) Cannot open file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - - /* - This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable - to print a long long int unless it is exactly 64-bits long. - */ - fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)nacc,(uint64_t)nbytes,oldurl,oldmsg,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache); - - if (fclose(fp_ou)==EOF) { - debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno)); - exit(EXIT_FAILURE); - } - ttopen=0; - - if(fp_tt) { - fputs("
%s%s%s
",fp_tt); + output_html_string(fp_tt,accurl,100); + fprintf(fp_tt,"%s%s
\n
\n",fp_tt); - fputs("\n\n",fp_tt); - fclose(fp_tt); - fp_tt=NULL; - } - - return; + FILE *fp_ou; + char wdirname[MAXLEN]; + + if(indexonly) return; + if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; + + if (snprintf(wdirname,sizeof(wdirname),"%s/%s.utmp",tmp,uinfo->filename)>=sizeof(wdirname)) { + debuga(_("Temporary file name too long: %s/%s.utmp\n"),tmp,uinfo->filename); + exit(EXIT_FAILURE); + } + + if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){ + debuga(_("(report) Cannot open file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + + /* + This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable + to print a long long int unless it is exactly 64-bits long. + */ + fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)nacc,(uint64_t)nbytes,oldurl,oldmsg,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache); + + if (fclose(fp_ou)==EOF) { + debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno)); + exit(EXIT_FAILURE); + } + ttopen=0; + + if(fp_tt) { + fputs("\n
\n",fp_tt); + fputs("\n\n",fp_tt); + fclose(fp_tt); + fp_tt=NULL; + } + + return; } static void gravatmp_hora(const char *dirname, const struct userinfostruct *uinfo, const char *data, const char *hora, long long int elap, long long int bytes, int indexonly) { - - FILE *fp_ou; - char wdirname[MAXLEN]; - int hour; - - if(indexonly || ((ReportType & REPORT_TYPE_USERS_SITES) == 0) || datetimeby==0) return; - - if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) { - debuga(_("Path too long %s/%s.htmp\n"),tmp,uinfo->filename); - exit(EXIT_FAILURE); - } - - if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){ - debuga(_("(report-2) Cannot open file %s - %s\n"),wdirname,strerror(errno)); - exit(EXIT_FAILURE); - } - - hour=atoi(hora); - fprintf(fp_ou,"%s\t%d",data,hour); - if((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%"PRIu64,(uint64_t)bytes); - if((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%"PRIu64,(uint64_t)elap); - fputs("\n",fp_ou); - - if (fclose(fp_ou)==EOF) { - debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno)); - exit(EXIT_FAILURE); - } - - return; + FILE *fp_ou; + char wdirname[MAXLEN]; + int hour; + + if(indexonly || ((ReportType & REPORT_TYPE_USERS_SITES) == 0) || datetimeby==0) return; + + if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) { + debuga(_("Path too long %s/%s.htmp\n"),tmp,uinfo->filename); + exit(EXIT_FAILURE); + } + + if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){ + debuga(_("(report-2) Cannot open file %s - %s\n"),wdirname,strerror(errno)); + exit(EXIT_FAILURE); + } + + hour=atoi(hora); + fprintf(fp_ou,"%s\t%d",data,hour); + if((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%"PRIu64,(uint64_t)bytes); + if((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%"PRIu64,(uint64_t)elap); + fputs("\n",fp_ou); + + if (fclose(fp_ou)==EOF) { + debuga(_("Failed to close file %s - %s\n"),wdirname,strerror(errno)); + exit(EXIT_FAILURE); + } + + return; } static void gravaporuser(const struct userinfostruct *uinfo, const char *dirname, const char *url, const char *ip, const char *data, const char *hora, long long int tam, long long int elap, int indexonly) { + FILE *fp_ou; + char wdirname[MAXLEN]; - FILE *fp_ou; - char wdirname[MAXLEN]; + if(indexonly || ((ReportType & REPORT_TYPE_USERS_SITES) == 0)) return; - if(indexonly || ((ReportType & REPORT_TYPE_USERS_SITES) == 0)) return; + if (snprintf(wdirname,sizeof(wdirname),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(wdirname)) { + debuga(_("Path too long %s/%s.ip\n"),tmp,uinfo->filename); + exit(EXIT_FAILURE); + } - if (snprintf(wdirname,sizeof(wdirname),"%s/%s.ip",tmp,uinfo->filename)>=sizeof(wdirname)) { - debuga(_("Path too long %s/%s.ip\n"),tmp,uinfo->filename); - exit(EXIT_FAILURE); - } + if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){ + debuga(_("(report) Cannot open file %s\n"),wdirname); + exit(EXIT_FAILURE); + } - if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){ - debuga(_("(report) Cannot open file %s\n"),wdirname); - exit(EXIT_FAILURE); - } + /* + This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable + to print a long long int unless it is exactly 64-bits long. + */ + fprintf(fp_ou,"%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\n",ip,url,data,hora,(uint64_t)tam,(uint64_t)elap); - /* - This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable - to print a long long int unless it is exactly 64-bits long. - */ - fprintf(fp_ou,"%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\n",ip,url,data,hora,(uint64_t)tam,(uint64_t)elap); - - fclose(fp_ou); - - return; + fclose(fp_ou); + return; } static void gravager(FILE *fp_gen, const struct userinfostruct *uinfo, long long int nacc, const char *url, long long int nbytes, const char *ip, const char *hora, const char *dia, long long int nelap, long long int incache, long long int oucache) { - /* - This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable - to print a long long int unless it is exactly 64-bits long. - */ - fprintf(fp_gen,"%s\t%"PRIu64"\t%"PRIu64"\t%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",uinfo->id,(uint64_t)nacc,(uint64_t)nbytes,url,ip,hora,dia,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache); - return; + /* + This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable + to print a long long int unless it is exactly 64-bits long. + */ + fprintf(fp_gen,"%s\t%"PRIu64"\t%"PRIu64"\t%s\t%s\t%s\t%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",uinfo->id,(uint64_t)nacc,(uint64_t)nbytes,url,ip,hora,dia,(uint64_t)nelap,(uint64_t)incache,(uint64_t)oucache); + return; } int ger_read(char *buffer,struct generalitemstruct *item,const char *filename) { - int i; - int sign; - long long int number; - - if (strncmp(buffer,"TOTAL\t",6)==0) { - item->total=1; - buffer+=6; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\t') { - debuga(_("Invalid total number of accesses in %s\n"),filename); - exit(EXIT_FAILURE); - } - buffer++; - item->nacc=number*sign; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\t') { - debuga(_("Invalid total size in %s\n"),filename); - exit(EXIT_FAILURE); - } - buffer++; - item->nbytes=number*sign; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\t') { - debuga(_("Invalid total elapsed time in %s\n"),filename); - exit(EXIT_FAILURE); - } - buffer++; - item->nelap=number*sign; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\t') { - debuga(_("Invalid total cache hit in %s\n"),filename); - exit(EXIT_FAILURE); - } - buffer++; - item->incache=number*sign; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\0') { - debuga(_("Invalid total cache miss in %s\n"),filename); - exit(EXIT_FAILURE); - } - item->oucache=number*sign; - } else { - item->total=0; - - item->user=buffer; - for (i=0 ; i=' ' ; i++) buffer++; - if (*buffer!='\t') { - debuga(_("User name too long or invalid in %s\n"),filename); - exit(EXIT_FAILURE); - } - *buffer++='\0'; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\t') { - debuga(_("Invalid number of accesses in %s\n"),filename); - exit(EXIT_FAILURE); - } - buffer++; - item->nacc=number*sign; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\t') { - debuga(_("Invalid number of bytes in %s\n"),filename); - exit(EXIT_FAILURE); - } - buffer++; - item->nbytes=number*sign; - - item->url=buffer; - while ((unsigned char)*buffer>=' ') buffer++; - if (*buffer!='\t') { - debuga(_("URL too long or invalid in %s\n"),filename); - exit(EXIT_FAILURE); - } - *buffer++='\0'; - - item->ip=buffer; - for (i=0 ; i=' ' ; i++) buffer++; - if (*buffer!='\t') { - debuga(_("IP address too long or invalid in %s\n"),filename); - exit(EXIT_FAILURE); - } - *buffer++='\0'; - - item->time=buffer; - for (i=0 ; i=' ' ; i++) buffer++; - if (*buffer!='\t') { - debuga(_("Time too long or invalid in %s\n"),filename); - exit(EXIT_FAILURE); - } - *buffer++='\0'; - - item->date=buffer; - for (i=0 ; i=' ' ; i++) buffer++; - if (*buffer!='\t') { - debuga(_("Date too long or invalid in %s\n"),filename); - exit(EXIT_FAILURE); - } - *buffer++='\0'; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\t') { - debuga(_("Invalid elapsed time in %s\n"),filename); - exit(EXIT_FAILURE); - } - buffer++; - item->nelap=number*sign; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\t') { - debuga(_("Invalid cache hit size in %s\n"),filename); - exit(EXIT_FAILURE); - } - buffer++; - item->incache=number*sign; - - sign=+1; - if (*buffer == '-') { - buffer++; - sign=-1; - } else if (*buffer == '+') { - buffer++; - } - number=0LL; - while (isdigit(*buffer)) - number=(number * 10) + (*buffer++)-'0'; - if (*buffer!='\0') { - debuga(_("Invalid cache miss size in %s\n"),filename); - exit(EXIT_FAILURE); - } - item->oucache=number*sign; - } - return(0); + int i; + int sign; + long long int number; + + if (strncmp(buffer,"TOTAL\t",6)==0) { + item->total=1; + buffer+=6; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid total number of accesses in %s\n"),filename); + exit(EXIT_FAILURE); + } + buffer++; + item->nacc=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid total size in %s\n"),filename); + exit(EXIT_FAILURE); + } + buffer++; + item->nbytes=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid total elapsed time in %s\n"),filename); + exit(EXIT_FAILURE); + } + buffer++; + item->nelap=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid total cache hit in %s\n"),filename); + exit(EXIT_FAILURE); + } + buffer++; + item->incache=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\0') { + debuga(_("Invalid total cache miss in %s\n"),filename); + exit(EXIT_FAILURE); + } + item->oucache=number*sign; + } else { + item->total=0; + + item->user=buffer; + for (i=0 ; i=' ' ; i++) buffer++; + if (*buffer!='\t') { + debuga(_("User name too long or invalid in %s\n"),filename); + exit(EXIT_FAILURE); + } + *buffer++='\0'; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid number of accesses in %s\n"),filename); + exit(EXIT_FAILURE); + } + buffer++; + item->nacc=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid number of bytes in %s\n"),filename); + exit(EXIT_FAILURE); + } + buffer++; + item->nbytes=number*sign; + + item->url=buffer; + while ((unsigned char)*buffer>=' ') buffer++; + if (*buffer!='\t') { + debuga(_("URL too long or invalid in %s\n"),filename); + exit(EXIT_FAILURE); + } + *buffer++='\0'; + + item->ip=buffer; + for (i=0 ; i=' ' ; i++) buffer++; + if (*buffer!='\t') { + debuga(_("IP address too long or invalid in %s\n"),filename); + exit(EXIT_FAILURE); + } + *buffer++='\0'; + + item->time=buffer; + for (i=0 ; i=' ' ; i++) buffer++; + if (*buffer!='\t') { + debuga(_("Time too long or invalid in %s\n"),filename); + exit(EXIT_FAILURE); + } + *buffer++='\0'; + + item->date=buffer; + for (i=0 ; i=' ' ; i++) buffer++; + if (*buffer!='\t') { + debuga(_("Date too long or invalid in %s\n"),filename); + exit(EXIT_FAILURE); + } + *buffer++='\0'; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid elapsed time in %s\n"),filename); + exit(EXIT_FAILURE); + } + buffer++; + item->nelap=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\t') { + debuga(_("Invalid cache hit size in %s\n"),filename); + exit(EXIT_FAILURE); + } + buffer++; + item->incache=number*sign; + + sign=+1; + if (*buffer == '-') { + buffer++; + sign=-1; + } else if (*buffer == '+') { + buffer++; + } + number=0LL; + while (isdigit(*buffer)) + number=(number * 10) + (*buffer++)-'0'; + if (*buffer!='\0') { + debuga(_("Invalid cache miss size in %s\n"),filename); + exit(EXIT_FAILURE); + } + item->oucache=number*sign; + } + return(0); } static void grava_SmartFilter(const char *dirname, const char *user, const char *ip, const char *data, const char *hora, const char *url, const char *smart) { + FILE *fp_ou; + char wdirname[MAXLEN]; - FILE *fp_ou; - char wdirname[MAXLEN]; - - sprintf(wdirname,"%s/smartfilter.unsort",dirname); - - if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){ - debuga(_("(report) Cannot open file %s\n"),wdirname); - exit(EXIT_FAILURE); - } + sprintf(wdirname,"%s/smartfilter.unsort",dirname); - fprintf(fp_ou,"%s\t%s\t%s\t%s\t%s\t%s\n",user,data,hora,ip,url,smart); - fputs("\n\n",fp_tt); + if((fp_ou=MY_FOPEN(wdirname,"a"))==NULL){ + debuga(_("(report) Cannot open file %s\n"),wdirname); + exit(EXIT_FAILURE); + } - fclose(fp_ou); + fprintf(fp_ou,"%s\t%s\t%s\t%s\t%s\t%s\n",user,data,hora,ip,url,smart); + fputs("\n\n",fp_tt); - return; + fclose(fp_ou); + return; } diff --git a/siteuser.c b/siteuser.c index 48bf876..d38f4c9 100644 --- a/siteuser.c +++ b/siteuser.c @@ -29,201 +29,200 @@ void siteuser(void) { - - FILE *fp_in, *fp_ou; - - char *buf; - char wuser[MAXLEN]; - char *ourl; - char csort[255]; - char general[MAXLEN]; - char general2[MAXLEN]; - char sites[MAXLEN]; - char report[MAXLEN]; - int regs=0; - int ucount=0; - int ourl_size; - int url_len; - char *users; - long long int obytes; - int cstatus; - longline line; - struct generalitemstruct item; - const struct userinfostruct *uinfo; - - if(Privacy) - return; - - nsitesusers = 0; - sprintf(general,"%s/sarg-general",outdirname); - sprintf(sites,"%s/sarg-sites",outdirname); - sprintf(general2,"%s/sarg-general2",outdirname); - sprintf(report,"%s/siteuser.html",outdirname); - - sprintf(csort,"sort -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - if((fp_in=fopen(general2,"r"))==NULL) { - debuga(_("(siteuser) Cannot open log file %s\n"),general2); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - if((fp_ou=fopen(report,"w"))==NULL) { - debuga(_("(siteuser) Cannot open log file %s\n"),report); - exit(EXIT_FAILURE); - } - - write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Sites & Users"),HTML_JS_SORTTABLE); - fputs("",fp_ou); - fprintf(fp_ou,_("Period: %s"),period.html); - fputs("\n",fp_ou); - fprintf(fp_ou,"%s\n",_("Sites & Users")); - close_html_header(fp_ou); - - fputs("
\n",_("ACCESSED SITE")); - if(BytesInSitesUsersReport) - fprintf(fp_ou,"",_("BYTES")); - fputs("\n",_("USERS")); - - ourl=NULL; - ourl_size=0; - obytes=0; - - if((users=(char *) malloc(204800))==NULL){ - debuga(_("ERROR: Cannot load. Memory fault\n")); - exit(EXIT_FAILURE); - } - strcpy(users," "); - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read file %s\n"),general2); - exit(EXIT_FAILURE); - } - - while((buf=longline_read(fp_in,line))!=NULL) { - ger_read(buf,&item,general2); - if(item.total) continue; - uinfo=userinfo_find_from_id(item.user); - if (!uinfo) { - debuga(_("Unknown user ID %s in file %s\n"),item.user,general2); - exit(EXIT_FAILURE); - } - - if (item.nacc > 0) nsitesusers = 1; - if(!regs) { - url_len=strlen(item.url); - if (!ourl || url_len>=ourl_size) { - ourl_size=url_len+1; - ourl=realloc(ourl,ourl_size); - if (!ourl) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(ourl,item.url); - obytes=item.nbytes; - regs++; - } - - sprintf(wuser," %s ",uinfo->label); - if(strstr(users,wuser) == 0 && strcmp(item.url,ourl) == 0) { - strcat(users,uinfo->label); - strcat(users," "); - ucount++; - if(ucount>4) { - strcat(users,"
"); - ucount=0; - } - } - - if(SiteUsersReportLimit) { - if(regs >= SiteUsersReportLimit) - continue; - } - - if(strcmp(item.url,ourl) != 0 && nsitesusers) { - fprintf(fp_ou,"",fp_ou); - - if (BytesInSitesUsersReport) { - fputs("",fixnum(obytes,1)); - } - fprintf(fp_ou,"\n",users); - - regs++; - ucount=0; - strcpy(users,uinfo->label); - strcat(users," "); - url_len=strlen(item.url); - if (url_len>=ourl_size) { - ourl_size=url_len+1; - ourl=realloc(ourl,ourl_size); - if (!ourl) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(ourl,item.url); - obytes=item.nbytes; - } - } - fclose(fp_in); - longline_destroy(&line); - - if(nsitesusers) { - fprintf(fp_ou,"",fp_ou); - if (BytesInSitesUsersReport) { - fputs("",fixnum(obytes,1)); - } - fprintf(fp_ou,"\n",users); - } - if (ourl) free(ourl); - - unlink(general2); - - fputs("
%s%s%s%s
%d",regs); - if(BlockIt[0]!='\0') { - fprintf(fp_ou," ",fp_ou); - } - fputs("",fp_ou); - output_html_string(fp_ou,ourl,100); - fputs("%s%s
%d",regs); - if(BlockIt[0]!='\0') { - fprintf(fp_ou," ",fp_ou); - } - fputs("",fp_ou); - output_html_string(fp_ou,ourl,100); - fputs("%s%s
\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),report); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - - if(users) - free(users); - - return; + FILE *fp_in, *fp_ou; + + char *buf; + char wuser[MAXLEN]; + char *ourl; + char csort[255]; + char general[MAXLEN]; + char general2[MAXLEN]; + char sites[MAXLEN]; + char report[MAXLEN]; + int regs=0; + int ucount=0; + int ourl_size; + int url_len; + char *users; + long long int obytes; + int cstatus; + longline line; + struct generalitemstruct item; + const struct userinfostruct *uinfo; + + if(Privacy) + return; + + nsitesusers = 0; + sprintf(general,"%s/sarg-general",outdirname); + sprintf(sites,"%s/sarg-sites",outdirname); + sprintf(general2,"%s/sarg-general2",outdirname); + sprintf(report,"%s/siteuser.html",outdirname); + + sprintf(csort,"sort -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + if((fp_in=fopen(general2,"r"))==NULL) { + debuga(_("(siteuser) Cannot open log file %s\n"),general2); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + if((fp_ou=fopen(report,"w"))==NULL) { + debuga(_("(siteuser) Cannot open log file %s\n"),report); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Sites & Users"),HTML_JS_SORTTABLE); + fputs("",fp_ou); + fprintf(fp_ou,_("Period: %s"),period.html); + fputs("\n",fp_ou); + fprintf(fp_ou,"%s\n",_("Sites & Users")); + close_html_header(fp_ou); + + fputs("
\n",_("ACCESSED SITE")); + if(BytesInSitesUsersReport) + fprintf(fp_ou,"",_("BYTES")); + fputs("\n",_("USERS")); + + ourl=NULL; + ourl_size=0; + obytes=0; + + if((users=(char *) malloc(204800))==NULL){ + debuga(_("ERROR: Cannot load. Memory fault\n")); + exit(EXIT_FAILURE); + } + strcpy(users," "); + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read file %s\n"),general2); + exit(EXIT_FAILURE); + } + + while((buf=longline_read(fp_in,line))!=NULL) { + ger_read(buf,&item,general2); + if(item.total) continue; + uinfo=userinfo_find_from_id(item.user); + if (!uinfo) { + debuga(_("Unknown user ID %s in file %s\n"),item.user,general2); + exit(EXIT_FAILURE); + } + + if (item.nacc > 0) nsitesusers = 1; + if(!regs) { + url_len=strlen(item.url); + if (!ourl || url_len>=ourl_size) { + ourl_size=url_len+1; + ourl=realloc(ourl,ourl_size); + if (!ourl) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(ourl,item.url); + obytes=item.nbytes; + regs++; + } + + sprintf(wuser," %s ",uinfo->label); + if(strstr(users,wuser) == 0 && strcmp(item.url,ourl) == 0) { + strcat(users,uinfo->label); + strcat(users," "); + ucount++; + if(ucount>4) { + strcat(users,"
"); + ucount=0; + } + } + + if(SiteUsersReportLimit) { + if(regs >= SiteUsersReportLimit) + continue; + } + + if(strcmp(item.url,ourl) != 0 && nsitesusers) { + fprintf(fp_ou,"",fp_ou); + + if (BytesInSitesUsersReport) { + fputs("",fixnum(obytes,1)); + } + fprintf(fp_ou,"\n",users); + + regs++; + ucount=0; + strcpy(users,uinfo->label); + strcat(users," "); + url_len=strlen(item.url); + if (url_len>=ourl_size) { + ourl_size=url_len+1; + ourl=realloc(ourl,ourl_size); + if (!ourl) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(ourl,item.url); + obytes=item.nbytes; + } + } + fclose(fp_in); + longline_destroy(&line); + + if(nsitesusers) { + fprintf(fp_ou,"",fp_ou); + if (BytesInSitesUsersReport) { + fputs("",fixnum(obytes,1)); + } + fprintf(fp_ou,"\n",users); + } + if (ourl) free(ourl); + + unlink(general2); + + fputs("
%s%s%s%s
%d",regs); + if(BlockIt[0]!='\0') { + fprintf(fp_ou," ",fp_ou); + } + fputs("",fp_ou); + output_html_string(fp_ou,ourl,100); + fputs("%s%s
%d",regs); + if(BlockIt[0]!='\0') { + fprintf(fp_ou," ",fp_ou); + } + fputs("",fp_ou); + output_html_string(fp_ou,ourl,100); + fputs("%s%s
\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),report); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); + + if(users) + free(users); + + return; } diff --git a/smartfilter.c b/smartfilter.c index d0d281f..aa6d5a2 100644 --- a/smartfilter.c +++ b/smartfilter.c @@ -29,173 +29,172 @@ void smartfilter_report(void) { - - FILE *fp_in = NULL, *fp_ou = NULL, *fp_user = NULL; - - char buf[MAXLEN]; - char url[MAXLEN]; - char csort[255]; - char smart_in[MAXLEN]; - char smart_ou[MAXLEN]; - char sites[MAXLEN]; - char report[MAXLEN]; - char ip[MAXLEN]; - char user[MAXLEN]; - char ouser[MAXLEN]; - char data[15]; - char hora[15]; - char smartcat[256]; - char smartheader[15]; - char ftime[128]; - char smartuser[MAXLEN]; - int fuser=0; - int cstatus; - struct getwordstruct gwarea; - const struct userinfostruct *uinfo; - - ouser[0]='\0'; - - strcpy(smartheader,_("SmartFilter")); - strup(smartheader); - - sprintf(smart_in,"%s/smartfilter.unsort",outdirname); - sprintf(sites,"%s/sarg-sites",outdirname); - sprintf(smart_ou,"%s/smartfilter.log",outdirname); - sprintf(report,"%s/smartfilter.html",outdirname); - - if (snprintf(csort,sizeof(csort),"sort -n -k 1,1 -k 2,2 -k 3,3 -o \"%s\" \"%s\"",smart_ou,smart_in)>=sizeof(csort)) { - debuga(_("cannot build the sort command to sort file %s\n"),smart_in); - exit(EXIT_FAILURE); - } - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - if((fp_in=fopen(smart_ou,"r"))==NULL) { - debuga(_("(smartfilter) Cannot open log file %s\n"),smart_ou); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - unlink(smart_in); - - if((fp_ou=fopen(report,"w"))==NULL) { - debuga(_("(smartfilter) Cannot open log file %s\n"),report); - exit(EXIT_FAILURE); - } - - fprintf(fp_ou, "\n\n\n \n",CharSet); - fputs("\n",fp_ou); - if(strlen(FontFace) > 0) fprintf(fp_ou,"\n",FontFace); - fprintf(fp_ou,"\n",BgColor,TxColor,BgImage); - fputs("
\n",fp_ou); - write_logo_image(fp_ou); - - fprintf(fp_ou,"\n",TiColor,Title); - fprintf(fp_ou,"\n",fp_ou); - fprintf(fp_ou,"\n",HeaderBgColor,FontSize,_("SmartFilter")); - fputs("
%s
",HeaderBgColor,FontSize); - fprintf(fp_ou,_("Period: %s"),period.html); - fputs("
%s
\n",fp_ou); - - fputs("
\n",fp_ou); - fputs("\n",fp_ou); - fputs("\n",fp_ou); - fputs("\n",fp_ou); - fprintf(fp_ou,"\n",HeaderBgColor,FontSize,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,smartheader); - - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - getword_start(&gwarea,buf); - if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(data,sizeof(data),&gwarea,'\t')<0 || - getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 || - getword(url,sizeof(url),&gwarea,'\t')<0 || getword(smartcat,sizeof(smartcat),&gwarea,'\n')<0) { - debuga(_("There is a broken record or garbage in file %s\n"),smart_ou); - exit(EXIT_FAILURE); - } - - uinfo=userinfo_find_from_id(user); - if (!uinfo) { - debuga(_("Unknown user ID %s in file %s\n"),user,smart_ou); - exit(EXIT_FAILURE); - } - if(strcmp(ouser,user) != 0) { - strcpy(ouser,user); - sprintf(smartuser,"%s/denied_%s.html",outdirname,uinfo->filename); - if(fuser) { - fuser=0; - fputs("
%s%s%s%s%s
\n",fp_user); - if(ShowSargInfo) { - zdate(ftime, sizeof(ftime), DateFormat); - fprintf(fp_user,"

%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); - } - fputs("\n\n",fp_user); - fclose(fp_user); - } - if ((fp_user = fopen(smartuser, "a")) == 0) { - debuga(_("(smartfilter) Cannot open file %s\n"),smartuser); - exit(EXIT_FAILURE); - } - fuser=1; - - fputs("\n",fp_ou); - fputs("\n",fp_user); - fputs("\n",fp_user); - fprintf(fp_user," \n",CharSet); - fputs("\n",fp_user); - - if(FontFace[0] != 0) { - /* - Before merging the sprintf and the fputs, the code looked like this: - sprintf(html2,"\n",FontFace); - fputs(url,fp_user); - The two lines don't use the same buffer so the string formated by sprintf is not the string - written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but - that font tag is not valid outside of the body. So, the generated html was likely - containing garbage not rendered by the browser. - */ - fprintf(fp_user,"\n",FontFace); - } - fprintf(fp_user,"\n",BgColor,TxColor,BgImage); - fputs("
\n",fp_user); - if(LogoImage[0]!='\0') fprintf(fp_user,"\n",TiColor,Title); - fputs("\n",fp_user); - fprintf(fp_user,"\n",HeaderBgColor,FontSize,_("User"),FontSize,uinfo->label); - fputs("
%s\n",LogoImage,Width,Height,LogoTextColor,LogoText); - fprintf(fp_user,"
%s
",fp_user); - fprintf(fp_user,_("Period: %s"),period.html); - fputs("
%s: %s
\n",fp_user); - fputs("
\n",fp_user); - fputs("\n",fp_user); - fputs("\n",fp_user); - fputs("\n",fp_user); - fprintf(fp_user,"\n",HeaderBgColor,FontSize,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,smartheader); - } - fprintf(fp_user,"\n",TxBgColor,FontSize,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat); - - fprintf(fp_ou,"\n",TxBgColor,FontSize,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat); - } - - fputs("
%s%s%s%s%s
%s%s%s-%s%s%s
%s%s%s-%s%s%s
\n",fp_ou); - - if(ShowSargInfo) { - zdate(ftime, sizeof(ftime), DateFormat); - fprintf(fp_ou,"

%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); - } - - fputs("\n\n",fp_user); - - fclose(fp_ou); - if(fp_user) { - fputs("\n",fp_user); - if(ShowSargInfo) { - zdate(ftime, sizeof(ftime), DateFormat); - fprintf(fp_user,"

%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); - } - fputs("\n\n",fp_user); - fclose(fp_user); - } - - return; + FILE *fp_in = NULL, *fp_ou = NULL, *fp_user = NULL; + + char buf[MAXLEN]; + char url[MAXLEN]; + char csort[255]; + char smart_in[MAXLEN]; + char smart_ou[MAXLEN]; + char sites[MAXLEN]; + char report[MAXLEN]; + char ip[MAXLEN]; + char user[MAXLEN]; + char ouser[MAXLEN]; + char data[15]; + char hora[15]; + char smartcat[256]; + char smartheader[15]; + char ftime[128]; + char smartuser[MAXLEN]; + int fuser=0; + int cstatus; + struct getwordstruct gwarea; + const struct userinfostruct *uinfo; + + ouser[0]='\0'; + + strcpy(smartheader,_("SmartFilter")); + strup(smartheader); + + sprintf(smart_in,"%s/smartfilter.unsort",outdirname); + sprintf(sites,"%s/sarg-sites",outdirname); + sprintf(smart_ou,"%s/smartfilter.log",outdirname); + sprintf(report,"%s/smartfilter.html",outdirname); + + if (snprintf(csort,sizeof(csort),"sort -n -k 1,1 -k 2,2 -k 3,3 -o \"%s\" \"%s\"",smart_ou,smart_in)>=sizeof(csort)) { + debuga(_("cannot build the sort command to sort file %s\n"),smart_in); + exit(EXIT_FAILURE); + } + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + if((fp_in=fopen(smart_ou,"r"))==NULL) { + debuga(_("(smartfilter) Cannot open log file %s\n"),smart_ou); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + unlink(smart_in); + + if((fp_ou=fopen(report,"w"))==NULL) { + debuga(_("(smartfilter) Cannot open log file %s\n"),report); + exit(EXIT_FAILURE); + } + + fprintf(fp_ou, "\n\n\n \n",CharSet); + fputs("\n",fp_ou); + if(strlen(FontFace) > 0) fprintf(fp_ou,"\n",FontFace); + fprintf(fp_ou,"\n",BgColor,TxColor,BgImage); + fputs("
\n",fp_ou); + write_logo_image(fp_ou); + + fprintf(fp_ou,"\n",TiColor,Title); + fprintf(fp_ou,"\n",fp_ou); + fprintf(fp_ou,"\n",HeaderBgColor,FontSize,_("SmartFilter")); + fputs("
%s
",HeaderBgColor,FontSize); + fprintf(fp_ou,_("Period: %s"),period.html); + fputs("
%s
\n",fp_ou); + + fputs("
\n",fp_ou); + fputs("\n",fp_ou); + fputs("\n",fp_ou); + fputs("\n",fp_ou); + fprintf(fp_ou,"\n",HeaderBgColor,FontSize,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,smartheader); + + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + getword_start(&gwarea,buf); + if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(data,sizeof(data),&gwarea,'\t')<0 || + getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 || + getword(url,sizeof(url),&gwarea,'\t')<0 || getword(smartcat,sizeof(smartcat),&gwarea,'\n')<0) { + debuga(_("There is a broken record or garbage in file %s\n"),smart_ou); + exit(EXIT_FAILURE); + } + + uinfo=userinfo_find_from_id(user); + if (!uinfo) { + debuga(_("Unknown user ID %s in file %s\n"),user,smart_ou); + exit(EXIT_FAILURE); + } + if(strcmp(ouser,user) != 0) { + strcpy(ouser,user); + sprintf(smartuser,"%s/denied_%s.html",outdirname,uinfo->filename); + if(fuser) { + fuser=0; + fputs("
%s%s%s%s%s
\n",fp_user); + if(ShowSargInfo) { + zdate(ftime, sizeof(ftime), DateFormat); + fprintf(fp_user,"

%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); + } + fputs("\n\n",fp_user); + fclose(fp_user); + } + if ((fp_user = fopen(smartuser, "a")) == 0) { + debuga(_("(smartfilter) Cannot open file %s\n"),smartuser); + exit(EXIT_FAILURE); + } + fuser=1; + + fputs("\n",fp_ou); + fputs("\n",fp_user); + fputs("\n",fp_user); + fprintf(fp_user," \n",CharSet); + fputs("\n",fp_user); + + if(FontFace[0] != 0) { + /* + Before merging the sprintf and the fputs, the code looked like this: + sprintf(html2,"\n",FontFace); + fputs(url,fp_user); + The two lines don't use the same buffer so the string formated by sprintf is not the string + written to fp_user. I (fmarchal) assumed it was a typo and replaced it by a fprintf but + that font tag is not valid outside of the body. So, the generated html was likely + containing garbage not rendered by the browser. + */ + fprintf(fp_user,"\n",FontFace); + } + fprintf(fp_user,"\n",BgColor,TxColor,BgImage); + fputs("
\n",fp_user); + if(LogoImage[0]!='\0') fprintf(fp_user,"\n",TiColor,Title); + fputs("\n",fp_user); + fprintf(fp_user,"\n",HeaderBgColor,FontSize,_("User"),FontSize,uinfo->label); + fputs("
%s\n",LogoImage,Width,Height,LogoTextColor,LogoText); + fprintf(fp_user,"
%s
",fp_user); + fprintf(fp_user,_("Period: %s"),period.html); + fputs("
%s: %s
\n",fp_user); + fputs("
\n",fp_user); + fputs("\n",fp_user); + fputs("\n",fp_user); + fputs("\n",fp_user); + fprintf(fp_user,"\n",HeaderBgColor,FontSize,_("USERID"),HeaderBgColor,FontSize,_("IP/NAME"),HeaderBgColor,FontSize,_("DATE/TIME"),HeaderBgColor,FontSize,_("ACCESSED SITE"),HeaderBgColor,FontSize,smartheader); + } + fprintf(fp_user,"\n",TxBgColor,FontSize,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat); + + fprintf(fp_ou,"\n",TxBgColor,FontSize,uinfo->label,TxBgColor,FontSize,ip,TxBgColor,FontSize,data,hora,TxBgColor,FontSize,url,TxBgColor,FontSize,smartcat); + } + + fputs("
%s%s%s%s%s
%s%s%s-%s%s%s
%s%s%s-%s%s%s
\n",fp_ou); + + if(ShowSargInfo) { + zdate(ftime, sizeof(ftime), DateFormat); + fprintf(fp_ou,"

%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); + } + + fputs("\n\n",fp_user); + + fclose(fp_ou); + if(fp_user) { + fputs("\n",fp_user); + if(ShowSargInfo) { + zdate(ftime, sizeof(ftime), DateFormat); + fprintf(fp_user,"

%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); + } + fputs("\n\n",fp_user); + fclose(fp_user); + } + + return; } diff --git a/sort.c b/sort.c index c2867e6..083c450 100644 --- a/sort.c +++ b/sort.c @@ -29,162 +29,161 @@ void tmpsort(void) { - - DIR *dirp; - struct dirent *direntp; - int cstatus; - const char tmpext[]=".utmp"; - int dlen; - char csort[MAXLEN]; - char arqou[MAXLEN], arqin[MAXLEN], wnome[MAXLEN]; - const char *field1="2,2"; - const char *field2="1,1"; - const char *field3="3,3"; - const char *order; - - if(indexonly) return; - if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; - - if((UserSort & USER_SORT_CONNECT) != 0) { - field1="1,1"; - field2="2,2"; - field3="3,3"; - } else if((UserSort & USER_SORT_SITE) != 0) { - field1="3,3"; - field2="2,2"; - field3="1,1"; - } else if((UserSort & USER_SORT_TIME) != 0) { - field1="5,5"; - field2="2,2"; - field3="1,1"; - } - - if((UserSort & USER_SORT_REVERSE) == 0) - order=""; - else - order="-r"; - - if ((dirp = opendir(tmp)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); - exit(EXIT_FAILURE); - } - while ((direntp = readdir( dirp )) != NULL ){ - dlen=strlen(direntp->d_name)-(sizeof(tmpext)-1); - if (dlen<0) continue; - if(strcmp(direntp->d_name+dlen,tmpext) != 0) - continue; - - if (dlen>0) { - if (dlen>=sizeof(wnome)) continue; - strncpy(wnome,direntp->d_name,dlen); - wnome[dlen]='\0'; - } else { - wnome[0]='\0'; - } - - strcpy(arqou,tmp); - strcat(arqou,"/"); - strcpy(arqin,arqou); - strcat(arqou,wnome); - strcat(arqin,direntp->d_name); - - if(debug) { - debuga(_("Sorting file: %s\n"),arqou); - } - - strcat(arqou,".txt"); - sprintf(csort,"sort -n -T \"%s\" %s -k %s -k %s -k %s -o \"%s\" \"%s\"",tmp,order,field1,field2,field3,arqou,arqin); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - unlink(arqin); - - } - - (void)closedir( dirp ); - return; + DIR *dirp; + struct dirent *direntp; + int cstatus; + const char tmpext[]=".utmp"; + int dlen; + char csort[MAXLEN]; + char arqou[MAXLEN], arqin[MAXLEN], wnome[MAXLEN]; + const char *field1="2,2"; + const char *field2="1,1"; + const char *field3="3,3"; + const char *order; + + if(indexonly) return; + if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; + + if((UserSort & USER_SORT_CONNECT) != 0) { + field1="1,1"; + field2="2,2"; + field3="3,3"; + } else if((UserSort & USER_SORT_SITE) != 0) { + field1="3,3"; + field2="2,2"; + field3="1,1"; + } else if((UserSort & USER_SORT_TIME) != 0) { + field1="5,5"; + field2="2,2"; + field3="1,1"; + } + + if((UserSort & USER_SORT_REVERSE) == 0) + order=""; + else + order="-r"; + + if ((dirp = opendir(tmp)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); + exit(EXIT_FAILURE); + } + while ((direntp = readdir( dirp )) != NULL ){ + dlen=strlen(direntp->d_name)-(sizeof(tmpext)-1); + if (dlen<0) continue; + if(strcmp(direntp->d_name+dlen,tmpext) != 0) + continue; + + if (dlen>0) { + if (dlen>=sizeof(wnome)) continue; + strncpy(wnome,direntp->d_name,dlen); + wnome[dlen]='\0'; + } else { + wnome[0]='\0'; + } + + strcpy(arqou,tmp); + strcat(arqou,"/"); + strcpy(arqin,arqou); + strcat(arqou,wnome); + strcat(arqin,direntp->d_name); + + if(debug) { + debuga(_("Sorting file: %s\n"),arqou); + } + + strcat(arqou,".txt"); + sprintf(csort,"sort -n -T \"%s\" %s -k %s -k %s -k %s -o \"%s\" \"%s\"",tmp,order,field1,field2,field3,arqou,arqin); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + unlink(arqin); + + } + + (void)closedir( dirp ); + return; } void sort_users_log(const char *tmp, int debug) { - DIR *dirp; - struct dirent *direntp; - char csort[MAXLEN]; - char user[MAXLEN]; - char wdname[MAXLEN]; - int cstatus; - int dlen; - int clen; - const char unsortext[]=".unsort"; - - if(debug) { - debuga(_("pre-sorting files\n")); - } - - if ((dirp = opendir(tmp)) == NULL) { - debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); - exit(EXIT_FAILURE); - } - while ( (direntp = readdir( dirp )) != NULL ){ - dlen=strlen(direntp->d_name)-(sizeof(unsortext)-1); - if (dlen<0) continue; - if(strcmp(direntp->d_name+dlen,unsortext) != 0) - continue; - if(strcmp(direntp->d_name,"authfail.log.unsort") == 0) - continue; - - if (dlen>0) { - if (dlen>=sizeof(user)) continue; - strncpy(user,direntp->d_name,dlen); - user[dlen]=0; - } else { - user[0]='\0'; - } - - if(strcmp(direntp->d_name,"download.unsort") == 0) - clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -k 3,3 -k 1,1 -k 2,2 -k 5,5 -o \"%s/%s.log\" \"%s/%s.unsort\"", - tmp, tmp, user, tmp, user); - else - clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -k 4,4 -k 1,1 -k 2,2 -o \"%s/%s.log\" \"%s/%s.unsort\"", - tmp, tmp, user, tmp, user); - if (clen>=sizeof(csort)) { - debuga(_("user name too long to sort %s\n"),csort); - exit(EXIT_FAILURE); - } - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - if (snprintf(wdname,sizeof(wdname),"%s/%s.unsort",tmp,user)>=sizeof(wdname)) { - debuga(_("user name too long for %s/%s.unsort\n"),tmp,user); - exit(EXIT_FAILURE); - } - unlink(wdname); - } - (void)closedir( dirp ); - - return; + DIR *dirp; + struct dirent *direntp; + char csort[MAXLEN]; + char user[MAXLEN]; + char wdname[MAXLEN]; + int cstatus; + int dlen; + int clen; + const char unsortext[]=".unsort"; + + if(debug) { + debuga(_("pre-sorting files\n")); + } + + if ((dirp = opendir(tmp)) == NULL) { + debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno)); + exit(EXIT_FAILURE); + } + while ( (direntp = readdir( dirp )) != NULL ){ + dlen=strlen(direntp->d_name)-(sizeof(unsortext)-1); + if (dlen<0) continue; + if(strcmp(direntp->d_name+dlen,unsortext) != 0) + continue; + if(strcmp(direntp->d_name,"authfail.log.unsort") == 0) + continue; + + if (dlen>0) { + if (dlen>=sizeof(user)) continue; + strncpy(user,direntp->d_name,dlen); + user[dlen]=0; + } else { + user[0]='\0'; + } + + if(strcmp(direntp->d_name,"download.unsort") == 0) + clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -k 3,3 -k 1,1 -k 2,2 -k 5,5 -o \"%s/%s.log\" \"%s/%s.unsort\"", + tmp, tmp, user, tmp, user); + else + clen=snprintf(csort,sizeof(csort),"sort -T \"%s\" -k 4,4 -k 1,1 -k 2,2 -o \"%s/%s.log\" \"%s/%s.unsort\"", + tmp, tmp, user, tmp, user); + if (clen>=sizeof(csort)) { + debuga(_("user name too long to sort %s\n"),csort); + exit(EXIT_FAILURE); + } + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + if (snprintf(wdname,sizeof(wdname),"%s/%s.unsort",tmp,user)>=sizeof(wdname)) { + debuga(_("user name too long for %s/%s.unsort\n"),tmp,user); + exit(EXIT_FAILURE); + } + unlink(wdname); + } + (void)closedir( dirp ); + + return; } void sort_labels(const char **label,const char **order) { - if((UserSort & USER_SORT_CONNECT) != 0) { - *label=_("connect"); - } else if((UserSort & USER_SORT_SITE) != 0) { - *label=_("site"); - } else if((UserSort & USER_SORT_TIME) != 0) { - *label=_("time"); - } else { - *label=_("bytes"); - } - - if((UserSort & USER_SORT_REVERSE) == 0) - *order=_("normal"); - else - *order=_("reverse"); + if((UserSort & USER_SORT_CONNECT) != 0) { + *label=_("connect"); + } else if((UserSort & USER_SORT_SITE) != 0) { + *label=_("site"); + } else if((UserSort & USER_SORT_TIME) != 0) { + *label=_("time"); + } else { + *label=_("bytes"); + } + + if((UserSort & USER_SORT_REVERSE) == 0) + *order=_("normal"); + else + *order=_("reverse"); } diff --git a/splitlog.c b/splitlog.c index 4c51c48..092bd58 100644 --- a/splitlog.c +++ b/splitlog.c @@ -29,58 +29,58 @@ void splitlog(const char *arq, char *df, int dfrom, int duntil, int convert) { - FILE *fp_in; - char *buf; - char data[30]; - char dia[11]; - time_t tt; - int idata=0; - struct tm *t; - struct getwordstruct gwarea; - longline line; + FILE *fp_in; + char *buf; + char data[30]; + char dia[11]; + time_t tt; + int idata=0; + struct tm *t; + struct getwordstruct gwarea; + longline line; - if(arq[0] == '\0') - arq="/var/log/squid/access.log"; + if(arq[0] == '\0') + arq="/var/log/squid/access.log"; - if((fp_in=MY_FOPEN(arq,"r"))==NULL) { - debuga(_("(splitlog) Cannot open log file %s - %s\n"),arq,strerror(errno)); - exit(EXIT_FAILURE); - } + if((fp_in=MY_FOPEN(arq,"r"))==NULL) { + debuga(_("(splitlog) Cannot open log file %s - %s\n"),arq,strerror(errno)); + exit(EXIT_FAILURE); + } - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the log file %s\n"),arq); - exit(EXIT_FAILURE); - } + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the log file %s\n"),arq); + exit(EXIT_FAILURE); + } - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(data,sizeof(data),&gwarea,' ')<0) { - debuga(_("Invalid date found in file %s\n"),arq); - exit(EXIT_FAILURE); - } - tt=atoi(data); - t=localtime(&tt); + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword(data,sizeof(data),&gwarea,' ')<0) { + debuga(_("Invalid date found in file %s\n"),arq); + exit(EXIT_FAILURE); + } + tt=atoi(data); + t=localtime(&tt); - if(dfrom) { - idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday; - if(idata < dfrom || idata > duntil) - continue; - } + if(dfrom) { + idata=(t->tm_year+1900)*10000+(t->tm_mon+1)*100+t->tm_mday; + if(idata < dfrom || idata > duntil) + continue; + } - if(!convert) { - printf("%s %s\n",data,gwarea.current); - } else { - if(df[0]=='e') - strftime(dia, sizeof(dia), "%d/%m/%Y", t); - else - strftime(dia, sizeof(dia), "%m/%d/%Y", t); + if(!convert) { + printf("%s %s\n",data,gwarea.current); + } else { + if(df[0]=='e') + strftime(dia, sizeof(dia), "%d/%m/%Y", t); + else + strftime(dia, sizeof(dia), "%m/%d/%Y", t); - printf("%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t->tm_min,t->tm_sec,gwarea.current); - } - } + printf("%s %02d:%02d:%02d %s\n",dia,t->tm_hour,t->tm_min,t->tm_sec,gwarea.current); + } + } - longline_destroy(&line); - if (fclose(fp_in)==EOF) { - debuga(_("Failed to close file %s - %s\n"),arq,strerror(errno)); - } + longline_destroy(&line); + if (fclose(fp_in)==EOF) { + debuga(_("Failed to close file %s - %s\n"),arq,strerror(errno)); + } } diff --git a/squidguard_log.c b/squidguard_log.c index 7926229..0c75879 100644 --- a/squidguard_log.c +++ b/squidguard_log.c @@ -33,355 +33,355 @@ static int nfiles_done = 0; static void read_log(const char *wentp, FILE *fp_ou,int dfrom,int duntil) { - FILE *fp_in = NULL; - char *buf; - char leks[5], sep[2], res[MAXLEN]; - char hour[15]; - char source[128], list[128]; - char url[MAX_URL_LEN]; - char user[MAX_USER_LEN]; - char ip[25]; - long long int lmon, lday, lyear; - int mon, day, year; - int idata=0; - int i; - char *str; - bool id_is_ip; - struct getwordstruct gwarea; - struct getwordstruct gwarea1; - struct userinfostruct *uinfo; - longline line; + FILE *fp_in = NULL; + char *buf; + char leks[5], sep[2], res[MAXLEN]; + char hour[15]; + char source[128], list[128]; + char url[MAX_URL_LEN]; + char user[MAX_USER_LEN]; + char ip[25]; + long long int lmon, lday, lyear; + int mon, day, year; + int idata=0; + int i; + char *str; + bool id_is_ip; + struct getwordstruct gwarea; + struct getwordstruct gwarea1; + struct userinfostruct *uinfo; + longline line; - if(debug) { - debuga(_("Reading redirector log file %s\n"),wentp); - } + if(debug) { + debuga(_("Reading redirector log file %s\n"),wentp); + } - /* With squidGuard, you can log groups in only one log file. - We must parse each log files only one time. Example : - dest porn { - domainlist porn/domains - urllist porn/urls - log file1.log - } - dest aggressive { - domainlist aggressive/domains - urllist aggressive/urls - log file2.log - } - dest audio-video { - domainlist audio-video/domains - urllist audio-video/urls - log file1.log - } - */ - for (i=0; i=sizeof(hour)) { - debuga(_("Hour string too long in redirector log file %s\n"),wentp); - exit(EXIT_FAILURE); - } - strcpy(hour,res); - } else if(strcmp(leks,"source") == 0) { - if (strlen(res)>=sizeof(source)) { - debuga(_("Banning source name too long in redirector log file %s\n"),wentp); - exit(EXIT_FAILURE); - } - strcpy(source,res); - } else if(strcmp(leks,"list") == 0) { - if (strlen(res)>=sizeof(list)) { - debuga(_("Banning list name too long in redirector log file %s\n"),wentp); - exit(EXIT_FAILURE); - } - strcpy(list,res); - } else if(strcmp(leks,"ip") == 0) { - if (strlen(res)>=sizeof(ip)) { - debuga(_("IP address too long in redirector log file %s\n"),wentp); - exit(EXIT_FAILURE); - } - strcpy(ip,res); - } else if(strcmp(leks,"user") == 0) { - if (strlen(res)>=sizeof(user)) { - debuga(_("User ID too long in redirector log file %s\n"),wentp); - exit(EXIT_FAILURE); - } - strcpy(user,res); - } else if(strcmp(leks,"url") == 0) { - if (strlen(res)>=sizeof(url)) { - debuga(_("URL too long in redirector log file %s\n"),wentp); - exit(EXIT_FAILURE); - } - strcpy(url,res); - } - } - } - } else { - if (getword_atoll(&lyear,&gwarea,'-')<0 || getword_atoll(&lmon,&gwarea,'-')<0 || - getword_atoll(&lday,&gwarea,' ')<0) { - debuga(_("Invalid date found in file %s\n"),wentp); - exit(EXIT_FAILURE); - } - year=(int)lyear; - mon=(int)lmon; - day=(int)lday; - if (getword(hour,sizeof(hour),&gwarea,' ')<0) { - debuga(_("Invalid time found in file %s\n"),wentp); - exit(EXIT_FAILURE); - } - if (getword_skip(MAXLEN,&gwarea,'(')<0 || getword(source,sizeof(source),&gwarea,'/')<0) { - debuga(_("Invalid redirected source in file %s\n"),wentp); - exit(EXIT_FAILURE); - } - if (getword(list,sizeof(list),&gwarea,'/')<0) { - debuga(_("Invalid redirected list in file %s\n"),wentp); - exit(EXIT_FAILURE); - } - if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword_limit(url,sizeof(url),&gwarea,' ')<0) { - debuga(_("Invalid URL in file %s\n"),wentp); - exit(EXIT_FAILURE); - } - if (getword(ip,sizeof(ip),&gwarea,'/')<0) { - debuga(_("Invalid source IP in file %s\n"),wentp); - exit(EXIT_FAILURE); - } - if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) { - debuga(_("Invalid user in file %s\n"),wentp); - exit(EXIT_FAILURE); - } - /* - The URL may be "http://url:port/data" if the method is GET or simply "url:port/" if the method is CONNECT. - The following code removes the protocol:// if it is detected and always truncates the URL after the domain name. - It will fail if the URL doesn't start with the protocol and contains two consecutive / in the path (i.e. - the URL is not normalized). - */ - str=strchr(url,'/'); - if (str) { - if (str[1]=='/') { - str+=2; - for (i=0 ; *str && *str!='/' ; i++) url[i]=*str++; - url[i]='\0'; - } else { - *str='\0'; - } - } - } + while ((buf=longline_read(fp_in,line)) != NULL) { + getword_start(&gwarea,buf); + if(RedirectorLogFormat[0] != '\0') { + getword_start(&gwarea1,RedirectorLogFormat); + leks[0]='\0'; + if (getword(leks,sizeof(leks),&gwarea1,'#')<0) { + debuga(_("There is a broken record or garbage in your %s file\n"),wentp); + exit(EXIT_FAILURE); + } + year=0; + mon=0; + day=0; + hour[0]='\0'; + source[0]='\0'; + list[0]='\0'; + ip[0]='\0'; + user[0]='\0'; + url[0]='\0'; + while(strcmp(leks,"end") != 0) { + if (getword(leks,sizeof(leks),&gwarea1,'#')<0 || getword(sep,sizeof(sep),&gwarea1,'#')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),wentp); + exit(EXIT_FAILURE); + } + if(strcmp(leks,"end") != 0) { + if (getword_limit(res,sizeof(res),&gwarea,sep[0])<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),wentp); + exit(EXIT_FAILURE); + } + if(strcmp(leks,"year") == 0) { + year=atoi(res); + } else if(strcmp(leks,"mon") == 0) { + mon=atoi(res); + } else if(strcmp(leks,"day") == 0) { + day=atoi(res); + } else if(strcmp(leks,"hour") == 0) { + if (strlen(res)>=sizeof(hour)) { + debuga(_("Hour string too long in redirector log file %s\n"),wentp); + exit(EXIT_FAILURE); + } + strcpy(hour,res); + } else if(strcmp(leks,"source") == 0) { + if (strlen(res)>=sizeof(source)) { + debuga(_("Banning source name too long in redirector log file %s\n"),wentp); + exit(EXIT_FAILURE); + } + strcpy(source,res); + } else if(strcmp(leks,"list") == 0) { + if (strlen(res)>=sizeof(list)) { + debuga(_("Banning list name too long in redirector log file %s\n"),wentp); + exit(EXIT_FAILURE); + } + strcpy(list,res); + } else if(strcmp(leks,"ip") == 0) { + if (strlen(res)>=sizeof(ip)) { + debuga(_("IP address too long in redirector log file %s\n"),wentp); + exit(EXIT_FAILURE); + } + strcpy(ip,res); + } else if(strcmp(leks,"user") == 0) { + if (strlen(res)>=sizeof(user)) { + debuga(_("User ID too long in redirector log file %s\n"),wentp); + exit(EXIT_FAILURE); + } + strcpy(user,res); + } else if(strcmp(leks,"url") == 0) { + if (strlen(res)>=sizeof(url)) { + debuga(_("URL too long in redirector log file %s\n"),wentp); + exit(EXIT_FAILURE); + } + strcpy(url,res); + } + } + } + } else { + if (getword_atoll(&lyear,&gwarea,'-')<0 || getword_atoll(&lmon,&gwarea,'-')<0 || + getword_atoll(&lday,&gwarea,' ')<0) { + debuga(_("Invalid date found in file %s\n"),wentp); + exit(EXIT_FAILURE); + } + year=(int)lyear; + mon=(int)lmon; + day=(int)lday; + if (getword(hour,sizeof(hour),&gwarea,' ')<0) { + debuga(_("Invalid time found in file %s\n"),wentp); + exit(EXIT_FAILURE); + } + if (getword_skip(MAXLEN,&gwarea,'(')<0 || getword(source,sizeof(source),&gwarea,'/')<0) { + debuga(_("Invalid redirected source in file %s\n"),wentp); + exit(EXIT_FAILURE); + } + if (getword(list,sizeof(list),&gwarea,'/')<0) { + debuga(_("Invalid redirected list in file %s\n"),wentp); + exit(EXIT_FAILURE); + } + if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword_limit(url,sizeof(url),&gwarea,' ')<0) { + debuga(_("Invalid URL in file %s\n"),wentp); + exit(EXIT_FAILURE); + } + if (getword(ip,sizeof(ip),&gwarea,'/')<0) { + debuga(_("Invalid source IP in file %s\n"),wentp); + exit(EXIT_FAILURE); + } + if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,' ')<0) { + debuga(_("Invalid user in file %s\n"),wentp); + exit(EXIT_FAILURE); + } + /* + The URL may be "http://url:port/data" if the method is GET or simply "url:port/" if the method is CONNECT. + The following code removes the protocol:// if it is detected and always truncates the URL after the domain name. + It will fail if the URL doesn't start with the protocol and contains two consecutive / in the path (i.e. + the URL is not normalized). + */ + str=strchr(url,'/'); + if (str) { + if (str[1]=='/') { + str+=2; + for (i=0 ; *str && *str!='/' ; i++) url[i]=*str++; + url[i]='\0'; + } else { + *str='\0'; + } + } + } - //sprintf(warea,"%04d%02d%02d",year,mon,day); + //sprintf(warea,"%04d%02d%02d",year,mon,day); - if(RedirectorFilterOutDate) { - idata = year*10000+mon*100+day; - if(idata < dfrom || idata > duntil) - continue; - } + if(RedirectorFilterOutDate) { + idata = year*10000+mon*100+day; + if(idata < dfrom || idata > duntil) + continue; + } - if(UserIp) { - strcpy(user,ip); - id_is_ip=true; - } else { - id_is_ip=false; - if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) { - if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) { - strcpy(user,ip); - id_is_ip=true; - } - if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE) - continue; - if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY) - strcpy(user,"everybody"); - } - } - uinfo=userinfo_find_from_id(user); - if (!uinfo) { - uinfo=userinfo_create(user); - uinfo->id_is_ip=id_is_ip; - if(Ip2Name && id_is_ip) ip2name(user,sizeof(user)); - user_find(uinfo->label,MAX_USER_LEN, user); - } - fprintf(fp_ou,"%s\t%04d%02d%02d\t%s\t%s\t%s\t",uinfo->id,year,mon,day,hour,ip,url); - if (source[0] && list[0]) - fprintf(fp_ou,"%s/%s\n",source,list); - else if (source[0]) - fprintf(fp_ou,"%s\n",source); - else - fprintf(fp_ou,"%s\n",list); - squidguard_count++; - } - fclose(fp_in); - longline_destroy(&line); - return; + if(UserIp) { + strcpy(user,ip); + id_is_ip=true; + } else { + id_is_ip=false; + if(strcmp(user,"-") == 0 || strcmp(user," ") == 0 || strcmp(user,"") == 0) { + if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) { + strcpy(user,ip); + id_is_ip=true; + } + if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE) + continue; + if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY) + strcpy(user,"everybody"); + } + } + uinfo=userinfo_find_from_id(user); + if (!uinfo) { + uinfo=userinfo_create(user); + uinfo->id_is_ip=id_is_ip; + if(Ip2Name && id_is_ip) ip2name(user,sizeof(user)); + user_find(uinfo->label,MAX_USER_LEN, user); + } + fprintf(fp_ou,"%s\t%04d%02d%02d\t%s\t%s\t%s\t",uinfo->id,year,mon,day,hour,ip,url); + if (source[0] && list[0]) + fprintf(fp_ou,"%s/%s\n",source,list); + else if (source[0]) + fprintf(fp_ou,"%s\n",source); + else + fprintf(fp_ou,"%s\n",list); + squidguard_count++; + } + fclose(fp_in); + longline_destroy(&line); + return; } void squidguard_log(void) { - FILE *fp_ou = NULL, *fp_guard = NULL; - char buf[MAXLEN]; - char guard_in[MAXLEN]; - char guard_ou[MAXLEN]; - char logdir[MAXLEN]; - char user[MAXLEN]; - char tmp6[MAXLEN]; - int i; - int y; - int cstatus; - int dfrom, duntil; - char *str; - char *str2; + FILE *fp_ou = NULL, *fp_guard = NULL; + char buf[MAXLEN]; + char guard_in[MAXLEN]; + char guard_ou[MAXLEN]; + char logdir[MAXLEN]; + char user[MAXLEN]; + char tmp6[MAXLEN]; + int i; + int y; + int cstatus; + int dfrom, duntil; + char *str; + char *str2; - str2 = user; + str2 = user; - if(SquidGuardConf[0] == '\0' && NRedirectorLogs == 0) - return; + if(SquidGuardConf[0] == '\0' && NRedirectorLogs == 0) + return; - sprintf(guard_in,"%s/redirector.unsort",tmp); - sprintf(guard_ou,"%s/redirector.log",tmp); - if((fp_ou=fopen(guard_in,"a"))==NULL) { - debuga(_("(squidguard) Cannot open log file %s\n"),guard_in); - exit(EXIT_FAILURE); - } + sprintf(guard_in,"%s/redirector.unsort",tmp); + sprintf(guard_ou,"%s/redirector.log",tmp); + if((fp_ou=fopen(guard_in,"a"))==NULL) { + debuga(_("(squidguard) Cannot open log file %s\n"),guard_in); + exit(EXIT_FAILURE); + } - dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+period.start.tm_mday; - duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.end.tm_mday; + dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+period.start.tm_mday; + duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.end.tm_mday; - if (NRedirectorLogs>0) { - for (i=0 ; i0) { + for (i=0 ; i=0 && (unsigned char)str[y]<=' ' ; y--); - if (y>=sizeof(logdir)-1) y=sizeof(logdir)-2; - logdir[y+1] = '\0'; - while (y>=0) { - logdir[y] = str[y]; - y--; - } - } else if((str=get_param_value("log",buf))!=NULL) { - if((str2=get_param_value("anonymous",str))!=NULL) - str=str2; + logdir[0]=0; + while(fgets(buf,sizeof(buf),fp_guard)!=NULL) { + fixendofline(buf); + if((str=get_param_value("logdir",buf))!=NULL) { + /* + We want to tolerate spaces inside the directory name but we must also + remove the trailing spaces left by the editor after the directory name. + This should not be a problem as nobody use a file name with trailing spaces. + */ + for (y=strlen(str)-1 ; y>=0 && (unsigned char)str[y]<=' ' ; y--); + if (y>=sizeof(logdir)-1) y=sizeof(logdir)-2; + logdir[y+1] = '\0'; + while (y>=0) { + logdir[y] = str[y]; + y--; + } + } else if((str=get_param_value("log",buf))!=NULL) { + if((str2=get_param_value("anonymous",str))!=NULL) + str=str2; - /* - If logdir is defined, we prepend it to the log file name, otherwise, we assume - the log directive provides an absolute file name to the log file. Therefore, - we don't need to add an additionnal / at the beginning of the log file name. - */ - y=(logdir[0]) ? sprintf(wentp,"%s/",logdir) : 0; - /* - Spaces are allowed in the name of the log file. The file name ends at the first # - because it is assumed it is an end of line comment. Any space before the # is then - removed. Any control character (i.e. a character with a code lower than 32) ends - the file name. That includes the terminating zero. - */ - while((unsigned char)*str>=' ' && *str!='#' && y0) { - str--; - y--; - } - } - wentp[y]=0; - read_log(wentp,fp_ou,dfrom,duntil); - } - } - } + /* + If logdir is defined, we prepend it to the log file name, otherwise, we assume + the log directive provides an absolute file name to the log file. Therefore, + we don't need to add an additionnal / at the beginning of the log file name. + */ + y=(logdir[0]) ? sprintf(wentp,"%s/",logdir) : 0; + /* + Spaces are allowed in the name of the log file. The file name ends at the first # + because it is assumed it is an end of line comment. Any space before the # is then + removed. Any control character (i.e. a character with a code lower than 32) ends + the file name. That includes the terminating zero. + */ + while((unsigned char)*str>=' ' && *str!='#' && y0) { + str--; + y--; + } + } + wentp[y]=0; + read_log(wentp,fp_ou,dfrom,duntil); + } + } + } - if (fp_guard) fclose(fp_guard); - if (fp_ou) fclose(fp_ou); + if (fp_guard) fclose(fp_guard); + if (fp_ou) fclose(fp_ou); - if (files_done) { - for (y=0; y",fp_ou); - fprintf(fp_ou,_("Period: %s"),period.html); - fputs("\n",fp_ou); - fprintf(fp_ou,"%s\n",_("Redirector report")); - close_html_header(fp_ou); - - fputs("
\n",fp_ou); - fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"),_("RULE")); - - while((buf=longline_read(fp_in,line))!=NULL) { - getword_start(&gwarea,buf); - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - debuga(_("Invalid user in file %s\n"),squidguard_in); - exit(EXIT_FAILURE); - } - if (getword_atoll(&data2,&gwarea,'\t')<0) { - debuga(_("Invalid date in file %s\n"),squidguard_in); - exit(EXIT_FAILURE); - } - if (getword(hora,sizeof(hora),&gwarea,'\t')<0) { - debuga(_("Invalid time in file %s\n"),squidguard_in); - exit(EXIT_FAILURE); - } - if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("Invalid IP address in file %s\n"),squidguard_in); - exit(EXIT_FAILURE); - } - if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("Invalid URL in file %s\n"),squidguard_in); - exit(EXIT_FAILURE); - } - if (getword(rule,sizeof(rule),&gwarea,'\n')<0) { - debuga(_("Invalid rule in file %s\n"),squidguard_in); - exit(EXIT_FAILURE); - } - - uinfo=userinfo_find_from_id(user); - if (!uinfo) { - debuga(_("Unknown user ID %s in file %s\n"),user,squidguard_in); - exit(EXIT_FAILURE); - } - - computedate(data2/10000,(data2/100)%10,data2%100,&t); - strftime(data,sizeof(data),"%x",&t); - - new_user=false; - if(!z) { - strcpy(ouser,user); - strcpy(oip,ip); - strcpy(oname,ip); - if (Ip2Name && !uinfo->id_is_ip) ip2name(oname,sizeof(oname)); - z=true; - new_user=true; - } else { - if(strcmp(ouser,user) != 0) { - strcpy(ouser,user); - new_user=true; - } - if(strcmp(oip,ip) != 0) { - strcpy(oip,ip); - strcpy(oname,ip); - if (Ip2Name && !uinfo->id_is_ip) ip2name(oname,sizeof(oname)); - new_user=true; - } - } - - if(SquidGuardReportLimit) { - if(strcmp(ouser2,uinfo->label) == 0) { - count++; - } else { - count=1; - strcpy(ouser2,uinfo->label); - } - if(count >= SquidGuardReportLimit) - continue; - } - - if (new_user) - fprintf(fp_ou,"",uinfo->label,ip); - else - fputs("",fp_ou); - fprintf(fp_ou,"\n",rule); - } - fclose(fp_in); - longline_destroy(&line); - - fputs("
%s%s%s%s%s
%s%s
%s-%s",fp_ou); - output_html_string(fp_ou,url,100); - fprintf(fp_ou,"%s
\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),report); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - - unlink(squidguard_in); - - return; + FILE *fp_in = NULL, *fp_ou = NULL; + + char *buf; + char *url; + char squidguard_in[MAXLEN]; + char report[MAXLEN]; + char ip[MAXLEN]; + char rule[255]; + char oip[MAXLEN]; + char user[MAXLEN]; + char ouser[MAXLEN]; + char data[15]; + char hora[15]; + char ouser2[255]; + char oname[MAXLEN]; + bool z=false; + int count=0; + long long int data2; + bool new_user; + struct getwordstruct gwarea; + const struct userinfostruct *uinfo; + struct tm t; + longline line; + + ouser[0]='\0'; + ouser2[0]='\0'; + + sprintf(squidguard_in,"%s/redirector.log",tmp); + if(!squidguard_count) { + unlink(squidguard_in); + return; + } + + snprintf(report,sizeof(report),"%s/redirector.html",outdirname); + + if((fp_in=fopen(squidguard_in,"r"))==NULL) { + debuga(_("(squidguard) Cannot open log file %s\n"),squidguard_in); + exit(EXIT_FAILURE); + } + + if((fp_ou=fopen(report,"w"))==NULL) { + debuga(_("(squidguard) Cannot open log file %s\n"),report); + exit(EXIT_FAILURE); + } + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the processed redirector log\n")); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Redirector report"),HTML_JS_NONE); + fputs("",fp_ou); + fprintf(fp_ou,_("Period: %s"),period.html); + fputs("\n",fp_ou); + fprintf(fp_ou,"%s\n",_("Redirector report")); + close_html_header(fp_ou); + + fputs("
\n",fp_ou); + fprintf(fp_ou,"\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"),_("RULE")); + + while((buf=longline_read(fp_in,line))!=NULL) { + getword_start(&gwarea,buf); + if (getword(user,sizeof(user),&gwarea,'\t')<0) { + debuga(_("Invalid user in file %s\n"),squidguard_in); + exit(EXIT_FAILURE); + } + if (getword_atoll(&data2,&gwarea,'\t')<0) { + debuga(_("Invalid date in file %s\n"),squidguard_in); + exit(EXIT_FAILURE); + } + if (getword(hora,sizeof(hora),&gwarea,'\t')<0) { + debuga(_("Invalid time in file %s\n"),squidguard_in); + exit(EXIT_FAILURE); + } + if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { + debuga(_("Invalid IP address in file %s\n"),squidguard_in); + exit(EXIT_FAILURE); + } + if (getword_ptr(buf,&url,&gwarea,'\t')<0) { + debuga(_("Invalid URL in file %s\n"),squidguard_in); + exit(EXIT_FAILURE); + } + if (getword(rule,sizeof(rule),&gwarea,'\n')<0) { + debuga(_("Invalid rule in file %s\n"),squidguard_in); + exit(EXIT_FAILURE); + } + + uinfo=userinfo_find_from_id(user); + if (!uinfo) { + debuga(_("Unknown user ID %s in file %s\n"),user,squidguard_in); + exit(EXIT_FAILURE); + } + + computedate(data2/10000,(data2/100)%10,data2%100,&t); + strftime(data,sizeof(data),"%x",&t); + + new_user=false; + if(!z) { + strcpy(ouser,user); + strcpy(oip,ip); + strcpy(oname,ip); + if (Ip2Name && !uinfo->id_is_ip) ip2name(oname,sizeof(oname)); + z=true; + new_user=true; + } else { + if(strcmp(ouser,user) != 0) { + strcpy(ouser,user); + new_user=true; + } + if(strcmp(oip,ip) != 0) { + strcpy(oip,ip); + strcpy(oname,ip); + if (Ip2Name && !uinfo->id_is_ip) ip2name(oname,sizeof(oname)); + new_user=true; + } + } + + if(SquidGuardReportLimit) { + if(strcmp(ouser2,uinfo->label) == 0) { + count++; + } else { + count=1; + strcpy(ouser2,uinfo->label); + } + if(count >= SquidGuardReportLimit) + continue; + } + + if (new_user) + fprintf(fp_ou,"",uinfo->label,ip); + else + fputs("",fp_ou); + fprintf(fp_ou,"\n",rule); + } + fclose(fp_in); + longline_destroy(&line); + + fputs("
%s%s%s%s%s
%s%s
%s-%s",fp_ou); + output_html_string(fp_ou,url,100); + fprintf(fp_ou,"%s
\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),report); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); + + unlink(squidguard_in); + + return; } diff --git a/topsites.c b/topsites.c index 0ae966e..eca6745 100644 --- a/topsites.c +++ b/topsites.c @@ -29,241 +29,239 @@ void topsites(void) { - - FILE *fp_in, *fp_ou; - - char *buf; - char *url; - char *ourl=NULL; - char csort[255]; - char general[MAXLEN]; - char general2[MAXLEN]; - char general3[MAXLEN]; - char sites[MAXLEN]; - char report[MAXLEN]; - const char *sortf; - const char *sortt; - long long int nacc; - long long int nbytes; - long long int ntime; - long long int tnacc=0; - long long int tnbytes=0; - long long int tntime=0; - long long int twork1=0, twork2=0, twork3=0; - int regs=0; - int cstatus; - int url_len; - int ourl_size=0; - struct getwordstruct gwarea; - longline line; - struct generalitemstruct item; - - if(Privacy) - return; - - sprintf(general,"%s/sarg-general",outdirname); - sprintf(sites,"%s/sarg-sites",outdirname); - sprintf(general2,"%s/sarg-general2",outdirname); - sprintf(general3,"%s/sarg-general3",outdirname); - - if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) - sprintf(report,"%s/index.html",outdirname); - else - sprintf(report,"%s/topsites.html",outdirname); - - sprintf(csort,"sort -k 4,4 -o \"%s\" \"%s\"",general2,general); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - if((fp_in=fopen(general2,"r"))==NULL) { - debuga(_("(topsites) Cannot open log file %s\n"),general2); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - if((fp_ou=fopen(general3,"w"))==NULL) { - debuga(_("(topsites) Cannot open log file %s\n"),general3); - exit(EXIT_FAILURE); - } - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read file %s\n"),general2); - exit(EXIT_FAILURE); - } - - while((buf=longline_read(fp_in,line))!=NULL) { - ger_read(buf,&item,general2); - if(item.total) continue; - - if(!regs) { - url_len=strlen(item.url); - if (!ourl || url_len>=ourl_size) { - ourl_size=url_len+1; - ourl=realloc(ourl,ourl_size); - if (!ourl) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(ourl,item.url); - regs++; - } - - if(strcmp(item.url,ourl) != 0) { - /* - This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable - to print a long long int unless it is exactly 64-bits long. - */ - fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl); - url_len=strlen(item.url); - if (url_len>=ourl_size) { - ourl_size=url_len+1; - ourl=realloc(ourl,ourl_size); - if (!ourl) { - debuga(_("Not enough memory to store the url\n")); - exit(EXIT_FAILURE); - } - } - strcpy(ourl,item.url); - tnacc=0; - tnbytes=0; - tntime=0; - } - - tnacc+=item.nacc; - tnbytes+=item.nbytes; - tntime+=item.nelap; - } - fclose(fp_in); - unlink(general2); - longline_destroy(&line); - - if (ourl) { - /* - This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable - to print a long long int unless it is exactly 64-bits long. - */ - fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl); - free(ourl); - } - - fclose(fp_ou); - - if((TopsitesSort & TOPSITE_SORT_CONNECT) != 0) { - sortf="-k 1,1 -k 2,2"; - } else if((TopsitesSort & TOPSITE_SORT_BYTES) != 0) { - sortf="-k 2,2 -k 1,1"; - } else if((TopsitesSort & TOPSITE_SORT_TIME) != 0) { - sortf="-k 3,3"; - } else { - sortf="-k 2,2 -k 1,1"; //default is BYTES - } - if((TopsitesSort & TOPSITE_SORT_REVERSE) != 0) { - sortt="-r"; - } else { - sortt=""; - } - - sprintf(csort,"sort %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - if((fp_in=fopen(sites,"r"))==NULL) { - debuga(_("(topsites) Cannot open log file %s\n"),sites); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - unlink(general2); - unlink(general3); - - if((fp_ou=fopen(report,"w"))==NULL) { - debuga(_("(topsites) Cannot open log file %s\n"),report); - exit(EXIT_FAILURE); - } - - write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE); - fputs("",fp_ou); - fprintf(fp_ou,_("Period: %s"),period.html); - fputs("\n",fp_ou); - fputs("",fp_ou); - fprintf(fp_ou,_("Top %d sites"),TopSitesNum); - fputs("\n",fp_ou); - close_html_header(fp_ou); - - fputs("
\n",fp_ou); - fprintf(fp_ou,"\n",_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME")); - - regs=0; - ntopsites = 0; - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read file %s\n"),sites); - exit(EXIT_FAILURE); - } - - while(regs",fixnum(twork1,1)); - fputs("",fixnum(twork2,1)); - fputs("\n",fixtime(twork3)); - } - fclose(fp_in); - longline_destroy(&line); - - fputs("
%s%s%s%s%s
%d",++regs); - - if(BlockIt[0] != '\0') { - fprintf(fp_ou," ",fp_ou); - } - - fputs("",fp_ou); - output_html_string(fp_ou,url,100); - fputs("%s%s%s
\n",fp_ou); - if (write_html_trailer(fp_ou)<0) - debuga(_("Write error in file %s\n"),report); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); - - return; - + FILE *fp_in, *fp_ou; + + char *buf; + char *url; + char *ourl=NULL; + char csort[255]; + char general[MAXLEN]; + char general2[MAXLEN]; + char general3[MAXLEN]; + char sites[MAXLEN]; + char report[MAXLEN]; + const char *sortf; + const char *sortt; + long long int nacc; + long long int nbytes; + long long int ntime; + long long int tnacc=0; + long long int tnbytes=0; + long long int tntime=0; + long long int twork1=0, twork2=0, twork3=0; + int regs=0; + int cstatus; + int url_len; + int ourl_size=0; + struct getwordstruct gwarea; + longline line; + struct generalitemstruct item; + + if(Privacy) + return; + + sprintf(general,"%s/sarg-general",outdirname); + sprintf(sites,"%s/sarg-sites",outdirname); + sprintf(general2,"%s/sarg-general2",outdirname); + sprintf(general3,"%s/sarg-general3",outdirname); + + if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) + sprintf(report,"%s/index.html",outdirname); + else + sprintf(report,"%s/topsites.html",outdirname); + + sprintf(csort,"sort -k 4,4 -o \"%s\" \"%s\"",general2,general); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + if((fp_in=fopen(general2,"r"))==NULL) { + debuga(_("(topsites) Cannot open log file %s\n"),general2); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + if((fp_ou=fopen(general3,"w"))==NULL) { + debuga(_("(topsites) Cannot open log file %s\n"),general3); + exit(EXIT_FAILURE); + } + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read file %s\n"),general2); + exit(EXIT_FAILURE); + } + + while((buf=longline_read(fp_in,line))!=NULL) { + ger_read(buf,&item,general2); + if(item.total) continue; + + if(!regs) { + url_len=strlen(item.url); + if (!ourl || url_len>=ourl_size) { + ourl_size=url_len+1; + ourl=realloc(ourl,ourl_size); + if (!ourl) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(ourl,item.url); + regs++; + } + + if(strcmp(item.url,ourl) != 0) { + /* + This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable + to print a long long int unless it is exactly 64-bits long. + */ + fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl); + url_len=strlen(item.url); + if (url_len>=ourl_size) { + ourl_size=url_len+1; + ourl=realloc(ourl,ourl_size); + if (!ourl) { + debuga(_("Not enough memory to store the url\n")); + exit(EXIT_FAILURE); + } + } + strcpy(ourl,item.url); + tnacc=0; + tnbytes=0; + tntime=0; + } + + tnacc+=item.nacc; + tnbytes+=item.nbytes; + tntime+=item.nelap; + } + fclose(fp_in); + unlink(general2); + longline_destroy(&line); + + if (ourl) { + /* + This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable + to print a long long int unless it is exactly 64-bits long. + */ + fprintf(fp_ou,"%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%s\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)tntime,ourl); + free(ourl); + } + + fclose(fp_ou); + + if((TopsitesSort & TOPSITE_SORT_CONNECT) != 0) { + sortf="-k 1,1 -k 2,2"; + } else if((TopsitesSort & TOPSITE_SORT_BYTES) != 0) { + sortf="-k 2,2 -k 1,1"; + } else if((TopsitesSort & TOPSITE_SORT_TIME) != 0) { + sortf="-k 3,3"; + } else { + sortf="-k 2,2 -k 1,1"; //default is BYTES + } + if((TopsitesSort & TOPSITE_SORT_REVERSE) != 0) { + sortt="-r"; + } else { + sortt=""; + } + + sprintf(csort,"sort %s -n %s -o \"%s\" \"%s\"",sortt,sortf,sites,general3); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + if((fp_in=fopen(sites,"r"))==NULL) { + debuga(_("(topsites) Cannot open log file %s\n"),sites); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + unlink(general2); + unlink(general3); + + if((fp_ou=fopen(report,"w"))==NULL) { + debuga(_("(topsites) Cannot open log file %s\n"),report); + exit(EXIT_FAILURE); + } + + write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Top sites"),HTML_JS_SORTTABLE); + fputs("",fp_ou); + fprintf(fp_ou,_("Period: %s"),period.html); + fputs("\n",fp_ou); + fputs("",fp_ou); + fprintf(fp_ou,_("Top %d sites"),TopSitesNum); + fputs("\n",fp_ou); + close_html_header(fp_ou); + + fputs("
\n",fp_ou); + fprintf(fp_ou,"\n",_("ACCESSED SITE"),_("CONNECT"),_("BYTES"),_("TIME")); + + regs=0; + ntopsites = 0; + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read file %s\n"),sites); + exit(EXIT_FAILURE); + } + + while(regs",fixnum(twork1,1)); + fputs("",fixnum(twork2,1)); + fputs("\n",fixtime(twork3)); + } + fclose(fp_in); + longline_destroy(&line); + + fputs("
%s%s%s%s%s
%d",++regs); + + if(BlockIt[0] != '\0') { + fprintf(fp_ou," ",fp_ou); + } + + fputs("",fp_ou); + output_html_string(fp_ou,url,100); + fputs("%s%s%s
\n",fp_ou); + if (write_html_trailer(fp_ou)<0) + debuga(_("Write error in file %s\n"),report); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); + + return; } diff --git a/topuser.c b/topuser.c index 9d26956..b2a3e43 100644 --- a/topuser.c +++ b/topuser.c @@ -29,408 +29,407 @@ void topuser(void) { - - FILE *fp_in = NULL, *fp_ou = NULL, *fp_top1 = NULL, *fp_top2 = NULL, *fp_top3 = NULL; - long long int ttnbytes=0, ttnacc=0, tnacc=0; - long long int tnbytes=0, ttnelap=0, tnelap=0; - long long int tnincache=0, tnoucache=0, ttnincache=0, ttnoucache=0; - long long int nbytes; - long long int nacc; - long long int elap, incac, oucac; - double perc=0.00; - double perc2=0.00; - double inperc=0.00, ouperc=0.00; - int posicao=0; - char olduser[MAX_USER_LEN], csort[MAXLEN]; - char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN]; - char user[MAX_USER_LEN], tusr[MAXLEN]; - char ipantes[MAXLEN], nameantes[MAXLEN]; - const char *sfield="-n -k 2,2"; - const char *order; - const char *sort_field; - const char *sort_order; - char title[80]; - char *warea; - int totuser=0; - int topcount=0; - int cstatus; - struct getwordstruct gwarea; - longline line; - struct generalitemstruct item; - const struct userinfostruct *uinfo; - - ipantes[0]='\0'; - nameantes[0]='\0'; - - ntopuser = 0; - snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname); - if((fp_in=fopen(wger,"r"))==NULL) { - debuga(_("(topuser) Cannot open file %s\n"),wger); - exit(EXIT_FAILURE); - } - - snprintf(top2,sizeof(top2),"%s/top.tmp",outdirname); - if((fp_top2=fopen(top2,"w"))==NULL) { - debuga(_("(topuser) Cannot open file %s\n"),top2); - exit(EXIT_FAILURE); - } - - olduser[0]='\0'; - totuser=0; - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the file %s\n"),wger); - exit(EXIT_FAILURE); - } - - while((warea=longline_read(fp_in,line))!=NULL) { - ger_read(warea,&item,wger); - if(item.total) continue; - if(strcmp(olduser,item.user) != 0) { - totuser++; - - if (olduser[0] != '\0') { - /* - This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable - to print a long long int unless it is exactly 64-bits long. - */ - fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache); - - ttnbytes+=tnbytes; - ttnacc+=tnacc; - ttnelap+=tnelap; - ttnincache+=tnincache; - ttnoucache+=tnoucache; - } - strcpy(olduser,item.user); - tnbytes=0; - tnacc=0; - tnelap=0; - tnincache=0; - tnoucache=0; - } - - tnbytes+=item.nbytes; - tnacc+=item.nacc; - tnelap+=item.nelap; - tnincache+=item.incache; - tnoucache+=item.oucache; - } - fclose(fp_in); - longline_destroy(&line); - - if (olduser[0] != '\0') { - /* - This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable - to print a long long int unless it is exactly 64-bits long. - */ - fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache); - - ttnbytes+=tnbytes; - ttnacc+=tnacc; - ttnelap+=tnelap; - ttnincache+=tnincache; - ttnoucache+=tnoucache; - } - fclose(fp_top2); - - if((TopuserSort & TOPUSER_SORT_USER) != 0) { - sfield="-k 1,1"; - sort_field=_("user"); - } else if((TopuserSort & TOPUSER_SORT_CONNECT) != 0) { - sfield="-n -k 3,3"; - sort_field=_("connect"); - } else if((TopuserSort & TOPUSER_SORT_TIME) != 0) { - sfield="-n -k 4,4"; - sort_field=_("time"); - } else { - sort_field=_("bytes"); - } - - if((TopuserSort & TOPUSER_SORT_REVERSE) == 0) { - order=""; - sort_order=_("normal"); - } else { - order="-r"; - sort_order=_("reverse"); - } - - snprintf(top1,sizeof(top1),"%s/top",outdirname); - sprintf(csort,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", tmp, order, sfield, top1, top2); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - if((fp_top1=fopen(top1,"r"))==NULL) { - debuga(_("(topuser) Cannot open file %s\n"),top1); - exit(EXIT_FAILURE); - } - - unlink(top2); - - snprintf(top3,sizeof(top3),"%s/index.html",outdirname); - if((fp_top3=fopen(top3,"w"))==NULL) { - debuga(_("(topuser) Cannot open file %s\n"),top3); - exit(EXIT_FAILURE); - } - - snprintf(title,sizeof(title),_("SARG report for %s"),period.text); - write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title,HTML_JS_SORTTABLE); - fputs("",fp_top3); - fprintf(fp_top3,_("Period: %s"),period.html); - fputs("\n",fp_top3); - fputs("",fp_top3); - fprintf(fp_top3,_("Sort: %s, %s"),sort_field,sort_order); - fputs("\n",fp_top3); - fprintf(fp_top3,"%s\n",_("Top users")); - close_html_header(fp_top3); - - fputs("
\n",fp_top3); - - if((ReportType & REPORT_TYPE_TOPSITES) != 0 && !Privacy) fprintf(fp_top3,"\n",_("Top sites")); - if((ReportType & REPORT_TYPE_SITES_USERS) != 0 && !Privacy) fprintf(fp_top3,"\n",_("Sites & Users")); - if(dansguardian_count) fprintf(fp_top3,"\n",_("DansGuardian")); - if(squidguard_count) fprintf(fp_top3,"\n",_("Redirector")); - if ((ReportType & REPORT_TYPE_DOWNLOADS) != 0 && download_count && !Privacy && ndownload) fprintf(fp_top3,"\n",_("Downloads")); - if ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Privacy) fprintf(fp_top3,"\n",_("Denied accesses")); - if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0 && authfail_count && !Privacy) fprintf(fp_top3,"\n",_("Authentication Failures")); - if(smartfilter) fprintf(fp_top3,"\n",_("SmartFilter")); - if(UserAgentLog[0] != '\0' && useragent_count) fprintf(fp_top3,"\n",_("Useragent")); - fputs("\n
%s
%s
%s
%s
%s
%s
%s
%s
%s
\n",fp_top3); - - if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) { - fputs("\n\n",fp_top3); - fclose (fp_top3); - return; - } - - fputs("
\n",fp_top3); - - if((TopUserFields & TOPUSERFIELDS_NUM) != 0) - fprintf(fp_top3,"",_("NUM")); - if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) { - fputs("",fp_top3); - } - if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { - fputs("",_("USERID")); - } - if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) - fprintf(fp_top3,"",_("CONNECT")); - if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) - fprintf(fp_top3,"",_("BYTES")); - if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) - fprintf(fp_top3,"",_("BYTES")); - if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) - fprintf(fp_top3,"",_("IN-CACHE-OUT")); - if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) - fprintf(fp_top3,"",_("ELAPSED TIME")); - if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) - fprintf(fp_top3,"",_("MILLISEC")); - if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) - fprintf(fp_top3,"",_("TIME")); - - fputs("\n",fp_top3); - - ntopuser = 0; - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the downloaded files\n")); - exit(EXIT_FAILURE); - } - - while((warea=longline_read(fp_top1,line))!=NULL) { - getword_start(&gwarea,warea); - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - debuga(_("There is a broken user in file %s\n"),top1); - exit(EXIT_FAILURE); - } - if (getword_atoll(&nbytes,&gwarea,'\t')<0) { - debuga(_("There is a broken number of bytes in file %s\n"),top1); - exit(EXIT_FAILURE); - } - if (getword_atoll(&nacc,&gwarea,'\t')<0) { - debuga(_("There is a broken number of access in file %s\n"),top1); - exit(EXIT_FAILURE); - } - if (getword_atoll(&elap,&gwarea,'\t')<0) { - debuga(_("There is a broken elpased time in file %s\n"),top1); - exit(EXIT_FAILURE); - } - if (getword_atoll(&incac,&gwarea,'\t')<0) { - debuga(_("There is a broken in-cache size in file %s\n"),top1); - exit(EXIT_FAILURE); - } - if (getword_atoll(&oucac,&gwarea,'\n')<0) { - debuga(_("There is a broken out-of-cache size in file %s\n"),top1); - exit(EXIT_FAILURE); - } - if(nacc < 1) - continue; - ntopuser = 1; - if(TopUsersNum > 0 && topcount >= TopUsersNum) break; - tnbytes=nbytes; - tnelap=elap; - - uinfo=userinfo_find_from_id(user); - if (!uinfo) { - debuga(_("Unknown user ID %s in file %s\n"),user,top1); - exit(EXIT_FAILURE); - } - - fputs("",fp_top3); - - posicao++; - if((TopUserFields & TOPUSERFIELDS_NUM) != 0) - fprintf(fp_top3,"",posicao); - - if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) { - fputs("\n",fp_top3); + fputs("\n",fp_top3); + fprintf(fp_top3,"\n",_("Top users")); + close_html_header(fp_top3); + + fputs("
%s%s%s%s%%%s%s%s%s%%%s
%d",fp_top3); + FILE *fp_in = NULL, *fp_ou = NULL, *fp_top1 = NULL, *fp_top2 = NULL, *fp_top3 = NULL; + long long int ttnbytes=0, ttnacc=0, tnacc=0; + long long int tnbytes=0, ttnelap=0, tnelap=0; + long long int tnincache=0, tnoucache=0, ttnincache=0, ttnoucache=0; + long long int nbytes; + long long int nacc; + long long int elap, incac, oucac; + double perc=0.00; + double perc2=0.00; + double inperc=0.00, ouperc=0.00; + int posicao=0; + char olduser[MAX_USER_LEN], csort[MAXLEN]; + char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN]; + char user[MAX_USER_LEN], tusr[MAXLEN]; + char ipantes[MAXLEN], nameantes[MAXLEN]; + const char *sfield="-n -k 2,2"; + const char *order; + const char *sort_field; + const char *sort_order; + char title[80]; + char *warea; + int totuser=0; + int topcount=0; + int cstatus; + struct getwordstruct gwarea; + longline line; + struct generalitemstruct item; + const struct userinfostruct *uinfo; + + ipantes[0]='\0'; + nameantes[0]='\0'; + + ntopuser = 0; + snprintf(wger,sizeof(wger),"%s/sarg-general",outdirname); + if((fp_in=fopen(wger,"r"))==NULL) { + debuga(_("(topuser) Cannot open file %s\n"),wger); + exit(EXIT_FAILURE); + } + + snprintf(top2,sizeof(top2),"%s/top.tmp",outdirname); + if((fp_top2=fopen(top2,"w"))==NULL) { + debuga(_("(topuser) Cannot open file %s\n"),top2); + exit(EXIT_FAILURE); + } + + olduser[0]='\0'; + totuser=0; + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the file %s\n"),wger); + exit(EXIT_FAILURE); + } + + while((warea=longline_read(fp_in,line))!=NULL) { + ger_read(warea,&item,wger); + if(item.total) continue; + if(strcmp(olduser,item.user) != 0) { + totuser++; + + if (olduser[0] != '\0') { + /* + This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable + to print a long long int unless it is exactly 64-bits long. + */ + fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache); + + ttnbytes+=tnbytes; + ttnacc+=tnacc; + ttnelap+=tnelap; + ttnincache+=tnincache; + ttnoucache+=tnoucache; + } + strcpy(olduser,item.user); + tnbytes=0; + tnacc=0; + tnelap=0; + tnincache=0; + tnoucache=0; + } + + tnbytes+=item.nbytes; + tnacc+=item.nacc; + tnelap+=item.nelap; + tnincache+=item.incache; + tnoucache+=item.oucache; + } + fclose(fp_in); + longline_destroy(&line); + + if (olduser[0] != '\0') { + /* + This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable + to print a long long int unless it is exactly 64-bits long. + */ + fprintf(fp_top2,"%s\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",olduser,(uint64_t)tnbytes,(uint64_t)tnacc,(uint64_t)tnelap,(uint64_t)tnincache,(uint64_t)tnoucache); + + ttnbytes+=tnbytes; + ttnacc+=tnacc; + ttnelap+=tnelap; + ttnincache+=tnincache; + ttnoucache+=tnoucache; + } + fclose(fp_top2); + + if((TopuserSort & TOPUSER_SORT_USER) != 0) { + sfield="-k 1,1"; + sort_field=_("user"); + } else if((TopuserSort & TOPUSER_SORT_CONNECT) != 0) { + sfield="-n -k 3,3"; + sort_field=_("connect"); + } else if((TopuserSort & TOPUSER_SORT_TIME) != 0) { + sfield="-n -k 4,4"; + sort_field=_("time"); + } else { + sort_field=_("bytes"); + } + + if((TopuserSort & TOPUSER_SORT_REVERSE) == 0) { + order=""; + sort_order=_("normal"); + } else { + order="-r"; + sort_order=_("reverse"); + } + + snprintf(top1,sizeof(top1),"%s/top",outdirname); + sprintf(csort,"sort -T \"%s\" %s %s -o \"%s\" \"%s\"", tmp, order, sfield, top1, top2); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + if((fp_top1=fopen(top1,"r"))==NULL) { + debuga(_("(topuser) Cannot open file %s\n"),top1); + exit(EXIT_FAILURE); + } + + unlink(top2); + + snprintf(top3,sizeof(top3),"%s/index.html",outdirname); + if((fp_top3=fopen(top3,"w"))==NULL) { + debuga(_("(topuser) Cannot open file %s\n"),top3); + exit(EXIT_FAILURE); + } + + snprintf(title,sizeof(title),_("SARG report for %s"),period.text); + write_html_header(fp_top3,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,title,HTML_JS_SORTTABLE); + fputs("
",fp_top3); + fprintf(fp_top3,_("Period: %s"),period.html); + fputs("
",fp_top3); + fprintf(fp_top3,_("Sort: %s, %s"),sort_field,sort_order); + fputs("
%s
\n",fp_top3); + + if((ReportType & REPORT_TYPE_TOPSITES) != 0 && !Privacy) fprintf(fp_top3,"\n",_("Top sites")); + if((ReportType & REPORT_TYPE_SITES_USERS) != 0 && !Privacy) fprintf(fp_top3,"\n",_("Sites & Users")); + if(dansguardian_count) fprintf(fp_top3,"\n",_("DansGuardian")); + if(squidguard_count) fprintf(fp_top3,"\n",_("Redirector")); + if ((ReportType & REPORT_TYPE_DOWNLOADS) != 0 && download_count && !Privacy && ndownload) fprintf(fp_top3,"\n",_("Downloads")); + if ((ReportType & REPORT_TYPE_DENIED) != 0 && denied_count && !Privacy) fprintf(fp_top3,"\n",_("Denied accesses")); + if ((ReportType & REPORT_TYPE_AUTH_FAILURES) != 0 && authfail_count && !Privacy) fprintf(fp_top3,"\n",_("Authentication Failures")); + if(smartfilter) fprintf(fp_top3,"\n",_("SmartFilter")); + if(UserAgentLog[0] != '\0' && useragent_count) fprintf(fp_top3,"\n",_("Useragent")); + fputs("\n
%s
%s
%s
%s
%s
%s
%s
%s
%s
\n",fp_top3); + + if ((ReportType & REPORT_TYPE_TOPUSERS) == 0) { + fputs("\n\n",fp_top3); + fclose (fp_top3); + return; + } + + fputs("
\n",fp_top3); + + if((TopUserFields & TOPUSERFIELDS_NUM) != 0) + fprintf(fp_top3,"",_("NUM")); + if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) { + fputs("",fp_top3); + } + if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { + fputs("",_("USERID")); + } + if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) + fprintf(fp_top3,"",_("CONNECT")); + if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) + fprintf(fp_top3,"",_("BYTES")); + if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) + fprintf(fp_top3,"",_("BYTES")); + if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) + fprintf(fp_top3,"",_("IN-CACHE-OUT")); + if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) + fprintf(fp_top3,"",_("ELAPSED TIME")); + if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) + fprintf(fp_top3,"",_("MILLISEC")); + if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) + fprintf(fp_top3,"",_("TIME")); + + fputs("\n",fp_top3); + + ntopuser = 0; + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the downloaded files\n")); + exit(EXIT_FAILURE); + } + + while((warea=longline_read(fp_top1,line))!=NULL) { + getword_start(&gwarea,warea); + if (getword(user,sizeof(user),&gwarea,'\t')<0) { + debuga(_("There is a broken user in file %s\n"),top1); + exit(EXIT_FAILURE); + } + if (getword_atoll(&nbytes,&gwarea,'\t')<0) { + debuga(_("There is a broken number of bytes in file %s\n"),top1); + exit(EXIT_FAILURE); + } + if (getword_atoll(&nacc,&gwarea,'\t')<0) { + debuga(_("There is a broken number of access in file %s\n"),top1); + exit(EXIT_FAILURE); + } + if (getword_atoll(&elap,&gwarea,'\t')<0) { + debuga(_("There is a broken elpased time in file %s\n"),top1); + exit(EXIT_FAILURE); + } + if (getword_atoll(&incac,&gwarea,'\t')<0) { + debuga(_("There is a broken in-cache size in file %s\n"),top1); + exit(EXIT_FAILURE); + } + if (getword_atoll(&oucac,&gwarea,'\n')<0) { + debuga(_("There is a broken out-of-cache size in file %s\n"),top1); + exit(EXIT_FAILURE); + } + if(nacc < 1) + continue; + ntopuser = 1; + if(TopUsersNum > 0 && topcount >= TopUsersNum) break; + tnbytes=nbytes; + tnelap=elap; + + uinfo=userinfo_find_from_id(user); + if (!uinfo) { + debuga(_("Unknown user ID %s in file %s\n"),user,top1); + exit(EXIT_FAILURE); + } + + fputs("",fp_top3); + + posicao++; + if((TopUserFields & TOPUSERFIELDS_NUM) != 0) + fprintf(fp_top3,"",posicao); + + if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) { + fputs("",uinfo->filename,uinfo->filename,ImageFile,_("date/time report")); - } else { - sprintf(val1,"%s/d%s.html",outdirname,uinfo->filename); - unlink(val1); - } - if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { - if((ReportType & REPORT_TYPE_USERS_SITES) == 0) - fprintf(fp_top3,"",name); - else - fprintf(fp_top3,"",uinfo->filename,uinfo->filename,uinfo->label); - } - if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) { - fputs("",fixnum(nacc,1)); - } - if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) { - fputs("",fixnum(tnbytes,1)); - } - if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) { - perc=(ttnbytes) ? tnbytes * 100. / ttnbytes : 0.; - fprintf(fp_top3,"",perc); - } - if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) { - inperc=(tnbytes) ? incac * 100. / tnbytes : 0.; - ouperc=(tnbytes) ? oucac * 100. / tnbytes : 0.; - fprintf(fp_top3,"",inperc,ouperc); + fprintf(fp_top3,"\"T\"",uinfo->filename,uinfo->filename,ImageFile,_("date/time report")); + } else { + sprintf(val1,"%s/d%s.html",outdirname,uinfo->filename); + unlink(val1); + } + if((TopUserFields & TOPUSERFIELDS_USERID) != 0) { + if((ReportType & REPORT_TYPE_USERS_SITES) == 0) + fprintf(fp_top3,"",name); + else + fprintf(fp_top3,"",uinfo->filename,uinfo->filename,uinfo->label); + } + if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) { + fputs("",fixnum(nacc,1)); + } + if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) { + fputs("",fixnum(tnbytes,1)); + } + if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) { + perc=(ttnbytes) ? tnbytes * 100. / ttnbytes : 0.; + fprintf(fp_top3,"",perc); + } + if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) { + inperc=(tnbytes) ? incac * 100. / tnbytes : 0.; + ouperc=(tnbytes) ? oucac * 100. / tnbytes : 0.; + fprintf(fp_top3,"",inperc,ouperc); #ifdef ENABLE_DOUBLE_CHECK_DATA - if (fabs(inperc+ouperc-100.)>=0.01) { - debuga(_("The total of the in-cache and cache-miss is not 100%% at position %d (user %s)\n"),posicao,uinfo->label); - } + if (fabs(inperc+ouperc-100.)>=0.01) { + debuga(_("The total of the in-cache and cache-miss is not 100%% at position %d (user %s)\n"),posicao,uinfo->label); + } #endif - } - if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) { - fputs("",buildtime(tnelap)); - } - if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) { - fputs("",fixnum2(tnelap,1)); - } - if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) { - perc2=(ttnelap) ? elap * 100. / ttnelap : 0.; - fprintf(fp_top3,"",perc2); - } - - fputs("\n",fp_top3); - - topcount++; - } - fclose(fp_top1); - unlink(top1); - longline_destroy(&line); - - if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) { - fputs("",fp_top3); - if((TopUserFields & TOPUSERFIELDS_NUM) != 0) - fputs("",fp_top3); - if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) - fputs("",fp_top3); - fprintf(fp_top3,"",_("TOTAL")); - - if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) - fprintf(fp_top3,"",fixnum(ttnacc,1)); - if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) - fprintf(fp_top3,"",fixnum(ttnbytes,1)); - if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) - fputs("",fp_top3); - if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) - { - inperc=(ttnbytes) ? ttnincache * 100. / ttnbytes : 0.; - ouperc=(ttnbytes) ? ttnoucache *100. / ttnbytes : 0.; - fprintf(fp_top3,"",inperc,ouperc); + } + if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) { + fputs("",buildtime(tnelap)); + } + if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) { + fputs("",fixnum2(tnelap,1)); + } + if((TopUserFields & TOPUSERFIELDS_PTIME) != 0) { + perc2=(ttnelap) ? elap * 100. / ttnelap : 0.; + fprintf(fp_top3,"",perc2); + } + + fputs("\n",fp_top3); + + topcount++; + } + fclose(fp_top1); + unlink(top1); + longline_destroy(&line); + + if((TopUserFields & TOPUSERFIELDS_TOTAL) != 0) { + fputs("",fp_top3); + if((TopUserFields & TOPUSERFIELDS_NUM) != 0) + fputs("",fp_top3); + if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) + fputs("",fp_top3); + fprintf(fp_top3,"",_("TOTAL")); + + if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) + fprintf(fp_top3,"",fixnum(ttnacc,1)); + if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) + fprintf(fp_top3,"",fixnum(ttnbytes,1)); + if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) + fputs("",fp_top3); + if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) + { + inperc=(ttnbytes) ? ttnincache * 100. / ttnbytes : 0.; + ouperc=(ttnbytes) ? ttnoucache *100. / ttnbytes : 0.; + fprintf(fp_top3,"",inperc,ouperc); #ifdef ENABLE_DOUBLE_CHECK_DATA - if (fabs(inperc+ouperc-100.)>=0.01) { - debuga(_("The total of the in-cache and cache-miss is not 100%%\n")); - } + if (fabs(inperc+ouperc-100.)>=0.01) { + debuga(_("The total of the in-cache and cache-miss is not 100%%\n")); + } #endif - } - if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) - fprintf(fp_top3,"",buildtime(ttnelap)); - if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) - fprintf(fp_top3,"",fixnum2(ttnelap,1)); - - fputs("\n",fp_top3); - } - - if(ntopuser && (TopUserFields & TOPUSERFIELDS_AVERAGE) != 0) { - fputs("",fp_top3); - if((TopUserFields & TOPUSERFIELDS_NUM) != 0) - fputs("",fp_top3); - if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) - fputs("",fp_top3); - fprintf(fp_top3,"",_("AVERAGE")); - - if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) - fprintf(fp_top3,"",fixnum(ttnacc/totuser,1)); - if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) { - tnbytes=(totuser) ? ttnbytes / totuser : 0; - fprintf(fp_top3,"",fixnum(tnbytes,1)); - } - if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) - fputs("",fp_top3); - if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) - fputs("",fp_top3); - if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) - fprintf(fp_top3,"",buildtime(ttnelap/totuser)); - if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) - fprintf(fp_top3,"",fixnum2(ttnelap/totuser,1)); - fputs("\n",fp_top3); - } - - fputs("
%s%s%s%s%%%s%s%s%s%%%s
%d",fp_top3); #ifdef HAVE_GD - if(Graphs && GraphFont[0]!='\0') { - //fprintf(fp_top3,"\"G\" ",uinfo->filename,ImageFile,_("Graphic")); - fprintf(fp_top3,"\"G\" ",uinfo->filename,ImageFile,_("Graphic")); - } + if(Graphs && GraphFont[0]!='\0') { + //fprintf(fp_top3,"\"G\" ",uinfo->filename,ImageFile,_("Graphic")); + fprintf(fp_top3,"\"G\" ",uinfo->filename,ImageFile,_("Graphic")); + } #endif - fprintf(fp_top3,"\"T\"%s%s%s%s%3.2lf%%%3.2lf%%%3.2lf%%%s%s%s%s%3.2lf%%%3.2lf%%%3.2lf%%%s%s%3.2lf%%
%s%s%15s%3.2lf%%%3.2lf%%%s%s%3.2lf%%
%s%s%15s%3.2lf%%%3.2lf%%%s%s
%s%s%15s%s%s
\n",fp_top3); - if (write_html_trailer(fp_top3)<0) - debuga(_("Write error in top user list %s\n"),top3); - if (fclose(fp_top3)==EOF) - debuga(_("Failed to close the top user list %s - %s\n"),top3,strerror(errno)); - - snprintf(tusr,sizeof(tusr),"%s/sarg-users",outdirname); - if((fp_ou=fopen(tusr,"w"))==NULL) { - debuga(_("(topuser) Cannot open file %s\n"),tusr); - exit(EXIT_FAILURE); - } - fprintf(fp_ou,"%d\n",totuser); - if (fclose(fp_ou)==EOF) - debuga(_("Failed to close file %s - %s\n"),tusr,strerror(errno)); - - return; + } + if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) + fprintf(fp_top3,"%s",buildtime(ttnelap)); + if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) + fprintf(fp_top3,"%s",fixnum2(ttnelap,1)); + + fputs("\n",fp_top3); + } + + if(ntopuser && (TopUserFields & TOPUSERFIELDS_AVERAGE) != 0) { + fputs("",fp_top3); + if((TopUserFields & TOPUSERFIELDS_NUM) != 0) + fputs("",fp_top3); + if((TopUserFields & TOPUSERFIELDS_DATE_TIME) !=0 && (ReportType & REPORT_TYPE_DATE_TIME) != 0) + fputs("",fp_top3); + fprintf(fp_top3,"%s",_("AVERAGE")); + + if((TopUserFields & TOPUSERFIELDS_CONNECT) != 0) + fprintf(fp_top3,"%s",fixnum(ttnacc/totuser,1)); + if((TopUserFields & TOPUSERFIELDS_BYTES) != 0) { + tnbytes=(totuser) ? ttnbytes / totuser : 0; + fprintf(fp_top3,"%15s",fixnum(tnbytes,1)); + } + if((TopUserFields & TOPUSERFIELDS_SETYB) != 0) + fputs("",fp_top3); + if((TopUserFields & TOPUSERFIELDS_IN_CACHE_OUT) != 0) + fputs("",fp_top3); + if((TopUserFields & TOPUSERFIELDS_USED_TIME) != 0) + fprintf(fp_top3,"%s",buildtime(ttnelap/totuser)); + if((TopUserFields & TOPUSERFIELDS_MILISEC) != 0) + fprintf(fp_top3,"%s",fixnum2(ttnelap/totuser,1)); + fputs("\n",fp_top3); + } + + fputs("
\n",fp_top3); + if (write_html_trailer(fp_top3)<0) + debuga(_("Write error in top user list %s\n"),top3); + if (fclose(fp_top3)==EOF) + debuga(_("Failed to close the top user list %s - %s\n"),top3,strerror(errno)); + + snprintf(tusr,sizeof(tusr),"%s/sarg-users",outdirname); + if((fp_ou=fopen(tusr,"w"))==NULL) { + debuga(_("(topuser) Cannot open file %s\n"),tusr); + exit(EXIT_FAILURE); + } + fprintf(fp_ou,"%d\n",totuser); + if (fclose(fp_ou)==EOF) + debuga(_("Failed to close file %s - %s\n"),tusr,strerror(errno)); + + return; } diff --git a/totday.c b/totday.c index 38adc11..bf6ddf5 100644 --- a/totday.c +++ b/totday.c @@ -29,115 +29,114 @@ void day_totalize(const char *tmp, const struct userinfostruct *uinfo, int indexonly) { + FILE *fp_in, *fp_ou; - FILE *fp_in, *fp_ou; + char buf[200]; + char date[20]; + long long int hour; + long long int bytes; + long long int elap; + long long int tbytes[MAX_DATETIME_DAYS*24]; + long long int telap[MAX_DATETIME_DAYS*24]; + int day,month,year; + int daylist[MAX_DATETIME_DAYS]; + int ndaylist; + int daynum; + int dayidx; + int i; + char wdirname[2048]; + char arqout[2048]; + char colsep; + struct getwordstruct gwarea; - char buf[200]; - char date[20]; - long long int hour; - long long int bytes; - long long int elap; - long long int tbytes[MAX_DATETIME_DAYS*24]; - long long int telap[MAX_DATETIME_DAYS*24]; - int day,month,year; - int daylist[MAX_DATETIME_DAYS]; - int ndaylist; - int daynum; - int dayidx; - int i; - char wdirname[2048]; - char arqout[2048]; - char colsep; - struct getwordstruct gwarea; + if(indexonly) return; + if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; + if (datetimeby==0) return; - if(indexonly) return; - if((ReportType & REPORT_TYPE_USERS_SITES) == 0) return; - if (datetimeby==0) return; + if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) { + debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".htmp"); + exit(EXIT_FAILURE); + } + if (snprintf(arqout,sizeof(arqout),"%s/%s.day",tmp,uinfo->filename)>=sizeof(arqout)) { + debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".day"); + exit(EXIT_FAILURE); + } - if (snprintf(wdirname,sizeof(wdirname),"%s/%s.htmp",tmp,uinfo->filename)>=sizeof(wdirname)) { - debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".htmp"); - exit(EXIT_FAILURE); - } - if (snprintf(arqout,sizeof(arqout),"%s/%s.day",tmp,uinfo->filename)>=sizeof(arqout)) { - debuga(_("File name too long: %s/%s%s\n"),tmp,uinfo->filename,".day"); - exit(EXIT_FAILURE); - } + if((fp_in=fopen(wdirname,"r"))==NULL) { + debuga(_("(totday) Cannot open log file %s\n"),wdirname); + exit(EXIT_FAILURE); + } - if((fp_in=fopen(wdirname,"r"))==NULL) { - debuga(_("(totday) Cannot open log file %s\n"),wdirname); - exit(EXIT_FAILURE); - } + memset(tbytes,0,sizeof(tbytes)); + memset(telap,0,sizeof(tbytes)); + ndaylist=0; - memset(tbytes,0,sizeof(tbytes)); - memset(telap,0,sizeof(tbytes)); - ndaylist=0; + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + fixendofline(buf); + getword_start(&gwarea,buf); + if (getword(date,sizeof(date),&gwarea,'\t')<0 || getword_atoll(&hour,&gwarea,'\t')<0) { + debuga(_("There is a broken record or garbage in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + if (sscanf(date,"%d/%d/%d",&day,&month,&year)!=3) continue; + if (day<1 || day>31 || month<1 || month>12 || year>9999) continue; + if (hour<0 || hour>=24) continue; + daynum=(year*10000)+(month*100)+day; + for (dayidx=0 ; dayidx=ndaylist) { + if (dayidx>=sizeof(daylist)/sizeof(*daylist)) { + debuga(_("Too many different dates in %s\n"),wdirname); + exit(EXIT_FAILURE); + } + daylist[ndaylist++]=daynum; + } + i=dayidx*24+hour; + if ((datetimeby & DATETIME_BYTE)!=0) { + colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; + if (getword_atoll(&bytes,&gwarea,colsep)<0) { + debuga(_("Invalid number of bytes in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + tbytes[i]+=bytes; + } + if ((datetimeby & DATETIME_ELAP)!=0) { + if (getword_atoll(&elap,&gwarea,'\0')<0) { + debuga(_("Invalid elapsed time in file %s\n"),wdirname); + exit(EXIT_FAILURE); + } + telap[i]+=elap; + } + } + fclose(fp_in); - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - fixendofline(buf); - getword_start(&gwarea,buf); - if (getword(date,sizeof(date),&gwarea,'\t')<0 || getword_atoll(&hour,&gwarea,'\t')<0) { - debuga(_("There is a broken record or garbage in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - if (sscanf(date,"%d/%d/%d",&day,&month,&year)!=3) continue; - if (day<1 || day>31 || month<1 || month>12 || year>9999) continue; - if (hour<0 || hour>=24) continue; - daynum=(year*10000)+(month*100)+day; - for (dayidx=0 ; dayidx=ndaylist) { - if (dayidx>=sizeof(daylist)/sizeof(*daylist)) { - debuga(_("Too many different dates in %s\n"),wdirname); - exit(EXIT_FAILURE); - } - daylist[ndaylist++]=daynum; - } - i=dayidx*24+hour; - if ((datetimeby & DATETIME_BYTE)!=0) { - colsep=((datetimeby & DATETIME_ELAP)!=0) ? '\t' : '\0'; - if (getword_atoll(&bytes,&gwarea,colsep)<0) { - debuga(_("Invalid number of bytes in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - tbytes[i]+=bytes; - } - if ((datetimeby & DATETIME_ELAP)!=0) { - if (getword_atoll(&elap,&gwarea,'\0')<0) { - debuga(_("Invalid elapsed time in file %s\n"),wdirname); - exit(EXIT_FAILURE); - } - telap[i]+=elap; - } - } - fclose(fp_in); + if((fp_ou=fopen(arqout,"w"))==NULL) { + debuga(_("(totday) Cannot open log file %s\n"),arqout); + exit(EXIT_FAILURE); + } - if((fp_ou=fopen(arqout,"w"))==NULL) { - debuga(_("(totday) Cannot open log file %s\n"),arqout); - exit(EXIT_FAILURE); - } + for (i=0 ; isizeof(daylist)/sizeof(*daylist)) continue; + hour=i%24; + daynum=daylist[dayidx]; + day=daynum%100; + month=(daynum/100)%100; + year=daynum/10000; + fprintf(fp_ou,"%d/%d/%d\t%d",day,month,year,(int)hour); + if ((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)tbytes[i]); + if ((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)telap[i]); + fputs("\n",fp_ou); + } - for (i=0 ; isizeof(daylist)/sizeof(*daylist)) continue; - hour=i%24; - daynum=daylist[dayidx]; - day=daynum%100; - month=(daynum/100)%100; - year=daynum/10000; - fprintf(fp_ou,"%d/%d/%d\t%d",day,month,year,(int)hour); - if ((datetimeby & DATETIME_BYTE)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)tbytes[i]); - if ((datetimeby & DATETIME_ELAP)!=0) fprintf(fp_ou,"\t%"PRIu64"",(uint64_t)telap[i]); - fputs("\n",fp_ou); - } + if (fclose(fp_ou)==EOF) { + debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno)); + exit(EXIT_FAILURE); + } - if (fclose(fp_ou)==EOF) { - debuga(_("Failed to close file %s - %s\n"),arqout,strerror(errno)); - exit(EXIT_FAILURE); - } - - if (unlink(wdirname)==-1) { - debuga(_("Cannot delete temporary file %s - %s\n"),wdirname,strerror(errno)); - exit(EXIT_FAILURE); - } - return; + if (unlink(wdirname)==-1) { + debuga(_("Cannot delete temporary file %s - %s\n"),wdirname,strerror(errno)); + exit(EXIT_FAILURE); + } + return; } diff --git a/totger.c b/totger.c index a521b77..2127394 100644 --- a/totger.c +++ b/totger.c @@ -29,56 +29,56 @@ int totalger(const char *dirname, int debug, const char *outdir) { - FILE *fp_in; - long long int tnacc=0; - long long int tnbytes=0; - long long int telap=0; - long long int tincache=0, toucache=0; - char wger[MAXLEN]; - char *warea; - longline line; - struct generalitemstruct item; + FILE *fp_in; + long long int tnacc=0; + long long int tnbytes=0; + long long int telap=0; + long long int tincache=0, toucache=0; + char wger[MAXLEN]; + char *warea; + longline line; + struct generalitemstruct item; - snprintf(wger,sizeof(wger),"%s/sarg-general",dirname); - if((fp_in=fopen(wger,"r+"))==NULL) { - debuga(_("(totger) Cannot open file %s\n"),wger); - exit(EXIT_FAILURE); - } + snprintf(wger,sizeof(wger),"%s/sarg-general",dirname); + if((fp_in=fopen(wger,"r+"))==NULL) { + debuga(_("(totger) Cannot open file %s\n"),wger); + exit(EXIT_FAILURE); + } - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the temporary file %s\n"),wger); - exit(EXIT_FAILURE); - } + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the temporary file %s\n"),wger); + exit(EXIT_FAILURE); + } - while((warea=longline_read(fp_in,line))!=NULL) - { - ger_read(warea,&item,wger); - tnacc+=item.nacc; - tnbytes+=item.nbytes; - telap+=item.nelap; - tincache+=item.incache; - toucache+=item.oucache; - } + while((warea=longline_read(fp_in,line))!=NULL) + { + ger_read(warea,&item,wger); + tnacc+=item.nacc; + tnbytes+=item.nbytes; + telap+=item.nelap; + tincache+=item.incache; + toucache+=item.oucache; + } - longline_destroy(&line); + longline_destroy(&line); - if (fseek(fp_in,0,SEEK_END)==-1) { - debuga(_("Failed to move to the end of %s - %s\n"),wger,strerror(errno)); - exit(EXIT_FAILURE); - } + if (fseek(fp_in,0,SEEK_END)==-1) { + debuga(_("Failed to move to the end of %s - %s\n"),wger,strerror(errno)); + exit(EXIT_FAILURE); + } - /* - This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable - to print a long long int unless it is exactly 64-bits long. - */ - if (fprintf(fp_in,"TOTAL\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)telap,(uint64_t)tincache,(uint64_t)toucache)<0) { - debuga(_("Failed to write the total line in %s\n"),wger); - exit(EXIT_FAILURE); - } - if (fclose(fp_in)==EOF) { - debuga(_("Failed to close file %s - %s\n"),wger,strerror(errno)); - exit(EXIT_FAILURE); - } + /* + This complicated printf is due to Microsoft's inability to comply with any standard. Msvcrt is unable + to print a long long int unless it is exactly 64-bits long. + */ + if (fprintf(fp_in,"TOTAL\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\t%"PRIu64"\n",(uint64_t)tnacc,(uint64_t)tnbytes,(uint64_t)telap,(uint64_t)tincache,(uint64_t)toucache)<0) { + debuga(_("Failed to write the total line in %s\n"),wger); + exit(EXIT_FAILURE); + } + if (fclose(fp_in)==EOF) { + debuga(_("Failed to close file %s - %s\n"),wger,strerror(errno)); + exit(EXIT_FAILURE); + } - return (0); + return (0); } diff --git a/usage.c b/usage.c index e7337ca..f622af2 100644 --- a/usage.c +++ b/usage.c @@ -29,32 +29,32 @@ void usage(const char *prog) { - printf(_("Usage: %s [options...]\n"), prog); - puts (_(" -a Hostname or IP address")); - puts (_(" -b Useragent log")); - puts (_(" -c Exclude file")); - puts (_(" -d Date from-until dd/mm/yyyy-dd/mm/yyyy")); - puts (_(" -e Email address to send reports (stdout for console)")); - printf(_(" -f Config file (%s/sarg.conf)\n"),SYSCONFDIR); - puts (_(" -g Date format [e=Europe -> dd/mm/yyyy, u=USA -> mm/dd/yyyy]")); - puts (_(" -h This help")); - puts (_(" -i Reports by user and IP address")); - puts (_(" -l Input log")); - puts (_(" -n Resolve IP Address")); - puts (_(" -o Output dir")); - puts (_(" -p Use Ip Address instead of userid (reports)")); - puts (_(" -s Accessed site [Eg. www.microsoft.com, www.netscape.com]")); - puts (_(" -t Time [HH, HH:MM]")); - puts (_(" -u User")); - puts (_(" -w Temporary dir")); - puts (_(" -x Debug messages")); - puts (_(" -z Process messages")); - puts (_(" -convert Convert the access.log file to a legible date")); - puts (_(" -split Split the log file by date in -d parameter")); - printf("\n\t%s-%s\n",PGM,VERSION); - puts ("\thttp://sarg.sourceforge.net"); - puts (_("\n\tPlease donate to the sarg project:")); - puts ("\t\thttp://sarg.sourceforge.net/donations.php\n"); + printf(_("Usage: %s [options...]\n"), prog); + puts (_(" -a Hostname or IP address")); + puts (_(" -b Useragent log")); + puts (_(" -c Exclude file")); + puts (_(" -d Date from-until dd/mm/yyyy-dd/mm/yyyy")); + puts (_(" -e Email address to send reports (stdout for console)")); + printf(_(" -f Config file (%s/sarg.conf)\n"),SYSCONFDIR); + puts (_(" -g Date format [e=Europe -> dd/mm/yyyy, u=USA -> mm/dd/yyyy]")); + puts (_(" -h This help")); + puts (_(" -i Reports by user and IP address")); + puts (_(" -l Input log")); + puts (_(" -n Resolve IP Address")); + puts (_(" -o Output dir")); + puts (_(" -p Use Ip Address instead of userid (reports)")); + puts (_(" -s Accessed site [Eg. www.microsoft.com, www.netscape.com]")); + puts (_(" -t Time [HH, HH:MM]")); + puts (_(" -u User")); + puts (_(" -w Temporary dir")); + puts (_(" -x Debug messages")); + puts (_(" -z Process messages")); + puts (_(" -convert Convert the access.log file to a legible date")); + puts (_(" -split Split the log file by date in -d parameter")); + printf("\n\t%s-%s\n",PGM,VERSION); + puts ("\thttp://sarg.sourceforge.net"); + puts (_("\n\tPlease donate to the sarg project:")); + puts ("\t\thttp://sarg.sourceforge.net/donations.php\n"); - return; + return; } diff --git a/useragent.c b/useragent.c index a0f967c..7dad971 100644 --- a/useragent.c +++ b/useragent.c @@ -29,292 +29,290 @@ void useragent(void) { - - FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL; - char buf[MAXLEN]; - char ip[MAXLEN], data[MAXLEN], agent[MAXLEN], user[MAXLEN]; - char ipbefore[MAXLEN], namebefore[MAXLEN]; - char tagent[MAXLEN]; - char user_old[MAXLEN]="$#%0a3bc6"; - char agent_old[MAXLEN]="$#%0a3bc6"; - char hfile[MAXLEN]; - char idate[MAXLEN], fdate[MAXLEN]; - char tmp2[MAXLEN]; - char tmp3[MAXLEN]; - char day[4],month[5],year[5], wdate[20]; - char csort[MAXLEN]; - int agentot=0, agentot2=0, agentdif=0, cont=0, nagent; - unsigned long totregsl=0; - int cstatus; - int ndate; - double perc; - struct getwordstruct gwarea, gwarea1; - - ip[0]='\0'; - data[0]='\0'; - agent[0]='\0'; - user[0]='\0'; - user_old[0]='\0'; - agent_old[0]='\0'; - ipbefore[0]='\0'; - namebefore[0]='\0'; - - sprintf(tmp3,"%s/squagent.unsort",tmp); - sprintf(tmp2,"%s/squagent.log",tmp); - - if((fp_in=fopen(UserAgentLog,"r"))==NULL) { - debuga(_("(useragent) Cannot open file %s\n"),UserAgentLog); - exit(EXIT_FAILURE); - } - - if((fp_ou=fopen(tmp3,"w"))==NULL) { - debuga(_("(useragent) Cannot open file %s\n"),tmp3); - exit(EXIT_FAILURE); - } - - if(debug) { - debuga(_("Reading useragent log: %s\n"),UserAgentLog); - } - - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - totregsl++; - getword_start(&gwarea,buf); - if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 || - getword(data,sizeof(data),&gwarea,' ')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); - exit(EXIT_FAILURE); - } - getword_start(&gwarea1,data); - if (getword(day,sizeof(day),&gwarea1,'/')<0 || getword(month,sizeof(month),&gwarea1,'/')<0 || - getword(year,sizeof(year),&gwarea1,':')<0) { - debuga(_("Maybe you have a broken date in your %s file\n"),UserAgentLog); - exit(EXIT_FAILURE); - } - buildymd(day,month,year,wdate); - ndate=atoi(wdate); - if (ndateduntil) break; - if(totregsl == 1) - strcpy(idate,data); - strcpy(fdate,data); - if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) { - debuga(_("Maybe you have a broken useragent entry in your %s file\n"),UserAgentLog); - exit(EXIT_FAILURE); - } - - if(gwarea.current[0]!='\0') { - if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); - exit(EXIT_FAILURE); - } - if(user[0] == '-') - strcpy(user,ip); - if(user[0] == '\0') - strcpy(user,ip); - } else { - strcpy(user,ip); - } - - fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user); - useragent_count++; - } - - if(debug) { - debuga(_(" Records read: %ld\n"),totregsl); - } - - fclose(fp_in); - if (fclose(fp_ou)==EOF) { - debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno)); - exit(EXIT_FAILURE); - } - - if(debug) { - debuga(_("Sorting file: %s\n"),tmp2); - } - - sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - if((fp_in=fopen(tmp2,"r"))==NULL) { - debuga(_("(useragent) Cannot open file %s\n"),tmp2); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - unlink(tmp3); - - snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname); - if((fp_ht=fopen(hfile,"w"))==NULL) { - debuga(_("(useragent) Cannot open file %s\n"),hfile); - exit(EXIT_FAILURE); - } - - if(debug) - debuga(_("Making Useragent report\n")); - - write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE); - fprintf(fp_ht,"%s\n",_("Squid Useragent's Report")); - fprintf(fp_ht,"%s: %s - %s\n",_("Period"),idate,fdate); - close_html_header(fp_ht); - - fputs("

\n",fp_ht); - - fputs("
\n",fp_ht); - fputs("",fp_ht); - - fprintf(fp_ht,"\n",_("USERID"),_("AGENT")); - - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - getword_start(&gwarea,buf); - if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("There is an invalid IP address in file %s\n"),tmp2); - exit(EXIT_FAILURE); - } - - if(Ip2Name) { - if(strcmp(ip,ipbefore) != 0) { - strcpy(ipbefore,ip); - ip2name(ip,sizeof(ip)); - strcpy(namebefore,ip); - } else strcpy(ip,namebefore); - } - - if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { - debuga(_("There is an invalid useragent in file %s\n"),tmp2); - exit(EXIT_FAILURE); - } - if (getword(user,sizeof(user),&gwarea,'\t')<0) { - debuga(_("There is an invalid user ID in file %s\n"),tmp2); - exit(EXIT_FAILURE); - } - - if(strcmp(user,user_old) != 0) { - fprintf(fp_ht,"\n",fp_ht); - strcpy(user_old,user); - strcpy(agent_old,agent); - } else if(strcmp(agent,agent_old) != 0) { - fputs("\n",fp_ht); - strcpy(agent_old,agent); - } - } - - fputs("
  
%s%s
%s",user); - output_html_string(fp_ht,agent,250); - fputs("
",fp_ht); - output_html_string(fp_ht,agent,250); - fputs("
\n",fp_ht); - fclose(fp_in); - - sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - if((fp_in=fopen(tmp3,"r"))==NULL) { - debuga(_("(useragent) Cannot open file %s\n"),tmp3); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - unlink(tmp2); - - if((fp_ou=fopen(tmp2,"w"))==NULL) { - debuga(_("(useragent) Cannot open file %s\n"),tmp2); - exit(EXIT_FAILURE); - } - - agent_old[0]='\0'; - cont=0; - - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - getword_start(&gwarea,buf); - if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("There is an invalid IP address in file %s\n"),tmp3); - exit(EXIT_FAILURE); - } - if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { - debuga(_("There is an invalid useragent in file %s\n"),tmp3); - exit(EXIT_FAILURE); - } - - if(!cont) { - cont++; - strcpy(agent_old,agent); - } - - if(strcmp(agent,agent_old) != 0) { - agentdif++; - fprintf(fp_ou,"%06d %s\n",agentot,agent_old); - strcpy(agent_old,agent); - agentot2+=agentot; - agentot=0; - } - agentot++; - } - agentdif++; - fprintf(fp_ou,"%06d %s\n",agentot,agent); - agentot2+=agentot; - - fclose(fp_in); - if (fclose(fp_ou)==EOF) { - debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno)); - exit(EXIT_FAILURE); - } - - unlink(tmp3); - - sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2); - cstatus=system(csort); - if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { - debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - if((fp_in=fopen(tmp3,"r"))==NULL) { - debuga(_("(useragent) Cannot open file %s\n"),tmp3); - debuga(_("sort command: %s\n"),csort); - exit(EXIT_FAILURE); - } - - unlink(tmp2); - - fputs("

\n",fp_ht); - - fputs("\n",fp_ht); - fprintf(fp_ht,"\n",_("AGENT"),_("TOTAL")); - - perc=0.; - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - fixendofline(buf); - getword_start(&gwarea,buf); - if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) { - debuga(_("There is an invalid useragent in file %s\n"),tmp3); - exit(EXIT_FAILURE); - } - nagent=atoi(tagent); - perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.; - - fputs("\n",nagent,perc); - } - fclose(fp_in); - - fputs("
%s%s%%
",fp_ht); - output_html_string(fp_ht,gwarea.current,250); - fprintf(fp_ht,"%d%3.2lf
\n",fp_ht); - if (write_html_trailer(fp_ht)<0) - debuga(_("Write error in file %s\n"),hfile); - if (fclose(fp_ht)==EOF) - debuga(_("Failed to close file %s - %s\n"),hfile,strerror(errno)); - - unlink(tmp3); - - return; - + FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL; + char buf[MAXLEN]; + char ip[MAXLEN], data[MAXLEN], agent[MAXLEN], user[MAXLEN]; + char ipbefore[MAXLEN], namebefore[MAXLEN]; + char tagent[MAXLEN]; + char user_old[MAXLEN]="$#%0a3bc6"; + char agent_old[MAXLEN]="$#%0a3bc6"; + char hfile[MAXLEN]; + char idate[MAXLEN], fdate[MAXLEN]; + char tmp2[MAXLEN]; + char tmp3[MAXLEN]; + char day[4],month[5],year[5], wdate[20]; + char csort[MAXLEN]; + int agentot=0, agentot2=0, agentdif=0, cont=0, nagent; + unsigned long totregsl=0; + int cstatus; + int ndate; + double perc; + struct getwordstruct gwarea, gwarea1; + + ip[0]='\0'; + data[0]='\0'; + agent[0]='\0'; + user[0]='\0'; + user_old[0]='\0'; + agent_old[0]='\0'; + ipbefore[0]='\0'; + namebefore[0]='\0'; + + sprintf(tmp3,"%s/squagent.unsort",tmp); + sprintf(tmp2,"%s/squagent.log",tmp); + + if((fp_in=fopen(UserAgentLog,"r"))==NULL) { + debuga(_("(useragent) Cannot open file %s\n"),UserAgentLog); + exit(EXIT_FAILURE); + } + + if((fp_ou=fopen(tmp3,"w"))==NULL) { + debuga(_("(useragent) Cannot open file %s\n"),tmp3); + exit(EXIT_FAILURE); + } + + if(debug) { + debuga(_("Reading useragent log: %s\n"),UserAgentLog); + } + + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + totregsl++; + getword_start(&gwarea,buf); + if (getword(ip,sizeof(ip),&gwarea,' ')<0 || getword_skip(MAXLEN,&gwarea,'[')<0 || + getword(data,sizeof(data),&gwarea,' ')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); + exit(EXIT_FAILURE); + } + getword_start(&gwarea1,data); + if (getword(day,sizeof(day),&gwarea1,'/')<0 || getword(month,sizeof(month),&gwarea1,'/')<0 || + getword(year,sizeof(year),&gwarea1,':')<0) { + debuga(_("Maybe you have a broken date in your %s file\n"),UserAgentLog); + exit(EXIT_FAILURE); + } + buildymd(day,month,year,wdate); + ndate=atoi(wdate); + if (ndateduntil) break; + if(totregsl == 1) + strcpy(idate,data); + strcpy(fdate,data); + if (getword_skip(MAXLEN,&gwarea,'"')<0 || getword(agent,sizeof(agent),&gwarea,'"')<0) { + debuga(_("Maybe you have a broken useragent entry in your %s file\n"),UserAgentLog); + exit(EXIT_FAILURE); + } + + if(gwarea.current[0]!='\0') { + if (getword_skip(MAXLEN,&gwarea,' ')<0 || getword(user,sizeof(user),&gwarea,'\n')<0) { + debuga(_("Maybe you have a broken record or garbage in your %s file\n"),UserAgentLog); + exit(EXIT_FAILURE); + } + if(user[0] == '-') + strcpy(user,ip); + if(user[0] == '\0') + strcpy(user,ip); + } else { + strcpy(user,ip); + } + + fprintf(fp_ou,"%s\t%s\t%s\n",ip,agent,user); + useragent_count++; + } + + if(debug) { + debuga(_(" Records read: %ld\n"),totregsl); + } + + fclose(fp_in); + if (fclose(fp_ou)==EOF) { + debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno)); + exit(EXIT_FAILURE); + } + + if(debug) { + debuga(_("Sorting file: %s\n"),tmp2); + } + + sprintf(csort,"sort -n -t \"\t\" -k 3,3 -k 2,2 -k 1,1 -o \"%s\" \"%s\"",tmp2,tmp3); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + if((fp_in=fopen(tmp2,"r"))==NULL) { + debuga(_("(useragent) Cannot open file %s\n"),tmp2); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + unlink(tmp3); + + snprintf(hfile,sizeof(hfile),"%s/useragent.html", outdirname); + if((fp_ht=fopen(hfile,"w"))==NULL) { + debuga(_("(useragent) Cannot open file %s\n"),hfile); + exit(EXIT_FAILURE); + } + + if(debug) + debuga(_("Making Useragent report\n")); + + write_html_header(fp_ht,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Squid Useragent's Report"),HTML_JS_NONE); + fprintf(fp_ht,"%s\n",_("Squid Useragent's Report")); + fprintf(fp_ht,"%s: %s - %s\n",_("Period"),idate,fdate); + close_html_header(fp_ht); + + fputs("

\n",fp_ht); + + fputs("
\n",fp_ht); + fputs("",fp_ht); + + fprintf(fp_ht,"\n",_("USERID"),_("AGENT")); + + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + getword_start(&gwarea,buf); + if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { + debuga(_("There is an invalid IP address in file %s\n"),tmp2); + exit(EXIT_FAILURE); + } + + if(Ip2Name) { + if(strcmp(ip,ipbefore) != 0) { + strcpy(ipbefore,ip); + ip2name(ip,sizeof(ip)); + strcpy(namebefore,ip); + } else strcpy(ip,namebefore); + } + + if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { + debuga(_("There is an invalid useragent in file %s\n"),tmp2); + exit(EXIT_FAILURE); + } + if (getword(user,sizeof(user),&gwarea,'\t')<0) { + debuga(_("There is an invalid user ID in file %s\n"),tmp2); + exit(EXIT_FAILURE); + } + + if(strcmp(user,user_old) != 0) { + fprintf(fp_ht,"\n",fp_ht); + strcpy(user_old,user); + strcpy(agent_old,agent); + } else if(strcmp(agent,agent_old) != 0) { + fputs("\n",fp_ht); + strcpy(agent_old,agent); + } + } + + fputs("
  
%s%s
%s",user); + output_html_string(fp_ht,agent,250); + fputs("
",fp_ht); + output_html_string(fp_ht,agent,250); + fputs("
\n",fp_ht); + fclose(fp_in); + + sprintf(csort,"sort -t \"\t\" -k 2,2 -o \"%s\" \"%s\"",tmp3,tmp2); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + if((fp_in=fopen(tmp3,"r"))==NULL) { + debuga(_("(useragent) Cannot open file %s\n"),tmp3); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + unlink(tmp2); + + if((fp_ou=fopen(tmp2,"w"))==NULL) { + debuga(_("(useragent) Cannot open file %s\n"),tmp2); + exit(EXIT_FAILURE); + } + + agent_old[0]='\0'; + cont=0; + + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + getword_start(&gwarea,buf); + if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { + debuga(_("There is an invalid IP address in file %s\n"),tmp3); + exit(EXIT_FAILURE); + } + if (getword(agent,sizeof(agent),&gwarea,'\t')<0) { + debuga(_("There is an invalid useragent in file %s\n"),tmp3); + exit(EXIT_FAILURE); + } + + if(!cont) { + cont++; + strcpy(agent_old,agent); + } + + if(strcmp(agent,agent_old) != 0) { + agentdif++; + fprintf(fp_ou,"%06d %s\n",agentot,agent_old); + strcpy(agent_old,agent); + agentot2+=agentot; + agentot=0; + } + agentot++; + } + agentdif++; + fprintf(fp_ou,"%06d %s\n",agentot,agent); + agentot2+=agentot; + + fclose(fp_in); + if (fclose(fp_ou)==EOF) { + debuga(_("Failed to close file %s - %s\n"),tmp3,strerror(errno)); + exit(EXIT_FAILURE); + } + + unlink(tmp3); + + sprintf(csort,"sort -n -r -k 1,1 -o \"%s\" \"%s\"",tmp3,tmp2); + cstatus=system(csort); + if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { + debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + if((fp_in=fopen(tmp3,"r"))==NULL) { + debuga(_("(useragent) Cannot open file %s\n"),tmp3); + debuga(_("sort command: %s\n"),csort); + exit(EXIT_FAILURE); + } + + unlink(tmp2); + + fputs("

\n",fp_ht); + + fputs("\n",fp_ht); + fprintf(fp_ht,"\n",_("AGENT"),_("TOTAL")); + + perc=0.; + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + fixendofline(buf); + getword_start(&gwarea,buf); + if (getword(tagent,sizeof(tagent),&gwarea,' ')<0) { + debuga(_("There is an invalid useragent in file %s\n"),tmp3); + exit(EXIT_FAILURE); + } + nagent=atoi(tagent); + perc=(agentot2>0) ? nagent * 100. / agentot2 : 0.; + + fputs("\n",nagent,perc); + } + fclose(fp_in); + + fputs("
%s%s%%
",fp_ht); + output_html_string(fp_ht,gwarea.current,250); + fprintf(fp_ht,"%d%3.2lf
\n",fp_ht); + if (write_html_trailer(fp_ht)<0) + debuga(_("Write error in file %s\n"),hfile); + if (fclose(fp_ht)==EOF) + debuga(_("Failed to close file %s - %s\n"),hfile,strerror(errno)); + + unlink(tmp3); + + return; } diff --git a/userinfo.c b/userinfo.c index fb664d7..4b315fa 100644 --- a/userinfo.c +++ b/userinfo.c @@ -36,12 +36,12 @@ allocations. */ struct usergroupstruct { - //! The next group of users. - struct usergroupstruct *next; - //! A group of users. - struct userinfostruct list[USERS_PER_GROUP]; - //! The number of users stored in the list. - int nusers; + //! The next group of users. + struct usergroupstruct *next; + //! A group of users. + struct userinfostruct list[USERS_PER_GROUP]; + //! The number of users stored in the list. + int nusers; }; //! The first group of users. @@ -49,102 +49,102 @@ static struct usergroupstruct *first_user_group=NULL; struct userinfostruct *userinfo_create(const char *userid) { - struct usergroupstruct *group, *last; - struct userinfostruct *user; - int i, lastuser; - int skip; - int flen; - int count, clen; - char cstr[9]; - - last=NULL; - for (group=first_user_group ; group ; group=group->next) { - if (group->nusersnext=group; - else - first_user_group=group; - } - user=group->list+group->nusers++; - - strncpy(user->id,userid,MAX_USER_LEN-1); - user->id[MAX_USER_LEN-1]='\0'; - - skip=0; - for(i=0 ; userid[i] && ifilename[i]=userid[i]; - skip=0; - } else { - if (!skip) { - user->filename[i]='_'; - skip=1; - } - } - } - user->filename[i]='\0'; - flen=i; - - count=0; - for (group=first_user_group ; group ; group=group->next) { - lastuser=(group->next) ? group->nusers : group->nusers-1; - for (i=0 ; ifilename,group->list[i].filename)==0) { - clen=sprintf(cstr,"-%04X",count++); - if (flen+clenfilename+flen,cstr); - else - strcpy(user->filename+MAX_USER_FNAME_LEN-clen,cstr); - } - } - } - - return(user); + struct usergroupstruct *group, *last; + struct userinfostruct *user; + int i, lastuser; + int skip; + int flen; + int count, clen; + char cstr[9]; + + last=NULL; + for (group=first_user_group ; group ; group=group->next) { + if (group->nusersnext=group; + else + first_user_group=group; + } + user=group->list+group->nusers++; + + strncpy(user->id,userid,MAX_USER_LEN-1); + user->id[MAX_USER_LEN-1]='\0'; + + skip=0; + for(i=0 ; userid[i] && ifilename[i]=userid[i]; + skip=0; + } else { + if (!skip) { + user->filename[i]='_'; + skip=1; + } + } + } + user->filename[i]='\0'; + flen=i; + + count=0; + for (group=first_user_group ; group ; group=group->next) { + lastuser=(group->next) ? group->nusers : group->nusers-1; + for (i=0 ; ifilename,group->list[i].filename)==0) { + clen=sprintf(cstr,"-%04X",count++); + if (flen+clenfilename+flen,cstr); + else + strcpy(user->filename+MAX_USER_FNAME_LEN-clen,cstr); + } + } + } + + return(user); } void userinfo_free(void) { - struct usergroupstruct *group, *next; + struct usergroupstruct *group, *next; - for (group=first_user_group ; group ; group=next) { - next=group->next; - free(group); - } - first_user_group=NULL; + for (group=first_user_group ; group ; group=next) { + next=group->next; + free(group); + } + first_user_group=NULL; } struct userinfostruct *userinfo_find_from_file(const char *filename) { - struct usergroupstruct *group; - int i; - - for (group=first_user_group ; group ; group=group->next) { - for (i=0 ; inusers ; i++) - if (strcmp(filename,group->list[i].filename)==0) - return(group->list+i); - } - return(NULL); + struct usergroupstruct *group; + int i; + + for (group=first_user_group ; group ; group=group->next) { + for (i=0 ; inusers ; i++) + if (strcmp(filename,group->list[i].filename)==0) + return(group->list+i); + } + return(NULL); } struct userinfostruct *userinfo_find_from_id(const char *id) { - struct usergroupstruct *group; - int i; - - for (group=first_user_group ; group ; group=group->next) { - for (i=0 ; inusers ; i++) - if (strcmp(id,group->list[i].id)==0) - return(group->list+i); - } - return(NULL); + struct usergroupstruct *group; + int i; + + for (group=first_user_group ; group ; group=group->next) { + for (i=0 ; inusers ; i++) + if (strcmp(id,group->list[i].id)==0) + return(group->list+i); + } + return(NULL); } diff --git a/usertab.c b/usertab.c index 36d84aa..2fbbe30 100644 --- a/usertab.c +++ b/usertab.c @@ -37,12 +37,12 @@ enum UserTabEnum { - //! Users matched against the ::UserTabFile file. - UTT_File, - //! Users matched agains a LDAP. - UTT_Ldap, - //! No user matching performed. - UTT_None + //! Users matched against the ::UserTabFile file. + UTT_File, + //! Users matched agains a LDAP. + UTT_Ldap, + //! No user matching performed. + UTT_None }; enum UserTabEnum which_usertab=UTT_None; @@ -55,220 +55,220 @@ static LDAP *ldap_handle=NULL; static void init_file_usertab(const char *UserTabFile) { - FILE *fp_usr; - long int nreg; - char buf[MAXLEN]; - int z1, z2; + FILE *fp_usr; + long int nreg; + char buf[MAXLEN]; + int z1, z2; - if((fp_usr=fopen(UserTabFile,"r"))==NULL) { - debuga(_("(usertab) Cannot open file %s - %s\n"),UserTabFile,strerror(errno)); - exit(EXIT_FAILURE); - } - if (fseek(fp_usr, 0, SEEK_END)==-1) { - debuga(_("Failed to move till the end of the usertab file %s: %s\n"),UserTabFile,strerror(errno)); - exit(EXIT_FAILURE); - } - nreg = ftell(fp_usr); - if (nreg<0) { - debuga(_("Cannot get the size of file %s"),UserTabFile); - exit(EXIT_FAILURE); - } - nreg += 100; - if (fseek(fp_usr, 0, SEEK_SET)==-1) { - debuga(_("Failed to rewind the usertab file %s: %s\n"),UserTabFile,strerror(errno)); - exit(EXIT_FAILURE); - } - if((userfile=(char *) malloc(nreg))==NULL){ - debuga(_("ERROR: Cannot load. Memory fault")); - exit(EXIT_FAILURE); - } - userfile[0]='\t'; - z2=1; - while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { - if (buf[0]=='#') continue; - fixendofline(buf); - z1=0; - while(buf[z1] && (unsigned char)buf[z1]>' ') { - if (z2+3>=nreg) { //need at least 3 additional bytes for the minimum string "\n\t\0" - debuga(_("The list of the users is too long in your %s file.\n"),UserTabFile); - exit(EXIT_FAILURE); - } - userfile[z2++]=buf[z1++]; - } - while(buf[z1] && (unsigned char)buf[z1]<=' ') z1++; - userfile[z2++]='\n'; - while(buf[z1] && (unsigned char)buf[z1]>=' ') { - if (z2+2>=nreg) { //need at least 2 additional bytes for "\t\0" - debuga(_("The list of the users is too long in your %s file.\n"),UserTabFile); - exit(EXIT_FAILURE); - } - userfile[z2++]=buf[z1++]; - } - while(userfile[z2-1]==' ') z2--; - userfile[z2++]='\t'; - } - userfile[z2]='\0'; - fclose(fp_usr); + if((fp_usr=fopen(UserTabFile,"r"))==NULL) { + debuga(_("(usertab) Cannot open file %s - %s\n"),UserTabFile,strerror(errno)); + exit(EXIT_FAILURE); + } + if (fseek(fp_usr, 0, SEEK_END)==-1) { + debuga(_("Failed to move till the end of the usertab file %s: %s\n"),UserTabFile,strerror(errno)); + exit(EXIT_FAILURE); + } + nreg = ftell(fp_usr); + if (nreg<0) { + debuga(_("Cannot get the size of file %s"),UserTabFile); + exit(EXIT_FAILURE); + } + nreg += 100; + if (fseek(fp_usr, 0, SEEK_SET)==-1) { + debuga(_("Failed to rewind the usertab file %s: %s\n"),UserTabFile,strerror(errno)); + exit(EXIT_FAILURE); + } + if((userfile=(char *) malloc(nreg))==NULL){ + debuga(_("ERROR: Cannot load. Memory fault")); + exit(EXIT_FAILURE); + } + userfile[0]='\t'; + z2=1; + while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { + if (buf[0]=='#') continue; + fixendofline(buf); + z1=0; + while(buf[z1] && (unsigned char)buf[z1]>' ') { + if (z2+3>=nreg) { //need at least 3 additional bytes for the minimum string "\n\t\0" + debuga(_("The list of the users is too long in your %s file.\n"),UserTabFile); + exit(EXIT_FAILURE); + } + userfile[z2++]=buf[z1++]; + } + while(buf[z1] && (unsigned char)buf[z1]<=' ') z1++; + userfile[z2++]='\n'; + while(buf[z1] && (unsigned char)buf[z1]>=' ') { + if (z2+2>=nreg) { //need at least 2 additional bytes for "\t\0" + debuga(_("The list of the users is too long in your %s file.\n"),UserTabFile); + exit(EXIT_FAILURE); + } + userfile[z2++]=buf[z1++]; + } + while(userfile[z2-1]==' ') z2--; + userfile[z2++]='\t'; + } + userfile[z2]='\0'; + fclose(fp_usr); } static void get_usertab_name(const char *user,char *name,int namelen) { - char warea[MAXLEN]; - char *str; + char warea[MAXLEN]; + char *str; - namelen--; - sprintf(warea,"\t%s\n",user); - if((str=(char *) strstr(userfile,warea)) == (char *) NULL ) { - strncpy(name,user,namelen); - name[namelen]=0; - } else { - str=strchr(str+1,'\n'); - str++; - for(z1=0; *str != '\t' && z1=sizeof(searchloginname)-1) break; - i+=sprintf(searchloginname+i,"\\%02X",*userlogin); - } else { - searchloginname[i++]=*userlogin; - } - } - searchloginname[i]='\0'; + for (i=0 ; i=sizeof(searchloginname)-1) break; + i+=sprintf(searchloginname+i,"\\%02X",*userlogin); + } else { + searchloginname[i++]=*userlogin; + } + } + searchloginname[i]='\0'; - if (!(searched_in_cache = search_in_cache(searchloginname))) { - snprintf(filtersearch, sizeof(filtersearch), LDAPFilterSearch, searchloginname, searchloginname, searchloginname, searchloginname, searchloginname); + if (!(searched_in_cache = search_in_cache(searchloginname))) { + snprintf(filtersearch, sizeof(filtersearch), LDAPFilterSearch, searchloginname, searchloginname, searchloginname, searchloginname, searchloginname); - /* Search record(s) in LDAP base */ + /* Search record(s) in LDAP base */ - int rc= ldap_search_s(ldap_handle, LDAPBaseSearch, LDAP_SCOPE_SUBTREE, filtersearch, NULL, 0, &result); - if ( rc != LDAP_SUCCESS ) { - debuga(_("LDAP search failed: %s\n"), ldap_err2string(rc)); - strncpy(mappedname,searchloginname,namelen-1); - mappedname[namelen-1]='\0'; - return; - } + int rc= ldap_search_s(ldap_handle, LDAPBaseSearch, LDAP_SCOPE_SUBTREE, filtersearch, NULL, 0, &result); + if ( rc != LDAP_SUCCESS ) { + debuga(_("LDAP search failed: %s\n"), ldap_err2string(rc)); + strncpy(mappedname,searchloginname,namelen-1); + mappedname[namelen-1]='\0'; + return; + } - if (!(e = ldap_first_entry(ldap_handle, result))) - insert_to_cache(searchloginname, searchloginname); - else - for (attr = ldap_first_attribute(ldap_handle, e, &ber); attr != NULL; attr = ldap_next_attribute(ldap_handle, e, ber)) { - if (!strcasecmp(attr, LDAPTargetAttr)) { - if ((vals = (char **)ldap_get_values(ldap_handle, e, attr))!=NULL) { - insert_to_cache(searchloginname, vals[0]); - strncpy(mappedname, vals[0],namelen-1); - mappedname[namelen-1]='\0'; - ldap_memfree(vals); - } - ldap_memfree(attr); - break; - } - ldap_memfree(attr); - } - ldap_msgfree(result); - } else { - strncpy(mappedname, searched_in_cache,namelen-1); - mappedname[namelen-1]='\0'; - } + if (!(e = ldap_first_entry(ldap_handle, result))) + insert_to_cache(searchloginname, searchloginname); + else + for (attr = ldap_first_attribute(ldap_handle, e, &ber); attr != NULL; attr = ldap_next_attribute(ldap_handle, e, ber)) { + if (!strcasecmp(attr, LDAPTargetAttr)) { + if ((vals = (char **)ldap_get_values(ldap_handle, e, attr))!=NULL) { + insert_to_cache(searchloginname, vals[0]); + strncpy(mappedname, vals[0],namelen-1); + mappedname[namelen-1]='\0'; + ldap_memfree(vals); + } + ldap_memfree(attr); + break; + } + ldap_memfree(attr); + } + ldap_msgfree(result); + } else { + strncpy(mappedname, searched_in_cache,namelen-1); + mappedname[namelen-1]='\0'; + } } #endif //HAVE_LDAP_H void init_usertab(const char *UserTabFile) { - if (strcmp(UserTabFile, "ldap") == 0) { - if(debug) - debuga(_("Loading User table: %s\n"),UserTabFile); + if (strcmp(UserTabFile, "ldap") == 0) { + if(debug) + debuga(_("Loading User table: %s\n"),UserTabFile); #ifdef HAVE_LDAP_H - which_usertab=UTT_Ldap; - init_ldap_usertab(); + which_usertab=UTT_Ldap; + init_ldap_usertab(); #else - debuga(_("LDAP module not compiled in sarg\n")); - exit(EXIT_FAILURE); + debuga(_("LDAP module not compiled in sarg\n")); + exit(EXIT_FAILURE); #endif //HAVE_LDAP_H - } else if (UserTabFile[0] != '\0') { - if(debug) - debuga(_("Loading User table: %s\n"),UserTabFile); - which_usertab=UTT_File; - init_file_usertab(UserTabFile); - } else { - which_usertab=UTT_None; - } + } else if (UserTabFile[0] != '\0') { + if(debug) + debuga(_("Loading User table: %s\n"),UserTabFile); + which_usertab=UTT_File; + init_file_usertab(UserTabFile); + } else { + which_usertab=UTT_None; + } } void user_find(char *mappedname, int namelen, const char *userlogin) { - if (which_usertab==UTT_File) { - get_usertab_name(userlogin,mappedname,namelen); - } + if (which_usertab==UTT_File) { + get_usertab_name(userlogin,mappedname,namelen); + } #ifdef HAVE_LDAP_H - else if (which_usertab==UTT_Ldap) { - get_ldap_name(userlogin,mappedname,namelen); - } + else if (which_usertab==UTT_Ldap) { + get_ldap_name(userlogin,mappedname,namelen); + } #endif //HAVE_LDAP_H - else { - strncpy(mappedname,userlogin,namelen-1); - mappedname[namelen-1]='\0'; - } + else { + strncpy(mappedname,userlogin,namelen-1); + mappedname[namelen-1]='\0'; + } } void close_usertab(void) { #ifdef HAVE_LDAP_H - if (ldap_handle) { - destroy_cache(); - ldap_unbind(ldap_handle); - ldap_handle=NULL; - } + if (ldap_handle) { + destroy_cache(); + ldap_unbind(ldap_handle); + ldap_handle=NULL; + } #endif //HAVE_LDAP_H - if(userfile) { - free(userfile); - userfile=NULL; - } + if(userfile) { + free(userfile); + userfile=NULL; + } } diff --git a/util.c b/util.c index 84c85db..35f0818 100644 --- a/util.c +++ b/util.c @@ -43,1250 +43,1242 @@ static char *excludecode=NULL; /*void fgetword(char *word, char *line, int stop) { - //VARIANT N1 - int x; + //VARIANT N1 + int x; - for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x]; - word[x] = '\0'; + for (x=0; line[x] && (line[x] != stop); x++) word[x] = line[x]; + word[x] = '\0'; - //VARIANT N2 - char *tchar; - int difflen; + //VARIANT N2 + char *tchar; + int difflen; - tchar = strchr(line, stop); - if (tchar == NULL) strcpy(word, line); - else - { + tchar = strchr(line, stop); + if (tchar == NULL) strcpy(word, line); + else + { difflen = tchar - line; strncpy(word, line, difflen); word[difflen] = '\0'; - } + } }*/ #if USE_GETWORD_BACKTRACE static void getword_backtrace(void) { - void *buffer[5]; - int i, n; - char **calls; - - n=backtrace(buffer,sizeof(buffer)/sizeof(buffer[0])); - if (n<=0) return; - calls=backtrace_symbols(buffer,n); - if (calls) { - debuga(_("getword backtrace:\n")); - for (i=0 ; ibeginning=line; - gwarea->current=line; - gwarea->modified=0; + gwarea->beginning=line; + gwarea->current=line; + gwarea->modified=0; } void getword_restart(struct getwordstruct *gwarea) { - if (gwarea->modified) { - debuga(_("Cannot parse again the line as it was modified\n")); - exit(EXIT_FAILURE); - } - gwarea->current=gwarea->beginning; + if (gwarea->modified) { + debuga(_("Cannot parse again the line as it was modified\n")); + exit(EXIT_FAILURE); + } + gwarea->current=gwarea->beginning; } int getword(char *word, int limit, struct getwordstruct *gwarea, char stop) { - int x; + int x; - for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) { - if(x>=limit) { - printf("SARG: getword loop detected after %d bytes.\n",x); - printf("SARG: Line=\"%s\"\n",gwarea->beginning); - printf("SARG: Record=\"%s\"\n",gwarea->current); - printf("SARG: searching for \'x%x\'\n",stop); - //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); - word[(limit>0) ? limit-1 : 0]='\0'; + for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) { + if(x>=limit) { + printf("SARG: getword loop detected after %d bytes.\n",x); + printf("SARG: Line=\"%s\"\n",gwarea->beginning); + printf("SARG: Record=\"%s\"\n",gwarea->current); + printf("SARG: searching for \'x%x\'\n",stop); + //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); + word[(limit>0) ? limit-1 : 0]='\0'; #if USE_GETWORD_BACKTRACE - getword_backtrace(); + getword_backtrace(); #endif - return(-1); - } - word[x] = gwarea->current[x]; - } + return(-1); + } + word[x] = gwarea->current[x]; + } - word[x] = '\0'; - if (gwarea->current[x]) ++x; - gwarea->current+=x; - return(0); + word[x] = '\0'; + if (gwarea->current[x]) ++x; + gwarea->current+=x; + return(0); } int getword_limit(char *word, int limit, struct getwordstruct *gwarea, char stop) { - int x; + int x; - limit--; - for(x=0; xcurrent[x] && gwarea->current[x] != stop ;x++) { - word[x] = gwarea->current[x]; - } - word[x] = '\0'; - gwarea->current+=x; - while (*gwarea->current && *gwarea->current != stop) gwarea->current++; - if (*gwarea->current) ++gwarea->current; - return(0); + limit--; + for(x=0; xcurrent[x] && gwarea->current[x] != stop ;x++) { + word[x] = gwarea->current[x]; + } + word[x] = '\0'; + gwarea->current+=x; + while (*gwarea->current && *gwarea->current != stop) gwarea->current++; + if (*gwarea->current) ++gwarea->current; + return(0); } int getword_multisep(char *word, int limit, struct getwordstruct *gwarea, char stop) { - int x; + int x; - for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) { - if(x>=limit) { - printf("SARG: getword_multisep loop detected.\n"); - printf("SARG: Line=\"%s\"\n",gwarea->beginning); - printf("SARG: Record=\"%s\"\n",gwarea->current); - printf("SARG: searching for \'x%x\'\n",stop); - //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); - if (limit>0) word[limit-1]='\0'; + for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++) { + if(x>=limit) { + printf("SARG: getword_multisep loop detected.\n"); + printf("SARG: Line=\"%s\"\n",gwarea->beginning); + printf("SARG: Record=\"%s\"\n",gwarea->current); + printf("SARG: searching for \'x%x\'\n",stop); + //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); + if (limit>0) word[limit-1]='\0'; #if USE_GETWORD_BACKTRACE - getword_backtrace(); + getword_backtrace(); #endif - //exit(EXIT_FAILURE); - return(-1); - } - word[x] = gwarea->current[x]; - } + //exit(EXIT_FAILURE); + return(-1); + } + word[x] = gwarea->current[x]; + } - word[x] = '\0'; - while (gwarea->current[x] && gwarea->current[x]==stop) ++x; - gwarea->current+=x; - return(0); + word[x] = '\0'; + while (gwarea->current[x] && gwarea->current[x]==stop) ++x; + gwarea->current+=x; + return(0); } int getword_skip(int limit, struct getwordstruct *gwarea, char stop) { - int x; + int x; - for(x=0;(gwarea->current[x] && (gwarea->current[x] != stop ));x++) { - if(x>=limit) { - printf("SARG: getword_skip loop detected after %d bytes.\n",x); - printf("SARG: Line=\"%s\"\n",gwarea->beginning); - printf("SARG: Record=\"%s\"\n",gwarea->current); - printf("SARG: searching for \'x%x\'\n",stop); - //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); + for(x=0;(gwarea->current[x] && (gwarea->current[x] != stop ));x++) { + if(x>=limit) { + printf("SARG: getword_skip loop detected after %d bytes.\n",x); + printf("SARG: Line=\"%s\"\n",gwarea->beginning); + printf("SARG: Record=\"%s\"\n",gwarea->current); + printf("SARG: searching for \'x%x\'\n",stop); + //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); #if USE_GETWORD_BACKTRACE - getword_backtrace(); + getword_backtrace(); #endif - return(-1); - } - } + return(-1); + } + } - if (gwarea->current[x]) ++x; - gwarea->current+=x; - return(0); + if (gwarea->current[x]) ++x; + gwarea->current+=x; + return(0); } int getword_atoll(long long int *number, struct getwordstruct *gwarea, char stop) { - int x; - int sign=+1; - - if (gwarea->current[0] == '-') { - gwarea->current++; - sign=-1; - } else if (gwarea->current[0] == '+') { - gwarea->current++; - } - *number=0LL; - for(x=0;isdigit(gwarea->current[x]);x++) { - *number=(*number * 10) + gwarea->current[x]-'0'; - } - if(gwarea->current[x] && gwarea->current[x]!=stop) { - printf("SARG: getword_atoll loop detected after %d bytes.\n",x); - printf("SARG: Line=\"%s\"\n",gwarea->beginning); - printf("SARG: Record=\"%s\"\n",gwarea->current); - printf("SARG: searching for \'x%x\'\n",stop); - //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); + int x; + int sign=+1; + + if (gwarea->current[0] == '-') { + gwarea->current++; + sign=-1; + } else if (gwarea->current[0] == '+') { + gwarea->current++; + } + *number=0LL; + for(x=0;isdigit(gwarea->current[x]);x++) { + *number=(*number * 10) + gwarea->current[x]-'0'; + } + if(gwarea->current[x] && gwarea->current[x]!=stop) { + printf("SARG: getword_atoll loop detected after %d bytes.\n",x); + printf("SARG: Line=\"%s\"\n",gwarea->beginning); + printf("SARG: Record=\"%s\"\n",gwarea->current); + printf("SARG: searching for \'x%x\'\n",stop); + //printf("SARG: Maybe you have a broken record or garbage in your access.log file.\n"); #if USE_GETWORD_BACKTRACE - getword_backtrace(); + getword_backtrace(); #endif - return(-1); - } - *number*=sign; + return(-1); + } + *number*=sign; - if (gwarea->current[x]) ++x; - gwarea->current+=x; - return(0); + if (gwarea->current[x]) ++x; + gwarea->current+=x; + return(0); } int getword_ptr(char *orig_line,char **word, struct getwordstruct *gwarea, char stop) { - /*! - \note Why pass the original buffer to the function ? Because we must modify it to - insert the terminating ASCII zero for the word we return and that's not compatible - with getword_restart(). Moreover, getword_start() sometime works on constant strings - so this function require the original buffer to detect any missuse. - */ - int x; - int sep; - int start; - - if (orig_line && orig_line!=gwarea->beginning) { - debuga(_("Invalid buffer passed to getword_ptr\n")); - return(-1); - } - - start=(gwarea->current-gwarea->beginning); - if (word && orig_line) *word=orig_line+start; - for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++); - sep=(gwarea->current[x]!='\0'); - if (word && orig_line) orig_line[start+x] = '\0'; - if (sep) ++x; - gwarea->current+=x; - gwarea->modified=1; - return(0); + /*! + \note Why pass the original buffer to the function ? Because we must modify it to + insert the terminating ASCII zero for the word we return and that's not compatible + with getword_restart(). Moreover, getword_start() sometime works on constant strings + so this function require the original buffer to detect any missuse. + */ + int x; + int sep; + int start; + + if (orig_line && orig_line!=gwarea->beginning) { + debuga(_("Invalid buffer passed to getword_ptr\n")); + return(-1); + } + + start=(gwarea->current-gwarea->beginning); + if (word && orig_line) *word=orig_line+start; + for(x=0;((gwarea->current[x]) && (gwarea->current[x] != stop ));x++); + sep=(gwarea->current[x]!='\0'); + if (word && orig_line) orig_line[start+x] = '\0'; + if (sep) ++x; + gwarea->current+=x; + gwarea->modified=1; + return(0); } #define MAXLLL 30 //!< Maximum number of digits in long long (a guess). long long int my_atoll (const char *nptr) { - long long int returnval=0LL; - int max_digits = MAXLLL ; + long long int returnval=0LL; + int max_digits = MAXLLL ; - // Soak up all the white space - while (isspace( *nptr )) { - nptr++; - } + // Soak up all the white space + while (isspace( *nptr )) { + nptr++; + } - //For each character left to right - //change the character to a single digit - //multiply what we had before by 10 and add the new digit + //For each character left to right + //change the character to a single digit + //multiply what we had before by 10 and add the new digit - while (--max_digits && isdigit( *nptr )) - { - returnval = ( returnval * 10 ) + ( *nptr++ - '0' ) ; - } + while (--max_digits && isdigit( *nptr )) + { + returnval = ( returnval * 10 ) + ( *nptr++ - '0' ) ; + } - return returnval; + return returnval; } int is_absolute(const char *path) { - if (*path=='/') return(1); + if (*path=='/') return(1); #ifdef WINDOWS - if (isalpha(path[0]) && path[1]==':') return(1); + if (isalpha(path[0]) && path[1]==':') return(1); #endif - return(0); + return(0); } void my_mkdir(const char *name) { - char w0[MAXLEN]; - int i; - int chars; - - if(!is_absolute(name)) { - debuga(_("Invalid path (%s). Please, use absolute paths only.\n"),name); - debuga(_("process aborted.\n")); - exit(EXIT_FAILURE); - } - - chars=0; - for (i=0 ; name[i] ; i++) { - if (i>=sizeof(w0)) { - debuga(_("directory name too long: %s\n"),name); - exit(EXIT_FAILURE); - } - if (chars>0 && name[i] == '/') { - w0[i] = '\0'; - if(access(w0, R_OK) != 0) { - if(mkdir(w0,0755)) { - debuga(_("mkdir %s %s\n"),w0,strerror(errno)); - debuga(_("process aborted.\n")); - exit(EXIT_FAILURE); - } - } - } - if (name[i] != '/') chars++; - w0[i] = name[i]; - } - - if(access(name, R_OK) != 0) { - if(mkdir(name,0755)) { - debuga(_("mkdir %s %s\n"),name,strerror(errno)); - debuga(_("process aborted.\n")); - exit(EXIT_FAILURE); - } - } + char w0[MAXLEN]; + int i; + int chars; + + if(!is_absolute(name)) { + debuga(_("Invalid path (%s). Please, use absolute paths only.\n"),name); + debuga(_("process aborted.\n")); + exit(EXIT_FAILURE); + } + + chars=0; + for (i=0 ; name[i] ; i++) { + if (i>=sizeof(w0)) { + debuga(_("directory name too long: %s\n"),name); + exit(EXIT_FAILURE); + } + if (chars>0 && name[i] == '/') { + w0[i] = '\0'; + if(access(w0, R_OK) != 0) { + if(mkdir(w0,0755)) { + debuga(_("mkdir %s %s\n"),w0,strerror(errno)); + debuga(_("process aborted.\n")); + exit(EXIT_FAILURE); + } + } + } + if (name[i] != '/') chars++; + w0[i] = name[i]; + } + + if(access(name, R_OK) != 0) { + if(mkdir(name,0755)) { + debuga(_("mkdir %s %s\n"),name,strerror(errno)); + debuga(_("process aborted.\n")); + exit(EXIT_FAILURE); + } + } } void my_lltoa(unsigned long long int n, char *s, int ssize, int len) { - int i; - int slen = 0; - int j; - char c; - - ssize--; - if (len>ssize) { - debuga(_("The requested number of digits passed to my_lltoa (%d) is bigger than the output buffer size (%d)\n"),len,ssize); - abort(); - } - - do { - s[slen++] = (n % 10) + '0'; - } while ((n /= 10) > 0 && slenslen) { - i=len-slen; - for(j=slen; j>=0; j--) - s[j+i]=s[j]; - for(j=0 ; jssize) { + debuga(_("The requested number of digits passed to my_lltoa (%d) is bigger than the output buffer size (%d)\n"),len,ssize); + abort(); + } + + do { + s[slen++] = (n % 10) + '0'; + } while ((n /= 10) > 0 && slenslen) { + i=len-slen; + for(j=slen; j>=0; j--) + s[j+i]=s[j]; + for(j=0 ; j12) { - snprintf(str,sizeof(str),"%03d",month); - return(str); - } - return(mtab1[month-1]); + if (month<1 || month>12) { + snprintf(str,sizeof(str),"%03d",month); + return(str); + } + return(mtab1[month-1]); } void name_month(char *month,int month_len) { - int x, z=atoi(month)-1; - char m[255]; - char w[20]; - struct getwordstruct gwarea; + int x, z=atoi(month)-1; + char m[255]; + char w[20]; + struct getwordstruct gwarea; - strcpy(m,_("January,February,March,April,May,June,July,August,September,October,November,December")); - getword_start(&gwarea,m); + strcpy(m,_("January,February,March,April,May,June,July,August,September,October,November,December")); + getword_start(&gwarea,m); - for(x=0; x -1; i--) { - if ( k == 2 && i != 0 ) { - k = 0; - pbuf[j++] = num[i]; - pbuf[j++] = (UseComma) ? ',' : '.'; - continue; - } - pbuf[j] = num[i]; - j++; - k++; - } - - pret[0]='\0'; - - for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++) - pret[j] = pbuf[i]; - - pret[j] = '\0'; - - return pret; + char num[MAXLEN_FIXNUM]=""; + char buf[MAXLEN_FIXNUM * 2]; + char *pbuf; + static char ret[MAXLEN_FIXNUM * 2]; + char *pret; + register int i, j, k; + int numlen; + static char abbrev[30]; + + my_lltoa(value, num, sizeof(num), 0); + + if(DisplayedValues==DISPLAY_ABBREV) { + numlen = strlen(num); + if(numlen <= 3) + sprintf(abbrev,"%s",num); + if(numlen == 4 || numlen == 7 || numlen == 10 || numlen == 13) { + snprintf(abbrev,2,"%s",num); + strncat(abbrev,".",1); + strncat(abbrev,num+1,2); + if(!n) return(abbrev); + if(numlen == 4) + strncat(abbrev,"K",1); + else if(numlen == 7) + strncat(abbrev,"M",1); + else if(numlen == 10) + strncat(abbrev,"G",1); + else if(numlen == 13) + strncat(abbrev,"T",1); + } + if(numlen == 5 || numlen == 8 || numlen == 11 || numlen == 14) { + snprintf(abbrev,3,"%s",num); + strncat(abbrev,".",1); + strncat(abbrev,num+2,2); + if(!n) return(abbrev); + if(numlen == 5) + strncat(abbrev,"K",1); + else if(numlen == 8) + strncat(abbrev,"M",1); + else if(numlen == 11) + strncat(abbrev,"G",1); + else if(numlen == 14) + strncat(abbrev,"T",1); + } + if(numlen == 6 || numlen == 9 || numlen == 12 || numlen == 15) { + snprintf(abbrev,4,"%s",num); + strncat(abbrev,".",1); + strncat(abbrev,num+3,2); + if(!n) return(abbrev); + if(numlen == 6) + strncat(abbrev,"K",1); + else if(numlen == 9) + strncat(abbrev,"M",1); + else if(numlen == 12) + strncat(abbrev,"G",1); + else if(numlen == 15) + strncat(abbrev,"T",1); + } + + return(abbrev); + } + + bzero(buf, MAXLEN_FIXNUM*2); + + pbuf = buf; + pret = ret; + k = 0; + + for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) { + if ( k == 2 && i != 0 ) { + k = 0; + pbuf[j++] = num[i]; + pbuf[j++] = (UseComma) ? ',' : '.'; + continue; + } + pbuf[j] = num[i]; + j++; + k++; + } + + pret[0]='\0'; + + for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++) + pret[j] = pbuf[i]; + + pret[j] = '\0'; + + return pret; } char *fixnum2(long long int value, int n) { #define MAXLEN_FIXNUM2 1024 - char num[MAXLEN_FIXNUM2]; - char buf[MAXLEN_FIXNUM2 * 2]; - char *pbuf; - static char ret[MAXLEN_FIXNUM2 * 2]; - char *pret; - register int i, j, k; + char num[MAXLEN_FIXNUM2]; + char buf[MAXLEN_FIXNUM2 * 2]; + char *pbuf; + static char ret[MAXLEN_FIXNUM2 * 2]; + char *pret; + register int i, j, k; - my_lltoa(value, num, sizeof(num), 0); - bzero(buf, MAXLEN_FIXNUM2*2); + my_lltoa(value, num, sizeof(num), 0); + bzero(buf, MAXLEN_FIXNUM2*2); - pbuf = buf; - pret = ret; - k = 0; + pbuf = buf; + pret = ret; + k = 0; - for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) { - if ( k == 2 && i != 0 ) { - k = 0; - pbuf[j++] = num[i]; - pbuf[j++] = (UseComma) ? ',' : '.'; - continue; - } - pbuf[j] = num[i]; - j++; - k++; - } + for ( i = strlen(num) - 1, j = 0 ; i > -1; i--) { + if ( k == 2 && i != 0 ) { + k = 0; + pbuf[j++] = num[i]; + pbuf[j++] = (UseComma) ? ',' : '.'; + continue; + } + pbuf[j] = num[i]; + j++; + k++; + } - pret[0]='\0'; + pret[0]='\0'; - for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++) - pret[j] = pbuf[i]; + for ( i = strlen(pbuf) - 1, j = 0 ; i > -1; i--, j++) + pret[j] = pbuf[i]; - pret[j] = '\0'; + pret[j] = '\0'; - return pret; + return pret; } char *buildtime(long long int elap) { + int num = elap / 1000; + int hor = 0; + int min = 0; + int sec = 0; + static char buf[12]; - int num = elap / 1000; - int hor = 0; - int min = 0; - int sec = 0; - static char buf[12]; - - buf[0]='\0'; + buf[0]='\0'; - hor=num / 3600; - min=(num % 3600) / 60; - sec=num % 60; - sprintf(buf,"%02d:%02d:%02d",hor,min,sec); - - return(buf); + hor=num / 3600; + min=(num % 3600) / 60; + sec=num % 60; + sprintf(buf,"%02d:%02d:%02d",hor,min,sec); + return(buf); } void obtdate(const char *dirname, const char *name, char *data) { + FILE *fp_in; + char wdir[MAXLEN]; - FILE *fp_in; - char wdir[MAXLEN]; - - sprintf(wdir,"%s%s/sarg-date",dirname,name); - if ((fp_in = fopen(wdir, "rt")) == 0) { - sprintf(wdir,"%s%s/date",dirname,name); - if ((fp_in = fopen(wdir, "rt")) == 0) { - data[0]='\0'; - return; - } - } + sprintf(wdir,"%s%s/sarg-date",dirname,name); + if ((fp_in = fopen(wdir, "rt")) == 0) { + sprintf(wdir,"%s%s/date",dirname,name); + if ((fp_in = fopen(wdir, "rt")) == 0) { + data[0]='\0'; + return; + } + } - if (!fgets(data,80,fp_in)) { - debuga(_("Failed to read the date in %s\n"),wdir); - exit(EXIT_FAILURE); - } - fclose(fp_in); - fixendofline(data); - - return; + if (!fgets(data,80,fp_in)) { + debuga(_("Failed to read the date in %s\n"),wdir); + exit(EXIT_FAILURE); + } + fclose(fp_in); + fixendofline(data); + return; } void formatdate(char *date,int date_size,int year,int month,int day,int hour,int minute,int second,int dst) { - struct tm ltm; - time_t unixtime; - struct tm *fulltm; + struct tm ltm; + time_t unixtime; + struct tm *fulltm; - memset(<m,0,sizeof(ltm)); - if (year>=1900) ltm.tm_year=year-1900; - if (month>=1 && month<=12) ltm.tm_mon=month-1; - if (day>=1 && day<=31) ltm.tm_mday=day; - if (hour>=0 && hour<24) ltm.tm_hour=hour; - if (minute>=0 && minute<60) ltm.tm_min=minute; - if (second>=0 && second<60) ltm.tm_sec=second; - ltm.tm_isdst=dst; - unixtime=mktime(<m); //fill the missing entries - fulltm=localtime(&unixtime); - //strftime(date,date_size,"%a %b %d %H:%M:%S %Z %Y",fulltm); - strftime(date,date_size,"%c",fulltm); + memset(<m,0,sizeof(ltm)); + if (year>=1900) ltm.tm_year=year-1900; + if (month>=1 && month<=12) ltm.tm_mon=month-1; + if (day>=1 && day<=31) ltm.tm_mday=day; + if (hour>=0 && hour<24) ltm.tm_hour=hour; + if (minute>=0 && minute<60) ltm.tm_min=minute; + if (second>=0 && second<60) ltm.tm_sec=second; + ltm.tm_isdst=dst; + unixtime=mktime(<m); //fill the missing entries + fulltm=localtime(&unixtime); + //strftime(date,date_size,"%a %b %d %H:%M:%S %Z %Y",fulltm); + strftime(date,date_size,"%c",fulltm); } void computedate(int year,int month,int day,struct tm *t) { - memset(t,0,sizeof(*t)); - t->tm_year=year-1900; - t->tm_mon=month-1; - t->tm_mday=day; + memset(t,0,sizeof(*t)); + t->tm_year=year-1900; + t->tm_mon=month-1; + t->tm_mday=day; } int obtuser(const char *dirname, const char *name) { + FILE *fp_in; + char wdir[MAXLEN]; + char tuser[20]; + int nuser; - FILE *fp_in; - char wdir[MAXLEN]; - char tuser[20]; - int nuser; - - sprintf(wdir,"%s%s/sarg-users",dirname,name); - if((fp_in=fopen(wdir,"r"))==NULL) { - sprintf(wdir,"%s%s/users",dirname,name); - if((fp_in=fopen(wdir,"r"))==NULL) { - return(0); - } - } + sprintf(wdir,"%s%s/sarg-users",dirname,name); + if((fp_in=fopen(wdir,"r"))==NULL) { + sprintf(wdir,"%s%s/users",dirname,name); + if((fp_in=fopen(wdir,"r"))==NULL) { + return(0); + } + } - if (!fgets(tuser,sizeof(tuser),fp_in)) { - debuga(_("Failed to read the number of users in %s\n"),wdir); - exit(EXIT_FAILURE); - } - fclose(fp_in); - nuser=atoi(tuser); + if (!fgets(tuser,sizeof(tuser),fp_in)) { + debuga(_("Failed to read the number of users in %s\n"),wdir); + exit(EXIT_FAILURE); + } + fclose(fp_in); + nuser=atoi(tuser); - return(nuser); + return(nuser); } void obttotal(const char *dirname, const char *name, int nuser, long long int *tbytes, long long int *media) { - FILE *fp_in; - char *buf; - char wdir[MAXLEN]; - char user[MAX_USER_LEN]; - char sep; - struct getwordstruct gwarea; - longline line; - - *tbytes=0; - *media=0; - - sprintf(wdir,"%s%s/sarg-general",dirname,name); - if ((fp_in = fopen(wdir, "r")) == 0) { - sprintf(wdir,"%s%s/general",dirname,name); - if ((fp_in = fopen(wdir, "r")) == 0) { - return; - } - } - - if ((line=longline_create())==NULL) { - debuga(_("Not enough memory to read the file %s\n"),wdir); - exit(EXIT_FAILURE); - } - - while((buf=longline_read(fp_in,line))!=NULL) { - if (strncmp(buf,"TOTAL\t",6) == 0) - sep='\t'; //new file - else if (strncmp(buf,"TOTAL ",6) == 0) - sep=' '; //old file - else - continue; - getword_start(&gwarea,buf); - if (getword(user,sizeof(user),&gwarea,sep)<0) { - debuga(_("There is a invalid user in file %s\n"),wdir); - exit(EXIT_FAILURE); - } - if(strcmp(user,"TOTAL") != 0) - continue; - if (getword_skip(MAXLEN,&gwarea,sep)<0) { - debuga(_("There a broken total number of access in file %s\n"),wdir); - exit(EXIT_FAILURE); - } - if (getword_atoll(tbytes,&gwarea,sep)<0) { - debuga(_("There is a broken number of bytes in file %s\n"),wdir); - exit(EXIT_FAILURE); - } - break; - } - fclose(fp_in); - longline_destroy(&line); - - if (nuser <= 0) - return; - - *media=*tbytes / nuser; - return; + FILE *fp_in; + char *buf; + char wdir[MAXLEN]; + char user[MAX_USER_LEN]; + char sep; + struct getwordstruct gwarea; + longline line; + + *tbytes=0; + *media=0; + + sprintf(wdir,"%s%s/sarg-general",dirname,name); + if ((fp_in = fopen(wdir, "r")) == 0) { + sprintf(wdir,"%s%s/general",dirname,name); + if ((fp_in = fopen(wdir, "r")) == 0) { + return; + } + } + + if ((line=longline_create())==NULL) { + debuga(_("Not enough memory to read the file %s\n"),wdir); + exit(EXIT_FAILURE); + } + + while((buf=longline_read(fp_in,line))!=NULL) { + if (strncmp(buf,"TOTAL\t",6) == 0) + sep='\t'; //new file + else if (strncmp(buf,"TOTAL ",6) == 0) + sep=' '; //old file + else + continue; + getword_start(&gwarea,buf); + if (getword(user,sizeof(user),&gwarea,sep)<0) { + debuga(_("There is a invalid user in file %s\n"),wdir); + exit(EXIT_FAILURE); + } + if(strcmp(user,"TOTAL") != 0) + continue; + if (getword_skip(MAXLEN,&gwarea,sep)<0) { + debuga(_("There a broken total number of access in file %s\n"),wdir); + exit(EXIT_FAILURE); + } + if (getword_atoll(tbytes,&gwarea,sep)<0) { + debuga(_("There is a broken number of bytes in file %s\n"),wdir); + exit(EXIT_FAILURE); + } + break; + } + fclose(fp_in); + longline_destroy(&line); + + if (nuser <= 0) + return; + + *media=*tbytes / nuser; + return; } int getperiod_fromsarglog(const char *arqtt,struct periodstruct *period) { - const char *str; - int day0, month0, year0, hour0, minute0; - int day1, month1, year1, hour1, minute1; - char month[4]; - int i; - - memset(period,0,sizeof(*period)); - - str=arqtt; - while((str=strstr(str,"sarg-"))!=NULL) { - str+=5; - if (!isdigit(str[0]) || !isdigit(str[1])) continue; - day0=(str[0]-'0')*10+(str[1]-'0'); - str+=2; - strncpy(month,str,3); - month[3]=0; - month0=month2num(month); - if (month0>=12) continue; - str+=3; - year0=0; - for (i=0 ; isdigit(str[i]) && i<4 ; i++) year0=year0*10+(str[i]-'0'); - if (i!=4) continue; - str+=4; - if (str[0]!='_') continue; - str++; - - if (!isdigit(str[0]) || !isdigit(str[1])) continue; - hour0=(str[0]-'0')*10+(str[1]-'0'); - str+=2; - if (!isdigit(str[0]) || !isdigit(str[1])) continue; - minute0=(str[0]-'0')*10+(str[1]-'0'); - str+=2; - - if (*str != '-') continue; - str++; - - if (!isdigit(str[0]) || !isdigit(str[1])) continue; - day1=(str[0]-'0')*10+(str[1]-'0'); - str+=2; - strncpy(month,str,3); - month[3]=0; - month1=month2num(month); - if (month1>=12) continue; - str+=3; - year1=0; - for (i=0 ; isdigit(str[i]) && i<4 ; i++) year1=year1*10+(str[i]-'0'); - if (i!=4) continue; - str+=4; - - if (str[0]!='_') continue; - str++; - - if (!isdigit(str[0]) || !isdigit(str[1])) continue; - hour1=(str[0]-'0')*10+(str[1]-'0'); - str+=2; - if (!isdigit(str[0]) || !isdigit(str[1])) continue; - minute1=(str[0]-'0')*10+(str[1]-'0'); - str+=2; - - period->start.tm_mday=day0; - period->start.tm_mon=month0; - period->start.tm_year=year0-1900; - period->start.tm_hour=hour0; - period->start.tm_min=minute0; - period->end.tm_mday=day1; - period->end.tm_mon=month1; - period->end.tm_year=year1-1900; - period->end.tm_hour=hour1; - period->end.tm_min=minute1; - return(0); - } - return(-1); + const char *str; + int day0, month0, year0, hour0, minute0; + int day1, month1, year1, hour1, minute1; + char month[4]; + int i; + + memset(period,0,sizeof(*period)); + + str=arqtt; + while((str=strstr(str,"sarg-"))!=NULL) { + str+=5; + if (!isdigit(str[0]) || !isdigit(str[1])) continue; + day0=(str[0]-'0')*10+(str[1]-'0'); + str+=2; + strncpy(month,str,3); + month[3]=0; + month0=month2num(month); + if (month0>=12) continue; + str+=3; + year0=0; + for (i=0 ; isdigit(str[i]) && i<4 ; i++) year0=year0*10+(str[i]-'0'); + if (i!=4) continue; + str+=4; + if (str[0]!='_') continue; + str++; + + if (!isdigit(str[0]) || !isdigit(str[1])) continue; + hour0=(str[0]-'0')*10+(str[1]-'0'); + str+=2; + if (!isdigit(str[0]) || !isdigit(str[1])) continue; + minute0=(str[0]-'0')*10+(str[1]-'0'); + str+=2; + + if (*str != '-') continue; + str++; + + if (!isdigit(str[0]) || !isdigit(str[1])) continue; + day1=(str[0]-'0')*10+(str[1]-'0'); + str+=2; + strncpy(month,str,3); + month[3]=0; + month1=month2num(month); + if (month1>=12) continue; + str+=3; + year1=0; + for (i=0 ; isdigit(str[i]) && i<4 ; i++) year1=year1*10+(str[i]-'0'); + if (i!=4) continue; + str+=4; + + if (str[0]!='_') continue; + str++; + + if (!isdigit(str[0]) || !isdigit(str[1])) continue; + hour1=(str[0]-'0')*10+(str[1]-'0'); + str+=2; + if (!isdigit(str[0]) || !isdigit(str[1])) continue; + minute1=(str[0]-'0')*10+(str[1]-'0'); + str+=2; + + period->start.tm_mday=day0; + period->start.tm_mon=month0; + period->start.tm_year=year0-1900; + period->start.tm_hour=hour0; + period->start.tm_min=minute0; + period->end.tm_mday=day1; + period->end.tm_mon=month1; + period->end.tm_year=year1-1900; + period->end.tm_hour=hour1; + period->end.tm_min=minute1; + return(0); + } + return(-1); } void getperiod_fromrange(struct periodstruct *period,int dfrom,int duntil) { - memset(&period->start,0,sizeof(period->start)); - period->start.tm_mday=dfrom%100; - period->start.tm_mon=(dfrom/100)%100-1; - period->start.tm_year=(dfrom/10000)-1900; + memset(&period->start,0,sizeof(period->start)); + period->start.tm_mday=dfrom%100; + period->start.tm_mon=(dfrom/100)%100-1; + period->start.tm_year=(dfrom/10000)-1900; - memset(&period->end,0,sizeof(period->end)); - period->end.tm_mday=duntil%100; - period->end.tm_mon=(duntil/100)%100-1; - period->end.tm_year=(duntil/10000)-1900; + memset(&period->end,0,sizeof(period->end)); + period->end.tm_mday=duntil%100; + period->end.tm_mon=(duntil/100)%100-1; + period->end.tm_year=(duntil/10000)-1900; } int getperiod_buildtext(struct periodstruct *period) { - int i; - int range; - char text1[40], text2[40]; - - if(df[0]=='u') { - i=strftime(text1, sizeof(text1), "%Y %b %d", &period->start); - }else if(df[0]=='e') { - i=strftime(text1, sizeof(text1), "%d %b %Y", &period->start); - } else /*if(df[0]=='w')*/ { - IndexTree=INDEX_TREE_FILE; - i=strftime(text1, sizeof(text1), "%Y.%U", &period->start); - } - if (i == 0) return(-1); - - range=(period->start.tm_year!=period->end.tm_year || - period->start.tm_mon!=period->end.tm_mon || - period->start.tm_mday!=period->end.tm_mday); - if (range) { - if(df[0]=='u') { - i=strftime(text2, sizeof(text2)-i, "%Y %b %d", &period->end); - } else if(df[0]=='e') { - i=strftime(text2, sizeof(text2)-i, "%d %b %Y", &period->end); - } else { - i=strftime(text2, sizeof(text2)-i, "%Y.%U", &period->end); - } - if (i == 0) return(-1); - } - - if (range) { - snprintf(period->text,sizeof(period->text),"%s-%s",text1,text2); - snprintf(period->html,sizeof(period->html),"%s—%s",text1,text2); - } else { - strncpy(period->text,text1,sizeof(period->text)-1); - period->text[sizeof(period->text)-1]='\0'; - strncpy(period->html,text1,sizeof(period->html)-1); - period->html[sizeof(period->html)-1]='\0'; - } - return(0); + int i; + int range; + char text1[40], text2[40]; + + if(df[0]=='u') { + i=strftime(text1, sizeof(text1), "%Y %b %d", &period->start); + }else if(df[0]=='e') { + i=strftime(text1, sizeof(text1), "%d %b %Y", &period->start); + } else /*if(df[0]=='w')*/ { + IndexTree=INDEX_TREE_FILE; + i=strftime(text1, sizeof(text1), "%Y.%U", &period->start); + } + if (i == 0) return(-1); + + range=(period->start.tm_year!=period->end.tm_year || + period->start.tm_mon!=period->end.tm_mon || + period->start.tm_mday!=period->end.tm_mday); + if (range) { + if(df[0]=='u') { + i=strftime(text2, sizeof(text2)-i, "%Y %b %d", &period->end); + } else if(df[0]=='e') { + i=strftime(text2, sizeof(text2)-i, "%d %b %Y", &period->end); + } else { + i=strftime(text2, sizeof(text2)-i, "%Y.%U", &period->end); + } + if (i == 0) return(-1); + } + + if (range) { + snprintf(period->text,sizeof(period->text),"%s-%s",text1,text2); + snprintf(period->html,sizeof(period->html),"%s—%s",text1,text2); + } else { + strncpy(period->text,text1,sizeof(period->text)-1); + period->text[sizeof(period->text)-1]='\0'; + strncpy(period->html,text1,sizeof(period->html)-1); + period->html[sizeof(period->html)-1]='\0'; + } + return(0); } static void copy_images(void) { - FILE *img_in, *img_ou; - char images[512]; - char imgdir[MAXLEN]; - char srcfile[MAXLEN]; - char dstfile[MAXLEN]; - DIR *dirp; - struct dirent *direntp; - char buffer[MAXLEN]; - size_t nread; - struct stat info; - - if (snprintf(images,sizeof(images),"%simages",outdir)>=sizeof(images)) { - debuga(_("Cannot copy images to target directory %simages\n"),outdir); - exit(EXIT_FAILURE); - } - if (access(images,R_OK)!=0) { - mkdir(images,0755); - } - - strcpy(imgdir,IMAGEDIR); - dirp = opendir(imgdir); - if(dirp==NULL) { - debuga(_("(util) Can't open directory %s: %s\n"),imgdir,strerror(errno)); - return; - } - while ((direntp = readdir( dirp )) != NULL ){ - if(direntp->d_name[0]=='.') - continue; - sprintf(srcfile,"%s/%s",imgdir,direntp->d_name); - if (stat(srcfile,&info)) { - debuga(_("Cannot stat \"%s\" - %s\n"),srcfile,strerror(errno)); - continue; - } - if (S_ISREG(info.st_mode)) { - sprintf(dstfile,"%s/%s",images,direntp->d_name); - img_in = fopen(srcfile, "rb"); - if(img_in!=NULL) { - img_ou = fopen(dstfile, "wb"); - if(img_ou!=NULL) { - while ((nread = fread(buffer,1,sizeof(buffer),img_in))>0) { - if (fwrite(buffer,1,nread,img_ou)!=nread) { - debuga(_("Failed to copy image %s to %s\n"),srcfile,dstfile); - break; - } - } - fclose(img_ou); - } else - fprintf(stderr,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open/create file", dstfile, strerror(errno)); - fclose(img_in); - } else - fprintf(stderr,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open file", srcfile, strerror(errno)); - } - } - (void) closedir(dirp); - - return; + FILE *img_in, *img_ou; + char images[512]; + char imgdir[MAXLEN]; + char srcfile[MAXLEN]; + char dstfile[MAXLEN]; + DIR *dirp; + struct dirent *direntp; + char buffer[MAXLEN]; + size_t nread; + struct stat info; + + if (snprintf(images,sizeof(images),"%simages",outdir)>=sizeof(images)) { + debuga(_("Cannot copy images to target directory %simages\n"),outdir); + exit(EXIT_FAILURE); + } + if (access(images,R_OK)!=0) { + mkdir(images,0755); + } + + strcpy(imgdir,IMAGEDIR); + dirp = opendir(imgdir); + if(dirp==NULL) { + debuga(_("(util) Can't open directory %s: %s\n"),imgdir,strerror(errno)); + return; + } + while ((direntp = readdir( dirp )) != NULL ){ + if(direntp->d_name[0]=='.') + continue; + sprintf(srcfile,"%s/%s",imgdir,direntp->d_name); + if (stat(srcfile,&info)) { + debuga(_("Cannot stat \"%s\" - %s\n"),srcfile,strerror(errno)); + continue; + } + if (S_ISREG(info.st_mode)) { + sprintf(dstfile,"%s/%s",images,direntp->d_name); + img_in = fopen(srcfile, "rb"); + if(img_in!=NULL) { + img_ou = fopen(dstfile, "wb"); + if(img_ou!=NULL) { + while ((nread = fread(buffer,1,sizeof(buffer),img_in))>0) { + if (fwrite(buffer,1,nread,img_ou)!=nread) { + debuga(_("Failed to copy image %s to %s\n"),srcfile,dstfile); + break; + } + } + fclose(img_ou); + } else + fprintf(stderr,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open/create file", dstfile, strerror(errno)); + fclose(img_in); + } else + fprintf(stderr,"SARG: (util): %s %s: %s\n", _("Cannot open file")?_("Cannot open file"):"Can't open file", srcfile, strerror(errno)); + } + } + (void) closedir(dirp); + + return; } int vrfydir(const struct periodstruct *per1, const char *addr, const char *site, const char *us, const char *form) { - FILE *fp_ou; - int num=1, count=0; - char wdir[MAXLEN]; - char dirname2[MAXLEN]; - int y1, y2; - int m1, m2; - int d1, d2; - int wlen, wlen2; - time_t curtime; - struct tm *loctm; - - strcpy(wdir,outdir); - wlen=strlen(wdir); - y1=per1->start.tm_year+1900; - y2=per1->end.tm_year+1900; - m1=per1->start.tm_mon+1; - m2=per1->end.tm_mon+1; - d1=per1->start.tm_mday; - d2=per1->end.tm_mday; - if(IndexTree == INDEX_TREE_DATE) { - wlen+=sprintf(wdir+wlen,"%04d",y1); - if(y1!=y2) wlen+=sprintf(wdir+wlen,"-%04d",y2); - if(access(wdir, R_OK) != 0) - my_mkdir(wdir); - - wlen+=sprintf(wdir+wlen,"/%02d",m1); - if(m1 != m2) wlen+=sprintf(wdir+wlen,"-%02d",m2); - if(access(wdir, R_OK) != 0) - my_mkdir(wdir); - - wlen+=sprintf(wdir+wlen,"/%02d",d1); - if(d1!=d2) wlen+=sprintf(wdir+wlen,"-%02d",d2); - } else { - if(df[0] == 'u') { - wlen=snprintf(wdir+wlen,sizeof(wdir)-wlen,"%04d%s%02d-%04d%s%02d",y1, - conv_month_name(m1),d1,y2,conv_month_name(m2),d2); - } else if(df[0] == 'e') { - wlen=snprintf(wdir+wlen,sizeof(wdir)-wlen,"%02d%s%04d-%02d%s%04d",d1, - conv_month_name(m1),y1,d2,conv_month_name(m2),y2); - } else if(df[0] == 'w') { - wlen2=strftime(wdir+wlen, sizeof(wdir)-wlen, "%Y.%U", &per1->start); - if (wlen2==0) return(-1); - wlen+=wlen2; - } - } - - if(us[0] != '\0') { - struct userinfostruct *uinfo=userinfo_find_from_id(us); - if (uinfo) { - strcat(wdir,"-"); - strcat(wdir,uinfo->filename); - } - } - if(addr[0] != '\0') { - strcat(wdir,"-"); - strcat(wdir,addr); - } - if(site[0] != '\0') { - strcat(wdir,"-"); - strcat(wdir,site); - } - - strcpy(outdirname,wdir); - - if(IndexTree != INDEX_TREE_DATE) { - if(!OverwriteReport) { - while(num) { - if(access(wdir,R_OK) == 0) { - sprintf(wdir,"%s.%d",outdirname,num); - num++; - count++; - } else - break; - } - - if(count > 0) { - if(debug) - debuga(_("File %s already exists, moved to %s\n"),outdirname,wdir); - rename(outdirname,wdir); - } - } else { - if(access(outdirname,R_OK) == 0) { - unlinkdir(outdirname,1); - } - } - my_mkdir(outdirname); - } else { - strcpy(dirname2,wdir); - if(!OverwriteReport) { - while(num) { - if(access(wdir,R_OK) == 0) { - sprintf(wdir,"%s.%d",dirname2,num); - num++; - count++; - } else - break; - } - - if(count > 0) { - if(debug) - debuga(_("File %s already exists, moved to %s\n"),dirname2,wdir); - rename(dirname2,wdir); - strcpy(dirname2,wdir); - } - } else { - if(access(wdir,R_OK) == 0) { - unlinkdir(wdir,1); - } - } - - if(access(wdir, R_OK) != 0) - my_mkdir(wdir); - } - - strcpy(dirname2,wdir); - - sprintf(wdir,"%s/sarg-date",outdirname); - if ((fp_ou = fopen(wdir, "wt")) == 0) { - debuga(_("cannot open %s for writing\n"),wdir); - perror("SARG:"); - exit(EXIT_FAILURE); - } - time(&curtime); - //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime)); - loctm=localtime(&curtime); - strftime(wdir,sizeof(wdir),"%Y-%m-%d %H:%M:%S",loctm); - if (fprintf(fp_ou,"%s %d\n",wdir,loctm->tm_isdst)<0) { - debuga(_("Failed to write the date in %s\n"),wdir); - perror("SARG:"); - exit(EXIT_FAILURE); - } - if (fclose(fp_ou)==EOF) { - debuga(_("Failed to write the date in %s\n"),wdir); - perror("SARG:"); - exit(EXIT_FAILURE); - } - - copy_images(); - return(0); + FILE *fp_ou; + int num=1, count=0; + char wdir[MAXLEN]; + char dirname2[MAXLEN]; + int y1, y2; + int m1, m2; + int d1, d2; + int wlen, wlen2; + time_t curtime; + struct tm *loctm; + + strcpy(wdir,outdir); + wlen=strlen(wdir); + y1=per1->start.tm_year+1900; + y2=per1->end.tm_year+1900; + m1=per1->start.tm_mon+1; + m2=per1->end.tm_mon+1; + d1=per1->start.tm_mday; + d2=per1->end.tm_mday; + if(IndexTree == INDEX_TREE_DATE) { + wlen+=sprintf(wdir+wlen,"%04d",y1); + if(y1!=y2) wlen+=sprintf(wdir+wlen,"-%04d",y2); + if(access(wdir, R_OK) != 0) + my_mkdir(wdir); + + wlen+=sprintf(wdir+wlen,"/%02d",m1); + if(m1 != m2) wlen+=sprintf(wdir+wlen,"-%02d",m2); + if(access(wdir, R_OK) != 0) + my_mkdir(wdir); + + wlen+=sprintf(wdir+wlen,"/%02d",d1); + if(d1!=d2) wlen+=sprintf(wdir+wlen,"-%02d",d2); + } else { + if(df[0] == 'u') { + wlen=snprintf(wdir+wlen,sizeof(wdir)-wlen,"%04d%s%02d-%04d%s%02d",y1, + conv_month_name(m1),d1,y2,conv_month_name(m2),d2); + } else if(df[0] == 'e') { + wlen=snprintf(wdir+wlen,sizeof(wdir)-wlen,"%02d%s%04d-%02d%s%04d",d1, + conv_month_name(m1),y1,d2,conv_month_name(m2),y2); + } else if(df[0] == 'w') { + wlen2=strftime(wdir+wlen, sizeof(wdir)-wlen, "%Y.%U", &per1->start); + if (wlen2==0) return(-1); + wlen+=wlen2; + } + } + + if(us[0] != '\0') { + struct userinfostruct *uinfo=userinfo_find_from_id(us); + if (uinfo) { + strcat(wdir,"-"); + strcat(wdir,uinfo->filename); + } + } + if(addr[0] != '\0') { + strcat(wdir,"-"); + strcat(wdir,addr); + } + if(site[0] != '\0') { + strcat(wdir,"-"); + strcat(wdir,site); + } + + strcpy(outdirname,wdir); + + if(IndexTree != INDEX_TREE_DATE) { + if(!OverwriteReport) { + while(num) { + if(access(wdir,R_OK) == 0) { + sprintf(wdir,"%s.%d",outdirname,num); + num++; + count++; + } else + break; + } + + if(count > 0) { + if(debug) + debuga(_("File %s already exists, moved to %s\n"),outdirname,wdir); + rename(outdirname,wdir); + } + } else { + if(access(outdirname,R_OK) == 0) { + unlinkdir(outdirname,1); + } + } + my_mkdir(outdirname); + } else { + strcpy(dirname2,wdir); + if(!OverwriteReport) { + while(num) { + if(access(wdir,R_OK) == 0) { + sprintf(wdir,"%s.%d",dirname2,num); + num++; + count++; + } else + break; + } + + if(count > 0) { + if(debug) + debuga(_("File %s already exists, moved to %s\n"),dirname2,wdir); + rename(dirname2,wdir); + strcpy(dirname2,wdir); + } + } else { + if(access(wdir,R_OK) == 0) { + unlinkdir(wdir,1); + } + } + + if(access(wdir, R_OK) != 0) + my_mkdir(wdir); + } + + strcpy(dirname2,wdir); + + sprintf(wdir,"%s/sarg-date",outdirname); + if ((fp_ou = fopen(wdir, "wt")) == 0) { + debuga(_("cannot open %s for writing\n"),wdir); + perror("SARG:"); + exit(EXIT_FAILURE); + } + time(&curtime); + //strftime(wdir,sizeof(wdir),"%a %b %d %H:%M:%S %Z %Y",localtime(&curtime)); + loctm=localtime(&curtime); + strftime(wdir,sizeof(wdir),"%Y-%m-%d %H:%M:%S",loctm); + if (fprintf(fp_ou,"%s %d\n",wdir,loctm->tm_isdst)<0) { + debuga(_("Failed to write the date in %s\n"),wdir); + perror("SARG:"); + exit(EXIT_FAILURE); + } + if (fclose(fp_ou)==EOF) { + debuga(_("Failed to write the date in %s\n"),wdir); + perror("SARG:"); + exit(EXIT_FAILURE); + } + + copy_images(); + return(0); } void strip_latin(char *line) { - int i,j; - int skip; - - j=0; - skip=0; - for (i=0;line[i];i++){ - if (skip){ - if (line[i]==';') skip=0; - } else { - if (line[i]=='&') - skip=1; - else - line[j++]=line[i]; - } - } - line[j]='\0'; - return; - + int i,j; + int skip; + + j=0; + skip=0; + for (i=0;line[i];i++){ + if (skip){ + if (line[i]==';') skip=0; + } else { + if (line[i]=='&') + skip=1; + else + line[j++]=line[i]; + } + } + line[j]='\0'; + return; } void zdate(char *ftime,int ftimesize, const char *DateFormat) { + time_t t; + struct tm *local; - time_t t; - struct tm *local; - - t = time(NULL); - local = localtime(&t); - if(strcmp(DateFormat,"u") == 0) - strftime(ftime, ftimesize, "%b/%d/%Y %H:%M", local); - if(strcmp(DateFormat,"e") == 0) - strftime(ftime, ftimesize, "%d/%b/%Y-%H:%M", local); - if(strcmp(DateFormat,"w") == 0) - strftime(ftime, ftimesize, "%W-%H-%M", local); - return; + t = time(NULL); + local = localtime(&t); + if(strcmp(DateFormat,"u") == 0) + strftime(ftime, ftimesize, "%b/%d/%Y %H:%M", local); + if(strcmp(DateFormat,"e") == 0) + strftime(ftime, ftimesize, "%d/%b/%Y-%H:%M", local); + if(strcmp(DateFormat,"w") == 0) + strftime(ftime, ftimesize, "%W-%H-%M", local); + return; } char *fixtime(long long int elap) { - int num = elap / 1000; - int hor = 0; - int min = 0; - int sec = 0; - static char buf[12]; + int num = elap / 1000; + int hor = 0; + int min = 0; + int sec = 0; + static char buf[12]; - hor=num / 3600; - min=(num % 3600) / 60; - sec=num % 60; + hor=num / 3600; + min=(num % 3600) / 60; + sec=num % 60; - if(hor==0 && min==0 && sec==0) - strcpy(buf,"0"); - else - sprintf(buf,"%d:%02d:%02d",hor,min,sec); + if(hor==0 && min==0 && sec==0) + strcpy(buf,"0"); + else + sprintf(buf,"%d:%02d:%02d",hor,min,sec); - return buf; + return buf; } void date_from(char *date, int *dfrom, int *duntil) { - int d0=0; - int m0=0; - int y0=0; - int d1=0; - int m1=0; - int y1=0; - - if (isdigit(date[0])) { - int next=-1; - - if (sscanf(date,"%d/%d/%d%n",&d0,&m0,&y0,&next)!=3 || y0<100 || m0<1 || m0>12 || d0<1 || d0>31 || next<0) { - debuga(_("The date passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n")); - exit(EXIT_FAILURE); - } - if (date[next]=='-') { - if (sscanf(date+next+1,"%d/%d/%d",&d1,&m1,&y1)!=3 || y1<100 || m1<1 || m1>12 || d1<1 || d1>31) { - debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n")); - exit(EXIT_FAILURE); - } - } else if (date[next]!='\0') { - debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n")); - exit(EXIT_FAILURE); - } else { - d1=d0; - m1=m0; - y1=y0; - } - } else { - int i; - time_t Today,t1; - struct tm *Date0,Date1; - - if (time(&Today)==(time_t)-1) { - debuga(_("Failed to get the current time\n")); - exit(EXIT_FAILURE); - } - if (sscanf(date,"day-%d",&i)==1) { - if (i<0) { - debuga(_("Invalid number of days in -d parameter\n")); - exit(EXIT_FAILURE); - } - Today-=i*24*60*60; - Date0=localtime(&Today); - if (Date0==NULL) { - debuga(_("Cannot convert local time: %s\n"),strerror(errno)); - exit(EXIT_FAILURE); - } - y0=y1=Date0->tm_year+1900; - m0=m1=Date0->tm_mon+1; - d0=d1=Date0->tm_mday; - } else if (sscanf(date,"week-%d",&i)==1) { - /* - There is no portable way to find the first day of the week even though the - information is available in the locale. nl_langinfo has the unofficial - parameters _NL_TIME_FIRST_WEEKDAY and _NL_TIME_WEEK_1STDAY but they are - undocumented as is their return value and it is discouraged to use them. - Beside, nl_langinfo isn't available on windows and the first day of the - week isn't available at all on that system. - */ - const int FirstWeekDay=1; - time_t WeekBegin; - - if (i<0) { - debuga(_("Invalid number of weeks in -d parameter\n")); - exit(EXIT_FAILURE); - } - Date0=localtime(&Today); - if (Date0==NULL) { - debuga(_("Cannot convert local time: %s\n"),strerror(errno)); - exit(EXIT_FAILURE); - } - WeekBegin=Today-((Date0->tm_wday-FirstWeekDay+7)%7)*24*60*60; - WeekBegin-=i*7*24*60*60; - Date0=localtime(&WeekBegin); - if (Date0==NULL) { - debuga(_("Cannot convert local time: %s\n"),strerror(errno)); - exit(EXIT_FAILURE); - } - y0=Date0->tm_year+1900; - m0=Date0->tm_mon+1; - d0=Date0->tm_mday; - WeekBegin+=6*24*60*60; - Date0=localtime(&WeekBegin); - if (Date0==NULL) { - debuga(_("Cannot convert local time: %s\n"),strerror(errno)); - exit(EXIT_FAILURE); - } - y1=Date0->tm_year+1900; - m1=Date0->tm_mon+1; - d1=Date0->tm_mday; - } else if (sscanf(date,"month-%d",&i)==1) { - if (i<0) { - debuga(_("Invalid number of months in -d parameter\n")); - exit(EXIT_FAILURE); - } - Date0=localtime(&Today); - if (Date0==NULL) { - debuga(_("Cannot convert local time: %s\n"),strerror(errno)); - exit(EXIT_FAILURE); - } - if (Date0->tm_montm_year+1900-i/12-1; - m0=(Date0->tm_mon+12-i%12)%12+1; - d0=1; - } else { - y0=Date0->tm_year+1900-i/12; - m0=Date0->tm_mon-i%12+1; - d0=1; - } - memcpy(&Date1,Date0,sizeof(struct tm)); - Date1.tm_isdst=-1; - Date1.tm_mday=1; - if (m0<12) { - Date1.tm_mon=m0; - Date1.tm_year=y0-1900; - } else { - Date1.tm_mon=0; - Date1.tm_year=y0-1900+1; - } - t1=mktime(&Date1); - t1-=24*60*60; - Date0=localtime(&t1); - y1=Date0->tm_year+1900; - m1=Date0->tm_mon+1; - d1=Date0->tm_mday; - } else { - debuga(_("Invalid date range passed on command line\n")); - exit(EXIT_FAILURE); - } - } - - *dfrom=y0*10000+m0*100+d0; - *duntil=y1*10000+m1*100+d1; - sprintf(date,"%02d/%02d/%04d-%02d/%02d/%04d",d0,m0,y0,d1,m1,y1); - return; + int d0=0; + int m0=0; + int y0=0; + int d1=0; + int m1=0; + int y1=0; + + if (isdigit(date[0])) { + int next=-1; + + if (sscanf(date,"%d/%d/%d%n",&d0,&m0,&y0,&next)!=3 || y0<100 || m0<1 || m0>12 || d0<1 || d0>31 || next<0) { + debuga(_("The date passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n")); + exit(EXIT_FAILURE); + } + if (date[next]=='-') { + if (sscanf(date+next+1,"%d/%d/%d",&d1,&m1,&y1)!=3 || y1<100 || m1<1 || m1>12 || d1<1 || d1>31) { + debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n")); + exit(EXIT_FAILURE); + } + } else if (date[next]!='\0') { + debuga(_("The date range passed as argument is not formated as dd/mm/yyyy or dd/mm/yyyy-dd/mm/yyyy\n")); + exit(EXIT_FAILURE); + } else { + d1=d0; + m1=m0; + y1=y0; + } + } else { + int i; + time_t Today,t1; + struct tm *Date0,Date1; + + if (time(&Today)==(time_t)-1) { + debuga(_("Failed to get the current time\n")); + exit(EXIT_FAILURE); + } + if (sscanf(date,"day-%d",&i)==1) { + if (i<0) { + debuga(_("Invalid number of days in -d parameter\n")); + exit(EXIT_FAILURE); + } + Today-=i*24*60*60; + Date0=localtime(&Today); + if (Date0==NULL) { + debuga(_("Cannot convert local time: %s\n"),strerror(errno)); + exit(EXIT_FAILURE); + } + y0=y1=Date0->tm_year+1900; + m0=m1=Date0->tm_mon+1; + d0=d1=Date0->tm_mday; + } else if (sscanf(date,"week-%d",&i)==1) { + /* + There is no portable way to find the first day of the week even though the + information is available in the locale. nl_langinfo has the unofficial + parameters _NL_TIME_FIRST_WEEKDAY and _NL_TIME_WEEK_1STDAY but they are + undocumented as is their return value and it is discouraged to use them. + Beside, nl_langinfo isn't available on windows and the first day of the + week isn't available at all on that system. + */ + const int FirstWeekDay=1; + time_t WeekBegin; + + if (i<0) { + debuga(_("Invalid number of weeks in -d parameter\n")); + exit(EXIT_FAILURE); + } + Date0=localtime(&Today); + if (Date0==NULL) { + debuga(_("Cannot convert local time: %s\n"),strerror(errno)); + exit(EXIT_FAILURE); + } + WeekBegin=Today-((Date0->tm_wday-FirstWeekDay+7)%7)*24*60*60; + WeekBegin-=i*7*24*60*60; + Date0=localtime(&WeekBegin); + if (Date0==NULL) { + debuga(_("Cannot convert local time: %s\n"),strerror(errno)); + exit(EXIT_FAILURE); + } + y0=Date0->tm_year+1900; + m0=Date0->tm_mon+1; + d0=Date0->tm_mday; + WeekBegin+=6*24*60*60; + Date0=localtime(&WeekBegin); + if (Date0==NULL) { + debuga(_("Cannot convert local time: %s\n"),strerror(errno)); + exit(EXIT_FAILURE); + } + y1=Date0->tm_year+1900; + m1=Date0->tm_mon+1; + d1=Date0->tm_mday; + } else if (sscanf(date,"month-%d",&i)==1) { + if (i<0) { + debuga(_("Invalid number of months in -d parameter\n")); + exit(EXIT_FAILURE); + } + Date0=localtime(&Today); + if (Date0==NULL) { + debuga(_("Cannot convert local time: %s\n"),strerror(errno)); + exit(EXIT_FAILURE); + } + if (Date0->tm_montm_year+1900-i/12-1; + m0=(Date0->tm_mon+12-i%12)%12+1; + d0=1; + } else { + y0=Date0->tm_year+1900-i/12; + m0=Date0->tm_mon-i%12+1; + d0=1; + } + memcpy(&Date1,Date0,sizeof(struct tm)); + Date1.tm_isdst=-1; + Date1.tm_mday=1; + if (m0<12) { + Date1.tm_mon=m0; + Date1.tm_year=y0-1900; + } else { + Date1.tm_mon=0; + Date1.tm_year=y0-1900+1; + } + t1=mktime(&Date1); + t1-=24*60*60; + Date0=localtime(&t1); + y1=Date0->tm_year+1900; + m1=Date0->tm_mon+1; + d1=Date0->tm_mday; + } else { + debuga(_("Invalid date range passed on command line\n")); + exit(EXIT_FAILURE); + } + } + + *dfrom=y0*10000+m0*100+d0; + *duntil=y1*10000+m1*100+d1; + sprintf(date,"%02d/%02d/%04d-%02d/%02d/%04d",d0,m0,y0,d1,m1,y1); + return; } char *strlow(char *string) { - char *s; + char *s; - if (string) - { - for (s = string; *s; ++s) - *s = tolower(*s); - } + if (string) + { + for (s = string; *s; ++s) + *s = tolower(*s); + } - return string; + return string; } @@ -1294,507 +1286,505 @@ char *strlow(char *string) char *strup(char *string) { - char *s; + char *s; - if (string) - { - for (s = string; *s; ++s) - *s = toupper(*s); - } + if (string) + { + for (s = string; *s; ++s) + *s = toupper(*s); + } - return string; + return string; } void removetmp(const char *outdir) { - FILE *fp_in; - char warea[256]; - char buf[MAXLEN]; - long pos; - - if(!RemoveTempFiles) - return; - - if(debug) { - debuga(_("Purging temporary file sarg-general\n")); - } - if (snprintf(warea,sizeof(warea),"%s/sarg-general",outdir)>=sizeof(warea)) { - debuga(_("(removetmp) directory too long to remove %s/sarg-period\n"),outdir); - exit(EXIT_FAILURE); - } - if((fp_in=fopen(warea,"r+"))==NULL){ - debuga(_("(removetmp) Cannot open file %s\n"),warea); - exit(EXIT_FAILURE); - } - while(fgets(buf,sizeof(buf),fp_in)!=NULL) { - if(strncmp(buf,"TOTAL",5) == 0 && (buf[6]=='\t' || buf[6]==' ')) - break; - } - if (fseek(fp_in,0,SEEK_SET)==-1) { - debuga(_("Failed to rewind to the beginning of the file %s: %s\n"),warea,strerror(errno)); - exit(EXIT_FAILURE); - } - - if (fputs(buf,fp_in)==EOF) { - debuga(_("Failed to write the total line in %s - %s\n"),warea,strerror(errno)); - exit(EXIT_FAILURE); - } - pos=ftell(fp_in); - if (pos>0 && ftruncate(fileno(fp_in),pos)==-1) { - debuga(_("Failed to truncate %s: %s\n"),warea,strerror(errno)); - exit(EXIT_FAILURE); - } - if (fclose(fp_in)==EOF) { - debuga(_("Failed to close %s after writing the total line - %s\n"),warea,strerror(errno)); - exit(EXIT_FAILURE); - } - - return; + FILE *fp_in; + char warea[256]; + char buf[MAXLEN]; + long pos; + + if(!RemoveTempFiles) + return; + + if(debug) { + debuga(_("Purging temporary file sarg-general\n")); + } + if (snprintf(warea,sizeof(warea),"%s/sarg-general",outdir)>=sizeof(warea)) { + debuga(_("(removetmp) directory too long to remove %s/sarg-period\n"),outdir); + exit(EXIT_FAILURE); + } + if((fp_in=fopen(warea,"r+"))==NULL){ + debuga(_("(removetmp) Cannot open file %s\n"),warea); + exit(EXIT_FAILURE); + } + while(fgets(buf,sizeof(buf),fp_in)!=NULL) { + if(strncmp(buf,"TOTAL",5) == 0 && (buf[6]=='\t' || buf[6]==' ')) + break; + } + if (fseek(fp_in,0,SEEK_SET)==-1) { + debuga(_("Failed to rewind to the beginning of the file %s: %s\n"),warea,strerror(errno)); + exit(EXIT_FAILURE); + } + + if (fputs(buf,fp_in)==EOF) { + debuga(_("Failed to write the total line in %s - %s\n"),warea,strerror(errno)); + exit(EXIT_FAILURE); + } + pos=ftell(fp_in); + if (pos>0 && ftruncate(fileno(fp_in),pos)==-1) { + debuga(_("Failed to truncate %s: %s\n"),warea,strerror(errno)); + exit(EXIT_FAILURE); + } + if (fclose(fp_in)==EOF) { + debuga(_("Failed to close %s after writing the total line - %s\n"),warea,strerror(errno)); + exit(EXIT_FAILURE); + } + + return; } void load_excludecodes(const char *ExcludeCodes) { - - FILE *fp_in; - char data[80]; - int i; - int Stored; - long int MemSize; - - if(ExcludeCodes[0] == '\0') - return; - - if((fp_in=fopen(ExcludeCodes,"r"))==NULL) { - debuga(_("(util) Cannot open file %s (exclude_codes)\n"),ExcludeCodes); - exit(EXIT_FAILURE); - } - - if (fseek(fp_in, 0, SEEK_END)==-1) { - debuga(_("Failed to move till the end of the excluded codes file %s: %s\n"),ExcludeCodes,strerror(errno)); - exit(EXIT_FAILURE); - } - MemSize = ftell(fp_in); - if (MemSize<0) { - debuga(_("Cannot get the size of file %s\n"),ExcludeCodes); - exit(EXIT_FAILURE); - } - if (fseek(fp_in, 0, SEEK_SET)==-1) { - debuga(_("Failed to rewind the excluded codes file %s: %s\n"),ExcludeCodes,strerror(errno)); - exit(EXIT_FAILURE); - } - - MemSize+=1; - if((excludecode=(char *) malloc(MemSize))==NULL) { - debuga(_("malloc error (%ld)\n"),MemSize); - exit(EXIT_FAILURE); - } - memset(excludecode,0,MemSize); - - Stored=0; - while(fgets(data,sizeof(data),fp_in)!=NULL) { - if (data[0]=='#') continue; - for (i=strlen(data)-1 ; i>=0 && (unsigned char)data[i]<=' ' ; i--) data[i]='\0'; - if (i<0) continue; - if (Stored+i+2>=MemSize) { - debuga(_("Too many codes to exclude in file %s\n"),ExcludeCodes); - break; - } - strcat(excludecode,data); - strcat(excludecode,";"); - Stored+=i+1; - } - - fclose(fp_in); - return; + FILE *fp_in; + char data[80]; + int i; + int Stored; + long int MemSize; + + if(ExcludeCodes[0] == '\0') + return; + + if((fp_in=fopen(ExcludeCodes,"r"))==NULL) { + debuga(_("(util) Cannot open file %s (exclude_codes)\n"),ExcludeCodes); + exit(EXIT_FAILURE); + } + + if (fseek(fp_in, 0, SEEK_END)==-1) { + debuga(_("Failed to move till the end of the excluded codes file %s: %s\n"),ExcludeCodes,strerror(errno)); + exit(EXIT_FAILURE); + } + MemSize = ftell(fp_in); + if (MemSize<0) { + debuga(_("Cannot get the size of file %s\n"),ExcludeCodes); + exit(EXIT_FAILURE); + } + if (fseek(fp_in, 0, SEEK_SET)==-1) { + debuga(_("Failed to rewind the excluded codes file %s: %s\n"),ExcludeCodes,strerror(errno)); + exit(EXIT_FAILURE); + } + + MemSize+=1; + if((excludecode=(char *) malloc(MemSize))==NULL) { + debuga(_("malloc error (%ld)\n"),MemSize); + exit(EXIT_FAILURE); + } + memset(excludecode,0,MemSize); + + Stored=0; + while(fgets(data,sizeof(data),fp_in)!=NULL) { + if (data[0]=='#') continue; + for (i=strlen(data)-1 ; i>=0 && (unsigned char)data[i]<=' ' ; i--) data[i]='\0'; + if (i<0) continue; + if (Stored+i+2>=MemSize) { + debuga(_("Too many codes to exclude in file %s\n"),ExcludeCodes); + break; + } + strcat(excludecode,data); + strcat(excludecode,";"); + Stored+=i+1; + } + + fclose(fp_in); + return; } void free_excludecodes(void) { - if (excludecode) { - free(excludecode); - excludecode=NULL; - } + if (excludecode) { + free(excludecode); + excludecode=NULL; + } } int vercode(const char *code) { - char *cod; - int clen; + char *cod; + int clen; - if (excludecode && excludecode[0]!='\0') { - clen=strlen(code); - cod=excludecode; - while (cod) { - if (strncmp(code,cod,clen)==0 && cod[clen]==';') - return 1; - cod=strchr(cod,';'); - if (cod) cod++; - } - } - return 0; + if (excludecode && excludecode[0]!='\0') { + clen=strlen(code); + cod=excludecode; + while (cod) { + if (strncmp(code,cod,clen)==0 && cod[clen]==';') + return 1; + cod=strchr(cod,';'); + if (cod) cod++; + } + } + return 0; } void fixnone(char *str) { - int i; + int i; - for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--); - if(i==3 && strncmp(str,"none",4) == 0) - str[0]='\0'; + for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--); + if(i==3 && strncmp(str,"none",4) == 0) + str[0]='\0'; - return; + return; } void fixendofline(char *str) { - int i; + int i; - for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--) str[i]=0; + for (i=strlen(str)-1 ; i>=0 && (unsigned char)str[i]<=' ' ; i--) str[i]=0; } #ifdef LEGACY_TESTVALIDUSERCHAR int testvaliduserchar(const char *user) { + int x=0; + int y=0; - int x=0; - int y=0; - - for (y=0; y *(int *)b ) return 1; - if( *(int *)a < *(int *)b ) return -1; - return 0; +{ + if( *(int *)a > *(int *)b ) return 1; + if( *(int *)a < *(int *)b ) return -1; + return 0; } int getnumlist( char *buf, numlist *list, const int len, const int maxvalue ) { - int i, j, d, flag, r1, r2; - char *pbuf, **bp, *strbufs[ 24 ]; - - bp = strbufs; - strtok( buf, " \t" ); - for( *bp = strtok( NULL, "," ), list->len = 0; *bp; *bp = strtok( NULL, "," ) ) { - if( ++bp >= &strbufs[ 24 ] ) - break; - list->len++; - } - if( ! list->len ) - return -1; - d = 0; - for( i = 0; i < list->len; i++ ) { - if( strchr( strbufs[ i ], '-' ) != 0 ) { - pbuf = strbufs[ i ]; - strtok( pbuf, "-" ); - pbuf = strtok( NULL, "\0" ); - r1 = atoi( strbufs[ i ] ); - if( ( r2 = atoi( pbuf ) ) >= maxvalue || r1 >= r2 ) - return -1; - if( i + d + ( r2 - r1 ) + 1 <= len ) { - for( j = r1; j <= r2; j++ ) - list->list[ i + d++ ] = j; - d--; - } - } - else - if( ( list->list[ i + d ] = atoi( strbufs[ i ] ) ) >= maxvalue ) - return 1; - } - list->len += d; - qsort( list->list, list->len, sizeof( int ), compar ); - do { - flag = 0; - for( i = 0; i < list->len - 1; i++ ) - if( list->list[ i ] == list->list[ i + 1 ] ) { - for( j = i + 1; j < list->len; j++ ) - list->list[ j - 1 ] = list->list[ j ]; - list->len--; - flag = 1; - break; - } - } while( flag ); - return 0; + int i, j, d, flag, r1, r2; + char *pbuf, **bp, *strbufs[ 24 ]; + + bp = strbufs; + strtok( buf, " \t" ); + for( *bp = strtok( NULL, "," ), list->len = 0; *bp; *bp = strtok( NULL, "," ) ) { + if( ++bp >= &strbufs[ 24 ] ) + break; + list->len++; + } + if( ! list->len ) + return -1; + d = 0; + for( i = 0; i < list->len; i++ ) { + if( strchr( strbufs[ i ], '-' ) != 0 ) { + pbuf = strbufs[ i ]; + strtok( pbuf, "-" ); + pbuf = strtok( NULL, "\0" ); + r1 = atoi( strbufs[ i ] ); + if( ( r2 = atoi( pbuf ) ) >= maxvalue || r1 >= r2 ) + return -1; + if( i + d + ( r2 - r1 ) + 1 <= len ) { + for( j = r1; j <= r2; j++ ) + list->list[ i + d++ ] = j; + d--; + } + } + else + if( ( list->list[ i + d ] = atoi( strbufs[ i ] ) ) >= maxvalue ) + return 1; + } + list->len += d; + qsort( list->list, list->len, sizeof( int ), compar ); + do { + flag = 0; + for( i = 0; i < list->len - 1; i++ ) + if( list->list[ i ] == list->list[ i + 1 ] ) { + for( j = i + 1; j < list->len; j++ ) + list->list[ j - 1 ] = list->list[ j ]; + list->len--; + flag = 1; + break; + } + } while( flag ); + return 0; } char *get_size(const char *path, const char *file) { - FILE *fp; - static char response[255]; - char cmd[255]; - char *ptr; - - if (snprintf(cmd,sizeof(cmd),"du -skh %s%s",path,file)>=sizeof(cmd)) { - debuga(_("Cannot get disk space because the path %s%s is too long\n"),path,file); - exit(EXIT_FAILURE); - } - if ((fp = popen(cmd, "r")) == NULL) { - debuga(_("Cannot get disk space with command %s\n"),cmd); - exit(EXIT_FAILURE); - } - if (!fgets(response, sizeof(response), fp)) { - debuga(_("Cannot get disk size with command %s\n"),cmd); - exit(EXIT_FAILURE); - } - ptr=strchr(response,'\t'); - if (ptr==NULL) { - debuga(_("The command %s failed\n"),cmd); - exit(EXIT_FAILURE); - } - pclose(fp); - *ptr='\0'; - - return (response); + FILE *fp; + static char response[255]; + char cmd[255]; + char *ptr; + + if (snprintf(cmd,sizeof(cmd),"du -skh %s%s",path,file)>=sizeof(cmd)) { + debuga(_("Cannot get disk space because the path %s%s is too long\n"),path,file); + exit(EXIT_FAILURE); + } + if ((fp = popen(cmd, "r")) == NULL) { + debuga(_("Cannot get disk space with command %s\n"),cmd); + exit(EXIT_FAILURE); + } + if (!fgets(response, sizeof(response), fp)) { + debuga(_("Cannot get disk size with command %s\n"),cmd); + exit(EXIT_FAILURE); + } + ptr=strchr(response,'\t'); + if (ptr==NULL) { + debuga(_("The command %s failed\n"),cmd); + exit(EXIT_FAILURE); + } + pclose(fp); + *ptr='\0'; + + return (response); } void show_info(FILE *fp_ou) { - char ftime[127]; + char ftime[127]; - if(!ShowSargInfo) return; - zdate(ftime, sizeof(ftime), DateFormat); - fprintf(fp_ou,"
%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); + if(!ShowSargInfo) return; + zdate(ftime, sizeof(ftime), DateFormat); + fprintf(fp_ou,"
%s %s-%s %s %s
\n",_("Generated by"),URL,PGM,VERSION,_("on"),ftime); } void show_sarg(FILE *fp_ou, int depth) { - int i; + int i; - if(!ShowSargLogo) return; - fputs("
\"Sarg\" Squid Analysis Report Generator
\n",fp_ou); + if(!ShowSargLogo) return; + fputs("
\"Sarg\" Squid Analysis Report Generator
\n",fp_ou); } void write_logo_image(FILE *fp_ou) { - if(LogoImage[0]!='\0') - fprintf(fp_ou, "
\"Logo\" %s
\n",LogoImage,Width,Height,LogoText); + if(LogoImage[0]!='\0') + fprintf(fp_ou, "
\"Logo\" %s
\n",LogoImage,Width,Height,LogoText); } void write_html_head(FILE *fp_ou, int depth, const char *page_title,int javascript) { - int i; + int i; - fputs("\n\n",fp_ou); - fprintf(fp_ou, "\n \n",CharSet); - if (page_title) fprintf(fp_ou,"%s\n",page_title); - css(fp_ou); - if ((javascript & HTML_JS_SORTTABLE)!=0 && SortTableJs[0]) { - fputs("\n",fp_ou); - } - fputs("\n\n",fp_ou); + fputs("\n\n",fp_ou); + fprintf(fp_ou, "\n \n",CharSet); + if (page_title) fprintf(fp_ou,"%s\n",page_title); + css(fp_ou); + if ((javascript & HTML_JS_SORTTABLE)!=0 && SortTableJs[0]) { + fputs("\n",fp_ou); + } + fputs("\n\n",fp_ou); } void write_html_header(FILE *fp_ou, int depth, const char *page_title,int javascript) { - write_html_head(fp_ou,depth,page_title,javascript); - write_logo_image(fp_ou); - show_sarg(fp_ou, depth); - fprintf(fp_ou,"
\n\n",Title); + write_html_head(fp_ou,depth,page_title,javascript); + write_logo_image(fp_ou); + show_sarg(fp_ou, depth); + fprintf(fp_ou,"
%s
\n\n",Title); } void close_html_header(FILE *fp_ou) { - fputs("
%s
\n",fp_ou); + fputs("
\n",fp_ou); } int write_html_trailer(FILE *fp_ou) { - show_info(fp_ou); - if (fputs("\n\n",fp_ou)==EOF) return(-1); - return(0); + show_info(fp_ou); + if (fputs("\n\n",fp_ou)==EOF) return(-1); + return(0); } void output_html_string(FILE *fp_ou,const char *str,int maxlen) { - int i=0; - - while (*str && (maxlen<=0 || i': - fputs(">",fp_ou); - break; - case '"': - fputs(""",fp_ou); - break; - case '\'': - fputs("'",fp_ou); - break; - default: - fputc(*str,fp_ou); - } - str++; - i++; - } - if (maxlen>0 && i>=maxlen) - fputs("…",fp_ou); + int i=0; + + while (*str && (maxlen<=0 || i': + fputs(">",fp_ou); + break; + case '"': + fputs(""",fp_ou); + break; + case '\'': + fputs("'",fp_ou); + break; + default: + fputc(*str,fp_ou); + } + str++; + i++; + } + if (maxlen>0 && i>=maxlen) + fputs("…",fp_ou); } void output_html_url(FILE *fp_ou,const char *url) { - while (*url) { - if (*url=='&') - fputs("&",fp_ou); - else - fputc(*url,fp_ou); - url++; - } + while (*url) { + if (*url=='&') + fputs("&",fp_ou); + else + fputc(*url,fp_ou); + url++; + } } void url_hostname(const char *url,char *hostname,int hostsize) { - int i; + int i; - hostsize--; - for (i=0 ; i=0; x--) { - if(url[x] == '/' || y>=sizeof(w)-1) break; - w[y++]=url[x]; - } - if (x<0) { - w2[0]='\0'; - return; - } + y=0; + for(x=strlen(url)-1; x>=0; x--) { + if(url[x] == '/' || y>=sizeof(w)-1) break; + w[y++]=url[x]; + } + if (x<0) { + w2[0]='\0'; + return; + } - x=0; - for(y=y-1; y>=0; y--) { - w2[x++]=w[y]; - } - w2[x]='\0'; + x=0; + for(y=y-1; y>=0; y--) { + w2[x++]=w[y]; + } + w2[x]='\0'; } void url_to_file(const char *url,char *file,int filesize) { - int i,skip; + int i,skip; - filesize--; - skip=0; - for(i=0; id_name[0] == '.' && (direntp->d_name[1] == '\0' || - (direntp->d_name[1] == '.' && direntp->d_name[2] == '\0'))) - continue; - if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) { - debuga(_("directory name to delete too long: %s/%s\n"),dir,direntp->d_name); - exit(EXIT_FAILURE); - } + struct stat st; + DIR *dirp; + struct dirent *direntp; + char dname[MAXLEN]; + int err; + + dirp=opendir(dir); + if (!dirp) return; + while ((direntp = readdir(dirp)) != NULL) { + if (direntp->d_name[0] == '.' && (direntp->d_name[1] == '\0' || + (direntp->d_name[1] == '.' && direntp->d_name[2] == '\0'))) + continue; + if (snprintf(dname,sizeof(dname),"%s/%s",dir,direntp->d_name)>=sizeof(dname)) { + debuga(_("directory name to delete too long: %s/%s\n"),dir,direntp->d_name); + exit(EXIT_FAILURE); + } #ifdef HAVE_LSTAT - err=lstat(dname,&st); + err=lstat(dname,&st); #else - err=stat(dname,&st); + err=stat(dname,&st); #endif - if (err) { - debuga(_("cannot stat %s\n"),dname); - exit(EXIT_FAILURE); - } - if (S_ISREG(st.st_mode)) { - if (unlink(dname)) { - debuga(_("cannot delete %s - %s\n"),dname,strerror(errno)); - exit(EXIT_FAILURE); - } - } else if (S_ISDIR(st.st_mode)) { - unlinkdir(dname,0); - } else { - debuga(_("unknown path type %s\n"),dname); - } - } - closedir(dirp); - - if (!contentonly) { - if (rmdir(dir)) { - debuga(_("cannot delete %s - %s\n"),dir,strerror(errno)); - exit(EXIT_FAILURE); - } - } + if (err) { + debuga(_("cannot stat %s\n"),dname); + exit(EXIT_FAILURE); + } + if (S_ISREG(st.st_mode)) { + if (unlink(dname)) { + debuga(_("cannot delete %s - %s\n"),dname,strerror(errno)); + exit(EXIT_FAILURE); + } + } else if (S_ISDIR(st.st_mode)) { + unlinkdir(dname,0); + } else { + debuga(_("unknown path type %s\n"),dname); + } + } + closedir(dirp); + + if (!contentonly) { + if (rmdir(dir)) { + debuga(_("cannot delete %s - %s\n"),dir,strerror(errno)); + exit(EXIT_FAILURE); + } + } } -- 2.47.2