+#include "include/readlog.h"
+
+//! Name of the file containing the unsorted authentication failure entries.
+static char authfail_unsort[MAXLEN]="";
+//! The file handle to write the entries.
+static FILE *fp_authfail=NULL;
+//! \c True if at least one anthentication failure entry exists.
+static bool authfail_exists=false;
+
+/*!
+Open a file to store the authentication failure.
+
+\return The file handle or NULL if no file is necessary.
+*/
+void authfail_open(void)
+{
+ if ((ReportType & REPORT_TYPE_AUTH_FAILURES) == 0) {
+ if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Authentication failures report not produced as it is not requested\n"));
+ return;
+ }
+ if (Privacy) {
+ if (debugz>=LogLevel_Process) debugaz(__FILE__,__LINE__,_("Authentication failures report not produced because privacy option is active\n"));
+ return;
+ }
+
+ snprintf(authfail_unsort,sizeof(authfail_unsort),"%s/authfail.int_unsort",tmp);
+ if ((fp_authfail=MY_FOPEN(authfail_unsort,"w"))==NULL) {
+ debuga(__FILE__,__LINE__,_("Cannot open file \"%s\": %s\n"),authfail_unsort,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ return;
+}
+
+/*!
+Write one entry in the unsorted authentication file file provided that it is required.
+
+\param log_entry The entry to write into the log file.
+*/
+void authfail_write(const struct ReadLogStruct *log_entry)
+{
+ char date[80];
+
+ if (fp_authfail && (strstr(log_entry->HttpCode,"DENIED/401") != 0 || strstr(log_entry->HttpCode,"DENIED/407") != 0)) {
+ strftime(date,sizeof(date),"%d/%m/%Y\t%H:%M:%S",&log_entry->EntryTime);
+ fprintf(fp_authfail, "%s\t%s\t%s\t%s\n",date,log_entry->User,log_entry->Ip,log_entry->Url);
+ authfail_exists=true;
+ }
+}
+
+/*!
+Close the file opened by authfail_open().
+*/
+void authfail_close(void)
+{
+ if (fp_authfail)
+ {
+ if (fclose(fp_authfail)==EOF) {
+ debuga(__FILE__,__LINE__,_("Write error in \"%s\": %s\n"),authfail_unsort,strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ fp_authfail=NULL;
+ }
+}
+
+/*!
+Tell the caller if a authentication failure report exists.
+
+\return \c True if the report is available or \c false if no report
+was generated.
+*/
+bool is_authfail(void)
+{
+ return(authfail_exists);
+}
+
+
+static void show_ignored_auth(FILE *fp_ou,int count)
+{
+ char ignored[80];
+
+ snprintf(ignored,sizeof(ignored),ngettext("%d more authentication failure not shown here…","%d more authentication failures not shown here…",count),count);
+ 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);
+}