]>
Commit | Line | Data |
---|---|---|
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 | int dfrom, duntil; | |
48 | struct getwordstruct gwarea; | |
49 | ||
50 | dfrom=(period.start.tm_year+1900)*10000+(period.start.tm_mon+1)*100+period.start.tm_mday; | |
51 | duntil=(period.end.tm_year+1900)*10000+(period.end.tm_mon+1)*100+period.end.tm_mday; | |
52 | ||
53 | snprintf(guard_in,sizeof(guard_in),"%s/dansguardian.unsort",tmp); | |
54 | snprintf(guard_ou,sizeof(guard_ou),"%s/dansguardian.log",tmp); | |
55 | ||
56 | if(access(DansGuardianConf, R_OK) != 0) { | |
57 | debuga(_("Cannot open DansGuardian config file: %s\n"),DansGuardianConf); | |
58 | exit(EXIT_FAILURE); | |
59 | } | |
60 | ||
61 | if((fp_guard=fopen(DansGuardianConf,"r"))==NULL) { | |
62 | debuga(_("(dansguardian) Cannot open log file: %s\n"),DansGuardianConf); | |
63 | exit(EXIT_FAILURE); | |
64 | } | |
65 | ||
66 | if((fp_ou=MY_FOPEN(guard_in,"a"))==NULL) { | |
67 | debuga(_("(dansguardian) Cannot open log file: %s\n"),guard_in); | |
68 | exit(EXIT_FAILURE); | |
69 | } | |
70 | ||
71 | while(fgets(buf,sizeof(buf),fp_guard)!=NULL) { | |
72 | fixendofline(buf); | |
73 | if(buf[0]=='#') | |
74 | continue; | |
75 | if(strstr(buf,"loglocation ") != 0) { | |
76 | getword_start(&gwarea,buf); | |
77 | if (getword_skip(MAXLEN,&gwarea,'\'')<0 || getword(loglocation,sizeof(loglocation),&gwarea,'\'')<0) { | |
78 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),DansGuardianConf); | |
79 | exit(EXIT_FAILURE); | |
80 | } | |
81 | break; | |
82 | } | |
83 | } | |
84 | ||
85 | if(debug) | |
86 | debuga(_("Reading DansGuardian log file: %s\n"),loglocation); | |
87 | ||
88 | if((fp_in=MY_FOPEN(loglocation,"r"))==NULL) { | |
89 | debuga(_("(dansguardian) Cannot open log file: %s\n"),loglocation); | |
90 | exit(EXIT_FAILURE); | |
91 | } | |
92 | ||
93 | while(fgets(buf,sizeof(buf),fp_in) != NULL) { | |
94 | if(strstr(buf," *DENIED* ") == 0) | |
95 | continue; | |
96 | getword_start(&gwarea,buf); | |
97 | if (getword(year,sizeof(year),&gwarea,'.')<0 || getword(mon,sizeof(mon),&gwarea,'.')<0 || | |
98 | getword(day,sizeof(day),&gwarea,' ')<0 || getword(hour,sizeof(hour),&gwarea,' ')<0 || | |
99 | getword(user,sizeof(user),&gwarea,' ')<0 || getword(ip,sizeof(ip),&gwarea,' ')<0 || | |
100 | getword_skip(MAXLEN,&gwarea,'/')<0 || getword_skip(MAXLEN,&gwarea,'/')<0) { | |
101 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation); | |
102 | exit(EXIT_FAILURE); | |
103 | } | |
104 | if (getword_ptr(buf,&url,&gwarea,' ')<0) { | |
105 | debuga(_("Maybe you have a broken url in your %s file\n"),loglocation); | |
106 | exit(EXIT_FAILURE); | |
107 | } | |
108 | if (getword_skip(255,&gwarea,' ')<0 || | |
109 | getword(code1,sizeof(code1),&gwarea,' ')<0 || getword(code2,sizeof(code2),&gwarea,' ')<0) { | |
110 | debuga(_("Maybe you have a broken record or garbage in your %s file\n"),loglocation); | |
111 | exit(EXIT_FAILURE); | |
112 | } | |
113 | sprintf(wdata,"%s%02d%s",year,atoi(mon),day); | |
114 | idata = atoi(wdata); | |
115 | ||
116 | if(DansguardianFilterOutDate) { | |
117 | if(idata < dfrom && idata > duntil) | |
118 | continue; | |
119 | } | |
120 | ||
121 | if (strcmp(user,"-") == 0) { | |
122 | strcpy(user,ip); | |
123 | bzero(ip, 30); | |
124 | } | |
125 | fprintf(fp_ou,"%s\t%d\t%s\t%s\t%s\t%s\t%s\n",user,idata,hour,ip,url,code1,code2); | |
126 | dansguardian_count++; | |
127 | } | |
128 | ||
129 | if(fp_in) fclose(fp_in); | |
130 | if(fp_guard) fclose(fp_guard); | |
131 | if(fp_ou) fclose(fp_ou); | |
132 | ||
133 | if(debug) | |
134 | debuga(_("Sorting file: %s\n"),guard_ou); | |
135 | ||
136 | snprintf(tmp6,sizeof(tmp6),"sort -k 1,1 -k 2,2 -k 4,4 \"%s\" -o \"%s\"",guard_in, guard_ou); | |
137 | cstatus=system(tmp6); | |
138 | if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) { | |
139 | debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus)); | |
140 | debuga(_("sort command: %s\n"),tmp6); | |
141 | exit(EXIT_FAILURE); | |
142 | } | |
143 | unlink(guard_in); | |
144 | } |