]> git.ipfire.org Git - thirdparty/sarg.git/blame - dansguardian_report.c
Attempt to fix a FreeBSD IP address resolution
[thirdparty/sarg.git] / dansguardian_report.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
fbd133bb 3 * 1998, 2011
25697a35
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
32e71fa4 30void dansguardian_report(void)
25697a35 31{
9bd92830
FM
32 FILE *fp_in = NULL, *fp_ou = NULL;
33
34 char buf[MAXLEN];
35 char *url;
36 char dansguardian_in[MAXLEN];
37 char report[MAXLEN];
38 char ip[MAXLEN];
39 char rule[255];
40 char oip[MAXLEN];
41 char user[MAXLEN];
42 char ouser[MAXLEN];
43 char date[15];
44 char date2[15];
45 char hour[15];
46 char ouser2[255];
47 int z=0;
48 int count=0;
49 struct getwordstruct gwarea;
50
51 ouser[0]='\0';
52
53 snprintf(dansguardian_in,sizeof(dansguardian_in),"%s/dansguardian.log",tmp);
54 if(!dansguardian_count) {
55 unlink(dansguardian_in);
56 return;
57 }
58
59 sprintf(report,"%s/dansguardian.html",outdirname);
60
61 if((fp_in=MY_FOPEN(dansguardian_in,"r"))==NULL) {
007905af
FM
62 debuga(_("(dansguardian_report) Cannot open log file %s\n"),dansguardian_in);
63 exit(EXIT_FAILURE);
9bd92830
FM
64 }
65
66 if((fp_ou=MY_FOPEN(report,"w"))==NULL) {
007905af
FM
67 debuga(_("(dansguardian_report) Cannot open log file %s\n"),report);
68 exit(EXIT_FAILURE);
9bd92830
FM
69 }
70
71 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("DansGuardian"),HTML_JS_NONE);
72 fputs("<tr><td class=\"header_c\">",fp_ou);
73 fprintf(fp_ou,_("Period: %s"),period.html);
74 fputs("</td></tr>\n",fp_ou);
75 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("DansGuardian"));
76 close_html_header(fp_ou);
77
78 fputs("<div class=\"report\"><table cellpadding=\"1\" cellspacing=\"2\">\n",fp_ou);
79 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><th class=\"header_l\">%s</th></tr>\n",_("USERID"),_("IP/NAME"),_("DATE/TIME"),_("ACCESSED SITE"),_("CAUSE"));
80
81 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
82 getword_start(&gwarea,buf);
83 if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(date2,sizeof(date2),&gwarea,'\t')<0 ||
007905af 84 getword(hour,sizeof(hour),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
9bd92830
FM
85 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),dansguardian_in);
86 exit(EXIT_FAILURE);
87 }
88 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
89 debuga(_("Maybe you have a broken url in your %s file\n"),dansguardian_in);
90 exit(EXIT_FAILURE);
91 }
92 if (getword(rule,sizeof(rule),&gwarea,'\n')<0) {
93 debuga(_("Maybe you have a broken rule in your %s file\n"),dansguardian_in);
94 exit(EXIT_FAILURE);
95 }
96
97 if(UserIp)
98 strcpy(user,ip);
99
100 bzero(date, 15);
101 if(strncmp(df,"u",1) != 0) {
102 strncpy(date,date2+6,2);
103 strcat(date,"/");
104 strncat(date,date2+4,2);
105 strcat(date,"/");
106 strncat(date,date2,4);
107 } else {
108 strncpy(date,date2+4,2);
109 strcat(date,"/");
110 strncat(date,date2+6,2);
111 strcat(date,"/");
112 strncat(date,date2,4);
113 }
114
115 if(Ip2Name)
116 ip2name(ip,sizeof(ip));
117
118 if(!z) {
119 strcpy(ouser,user);
120 strcpy(oip,ip);
121 z++;
122 } else {
123 if(strcmp(ouser,user) == 0)
124 user[0]='\0';
125 if(user[0] != '\0')
126 strcpy(ouser,user);
127 if(strcmp(oip,ip) == 0)
128 ip[0]='\0';
129 if(ip[0] != '\0')
130 strcpy(oip,ip);
131 }
132
133 user_find(name, sizeof(name), user);
134
007905af 135 if(DansGuardianReportLimit) {
9bd92830
FM
136 if(strcmp(ouser2,name) == 0) {
137 count++;
138 } else {
139 count=1;
140 strcpy(ouser2,name);
141 }
142 if(count >= DansGuardianReportLimit)
143 continue;
144 }
145
67a93701
FM
146 fprintf(fp_ou,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td><td class=\"data2\">%s-%s</td><td class=\"data2\">",name,ip,date,hour);
147 output_html_link(fp_ou,url,100);
148 fprintf(fp_ou,"</td><td class=\"data2\">%s</td></tr>\n",rule);
9bd92830
FM
149 }
150 fclose(fp_in);
151
152 fputs("</table></div>\n",fp_ou);
153 if (write_html_trailer(fp_ou)<0)
154 debuga(_("Write error in file %s\n"),report);
155 if (fclose(fp_ou)==EOF)
156 debuga(_("Failed to close file %s - %s\n"),report,strerror(errno));
157
08f9b029
FM
158 if (unlink(dansguardian_in)) {
159 debuga(_("Cannot delete %s - %s\n"),dansguardian_in,strerror(errno));
160 exit(EXIT_FAILURE);
161 }
9bd92830
FM
162
163 return;
25697a35 164}