]> git.ipfire.org Git - thirdparty/sarg.git/blame - authfail.c
Rename configure.in as configure.ac
[thirdparty/sarg.git] / authfail.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
110ce984 3 * 1998, 2015
94ff9470
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"
16b013cc
FM
29#include "include/readlog.h"
30
31//! Name of the file containing the unsorted authentication failure entries.
32static char authfail_unsort[MAXLEN]="";
33//! The file handle to write the entries.
34static FILE *fp_authfail=NULL;
35//! \c True if at least one anthentication failure entry exists.
36static bool authfail_exists=false;
37
38/*!
39Open a file to store the authentication failure.
40
41\return The file handle or NULL if no file is necessary.
42*/
43void authfail_open(void)
44{
45 if ((ReportType & REPORT_TYPE_AUTH_FAILURES) == 0) {
af961877 46 if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Authentication failures report not produced as it is not requested\n"));
16b013cc
FM
47 return;
48 }
49 if (Privacy) {
af961877 50 if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Authentication failures report not produced because privacy option is active\n"));
16b013cc
FM
51 return;
52 }
bd43d81f 53
16b013cc
FM
54 snprintf(authfail_unsort,sizeof(authfail_unsort),"%s/authfail.int_unsort",tmp);
55 if ((fp_authfail=MY_FOPEN(authfail_unsort,"w"))==NULL) {
af961877 56 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),authfail_unsort,strerror(errno));
16b013cc
FM
57 exit(EXIT_FAILURE);
58 }
59 return;
60}
61
62/*!
63Write one entry in the unsorted authentication file file provided that it is required.
64
65\param log_entry The entry to write into the log file.
66*/
67void authfail_write(const struct ReadLogStruct *log_entry)
68{
69 char date[80];
bd43d81f 70
16b013cc 71 if (fp_authfail && (strstr(log_entry->HttpCode,"DENIED/401") != 0 || strstr(log_entry->HttpCode,"DENIED/407") != 0)) {
cb53374b 72 strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",&log_entry->EntryTime);
16b013cc
FM
73 fprintf(fp_authfail, "%s\t%s\t%s\t%s\n",date,log_entry->User,log_entry->Ip,log_entry->Url);
74 authfail_exists=true;
75 }
76}
77
78/*!
79Close the file opened by authfail_open().
80*/
81void authfail_close(void)
82{
83 if (fp_authfail)
84 {
507460ae 85 if (fclose(fp_authfail)==EOF) {
af961877 86 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),authfail_unsort,strerror(errno));
16b013cc
FM
87 exit(EXIT_FAILURE);
88 }
89 fp_authfail=NULL;
90 }
91}
92
93/*!
94Tell the caller if a authentication failure report exists.
95
96\return \c True if the report is available or \c false if no report
97was generated.
98*/
99bool is_authfail(void)
100{
101 return(authfail_exists);
102}
103
25697a35 104
7ae50eee
FM
105static void show_ignored_auth(FILE *fp_ou,int count)
106{
107 char ignored[80];
108
109 snprintf(ignored,sizeof(ignored),ngettext("%d more authentication failure not shown here…","%d more authentication failures not shown here…",count),count);
110 fprintf(fp_ou,"<tr><td class=\"data\"></td><td class=\"data\"></td><td class=\"data\"></td><td class=\"data2 more\">%s</td></tr>\n",ignored);
111}
112
32e71fa4 113void authfail_report(void)
25697a35 114{
9bd92830
FM
115 FILE *fp_in = NULL, *fp_ou = NULL;
116
117 char *buf;
118 char *url;
16b013cc 119 char authfail_sort[MAXLEN];
9bd92830
FM
120 char report[MAXLEN];
121 char ip[MAXLEN];
16b013cc 122 char oip[MAXLEN]="";
9bd92830 123 char user[MAXLEN];
16b013cc
FM
124 char ouser[MAXLEN]="";
125 char ouser2[MAXLEN]="";
9bd92830
FM
126 char data[15];
127 char hora[15];
9bd92830 128 char csort[MAXLEN];
16b013cc
FM
129 int z=0;
130 int count=0;
131 int cstatus;
9bd92830
FM
132 int day,month,year;
133 bool new_user;
134 struct getwordstruct gwarea;
135 longline line;
136 struct userinfostruct *uinfo;
137 struct tm t;
138
16b013cc 139 if (!authfail_exists) {
ab7f8e50 140 if (!KeepTempLog && authfail_unsort[0]!='\0' && unlink(authfail_unsort))
af961877 141 debuga(__FILE__,__LINE__,_("Failed to delete \"%s\": %s\n"),authfail_unsort,strerror(errno));
9bd92830 142
16b013cc 143 authfail_unsort[0]='\0';
af961877 144 if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Authentication failures report not produced because it is empty\n"));
9bd92830
FM
145 return;
146 }
b7413c4c 147 if (debugz>=LogLevel_Process)
af961877 148 debuga(__FILE__,__LINE__,_("Creating authentication failures report...\n"));
9bd92830 149
16b013cc 150 snprintf(authfail_sort,sizeof(authfail_sort),"%s/authfail.int_log",tmp);
9bd92830
FM
151 snprintf(report,sizeof(report),"%s/authfail.html",outdirname);
152
16b013cc 153 snprintf(csort,sizeof(csort),"sort -b -t \"\t\" -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"", tmp, authfail_sort, authfail_unsort);
9bd92830
FM
154 cstatus=system(csort);
155 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
af961877
FM
156 debuga(__FILE__,__LINE__,_("sort command return status %d\n"),WEXITSTATUS(cstatus));
157 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
158 exit(EXIT_FAILURE);
159 }
16b013cc 160 if((fp_in=MY_FOPEN(authfail_sort,"r"))==NULL) {
af961877
FM
161 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),authfail_sort,strerror(errno));
162 debuga(__FILE__,__LINE__,_("sort command: %s\n"),csort);
9bd92830
FM
163 exit(EXIT_FAILURE);
164 }
b378aaf1 165 if (!KeepTempLog && unlink(authfail_unsort)) {
af961877 166 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),authfail_unsort,strerror(errno));
08f9b029
FM
167 exit(EXIT_FAILURE);
168 }
16b013cc 169 authfail_unsort[0]='\0';
9bd92830
FM
170
171 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
af961877 172 debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),report,strerror(errno));
007905af 173 exit(EXIT_FAILURE);
9bd92830
FM
174 }
175
176 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Authentication Failures"),HTML_JS_NONE);
177 fputs("<tr><td class=\"header_c\">",fp_ou);
178 fprintf(fp_ou,_("Period: %s"),period.html);
179 fputs("</td></tr>\n",fp_ou);
180 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Authentication Failures"));
181 close_html_header(fp_ou);
182
183 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
184 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"));
185
186 if ((line=longline_create())==NULL) {
af961877 187 debuga(__FILE__,__LINE__,_("Not enough memory to read file \"%s\"\n"),authfail_sort);
9bd92830
FM
188 exit(EXIT_FAILURE);
189 }
190
191 while((buf=longline_read(fp_in,line))!=NULL) {
192 getword_start(&gwarea,buf);
193 if (getword(data,sizeof(data),&gwarea,'\t')<0) {
af961877 194 debuga(__FILE__,__LINE__,_("Invalid date in file \"%s\"\n"),authfail_sort);
9bd92830
FM
195 exit(EXIT_FAILURE);
196 }
197 if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
af961877 198 debuga(__FILE__,__LINE__,_("Invalid time in file \"%s\"\n"),authfail_sort);
9bd92830
FM
199 exit(EXIT_FAILURE);
200 }
201 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
af961877 202 debuga(__FILE__,__LINE__,_("Invalid user ID in file \"%s\"\n"),authfail_sort);
9bd92830
FM
203 exit(EXIT_FAILURE);
204 }
205 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
af961877 206 debuga(__FILE__,__LINE__,_("Invalid IP address in file \"%s\"\n"),authfail_sort);
9bd92830
FM
207 exit(EXIT_FAILURE);
208 }
209 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
af961877 210 debuga(__FILE__,__LINE__,_("Invalid url in file \"%s\"\n"),authfail_sort);
9bd92830
FM
211 exit(EXIT_FAILURE);
212 }
213 if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
214 computedate(year,month,day,&t);
215 strftime(data,sizeof(data),"%x",&t);
216
217 uinfo=userinfo_find_from_id(user);
218 if (!uinfo) {
af961877 219 debuga(__FILE__,__LINE__,_("Unknown user ID %s in file \"%s\"\n"),user,authfail_sort);
9bd92830
FM
220 exit(EXIT_FAILURE);
221 }
222
223 new_user=false;
224 if(z == 0) {
225 strcpy(ouser,user);
226 strcpy(oip,ip);
227 z++;
228 new_user=true;
229 } else {
230 if(strcmp(ouser,user) != 0) {
231 strcpy(ouser,user);
232 new_user=true;
233 }
234 if(strcmp(oip,ip) != 0) {
235 strcpy(oip,ip);
236 new_user=true;
237 }
238 }
239
007905af
FM
240 if(AuthfailReportLimit>0) {
241 if(strcmp(ouser2,uinfo->label) == 0) {
9bd92830
FM
242 count++;
243 } else {
7ae50eee
FM
244 if(count>AuthfailReportLimit && AuthfailReportLimit>0)
245 show_ignored_auth(fp_ou,count-AuthfailReportLimit);
9bd92830
FM
246 count=1;
247 strcpy(ouser2,uinfo->label);
248 }
7ae50eee 249 if(count > AuthfailReportLimit)
9bd92830
FM
250 continue;
251 }
252
253 fputs("<tr>",fp_ou);
254 if (new_user)
255 fprintf(fp_ou,"<td class=\"data2\">%s</td><td class=\"data2\">%s</td>",uinfo->label,ip);
256 else
257 fputs("<td class=\"data2\"></td><td class=\"data2\"></td>",fp_ou);
258 fprintf(fp_ou,"<td class=\"data2\">%s-%s</td><td class=\"data2\">",data,hora);
67a93701 259 if(BlockIt[0]!='\0' && url[0]!=ALIAS_PREFIX) {
9bd92830
FM
260 fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
261 output_html_url(fp_ou,url);
262 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
263 }
6fa33a32 264 output_html_link(fp_ou,url,100);
67a93701 265 fputs("</td></th>\n",fp_ou);
9bd92830 266 }
204781f4 267 if (fclose(fp_in)==EOF) {
af961877 268 debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),authfail_sort,strerror(errno));
204781f4
FM
269 exit(EXIT_FAILURE);
270 }
9bd92830
FM
271 longline_destroy(&line);
272
7ae50eee
FM
273 if(count>AuthfailReportLimit && AuthfailReportLimit>0)
274 show_ignored_auth(fp_ou,count-AuthfailReportLimit);
275
9bd92830 276 fputs("</table></div>\n",fp_ou);
342bd723 277 write_html_trailer(fp_ou);
507460ae 278 if (fclose(fp_ou)==EOF) {
af961877 279 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),report,strerror(errno));
507460ae
FM
280 exit(EXIT_FAILURE);
281 }
9bd92830 282
b378aaf1 283 if (!KeepTempLog && unlink(authfail_sort)) {
af961877 284 debuga(__FILE__,__LINE__,_("Cannot delete \"%s\": %s\n"),authfail_sort,strerror(errno));
08f9b029
FM
285 exit(EXIT_FAILURE);
286 }
9bd92830
FM
287
288 return;
25697a35 289}
16b013cc
FM
290
291/*!
292Remove any temporary file left by the authfail module.
293*/
294void authfail_cleanup(void)
295{
507460ae
FM
296 if (fp_authfail) {
297 if (fclose(fp_authfail)==EOF) {
af961877 298 debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),authfail_unsort,strerror(errno));
507460ae
FM
299 exit(EXIT_FAILURE);
300 }
16b013cc
FM
301 fp_authfail=NULL;
302 }
303 if(authfail_unsort[0]) {
d89ead3c 304 if (!KeepTempLog && unlink(authfail_unsort)==-1)
af961877 305 debuga(__FILE__,__LINE__,_("Failed to delete \"%s\": %s\n"),authfail_unsort,strerror(errno));
16b013cc
FM
306 }
307}