]> git.ipfire.org Git - thirdparty/sarg.git/blob - squidguard_report.c
Disable __attribute__ for non gnu compiler
[thirdparty/sarg.git] / squidguard_report.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 squidguard_report(void)
31 {
32
33 FILE *fp_in = NULL, *fp_ou = NULL;
34
35 char buf[MAXLEN];
36 char *url;
37 char squidguard_in[MAXLEN];
38 char per[MAXLEN];
39 char report[MAXLEN];
40 char period[100];
41 char ip[MAXLEN];
42 char rule[255];
43 char oip[MAXLEN];
44 char user[MAXLEN];
45 char ouser[MAXLEN];
46 char data[15];
47 char data2[15];
48 char hora[15];
49 char ouser2[255];
50 int z=0;
51 int count=0;
52 struct getwordstruct gwarea;
53
54 ouser[0]='\0';
55
56 sprintf(squidguard_in,"%s/squidguard.log",tmp);
57 if(!squidguard_count) {
58 unlink(squidguard_in);
59 return;
60 }
61
62 snprintf(per,sizeof(per),"%s/sarg-period",outdirname);
63 snprintf(report,sizeof(report),"%s/squidguard.html",outdirname);
64
65 if ((fp_in = fopen(per, "r")) == 0) {
66 debuga(_("(squidguard) Cannot open file %s\n"),per);
67 exit(EXIT_FAILURE);
68 }
69
70 if (!fgets(period,sizeof(period),fp_in)) {
71 debuga(_("(squidguard) read error in %s\n"),per);
72 exit(EXIT_FAILURE);
73 }
74 fclose(fp_in);
75
76 if((fp_in=fopen(squidguard_in,"r"))==NULL) {
77 debuga(_("(squidguard) Cannot open log file %s\n"),squidguard_in);
78 exit(EXIT_FAILURE);
79 }
80
81 if((fp_ou=fopen(report,"w"))==NULL) {
82 debuga(_("(squidguard) Cannot open log file %s\n"),report);
83 exit(EXIT_FAILURE);
84 }
85
86 write_html_header(fp_ou,(IndexTree == INDEX_TREE_DATE) ? 3 : 1,_("SQUIDGUARD"));
87 fprintf(fp_ou,"<tr><td class=\"header_l\">%s: %s</td></tr>\n",_("Period"),period);
88 fprintf(fp_ou,"<tr><th class=\"header_c\">%s</th></tr>\n",_("SQUIDGUARD"));
89 close_html_header(fp_ou);
90
91 fputs("<div class=\"report\"><table cellpadding=1 cellspacing=2>\n",fp_ou);
92 fputs("<tr><td></td></tr>\n",fp_ou);
93 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"),_("RULE"));
94
95 while(fgets(buf,sizeof(buf),fp_in)!=NULL) {
96 getword_start(&gwarea,buf);
97 if (getword(user,sizeof(user),&gwarea,'\t')<0 || getword(data2,sizeof(data2),&gwarea,'\t')<0 ||
98 getword(hora,sizeof(hora),&gwarea,'\t')<0 || getword(ip,sizeof(ip),&gwarea,'\t')<0) {
99 debuga(_("There is a broken record or garbage in file %s\n"),squidguard_in);
100 exit(EXIT_FAILURE);
101 }
102 if (getword_ptr(buf,&url,&gwarea,'\t')<0) {
103 debuga(_("There is a broken url in file %s\n"),squidguard_in);
104 exit(EXIT_FAILURE);
105 }
106 if (getword(rule,sizeof(rule),&gwarea,'\n')<0) {
107 debuga(_("There is a broken rule in file %s\n"),squidguard_in);
108 exit(EXIT_FAILURE);
109 }
110
111 if(UserIp)
112 strcpy(user,ip);
113
114 bzero(data, 15);
115 if(strncmp(df,"u",1) != 0) {
116 strncpy(data,data2+6,2);
117 strcat(data,"/");
118 strncat(data,data2+4,2);
119 strcat(data,"/");
120 strncat(data,data2,4);
121 } else {
122 strncpy(data,data2+4,2);
123 strcat(data,"/");
124 strncat(data,data2+6,2);
125 strcat(data,"/");
126 strncat(data,data2,4);
127 }
128
129 if(Ip2Name)
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 user_find(name, sizeof(name), user);
148
149 if(SquidGuardReportLimit) {
150 if(strcmp(ouser2,name) == 0) {
151 count++;
152 } else {
153 count=1;
154 strcpy(ouser2,name);
155 }
156 if(count >= SquidGuardReportLimit)
157 continue;
158 }
159
160 fprintf(fp_ou,"<tr><td class=\"data2\">%s</td><td class=\"data2\">%s</td><td class=\"data2\">%s-%s</td><td class=\"data2\"><a href=\"http://",name,ip,data,hora);
161 output_html_url(fp_ou,url);
162 fputs("\">",fp_ou);
163 output_html_string(fp_ou,url,100);
164 fprintf(fp_ou,"</a></td><td class=\"data2\">%s</td></th>\n",rule);
165 }
166 fclose(fp_in);
167
168 fputs("</table></div>\n",fp_ou);
169 write_html_trailer(fp_ou);
170 fclose(fp_ou);
171
172 unlink(squidguard_in);
173
174 return;
175 }