1010 *
1111 *
1212 * IDENTIFICATION
13- * $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.113 2003/09/0520:31:35 tgl Exp $
13+ * $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.114 2003/09/0523:07:21 tgl Exp $
1414 *
1515 *-------------------------------------------------------------------------
1616 */
@@ -550,12 +550,12 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
550550char * token ;
551551char * db ;
552552char * user ;
553- struct addrinfo * file_ip_addr = NULL ,
554- * file_ip_mask = NULL ;
553+ struct addrinfo * gai_result ;
555554struct addrinfo hints ;
556- struct sockaddr_storage * mask ;
557- char * cidr_slash ;
558555int ret ;
556+ struct sockaddr_storage addr ;
557+ struct sockaddr_storage mask ;
558+ char * cidr_slash ;
559559
560560Assert (line != NIL );
561561line_number = lfirsti (line );
@@ -648,6 +648,7 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
648648if (cidr_slash )
649649* cidr_slash = '\0' ;
650650
651+ /* Get the IP address either way */
651652hints .ai_flags = AI_NUMERICHOST ;
652653hints .ai_family = PF_UNSPEC ;
653654hints .ai_socktype = 0 ;
@@ -657,27 +658,30 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
657658hints .ai_addr = NULL ;
658659hints .ai_next = NULL ;
659660
660- /* Get the IP address either way */
661- ret = getaddrinfo_all (token ,NULL ,& hints ,& file_ip_addr );
662- if (ret || !file_ip_addr )
661+ ret = getaddrinfo_all (token ,NULL ,& hints ,& gai_result );
662+ if (ret || !gai_result )
663663{
664664ereport (LOG ,
665665(errcode (ERRCODE_CONFIG_FILE_ERROR ),
666666errmsg ("could not interpret IP address \"%s\" in config file: %s" ,
667667token ,gai_strerror (ret ))));
668668if (cidr_slash )
669669* cidr_slash = '/' ;
670+ if (gai_result )
671+ freeaddrinfo_all (hints .ai_family ,gai_result );
670672gotohba_syntax ;
671673}
672674
673675if (cidr_slash )
674676* cidr_slash = '/' ;
675677
678+ memcpy (& addr ,gai_result -> ai_addr ,gai_result -> ai_addrlen );
679+ freeaddrinfo_all (hints .ai_family ,gai_result );
680+
676681/* Get the netmask */
677682if (cidr_slash )
678683{
679- if (SockAddr_cidr_mask (& mask ,cidr_slash + 1 ,
680- file_ip_addr -> ai_family )< 0 )
684+ if (SockAddr_cidr_mask (& mask ,cidr_slash + 1 ,addr .ss_family )< 0 )
681685gotohba_syntax ;
682686}
683687else
@@ -688,55 +692,54 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
688692gotohba_syntax ;
689693token = lfirst (line );
690694
691- ret = getaddrinfo_all (token ,NULL ,& hints ,& file_ip_mask );
692- if (ret || !file_ip_mask )
695+ ret = getaddrinfo_all (token ,NULL ,& hints ,& gai_result );
696+ if (ret || !gai_result )
697+ {
698+ if (gai_result )
699+ freeaddrinfo_all (hints .ai_family ,gai_result );
693700gotohba_syntax ;
701+ }
694702
695- mask = (struct sockaddr_storage * )file_ip_mask -> ai_addr ;
703+ memcpy (& mask ,gai_result -> ai_addr ,gai_result -> ai_addrlen );
704+ freeaddrinfo_all (hints .ai_family ,gai_result );
696705
697- if (file_ip_addr -> ai_family != mask -> ss_family )
706+ if (addr . ss_family != mask . ss_family )
698707gotohba_syntax ;
699708}
700709
701- if (file_ip_addr -> ai_family != port -> raddr .addr .ss_family )
710+ if (addr . ss_family != port -> raddr .addr .ss_family )
702711{
703712/*
704713 * Wrong address family. We allow only one case: if the
705714 * file has IPv4 and the port is IPv6, promote the file
706715 * address to IPv6 and try to match that way.
707716 */
708717#ifdef HAVE_IPV6
709- if (file_ip_addr -> ai_family == AF_INET &&
718+ if (addr . ss_family == AF_INET &&
710719port -> raddr .addr .ss_family == AF_INET6 )
711720{
712- promote_v4_to_v6_addr (( struct sockaddr_storage * ) file_ip_addr -> ai_addr );
713- promote_v4_to_v6_mask (mask );
721+ promote_v4_to_v6_addr (& addr );
722+ promote_v4_to_v6_mask (& mask );
714723}
715724else
716725#endif /* HAVE_IPV6 */
717726{
718- freeaddrinfo_all ( hints . ai_family , file_ip_addr );
727+ /* Line doesn't match client port, so ignore it. */
719728return ;
720729}
721730}
722731
732+ /* Ignore line if client port is not in the matching addr range. */
733+ if (!rangeSockAddr (& port -> raddr .addr ,& addr ,& mask ))
734+ return ;
735+
723736/* Read the rest of the line. */
724737line = lnext (line );
725738if (!line )
726739gotohba_syntax ;
727740parse_hba_auth (line ,& port -> auth_method ,& port -> auth_arg ,error_p );
728741if (* error_p )
729742gotohba_syntax ;
730-
731- /* Must meet network restrictions */
732- if (!rangeSockAddr (& port -> raddr .addr ,
733- (struct sockaddr_storage * )file_ip_addr -> ai_addr ,
734- mask ))
735- gotohba_freeaddr ;
736-
737- freeaddrinfo_all (hints .ai_family ,file_ip_addr );
738- if (file_ip_mask )
739- freeaddrinfo_all (hints .ai_family ,file_ip_mask );
740743}
741744else
742745gotohba_syntax ;
@@ -763,12 +766,6 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
763766line_number )));
764767
765768* error_p = true;
766-
767- hba_freeaddr :
768- if (file_ip_addr )
769- freeaddrinfo_all (hints .ai_family ,file_ip_addr );
770- if (file_ip_mask )
771- freeaddrinfo_all (hints .ai_family ,file_ip_mask );
772769}
773770
774771