]>
Commit | Line | Data |
---|---|---|
25697a35 | 1 | /* |
94ff9470 | 2 | * SARG Squid Analysis Report Generator http://sarg.sourceforge.net |
1164c474 | 3 | * 1998, 2010 |
25697a35 GS |
4 | * |
5 | * SARG donations: | |
6 | * please look at http://sarg.sourceforge.net/donations.php | |
1164c474 FM |
7 | * Support: |
8 | * http://sourceforge.net/projects/sarg/forums/forum/363374 | |
25697a35 GS |
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" | |
5f3cfd1d | 28 | #include "include/defs.h" |
25697a35 | 29 | |
2824ec9b FM |
30 | /*@null@*/static char *DownloadSuffix=NULL; |
31 | /*@null@*/static char **DownloadSuffixIndex=NULL; | |
6e792ade FM |
32 | static int NDownloadSuffix=0; |
33 | ||
32e71fa4 | 34 | void download_report(void) |
25697a35 GS |
35 | { |
36 | ||
37 | FILE *fp_in = NULL, *fp_ou = NULL; | |
4157aa09 | 38 | |
ac422f9b | 39 | char *buf; |
e5b2c6f0 | 40 | char *url; |
25697a35 | 41 | char report_in[MAXLEN]; |
25697a35 | 42 | char report[MAXLEN]; |
25697a35 GS |
43 | char ip[MAXLEN]; |
44 | char oip[MAXLEN]; | |
45 | char user[MAXLEN]; | |
46 | char ouser[MAXLEN]; | |
47 | char ouser2[MAXLEN]; | |
48 | char data[15]; | |
49 | char hora[15]; | |
25697a35 GS |
50 | int z=0; |
51 | int count=0; | |
dfb337be | 52 | int i; |
324ba7f3 | 53 | int day,month,year; |
2824ec9b | 54 | bool new_user; |
9c7c6346 | 55 | struct getwordstruct gwarea; |
afaa3b67 | 56 | longline line; |
f2ec8c75 | 57 | struct userinfostruct *uinfo; |
324ba7f3 | 58 | struct tm t; |
25697a35 GS |
59 | |
60 | ouser[0]='\0'; | |
4157aa09 | 61 | ouser2[0]='\0'; |
25697a35 | 62 | |
e7b4b9e5 | 63 | snprintf(report_in,sizeof(report_in),"%s/download.log",tmp); |
25697a35 GS |
64 | if(access(report_in, R_OK) != 0) |
65 | return; | |
66 | ||
2824ec9b | 67 | snprintf(report,sizeof(report),"%s/download.html",outdirname); |
25697a35 | 68 | |
936c9905 | 69 | if((fp_in=MY_FOPEN(report_in,"r"))==NULL) { |
9f70c14e | 70 | debuga(_("(download) Cannot open log file %s\n"),report_in); |
06b39c87 | 71 | exit(EXIT_FAILURE); |
25697a35 GS |
72 | } |
73 | ||
936c9905 | 74 | if((fp_ou=MY_FOPEN(report,"w"))==NULL) { |
9f70c14e | 75 | debuga(_("(download) Cannot open log file %s\n"),report); |
06b39c87 | 76 | exit(EXIT_FAILURE); |
25697a35 GS |
77 | } |
78 | ||
c0ec9cc7 | 79 | write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Downloads")); |
94558017 | 80 | fputs("<tr><td class=\"header_c\">",fp_ou); |
fd46f082 | 81 | fprintf(fp_ou,_("Period: %s"),period.html); |
fa6552b0 | 82 | fputs("</td></tr>\n",fp_ou); |
c36c7384 | 83 | fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Downloads")); |
c0ec9cc7 | 84 | close_html_header(fp_ou); |
25697a35 | 85 | |
c0ec9cc7 | 86 | fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou); |
c36c7384 | 87 | fprintf(fp_ou,"<tr><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE")); |
25697a35 | 88 | |
afaa3b67 | 89 | if ((line=longline_create())==NULL) { |
10210234 | 90 | debuga(_("Not enough memory to read the downloaded files\n")); |
06b39c87 | 91 | exit(EXIT_FAILURE); |
ac422f9b FM |
92 | } |
93 | ||
afaa3b67 | 94 | while((buf=longline_read(fp_in,line))!=NULL) { |
9c7c6346 FM |
95 | getword_start(&gwarea,buf); |
96 | if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 || | |
e5b2c6f0 | 97 | getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) { |
10210234 | 98 | debuga(_("There is a broken record or garbage in file %s\n"),report_in); |
06b39c87 | 99 | exit(EXIT_FAILURE); |
e5b2c6f0 FM |
100 | } |
101 | if (getword_ptr(buf,&url,&gwarea,'\t')<0) { | |
10210234 | 102 | debuga(_("There is a broken url in file %s\n"),report_in); |
06b39c87 | 103 | exit(EXIT_FAILURE); |
4bcb77cf | 104 | } |
324ba7f3 FM |
105 | if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue; |
106 | computedate(year,month,day,&t); | |
107 | strftime(data,sizeof(data),"%x",&t); | |
dfb337be | 108 | |
f2ec8c75 FM |
109 | uinfo=userinfo_find_from_id(user); |
110 | if (!uinfo) { | |
10210234 | 111 | debuga(_("Unknown user ID %s in file %s\n"),user,report_in); |
06b39c87 | 112 | exit(EXIT_FAILURE); |
25697a35 | 113 | } |
2824ec9b | 114 | new_user=false; |
25697a35 GS |
115 | if(!z) { |
116 | strcpy(ouser,user); | |
117 | strcpy(oip,ip); | |
118 | z++; | |
2824ec9b | 119 | new_user=true; |
25697a35 | 120 | } else { |
f2ec8c75 | 121 | if(strcmp(ouser,user) != 0) { |
25697a35 | 122 | strcpy(ouser,user); |
2824ec9b | 123 | new_user=true; |
f2ec8c75 FM |
124 | } |
125 | if(strcmp(oip,ip) != 0) { | |
25697a35 | 126 | strcpy(oip,ip); |
2824ec9b | 127 | new_user=true; |
f2ec8c75 | 128 | } |
94ff9470 GS |
129 | } |
130 | ||
131 | if(DownloadReportLimit) { | |
f2ec8c75 | 132 | if(strcmp(ouser2,uinfo->label) == 0) { |
4157aa09 | 133 | count++; |
25697a35 GS |
134 | } else { |
135 | count=1; | |
f2ec8c75 | 136 | strcpy(ouser2,uinfo->label); |
25697a35 | 137 | } |
94ff9470 | 138 | if(count >= DownloadReportLimit) |
25697a35 GS |
139 | continue; |
140 | } | |
141 | ||
dfb337be | 142 | for (i=strlen(url)-1 ; i>=0 && (unsigned char)url[i]<' ' ; i--) url[i]=0; |
25697a35 | 143 | |
f2ec8c75 FM |
144 | fputs("<tr>",fp_ou); |
145 | if (new_user) | |
146 | fprintf(fp_ou,"<td class=\"data\">%s</td><td class=\"data\">%s</td>",uinfo->label,ip); | |
147 | else | |
148 | fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou); | |
149 | fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora); | |
ac422f9b FM |
150 | if(BlockIt[0]!='\0') { |
151 | fprintf(fp_ou,"<a href=\"%s%s?url=\"",wwwDocumentRoot,BlockIt); | |
152 | output_html_url(fp_ou,url); | |
153 | fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a> ",ImageFile); | |
154 | } | |
f2ec8c75 | 155 | fputs("<a href=\"http://",fp_ou); |
ac422f9b | 156 | output_html_url(fp_ou,url); |
f2ec8c75 | 157 | fputs("\">http://",fp_ou); |
ac422f9b | 158 | output_html_string(fp_ou,url,100); |
dfb337be | 159 | fputs("</a></td></tr>\n",fp_ou); |
25697a35 | 160 | } |
25697a35 | 161 | fclose(fp_in); |
afaa3b67 | 162 | longline_destroy(&line); |
c0ec9cc7 FM |
163 | |
164 | fputs("</table></div>\n",fp_ou); | |
fa6552b0 FM |
165 | if (write_html_trailer(fp_ou)<0) |
166 | debuga(_("Write error in file %s\n"),report); | |
167 | if (fclose(fp_ou)==EOF) | |
168 | debuga(_("Failed to close file %s - %s\n"),report,strerror(errno)); | |
25697a35 GS |
169 | |
170 | unlink(report_in); | |
171 | ||
172 | return; | |
173 | } | |
6e792ade FM |
174 | |
175 | void free_download(void) | |
176 | { | |
177 | if (DownloadSuffix) { | |
178 | free(DownloadSuffix); | |
179 | DownloadSuffix=NULL; | |
180 | } | |
181 | if (DownloadSuffixIndex) { | |
182 | free(DownloadSuffixIndex); | |
183 | DownloadSuffixIndex=NULL; | |
184 | } | |
185 | NDownloadSuffix=0; | |
186 | } | |
187 | ||
188 | void set_download_suffix(const char *list) | |
189 | { | |
190 | char *str; | |
191 | int i, j, k; | |
192 | int cmp; | |
193 | ||
194 | free_download(); | |
195 | ||
196 | DownloadSuffix=strdup(list); | |
197 | if (!DownloadSuffix) { | |
9f70c14e | 198 | debuga(_("Download suffix list too long\n")); |
06b39c87 | 199 | exit(EXIT_FAILURE); |
6e792ade FM |
200 | } |
201 | j = 1; | |
202 | for (i=0 ; list[i] ; i++) | |
203 | if (list[i] == ',') j++; | |
204 | DownloadSuffixIndex=malloc(j*sizeof(char *)); | |
205 | if (!DownloadSuffixIndex) { | |
9f70c14e | 206 | debuga(_("Too many download suffixes\n")); |
06b39c87 | 207 | exit(EXIT_FAILURE); |
6e792ade FM |
208 | } |
209 | ||
210 | str = DownloadSuffix; | |
211 | for (i=0 ; DownloadSuffix[i] ; i++) { | |
212 | if (DownloadSuffix[i] == ',') { | |
213 | DownloadSuffix[i] = '\0'; | |
214 | if (*str) { | |
fabbc7cc | 215 | cmp = -1; |
6e792ade FM |
216 | for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++); |
217 | if (cmp != 0) { | |
218 | for (k=NDownloadSuffix ; k>j ; k--) | |
219 | DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1]; | |
220 | NDownloadSuffix++; | |
221 | DownloadSuffixIndex[j]=str; | |
222 | } | |
223 | } | |
224 | str=DownloadSuffix+i+1; | |
225 | } | |
226 | } | |
227 | ||
228 | if (*str) { | |
fabbc7cc | 229 | cmp = -1; |
6e792ade FM |
230 | for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++); |
231 | if (cmp != 0) { | |
232 | for (k=NDownloadSuffix ; k>j ; k--) | |
233 | DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1]; | |
234 | NDownloadSuffix++; | |
235 | DownloadSuffixIndex[j]=str; | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
2824ec9b | 240 | bool is_download_suffix(const char *url) |
6e792ade FM |
241 | { |
242 | int urllen; | |
243 | int i; | |
244 | int down, up, center; | |
245 | const char *suffix; | |
246 | int cmp; | |
247 | const int max_suffix=10; | |
248 | ||
2824ec9b | 249 | if (DownloadSuffix == NULL || NDownloadSuffix == 0) return(false); |
6e792ade FM |
250 | |
251 | urllen=strlen(url)-1; | |
2824ec9b FM |
252 | if (urllen<=0) return(false); |
253 | if (url[urllen] == '.') return(false); //reject a single trailing dot | |
8947bb9b | 254 | for (i=0 ; i<urllen && (url[i]!='/' || url[i+1]=='/') && url[i]!='?' ; i++); |
2824ec9b | 255 | if (i>=urllen) return(false); // url is a hostname without any path or file to download |
6e792ade FM |
256 | |
257 | for (i=0 ; i<=max_suffix && i<urllen && url[urllen-i]!='.' ; i++) | |
2824ec9b FM |
258 | if (url[urllen-i] == '/' || url[urllen-i] == '?') return(false); |
259 | if (i>max_suffix || i>=urllen) return(false); | |
6e792ade FM |
260 | |
261 | suffix=url+urllen-i+1; | |
262 | down=0; | |
263 | up=NDownloadSuffix-1; | |
264 | while (down<=up) { | |
265 | center=(down+up)/2; | |
266 | cmp=strcasecmp(suffix,DownloadSuffixIndex[center]); | |
2824ec9b | 267 | if (cmp == 0) return(true); |
6e792ade FM |
268 | if (cmp < 0) |
269 | up = center-1; | |
270 | else | |
271 | down = center+1; | |
272 | } | |
2824ec9b | 273 | return(false); |
6e792ade FM |
274 | } |
275 |