]> git.ipfire.org Git - thirdparty/sarg.git/blob - authfail.c
Ported r225 from branches/v2_2_7 (remove Pedro Orso's mail from headers)
[thirdparty/sarg.git] / authfail.c
1 /*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
3 * 1998, 2010
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
33 FILE *fp_in = NULL, *fp_ou = NULL;
34
35 char url[MAXLEN];
36 char authfail_in[MAXLEN];
37 char per[MAXLEN];
38 char report[MAXLEN];
39 char period[100];
40 char ip[MAXLEN];
41 char oip[MAXLEN];
42 char user[MAXLEN];
43 char ouser[MAXLEN];
44 char ouser2[MAXLEN];
45 char data[15];
46 char hora[15];
47 char *str;
48 int z=0;
49 int count=0;
50 int cstatus;
51 struct getwordstruct gwarea;
52
53 if(DataFile[0] != '\0') return;
54
55 ouser[0]='\0';
56
57 sprintf(tmp4,"%s/sarg/authfail.log.unsort",TempDir);
58
59 if(!authfail_count) {
60 unlink(tmp4);
61 return;
62 }
63
64 sprintf(authfail_in,"%s/authfail.log",TempDir);
65 sprintf(per,"%s/sarg-period",dirname);
66 sprintf(report,"%s/authfail.html",dirname);
67
68 if ((fp_in = fopen(per, "r")) == 0) {
69 fprintf(stderr, "SARG: (authfail) %s: %s\n",text[45],per);
70 exit(1);
71 }
72 if (!fgets(period,sizeof(period),fp_in)) {
73 fprintf(stderr,"SARG: (authfail) read error in %s\n",per);
74 exit(1);
75 }
76 fclose(fp_in);
77
78 sprintf(csort,"sort -b -T \"%s\" -k 3,3 -k 5,5 -o \"%s\" \"%s\"", TempDir, authfail_in, tmp4);
79 cstatus=system(csort);
80 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
81 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
82 fprintf(stderr, "SARG: sort command: %s\n",csort);
83 exit(1);
84 }
85 if((fp_in=MY_FOPEN(authfail_in,"r"))==NULL) {
86 fprintf(stderr, "SARG: (authfail) %s: %s\n",text[45],authfail_in);
87 fprintf(stderr, "SARG: sort command: %s\n",csort);
88 exit(1);
89 }
90 unlink(tmp4);
91
92 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
93 fprintf(stderr, "SARG: (authfail) %s: %s\n",text[45],report);
94 exit(1);
95 }
96
97 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);
98 css(fp_ou);
99 fputs("</head>\n",fp_ou);
100 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
101 fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
102 write_logo_image(fp_ou);
103
104 if(IndexTree == INDEX_TREE_DATE)
105 show_sarg(fp_ou, "../../..");
106 else
107 show_sarg(fp_ou,"..");
108 fputs("<div align=\"center\"><table cellpadding=0 cellspacing=0>\n",fp_ou);
109 fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title);
110
111 fprintf(fp_ou,"<tr><td class=\"header\">%s: %s</td></tr>\n",text[89],period);
112 fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[117]);
113 fputs("</table></div>\n",fp_ou);
114
115 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
116 fputs("<tr><td></td></tr>\n",fp_ou);
117 fputs("<tr><td></td></tr>\n",fp_ou);
118 fputs("<tr><td></td></tr>\n",fp_ou);
119 fprintf(fp_ou,"<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]);
120
121 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
122 getword_start(&gwarea,buf);
123 if (getword(data,sizeof(data),&gwarea,'\t')<0 || getword(hora,sizeof(hora),&gwarea,'\t')<0 ||
124 getword(user,sizeof(user),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0 ||
125 getword(url,sizeof(url),&gwarea,'\t')<0) {
126 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",authfail_in);
127 exit(1);
128 }
129
130 if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
131 if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
132 fixip(user);
133 }
134
135 if(Ip2Name)
136 ip2name(ip,sizeof(ip));
137
138 if(!z) {
139 strcpy(ouser,user);
140 strcpy(oip,ip);
141 z++;
142 } else {
143 if(strcmp(ouser,user) == 0)
144 user[0]='\0';
145 if(user[0] != '\0')
146 strcpy(ouser,user);
147 if(strcmp(oip,ip) == 0)
148 ip[0]='\0';
149 if(ip[0] != '\0')
150 strcpy(oip,ip);
151 }
152
153 user_find(name,sizeof(name), user);
154
155 if(dotinuser && strchr(name,'_')) {
156 subs(name,sizeof(name),"_",".");
157 }
158
159 if(AuthfailReportLimit) {
160 if(strcmp(ouser2,name) == 0) {
161 count++;
162 } else {
163 count=1;
164 strcpy(ouser2,name);
165 }
166 if(count >= AuthfailReportLimit)
167 continue;
168 }
169
170 fprintf(fp_ou,"<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);
171 }
172
173 fputs("</table>\n",fp_ou);
174
175 show_info(fp_ou);
176
177 fputs("</body>\n</html>\n",fp_ou);
178
179 fclose(fp_in);
180 fclose(fp_ou);
181
182 unlink(authfail_in);
183
184 return;
185 }