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