]> git.ipfire.org Git - thirdparty/sarg.git/blob - exclude.c
Add more messages to be translated and fix some difficult to translate messages
[thirdparty/sarg.git] / exclude.c
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 struct hostip4struct
31 {
32 //! The IP address.
33 unsigned long int address;
34 //! The mask to match the address of the URL.
35 unsigned long int mask;
36 };
37
38 struct hostnamestruct
39 {
40 //! The URL to match without any leading wildcard.
41 char *url;
42 //! The number of dots in the url if a wildcard is present or -1 if the address is complete (no wildcard)
43 int ndots;
44 };
45
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;
52
53 static char *excludeuser=NULL;
54
55 static void store_exclude_ip4(unsigned short int *addr,int mask)
56 {
57 int i;
58
59 if (num_exclude_ip4>=ip4allocated) {
60 struct hostip4struct *temp;
61
62 ip4allocated+=5;
63 temp=realloc(exclude_ip4,ip4allocated*sizeof(*temp));
64 if (temp==NULL) {
65 debuga(_("Not enough memory to store the exlcluded IP addresses\n"));
66 exit(1);
67 }
68 exclude_ip4=temp;
69 }
70 exclude_ip4[num_exclude_ip4].address=0UL;
71 for (i=0 ; i<4 ; i++)
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));
74 num_exclude_ip4++;
75 }
76
77 static void store_exclude_url(char *url,int length)
78 {
79 int start;
80 int i;
81 int ndots, firstdot;
82 struct hostnamestruct *item;
83
84 start=0;
85 ndots=-1;
86 firstdot=0;
87 for (i=0 ; i<length ; i++)
88 if (url[i]=='*') {
89 firstdot=1;
90 } else if (url[i]=='.') {
91 if (firstdot) {
92 firstdot=0;
93 ndots=1;
94 start=i+1;
95 } else if (ndots>=0)
96 ndots++;
97 }
98 if (start>=length || firstdot) return;
99 if (start>0) {
100 url+=start;
101 length-=start;
102 }
103
104 if (num_exclude_name>=nameallocated) {
105 struct hostnamestruct *temp;
106
107 nameallocated+=5;
108 temp=realloc(exclude_name,nameallocated*sizeof(*temp));
109 if (temp==NULL) {
110 debuga(_("Not enough memory to store the excluded URLs\n"));
111 exit(1);
112 }
113 exclude_name=temp;
114 }
115
116 item=exclude_name+num_exclude_name;
117 num_exclude_name++;
118 item->url=malloc(length+1);
119 if (!item->url) {
120 debuga(_("Not enough memory to store the excluded URLs\n"));
121 exit(1);
122 }
123 strncpy(item->url,url,length);
124 item->url[length]='\0';
125 item->ndots=(ndots>0) ? ndots : -1;
126 }
127
128
129 void gethexclude(const char *hexfile, int debug)
130 {
131
132 FILE *fp_ex;
133 char buf[255];
134 int i;
135 int ip_size;
136 unsigned int value4, value6;
137 unsigned short int addr[8];
138 int addr_len;
139 int mask, max_mask;
140
141 if(access(hexfile, R_OK) != 0) {
142 debuga(_("Cannot open exclude_hosts file: %s - %s\n"),hexfile,strerror(errno));
143 exit(1);
144 }
145 if(debug)
146 debuga(_("Loading exclude file from: %s\n"),hexfile);
147
148 if ((fp_ex = fopen(hexfile, "r")) == NULL) {
149 debuga(_("(gethexclude) Cannot open file %s - %s\n"),hexfile,strerror(errno));
150 exit(1);
151 }
152
153 while(fgets(buf,sizeof(buf),fp_ex)!=NULL){
154 if(strchr(buf,'#') != NULL)
155 continue;
156 fixendofline(buf);
157 ip_size=0x60 | 0x04;
158 value4=0U;
159 value6=0U;
160 addr_len=0;
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);
168 value4=0U;
169 } else {
170 ip_size&=~0x04;
171 }
172 }
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);
182 value6=0U;
183 } else {
184 ip_size&=~0x60;
185 }
186 }
187 }
188 if (i==0) continue;
189 if (ip_size & 0x04) {
190 if (addr_len!=3)
191 ip_size&=~0x04;
192 else
193 addr[addr_len++]=(unsigned short)(value4 & 0xFFU);
194 }
195 if (ip_size & 0x60) {
196 if (addr_len>=8)
197 ip_size&=~0x60;
198 else
199 addr[addr_len++]=(unsigned short)(value6 & 0xFFFFU);
200 }
201 if (ip_size) {
202 max_mask=(ip_size & 0x04) ? 4*8 : 8*16;
203 if (buf[i]=='/') {
204 mask=atoi(buf+i+1);
205 if (mask<0 || mask>max_mask) mask=max_mask;
206 } else
207 mask=max_mask;
208 if (ip_size & 0x04)
209 store_exclude_ip4(addr,mask);
210 else {
211 debuga(_("IPv6 addresses are not supported (found in %s)\n"),hexfile);
212 exit(1);
213 }
214 } else {
215 store_exclude_url(buf,i);
216 }
217 }
218
219 fclose(fp_ex);
220
221 return;
222 }
223
224 int vhexclude(const char *url)
225 {
226 int i, j;
227 int length;
228 int ip_size;
229 unsigned int value4, value6;
230 unsigned long int addr4;
231 unsigned short int addr6[8];
232 int addr_len;
233 int dotpos[10];
234 int ndots;
235
236 ip_size=0x60 | 0x04;
237 addr4=0UL;
238 value4=0U;
239 value6=0U;
240 addr_len=0;
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) {
247 addr_len++;
248 addr4=(addr4<<8) | (unsigned long int)(value4 & 0xFFU);
249 value4=0U;
250 } else {
251 ip_size&=~0x04;
252 }
253 }
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);
263 value6=0U;
264 } else {
265 ip_size&=~0x60;
266 }
267 }
268 }
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);
276 } else {
277 if (exclude_name == NULL) return(1);
278 ndots=0;
279 for (length=0 ; (unsigned char)url[length]>' ' && url[length]!=':' && url[length]!='/' && url[length]!='?' ; length++)
280 if (url[length]=='.') {
281 /*
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.
286 */
287 if (ndots<sizeof(dotpos)/sizeof(dotpos[0]))
288 dotpos[ndots++]=length+1;
289 else {
290 for (j=1 ; j<ndots ; j++) dotpos[j-1]=dotpos[j];
291 dotpos[ndots-1]=length+1;
292 }
293 }
294 if (length>0) {
295 for (i=0 ; i<num_exclude_name ; i++) {
296 if (exclude_name[i].ndots>0) {
297 const char *wurl=url;
298 int len=length;
299 if (exclude_name[i].ndots<=ndots) {
300 wurl+=dotpos[ndots-exclude_name[i].ndots];
301 len-=dotpos[ndots-exclude_name[i].ndots];
302 }
303 if (strncmp(exclude_name[i].url,wurl,len)==0 && exclude_name[i].url[len]=='\0') return(0);
304 } else {
305 if (strncmp(exclude_name[i].url,url,length)==0 && exclude_name[i].url[length]=='\0') return(0);
306 }
307 }
308 }
309 }
310
311 return(1);
312 }
313
314
315 void getuexclude(const char *uexfile, int debug)
316 {
317
318 FILE *fp_ex;
319 char buf[255];
320 long int nreg=0;
321
322 if(debug)
323 debuga(_("Loading exclude file from: %s\n"),uexfile);
324
325 if ((fp_ex = fopen(uexfile, "r")) == NULL) {
326 debuga(_("(gethexclude) Cannot open file %s - %s\n"),uexfile,strerror(errno));
327 exit(1);
328 }
329
330 fseek(fp_ex, 0, SEEK_END);
331 nreg = ftell(fp_ex);
332 if (nreg<0) {
333 debuga(_("Cannot get the size of file %s\n"),uexfile);
334 exit(1);
335 }
336 nreg += 11;
337 fseek(fp_ex, 0, SEEK_SET);
338
339 if((excludeuser=(char *) malloc(nreg))==NULL){
340 debuga(_("malloc error (%ld bytes required)\n"),nreg);
341 exit(1);
342 }
343
344 bzero(excludeuser,nreg);
345
346 while(fgets(buf,sizeof(buf),fp_ex)!=NULL){
347 if(strchr(buf,'#') != NULL)
348 continue;
349 fixendofline(buf);
350 strcat(excludeuser,buf);
351 strcat(excludeuser," ");
352 }
353
354 strcat(excludeuser,"*END* ");
355
356 fclose(fp_ex);
357
358 return;
359 }
360
361 int vuexclude(const char *user)
362 {
363 const char *wuser;
364 int len;
365
366 if (excludeuser) {
367 len=strlen(user);
368 wuser=excludeuser;
369 while ((wuser=strstr(wuser,user))!=NULL) {
370 if (wuser[len]==' ') return(0);
371 wuser+=len;
372 }
373 }
374
375 return(1);
376 }
377
378 int is_indexonly(void)
379 {
380 if (excludeuser==NULL) return(0);
381 return(strstr(excludeuser,"indexonly") != NULL);
382 }
383
384 void free_exclude(void)
385 {
386 int i;
387
388 if (exclude_ip4) {
389 free(exclude_ip4);
390 exclude_ip4=NULL;
391 }
392
393 if (exclude_name) {
394 for (i=0 ; i<num_exclude_name ; i++)
395 if (exclude_name[i].url) free(exclude_name[i].url);
396 free(exclude_name);
397 exclude_name=NULL;
398 }
399
400 if(excludeuser) {
401 free(excludeuser);
402 excludeuser=NULL;
403 }
404 }