/*
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
- * 1998, 2010
+ * 1998, 2011
*
* SARG donations:
* please look at http://sarg.sourceforge.net/donations.php
void siteuser(void)
{
-
- FILE *fp_in, *fp_ou;
-
- char user[MAXLEN];
- char url[MAXLEN];
- char wuser[MAXLEN];
- char ourl[MAXLEN];
- char csort[255];
- char general[MAXLEN];
- char general2[MAXLEN];
- char per[MAXLEN];
- char sites[MAXLEN];
- char report[MAXLEN];
- char period[100];
- int regs=0;
- int ucount=0;
- char *users;
- long long int nbytes;
- long long int obytes;
- long long int nacc;
- int cstatus;
- struct getwordstruct gwarea;
-
- if(Privacy)
- return;
-
- nsitesusers = 0;
- sprintf(general,"%s/sarg-general",dirname);
- sprintf(sites,"%s/sarg-sites",dirname);
- sprintf(general2,"%s/sarg-general2",dirname);
- sprintf(per,"%s/sarg-period",dirname);
- sprintf(report,"%s/siteuser.html",dirname);
-
- if ((fp_in = fopen(per, "r")) == 0) {
- fprintf(stderr, "SARG: (siteuser) %s: %s\n",text[45],per);
- exit(1);
- }
-
- if (!fgets(period,sizeof(period),fp_in)) {
- fprintf(stderr,"SARG: (siteuser) read error in %s\n",per);
- exit(1);
- }
- fclose(fp_in);
-
- sprintf(csort,"sort -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general);
- cstatus=system(csort);
- if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
- fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
- fprintf(stderr, "SARG: sort command: %s\n",csort);
- exit(1);
- }
-
- if((fp_in=fopen(general2,"r"))==NULL) {
- fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general2);
- fprintf(stderr, "SARG: sort command: %s\n",csort);
- exit(1);
- }
-
- if((fp_ou=fopen(report,"w"))==NULL) {
- fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],report);
- exit(1);
- }
-
- fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
- css(fp_ou);
- fputs("</head>\n",fp_ou);
- //if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=\"%s\">\n",FontFace);
- fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
- write_logo_image(fp_ou);
-
- if(strcmp(IndexTree,"date") == 0)
- show_sarg(fp_ou, "../../..");
- else
- show_sarg(fp_ou, "..");
-
- fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
- fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title);
-
- fprintf(fp_ou,"<tr><td class=\"header3\">%s: %s</td></tr>\n",text[89],period);
- fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[85]);
- fputs("</table></div>\n",fp_ou);
-
- fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
- fputs("<tr><td></td></tr>\n",fp_ou);
- if(BytesInSitesUsersReport)
- fprintf(fp_ou,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th></tr>\n",text[100],text[91],text[93],text[103]);
- else
- fprintf(fp_ou,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th></tr>\n",text[100],text[91],text[103]);
-
- user[0]='\0';
- ourl[0]='\0';
- obytes=0;
-
- if((users=(char *) malloc(204800))==NULL){
- fprintf(stderr, "SARG: ERROR: %s",text[87]);
- exit(1);
- }
- strcpy(users," ");
-
- while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
- getword_start(&gwarea,buf);
- if (getword(user,sizeof(user),&gwarea,'\t')<0) {
- printf("SARG: Maybe you have an invalid user in the %s file of the siteuser.\n",general2);
- exit(1);
- }
- if(strcmp(user,"TOTAL") == 0)
- continue;
- if(userip)
- fixip(user);
-
- get_usertab_name(user,name,sizeof(name));
-
- if(dotinuser && strchr(name,'_')) {
- subs(name,sizeof(name),"_",".");
- }
-
- /*
- In fact, even the first call is unecessary as the resolved user variable is never used.
- if(Ip2Name)
- ip2name(user,sizeof(user));
- */
-
- /*
- Is this redundant ip2name a mistake or is it really necessary ? It definitely slow down sarg
- if the first ip2name succeed because it will try to resolve a name which is not an IP but if
- the first ip2name failed, the second attempt may find the expected address which was resolved
- in the mean time by the DNS server.
- if(Ip2Name)
- ip2name(user,sizeof(user));
- */
-
- if (getword_atoll(&nacc,&gwarea,'\t')<0){
- printf("SARG: Maybe you have an invalid number of access in your %s file of the siteuser.\n",general2);
- exit(1);
- }
- if (nacc > 0) nsitesusers = 1;
- if (getword_atoll(&nbytes,&gwarea,'\t')<0){
- printf("SARG: Maybe you have an invalid number of bytes in your %s file of the siteuser.\n",general2);
- exit(1);
- }
- if (getword(url,sizeof(url),&gwarea,'\t')<0) {
- printf("SARG: Maybe you have an invalid url in your %s file of the siteuser.\n",general2);
- exit(1);
- }
-
- if(!regs) {
- strcpy(ourl,url);
- obytes=nbytes;
- regs++;
- }
-
- sprintf(wuser," %s ",name);
- if(strstr(users,wuser) == 0 && strcmp(url,ourl) == 0) {
- strcat(users,name);
- strcat(users," ");
- ucount++;
- if(ucount>4) {
- strcat(users,"<br>");
- ucount=0;
- }
- }
-
- if(SiteUsersReportLimit) {
- if(regs >= SiteUsersReportLimit)
- continue;
- }
-
- if(BlockIt[0]!='\0')
- sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\" border=\"0\"></a> ",wwwDocumentRoot,BlockIt,ourl);
- else BlockImage[0]='\0';
-
- if(strcmp(url,ourl) != 0 && nsitesusers) {
- if(BytesInSitesUsersReport) {
- sprintf(wwork2,"%s",fixnum(obytes,1));
- fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\">%s</a></td><td class=\"data\">%s</td><td class=\"data2\">%s</td></tr>\n",regs,BlockImage,ourl,ourl,wwork2,users);
- } else
- fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",regs,BlockImage,ourl,ourl,users);
- regs++;
- ucount=0;
- strcpy(users,name);
- strcat(users," ");
- strcpy(ourl,url);
- obytes=nbytes;
- }
- }
-
- if(nsitesusers) {
- fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\"><a href=\"http://%s\">%s</a></td><td class=\"data2\">%s</td></tr>\n",regs,ourl,ourl,users);
- }
-
- unlink(general2);
-
- fputs("</table>\n</div>\n",fp_ou);
-
- show_info(fp_ou);
-
- fputs("</body>\n</html>\n",fp_ou);
-
- fclose(fp_in);
- fclose(fp_ou);
-
- 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);
+
+ if (snprintf(csort,sizeof(csort),"sort -t \"\t\" -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general)>=sizeof(csort)) {
+ debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),general,general2);
+ 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(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("<tr><td class=\"header_c\">",fp_ou);
+ fprintf(fp_ou,_("Period: %s"),period.html);
+ fputs("</td></tr>\n",fp_ou);
+ fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Sites & Users"));
+ close_html_header(fp_ou);
+
+ fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\"",fp_ou);
+ if (SortTableJs[0]) fputs(" class=\"sortable\"",fp_ou);
+ fprintf(fp_ou,">\n<thead><tr><th class=\"header_l\">%s</th><th class=\"header_l",_("NUM"));
+ if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
+ fprintf(fp_ou,"\">%s</th>",_("ACCESSED SITE"));
+ if(BytesInSitesUsersReport)
+ fprintf(fp_ou,"<th class=\"header_l\">%s</th>",_("BYTES"));
+ fputs("<th class=\"header_l",fp_ou);
+ if (SortTableJs[0]) fputs(" sorttable_alpha",fp_ou);
+ fprintf(fp_ou,"\">%s</th></tr></thead>\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,"<br>");
+ ucount=0;
+ }
+ }
+
+ if(SiteUsersReportLimit) {
+ if(regs >= SiteUsersReportLimit)
+ continue;
+ }
+
+ if(strcmp(item.url,ourl) != 0 && nsitesusers) {
+ fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">",regs);
+ if(BlockIt[0]!='\0' && ourl[0]!=ALIAS_PREFIX) {
+ fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
+ output_html_url(fp_ou,ourl);
+ fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou);
+ }
+ output_html_link(fp_ou,ourl,100);
+ fputs("</td>",fp_ou);
+
+ if (BytesInSitesUsersReport) {
+ fputs("<td class=\"data\"",fp_ou);
+ if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(int64_t)obytes);
+ fprintf(fp_ou,">%s</td>",fixnum(obytes,1));
+ }
+ fprintf(fp_ou,"<td class=\"data2\">%s</td></tr>\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,"<tr><td class=\"data\">%d</td><td class=\"data2\">",regs);
+ if(BlockIt[0]!='\0' && ourl[0]!=ALIAS_PREFIX) {
+ fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
+ output_html_url(fp_ou,ourl);
+ fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a> ",fp_ou);
+ }
+ output_html_link(fp_ou,ourl,100);
+ fputs("</td>",fp_ou);
+ if (BytesInSitesUsersReport) {
+ fputs("<td class=\"data\"",fp_ou);
+ if (SortTableJs[0]) fprintf(fp_ou," sorttable_customkey=\"%"PRId64"\"",(int64_t)obytes);
+ fprintf(fp_ou,">%s</td>",fixnum(obytes,1));
+ }
+ fprintf(fp_ou,"<td class=\"data2\">%s</td></tr>\n",users);
+ }
+ if (ourl) free(ourl);
+
+ if (unlink(general2)) {
+ debuga(_("Cannot delete %s - %s\n"),general2,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ fputs("</table></div>\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;
}