]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
c37945ed | 2 | * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com |
e3af0ae9 | 3 | * 1998, 2009 |
94ff9470 | 4 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
25697a35 GS |
5 | * |
6 | * SARG donations: | |
7 | * please look at http://sarg.sourceforge.net/donations.php | |
8 | * --------------------------------------------------------------------- | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
23 | * | |
24 | */ | |
25 | ||
26 | #include "include/conf.h" | |
5f3cfd1d | 27 | #include "include/defs.h" |
25697a35 | 28 | |
32e71fa4 | 29 | void siteuser(void) |
25697a35 GS |
30 | { |
31 | ||
32 | FILE *fp_in, *fp_ou; | |
48864d28 | 33 | |
25697a35 GS |
34 | char user[MAXLEN]; |
35 | char url[MAXLEN]; | |
36 | char wuser[MAXLEN]; | |
37 | char ourl[MAXLEN]; | |
38 | char nacc[20]; | |
39 | char nbytes[20]; | |
d6e703cc | 40 | char obytes[20]; |
25697a35 | 41 | char csort[255]; |
d6e703cc FM |
42 | char general[MAXLEN]; |
43 | char general2[MAXLEN]; | |
25697a35 | 44 | char per[MAXLEN]; |
25697a35 GS |
45 | char sites[MAXLEN]; |
46 | char report[MAXLEN]; | |
d6e703cc | 47 | char period[100]; |
25697a35 GS |
48 | int regs=0; |
49 | int ucount=0; | |
50 | char *users; | |
d6e703cc | 51 | long long int llbytes=0; |
456d78a5 | 52 | int cstatus; |
25697a35 | 53 | |
d6e703cc FM |
54 | if(strcmp(Privacy,"yes") == 0) |
55 | return; | |
56 | ||
57 | nsitesusers = 0; | |
58 | sprintf(general,"%s/sarg-general",dirname); | |
59 | sprintf(sites,"%s/sarg-sites",dirname); | |
60 | sprintf(general2,"%s/sarg-general2",dirname); | |
61 | sprintf(per,"%s/sarg-period",dirname); | |
25697a35 GS |
62 | sprintf(report,"%s/siteuser.html",dirname); |
63 | ||
64 | if ((fp_in = fopen(per, "r")) == 0) { | |
05b90947 | 65 | fprintf(stderr, "SARG: (siteuser) %s: %s\n",text[45],per); |
25697a35 GS |
66 | exit(1); |
67 | } | |
68 | ||
05b90947 FM |
69 | if (!fgets(period,sizeof(period),fp_in)) { |
70 | fprintf(stderr,"SARG: (siteuser) read error in %s\n",per); | |
71 | exit(1); | |
72 | } | |
25697a35 GS |
73 | fclose(fp_in); |
74 | ||
9a2efbd0 | 75 | sprintf(csort,"sort -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general); |
456d78a5 FM |
76 | cstatus=system(csort); |
77 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
78 | fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus)); | |
79 | fprintf(stderr, "SARG: sort command: %s\n",csort); | |
80 | exit(1); | |
81 | } | |
25697a35 | 82 | |
d6e703cc FM |
83 | if((fp_in=fopen(general2,"r"))==NULL) { |
84 | fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general2); | |
456d78a5 | 85 | fprintf(stderr, "SARG: sort command: %s\n",csort); |
25697a35 GS |
86 | exit(1); |
87 | } | |
88 | ||
89 | if((fp_ou=fopen(report,"w"))==NULL) { | |
90 | fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],report); | |
91 | exit(1); | |
92 | } | |
93 | ||
d6e703cc | 94 | 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); |
25697a35 GS |
95 | css(fp_ou); |
96 | fputs("</head>\n",fp_ou); | |
dfb337be FM |
97 | //if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=\"%s\">\n",FontFace); |
98 | fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage); | |
99 | write_logo_image(fp_ou); | |
25697a35 | 100 | |
491b862f GS |
101 | if(strcmp(IndexTree,"date") == 0) |
102 | show_sarg(fp_ou, "../../.."); | |
103 | else | |
104 | show_sarg(fp_ou, ".."); | |
105 | ||
dfb337be | 106 | fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou); |
354c1a68 | 107 | fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title); |
25697a35 | 108 | |
354c1a68 FM |
109 | fprintf(fp_ou,"<tr><td class=\"header3\">%s: %s</td></tr>\n",text[89],period); |
110 | fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[85]); | |
dfb337be | 111 | fputs("</table></div>\n",fp_ou); |
25697a35 | 112 | |
dfb337be | 113 | fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou); |
25697a35 | 114 | fputs("<tr><td></td></tr>\n",fp_ou); |
d6e703cc | 115 | if(strncmp(strlow(BytesInSitesUsersReport),"yes",3) == 0) |
354c1a68 FM |
116 | 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]); |
117 | else | |
118 | 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]); | |
dfb337be | 119 | |
25697a35 GS |
120 | user[0]='\0'; |
121 | ourl[0]='\0'; | |
d6e703cc | 122 | obytes[0]='\0'; |
25697a35 GS |
123 | |
124 | if((users=(char *) malloc(204800))==NULL){ | |
125 | fprintf(stderr, "SARG: ERROR: %s",text[87]); | |
126 | exit(1); | |
127 | } | |
936c9905 | 128 | strcpy(users," "); |
25697a35 GS |
129 | |
130 | while(fgets(buf,sizeof(buf),fp_in)!=NULL) { | |
120d768c | 131 | if (getword(user,sizeof(user),buf,'\t')<0) { |
4bcb77cf FM |
132 | printf("SARG: Maybe you have an invalid user in the %s file of the siteuser.\n",general2); |
133 | exit(1); | |
134 | } | |
25697a35 GS |
135 | if(strcmp(user,"TOTAL") == 0) |
136 | continue; | |
137 | if(userip) | |
138 | fixip(user); | |
139 | ||
e3af0ae9 PO |
140 | // get_usertab_name(user,name,sizeof(name)); |
141 | user_find(name, user); | |
936c9905 FM |
142 | |
143 | if(dotinuser && strchr(name,'_')) { | |
48864d28 | 144 | subs(name,sizeof(name),"_","."); |
94ff9470 | 145 | } |
25697a35 | 146 | |
936c9905 FM |
147 | /* |
148 | In fact, even the first call is unecessary as the resolved user variable is never used. | |
25697a35 | 149 | if(strcmp(Ip2Name,"yes") == 0) |
a1c55d8c | 150 | ip2name(user,sizeof(user)); |
936c9905 | 151 | */ |
25697a35 | 152 | |
936c9905 FM |
153 | /* |
154 | Is this redundant ip2name a mistake or is it really necessary ? It definitely slow down sarg | |
155 | if the first ip2name succeed because it will try to resolve a name which is not an IP but if | |
156 | the first ip2name failed, the second attempt may find the expected address which was resolved | |
157 | in the mean time by the DNS server. | |
25697a35 | 158 | if(strcmp(Ip2Name,"yes") == 0) |
a1c55d8c | 159 | ip2name(user,sizeof(user)); |
936c9905 | 160 | */ |
25697a35 | 161 | |
120d768c | 162 | if (getword(nacc,sizeof(nacc),buf,'\t')<0){ |
4bcb77cf FM |
163 | printf("SARG: Maybe you have an invalid number of access in your %s file of the siteuser.\n",general2); |
164 | exit(1); | |
165 | } | |
d6e703cc | 166 | if (atoi(nacc) > 0) nsitesusers = 1; |
120d768c | 167 | if (getword(nbytes,sizeof(nbytes),buf,'\t')<0){ |
4bcb77cf FM |
168 | printf("SARG: Maybe you have an invalid number of bytes in your %s file of the siteuser.\n",general2); |
169 | exit(1); | |
170 | } | |
120d768c | 171 | if (getword(url,sizeof(url),buf,'\t')<0) { |
4bcb77cf FM |
172 | printf("SARG: Maybe you have an invalid url in your %s file of the siteuser.\n",general2); |
173 | exit(1); | |
174 | } | |
25697a35 GS |
175 | |
176 | if(!regs) { | |
177 | strcpy(ourl,url); | |
d6e703cc | 178 | strcpy(obytes,nbytes); |
25697a35 GS |
179 | regs++; |
180 | } | |
dfb337be | 181 | |
25697a35 GS |
182 | sprintf(wuser," %s ",name); |
183 | if(strstr(users,wuser) == 0 && strcmp(url,ourl) == 0) { | |
184 | strcat(users,name); | |
185 | strcat(users," "); | |
186 | ucount++; | |
187 | if(ucount>4) { | |
188 | strcat(users,"<br>"); | |
189 | ucount=0; | |
190 | } | |
191 | } | |
192 | ||
193 | if(SiteUsersReportLimit) { | |
194 | if(regs >= SiteUsersReportLimit) | |
195 | continue; | |
196 | } | |
197 | ||
dfb337be | 198 | if(BlockIt[0]!='\0') |
25697a35 GS |
199 | sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\" border=\"0\"></a> ",wwwDocumentRoot,BlockIt,ourl); |
200 | else BlockImage[0]='\0'; | |
201 | ||
d6e703cc FM |
202 | if(strcmp(url,ourl) != 0 && nsitesusers) { |
203 | if(strncmp(strlow(BytesInSitesUsersReport),"yes",3) == 0) { | |
204 | llbytes=my_atoll(obytes); | |
205 | sprintf(wwork2,"%s",fixnum(llbytes,1)); | |
dfb337be | 206 | 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); |
354c1a68 | 207 | } else |
dfb337be | 208 | 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); |
25697a35 GS |
209 | regs++; |
210 | ucount=0; | |
354c1a68 | 211 | strcpy(users,name); |
25697a35 GS |
212 | strcat(users," "); |
213 | strcpy(ourl,url); | |
d6e703cc | 214 | strcpy(obytes,nbytes); |
25697a35 GS |
215 | } |
216 | } | |
217 | ||
d6e703cc | 218 | if(nsitesusers) { |
dfb337be | 219 | 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); |
d6e703cc | 220 | } |
25697a35 | 221 | |
d6e703cc | 222 | unlink(general2); |
25697a35 | 223 | |
dfb337be | 224 | fputs("</table>\n</div>\n",fp_ou); |
25697a35 GS |
225 | |
226 | show_info(fp_ou); | |
227 | ||
228 | fputs("</body>\n</html>\n",fp_ou); | |
dfb337be | 229 | |
25697a35 GS |
230 | fclose(fp_in); |
231 | fclose(fp_ou); | |
232 | ||
491b862f GS |
233 | if(users) |
234 | free(users); | |
235 | ||
25697a35 GS |
236 | return; |
237 | ||
238 | } |