/*
* AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
- * 1998, 2009
+ * 1998, 2010
* SARG Squid Analysis Report Generator http://sarg.sourceforge.net
*
* SARG donations:
#include "include/conf.h"
#include "include/defs.h"
+static char *DownloadSuffix=NULL;
+static char **DownloadSuffixIndex=NULL;
+static int NDownloadSuffix=0;
+
void download_report(void)
{
int z=0;
int count=0;
int i;
+ struct getwordstruct gwarea;
ouser[0]='\0';
ouser2[0]='\0';
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]);
while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
- if (getword(data,sizeof(data),buf,'\t')<0 || getword(hora,sizeof(hora),buf,'\t')<0 ||
- getword(user,sizeof(user),buf,'\t')<0 || getword(ip,sizeof(ip),buf,'\t')<0 ||
- getword(url,sizeof(url),buf,'\t')<0) {
+ getword_start(&gwarea,buf);
+ if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
+ getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
+ getword(url,sizeof(url),&gwarea,'\t')<0) {
printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",report_in);
exit(1);
}
fixip(user);
}
- if(strcmp(Ip2Name,"yes") == 0)
+ if(Ip2Name)
ip2name(ip,sizeof(ip));
if(!z) {
return;
}
+
+void free_download(void)
+{
+ if (DownloadSuffix) {
+ free(DownloadSuffix);
+ DownloadSuffix=NULL;
+ }
+ if (DownloadSuffixIndex) {
+ free(DownloadSuffixIndex);
+ DownloadSuffixIndex=NULL;
+ }
+ NDownloadSuffix=0;
+}
+
+void set_download_suffix(const char *list)
+{
+ char *str;
+ int i, j, k;
+ int cmp;
+
+ free_download();
+
+ DownloadSuffix=strdup(list);
+ if (!DownloadSuffix) {
+ fprintf(stderr,"SARG: Download suffix list too long\n");
+ exit(1);
+ }
+ j = 1;
+ for (i=0 ; list[i] ; i++)
+ if (list[i] == ',') j++;
+ DownloadSuffixIndex=malloc(j*sizeof(char *));
+ if (!DownloadSuffixIndex) {
+ fprintf(stderr,"SARG: Too many download suffixes\n");
+ exit(1);
+ }
+
+ str = DownloadSuffix;
+ for (i=0 ; DownloadSuffix[i] ; i++) {
+ if (DownloadSuffix[i] == ',') {
+ DownloadSuffix[i] = '\0';
+ if (*str) {
+ cmp = -1;
+ for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
+ if (cmp != 0) {
+ for (k=NDownloadSuffix ; k>j ; k--)
+ DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
+ NDownloadSuffix++;
+ DownloadSuffixIndex[j]=str;
+ }
+ }
+ str=DownloadSuffix+i+1;
+ }
+ }
+
+ if (*str) {
+ cmp = -1;
+ for (j=0 ; j<NDownloadSuffix && (cmp=strcasecmp(str,DownloadSuffixIndex[j]))>0 ; j++);
+ if (cmp != 0) {
+ for (k=NDownloadSuffix ; k>j ; k--)
+ DownloadSuffixIndex[k]=DownloadSuffixIndex[k-1];
+ NDownloadSuffix++;
+ DownloadSuffixIndex[j]=str;
+ }
+ }
+}
+
+int is_download_suffix(const char *url)
+{
+ int urllen;
+ int i;
+ int down, up, center;
+ const char *suffix;
+ int cmp;
+ const int max_suffix=10;
+
+ if (DownloadSuffix == NULL || NDownloadSuffix == 0) return(0);
+
+ urllen=strlen(url)-1;
+ if (urllen<=0) return(0);
+ if (url[urllen] == '.') return(0); //reject a single trailing dot
+ for (i=0 ; i<urllen && (url[i]!='/' || url[i+1]=='/') && url[i]!='?' ; i++);
+ if (i>=urllen) return(0); // url is a hostname without any path or file to download
+
+ for (i=0 ; i<=max_suffix && i<urllen && url[urllen-i]!='.' ; i++)
+ if (url[urllen-i] == '/' || url[urllen-i] == '?') return(0);
+ if (i>max_suffix || i>=urllen) return(0);
+
+ suffix=url+urllen-i+1;
+ down=0;
+ up=NDownloadSuffix-1;
+ while (down<=up) {
+ center=(down+up)/2;
+ cmp=strcasecmp(suffix,DownloadSuffixIndex[center]);
+ if (cmp == 0) return(1);
+ if (cmp < 0)
+ up = center-1;
+ else
+ down = center+1;
+ }
+ return(0);
+}
+