]> git.ipfire.org Git - thirdparty/sarg.git/blame - realtime.c
Protect ip2name against buffer overflows and replace characters that are valid in...
[thirdparty/sarg.git] / realtime.c
CommitLineData
d5c1b1c1
GS
1/*
2 * AUTHOR: Pedro Lineu Orso orso@penguintech.com.br
3 * 1998, 2005
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
4bcb77cf 28int getdata(char *, FILE *);
d5c1b1c1 29void datashow(char *);
14c2eac6
GS
30void getlog();
31void header();
d5c1b1c1
GS
32
33char dat[128];
34char tim[128];
35char typ[128];
36char ouser[MAXLEN]="";
37char ourl[MAXLEN]="";
38
39void
40realtime(int argc, char *argv[])
41{
42
43 getlog();
44
45}
46
47void getlog()
48{
d6e703cc 49 FILE *tmp, *fp, *fp_usr;
d5c1b1c1
GS
50 char template1[255]="/var/tmp/sargtpl1.XXXXXX";
51 char template2[255]="/var/tmp/sargtpl2.XXXXXX";
52 char cmd[512];
53 char buf[512];
d6e703cc 54 int fd1,fd2,nreg;
456d78a5 55 int cstatus;
d6e703cc
FM
56
57 if(UserTabFile[0] != '\0') {
58 if(debug) {
59 sprintf(msg,"%s: %s",text[86],UserTabFile);
60 debuga(msg);
61 }
62 if((fp_usr=fopen(UserTabFile,"r"))==NULL) {
63 fprintf(stderr, "SARG: (realtime) %s: %s - %s\n",text[45],UserTabFile,strerror(errno));
64 exit(1);
65 }
66 nreg = lseek(fileno(fp_usr), 0, SEEK_END);
67 lseek(fileno(fp_usr), 0, 0);
68 if((userfile=(char *) malloc(nreg+100))==NULL){
69 fprintf(stderr, "SARG ERROR: %s",text[87]);
70 exit(1);
71 }
72 bzero(userfile,nreg+100);
73 strncat(userfile,":",1);
74 z1=0;
75 z2=1;
76 while(fgets(buf,MAXLEN,fp_usr)!=NULL){
77 buf[strlen(buf)-1]='\0';
78 if(strstr(buf,"\r") != 0) buf[strlen(buf)-1]='\0';
4bcb77cf
FM
79 if (getword(bufy,sizeof(bufy),buf,' ')<0) {
80 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",UserTabFile);
81 exit(1);
82 }
d6e703cc
FM
83 for(z1=0; z1<=strlen(bufy); z1++) {
84 userfile[z2]=bufy[z1];
85 z2++;
86 }
87 strncat(userfile,":",1);
88 for(z1=0; z1<=strlen(buf); z1++) {
89 userfile[z2]=buf[z1];
90 z2++;
91 }
92 strncat(userfile,":",1);
93 }
94 fclose(fp_usr);
95 }
d5c1b1c1
GS
96
97 fd1 = mkstemp(template1);
98 fd2 = mkstemp(template2);
99
100 if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */
101 fprintf(stderr, "SARG: (realtime) mkstemp error - %s\n",strerror(errno));
102 exit(1);
103 }
104
105 sprintf(cmd,"tail -%d %s",realtime_access_log_lines,AccessLog);
106 fp = popen(cmd, "r");
107 while(fgets(buf,sizeof(buf),fp) != NULL )
4bcb77cf
FM
108 if (getdata(buf,tmp)<0) {
109 printf("SARG: Maybe a broken record or garbage was returned by %s.\n",cmd);
110 exit(1);
111 }
d5c1b1c1
GS
112 pclose(fp);
113 fclose(tmp);
114
d6e703cc 115 sprintf(cmd,"sort -r -k 1,1 -k 2,2 -o %s %s",template2,template1);
456d78a5
FM
116 cstatus=system(cmd);
117 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
118 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
119 fprintf(stderr, "SARG: sort command: %s\n",cmd);
120 exit(1);
121 }
d5c1b1c1
GS
122 unlink(template1);
123 datashow(template2);
124}
125
4bcb77cf 126int getdata(char *rec, FILE *ftmp)
d5c1b1c1
GS
127{
128 time_t tt;
129 struct tm *t;
130
4bcb77cf
FM
131 if (getword3(dat,sizeof(dat),rec,' ')<0) {
132 return(-1);
133 }
134 if (getword3(warea,sizeof(warea),rec,' ')<0) {
135 return(-1);
136 }
d5c1b1c1 137 while(strcmp(warea,"") == 0 && strlen(rec) > 0)
4bcb77cf
FM
138 if (getword3(warea,sizeof(warea),rec,' ')<0) {
139 return(-1);
140 }
141 if (getword3(ip,sizeof(ip),rec,' ')<0) {
142 return(-1);
143 }
144 if (getword3(warea,sizeof(warea),rec,' ')<0) {
145 return(-1);
146 }
147 if (getword3(warea,sizeof(warea),rec,' ')<0) {
148 return(-1);
149 }
150 if (getword3(typ,sizeof(typ),rec,' ')<0) {
151 return(-1);
152 }
d5c1b1c1 153 if(strncmp(typ,"CONNECT",7) == 0) {
4bcb77cf
FM
154 if (getword3(url,sizeof(url),rec,' ')<0) {
155 return(-1);
156 }
157 if (getword3(user,sizeof(user),rec,' ')<0) {
158 return(-1);
159 }
d5c1b1c1 160 }else {
4bcb77cf
FM
161 if (getword3(url,sizeof(url),rec,'/')<0) {
162 return(-1);
163 }
164 if (getword3(url,sizeof(url),rec,'/')<0) {
165 return(-1);
166 }
167 if (getword3(url,sizeof(url),rec,'/')<0) {
168 return(-1);
169 }
170 if (getword3(user,sizeof(user),rec,' ')<0) {
171 return(-1);
172 }
173 if (getword3(user,sizeof(user),rec,' ')<0) {
174 return(-1);
175 }
d5c1b1c1
GS
176 }
177
178 if(strncmp(user,"-",1) == 0 && strcmp(RealtimeUnauthRec,"ignore") == 0)
4bcb77cf 179 return(0);
d5c1b1c1
GS
180
181 tt=atoi(dat);
182 t=localtime(&tt);
183 if(strncmp(DateFormat,"u",1) == 0)
184 strftime(tbuf, 127, "%Y-%m-%d %H:%M", t);
185 else if(strncmp(DateFormat,"e",1) == 0)
186 strftime(tbuf, 127, "%d-%m-%Y %H:%M", t);
187
188 sprintf(warea,"%s %s %s %s %s\n",tbuf,ip,user,url,typ);
189 fputs(warea,ftmp);
4bcb77cf 190 return(0);
d5c1b1c1
GS
191}
192
193void datashow(char *tmp)
194{
195 FILE *fin;
196 char buf[MAXLEN];
197
198 if((fin=fopen(tmp,"r"))==NULL) {
199 fprintf(stderr, "SARG: (realtime) open error %s - %s\n",tmp,strerror(errno));
200 exit(1);
201 }
202
203 header();
204
205 while(fgets(buf, MAXLEN, fin)) {
206 buf[strlen(buf)-1]='\0';
4bcb77cf
FM
207 if (getword3(dat,sizeof(dat),buf,' ')<0) {
208 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
209 exit(1);
210 }
211 if (getword3(tim,sizeof(tim),buf,' ')<0) {
212 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
213 exit(1);
214 }
215 if (getword3(ip,sizeof(ip),buf,' ')<0) {
216 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
217 exit(1);
218 }
219 if (getword3(user,sizeof(user),buf,' ')<0) {
220 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
221 exit(1);
222 }
d5c1b1c1 223 if(strlen(dat) < 3 || strlen(user) < 1) continue;
4bcb77cf
FM
224 if (getword3(url,sizeof(url),buf,' ')<0) {
225 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
226 exit(1);
227 }
228 if (getword3(typ,sizeof(typ),buf,' ')<0) {
229 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",tmp);
230 exit(1);
231 }
d5c1b1c1
GS
232 if(strstr(RealtimeTypes,typ) == 0)
233 continue;
234
235 if(strcmp(ouser,user) == 0 && strcmp(ourl,url) == 0)
236 continue;
237
d6e703cc
FM
238 strcpy(u2,user);
239 if(strcmp(Ip2Name,"yes") == 0)
a1c55d8c 240 ip2name(u2,sizeof(u2));
d6e703cc
FM
241 if(UserTabFile[0] != '\0') {
242 sprintf(warea,":%s:",u2);
243 if((str=(char *) strstr(userfile,warea)) != (char *) NULL ) {
244 z1=0;
245 str2=(char *) strstr(str+1,":");
246 str2++;
247 bzero(name, MAXLEN);
248 while(str2[z1] != ':') {
249 name[z1]=str2[z1];
250 z1++;
251 }
252 } else strcpy(name,u2);
253 } else strcpy(name,u2);
254
255 if(dotinuser && strstr(name,"_")) {
256 str2=(char *)subs(name,"_",".");
257 strcpy(name,str2);
258 }
259
260 printf("<tr><td class=\"data\">%s %s</td><td class=\"data3\">%s</td><td class=\"data3\">%s</td><td class=\"data3\">%s</td><td class=\"data2\"><a href=\"http://%s\">%s</td></tr>\n",dat,tim,ip,name,typ,url,url);
d5c1b1c1
GS
261 strcpy(ouser,user);
262 strcpy(ourl,url);
263 }
264
265 puts("</table>\n</html>\n");
266 fclose(fin);
267 unlink(tmp);
268 fflush(NULL);
269
270}
271
272void header()
273{
274 puts("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"");
275 puts(" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
276 puts("<html>\n");
277 puts("<head>\n");
278 if(realtime_refresh)
279 printf(" <meta http-equiv=refresh content=\"%d\" url=\"sarg-php/sarg-realtime.php\"; charset=\"%s\">\n",realtime_refresh,CharSet);
280 else
281 printf(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
282 css(stdout);
283 puts("</head>\n");
284 printf(buf,"<body style=\"font-family:%s;font-size:%s;background-color:%s;background-image:url(%s)\">\n",FontFace,TitleFontSize,BgColor,BgImage);
285 puts("<center><table cellpadding=\"1\" cellspacing=\"1\">\n");
286 printf("<tr><th class=\"title2\" colspan=\"10\">SARG %s</th></tr>\n",text[134]);
287 printf("<tr><th class=\"text\" colspan=\"10\">%s: %d s</th></tr>\n",text[136],realtime_refresh);
288 printf("<tr><th class=\"header3\">%s</th><th class=\"header3\">%s</th><th class=\"header3\">%s</th><th class=\"header3\">%s</th><th class=\"header\">%s</th></tr>\n",text[110],text[111],text[98],text[135],text[91]);
289}