openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/rsyslog/8.1903.0-r0/rsyslog-8.1903.0/runtime/net.c
static rsRetVal AddAllowedSender(struct AllowedSenders** ppRoot,
                                 struct AllowedSenders** ppLast,
                                 struct NetAddr* iAllow,
                                 uint8_t iSignificantBits)
{
    struct addrinfo* restmp = NULL;
    DEFiRet;

    assert(ppRoot != NULL);
    assert(ppLast != NULL);
    assert(iAllow != NULL);

    if (!F_ISSET(iAllow->flags, ADDR_NAME))
    {
        if (iSignificantBits == 0)
            /* we handle this seperatly just to provide a better
             * error message.
             */
            LogError(0, NO_ERRCODE,
                     "You can not specify 0 bits of the netmask, this would "
                     "match ALL systems. If you really intend to do that, "
                     "remove all $AllowedSender directives.");

        switch (iAllow->addr.NetAddr->sa_family)
        {
            case AF_INET:
                if ((iSignificantBits < 1) || (iSignificantBits > 32))
                {
                    LogError(0, NO_ERRCODE,
                             "Invalid number of bits (%d) in IPv4 address - "
                             "adjusted to 32",
                             (int)iSignificantBits);
                    iSignificantBits = 32;
                }

                MaskIP4(&(SIN(iAllow->addr.NetAddr)->sin_addr),
                        iSignificantBits);
                break;
            case AF_INET6:
                if ((iSignificantBits < 1) || (iSignificantBits > 128))
                {
                    LogError(0, NO_ERRCODE,
                             "Invalid number of bits (%d) in IPv6 address - "
                             "adjusted to 128",
                             iSignificantBits);
                    iSignificantBits = 128;
                }

                MaskIP6(&(SIN6(iAllow->addr.NetAddr)->sin6_addr),
                        iSignificantBits);
                break;
            default:
                /* rgerhards, 2007-07-16: We have an internal program error in
                 * this case. However, there is not much we can do against it
                 * right now. Of course, we could abort, but that would probably
                 * cause more harm than good. So we continue to run. We simply
                 * do not add this line - the worst thing that happens is that
                 * one host will not be allowed to log.
                 */
                LogError(0, NO_ERRCODE,
                         "Internal error caused AllowedSender to be ignored, "
                         "AF = %d",
                         iAllow->addr.NetAddr->sa_family);
                ABORT_FINALIZE(RS_RET_ERR);
        }
        /* OK, entry constructed, now lets add it to the ACL list */
        iRet = AddAllowedSenderEntry(ppRoot, ppLast, iAllow, iSignificantBits);
    }
    else
    {
        /* we need to process a hostname ACL */
        if (glbl.GetDisableDNS())
        {
            LogError(0, NO_ERRCODE,
                     "Ignoring hostname based ACLs because DNS is disabled.");
            ABORT_FINALIZE(RS_RET_OK);
        }

        if (!strchr(iAllow->addr.HostWildcard, '*') &&
            !strchr(iAllow->addr.HostWildcard, '?') && ACLDontResolve == 0)
        {
            /* single host - in this case, we pull its IP addresses from DNS
             * and add IP-based ACLs.
             */
            struct addrinfo hints, *res;
            struct NetAddr allowIP;

            memset(&hints, 0, sizeof(struct addrinfo));
            hints.ai_family = AF_UNSPEC;
            hints.ai_socktype = SOCK_DGRAM;
#ifdef AI_ADDRCONFIG /* seems not to be present on all systems */
            hints.ai_flags = AI_ADDRCONFIG;
#endif

            if (getaddrinfo(iAllow->addr.HostWildcard, NULL, &hints, &res) != 0)
            {
                LogError(0, NO_ERRCODE, "DNS error: Can't resolve \"%s\"",
                         iAllow->addr.HostWildcard);

                if (ACLAddHostnameOnFail)
                {
                    LogError(0, NO_ERRCODE,
                             "Adding hostname \"%s\" to ACL as a wildcard "
                             "entry.",
                             iAllow->addr.HostWildcard);
                    iRet = AddAllowedSenderEntry(ppRoot, ppLast, iAllow,
                                                 iSignificantBits);
                    FINALIZE;
                }
                else
                {
                    LogError(0, NO_ERRCODE,
                             "Hostname \"%s\" WON\'T be added to ACL.",
                             iAllow->addr.HostWildcard);
                    ABORT_FINALIZE(RS_RET_NOENTRY);
                }
            }

            restmp = res;
            for (; res != NULL; res = res->ai_next)
            {
                switch (res->ai_family)
                {
                    case AF_INET: /* add IPv4 */
                        iSignificantBits = 32;
                        allowIP.flags = 0;
                        if ((allowIP.addr.NetAddr = malloc(res->ai_addrlen)) ==
                            NULL)
                        {
                            ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
                        }
                        memcpy(allowIP.addr.NetAddr, res->ai_addr,
                               res->ai_addrlen);

                        if ((iRet = AddAllowedSenderEntry(
                                 ppRoot, ppLast, &allowIP, iSignificantBits)) !=
                            RS_RET_OK)
                        {
                            free(allowIP.addr.NetAddr);
                            FINALIZE;
                        }
                        break;
                    case AF_INET6: /* IPv6 - but need to check if it is a
                                      v6-mapped IPv4 */
                        if (IN6_IS_ADDR_V4MAPPED(
                                &SIN6(res->ai_addr)->sin6_addr))
                        {
                            /* extract & add IPv4 */

                            iSignificantBits = 32;
                            allowIP.flags = 0;
                            if ((allowIP.addr.NetAddr = (struct sockaddr*)
                                     malloc(sizeof(struct sockaddr))) == NULL)
                            {
                                ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
                            }
                            SIN(allowIP.addr.NetAddr)->sin_family = AF_INET;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
                            SIN(allowIP.addr.NetAddr)->sin_len =
                                sizeof(struct sockaddr_in);
#endif
                            SIN(allowIP.addr.NetAddr)->sin_port = 0;
                            memcpy(
                                &(SIN(allowIP.addr.NetAddr)->sin_addr.s_addr),
                                &(SIN6(res->ai_addr)->sin6_addr.s6_addr32[3]),
                                sizeof(in_addr_t));

                            if ((iRet = AddAllowedSenderEntry(
                                     ppRoot, ppLast, &allowIP,
                                     iSignificantBits)) != RS_RET_OK)
                            {
                                free(allowIP.addr.NetAddr);
                                FINALIZE;
                            }
                        }
                        else
                        {
                            /* finally add IPv6 */

                            iSignificantBits = 128;
                            allowIP.flags = 0;
                            if ((allowIP.addr.NetAddr =
                                     malloc(res->ai_addrlen)) == NULL)
                            {
                                ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
                            }
                            memcpy(allowIP.addr.NetAddr, res->ai_addr,
                                   res->ai_addrlen);

                            if ((iRet = AddAllowedSenderEntry(
                                     ppRoot, ppLast, &allowIP,
                                     iSignificantBits)) != RS_RET_OK)
                            {
                                free(allowIP.addr.NetAddr);
                                FINALIZE;
                            }
                        }
                        break;
                }
            }
        }
        else
        {
            /* wildcards in hostname - we need to add a text-based ACL.
             * For this, we already have everything ready and just need
             * to pass it along...
             */
            iRet =
                AddAllowedSenderEntry(ppRoot, ppLast, iAllow, iSignificantBits);
        }
    }

finalize_it:
    if (restmp != NULL)
    {
        freeaddrinfo(restmp);
    }
    RETiRet;
}
/* function to add an allowed sender to the allowed sender list. The
 * root of the list is caller-provided, so it can be used for all
 * supported lists. The caller must provide a pointer to the root,
 * as it eventually needs to be updated. Also, a pointer to the
 * pointer to the last element must be provided (to speed up adding
 * list elements).
 * rgerhards, 2005-09-26
 * If a hostname is given there are possible multiple entries
 * added (all addresses from that host).
 */
function to add an allowed sender to the allowed sender list. The
root of the list is caller-provided, so it can be used for all
supported lists. The caller must provide a pointer to the root,
as it eventually needs to be updated. Also, a pointer to the
pointer to the last element must be provided (to speed up adding
list elements).
rgerhards, 2005-09-26
If a hostname is given there are possible multiple entries
added (all addresses from that host).
function to add an allowed sender to the allowed sender list . The root of the list is caller - provided so it can be used for all supported lists . The caller must provide a pointer to the root as it eventually needs to be updated . Also a pointer to the pointer to the last element must be provided ( to speed up adding list elements ) . rgerhards 2005 - 09 - 26 If a hostname is given there are possible multiple entries added ( all addresses from that host ) .
static rsRetVal AddAllowedSender(struct AllowedSenders** ppRoot,
                                 struct AllowedSenders** ppLast,
                                 struct NetAddr* iAllow,
                                 uint8_t iSignificantBits)
{
    struct addrinfo* restmp = NULL;
    DEFiRet;
    assert(ppRoot != NULL);
    assert(ppLast != NULL);
    assert(iAllow != NULL);
    if (!F_ISSET(iAllow->flags, ADDR_NAME))
    {
        if (iSignificantBits == 0)
            LogError(0, NO_ERRCODE,
                     "You can not specify 0 bits of the netmask, this would "
                     "match ALL systems. If you really intend to do that, "
                     "remove all $AllowedSender directives.");
        switch (iAllow->addr.NetAddr->sa_family)
        {
            case AF_INET:
                if ((iSignificantBits < 1) || (iSignificantBits > 32))
                {
                    LogError(0, NO_ERRCODE,
                             "Invalid number of bits (%d) in IPv4 address - "
                             "adjusted to 32",
                             (int)iSignificantBits);
                    iSignificantBits = 32;
                }
                MaskIP4(&(SIN(iAllow->addr.NetAddr)->sin_addr),
                        iSignificantBits);
                break;
            case AF_INET6:
                if ((iSignificantBits < 1) || (iSignificantBits > 128))
                {
                    LogError(0, NO_ERRCODE,
                             "Invalid number of bits (%d) in IPv6 address - "
                             "adjusted to 128",
                             iSignificantBits);
                    iSignificantBits = 128;
                }
                MaskIP6(&(SIN6(iAllow->addr.NetAddr)->sin6_addr),
                        iSignificantBits);
                break;
            default:
                LogError(0, NO_ERRCODE,
                         "Internal error caused AllowedSender to be ignored, "
                         "AF = %d",
                         iAllow->addr.NetAddr->sa_family);
                ABORT_FINALIZE(RS_RET_ERR);
        }
        iRet = AddAllowedSenderEntry(ppRoot, ppLast, iAllow, iSignificantBits);
    }
    else
    {
        if (glbl.GetDisableDNS())
        {
            LogError(0, NO_ERRCODE,
                     "Ignoring hostname based ACLs because DNS is disabled.");
            ABORT_FINALIZE(RS_RET_OK);
        }
        if (!strchr(iAllow->addr.HostWildcard, '*') &&
            !strchr(iAllow->addr.HostWildcard, '?') && ACLDontResolve == 0)
        {
            struct addrinfo hints, *res;
            struct NetAddr allowIP;
            memset(&hints, 0, sizeof(struct addrinfo));
            hints.ai_family = AF_UNSPEC;
            hints.ai_socktype = SOCK_DGRAM;
#ifdef AI_ADDRCONFIG 
            hints.ai_flags = AI_ADDRCONFIG;
#endif
            if (getaddrinfo(iAllow->addr.HostWildcard, NULL, &hints, &res) != 0)
            {
                LogError(0, NO_ERRCODE, "DNS error: Can't resolve \"%s\"",
                         iAllow->addr.HostWildcard);
                if (ACLAddHostnameOnFail)
                {
                    LogError(0, NO_ERRCODE,
                             "Adding hostname \"%s\" to ACL as a wildcard "
                             "entry.",
                             iAllow->addr.HostWildcard);
                    iRet = AddAllowedSenderEntry(ppRoot, ppLast, iAllow,
                                                 iSignificantBits);
                    FINALIZE;
                }
                else
                {
                    LogError(0, NO_ERRCODE,
                             "Hostname \"%s\" WON\'T be added to ACL.",
                             iAllow->addr.HostWildcard);
                    ABORT_FINALIZE(RS_RET_NOENTRY);
                }
            }
            restmp = res;
            for (; res != NULL; res = res->ai_next)
            {
                switch (res->ai_family)
                {
                    case AF_INET: 
                        iSignificantBits = 32;
                        allowIP.flags = 0;
                        if ((allowIP.addr.NetAddr = malloc(res->ai_addrlen)) ==
                            NULL)
                        {
                            ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
                        }
                        memcpy(allowIP.addr.NetAddr, res->ai_addr,
                               res->ai_addrlen);
                        if ((iRet = AddAllowedSenderEntry(
                                 ppRoot, ppLast, &allowIP, iSignificantBits)) !=
                            RS_RET_OK)
                        {
                            free(allowIP.addr.NetAddr);
                            FINALIZE;
                        }
                        break;
                    case AF_INET6: 
                        if (IN6_IS_ADDR_V4MAPPED(
                                &SIN6(res->ai_addr)->sin6_addr))
                        {
                            iSignificantBits = 32;
                            allowIP.flags = 0;
                            if ((allowIP.addr.NetAddr = (struct sockaddr*)
                                     malloc(sizeof(struct sockaddr))) == NULL)
                            {
                                ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
                            }
                            SIN(allowIP.addr.NetAddr)->sin_family = AF_INET;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
                            SIN(allowIP.addr.NetAddr)->sin_len =
                                sizeof(struct sockaddr_in);
#endif
                            SIN(allowIP.addr.NetAddr)->sin_port = 0;
                            memcpy(
                                &(SIN(allowIP.addr.NetAddr)->sin_addr.s_addr),
                                &(SIN6(res->ai_addr)->sin6_addr.s6_addr32[3]),
                                sizeof(in_addr_t));
                            if ((iRet = AddAllowedSenderEntry(
                                     ppRoot, ppLast, &allowIP,
                                     iSignificantBits)) != RS_RET_OK)
                            {
                                free(allowIP.addr.NetAddr);
                                FINALIZE;
                            }
                        }
                        else
                        {
                            iSignificantBits = 128;
                            allowIP.flags = 0;
                            if ((allowIP.addr.NetAddr =
                                     malloc(res->ai_addrlen)) == NULL)
                            {
                                ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
                            }
                            memcpy(allowIP.addr.NetAddr, res->ai_addr,
                                   res->ai_addrlen);
                            if ((iRet = AddAllowedSenderEntry(
                                     ppRoot, ppLast, &allowIP,
                                     iSignificantBits)) != RS_RET_OK)
                            {
                                free(allowIP.addr.NetAddr);
                                FINALIZE;
                            }
                        }
                        break;
                }
            }
        }
        else
        {
            iRet =
                AddAllowedSenderEntry(ppRoot, ppLast, iAllow, iSignificantBits);
        }
    }
finalize_it:
    if (restmp != NULL)
    {
        freeaddrinfo(restmp);
    }
    RETiRet;
}
static rsRetVal AddAllowedSender ( struct AllowedSenders * * ppRoot , struct AllowedSenders * * ppLast , struct NetAddr * iAllow , uint8_t iSignificantBits ) { struct addrinfo * restmp = NULL ; DEFiRet ; assert ( ppRoot != NULL ) ; assert ( ppLast != NULL ) ; assert ( iAllow != NULL ) ; if ( ! F_ISSET ( iAllow -> flags , ADDR_NAME ) ) { if ( iSignificantBits == 0 ) LogError ( 0 , NO_ERRCODE , " " " " " " ) ; switch ( iAllow -> addr . NetAddr -> sa_family ) { case AF_INET : if ( ( iSignificantBits < 1 ) || ( iSignificantBits > 32 ) ) { LogError ( 0 , NO_ERRCODE , " " " " , ( int ) iSignificantBits ) ; iSignificantBits = 32 ; } MaskIP4 ( & ( SIN ( iAllow -> addr . NetAddr ) -> sin_addr ) , iSignificantBits ) ; break ; case AF_INET6 : if ( ( iSignificantBits < 1 ) || ( iSignificantBits > 128 ) ) { LogError ( 0 , NO_ERRCODE , " " " " , iSignificantBits ) ; iSignificantBits = 128 ; } MaskIP6 ( & ( SIN6 ( iAllow -> addr . NetAddr ) -> sin6_addr ) , iSignificantBits ) ; break ; default : LogError ( 0 , NO_ERRCODE , " " " " , iAllow -> addr . NetAddr -> sa_family ) ; ABORT_FINALIZE ( RS_RET_ERR ) ; } iRet = AddAllowedSenderEntry ( ppRoot , ppLast , iAllow , iSignificantBits ) ; } else { if ( glbl . GetDisableDNS ( ) ) { LogError ( 0 , NO_ERRCODE , " " ) ; ABORT_FINALIZE ( RS_RET_OK ) ; } if ( ! strchr ( iAllow -> addr . HostWildcard , ' ' ) && ! strchr ( iAllow -> addr . HostWildcard , ' ' ) && ACLDontResolve == 0 ) { struct addrinfo hints , * res ; struct NetAddr allowIP ; memset ( & hints , 0 , sizeof ( struct addrinfo ) ) ; hints . ai_family = AF_UNSPEC ; hints . ai_socktype = SOCK_DGRAM ; #ifdef AI_ADDRCONFIG hints . ai_flags = AI_ADDRCONFIG ; #endif if ( getaddrinfo ( iAllow -> addr . HostWildcard , NULL , & hints , & res ) != 0 ) { LogError ( 0 , NO_ERRCODE , " \" \" " , iAllow -> addr . HostWildcard ) ; if ( ACLAddHostnameOnFail ) { LogError ( 0 , NO_ERRCODE , " \" \" " " " , iAllow -> addr . HostWildcard ) ; iRet = AddAllowedSenderEntry ( ppRoot , ppLast , iAllow , iSignificantBits ) ; FINALIZE ; } else { LogError ( 0 , NO_ERRCODE , " \" \" \' " , iAllow -> addr . HostWildcard ) ; ABORT_FINALIZE ( RS_RET_NOENTRY ) ; } } restmp = res ; for ( ; res != NULL ; res = res -> ai_next ) { switch ( res -> ai_family ) { case AF_INET : iSignificantBits = 32 ; allowIP . flags = 0 ; if ( ( allowIP . addr . NetAddr = malloc ( res -> ai_addrlen ) ) == NULL ) { ABORT_FINALIZE ( RS_RET_OUT_OF_MEMORY ) ; } memcpy ( allowIP . addr . NetAddr , res -> ai_addr , res -> ai_addrlen ) ; if ( ( iRet = AddAllowedSenderEntry ( ppRoot , ppLast , & allowIP , iSignificantBits ) ) != RS_RET_OK ) { free ( allowIP . addr . NetAddr ) ; FINALIZE ; } break ; case AF_INET6 : if ( IN6_IS_ADDR_V4MAPPED ( & SIN6 ( res -> ai_addr ) -> sin6_addr ) ) { iSignificantBits = 32 ; allowIP . flags = 0 ; if ( ( allowIP . addr . NetAddr = ( struct sockaddr * ) malloc ( sizeof ( struct sockaddr ) ) ) == NULL ) { ABORT_FINALIZE ( RS_RET_OUT_OF_MEMORY ) ; } SIN ( allowIP . addr . NetAddr ) -> sin_family = AF_INET ; #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN SIN ( allowIP . addr . NetAddr ) -> sin_len = sizeof ( struct sockaddr_in ) ; #endif SIN ( allowIP . addr . NetAddr ) -> sin_port = 0 ; memcpy ( & ( SIN ( allowIP . addr . NetAddr ) -> sin_addr . s_addr ) , & ( SIN6 ( res -> ai_addr ) -> sin6_addr . s6_addr32 [ 3 ] ) , sizeof ( in_addr_t ) ) ; if ( ( iRet = AddAllowedSenderEntry ( ppRoot , ppLast , & allowIP , iSignificantBits ) ) != RS_RET_OK ) { free ( allowIP . addr . NetAddr ) ; FINALIZE ; } } else { iSignificantBits = 128 ; allowIP . flags = 0 ; if ( ( allowIP . addr . NetAddr = malloc ( res -> ai_addrlen ) ) == NULL ) { ABORT_FINALIZE ( RS_RET_OUT_OF_MEMORY ) ; } memcpy ( allowIP . addr . NetAddr , res -> ai_addr , res -> ai_addrlen ) ; if ( ( iRet = AddAllowedSenderEntry ( ppRoot , ppLast , & allowIP , iSignificantBits ) ) != RS_RET_OK ) { free ( allowIP . addr . NetAddr ) ; FINALIZE ; } } break ; } } } else { iRet = AddAllowedSenderEntry ( ppRoot , ppLast , iAllow , iSignificantBits ) ; } } finalize_it : if ( restmp != NULL ) { freeaddrinfo ( restmp ) ; } RETiRet ; }
function to add an allowed sender to the allowed sender list.
function to add an allowed sender to the allowed sender list .
/* we handle this seperatly just to provide a better
             * error message.
             */ /* rgerhards, 2007-07-16: We have an internal program error in
                 * this case. However, there is not much we can do against it
                 * right now. Of course, we could abort, but that would probably
                 * cause more harm than good. So we continue to run. We simply
                 * do not add this line - the worst thing that happens is that
                 * one host will not be allowed to log.
                 */ /* OK, entry constructed, now lets add it to the ACL list */ /* we need to process a hostname ACL */ /* single host - in this case, we pull its IP addresses from DNS
             * and add IP-based ACLs.
             */ /* seems not to be present on all systems */ /* add IPv4 */ /* IPv6 - but need to check if it is a
                                      v6-mapped IPv4 */ /* extract & add IPv4 */ /* finally add IPv6 */ /* wildcards in hostname - we need to add a text-based ACL.
             * For this, we already have everything ready and just need
             * to pass it along...
             */