]> git.ipfire.org Git - thirdparty/sarg.git/blob - dansguardian_log.c
Give a try to splint and apply a few of the recommandations
[thirdparty/sarg.git] / dansguardian_log.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 dansguardian_log(void)
31 {
32
33 FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL;
34 char buf[MAXLEN];
35 char guard_in[MAXLEN];
36 char guard_ou[MAXLEN];
37 char loglocation[MAXLEN] = "/var/log/dansguardian/access.log";
38 char year[10], mon[10], day[10];
39 char hour[15];
40 char user[MAXLEN], code1[255], code2[255];
41 char ip[30];
42 char wdata[127];
43 char *url;
44 char tmp6[MAXLEN];
45 int idata=0;
46 int cstatus;
47 struct getwordstruct gwarea;
48
49 bzero(day, 3);
50 bzero(mon, 4);
51 bzero(year, 5);
52
53 if(strcmp(df,"e") == 0) {
54 strncpy(day,period,2);
55 strncpy(mon,period+2,3);
56 strncpy(year,period+5,4);
57 conv_month(mon);
58 sprintf(warea,"%s%s%s",year,mon,day);
59 dfrom=atoi(warea);
60 strncpy(day,period+10,2);
61 strncpy(mon,period+12,3);
62 strncpy(year,period+15,4);
63 conv_month(mon);
64 sprintf(warea,"%s%s%s",year,mon,day);
65 duntil=atoi(warea);
66 } else {
67 strncpy(day,period+7,2);
68 strncpy(mon,period+4,3);
69 strncpy(year,period,4);
70 conv_month(mon);
71 sprintf(warea,"%s%s%s",year,mon,day);
72 dfrom=atoi(warea);
73 strncpy(day,period+17,2);
74 strncpy(mon,period+14,3);
75 strncpy(year,period+10,4);
76 conv_month(mon);
77 sprintf(warea,"%s%s%s",year,mon,day);
78 duntil=atoi(warea);
79 }
80
81 snprintf(guard_in,sizeof(guard_in),"%s/dansguardian.unsort",tmp);
82 snprintf(guard_ou,sizeof(guard_ou),"%s/dansguardian.log",tmp);
83
84 if(access(DansGuardianConf, R_OK) != 0) {
85 debuga(_("Cannot open DansGuardian config file: %s\n"),DansGuardianConf);
86 exit(EXIT_FAILURE);
87 }
88
89 if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
90 debuga(_("(dansguardian) Cannot open log file: %s\n"),DansGuardianConf);
91 exit(EXIT_FAILURE);
92 }
93
94 if((fp_ou=MY_FOPEN(guard_in,"a"))==NULL) {
95 debuga(_("(dansguardian) Cannot open log file: %s\n"),guard_in);
96 exit(EXIT_FAILURE);
97 }
98
99 while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
100 fixendofline(buf);
101 if(buf[0]=='#')
102 continue;
103 if(strstr(buf,"loglocation ") != 0) {
104 getword_start(&gwarea,buf);
105 if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) {
106 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),DansGuardianConf);
107 exit(EXIT_FAILURE);
108 }
109 break;
110 }
111 }
112
113 if(debug)
114 debuga(_("Reading DansGuardian log file: %s\n"),loglocation);
115
116 if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) {
117 debuga(_("(dansguardian) Cannot open log file: %s\n"),loglocation);
118 exit(EXIT_FAILURE);
119 }
120
121 while(fgets(buf,sizeof(buf),fp_in) != NULL) {
122 if(strstr(buf," *DENIED* ") == 0)
123 continue;
124 getword_start(&gwarea,buf);
125 if (getword(year,sizeof(year),&gwarea,'.')<0 || getword(mon,sizeof(mon),&gwarea,'.')<0 ||
126 getword(day,sizeof(day),&gwarea,' ')<0 || getword(hour,sizeof(hour),&gwarea,' ')<0 ||
127 getword(user,sizeof(user),&gwarea,' ')<0 || getword(ip,sizeof(ip),&gwarea,' ')<0 ||
128 getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) {
129 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation);
130 exit(EXIT_FAILURE);
131 }
132 if (getword_ptr(buf,&url,&gwarea,' ')<0) {
133 debuga(_("Maybe you have a broken url in your %s file\n"),loglocation);
134 exit(EXIT_FAILURE);
135 }
136 if (getword_skip(255,&gwarea,' ')<0 ||
137 getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) {
138 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation);
139 exit(EXIT_FAILURE);
140 }
141 sprintf(wdata,"%s%02d%s",year,atoi(mon),day);
142 idata = atoi(wdata);
143
144 if(DansguardianIgnoreDate) {
145 if(idata < dfrom && idata > duntil)
146 continue;
147 }
148
149 if (strcmp(user,"-") == 0) {
150 strcpy(user,ip);
151 bzero(ip, 30);
152 }
153 fprintf(fp_ou,"%s\t%d\t%s\t%s\t%s\t%s\t%s\n",user,idata,hour,ip,url,code1,code2);
154 dansguardian_count++;
155 }
156
157 if(fp_in) fclose(fp_in);
158 if(fp_guard) fclose(fp_guard);
159 if(fp_ou) fclose(fp_ou);
160
161 if(debug)
162 debuga(_("Sorting file: %s\n"),guard_ou);
163
164 snprintf(tmp6,sizeof(tmp6),"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou);
165 cstatus=system(tmp6);
166 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
167 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
168 debuga(_("sort command: %s\n"),tmp6);
169 exit(EXIT_FAILURE);
170 }
171 unlink(guard_in);
172 }