1010 *
1111 *
1212 * IDENTIFICATION
13- * $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.113 2003/09/05 20:31:35 tgl Exp $
13+ * $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.114 2003/09/05 23:07:21 tgl Exp $
1414 *
1515 *-------------------------------------------------------------------------
1616 */
@@ -550,12 +550,12 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
550550 char * token ;
551551 char * db ;
552552 char * user ;
553- struct addrinfo * file_ip_addr = NULL ,
554- * file_ip_mask = NULL ;
553+ struct addrinfo * gai_result ;
555554 struct addrinfo hints ;
556- struct sockaddr_storage * mask ;
557- char * cidr_slash ;
558555 int ret ;
556+ struct sockaddr_storage addr ;
557+ struct sockaddr_storage mask ;
558+ char * cidr_slash ;
559559
560560 Assert (line != NIL );
561561 line_number = lfirsti (line );
@@ -648,6 +648,7 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
648648 if (cidr_slash )
649649 * cidr_slash = '\0' ;
650650
651+ /* Get the IP address either way */
651652 hints .ai_flags = AI_NUMERICHOST ;
652653 hints .ai_family = PF_UNSPEC ;
653654 hints .ai_socktype = 0 ;
@@ -657,27 +658,30 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
657658 hints .ai_addr = NULL ;
658659 hints .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 {
664664 ereport (LOG ,
665665 (errcode (ERRCODE_CONFIG_FILE_ERROR ),
666666 errmsg ("could not interpret IP address \"%s\" in config file: %s" ,
667667 token , gai_strerror (ret ))));
668668 if (cidr_slash )
669669 * cidr_slash = '/' ;
670+ if (gai_result )
671+ freeaddrinfo_all (hints .ai_family , gai_result );
670672 goto hba_syntax ;
671673 }
672674
673675 if (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 */
677682 if (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 )
681685 goto hba_syntax ;
682686 }
683687 else
@@ -688,55 +692,54 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
688692 goto hba_syntax ;
689693 token = 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 );
693700 goto hba_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 )
698707 goto hba_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 &&
710719 port -> 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 }
715724 else
716725#endif /* HAVE_IPV6 */
717726 {
718- freeaddrinfo_all ( hints . ai_family , file_ip_addr );
727+ /* Line doesn't match client port, so ignore it. */
719728 return ;
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. */
724737 line = lnext (line );
725738 if (!line )
726739 goto hba_syntax ;
727740 parse_hba_auth (line , & port -> auth_method , & port -> auth_arg , error_p );
728741 if (* error_p )
729742 goto hba_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- goto hba_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 }
741744 else
742745 goto hba_syntax ;
@@ -763,12 +766,6 @@ parse_hba(List *line, hbaPort *port, bool *found_p, bool *error_p)
763766 line_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
0 commit comments