]>
git.ipfire.org Git - thirdparty/sarg.git/blob - exclude.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
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.
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.
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.
27 #include "include/conf.h"
28 #include "include/defs.h"
33 unsigned long int address
;
34 //! The mask to match the address of the URL.
35 unsigned long int mask
;
40 //! The URL to match without any leading wildcard.
42 //! The number of dots in the url if a wildcard is present or -1 if the address is complete (no wildcard)
46 static struct hostip4struct
*exclude_ip4
=NULL
;
47 static int num_exclude_ip4
=0;
48 static struct hostnamestruct
*exclude_name
=NULL
;
49 static int num_exclude_name
=0;
50 static int ip4allocated
=0;
51 static int nameallocated
=0;
53 static char *excludeuser
=NULL
;
55 static void store_exclude_ip4(unsigned short int *addr
,int mask
)
59 if (num_exclude_ip4
>=ip4allocated
) {
60 struct hostip4struct
*temp
;
63 temp
=realloc(exclude_ip4
,ip4allocated
*sizeof(*temp
));
65 debuga(_("Not enough memory to store the exlcluded IP addresses\n"));
70 exclude_ip4
[num_exclude_ip4
].address
=0UL;
72 exclude_ip4
[num_exclude_ip4
].address
=(exclude_ip4
[num_exclude_ip4
].address
<<8) | (unsigned char)(addr
[i
] & 0xFFU
);
73 exclude_ip4
[num_exclude_ip4
].mask
=(0xFFFFFFFFUL
<< (32-mask
));
77 static void store_exclude_url(char *url
,int length
)
82 struct hostnamestruct
*item
;
87 for (i
=0 ; i
<length
; i
++)
90 } else if (url
[i
]=='.') {
98 if (start
>=length
|| firstdot
) return;
104 if (num_exclude_name
>=nameallocated
) {
105 struct hostnamestruct
*temp
;
108 temp
=realloc(exclude_name
,nameallocated
*sizeof(*temp
));
110 debuga(_("Not enough memory to store the excluded URLs\n"));
116 item
=exclude_name
+num_exclude_name
;
118 item
->url
=malloc(length
+1);
120 debuga(_("Not enough memory to store the excluded URLs\n"));
123 strncpy(item
->url
,url
,length
);
124 item
->url
[length
]='\0';
125 item
->ndots
=(ndots
>0) ? ndots
: -1;
129 void gethexclude(const char *hexfile
, int debug
)
136 unsigned int value4
, value6
;
137 unsigned short int addr
[8];
141 if(access(hexfile
, R_OK
) != 0) {
142 debuga(_("Cannot open exclude_hosts file: %s - %s\n"),hexfile
,strerror(errno
));
146 debuga(_("Loading exclude file from: %s\n"),hexfile
);
148 if ((fp_ex
= fopen(hexfile
, "r")) == NULL
) {
149 debuga(_("(gethexclude) Cannot open file %s - %s\n"),hexfile
,strerror(errno
));
153 while(fgets(buf
,sizeof(buf
),fp_ex
)!=NULL
){
154 if(strchr(buf
,'#') != NULL
)
161 for (i
=0 ; (unsigned char)buf
[i
]>' ' && buf
[i
]!='/' ; i
++) {
162 if (ip_size
& 0x04) {
163 if (isdigit(buf
[i
])) {
164 value4
=value4
*10+(buf
[i
]-'0');
165 if (value4
>0xFFU
) ip_size
&=~0x04;
166 } else if (buf
[i
]=='.' && addr_len
<4) {
167 addr
[addr_len
++]=(unsigned short)(value4
& 0xFFU
);
173 if (ip_size
& 0x60) {
174 if (isdigit(buf
[i
])) {
175 value6
=(value6
<<4)+(buf
[i
]-'0');
176 if (value6
>0xFFFFU
) ip_size
&=~0x60;
177 } else if (toupper(buf
[i
])>='A' && toupper(buf
[i
])<='F') {
178 value6
=(value6
<<4)+(toupper(buf
[i
])-'A'+10);
179 if (value6
>0xFFFFU
) ip_size
&=~0x60;
180 } else if (buf
[i
]==':' && addr_len
<8) {
181 addr
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
189 if (ip_size
& 0x04) {
193 addr
[addr_len
++]=(unsigned short)(value4
& 0xFFU
);
195 if (ip_size
& 0x60) {
199 addr
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
202 max_mask
=(ip_size
& 0x04) ? 4*8 : 8*16;
205 if (mask
<0 || mask
>max_mask
) mask
=max_mask
;
209 store_exclude_ip4(addr
,mask
);
211 debuga(_("IPv6 addresses are not supported (found in %s)\n"),hexfile
);
215 store_exclude_url(buf
,i
);
224 int vhexclude(const char *url
)
229 unsigned int value4
, value6
;
230 unsigned long int addr4
;
231 unsigned short int addr6
[8];
241 for (i
=0 ; (unsigned char)url
[i
]>' ' && url
[i
]!='/' && url
[i
]!='?'&& ((ip_size
& 0x60)!=0 || url
[i
]!=':') && ip_size
; i
++) {
242 if (ip_size
& 0x04) {
243 if (isdigit(url
[i
])) {
244 value4
=value4
*10+(url
[i
]-'0');
245 if (value4
>0xFFU
) ip_size
&=~0x04;
246 } else if (url
[i
]=='.' && addr_len
<4) {
248 addr4
=(addr4
<<8) | (unsigned long int)(value4
& 0xFFU
);
254 if (ip_size
& 0x60) {
255 if (isdigit(url
[i
])) {
256 value6
=(value6
<<4)+(url
[i
]-'0');
257 if (value6
>0xFFFFU
) ip_size
&=~0x60;
258 } else if (toupper(url
[i
])>='A' && toupper(url
[i
])<='F') {
259 value6
=(value6
<<4)+(toupper(url
[i
])-'A'+10);
260 if (value6
>0xFFFFU
) ip_size
&=~0x60;
261 } else if (url
[i
]==':' && addr_len
<8) {
262 addr6
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
269 if ((ip_size
& 0x04) && addr_len
==3) {
270 if (exclude_ip4
== NULL
) return(1);
271 addr4
=(addr4
<<8) | (unsigned long int)(value4
& 0xFFU
);
272 for (i
=0 ; i
<num_exclude_ip4
; i
++)
273 if (((exclude_ip4
[i
].address
^ addr4
) & exclude_ip4
[i
].mask
)==0) return(0);
274 } else if ((ip_size
& 0x60) && addr_len
<8) {
275 addr6
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
277 if (exclude_name
== NULL
) return(1);
279 for (length
=0 ; (unsigned char)url
[length
]>' ' && url
[length
]!=':' && url
[length
]!='/' && url
[length
]!='?' ; length
++)
280 if (url
[length
]=='.') {
282 We store the position of each dots of the URL to match it against any
283 wildcard in the excluded list. The size of dotpos is big enough for the most
284 ambitious URL but we have a safety mechanism that shift the positions should there be too
285 many dots in the URL.
287 if (ndots
<sizeof(dotpos
)/sizeof(dotpos
[0]))
288 dotpos
[ndots
++]=length
+1;
290 for (j
=1 ; j
<ndots
; j
++) dotpos
[j
-1]=dotpos
[j
];
291 dotpos
[ndots
-1]=length
+1;
295 for (i
=0 ; i
<num_exclude_name
; i
++) {
296 if (exclude_name
[i
].ndots
>0) {
297 const char *wurl
=url
;
299 if (exclude_name
[i
].ndots
<=ndots
) {
300 wurl
+=dotpos
[ndots
-exclude_name
[i
].ndots
];
301 len
-=dotpos
[ndots
-exclude_name
[i
].ndots
];
303 if (strncmp(exclude_name
[i
].url
,wurl
,len
)==0 && exclude_name
[i
].url
[len
]=='\0') return(0);
305 if (strncmp(exclude_name
[i
].url
,url
,length
)==0 && exclude_name
[i
].url
[length
]=='\0') return(0);
315 void getuexclude(const char *uexfile
, int debug
)
323 debuga(_("Loading exclude file from: %s\n"),uexfile
);
325 if ((fp_ex
= fopen(uexfile
, "r")) == NULL
) {
326 debuga(_("(gethexclude) Cannot open file %s - %s\n"),uexfile
,strerror(errno
));
330 fseek(fp_ex
, 0, SEEK_END
);
333 debuga(_("Cannot get the size of file %s\n"),uexfile
);
337 fseek(fp_ex
, 0, SEEK_SET
);
339 if((excludeuser
=(char *) malloc(nreg
))==NULL
){
340 debuga(_("malloc error (%ld bytes required)\n"),nreg
);
344 bzero(excludeuser
,nreg
);
346 while(fgets(buf
,sizeof(buf
),fp_ex
)!=NULL
){
347 if(strchr(buf
,'#') != NULL
)
350 strcat(excludeuser
,buf
);
351 strcat(excludeuser
," ");
354 strcat(excludeuser
,"*END* ");
361 int vuexclude(const char *user
)
369 while ((wuser
=strstr(wuser
,user
))!=NULL
) {
370 if (wuser
[len
]==' ') return(0);
378 int is_indexonly(void)
380 if (excludeuser
==NULL
) return(0);
381 return(strstr(excludeuser
,"indexonly") != NULL
);
384 void free_exclude(void)
394 for (i
=0 ; i
<num_exclude_name
; i
++)
395 if (exclude_name
[i
].url
) free(exclude_name
[i
].url
);