]> git.ipfire.org Git - thirdparty/sarg.git/blame - dansguardian_log.c
Check for the proper creation of a directory
[thirdparty/sarg.git] / dansguardian_log.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
1164c474 3 * 1998, 2010
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_log(void)
25697a35 31{
9bd92830
FM
32 FILE *fp_in = NULL, *fp_ou = NULL, *fp_guard = NULL;
33 char buf[MAXLEN];
34 char guard_in[MAXLEN];
35 char guard_ou[MAXLEN];
36 char loglocation[MAXLEN] = "/var/log/dansguardian/access.log";
37 char year[10], mon[10], day[10];
38 char hour[15];
39 char user[MAXLEN], code1[255], code2[255];
40 char ip[30];
41 char wdata[127];
42 char *url;
43 char tmp6[MAXLEN];
44 int idata=0;
45 int cstatus;
46 int dfrom, duntil;
47 struct getwordstruct gwarea;
48
49 dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+period.start.tm_mday;
50 duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.end.tm_mday;
51
52 snprintf(guard_in,sizeof(guard_in),"%s/dansguardian.unsort",tmp);
53 snprintf(guard_ou,sizeof(guard_ou),"%s/dansguardian.log",tmp);
54
55 if(access(DansGuardianConf, R_OK) != 0) {
56 debuga(_("Cannot open DansGuardian config file: %s\n"),DansGuardianConf);
57 exit(EXIT_FAILURE);
58 }
59
60 if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) {
61 debuga(_("(dansguardian) Cannot open log file: %s\n"),DansGuardianConf);
62 exit(EXIT_FAILURE);
63 }
64
65 if((fp_ou=MY_FOPEN(guard_in,"a"))==NULL) {
66 debuga(_("(dansguardian) Cannot open log file: %s\n"),guard_in);
67 exit(EXIT_FAILURE);
68 }
69
70 while(fgets(buf,sizeof(buf),fp_guard)!=NULL) {
71 fixendofline(buf);
72 if(buf[0]=='#')
73 continue;
74 if(strstr(buf,"loglocation ") != 0) {
75 getword_start(&gwarea,buf);
76 if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) {
77 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),DansGuardianConf);
78 exit(EXIT_FAILURE);
79 }
80 break;
81 }
82 }
83
84 if(debug)
85 debuga(_("Reading DansGuardian log file: %s\n"),loglocation);
86
87 if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) {
88 debuga(_("(dansguardian) Cannot open log file: %s\n"),loglocation);
89 exit(EXIT_FAILURE);
90 }
91
92 while(fgets(buf,sizeof(buf),fp_in) != NULL) {
93 if(strstr(buf," *DENIED* ") == 0)
94 continue;
95 getword_start(&gwarea,buf);
96 if (getword(year,sizeof(year),&gwarea,'.')<0 || getword(mon,sizeof(mon),&gwarea,'.')<0 ||
007905af
FM
97 getword(day,sizeof(day),&gwarea,' ')<0 || getword(hour,sizeof(hour),&gwarea,' ')<0 ||
98 getword(user,sizeof(user),&gwarea,' ')<0 || getword(ip,sizeof(ip),&gwarea,' ')<0 ||
99 getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) {
9bd92830
FM
100 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation);
101 exit(EXIT_FAILURE);
102 }
103 if (getword_ptr(buf,&url,&gwarea,' ')<0) {
104 debuga(_("Maybe you have a broken url in your %s file\n"),loglocation);
105 exit(EXIT_FAILURE);
106 }
107 if (getword_skip(255,&gwarea,' ')<0 ||
007905af 108 getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) {
9bd92830
FM
109 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation);
110 exit(EXIT_FAILURE);
111 }
112 sprintf(wdata,"%s%02d%s",year,atoi(mon),day);
113 idata = atoi(wdata);
114
115 if(DansguardianFilterOutDate) {
116 if(idata < dfrom && idata > duntil)
117 continue;
118 }
119
120 if (strcmp(user,"-") == 0) {
121 strcpy(user,ip);
122 bzero(ip, 30);
123 }
124 fprintf(fp_ou,"%s\t%d\t%s\t%s\t%s\t%s\t%s\n",user,idata,hour,ip,url,code1,code2);
125 dansguardian_count++;
126 }
127
128 if(fp_in) fclose(fp_in);
129 if(fp_guard) fclose(fp_guard);
130 if(fp_ou) fclose(fp_ou);
131
132 if(debug)
133 debuga(_("Sorting file: %s\n"),guard_ou);
134
135 snprintf(tmp6,sizeof(tmp6),"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou);
136 cstatus=system(tmp6);
137 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
138 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
139 debuga(_("sort command: %s\n"),tmp6);
140 exit(EXIT_FAILURE);
141 }
142 unlink(guard_in);
25697a35 143}