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