]> git.ipfire.org Git - thirdparty/sarg.git/blob - repday.c
Protect ip2name against buffer overflows and replace characters that are valid in...
[thirdparty/sarg.git] / repday.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 extern numlist hours;
29
30 void report_day(char *user, int *iprel, int *ipuser)
31 {
32
33 FILE *fp_in, *fp_ou;
34
35 char data[20];
36 char odata[20];
37 char hour[20];
38 char elap[20];
39 char oelap[20];
40 char html[8000];
41 char arqout[MAXLEN];
42 char wdirname[MAXLEN];
43 char wuser[255];
44 char c[ 24 ][20];
45 int count=0;
46 int ihour=0;
47 long long int v[ 24 ] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
48 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
49 long long int t[ 24 ] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
50 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
51 long long int tt=0, ttt=0;
52 int i;
53
54 sprintf(arqout,"%s/%s/d%s.html",dirname,user,user);
55 sprintf(wdirname,"%s/%s.day",tmp,user);
56
57 if(access(wdirname, R_OK) != 0)
58 return;
59
60 if((fp_in=fopen(wdirname,"r"))==NULL) {
61 fprintf(stderr, "SARG: (repday) %s: %s\n",text[8],wdirname);
62 exit(1);
63 }
64
65 if((fp_ou=fopen(arqout,"w"))==NULL) {
66 fprintf(stderr, "SARG: (repday) %s: %s\n",text[8],arqout);
67 exit(1);
68 }
69
70 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);
71 css(fp_ou);
72 fputs("</head>\n",fp_ou);
73 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
74 fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
75 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);
76
77 if(strcmp(IndexTree,"date") == 0)
78 show_sarg(fp_ou, "../../../..");
79 else
80 show_sarg(fp_ou, "../..");
81
82 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ou);
83 sprintf(html,"<tr><th class=\"title\" colspan=2>%s</th></tr>\n",Title);
84 fputs(html,fp_ou);
85
86 fputs("</table></center>\n",fp_ou);
87 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ou);
88
89 sprintf(html,"<tr><td class=\"header3\" colspan=\"2\">%s:&nbsp;%s</td></tr>\n",text[89],period);
90 fputs(html,fp_ou);
91
92 strcpy(wuser,user);
93 if(strstr(wuser,"_") != 0)
94 fixip(wuser);
95
96 if(strcmp(Ip2Name,"yes") == 0)
97 if((str=(char *) strstr(name, ".")) != (char *) NULL) {
98 if((str=(char *) strstr(str+1, ".")) != (char *) NULL)
99 ip2name(wuser,sizeof(wuser));
100 }
101
102 if(UserTabFile[0] != '\0') {
103 sprintf(warea,":%s:",wuser);
104 if((str=(char *) strstr(userfile,warea)) != (char *) NULL ) {
105 z1=0;
106 str2=(char *) strstr(str+1,":");
107 str2++;
108 bzero(name, MAXLEN);
109 while(str2[z1] != ':') {
110 name[z1]=str2[z1];
111 z1++;
112 }
113 } else strcpy(name,wuser);
114 } else strcpy(name,user);
115
116 if(dotinuser && strstr(name,"_")) {
117 str2=(char *)subs(name,"_",".");
118 strcpy(name,str2);
119 }
120
121 sprintf(html,"<tr><th class=\"header3\" colspan=\"2\">%s:&nbsp;%s</th></tr>\n",text[90],name);
122 fputs(html,fp_ou);
123
124 fputs("<tr><td></td></tr><tr><td></td></tr>\n",fp_ou);
125 fputs("<tr><td></td></tr><tr><td></td></tr></table>\n",fp_ou);
126
127 fputs("<table cellpadding=0 cellspacing=2>\n", fp_ou);
128
129 fputs( "<tr><td border=0></td>\n", fp_ou );
130
131 if(strcmp(datetimeby,"bytes") == 0)
132 strcpy( html, text[93] );
133 else
134 strcpy( html, "H:M:S" );
135
136 for( i = 0; i < hours.len; i++ )
137 fprintf( fp_ou,
138 "<td class=\"header3\">%02dH<br>%s</td>\n", hours.list[ i ], html );
139 fprintf( fp_ou,
140 "<td class=\"header3\">%s<br>%s</td></tr>\n", text[107], html );
141
142 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
143 if (getword(data,sizeof(data),buf,' ')<0) {
144 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdirname);
145 exit(1);
146 }
147 if(!count) {
148 strcpy(odata,data);
149 count++;
150 }
151
152 if (getword(hour,sizeof(hour),buf,' ')<0 || getword(elap,sizeof(elap),buf,' ')<0) {
153 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",wdirname);
154 exit(1);
155 }
156 elap[strlen(elap)-1]='\0';
157
158 if(strcmp(data,odata) != 0) {
159 strcpy(oelap,elap);
160
161 for( i = 0; i < hours.len; i++ )
162 sprintf(c[ hours.list[ i ] ],"%s",fixtime(v[ hours.list[ i ] ]));
163
164 for( i = 8; i <= 17; i++ )
165 if(strcmp(c[ hours.list[ i ] ],"00:00:00") == 0)
166 bzero(c[ hours.list[ i ] ],12);
167
168 fprintf( fp_ou,
169 "<tr><td class=\"data\">%s</td>\n", odata );
170 for( i = 0; i < hours.len; i++ )
171 fprintf( fp_ou,
172 "<td class=\"data\">%s</td>\n", c[ hours.list[ i ] ] );
173 fprintf( fp_ou,
174 "<td class=\"data\">%s</td>\n", fixtime(tt) );
175
176 tt=0;
177 for( i = 0; i < hours.len; i++ ) v[ hours.list[ i ] ]=0;
178 strcpy(odata,data);
179 strcpy(elap,oelap);
180 }
181
182 ihour=atoi(hour);
183
184 v[ ihour ]+=my_atoll(elap);
185 tt+=my_atoll(elap);
186 t[ ihour ]+=my_atoll(elap);
187 ttt+=my_atoll(elap);
188
189 }
190
191 for( i = 0; i < hours.len; i++ )
192 sprintf(c[ hours.list[ i ] ],"%s",fixtime(v[ hours.list[ i ] ]));
193
194 for( i = 0; i < hours.len; i++ )
195 if(strcmp(c[ hours.list[ i ] ],"00:00:00") == 0) bzero(c[ hours.list[ i ] ],12);
196
197 fprintf( fp_ou,
198 "<tr><td class=\"data\">%s</td>\n", data );
199 for( i = 0; i < hours.len; i++ )
200 fprintf( fp_ou,
201 "<td class=\"data\">%s</td>\n", c[ hours.list[ i ] ] );
202 fprintf( fp_ou,
203 "<td class=\"data\">%s</td></tr>\n", fixtime(tt) );
204
205 for( i = 0; i < hours.len; i++ )
206 sprintf(c[ hours.list[ i ] ],"%s",fixtime(t[ hours.list[ i ] ]));
207
208 fprintf( fp_ou,
209 "<tr><td class=\"header\">%s</td>\n", text[107] );
210 for( i = 0; i < hours.len; i++ )
211 fprintf( fp_ou,
212 "<td class=\"header2\">%s</td>\n", c[ hours.list[ i ] ] );
213 fprintf( fp_ou,
214 "<td class=\"header2\">%s</td></tr>\n", fixtime(ttt) );
215
216 fputs("</body>\n</html>\n",fp_ou);
217
218 show_info(fp_ou);
219
220 fclose(fp_in);
221 fclose(fp_ou);
222 return;
223 }