]> git.ipfire.org Git - thirdparty/sarg.git/blob - authfail.c
69ebbfbdba9430785d3e9aebe67ccd954783d4e3
[thirdparty/sarg.git] / authfail.c
1 /*
2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
3 * 1998, 2008
4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
5 *
6 * SARG donations:
7 * please look at http://sarg.sourceforge.net/donations.php
8 * ---------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
23 *
24 */
25
26 #include "include/conf.h"
27
28 void authfail_report(void)
29 {
30
31 FILE *fp_in = NULL, *fp_ou = NULL;
32
33 char url[MAXLEN];
34 char html2[MAXLEN];
35 char authfail_in[MAXLEN];
36 char per[MAXLEN];
37 char report[MAXLEN];
38 char period[100];
39 char ip[MAXLEN];
40 char oip[MAXLEN];
41 char user[MAXLEN];
42 char ouser[MAXLEN];
43 char ouser2[MAXLEN];
44 char data[15];
45 char hora[15];
46 char *str;
47 int z=0;
48 int count=0;
49 int cstatus;
50
51 if(strlen(DataFile) > 0) return;
52
53 ouser[0]='\0';
54
55 sprintf(tmp4,"%s/sarg/authfail.log.unsort",TempDir);
56
57 if(!authfail_count) {
58 unlink(tmp4);
59 return;
60 }
61
62 sprintf(authfail_in,"%s/authfail.log",TempDir);
63 sprintf(per,"%s/sarg-period",dirname);
64 sprintf(report,"%s/authfail.html",dirname);
65
66 if ((fp_in = fopen(per, "r")) == 0) {
67 fprintf(stderr, "SARG: (authfail) %s: %s\n",text[45],per);
68 exit(1);
69 }
70 fgets(period,sizeof(period),fp_in);
71 fclose(fp_in);
72
73 sprintf(csort,"sort -b -T %s -k 3,3 -k 5,5 -o '%s' '%s'", TempDir, authfail_in, tmp4);
74 cstatus=system(csort);
75 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
76 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
77 fprintf(stderr, "SARG: sort command: %s\n",csort);
78 exit(1);
79 }
80 #if defined(HAVE_FOPEN64)
81 if((fp_in=fopen64(authfail_in,"r"))==NULL) {
82 #else
83 if((fp_in=fopen(authfail_in,"r"))==NULL) {
84 #endif
85 fprintf(stderr, "SARG: (authfail) %s: %s\n",text[45],authfail_in);
86 fprintf(stderr, "SARG: sort command: %s\n",csort);
87 exit(1);
88 }
89 unlink(tmp4);
90
91 #if defined(HAVE_FOPEN64)
92 if((fp_ou=fopen64(report,"w"))==NULL) {
93 #else
94 if((fp_ou=fopen(report,"w"))==NULL) {
95 #endif
96 fprintf(stderr, "SARG: (authfail) %s: %s\n",text[45],report);
97 exit(1);
98 }
99
100 fprintf(fp_ou, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\">\n",CharSet);
101 css(fp_ou);
102 fputs("</head>\n",fp_ou);
103 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
104 fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
105 if(strlen(LogoImage) > 0) fprintf(fp_ou, "<center><table cellpadding=\"0\" cellspacing=\"0\">\n<tr><th class=\"logo\"><img src='%s' border=0 align=absmiddle width=%s height=%s>&nbsp;%s</th></tr>\n<tr><td height=\"5\"></td></tr>\n</table>\n",LogoImage,Width,Height,LogoText);
106
107 if(strcmp(IndexTree,"date") == 0)
108 show_sarg(fp_ou, "../../..");
109 else
110 show_sarg(fp_ou,"..");
111 fputs("<center><table cellpadding=0 cellspacing=0>\n",fp_ou);
112 sprintf(url,"<tr><th class=\"title\">%s</th></tr>\n",Title);
113 fputs(url,fp_ou);
114
115 sprintf(url,"<tr><td class=\"header\">%s: %s</td></tr>\n",text[89],period);
116 fputs(url,fp_ou);
117 sprintf(url,"<tr><th class=\"header3\">%s</th></tr>\n",text[117]);
118 fputs(url,fp_ou);
119 fputs("</table></center>\n",fp_ou);
120
121 fputs("<center><table cellpadding=0 cellspacing=2>\n",fp_ou);
122 fputs("<tr><td></td></tr>\n",fp_ou);
123 fputs("<tr><td></td></tr>\n",fp_ou);
124 fputs("<tr><td></td></tr>\n",fp_ou);
125 sprintf(url,"<tr><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th><th class=\"header\">%s</th></tr>\n",text[98],text[111],text[110],text[91]);
126 fputs(url,fp_ou);
127
128 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
129 if (getword_multisep(data,sizeof(data),buf,' ')<0 || getword_multisep(hora,sizeof(hora),buf,' ')<0 ||
130 getword_multisep(user,sizeof(user),buf,' ')<0 || getword_multisep(ip,sizeof(ip),buf,' ')<0 ||
131 getword_multisep(url,sizeof(url),buf,' ')<0) {
132 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",authfail_in);
133 exit(1);
134 }
135
136 if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
137 if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
138 fixip(user);
139 }
140
141 if(strcmp(Ip2Name,"yes") == 0)
142 ip2name(ip,sizeof(ip));
143
144 if(!z) {
145 strcpy(ouser,user);
146 strcpy(oip,ip);
147 z++;
148 } else {
149 if(strcmp(ouser,user) == 0)
150 user[0]='\0';
151 if(user[0] != '\0')
152 strcpy(ouser,user);
153 if(strcmp(oip,ip) == 0)
154 ip[0]='\0';
155 if(ip[0] != '\0')
156 strcpy(oip,ip);
157 }
158
159 if(UserTabFile[0] != '\0') {
160 sprintf(warea,":%s:",user);
161 if((str=(char *) strstr(userfile,warea)) != (char *) NULL ) {
162 z1=0;
163 str2=(char *) strstr(str+1,":");
164 str2++;
165 bzero(name, MAXLEN);
166 while(str2[z1] != ':') {
167 name[z1]=str2[z1];
168 z1++;
169 }
170 } else strcpy(name,user);
171 } else strcpy(name,user);
172
173 if(dotinuser && strstr(name,"_")) {
174 str2=(char *)subs(name,"_",".");
175 strcpy(name,str2);
176 }
177
178 if(AuthfailReportLimit) {
179 if(strcmp(ouser2,name) == 0) {
180 count++;
181 } else {
182 count=1;
183 strcpy(ouser2,name);
184 }
185 if(count >= AuthfailReportLimit)
186 continue;
187 }
188
189 sprintf(html2,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td><td class=\"data2\">%s-%s</td><td class=\"data2\">%s<a href=\"%s\">%s</a></td></th>\n",name,ip,data,hora,BlockImage,url,url);
190 fputs(html2,fp_ou);
191 }
192
193 fputs("</table>\n",fp_ou);
194
195 show_info(fp_ou);
196
197 fputs("</body>\n</html>\n",fp_ou);
198
199 fclose(fp_in);
200 fclose(fp_ou);
201
202 unlink(authfail_in);
203
204 return;
205 }