]> git.ipfire.org Git - thirdparty/sarg.git/blob - siteuser.c
Ported r208 from branches/v2_2_7
[thirdparty/sarg.git] / siteuser.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2010
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 csort[255];
39 char general[MAXLEN];
40 char general2[MAXLEN];
41 char per[MAXLEN];
42 char sites[MAXLEN];
43 char report[MAXLEN];
44 char period[100];
45 int regs=0;
46 int ucount=0;
47 char *users;
48 long long int nbytes;
49 long long int obytes;
50 long long int nacc;
51 int cstatus;
52 struct getwordstruct gwarea;
53
54 if(Privacy)
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: (siteuser) %s: %s\n",text[45],per);
66 exit(1);
67 }
68
69 if (!fgets(period,sizeof(period),fp_in)) {
70 fprintf(stderr,"SARG: (siteuser) read error in %s\n",per);
71 exit(1);
72 }
73 fclose(fp_in);
74
75 sprintf(csort,"sort -k 4,4 -k 1,1 -o \"%s\" \"%s\"",general2,general);
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 }
82
83 if((fp_in=fopen(general2,"r"))==NULL) {
84 fprintf(stderr, "SARG: (topsite) %s: %s\n",text[8],general2);
85 fprintf(stderr, "SARG: sort command: %s\n",csort);
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
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);
95 css(fp_ou);
96 fputs("</head>\n",fp_ou);
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);
100
101 if(strcmp(IndexTree,"date") == 0)
102 show_sarg(fp_ou, "../../..");
103 else
104 show_sarg(fp_ou, "..");
105
106 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
107 fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title);
108
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]);
111 fputs("</table></div>\n",fp_ou);
112
113 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
114 fputs("<tr><td></td></tr>\n",fp_ou);
115 if(BytesInSitesUsersReport)
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]);
119
120 user[0]='\0';
121 ourl[0]='\0';
122 obytes=0;
123
124 if((users=(char *) malloc(204800))==NULL){
125 fprintf(stderr, "SARG: ERROR: %s",text[87]);
126 exit(1);
127 }
128 strcpy(users," ");
129
130 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
131 getword_start(&gwarea,buf);
132 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
133 printf("SARG: Maybe you have an invalid user in the %s file of the siteuser.\n",general2);
134 exit(1);
135 }
136 if(strcmp(user,"TOTAL") == 0)
137 continue;
138 if(userip)
139 fixip(user);
140
141 // get_usertab_name(user,name,sizeof(name));
142 user_find(name, user);
143
144 if(dotinuser && strchr(name,'_')) {
145 subs(name,sizeof(name),"_",".");
146 }
147
148 /*
149 In fact, even the first call is unecessary as the resolved user variable is never used.
150 if(Ip2Name)
151 ip2name(user,sizeof(user));
152 */
153
154 /*
155 Is this redundant ip2name a mistake or is it really necessary ? It definitely slow down sarg
156 if the first ip2name succeed because it will try to resolve a name which is not an IP but if
157 the first ip2name failed, the second attempt may find the expected address which was resolved
158 in the mean time by the DNS server.
159 if(Ip2Name)
160 ip2name(user,sizeof(user));
161 */
162
163 if (getword_atoll(&nacc,&gwarea,'\t')<0){
164 printf("SARG: Maybe you have an invalid number of access in your %s file of the siteuser.\n",general2);
165 exit(1);
166 }
167 if (nacc > 0) nsitesusers = 1;
168 if (getword_atoll(&nbytes,&gwarea,'\t')<0){
169 printf("SARG: Maybe you have an invalid number of bytes in your %s file of the siteuser.\n",general2);
170 exit(1);
171 }
172 if (getword(url,sizeof(url),&gwarea,'\t')<0) {
173 printf("SARG: Maybe you have an invalid url in your %s file of the siteuser.\n",general2);
174 exit(1);
175 }
176
177 if(!regs) {
178 strcpy(ourl,url);
179 obytes=nbytes;
180 regs++;
181 }
182
183 sprintf(wuser," %s ",name);
184 if(strstr(users,wuser) == 0 && strcmp(url,ourl) == 0) {
185 strcat(users,name);
186 strcat(users," ");
187 ucount++;
188 if(ucount>4) {
189 strcat(users,"<br>");
190 ucount=0;
191 }
192 }
193
194 if(SiteUsersReportLimit) {
195 if(regs >= SiteUsersReportLimit)
196 continue;
197 }
198
199 if(BlockIt[0]!='\0')
200 sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"../images/sarg-squidguard-block.png\" border=\"0\"></a>&nbsp;",wwwDocumentRoot,BlockIt,ourl);
201 else BlockImage[0]='\0';
202
203 if(strcmp(url,ourl) != 0 && nsitesusers) {
204 if(BytesInSitesUsersReport) {
205 sprintf(wwork2,"%s",fixnum(obytes,1));
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);
207 } else
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);
209 regs++;
210 ucount=0;
211 strcpy(users,name);
212 strcat(users," ");
213 strcpy(ourl,url);
214 obytes=nbytes;
215 }
216 }
217
218 if(nsitesusers) {
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);
220 }
221
222 unlink(general2);
223
224 fputs("</table>\n</div>\n",fp_ou);
225
226 show_info(fp_ou);
227
228 fputs("</body>\n</html>\n",fp_ou);
229
230 fclose(fp_in);
231 fclose(fp_ou);
232
233 if(users)
234 free(users);
235
236 return;
237
238 }