]> git.ipfire.org Git - thirdparty/sarg.git/blob - authfail.c
Merge commit '6d4c97986f54adcfd0c7ec'
[thirdparty/sarg.git] / authfail.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2011
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 authfail_report(void)
31 {
32 FILE *fp_in = NULL, *fp_ou = NULL;
33
34 char *buf;
35 char *url;
36 char authfail_in[MAXLEN];
37 char report[MAXLEN];
38 char ip[MAXLEN];
39 char oip[MAXLEN];
40 char user[MAXLEN];
41 char ouser[MAXLEN];
42 char ouser2[MAXLEN];
43 char data[15];
44 char hora[15];
45 char tmp4[MAXLEN];
46 char csort[MAXLEN];
47 int z=0;
48 int count=0;
49 int cstatus;
50 int day,month,year;
51 bool new_user;
52 struct getwordstruct gwarea;
53 longline line;
54 struct userinfostruct *uinfo;
55 struct tm t;
56
57 if(DataFile[0] != '\0') return;
58
59 ouser[0]='\0';
60 ouser2[0]='\0';
61 oip[0]='\0';
62
63 snprintf(tmp4,sizeof(tmp4),"%s/authfail.log.unsort",tmp);
64
65 if(authfail_count == 0) {
66 unlink(tmp4);
67 return;
68 }
69
70 snprintf(authfail_in,sizeof(authfail_in),"%s/authfail.log",tmp);
71 snprintf(report,sizeof(report),"%s/authfail.html",outdirname);
72
73 snprintf(csort,sizeof(csort),"sort -b -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"", tmp, authfail_in, tmp4);
74 cstatus=system(csort);
75 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
76 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
77 debuga(_("sort command: %s\n"),csort);
78 exit(EXIT_FAILURE);
79 }
80 if((fp_in=MY_FOPEN(authfail_in,"r"))==NULL) {
81 debuga(_("(authfail) Cannot open file %s\n"),authfail_in);
82 debuga(_("sort command: %s\n"),csort);
83 exit(EXIT_FAILURE);
84 }
85 if (unlink(tmp4)) {
86 debuga(_("Cannot delete %s - %s\n"),tmp4,strerror(errno));
87 exit(EXIT_FAILURE);
88 }
89
90 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
91 debuga(_("(authfail) Cannot open file %s\n"),report);
92 exit(EXIT_FAILURE);
93 }
94
95 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("Authentication Failures"),HTML_JS_NONE);
96 fputs("<tr><td class=\"header_c\">",fp_ou);
97 fprintf(fp_ou,_("Period: %s"),period.html);
98 fputs("</td></tr>\n",fp_ou);
99 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("Authentication Failures"));
100 close_html_header(fp_ou);
101
102 fputs("<div class=\"report\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
103 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"));
104
105 if ((line=longline_create())==NULL) {
106 debuga(_("Not enough memory to read file %s\n"),authfail_in);
107 exit(EXIT_FAILURE);
108 }
109
110 while((buf=longline_read(fp_in,line))!=NULL) {
111 getword_start(&gwarea,buf);
112 if (getword(data,sizeof(data),&gwarea,'\t')<0) {
113 debuga(_("There is a broken date in file %s\n"),authfail_in);
114 exit(EXIT_FAILURE);
115 }
116 if (getword(hora,sizeof(hora),&gwarea,'\t')<0) {
117 debuga(_("There is a broken time in file %s\n"),authfail_in);
118 exit(EXIT_FAILURE);
119 }
120 if (getword(user,sizeof(user),&gwarea,'\t')<0) {
121 debuga(_("There is a broken user ID in file %s\n"),authfail_in);
122 exit(EXIT_FAILURE);
123 }
124 if (getword(ip,sizeof(ip),&gwarea,'\t')<0) {
125 debuga(_("There is a broken IP address in file %s\n"),authfail_in);
126 exit(EXIT_FAILURE);
127 }
128 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
129 debuga(_("There is a broken url in file %s\n"),authfail_in);
130 exit(EXIT_FAILURE);
131 }
132 if (sscanf(data,"%d/%d/%d",&day,&month,&year)!=3) continue;
133 computedate(year,month,day,&t);
134 strftime(data,sizeof(data),"%x",&t);
135
136 uinfo=userinfo_find_from_id(user);
137 if (!uinfo) {
138 debuga(_("Unknown user ID %s in file %s\n"),user,authfail_in);
139 exit(EXIT_FAILURE);
140 }
141
142 new_user=false;
143 if(z == 0) {
144 strcpy(ouser,user);
145 strcpy(oip,ip);
146 z++;
147 new_user=true;
148 } else {
149 if(strcmp(ouser,user) != 0) {
150 strcpy(ouser,user);
151 new_user=true;
152 }
153 if(strcmp(oip,ip) != 0) {
154 strcpy(oip,ip);
155 new_user=true;
156 }
157 }
158
159 if(AuthfailReportLimit>0) {
160 if(strcmp(ouser2,uinfo->label) == 0) {
161 count++;
162 } else {
163 count=1;
164 strcpy(ouser2,uinfo->label);
165 }
166 if(count >= AuthfailReportLimit)
167 continue;
168 }
169
170 fputs("<tr>",fp_ou);
171 if (new_user)
172 fprintf(fp_ou,"<td class=\"data2\">%s</td><td class=\"data2\">%s</td>",uinfo->label,ip);
173 else
174 fputs("<td class=\"data2\"></td><td class=\"data2\"></td>",fp_ou);
175 fprintf(fp_ou,"<td class=\"data2\">%s-%s</td><td class=\"data2\">",data,hora);
176 if(BlockIt[0]!='\0') {
177 fprintf(fp_ou,"<a href=\"%s%s?url=",wwwDocumentRoot,BlockIt);
178 output_html_url(fp_ou,url);
179 fputs("\"><img src=\"../images/sarg-squidguard-block.png\"></a>&nbsp;",fp_ou);
180 }
181 fputs("<a href=\"",fp_ou);
182 output_html_url(fp_ou,url);
183 fputs("\">",fp_ou);
184 output_html_string(fp_ou,url,100);
185 fputs("</a></td></th>\n",fp_ou);
186 }
187 fclose(fp_in);
188 longline_destroy(&line);
189
190 fputs("</table></div>\n",fp_ou);
191 if (write_html_trailer(fp_ou)<0)
192 debuga(_("Write error in file %s\n"),report);
193 if (fclose(fp_ou)==EOF)
194 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
195
196 if (unlink(authfail_in)) {
197 debuga(_("Cannot delete %s - %s\n"),authfail_in,strerror(errno));
198 exit(EXIT_FAILURE);
199 }
200
201 return;
202 }