]>
git.ipfire.org Git - thirdparty/sarg.git/blob - url.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 #error "PCRE not compiled in"
37 A host name and the name to report.
41 //! The next host name in the list or NULL for the last item.
42 struct hostalias_name
*Next
;
43 //! The minimum length of a candidate host name.
45 //! The length of the constant part at the beginning of the mask.
47 //! The length of the constant part at the end of the mask.
49 //! The first part of the mask of the host name.
50 const char *HostName_Prefix
;
51 //! The second part of the mask of the host name.
52 const char *HostName_Suffix
;
53 //! The replacement name.
58 An IPv4 address and the name to report.
62 //! The next host name in the list or NULL for the last item.
63 struct hostalias_ipv4
*Next
;
66 //! The number of bits in the prefix.
68 //! The replacement name.
73 An IPv6 address and the name to report.
77 //! The next host name in the list or NULL for the last item.
78 struct hostalias_ipv6
*Next
;
81 //! The number of bits in the prefix.
83 //! The replacement name.
91 struct hostalias_regex
93 //! The next regular expression to test.
94 struct hostalias_regex
*Next
;
95 //! The regular expression to match against the host name.
97 //! The replacement name.
102 //! The first host name.
103 static struct hostalias_name
*FirstAliasName
=NULL
;
104 //! The first IPv4 address.
105 static struct hostalias_ipv4
*FirstAliasIpv4
=NULL
;
106 //! The first IPvĀ§ address.
107 static struct hostalias_ipv6
*FirstAliasIpv6
=NULL
;
110 static struct hostalias_regex
*FirstAliasRe
=NULL
;
114 Store a name to alias.
116 \param name The name to match including the wildcard.
117 \param next A pointer to the first character after the name.
119 \retval 1 Alias added.
120 \retval 0 Ignore the line.
123 static int Alias_StoreName(const char *name
,const char *next
)
125 const char *NameBegin
;
126 const char *NameBeginE
;
128 const char *NameEndE
;
130 const char *ReplaceE
;
133 struct hostalias_name
*alias
;
134 struct hostalias_name
*new_alias
;
135 struct hostalias_name
*prev_alias
;
139 if (*name
=='#' || *name
==';') return(0);
141 // get host name and split at the wildcard
143 for (str
=NameBegin
; str
<next
&& (unsigned char)*str
>' ' && *str
!='*' ; str
++);
145 if (NameBegin
==NameBeginE
) NameBegin
=NULL
;
146 if (str
<next
&& *str
=='*') {
148 while (str
<next
&& (unsigned char)*str
>' ') {
150 debuga(_("Host name alias \"%s*%s\" contains too many wildcards (*)\n"),NameBegin
,NameEnd
);
156 if (NameEnd
==NameEndE
) {
157 debuga(_("Host name alias \"%*s\" must not end with a wildcard\n"),(int)(next
-name
),name
);
163 while (str
<next
&& (unsigned char)*str
<=' ') str
++;
164 if (!NameBegin
&& !NameEnd
) return(0);
168 if (sep
==' ' || sep
=='\t') {
170 while (*Replace
==' ' || *Replace
=='\t') Replace
++;
171 if ((unsigned char)*Replace
<' ') {
174 for (str
=Replace
; *str
&& (unsigned char)*str
>=' ' ; str
++);
181 len
=(int)(NameBeginE
-NameBegin
);
184 debuga(_("Not enough memory to store the host name aliasing directives\n"));
187 memcpy(tmp
,NameBegin
,len
);
192 len
=(int)(NameEndE
-NameEnd
);
195 if (NameBegin
) free((void*)NameBegin
);
196 debuga(_("Not enough memory to store the host name aliasing directives\n"));
199 memcpy(tmp
,NameEnd
,len
);
206 for (alias
=FirstAliasName
; alias
; alias
=alias
->Next
) {
207 if (((NameBegin
&& alias
->HostName_Prefix
&& !strcmp(NameBegin
,alias
->HostName_Prefix
)) || (!NameBegin
&& !alias
->HostName_Prefix
)) &&
208 ((NameEnd
&& alias
->HostName_Suffix
&& !strcmp(NameEnd
,alias
->HostName_Suffix
)) || (!NameEnd
&& !alias
->HostName_Suffix
))) {
209 if (NameBegin
) free((void*)NameBegin
);
215 // insert into the list
216 new_alias
=malloc(sizeof(*new_alias
));
218 if (NameBegin
) free((void*)NameBegin
);
219 if (NameEnd
) free((void*)NameEnd
);
220 debuga(_("Not enough memory to store the host name aliasing directives\n"));
225 new_alias
->HostName_Prefix
=NameBegin
;
226 new_alias
->MinLen
+=strlen(NameBegin
);
227 new_alias
->PrefixLen
=strlen(NameBegin
);
229 new_alias
->HostName_Prefix
=NULL
;
230 new_alias
->PrefixLen
=0;
233 new_alias
->HostName_Suffix
=NameEnd
;
234 new_alias
->MinLen
+=strlen(NameEnd
)+1;
235 new_alias
->SuffixLen
=strlen(NameEnd
);
237 new_alias
->HostName_Suffix
=NULL
;
238 new_alias
->SuffixLen
=0;
241 len
=(int)(ReplaceE
-Replace
);
244 debuga(_("Not enough memory to store the host name aliasing directives\n"));
248 memcpy(tmp
+1,Replace
,len
);
250 new_alias
->Alias
=tmp
;
252 tmp
=malloc(new_alias
->MinLen
+2);
254 debuga(_("Not enough memory to store the host name aliasing directives\n"));
258 if (new_alias
->HostName_Prefix
) strcpy(tmp
+1,new_alias
->HostName_Prefix
);
259 if (new_alias
->HostName_Suffix
) {
260 tmp
[new_alias
->PrefixLen
+1]='*';
261 strcpy(tmp
+new_alias
->PrefixLen
+2,new_alias
->HostName_Suffix
);
263 new_alias
->Alias
=tmp
;
266 new_alias
->Next
=NULL
;
268 prev_alias
->Next
=new_alias
;
270 FirstAliasName
=new_alias
;
275 Store a IPv4 to alias.
277 \param ipv4 The IPv4 to match.
278 \param nbits The number of bits in the prefix
279 \param next A pointer to the first character after the address.
281 \retval 1 Alias added.
282 \retval 0 Ignore the line.
285 static int Alias_StoreIpv4(unsigned char *ipv4
,int nbits
,const char *next
)
288 const char *ReplaceE
;
290 struct hostalias_ipv4
*alias
;
291 struct hostalias_ipv4
*new_alias
;
292 struct hostalias_ipv4
*prev_alias
;
299 while (*Replace
==' ' || *Replace
=='\t') Replace
++;
300 if ((unsigned char)*Replace
<' ') {
303 for (str
=Replace
; *str
&& (unsigned char)*str
>=' ' ; str
++);
307 // store more restrictive range first
309 for (alias
=FirstAliasIpv4
; alias
; alias
=alias
->Next
) {
310 i
=(nbits
<alias
->NBits
) ? nbits
: alias
->NBits
;
311 if ((i
<8 || memcmp(ipv4
,alias
->Ip
,i
/8)==0) && ((i
%8)==0 || (ipv4
[i
/8] ^ alias
->Ip
[i
/8]) & (0xFFU
<<(8-i
%8)))==0) {
317 // insert into the list
318 new_alias
=malloc(sizeof(*new_alias
));
320 debuga(_("Not enough memory to store the host name aliasing directives\n"));
323 memcpy(new_alias
->Ip
,ipv4
,4);
324 new_alias
->NBits
=nbits
;
326 len
=(int)(ReplaceE
-Replace
);
329 debuga(_("Not enough memory to store the host name aliasing directives\n"));
333 memcpy(tmp
+1,Replace
,len
);
335 new_alias
->Alias
=tmp
;
339 debuga(_("Not enough memory to store the host name aliasing directives\n"));
342 sprintf(tmp
,"%c%d.%d.%d.%d/%d",ALIAS_PREFIX
,ipv4
[0],ipv4
[1],ipv4
[2],ipv4
[3],nbits
);
343 new_alias
->Alias
=tmp
;
347 new_alias
->Next
=prev_alias
->Next
;
348 prev_alias
->Next
=new_alias
;
350 new_alias
->Next
=NULL
;
351 FirstAliasIpv4
=new_alias
;
357 Store a IPv6 to alias.
359 \param ipv6 The IPv6 to match.
360 \param nbits The number of bits in the prefix
361 \param next A pointer to the first character after the address.
363 \retval 1 Alias added.
364 \retval 0 Ignore the line.
367 static int Alias_StoreIpv6(unsigned short *ipv6
,int nbits
,const char *next
)
370 const char *ReplaceE
;
372 struct hostalias_ipv6
*alias
;
373 struct hostalias_ipv6
*new_alias
;
374 struct hostalias_ipv6
*prev_alias
;
381 while (*Replace
==' ' || *Replace
=='\t') Replace
++;
382 if ((unsigned char)*Replace
<' ') {
385 for (str
=Replace
; *str
&& (unsigned char)*str
>=' ' ; str
++);
389 // store more restrictive range first
391 for (alias
=FirstAliasIpv6
; alias
; alias
=alias
->Next
) {
392 i
=(nbits
<alias
->NBits
) ? nbits
: alias
->NBits
;
393 if ((i
<16 || memcmp(ipv6
,alias
->Ip
,i
/16*2)==0) && ((i
%16)==0 || (ipv6
[i
/16] ^ alias
->Ip
[i
/16]) & (0xFFFFU
<<(16-i
%16)))==0) {
399 // insert into the list
400 new_alias
=malloc(sizeof(*new_alias
));
402 debuga(_("Not enough memory to store the host name aliasing directives\n"));
405 memcpy(new_alias
->Ip
,ipv6
,8*sizeof(unsigned short int));
406 new_alias
->NBits
=nbits
;
408 len
=ReplaceE
-Replace
;
411 debuga(_("Not enough memory to store the host name aliasing directives\n"));
415 memcpy(tmp
+1,Replace
,len
);
417 new_alias
->Alias
=tmp
;
421 debuga(_("Not enough memory to store the host name aliasing directives\n"));
424 sprintf(tmp
,"%c%x:%x:%x:%x:%x:%x:%x:%x/%d",ALIAS_PREFIX
,ipv6
[0],ipv6
[1],ipv6
[2],ipv6
[3],ipv6
[4],ipv6
[5],ipv6
[6],ipv6
[7],nbits
);
425 new_alias
->Alias
=tmp
;
429 new_alias
->Next
=prev_alias
->Next
;
430 prev_alias
->Next
=new_alias
;
432 new_alias
->Next
=NULL
;
433 FirstAliasIpv6
=new_alias
;
440 Store a regular expression to match the alias.
442 \retval 1 Alias added.
443 \retval 0 Ignore the line.
446 static int Alias_StoreRegexp(char *buf
)
450 struct hostalias_regex
*alias
;
451 struct hostalias_regex
*new_alias
;
452 struct hostalias_regex
**prev_alias
;
453 const char *PcreError
;
461 for (End
=buf
; *End
&& *End
!=Delimiter
; End
++) {
464 debuga(_("Invalid NUL character found in regular expression\n"));
467 End
++; //ignore the escaped character
470 if (*End
!=Delimiter
) {
471 debuga(_("Unterminated regular expression\n"));
477 for (Replace
=End
; *Replace
==' ' || *Replace
=='\t' ; Replace
++);
478 for (End
=Replace
; *End
&& (unsigned char)*End
>' ' ; End
++);
482 new_alias
=malloc(sizeof(*new_alias
));
484 debuga(_("Not enough memory to store the host name aliasing directives\n"));
487 new_alias
->Next
=NULL
;
488 new_alias
->Re
=pcre_compile(buf
,0,&PcreError
,&ErrorOffset
,NULL
);
489 if (new_alias
->Re
==NULL
) {
490 debuga(_("Failed to compile the regular expression: %s\n"),PcreError
);
497 debuga(_("Not enough memory to store the host name aliasing directives\n"));
498 pcre_free(new_alias
->Re
);
502 memcpy(tmp
+1,Replace
,len
);
504 new_alias
->Alias
=tmp
;
507 prev_alias
=&FirstAliasRe
;
508 for (alias
=FirstAliasRe
; alias
; alias
=alias
->Next
)
509 prev_alias
=&alias
->Next
;
510 *prev_alias
=new_alias
;
517 Store an alias in the corresponding list.
519 \param buf The string to parse and store.
522 \retval -1 Error in file.
524 static int Alias_Store(char *buf
)
528 unsigned char ipv4
[4];
529 unsigned short int ipv6
[8];
535 if (strncasecmp(buf
,"re:",3)==0) {
536 if (Alias_StoreRegexp(buf
+3)<0)
541 type
=extract_address_mask(buf
,&name
,ipv4
,ipv6
,&nbits
,&next
);
547 Error
=Alias_StoreName(name
,next
);
548 } else if (type
==2) {
549 Error
=Alias_StoreIpv4(ipv4
,nbits
,next
);
550 } else if (type
==3) {
551 Error
=Alias_StoreIpv6(ipv6
,nbits
,next
);
553 if (Error
<0) return(-1);
558 Read the file containing the host names to alias in the report.
560 \param Filename The name of the file.
562 void read_hostalias(const char *Filename
)
568 if (debug
) debuga(_("Reading host alias file \"%s\"\n"),Filename
);
569 fi
=fopen(Filename
,"rt");
571 debuga(_("Cannot read host name alias file \"%s\" - %s\n"),Filename
,strerror(errno
));
575 if ((line
=longline_create())==NULL
) {
576 debuga(_("Not enough memory to read the host name aliases\n"));
580 while ((buf
=longline_read(fi
,line
)) != NULL
) {
581 if (Alias_Store(buf
)<0) {
582 debuga(_("While reading \"%s\"\n"),Filename
);
587 longline_destroy(&line
);
591 struct hostalias_name
*alias1
;
592 struct hostalias_ipv4
*alias4
;
593 struct hostalias_ipv6
*alias6
;
595 debuga(_("List of host names to alias:\n"));
596 for (alias1
=FirstAliasName
; alias1
; alias1
=alias1
->Next
) {
597 if (alias1
->HostName_Prefix
&& alias1
->HostName_Suffix
)
598 debuga(_(" %s*%s => %s\n"),alias1
->HostName_Prefix
,alias1
->HostName_Suffix
,alias1
->Alias
);
599 else if (alias1
->HostName_Prefix
)
600 debuga(_(" %s => %s\n"),alias1
->HostName_Prefix
,alias1
->Alias
);
602 debuga(_(" *%s => %s\n"),alias1
->HostName_Suffix
,alias1
->Alias
);
604 for (alias4
=FirstAliasIpv4
; alias4
; alias4
=alias4
->Next
) {
605 debuga(_(" %d.%d.%d.%d/%d => %s\n"),alias4
->Ip
[0],alias4
->Ip
[1],alias4
->Ip
[2],alias4
->Ip
[3],alias4
->NBits
,alias4
->Alias
);
607 for (alias6
=FirstAliasIpv6
; alias6
; alias6
=alias6
->Next
) {
608 debuga(_(" %x:%x:%x:%x:%x:%x:%x:%x/%d => %s\n"),alias6
->Ip
[0],alias6
->Ip
[1],alias6
->Ip
[2],alias6
->Ip
[3],
609 alias6
->Ip
[4],alias6
->Ip
[5],alias6
->Ip
[6],alias6
->Ip
[7],alias6
->NBits
,alias6
->Alias
);
615 Free the memory allocated by read_hostalias().
617 void free_hostalias(void)
620 struct hostalias_name
*alias1
;
621 struct hostalias_name
*next1
;
623 for (alias1
=FirstAliasName
; alias1
; alias1
=next1
) {
625 if (alias1
->HostName_Prefix
) free((void *)alias1
->HostName_Prefix
);
626 if (alias1
->HostName_Suffix
) free((void *)alias1
->HostName_Suffix
);
627 free((void *)alias1
->Alias
);
633 struct hostalias_ipv4
*alias4
;
634 struct hostalias_ipv4
*next4
;
636 for (alias4
=FirstAliasIpv4
; alias4
; alias4
=next4
) {
638 free((void *)alias4
->Alias
);
644 struct hostalias_ipv6
*alias6
;
645 struct hostalias_ipv6
*next6
;
647 for (alias6
=FirstAliasIpv6
; alias6
; alias6
=next6
) {
649 free((void *)alias6
->Alias
);
656 struct hostalias_regex
*alias
;
657 struct hostalias_regex
*next
;
659 for (alias
=FirstAliasRe
; alias
; alias
=next
) {
661 pcre_free(alias
->Re
);
662 free((void *)alias
->Alias
);
671 Replace the host name by its alias if it is in our list.
673 \param url The host name.
675 \return The pointer to the host name or its alias.
677 static const char *alias_url_name(const char *url
,const char *next
)
679 struct hostalias_name
*alias
;
683 for (alias
=FirstAliasName
; alias
; alias
=alias
->Next
) {
684 if (len
<alias
->MinLen
) continue;
685 if (alias
->HostName_Prefix
) {
686 if (alias
->HostName_Suffix
) {
687 if (strncasecmp(url
,alias
->HostName_Prefix
,alias
->PrefixLen
)==0 &&
688 strncasecmp(url
+(len
-alias
->SuffixLen
),alias
->HostName_Suffix
,len
)==0) {
689 return(alias
->Alias
);
692 if (len
==alias
->PrefixLen
&& strncasecmp(url
,alias
->HostName_Prefix
,len
)==0) {
693 return(alias
->Alias
);
696 } else if (strncasecmp(url
+(len
-alias
->SuffixLen
),alias
->HostName_Suffix
,len
)==0) {
697 return(alias
->Alias
);
704 Replace the IPv4 address by its alias if it is in our list.
706 \param url The host name.
707 \param ipv4 The address.
709 \return The pointer to the host name or its alias.
711 static const char *alias_url_ipv4(const char *url
,unsigned char *ipv4
)
713 struct hostalias_ipv4
*alias
;
716 for (alias
=FirstAliasIpv4
; alias
; alias
=alias
->Next
) {
718 if ((len
<8 || memcmp(ipv4
,alias
->Ip
,len
/8)==0) && ((len
%8)==0 || (ipv4
[len
/8] ^ alias
->Ip
[len
/8]) & (0xFFU
<<(8-len
%8)))==0) {
719 return(alias
->Alias
);
726 Replace the IPv6 address by its alias if it is in our list.
728 \param url The host name.
729 \param ipv6 The address.
731 \return The pointer to the host name or its alias.
733 static const char *alias_url_ipv6(const char *url
,unsigned short int *ipv6
)
735 struct hostalias_ipv6
*alias
;
739 for (alias
=FirstAliasIpv6
; alias
; alias
=alias
->Next
) {
741 for (i
=len
/16-1 ; i
>=0 && ipv6
[i
]==alias
->Ip
[i
] ; i
--);
744 if (i
>=8 || len
%16==0 || ((ipv6
[i
] ^ alias
->Ip
[i
]) & (0xFFFF<<(len
-i
*16)))==0) {
745 return(alias
->Alias
);
753 Replace the host name by its alias if it is in our list.
755 \param url The host name.
757 \return The pointer to the host name or its alias.
759 static const char *alias_url_regex(const char *url
)
761 struct hostalias_regex
*alias
;
763 for (alias
=FirstAliasRe
; alias
; alias
=alias
->Next
) {
764 if (pcre_exec(alias
->Re
,NULL
,url
,strlen(url
),0,0,NULL
,0)==0) {
765 return(alias
->Alias
);
773 Find the beginning of the URL beyond the scheme://
775 \param url The url possibly containing a scheme.
777 \return The beginning of the url beyond the scheme.
779 const char *skip_scheme(const char *url
)
784 Skip any scheme:// at the beginning of the URL (see rfc2396 section 3.1).
785 The underscore is not part of the standard but is found in the squid logs as cache_object://.
787 for (str
=url
; *str
&& (isalnum(*str
) || *str
=='+' || *str
=='-' || *str
=='.' || *str
=='_') ; str
++);
788 if (str
[0]==':' && str
[1]=='/' && str
[2]=='/') {
790 while (*url
=='/') url
++;
796 Get the part of the URL necessary to generate the report.
798 \param url The URL as extracted from the report.
799 \param full_url \c True to keep the whole URL. If \c false,
800 the URL is truncated to only keep the host name and port number.
802 const char *process_url(char *url
,bool full_url
)
808 unsigned char ipv4
[4];
809 unsigned short int ipv6
[8];
812 start
=skip_scheme(url
);
814 for (str
=(char *)start
; *str
&& *str
!='/' && *str
!='?' ; str
++);
818 address
=alias_url_regex(start
);
819 if (address
!=start
) return(address
);
822 type
=extract_address_mask(start
,&address
,ipv4
,ipv6
,NULL
,&next
);
825 start
=alias_url_name(start
,next
);
826 } else if (type
==2) {
828 start
=alias_url_ipv4(start
,ipv4
);
829 } else if (type
==3) {
831 start
=alias_url_ipv6(start
,ipv6
);
838 Extract the host name from the URL.
840 \param url The url whose host name must be extracted.
841 \param hostname The buffer to store the host name.
842 \param hostsize The size of the host name buffer.
844 \note The function is stupid at this time. It just searches for the first slash
845 in the URL and truncates the URL there. It doesn't take the protocol into account
846 nor the port number nor any user or password information.
848 void url_hostname(const char *url
,char *hostname
,int hostsize
)
853 for (i
=0 ; i
<hostsize
&& url
[i
] && url
[i
]!='/' ; i
++)