LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/krb5 - get_addrs.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 0 135 0.0 %
Date: 2024-04-13 12:30:31 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  *
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  *
      13             :  * 2. Redistributions in binary form must reproduce the above copyright
      14             :  *    notice, this list of conditions and the following disclaimer in the
      15             :  *    documentation and/or other materials provided with the distribution.
      16             :  *
      17             :  * 3. Neither the name of the Institute nor the names of its contributors
      18             :  *    may be used to endorse or promote products derived from this software
      19             :  *    without specific prior written permission.
      20             :  *
      21             :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      22             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      23             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      24             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      25             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      26             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      27             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      28             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      29             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      30             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      31             :  * SUCH DAMAGE.
      32             :  */
      33             : 
      34             : #include "krb5_locl.h"
      35             : 
      36             : #ifdef __osf__
      37             : /* hate */
      38             : struct rtentry;
      39             : struct mbuf;
      40             : #endif
      41             : #ifdef HAVE_NET_IF_H
      42             : #include <net/if.h>
      43             : #endif
      44             : #include <ifaddrs.h>
      45             : 
      46             : static krb5_error_code
      47           0 : gethostname_fallback (krb5_context context, krb5_addresses *res)
      48             : {
      49           0 :     krb5_error_code ret;
      50           0 :     char hostname[MAXHOSTNAMELEN];
      51           0 :     struct hostent *hostent;
      52             : 
      53           0 :     if (gethostname (hostname, sizeof(hostname))) {
      54           0 :         ret = errno;
      55           0 :         krb5_set_error_message(context, ret, "gethostname: %s", strerror(ret));
      56           0 :         return ret;
      57             :     }
      58           0 :     hostent = roken_gethostbyname (hostname);
      59           0 :     if (hostent == NULL) {
      60           0 :         ret = errno;
      61           0 :         krb5_set_error_message (context, ret, "gethostbyname %s: %s",
      62             :                                 hostname, strerror(ret));
      63           0 :         return ret;
      64             :     }
      65           0 :     res->len = 1;
      66           0 :     res->val = malloc (sizeof(*res->val));
      67           0 :     if (res->val == NULL)
      68           0 :         return krb5_enomem(context);
      69           0 :     res->val[0].addr_type = hostent->h_addrtype;
      70           0 :     res->val[0].address.data = NULL;
      71           0 :     res->val[0].address.length = 0;
      72           0 :     ret = krb5_data_copy (&res->val[0].address,
      73           0 :                           hostent->h_addr,
      74           0 :                           hostent->h_length);
      75           0 :     if (ret) {
      76           0 :         free (res->val);
      77           0 :         return ret;
      78             :     }
      79           0 :     return 0;
      80             : }
      81             : 
      82             : enum {
      83             :     LOOP            = 1,        /* do include loopback addrs */
      84             :     LOOP_IF_NONE    = 2,        /* include loopback addrs if no others */
      85             :     EXTRA_ADDRESSES = 4,        /* include extra addresses */
      86             :     SCAN_INTERFACES = 8         /* scan interfaces for addresses */
      87             : };
      88             : 
      89             : /*
      90             :  * Try to figure out the addresses of all configured interfaces with a
      91             :  * lot of magic ioctls.
      92             :  */
      93             : 
      94             : static krb5_error_code
      95           0 : find_all_addresses (krb5_context context, krb5_addresses *res, int flags)
      96             : {
      97           0 :     struct sockaddr sa_zero;
      98           0 :     struct ifaddrs *ifa0, *ifa;
      99           0 :     krb5_error_code ret = ENXIO;
     100           0 :     unsigned int num, idx;
     101           0 :     krb5_addresses ignore_addresses;
     102             : 
     103           0 :     if (getifaddrs(&ifa0) == -1) {
     104           0 :         ret = errno;
     105           0 :         krb5_set_error_message(context, ret, "getifaddrs: %s", strerror(ret));
     106           0 :         return (ret);
     107             :     }
     108             : 
     109           0 :     memset(&sa_zero, 0, sizeof(sa_zero));
     110             : 
     111             :     /* First, count all the ifaddrs. */
     112           0 :     for (ifa = ifa0, num = 0; ifa != NULL; ifa = ifa->ifa_next, num++)
     113             :         /* nothing */;
     114             : 
     115           0 :     if (num == 0) {
     116           0 :         freeifaddrs(ifa0);
     117           0 :         krb5_set_error_message(context, ENXIO, N_("no addresses found", ""));
     118           0 :         return (ENXIO);
     119             :     }
     120             : 
     121           0 :     if (flags & EXTRA_ADDRESSES) {
     122             :         /* we'll remove the addresses we don't care about */
     123           0 :         ret = krb5_get_ignore_addresses(context, &ignore_addresses);
     124           0 :         if(ret)
     125           0 :             return ret;
     126             :     }
     127             : 
     128             :     /* Allocate storage for them. */
     129           0 :     res->val = calloc(num, sizeof(*res->val));
     130           0 :     if (res->val == NULL) {
     131           0 :         if (flags & EXTRA_ADDRESSES)
     132           0 :             krb5_free_addresses(context, &ignore_addresses);
     133           0 :         freeifaddrs(ifa0);
     134           0 :         return krb5_enomem(context);
     135             :     }
     136             : 
     137             :     /* Now traverse the list. */
     138           0 :     for (ifa = ifa0, idx = 0; ifa != NULL; ifa = ifa->ifa_next) {
     139           0 :         if ((ifa->ifa_flags & IFF_UP) == 0)
     140           0 :             continue;
     141           0 :         if (ifa->ifa_addr == NULL)
     142           0 :             continue;
     143           0 :         if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
     144           0 :             continue;
     145           0 :         if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
     146           0 :             continue;
     147           0 :         if (krb5_sockaddr_is_loopback(ifa->ifa_addr) && (flags & LOOP) == 0)
     148             :             /* We'll deal with the LOOP_IF_NONE case later. */
     149           0 :             continue;
     150             : 
     151           0 :         ret = krb5_sockaddr2address(context, ifa->ifa_addr, &res->val[idx]);
     152           0 :         if (ret) {
     153             :             /*
     154             :              * The most likely error here is going to be "Program
     155             :              * lacks support for address type".  This is no big
     156             :              * deal -- just continue, and we'll listen on the
     157             :              * addresses who's type we *do* support.
     158             :              */
     159           0 :             continue;
     160             :         }
     161             :         /* possibly skip this address? */
     162           0 :         if((flags & EXTRA_ADDRESSES) &&
     163           0 :            krb5_address_search(context, &res->val[idx], &ignore_addresses)) {
     164           0 :             krb5_free_address(context, &res->val[idx]);
     165           0 :             flags &= ~LOOP_IF_NONE; /* we actually found an address,
     166             :                                        so don't add any loop-back
     167             :                                        addresses */
     168           0 :             continue;
     169             :         }
     170             : 
     171           0 :         idx++;
     172             :     }
     173             : 
     174             :     /*
     175             :      * If no addresses were found, and LOOP_IF_NONE is set, then find
     176             :      * the loopback addresses and add them to our list.
     177             :      */
     178           0 :     if ((flags & LOOP_IF_NONE) != 0 && idx == 0) {
     179           0 :         for (ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next) {
     180           0 :             if ((ifa->ifa_flags & IFF_UP) == 0)
     181           0 :                 continue;
     182           0 :             if (ifa->ifa_addr == NULL)
     183           0 :                 continue;
     184           0 :             if (memcmp(ifa->ifa_addr, &sa_zero, sizeof(sa_zero)) == 0)
     185           0 :                 continue;
     186           0 :             if (krb5_sockaddr_uninteresting(ifa->ifa_addr))
     187           0 :                 continue;
     188           0 :             if (!krb5_sockaddr_is_loopback(ifa->ifa_addr))
     189           0 :                 continue;
     190           0 :             if ((ifa->ifa_flags & IFF_LOOPBACK) == 0)
     191             :                 /* Presumably loopback addrs are only used on loopback ifs! */
     192           0 :                 continue;
     193           0 :             ret = krb5_sockaddr2address(context,
     194           0 :                                         ifa->ifa_addr, &res->val[idx]);
     195           0 :             if (ret)
     196           0 :                 continue; /* We don't consider this failure fatal */
     197           0 :             if((flags & EXTRA_ADDRESSES) &&
     198           0 :                krb5_address_search(context, &res->val[idx],
     199             :                                    &ignore_addresses)) {
     200           0 :                 krb5_free_address(context, &res->val[idx]);
     201           0 :                 continue;
     202             :             }
     203           0 :             idx++;
     204             :         }
     205             :     }
     206             : 
     207           0 :     if (flags & EXTRA_ADDRESSES)
     208           0 :         krb5_free_addresses(context, &ignore_addresses);
     209           0 :     freeifaddrs(ifa0);
     210           0 :     if (ret) {
     211           0 :         free(res->val);
     212           0 :         res->val = NULL;
     213             :     } else
     214           0 :         res->len = idx;        /* Now a count. */
     215           0 :     return (ret);
     216             : }
     217             : 
     218             : static krb5_error_code
     219           0 : get_addrs_int (krb5_context context, krb5_addresses *res, int flags)
     220             : {
     221           0 :     krb5_error_code ret = -1;
     222             : 
     223           0 :     res->len = 0;
     224           0 :     res->val = NULL;
     225             : 
     226           0 :     if (flags & SCAN_INTERFACES) {
     227           0 :         ret = find_all_addresses (context, res, flags);
     228           0 :         if(ret || res->len == 0)
     229           0 :             ret = gethostname_fallback (context, res);
     230             :     } else {
     231           0 :         ret = 0;
     232             :     }
     233             : 
     234           0 :     if(ret == 0 && (flags & EXTRA_ADDRESSES)) {
     235           0 :         krb5_addresses a;
     236             :         /* append user specified addresses */
     237           0 :         ret = krb5_get_extra_addresses(context, &a);
     238           0 :         if(ret) {
     239           0 :             krb5_free_addresses(context, res);
     240           0 :             return ret;
     241             :         }
     242           0 :         ret = krb5_append_addresses(context, res, &a);
     243           0 :         if(ret) {
     244           0 :             krb5_free_addresses(context, res);
     245           0 :             return ret;
     246             :         }
     247           0 :         krb5_free_addresses(context, &a);
     248             :     }
     249           0 :     if(res->len == 0) {
     250           0 :         free(res->val);
     251           0 :         res->val = NULL;
     252             :     }
     253           0 :     return ret;
     254             : }
     255             : 
     256             : /*
     257             :  * Try to get all addresses, but return the one corresponding to
     258             :  * `hostname' if we fail.
     259             :  *
     260             :  * Only include loopback address if there are no other.
     261             :  */
     262             : 
     263             : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
     264           0 : krb5_get_all_client_addrs (krb5_context context, krb5_addresses *res)
     265             : {
     266           0 :     int flags = LOOP_IF_NONE | EXTRA_ADDRESSES;
     267             : 
     268           0 :     if (context->scan_interfaces)
     269           0 :         flags |= SCAN_INTERFACES;
     270             : 
     271           0 :     return get_addrs_int (context, res, flags);
     272             : }
     273             : 
     274             : /*
     275             :  * Try to get all local addresses that a server should listen to.
     276             :  * If that fails, we return the address corresponding to `hostname'.
     277             :  */
     278             : 
     279             : KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
     280           0 : krb5_get_all_server_addrs (krb5_context context, krb5_addresses *res)
     281             : {
     282           0 :     return get_addrs_int (context, res, LOOP | SCAN_INTERFACES);
     283             : }

Generated by: LCOV version 1.14