]> git.ipfire.org Git - thirdparty/sarg.git/blob - useragent.c
Protection against buffer overflows in getword and friends and report the origin...
[thirdparty/sarg.git] / useragent.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
28 void useragent()
29 {
30
31 FILE *fp_in = NULL, *fp_ou = NULL, *fp_ht = NULL;
32 char ip[MAXLEN], data[MAXLEN], agent[MAXLEN], user[MAXLEN];
33 char ipbefore[MAXLEN], namebefore[MAXLEN];
34 char tagent[MAXLEN];
35 char user_old[MAXLEN]="$#%0a3bc6";
36 char agent_old[MAXLEN]="$#%0a3bc6";
37 char hfile[MAXLEN];
38 char idate[MAXLEN], fdate[MAXLEN];
39 int agentot=0, agentot2=0, agentdif=0, cont=0;
40 unsigned long totregsl=0;
41
42 ip[0]='\0';
43 data[0]='\0';
44 agent[0]='\0';
45 user[0]='\0';
46 user_old[0]='\0';
47 agent_old[0]='\0';
48 ipbefore[0]='\0';
49 namebefore[0]='\0';
50
51 sprintf(hfile,"%s/%s/useragent.html", outdir,period);
52
53 sprintf(tmp3,"%s/squagent.unsort",TempDir);
54 sprintf(tmp2,"%s/squagent.log",TempDir);
55
56 if((fp_in=fopen(UserAgentLog,"r"))==NULL) {
57 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],UserAgentLog);
58 exit(1);
59 }
60
61 if((fp_ou=fopen(tmp3,"w"))==NULL) {
62 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],tmp3);
63 exit(1);
64 }
65
66 if(debug) {
67 sprintf(msg,"%s: %s",text[66],UserAgentLog);
68 debuga(msg);
69 }
70
71 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
72 totregsl++;
73 if (getword(ip,sizeof(ip),buf,' ')<0 || getword(data,sizeof(data),buf,'[')<0 ||
74 getword(data,sizeof(data),buf,' ')<0) {
75 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
76 exit(1);
77 }
78 if(totregsl == 1)
79 strcpy(idate,data);
80 if (getword(agent,sizeof(agent),buf,'"')<0 || getword(agent,sizeof(agent),buf,'"')<0) {
81 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
82 exit(1);
83 }
84 strcpy(warea,agent);
85 strup(warea);
86 if(strstr(warea,"SCRIPT") != 0 || strstr(warea,"ONLOAD") != 0)
87 baddata();
88
89 if(strlen(buf)) {
90 if (getword(user,sizeof(user),buf,' ')<0 || getword(user,sizeof(user),buf,'\n')<0) {
91 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserAgentLog);
92 exit(1);
93 }
94 }
95
96 if(user[0] == '-')
97 strcpy(user,ip);
98 if(strlen(user) == 0)
99 strcpy(user,ip);
100
101 sprintf(buf,"%s\\%s\\%s\\%s\\\n",ip,data,agent,user);
102 fputs(buf,fp_ou);
103 user[0]='\0';
104 }
105
106 strcpy(fdate,data);
107
108 if(debug) {
109 sprintf(msg, " %s: %ld",text[10],totregsl);
110 debuga(msg);
111 }
112
113 fclose(fp_in);
114 fclose(fp_ou);
115
116 if (fp_ht) {
117 fclose(fp_ht);
118 }
119
120 if(debug) {
121 sprintf(msg,"%s: %s",text[54],tmp2);
122 debuga(msg);
123 }
124
125 sprintf(csort,"sort -n -t '\\' -k 4,4 -k 3,3 -k 2,2 -k 1,1 -o '%s' '%s'",tmp2,tmp3);
126 system(csort);
127
128 unlink(tmp3);
129
130 if((fp_in=fopen(tmp2,"r"))==NULL) {
131 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
132 exit(1);
133 }
134
135 if((fp_ht=fopen(hfile,"w"))==NULL) {
136 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
137 exit(1);
138 }
139
140 if(debug)
141 debuga(text[72]);
142
143 fprintf(fp_ht, "<!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);
144 fputs("</head>\n",fp_ht);
145 if(strlen(FontFace) > 0) fprintf(fp_ht,"<font face=%s>\n",FontFace);
146 fprintf(fp_ht,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
147 if(strlen(LogoImage) > 0) fprintf(fp_ht, "<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);
148
149 if(strcmp(IndexTree,"date") == 0)
150 show_sarg(fp_ht, "../../..");
151 else
152 show_sarg(fp_ht,"..");
153 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht);
154 sprintf(html,"<tr><th align=center colspan=2><b><font color=%s size=+1>%s</font></b></th></tr>\n",TiColor,text[105]);
155 fputs(html,fp_ht);
156
157 fputs("<tr><td></td></tr><tr><td></td></tr>",fp_ht);
158 fputs("</table></center>\n",fp_ht);
159
160 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht);
161
162 sprintf(html,"<tr><td align=right bgcolor=%s><font size=%s>%s:</font><td align=left bgcolor=%s><font size=%s>%s - %s</font></td></td></tr>\n",HeaderBgColor,FontSize,text[89],TxBgColor,FontSize,idate,fdate);
163 fputs(html,fp_ht);
164
165 fputs("</table></center>\n",fp_ht);
166 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht);
167 fputs("<tr><td></td><td></td></tr>",fp_ht);
168
169 sprintf(html,"<tr><th align=left bgcolor=%s><font size=%s color=%s>%s</font></th><th bgcolor=%s align=left><font size=%s color=%s>%s</font></th></tr>\n",HeaderBgColor,FontSize,HeaderColor,text[98],HeaderBgColor,FontSize,HeaderColor,text[106]);
170 fputs(html,fp_ou);
171
172 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
173 if (getword(ip,sizeof(ip),buf,'\\')<0) {
174 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp2);
175 exit(1);
176 }
177
178 if(strcmp(Ip2Name,"yes") == 0) {
179 if(strcmp(ip,ipbefore) != 0) {
180 strcpy(ipbefore,ip);
181 ip2name(ip);
182 strcpy(namebefore,ip);
183 } else strcpy(ip,namebefore);
184 }
185
186 if (getword(data,sizeof(data),buf,'\\')<0 || getword(agent,sizeof(agent),buf,'\\')<0 ||
187 getword(user,sizeof(user),buf,'\\')<0) {
188 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp2);
189 exit(1);
190 }
191
192 if(strcmp(user,user_old) != 0) {
193 sprintf(html,"<tr><td align=left bgcolor=%s><font size=%s>%s</td><td align=left bgcolor=%s><font size=%s>%s</td></tr>\n",TxBgColor,FontSize,user,TxBgColor,FontSize,agent);
194 fputs(html,fp_ht);
195 strcpy(user_old,user);
196 strcpy(agent_old,agent);
197 } else {
198 if(strcmp(agent,agent_old) != 0) {
199 sprintf(html,"<tr><td></td><td align=left bgcolor=%s><font size=%s>%s</td></tr>\n",TxBgColor,FontSize,agent);
200 fputs(html,fp_ht);
201 strcpy(agent_old,agent);
202 }
203 }
204 }
205
206 fputs("</table>\n",fp_ht);
207 fclose(fp_in);
208 fclose(fp_ou);
209
210 sprintf(csort,"sort -n -t '\\' -k 3,3 -o '%s' '%s'",tmp3,tmp2);
211 system(csort);
212
213 unlink(tmp2);
214
215 if((fp_in=fopen(tmp3,"r"))==NULL) {
216 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp3);
217 exit(1);
218 }
219
220 if((fp_ou=fopen(tmp2,"w"))==NULL) {
221 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp2);
222 exit(1);
223 }
224
225 agent_old[0]='\0';
226
227 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
228 if (getword(ip,sizeof(ip),buf,'\\')<0 || getword(data,sizeof(data),buf,'\\')<0 ||
229 getword(agent,sizeof(agent),buf,'\\')<0) {
230 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
231 exit(1);
232 }
233
234 if(!cont) {
235 cont++;
236 strcpy(agent_old,agent);
237 }
238
239 agentot++;
240
241 if(strcmp(agent,agent_old) != 0) {
242 agentdif++;
243 sprintf(html,"%06d %s\n",agentot,agent_old);
244 fputs(html,fp_ou);
245 strcpy(agent_old,agent);
246 agentot2+=agentot;
247 agentot=1;
248 }
249 }
250 agentdif++;
251 sprintf(html,"%06d %s\n",agentot,agent);
252 fputs(html,fp_ou);
253 agentot2+=agentot;
254
255 fclose(fp_in);
256 fclose(fp_ou);
257
258 unlink(tmp3);
259
260 sprintf(csort,"sort -n -r -k 1,1 -o '%s' '%s'",tmp3,tmp2);
261 system(csort);
262
263 unlink(tmp2);
264
265 if((fp_in=fopen(tmp3,"r"))==NULL) {
266 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],tmp3);
267 exit(1);
268 }
269
270 if((fp_ht=fopen(hfile,"a"))==NULL) {
271 fprintf(stderr, "SARG: (useragent) %s: %s\n",text[45],hfile);
272 exit(1);
273 }
274
275 fputs("<br><br>\n",fp_ht);
276
277 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ht);
278 sprintf(html,"<tr><th align=left bgcolor=%s><font size=%s color=%s>%s</font></th><th bgcolor=%s align=left><font size=%s color=%s>%s</font></th><th align=center bgcolor=%s><font size=%s color=%s>%%</font></th></tr>\n",HeaderBgColor,FontSize,HeaderColor,text[106],HeaderBgColor,FontSize,HeaderColor,text[107],HeaderBgColor,FontSize,HeaderColor);
279 fputs(html,fp_ht);
280
281 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
282 buf[strlen(buf)-1]='\0';
283 if (getword(tagent,sizeof(tagent),buf,' ')<0) {
284 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
285 exit(1);
286 }
287 perc=atoi(tagent) * 100;
288 perc=perc / agentot2;
289
290 sprintf(html,"<tr><td align=left bgcolor=%s><font size=%s>%s</td><td align=right bgcolor=%s><font size=%s>%d</td><td align=right bgcolor=%s><font size=%s>%3.2f</td></tr>\n",TxBgColor,FontSize,buf,TxBgColor,FontSize,atoi(tagent),TxBgColor,FontSize,perc);
291 fputs(html,fp_ht);
292 }
293
294 fputs("</table></html>\n",fp_ht);
295 if(strcmp(ShowSargInfo,"yes") == 0) {
296 zdate(ftime, DateFormat);
297 sprintf(html,"<br><br><center><font size=-2>%s <a href='%s'>%s-%s</a> %s %s</font></center>\n",text[108],URL,PGM,VERSION,text[109],ftime);
298 fputs(html,fp_ht);
299 }
300
301 fputs("</table>\n</body>\n</html>\n",fp_ht);
302 fclose(fp_in);
303 fclose(fp_ht);
304
305 unlink(tmp3);
306
307 return;
308
309 }