]> git.ipfire.org Git - thirdparty/sarg.git/blob - siteuser.c
b486c9f8703817b488051bf369d863fce887cabe
[thirdparty/sarg.git] / siteuser.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
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"
27 #include "include/defs.h"
28
29 void siteuser(void)
30 {
31
32 FILE *fp_in, *fp_ou;
33
34 char user[MAXLEN];
35 char url[MAXLEN];
36 char wuser[MAXLEN];
37 char ourl[MAXLEN];
38 char nacc[20];
39 char nbytes[20];
40 char obytes[20];
41 char csort[255];
42 char general[MAXLEN];
43 char general2[MAXLEN];
44 char per[MAXLEN];
45 char sites[MAXLEN];
46 char report[MAXLEN];
47 char period[100];
48 int regs=0;
49 int ucount=0;
50 char *users;
51 long long int llbytes=0;
52 int cstatus;
53
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);
62 sprintf(report,"%s/siteuser.html",dirname);
63
64 if ((fp_in = fopen(per, "r")) == 0) {
65 fprintf(stderr, "SARG: (topuser) %s: %s\n",text[45],per);
66 exit(1);
67 }
68
69 fgets(period,sizeof(period),fp_in);
70 fclose(fp_in);
71
72 sprintf(csort,"sort -k 4,4 -k 1,1 -o '%s' '%s'",general2,general);
73 cstatus=system(csort);
74 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
75 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
76 fprintf(stderr, "SARG: sort command: %s\n",csort);
77 exit(1);
78 }
79
80 if((fp_in=fopen(general2,"r"))==NULL) {
81 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general2);
82 fprintf(stderr, "SARG: sort command: %s\n",csort);
83 exit(1);
84 }
85
86 if((fp_ou=fopen(report,"w"))==NULL) {
87 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],report);
88 exit(1);
89 }
90
91 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);
92 css(fp_ou);
93 fputs("</head>\n",fp_ou);
94 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
95 fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
96 if(strlen(LogoImage) > 0) fprintf(fp_ou, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText);
97
98 if(strcmp(IndexTree,"date") == 0)
99 show_sarg(fp_ou, "../../..");
100 else
101 show_sarg(fp_ou, "..");
102
103 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ou);
104 fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title);
105
106 fprintf(fp_ou,"<tr><td class=\"header3\">%s: %s</td></tr>\n",text[89],period);
107 fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[85]);
108 fputs("</table></center>\n",fp_ou);
109
110 fputs("<center><table cellpadding=0 cellspacing=2>\n",fp_ou);
111 fputs("<tr><td></td></tr>\n",fp_ou);
112 if(strncmp(strlow(BytesInSitesUsersReport),"yes",3) == 0)
113 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]);
114 else
115 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]);
116
117 user[0]='\0';
118 ourl[0]='\0';
119 obytes[0]='\0';
120
121 if((users=(char *) malloc(204800))==NULL){
122 fprintf(stderr, "SARG: ERROR: %s",text[87]);
123 exit(1);
124 }
125 strcpy(users," ");
126
127 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
128 if (getword(user,sizeof(user),buf,' ')<0) {
129 printf("SARG: Maybe you have an invalid user in the %s file of the siteuser.\n",general2);
130 exit(1);
131 }
132 if(strcmp(user,"TOTAL") == 0)
133 continue;
134 if(userip)
135 fixip(user);
136
137 get_usertab_name(user,name,sizeof(name));
138
139 if(dotinuser && strchr(name,'_')) {
140 subs(name,sizeof(name),"_",".");
141 }
142
143 /*
144 In fact, even the first call is unecessary as the resolved user variable is never used.
145 if(strcmp(Ip2Name,"yes") == 0)
146 ip2name(user,sizeof(user));
147 */
148
149 /*
150 Is this redundant ip2name a mistake or is it really necessary ? It definitely slow down sarg
151 if the first ip2name succeed because it will try to resolve a name which is not an IP but if
152 the first ip2name failed, the second attempt may find the expected address which was resolved
153 in the mean time by the DNS server.
154 if(strcmp(Ip2Name,"yes") == 0)
155 ip2name(user,sizeof(user));
156 */
157
158 if (getword(nacc,sizeof(nacc),buf,' ')<0){
159 printf("SARG: Maybe you have an invalid number of access in your %s file of the siteuser.\n",general2);
160 exit(1);
161 }
162 if (atoi(nacc) > 0) nsitesusers = 1;
163 if (getword(nbytes,sizeof(nbytes),buf,' ')<0){
164 printf("SARG: Maybe you have an invalid number of bytes in your %s file of the siteuser.\n",general2);
165 exit(1);
166 }
167 if (getword(url,sizeof(url),buf,' ')<0) {
168 printf("SARG: Maybe you have an invalid url in your %s file of the siteuser.\n",general2);
169 exit(1);
170 }
171
172 if(!regs) {
173 strcpy(ourl,url);
174 strcpy(obytes,nbytes);
175 regs++;
176 }
177
178 sprintf(wuser," %s ",name);
179 if(strstr(users,wuser) == 0 && strcmp(url,ourl) == 0) {
180 strcat(users,name);
181 strcat(users," ");
182 ucount++;
183 if(ucount>4) {
184 strcat(users,"<br>");
185 ucount=0;
186 }
187 }
188
189 if(SiteUsersReportLimit) {
190 if(regs >= SiteUsersReportLimit)
191 continue;
192 }
193
194 if(strlen(BlockIt) > 0)
195 sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\" border=\"0\"></a>&nbsp;",wwwDocumentRoot,BlockIt,ourl);
196 else BlockImage[0]='\0';
197
198 if(strcmp(url,ourl) != 0 && nsitesusers) {
199 if(strncmp(strlow(BytesInSitesUsersReport),"yes",3) == 0) {
200 llbytes=my_atoll(obytes);
201 sprintf(wwork2,"%s",fixnum(llbytes,1));
202 fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\">%s</td><td class=\"data\">%s</td><td class=\"data2\">%s</td></tr>\n",regs,BlockImage,ourl,ourl,wwork2,users);
203 } else
204 fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\">%s<a href=\"http://%s\">%s</td><td class=\"data2\">%s</td></tr>\n",regs,BlockImage,ourl,ourl,users);
205 regs++;
206 ucount=0;
207 strcpy(users,name);
208 strcat(users," ");
209 strcpy(ourl,url);
210 strcpy(obytes,nbytes);
211 }
212 }
213
214 if(nsitesusers) {
215 fprintf(fp_ou,"<tr><td class=\"data\">%d</td><td class=\"data2\"><a href=\"http://%s\">%s</td><td class=\"data2\">%s</td></tr>\n",regs,ourl,ourl,users);
216 }
217
218 unlink(general2);
219
220 fputs("</table></center>\n",fp_ou);
221
222 show_info(fp_ou);
223
224 fputs("</body>\n</html>\n",fp_ou);
225
226 fclose(fp_in);
227 fclose(fp_ou);
228
229 if(users)
230 free(users);
231
232 return;
233
234 }