]>
git.ipfire.org Git - thirdparty/sarg.git/blob - exclude.c
915dc1541b1285a828db34876f33646c8322617c
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
)
135 unsigned int value4
, value6
;
136 unsigned short int addr
[8];
140 if(access(hexfile
, R_OK
) != 0) {
141 debuga(_("Cannot open exclude_hosts file: %s - %s\n"),hexfile
,strerror(errno
));
145 debuga(_("Loading exclude host file from: %s\n"),hexfile
);
147 if ((fp_ex
= fopen(hexfile
, "r")) == NULL
) {
148 debuga(_("(gethexclude) Cannot open file %s - %s\n"),hexfile
,strerror(errno
));
152 while(fgets(buf
,sizeof(buf
),fp_ex
)!=NULL
){
160 for (i
=0 ; (unsigned char)buf
[i
]>' ' && buf
[i
]!='/' ; i
++) {
161 if (ip_size
& 0x04) {
162 if (isdigit(buf
[i
])) {
163 value4
=value4
*10+(buf
[i
]-'0');
164 if (value4
>0xFFU
) ip_size
&=~0x04;
165 } else if (buf
[i
]=='.' && addr_len
<4) {
166 addr
[addr_len
++]=(unsigned short)(value4
& 0xFFU
);
172 if (ip_size
& 0x60) {
173 if (isdigit(buf
[i
])) {
174 value6
=(value6
<<4)+(buf
[i
]-'0');
175 if (value6
>0xFFFFU
) ip_size
&=~0x60;
176 } else if (toupper(buf
[i
])>='A' && toupper(buf
[i
])<='F') {
177 value6
=(value6
<<4)+(toupper(buf
[i
])-'A'+10);
178 if (value6
>0xFFFFU
) ip_size
&=~0x60;
179 } else if (buf
[i
]==':' && addr_len
<8) {
180 addr
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
188 if (ip_size
& 0x04) {
192 addr
[addr_len
++]=(unsigned short)(value4
& 0xFFU
);
194 if (ip_size
& 0x60) {
198 addr
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
201 max_mask
=(ip_size
& 0x04) ? 4*8 : 8*16;
204 if (mask
<0 || mask
>max_mask
) mask
=max_mask
;
208 store_exclude_ip4(addr
,mask
);
210 debuga(_("IPv6 addresses are not supported (found in %s)\n"),hexfile
);
214 store_exclude_url(buf
,i
);
222 int vhexclude(const char *url
)
227 unsigned int value4
, value6
;
228 unsigned long int addr4
;
229 unsigned short int addr6
[8];
239 for (i
=0 ; (unsigned char)url
[i
]>' ' && url
[i
]!='/' && url
[i
]!='?'&& ((ip_size
& 0x60)!=0 || url
[i
]!=':') && ip_size
; i
++) {
240 if (ip_size
& 0x04) {
241 if (isdigit(url
[i
])) {
242 value4
=value4
*10+(url
[i
]-'0');
243 if (value4
>0xFFU
) ip_size
&=~0x04;
244 } else if (url
[i
]=='.' && addr_len
<4) {
246 addr4
=(addr4
<<8) | (unsigned long int)(value4
& 0xFFU
);
252 if (ip_size
& 0x60) {
253 if (isdigit(url
[i
])) {
254 value6
=(value6
<<4)+(url
[i
]-'0');
255 if (value6
>0xFFFFU
) ip_size
&=~0x60;
256 } else if (toupper(url
[i
])>='A' && toupper(url
[i
])<='F') {
257 value6
=(value6
<<4)+(toupper(url
[i
])-'A'+10);
258 if (value6
>0xFFFFU
) ip_size
&=~0x60;
259 } else if (url
[i
]==':' && addr_len
<8) {
260 addr6
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
267 if ((ip_size
& 0x04) && addr_len
==3) {
268 if (exclude_ip4
== NULL
) return(1);
269 addr4
=(addr4
<<8) | (unsigned long int)(value4
& 0xFFU
);
270 for (i
=0 ; i
<num_exclude_ip4
; i
++)
271 if (((exclude_ip4
[i
].address
^ addr4
) & exclude_ip4
[i
].mask
)==0) return(0);
272 } else if ((ip_size
& 0x60) && addr_len
<8) {
273 addr6
[addr_len
++]=(unsigned short)(value6
& 0xFFFFU
);
275 if (exclude_name
== NULL
) return(1);
277 for (length
=0 ; (unsigned char)url
[length
]>' ' && url
[length
]!=':' && url
[length
]!='/' && url
[length
]!='?' ; length
++)
278 if (url
[length
]=='.') {
280 We store the position of each dots of the URL to match it against any
281 wildcard in the excluded list. The size of dotpos is big enough for the most
282 ambitious URL but we have a safety mechanism that shift the positions should there be too
283 many dots in the URL.
285 if (ndots
<sizeof(dotpos
)/sizeof(dotpos
[0]))
286 dotpos
[ndots
++]=length
+1;
288 for (j
=1 ; j
<ndots
; j
++) dotpos
[j
-1]=dotpos
[j
];
289 dotpos
[ndots
-1]=length
+1;
293 for (i
=0 ; i
<num_exclude_name
; i
++) {
294 if (exclude_name
[i
].ndots
>0) {
295 const char *wurl
=url
;
297 if (exclude_name
[i
].ndots
<=ndots
) {
298 wurl
+=dotpos
[ndots
-exclude_name
[i
].ndots
];
299 len
-=dotpos
[ndots
-exclude_name
[i
].ndots
];
301 if (strncmp(exclude_name
[i
].url
,wurl
,len
)==0 && exclude_name
[i
].url
[len
]=='\0') return(0);
303 if (strncmp(exclude_name
[i
].url
,url
,length
)==0 && exclude_name
[i
].url
[length
]=='\0') return(0);
313 void getuexclude(const char *uexfile
, int debug
)
321 debuga(_("Loading exclude file from: %s\n"),uexfile
);
323 if ((fp_ex
= fopen(uexfile
, "r")) == NULL
) {
324 debuga(_("(gethexclude) Cannot open file %s - %s\n"),uexfile
,strerror(errno
));
328 if (fseek(fp_ex
, 0, SEEK_END
)==-1) {
329 debuga(_("Failed to move till the end of the excluded users file %s: %s\n"),uexfile
,strerror(errno
));
334 debuga(_("Cannot get the size of file %s\n"),uexfile
);
338 if (fseek(fp_ex
, 0, SEEK_SET
)==-1) {
339 debuga(_("Failed to rewind the excluded users file %s: %s\n"),uexfile
,strerror(errno
));
343 if((excludeuser
=(char *) malloc(nreg
))==NULL
){
344 debuga(_("malloc error (%ld bytes required)\n"),nreg
);
348 bzero(excludeuser
,nreg
);
350 while(fgets(buf
,sizeof(buf
),fp_ex
)!=NULL
){
351 if(strchr(buf
,'#') != NULL
)
354 strcat(excludeuser
,buf
);
355 strcat(excludeuser
," ");
358 strcat(excludeuser
,"*END* ");
365 int vuexclude(const char *user
)
373 while ((wuser
=strstr(wuser
,user
))!=NULL
) {
374 if (wuser
[len
]==' ') return(0);
382 bool is_indexonly(void)
384 if (excludeuser
==NULL
) return(false);
385 return(strstr(excludeuser
,"indexonly") != NULL
);
388 void free_exclude(void)
398 for (i
=0 ; i
<num_exclude_name
; i
++)
399 if (exclude_name
[i
].url
) free(exclude_name
[i
].url
);