]> git.ipfire.org Git - thirdparty/sarg.git/blame - email.c
Protect the reading of the configuration file against buffer overflows.
[thirdparty/sarg.git] / email.c
CommitLineData
25697a35 1/*
c37945ed
FM
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
94ff9470 4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
25697a35
GS
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"
5f3cfd1d 27#include "include/defs.h"
25697a35 28
32e71fa4 29int geramail(const char *dirname, int debug, const char *outdir, int userip, const char *email, const char *TempDir)
25697a35
GS
30{
31
32 FILE *fp_in, *fp_top1, *fp_top2, *fp_top3;
33 long long int ttnbytes=0, ttnacc=0, tnacc=0;
34 long long int tnbytes=0, ttnelap=0, tnelap=0;
35 float perc=0.00;
36 float perc2=0.00;
37 int posicao=0;
d6e703cc 38 char olduser[MAXLEN], csort[MAXLEN], period[MAXLEN], arqper[MAXLEN];
354c1a68 39 char wger[MAXLEN], top1[MAXLEN], top2[MAXLEN], top3[MAXLEN], user[MAXLEN], nacc[20], nbytes[20], url[1024], tusr[MAXLEN];
25697a35
GS
40 char ip[MAXLEN], hora[9], data[11], elap[15], user2[MAXLEN], wperc[8], wperc2[8];
41 char strip1[MAXLEN], strip2[MAXLEN], strip3[MAXLEN], strip4[MAXLEN], strip5[MAXLEN], strip6[MAXLEN], strip7[MAXLEN];
42 char incac[16], oucac[16];
43 int totuser=0;
44 time_t t;
45 struct tm *local;
456d78a5 46 int cstatus;
25697a35
GS
47
48 strcpy(wger,dirname);
49 strcpy(top1,dirname);
50 strcpy(top2,dirname);
51 strcpy(top3,dirname);
52 strcpy(tusr,dirname);
d6e703cc 53 strcat(wger,"/sarg-general");
25697a35
GS
54 strcat(top1,"/top");
55 strcat(top2,"/top.tmp");
56 strcat(top3,"/report");
d6e703cc 57 strcat(tusr,"/sarg-users");
25697a35
GS
58
59 if((fp_in=fopen(wger,"r"))==NULL) {
60 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],wger);
61 exit(1);
62 }
63
64 if((fp_top1=fopen(top1,"w"))==NULL) {
65 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
66 exit(1);
67 }
68
69 if((fp_top2=fopen(top2,"w"))==NULL) {
70 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top2);
71 exit(1);
72 }
73
74 fscanf(fp_in,"%s",user);
75 fscanf(fp_in,"%s",nacc);
76 fscanf(fp_in,"%s",nbytes);
77 fscanf(fp_in,"%s",url);
78 fscanf(fp_in,"%s",ip);
79 fscanf(fp_in,"%s",hora);
80 fscanf(fp_in,"%s",data);
81 fscanf(fp_in,"%s",elap);
82 fscanf(fp_in,"%s",incac);
83 fscanf(fp_in,"%s",oucac);
84
85 strcpy(olduser,user);
86 totuser=1;
87
88 while(!feof(fp_in))
89 {
90
91 if(strcmp(olduser,user) != 0)
92 {
93 if(strcmp(user,"TOTAL") != 0)
94 totuser++;
95
96#if defined(__FreeBSD__)
354c1a68 97 fprintf(fp_top2,"%s %15qu %15qu %15qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 98#elif defined(__alpha) || __ALPHA
354c1a68 99 fprintf(fp_top2,"%s %15ld %15ld %15ld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 100#else
354c1a68 101 fprintf(fp_top2,"%s %15lld %15lld %15lld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 102#endif
354c1a68 103 strcpy(olduser,user);
25697a35 104 ttnbytes=ttnbytes+tnbytes;
354c1a68
FM
105 ttnacc=ttnacc+tnacc;
106 ttnelap=ttnelap+tnelap;
25697a35
GS
107 tnbytes=0;
108 tnacc=0;
109 tnelap=0;
110 }
111
112 tnbytes=tnbytes+atol(nbytes);
113 tnacc=tnacc+atol(nacc);
114 tnelap=tnelap+atol(elap);
115
116 fscanf(fp_in,"%s",user);
117 fscanf(fp_in,"%s",nacc);
118 fscanf(fp_in,"%s",nbytes);
119 fscanf(fp_in,"%s",url);
120 fscanf(fp_in,"%s",ip);
121 fscanf(fp_in,"%s",hora);
122 fscanf(fp_in,"%s",data);
123 fscanf(fp_in,"%s",elap);
124 fscanf(fp_in,"%s",incac);
125 fscanf(fp_in,"%s",oucac);
126
127 if(strcmp(user,"TOTAL") == 0)
128 continue;
129 }
130#if defined(__FreeBSD__)
354c1a68 131 fprintf(fp_top2,"%s %15qu %15qu %15qu\n",olduser,tnbytes,tnacc,tnelap);
25697a35 132#elif defined(__alpha) || __ALPHA
354c1a68 133 fprintf(fp_top2,"%s %15ld %15ld %15ld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 134#else
354c1a68 135 fprintf(fp_top2,"%s %15lld %15lld %15lld\n",olduser,tnbytes,tnacc,tnelap);
25697a35 136#endif
25697a35
GS
137 ttnbytes=ttnbytes+tnbytes;
138 ttnacc=ttnacc+tnacc;
139 ttnelap=ttnelap+tnelap;
140
25697a35
GS
141 fclose(fp_in);
142 fclose(fp_top2);
143
94ff9470 144 sprintf(csort,"sort -n -T %s -r -k 2,2 -o '%s' '%s'", TempDir, top1, top2);
456d78a5
FM
145 cstatus=system(csort);
146 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
147 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
148 fprintf(stderr, "SARG: sort command: %s\n",csort);
149 exit(1);
150 }
25697a35
GS
151
152 unlink(top2);
153
154 if((fp_top1=fopen(top1,"a"))==NULL) {
155 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
156 exit(1);
157 }
354c1a68
FM
158#if defined(__FreeBSD__)
159 fprintf(fp_top1,"TOTAL %15qu %15qu %15qu\n",ttnbytes,ttnacc,ttnelap);
160#elif defined(__alpha) || __ALPHA
161 fprintf(fp_top1,"TOTAL %15ld %15ld %15ld\n",ttnbytes,ttnacc,ttnelap);
162#else
163 fprintf(fp_top1,"TOTAL %15lld %15lld %15lld\n",ttnbytes,ttnacc,ttnelap);
164#endif
25697a35
GS
165 fclose(fp_top1);
166
167 if((fp_top1=fopen(top1,"r"))==NULL) {
168 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top1);
169 exit(1);
170 }
171
172 if((fp_top3=fopen(top3,"w"))==NULL) {
173 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
174 exit(1);
175 }
176
177 /*
d6e703cc 178 * Obtem o period
25697a35
GS
179 */
180
181 strcpy(arqper,dirname);
d6e703cc 182 strcat(arqper,"/sarg-period");
25697a35
GS
183
184 if ((fp_in = fopen(arqper, "r")) == 0){
185 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],arqper);
186 exit(1);
187 }
188
d6e703cc 189 fgets(period,sizeof(period),fp_in);
25697a35
GS
190 fclose(fp_in);
191
192 sprintf(strip1,"%s",text[88]);
193 strip_latin(strip1);
354c1a68 194 fprintf(fp_top3,"%s\n",strip1);
25697a35
GS
195
196 sprintf(strip1,"%s",text[97]);
197 strip_latin(strip1);
354c1a68 198 fprintf(fp_top3,"%s\n",strip1);
25697a35
GS
199
200 sprintf(strip1,"%s",text[89]);
201 strip_latin(strip1);
354c1a68 202 fprintf(fp_top3,"%s %s\n\n",strip1,period);
25697a35
GS
203
204 sprintf(strip1,"%s",text[100]);
205 strip_latin(strip1);
206 sprintf(strip2,"%s",text[98]);
207 strip_latin(strip2);
208 sprintf(strip3,"%s",text[92]);
209 strip_latin(strip3);
210 sprintf(strip4,"%s",text[93]);
211 strip_latin(strip4);
212 sprintf(strip5,"%s",text[94]);
213 strip_latin(strip5);
214 sprintf(strip6,"%s",text[95]);
215 strip_latin(strip6);
216 sprintf(strip7,"%s",text[99]);
217 strip_latin(strip7);
218
354c1a68 219 fprintf(fp_top3,"%-7s %-20s %-8s %-15s %%%-6s %-10s %-10s %%%-7s\n------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",strip1,strip2,strip3,strip4,strip4,strip5,strip6,strip7);
25697a35
GS
220
221 fscanf(fp_top1,"%s",user);
222 fscanf(fp_top1,"%s",nbytes);
223 fscanf(fp_top1,"%s",nacc);
224 fscanf(fp_top1,"%s",elap);
225
226 while(!feof(fp_top1))
227 {
228
229 if(strstr(user,"_") != 0)
230 fixip(user);
231
232 strcpy(user2,user);
233
234 tnbytes=atol(nbytes);
235
236 if(tnbytes) {
237 perc=ttnbytes / 100;
238 perc=tnbytes / perc;
239 } else perc = 0;
240
241 if(atol(elap)) {
242 perc2=ttnelap / 100;
243 perc2=atol(elap) / perc2;
244 } else perc2 = 0;
245
246 posicao++;
247 tnelap=atol(elap);
248
249 sprintf(wperc,"%3.2f%%",perc);
250 sprintf(wperc2,"%3.2f%%",perc2);
251
354c1a68
FM
252 if(strstr(user,"TOTAL") != 0){
253 fputs("------- -------------------- -------- --------------- ------- ---------- ---------- -------\n",fp_top3);
25697a35 254#if defined(__FreeBSD__)
354c1a68 255 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 256#elif defined(__alpha) || __ALPHA
354c1a68 257 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 258#else
354c1a68 259 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",text[107]," ",ttnacc,fixnum(ttnbytes,1)," ",buildtime(ttnelap),ttnelap);
25697a35 260#endif
354c1a68 261 } else {
25697a35 262#if defined(__FreeBSD__)
354c1a68 263 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10qu %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
25697a35 264#elif defined(__alpha) || __ALPHA
354c1a68 265 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10ld %7s%%\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
25697a35 266#else
354c1a68 267 fprintf(fp_top3,"%7d %20s %8s %15s %7s %10s %10lld %7s\n",posicao,user2,nacc,fixnum(tnbytes,1),wperc,buildtime(tnelap),tnelap,wperc2);
25697a35
GS
268#endif
269 }
270
25697a35
GS
271 fscanf(fp_top1,"%s",user);
272 fscanf(fp_top1,"%s",nbytes);
273 fscanf(fp_top1,"%s",nacc);
274 fscanf(fp_top1,"%s",elap);
275 }
276
277 if(ttnbytes) {
278 tnbytes=ttnbytes / totuser;
279 } else tnbytes=0;
280
281 sprintf(strip1,"%s",text[96]);
282 strip_latin(strip1);
283#if defined(__FreeBSD__)
354c1a68 284 fprintf(fp_top3,"%-7s %20s %8qu %15s %8s %9s %10qu\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 285#elif defined(__alpha) || __ALPHA
354c1a68 286 fprintf(fp_top3,"%-7s %20s %8ld %15s %8s %9s %10ld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 287#else
354c1a68 288 fprintf(fp_top3,"%-7s %20s %8lld %15s %8s %9s %10lld\n",strip1," ",ttnacc/totuser,fixnum(tnbytes,1)," ",buildtime(ttnelap/totuser),ttnelap/totuser);
25697a35 289#endif
25697a35
GS
290
291 fclose(fp_top1);
292 unlink(top1);
293
294 t = time(NULL);
295 local = localtime(&t);
354c1a68 296 fprintf(fp_top3, "\n%s\n", asctime(local));
25697a35
GS
297
298 fclose(fp_top3);
299
300 if(strcmp(email,"stdout") == 0) {
301 if((fp_top3=fopen(top3,"r"))==NULL) {
302 fprintf(stderr, "SARG: (email) %s: %s\n",text[45],top3);
303 exit(1);
304 }
305
306 while(fgets(buf,sizeof(buf),fp_top3)!=NULL)
307 printf("%s",buf);
308 } else {
309 sprintf(buf,"%s -s 'SARG %s, %s' %s <%s",MailUtility,text[55],asctime(local),email,top3);
456d78a5
FM
310 cstatus=system(buf);
311 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
312 fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
313 fprintf(stderr, "SARG: command: %s\n",buf);
314 exit(1);
315 }
316 }
25697a35 317
d6e703cc 318 sprintf(csort,"rm -rf %s/sarg",TempDir);
25697a35
GS
319 system(csort);
320
321 return (0);
322}