]> git.ipfire.org Git - thirdparty/sarg.git/blame - dansguardian_log.c
Fix HTML output
[thirdparty/sarg.git] / dansguardian_log.c
CommitLineData
25697a35 1/*
c37945ed 2 * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com
a6e5c172 3 * 1998, 2010
94ff9470 4 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
25697a35
GS
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"
5f3cfd1d 27#include "include/defs.h"
25697a35 28
32e71fa4 29void dansguardian_log(void)
25697a35
GS
30{
31
32 FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL;
33 char guard_in[MAXLEN];
34 char guard_ou[MAXLEN];
35 char loglocation[MAXLEN] = "/var/log/dansguardian/access.log";
36 char year[10], mon[10], day[10];
37 char hour[15];
38 char user[MAXLEN], code1[255], code2[255];
39 char ip[30];
40 char wdata[127];
41 int idata=0;
456d78a5 42 int cstatus;
9c7c6346 43 struct getwordstruct gwarea;
25697a35 44
25697a35
GS
45 bzero(day, 3);
46 bzero(mon, 4);
47 bzero(year, 5);
48
49 if(strcmp(df,"e") == 0) {
d6e703cc
FM
50 strncpy(day,period,2);
51 strncpy(mon,period+2,3);
52 strncpy(year,period+5,4);
25697a35
GS
53 conv_month(mon);
54 sprintf(warea,"%s%s%s",year,mon,day);
55 dfrom=atoi(warea);
d6e703cc
FM
56 strncpy(day,period+10,2);
57 strncpy(mon,period+12,3);
58 strncpy(year,period+15,4);
25697a35
GS
59 conv_month(mon);
60 sprintf(warea,"%s%s%s",year,mon,day);
61 duntil=atoi(warea);
62 } else {
d6e703cc
FM
63 strncpy(day,period+7,2);
64 strncpy(mon,period+4,3);
65 strncpy(year,period,4);
25697a35
GS
66 conv_month(mon);
67 sprintf(warea,"%s%s%s",year,mon,day);
68 dfrom=atoi(warea);
d6e703cc
FM
69 strncpy(day,period+17,2);
70 strncpy(mon,period+14,3);
71 strncpy(year,period+10,4);
25697a35
GS
72 conv_month(mon);
73 sprintf(warea,"%s%s%s",year,mon,day);
74 duntil=atoi(warea);
75 }
76
77 sprintf(guard_in,"%s/dansguardian.unsort",tmp);
78 sprintf(guard_ou,"%s/dansguardian.log",tmp);
79
80 if(access(DansGuardianConf, R_OK) != 0) {
d2fe0c32 81 debuga("Cannot open DansGuardian config file: %s",DansGuardianConf);
25697a35
GS
82 exit(1);
83 }
84
85 if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
86 fprintf(stderr, "SARG: (dansguardian) %s: %s\n",text[8],DansGuardianConf);
87 exit(1);
88 }
89
936c9905 90 if((fp_ou=MY_FOPEN(guard_in,"a"))==NULL) {
25697a35
GS
91 fprintf(stderr, "SARG: (dansguardian) %s: %s\n",text[8],guard_in);
92 exit(1);
93 }
94
95 while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
9c7c6346
FM
96 fixendofline(buf);
97 if(buf[0]=='#')
25697a35
GS
98 continue;
99 if(strstr(buf,"loglocation ") != 0) {
9c7c6346
FM
100 getword_start(&gwarea,buf);
101 if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) {
4bcb77cf
FM
102 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",DansGuardianConf);
103 exit(1);
104 }
25697a35
GS
105 break;
106 }
107 }
108
109 if(debug) {
9c7c6346
FM
110 getword_start(&gwarea,text[7]);
111 if (getword(urly,sizeof(urly),&gwarea,' ')<0 || getword(href,sizeof(href),&gwarea,' ')<0) {
4bcb77cf
FM
112 printf("SARG: Maybe you have a broken record or garbage in your %s entry.\n",text[7]);
113 exit(1);
114 }
9c7c6346 115 debuga("%s DansGuardian %s: %s",urly,gwarea.current,loglocation);
25697a35 116 }
936c9905
FM
117
118 if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) {
25697a35
GS
119 fprintf(stderr, "SARG: (dansguardian) %s: %s\n",text[8],loglocation);
120 exit(1);
121 }
936c9905 122
25697a35
GS
123 while(fgets(buf,sizeof(buf),fp_in) != NULL) {
124 if(strstr(buf," *DENIED* ") == 0)
125 continue;
9c7c6346
FM
126 getword_start(&gwarea,buf);
127 if (getword(year,sizeof(year),&gwarea,'.')<0 || getword(mon,sizeof(mon),&gwarea,'.')<0 ||
128 getword(day,sizeof(day),&gwarea,' ')<0 || getword(hour,sizeof(hour),&gwarea,' ')<0 ||
129 getword(user,sizeof(user),&gwarea,' ')<0 || getword(ip,sizeof(ip),&gwarea,' ')<0 ||
130 getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0 ||
131 getword(url,sizeof(url),&gwarea,' ')<0 || getword_skip(255,&gwarea,' ')<0 ||
132 getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) {
4bcb77cf
FM
133 printf("SARG: Maybe you have a broken record or garbage in your %s file.\n",loglocation);
134 exit(1);
135 }
25697a35
GS
136 sprintf(wdata,"%s%02d%s",year,atoi(mon),day);
137 idata = atoi(wdata);
138
e6414a9d 139 if(DansguardianIgnoreDate) {
491b862f
GS
140 if(idata < dfrom && idata > duntil)
141 continue;
142 }
143
144 if (strcmp(user,"-") == 0) {
145 strcpy(user,ip);
146 bzero(ip, 30);
25697a35 147 }
120d768c 148 fprintf(fp_ou,"%s\t%d\t%s\t%s\t%s\t%s\t%s\n",user,idata,hour,ip,url,code1,code2);
491b862f 149 dansguardian_count++;
25697a35 150 }
491b862f
GS
151
152 if(fp_in) fclose(fp_in);
153 if(fp_guard) fclose(fp_guard);
154 if(fp_ou) fclose(fp_ou);
155
d2fe0c32
FM
156 if(debug)
157 debuga("%s: %s",text[54],guard_ou);
491b862f 158
9a2efbd0 159 sprintf(tmp6,"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou);
456d78a5
FM
160 cstatus=system(tmp6);
161 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
162 fprintf(stderr, "SARG: sort command return status %d\n",WEXITSTATUS(cstatus));
163 fprintf(stderr, "SARG: sort command: %s\n",tmp6);
164 exit(1);
165 }
25697a35 166 unlink(guard_in);
25697a35 167}