]> git.ipfire.org Git - thirdparty/sarg.git/blob - denied.c
Fixed a regression in the usertab file not accepting IPv6 addresses any more.
[thirdparty/sarg.git] / denied.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2010
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 #include "include/defs.h"
28
29 void gen_denied_report(void)
30 {
31
32 FILE *fp_in = NULL, *fp_ou = NULL;
33
34 char url[MAXLEN];
35 char denied_in[MAXLEN];
36 char per[MAXLEN];
37 char report[MAXLEN];
38 char period[100];
39 char ip[MAXLEN];
40 char oip[MAXLEN];
41 char user[MAXLEN];
42 char ouser[MAXLEN];
43 char ouser2[MAXLEN];
44 char data[15];
45 char hora[15];
46 char *str;
47 int z=0;
48 int count=0;
49 struct getwordstruct gwarea;
50
51 ouser[0]='\0';
52 ouser2[0]='\0';
53
54 sprintf(denied_in,"%s/sarg/denied.log",TempDir);
55 if(!denied_count) {
56 unlink(denied_in);
57 return;
58 }
59
60 sprintf(per,"%s/sarg-period",dirname);
61 sprintf(report,"%s/denied.html",dirname);
62
63 if ((fp_in = fopen(per, "r")) == 0) {
64 fprintf(stderr, "SARG: (denied) %s: %s\n",text[45],per);
65 exit(1);
66 }
67
68 if (!fgets(period,sizeof(period),fp_in)) {
69 fprintf(stderr,"SARG: (denied) read error in %s\n",per);
70 exit(1);
71 }
72 fclose(fp_in);
73
74 if((fp_in=MY_FOPEN(denied_in,"r"))==NULL) {
75 fprintf(stderr, "SARG: (denied) %s: %s\n",text[8],denied_in);
76 exit(1);
77 }
78
79 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
80 fprintf(stderr, "SARG: (denied) %s: %s\n",text[8],report);
81 exit(1);
82 }
83
84 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);
85 css(fp_ou);
86 fputs("</head>\n",fp_ou);
87 fprintf(fp_ou,"<body bgcolor=\"%s\" text=\"%s\" background=\"%s\">\n",BgColor,TxColor,BgImage);
88 write_logo_image(fp_ou);
89
90 if(strcmp(IndexTree,"date") == 0)
91 show_sarg(fp_ou, "../../..");
92 else
93 show_sarg(fp_ou, "..");
94
95 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\">\n",fp_ou);
96 fprintf(fp_ou,"<tr><th class=\"title\">%s</b></th></tr>\n",Title);
97
98 fprintf(fp_ou,"<tr><td class=\"header\">%s: %s</td></tr>\n",text[89],period);
99 fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[46]);
100 fputs("</table></div>\n",fp_ou);
101
102 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
103 fputs("<tr><td></td></tr>\n",fp_ou);
104 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]);
105
106 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
107 getword_start(&gwarea,buf);
108 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
109 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
110 getword(url,sizeof(url),&gwarea,'\t')<0) {
111 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",denied_in);
112 exit(1);
113 }
114
115 if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
116 if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
117 fixip(user);
118 }
119
120 if(Ip2Name)
121 ip2name(ip,sizeof(ip));
122
123 if(!z) {
124 strcpy(ouser,user);
125 strcpy(oip,ip);
126 z++;
127 } else {
128 if(strcmp(ouser,user) == 0)
129 user[0]='\0';
130 if(user[0] != '\0')
131 strcpy(ouser,user);
132 if(strcmp(oip,ip) == 0)
133 ip[0]='\0';
134 if(ip[0] != '\0')
135 strcpy(oip,ip);
136 }
137
138 user_find(name, sizeof(name), user);
139
140 if(dotinuser && strchr(name,'_')) {
141 subs(name,sizeof(name),"_",".");
142 }
143
144 if(DeniedReportLimit) {
145 if(strcmp(ouser2,name) == 0) {
146 count++;
147 } else {
148 count=1;
149 strcpy(ouser2,name);
150 }
151 if(count >= DeniedReportLimit)
152 continue;
153 }
154
155 if(BlockIt[0] != '\0')
156 sprintf(BlockImage,"<a href=\"%s%s?url=%s\"><img src=\"%s/sarg-squidguard-block.png\" border=\"0\"></a>&nbsp;",wwwDocumentRoot,BlockIt,url,ImageFile);
157 else BlockImage[0]='\0';
158
159 fprintf(fp_ou,"<tr><td class=\"data\">%s</td><td class=\"data\">%s</td><td class=\"data\">%s-%s</td><td class=\"data2\">%s<a href=\"%s\">%s</a></td></th>\n",name,ip,data,hora,BlockImage,url,url);
160 }
161
162 fputs("</table>\n",fp_ou);
163
164 show_info(fp_ou);
165 fputs("</body></html>\n",fp_ou);
166
167 fclose(fp_in);
168 fclose(fp_ou);
169
170 unlink(denied_in);
171
172 return;
173 }