]> git.ipfire.org Git - thirdparty/sarg.git/blob - download.c
Ported r225 from branches/v2_2_7 (remove Pedro Orso's mail from headers)
[thirdparty/sarg.git] / download.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 static char *DownloadSuffix=NULL;
31 static char **DownloadSuffixIndex=NULL;
32 static int NDownloadSuffix=0;
33
34 void download_report(void)
35 {
36
37 FILE *fp_in = NULL, *fp_ou = NULL;
38
39 char url[MAXLEN];
40 char report_in[MAXLEN];
41 char wdirname[MAXLEN];
42 char report[MAXLEN];
43 char period[100];
44 char ip[MAXLEN];
45 char oip[MAXLEN];
46 char user[MAXLEN];
47 char ouser[MAXLEN];
48 char ouser2[MAXLEN];
49 char data[15];
50 char hora[15];
51 char *str;
52 int z=0;
53 int count=0;
54 int i;
55 struct getwordstruct gwarea;
56
57 ouser[0]='\0';
58 ouser2[0]='\0';
59
60 sprintf(report_in,"%s/sarg/download.log",TempDir);
61 if(access(report_in, R_OK) != 0)
62 return;
63
64 strcpy(wdirname,dirname);
65 sprintf(report,"%s/download.html",wdirname);
66 strcat(wdirname,"/");
67 strcat(wdirname,"sarg-period");
68
69 if ((fp_in = fopen(wdirname, "r")) == 0) {
70 fprintf(stderr, "SARG: (download) %s: %s\n",text[45],wdirname);
71 exit(1);
72 }
73
74 if (!fgets(period,sizeof(period),fp_in)) {
75 fprintf(stderr,"SARG: (download) read error in %s\n",wdirname);
76 exit(1);
77 }
78 fclose(fp_in);
79
80 if((fp_in=MY_FOPEN(report_in,"r"))==NULL) {
81 fprintf(stderr, "SARG: (download) %s: %s\n",text[8],report_in);
82 exit(1);
83 }
84
85 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
86 fprintf(stderr, "SARG: (download) %s: %s\n",text[8],report);
87 exit(1);
88 }
89
90 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);
91 css(fp_ou);
92 fputs("</head>\n",fp_ou);
93
94 fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
95
96 write_logo_image(fp_ou);
97
98 if(IndexTree == INDEX_TREE_DATE)
99 show_sarg(fp_ou, "../../..");
100 else
101 show_sarg(fp_ou, "..");
102
103 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
104 fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title);
105
106 fprintf(fp_ou,"<tr><td class=\"header\">%s: %s</td></tr>\n",text[89],period);
107 fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[125]);
108 fputs("</table></div>\n",fp_ou);
109
110 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
111 fputs("<tr><td></td></tr>\n",fp_ou);
112 fprintf(fp_ou,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th></tr>\n",text[98],text[111],text[110],text[91]);
113
114 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
115 getword_start(&gwarea,buf);
116 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
117 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
118 getword(url,sizeof(url),&gwarea,'\t')<0) {
119 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",report_in);
120 exit(1);
121 }
122
123 if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
124 if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
125 fixip(user);
126 }
127
128 if(Ip2Name)
129 ip2name(ip,sizeof(ip));
130
131 if(!z) {
132 strcpy(ouser,user);
133 strcpy(oip,ip);
134 z++;
135 } else {
136 if(strcmp(ouser,user) == 0)
137 user[0]='\0';
138 if(user[0] != '\0')
139 strcpy(ouser,user);
140 if(strcmp(oip,ip) == 0)
141 ip[0]='\0';
142 if(ip[0] != '\0')
143 strcpy(oip,ip);
144 }
145
146 user_find(name, sizeof(name), user);
147
148 if(dotinuser && strchr(name,'_')) {
149 subs(name,sizeof(name),"_",".");
150 }
151
152 if(DownloadReportLimit) {
153 if(strcmp(ouser2,name) == 0) {
154 count++;
155 } else {
156 count=1;
157 strcpy(ouser2,name);
158 }
159 if(count >= DownloadReportLimit)
160 continue;
161 }
162
163 for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0;
164
165 fprintf(fp_ou,"<tr><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s-%s</td><td class=\"data2\">",name,ip,data,hora);
166 if(BlockIt[0]!='\0')
167 fprintf(fp_ou,"<a href=\"%s%s?url=%s\"><img src=\"%s/sarg-squidguard-block.png\" border=\"0\"></a>&nbsp;",wwwDocumentRoot,BlockIt,url,ImageFile);
168 fprintf(fp_ou,"<a href=\"%s\">",url);
169 output_html_string(fp_ou,url);
170 fputs("</a></td></tr>\n",fp_ou);
171 }
172
173 fputs("</table>\n",fp_ou);
174
175 show_info(fp_ou);
176 fputs("</body>\n</html>\n",fp_ou);
177
178 fclose(fp_in);
179 fclose(fp_ou);
180
181 unlink(report_in);
182
183 return;
184 }
185
186 void free_download(void)
187 {
188 if (DownloadSuffix) {
189 free(DownloadSuffix);
190 DownloadSuffix=NULL;
191 }
192 if (DownloadSuffixIndex) {
193 free(DownloadSuffixIndex);
194 DownloadSuffixIndex=NULL;
195 }
196 NDownloadSuffix=0;
197 }
198
199 void set_download_suffix(const char *list)
200 {
201 char *str;
202 int i, j, k;
203 int cmp;
204
205 free_download();
206
207 DownloadSuffix=strdup(list);
208 if (!DownloadSuffix) {
209 fprintf(stderr,"SARG: Download suffix list too long\n");
210 exit(1);
211 }
212 j = 1;
213 for (i=0 ; list[i] ; i++)
214 if (list[i] == ',') j++;
215 DownloadSuffixIndex=malloc(j*sizeof(char *));
216 if (!DownloadSuffixIndex) {
217 fprintf(stderr,"SARG: Too many download suffixes\n");
218 exit(1);
219 }
220
221 str = DownloadSuffix;
222 for (i=0 ; DownloadSuffix[i] ; i++) {
223 if (DownloadSuffix[i] == ',') {
224 DownloadSuffix[i] = '\0';
225 if (*str) {
226 cmp = -1;
227 for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
228 if (cmp != 0) {
229 for (k=NDownloadSuffix ; k>j ; k--)
230 DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
231 NDownloadSuffix++;
232 DownloadSuffixIndex[j]=str;
233 }
234 }
235 str=DownloadSuffix+i+1;
236 }
237 }
238
239 if (*str) {
240 cmp = -1;
241 for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
242 if (cmp != 0) {
243 for (k=NDownloadSuffix ; k>j ; k--)
244 DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
245 NDownloadSuffix++;
246 DownloadSuffixIndex[j]=str;
247 }
248 }
249 }
250
251 int is_download_suffix(const char *url)
252 {
253 int urllen;
254 int i;
255 int down, up, center;
256 const char *suffix;
257 int cmp;
258 const int max_suffix=10;
259
260 if (DownloadSuffix == NULL || NDownloadSuffix == 0) return(0);
261
262 urllen=strlen(url)-1;
263 if (urllen<=0) return(0);
264 if (url[urllen] == '.') return(0); //reject a single trailing dot
265 for (i=0 ; i<urllen && (url[i]!='/' || url[i+1]=='/') && url[i]!='?' ; i++);
266 if (i>=urllen) return(0); // url is a hostname without any path or file to download
267
268 for (i=0 ; i<=max_suffix && i<urllen && url[urllen-i]!='.' ; i++)
269 if (url[urllen-i] == '/' || url[urllen-i] == '?') return(0);
270 if (i>max_suffix || i>=urllen) return(0);
271
272 suffix=url+urllen-i+1;
273 down=0;
274 up=NDownloadSuffix-1;
275 while (down<=up) {
276 center=(down+up)/2;
277 cmp=strcasecmp(suffix,DownloadSuffixIndex[center]);
278 if (cmp == 0) return(1);
279 if (cmp < 0)
280 up = center-1;
281 else
282 down = center+1;
283 }
284 return(0);
285 }
286