]> git.ipfire.org Git - thirdparty/sarg.git/blob - denied.c
Display the http protocol in the reported address
[thirdparty/sarg.git] / denied.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 void gen_denied_report(void)
31 {
32
33 FILE *fp_in = NULL, *fp_ou = NULL;
34
35 char *buf;
36 char *url;
37 char denied_in[MAXLEN];
38 char per[MAXLEN];
39 char report[MAXLEN];
40 char period[100];
41 char ip[MAXLEN];
42 char oip[MAXLEN];
43 char user[MAXLEN];
44 char ouser[MAXLEN];
45 char ouser2[MAXLEN];
46 char data[15];
47 char hora[15];
48 int z=0;
49 int count=0;
50 int new_user;
51 struct getwordstruct gwarea;
52 struct longlinestruct line;
53 struct userinfostruct *uinfo;
54
55 ouser[0]='\0';
56 ouser2[0]='\0';
57
58 sprintf(denied_in,"%s/sarg/denied.log",TempDir);
59 if(!denied_count) {
60 unlink(denied_in);
61 return;
62 }
63
64 sprintf(per,"%s/sarg-period",dirname);
65 sprintf(report,"%s/denied.html",dirname);
66
67 if ((fp_in = fopen(per, "r")) == 0) {
68 fprintf(stderr, "SARG: (denied) %s: %s\n",text[45],per);
69 exit(1);
70 }
71
72 if (!fgets(period,sizeof(period),fp_in)) {
73 debuga(_("(denied) read error in %s"),per);
74 exit(1);
75 }
76 fclose(fp_in);
77
78 if((fp_in=MY_FOPEN(denied_in,"r"))==NULL) {
79 fprintf(stderr, "SARG: (denied) %s: %s\n",text[8],denied_in);
80 exit(1);
81 }
82
83 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
84 fprintf(stderr, "SARG: (denied) %s: %s\n",text[8],report);
85 exit(1);
86 }
87
88 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("DENIED"));
89 fprintf(fp_ou,"<tr><td class=\"header_l\">%s: %s</td></tr>\n",text[89],period);
90 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",text[46]);
91 close_html_header(fp_ou);
92
93 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
94 fputs("<tr><td></td></tr>\n",fp_ou);
95 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",text[98],text[111],text[110],text[91]);
96
97 if (longline_prepare(&line)<0) {
98 debuga(_("Not enough memory to read the denied accesses"));
99 exit(1);
100 }
101
102 while((buf=longline_read(fp_in,&line))!=NULL) {
103 getword_start(&gwarea,buf);
104 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
105 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
106 debuga(_("There is a broken record or garbage in file %s"),denied_in);
107 exit(1);
108 }
109 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
110 debuga(_("There is a broken url in file %s"),denied_in);
111 exit(1);
112 }
113
114 uinfo=userinfo_find_from_id(user);
115 if (!uinfo) {
116 debuga(_("Unknown user ID %s in file %s"),user,denied_in);
117 exit(1);
118 }
119
120 new_user=0;
121 if(!z) {
122 strcpy(ouser,user);
123 strcpy(oip,ip);
124 z++;
125 new_user=1;
126 } else {
127 if(strcmp(ouser,user) != 0) {
128 strcpy(ouser,user);
129 new_user=1;
130 }
131 if(strcmp(oip,ip) != 0) {
132 strcpy(oip,ip);
133 new_user=1;
134 }
135 }
136
137 if(DeniedReportLimit) {
138 if(strcmp(ouser2,uinfo->label) == 0) {
139 count++;
140 } else {
141 count=1;
142 strcpy(ouser2,uinfo->label);
143 }
144 if(count >= DeniedReportLimit)
145 continue;
146 }
147
148 fputs("<tr>",fp_ou);
149 if (new_user)
150 fprintf(fp_ou,"<td class=\"data\">%s</td><td class=\"data\">%s</td>",uinfo->label,ip);
151 else
152 fputs("<td class=\"data\"></td><td class=\"data\"></td>",fp_ou);
153 fprintf(fp_ou,"<td class=\"data\">%s-%s</td><td class=\"data2\">",data,hora);
154 if(BlockIt[0] != '\0') {
155 fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
156 output_html_url(fp_ou,url);
157 fprintf(fp_ou,"\"><img src=\"%s/sarg-squidguard-block.png\"></a>&nbsp;",ImageFile);
158 }
159 fputs("<a href=\"http://",fp_ou);
160 output_html_url(fp_ou,url);
161 fputs("\">http://",fp_ou);
162 output_html_string(fp_ou,url,100);
163 fputs("</a></td></tr>\n",fp_ou);
164 }
165 fclose(fp_in);
166 longline_free(&line);
167
168 fputs("</table></div>\n",fp_ou);
169 write_html_trailer(fp_ou);
170 fclose(fp_ou);
171
172 unlink(denied_in);
173
174 return;
175 }