]> git.ipfire.org Git - thirdparty/sarg.git/blob - authfail.c
Compute the report date without resorting to the system command.
[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 #include "include/defs.h"
28
29 void authfail_report(void)
30 {
31
32 FILE *fp_in = NULL, *fp_ou = NULL;
33
34 char url[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((fp_in=MY_FOPEN(authfail_in,"r"))==NULL) {
81 fprintf(stderr, "SARG: (authfail) %s: %s\n",text[45],authfail_in);
82 fprintf(stderr, "SARG: sort command: %s\n",csort);
83 exit(1);
84 }
85 unlink(tmp4);
86
87 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
88 fprintf(stderr, "SARG: (authfail) %s: %s\n",text[45],report);
89 exit(1);
90 }
91
92 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);
93 css(fp_ou);
94 fputs("</head>\n",fp_ou);
95 if(strlen(FontFace) > 0) fprintf(fp_ou,"<font face=%s>\n",FontFace);
96 fprintf(fp_ou,"<body bgcolor=%s text=%s background='%s'>\n",BgColor,TxColor,BgImage);
97 write_logo_image(fp_ou);
98
99 if(strcmp(IndexTree,"date") == 0)
100 show_sarg(fp_ou, "../../..");
101 else
102 show_sarg(fp_ou,"..");
103 fputs("<div align=\"center\"><table cellpadding=0 cellspacing=0>\n",fp_ou);
104 fprintf(fp_ou,"<tr><th class=\"title\">%s</th></tr>\n",Title);
105
106 fprintf(fp_ou,"<tr><td class=\"header\">%s: %s</td></tr>\n",text[89],period);
107 fprintf(fp_ou,"<tr><th class=\"header3\">%s</th></tr>\n",text[117]);
108 fputs("</table></div>\n",fp_ou);
109
110 fputs("<div align=\"center\"><table cellpadding=\"0\" cellspacing=\"2\">\n",fp_ou);
111 fputs("<tr><td></td></tr>\n",fp_ou);
112 fputs("<tr><td></td></tr>\n",fp_ou);
113 fputs("<tr><td></td></tr>\n",fp_ou);
114 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]);
115
116 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
117 if (getword_multisep(data,sizeof(data),buf,' ')<0 || getword_multisep(hora,sizeof(hora),buf,' ')<0 ||
118 getword_multisep(user,sizeof(user),buf,' ')<0 || getword_multisep(ip,sizeof(ip),buf,' ')<0 ||
119 getword_multisep(url,sizeof(url),buf,' ')<0) {
120 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",authfail_in);
121 exit(1);
122 }
123
124 if((str=(char *) strstr(user, "_")) != (char *) NULL ) {
125 if((str=(char *) strstr(str+1, "_")) != (char *) NULL )
126 fixip(user);
127 }
128
129 if(strcmp(Ip2Name,"yes") == 0)
130 ip2name(ip,sizeof(ip));
131
132 if(!z) {
133 strcpy(ouser,user);
134 strcpy(oip,ip);
135 z++;
136 } else {
137 if(strcmp(ouser,user) == 0)
138 user[0]='\0';
139 if(user[0] != '\0')
140 strcpy(ouser,user);
141 if(strcmp(oip,ip) == 0)
142 ip[0]='\0';
143 if(ip[0] != '\0')
144 strcpy(oip,ip);
145 }
146
147 get_usertab_name(user,name,sizeof(name));
148
149 if(dotinuser && strchr(name,'_')) {
150 subs(name,sizeof(name),"_",".");
151 }
152
153 if(AuthfailReportLimit) {
154 if(strcmp(ouser2,name) == 0) {
155 count++;
156 } else {
157 count=1;
158 strcpy(ouser2,name);
159 }
160 if(count >= AuthfailReportLimit)
161 continue;
162 }
163
164 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);
165 }
166
167 fputs("</table>\n",fp_ou);
168
169 show_info(fp_ou);
170
171 fputs("</body>\n</html>\n",fp_ou);
172
173 fclose(fp_in);
174 fclose(fp_ou);
175
176 unlink(authfail_in);
177
178 return;
179 }