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