sdap_async_sudo_hostinfo.c revision d6133b04d4ee47f147db26cd732ea5d209af5b8e
842ae4bd224140319ae7feec1872b93dfd491143fielding/*
842ae4bd224140319ae7feec1872b93dfd491143fielding Authors:
842ae4bd224140319ae7feec1872b93dfd491143fielding Pavel Březina <pbrezina@redhat.com>
842ae4bd224140319ae7feec1872b93dfd491143fielding
842ae4bd224140319ae7feec1872b93dfd491143fielding Copyright (C) 2012 Red Hat
842ae4bd224140319ae7feec1872b93dfd491143fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding This program is free software; you can redistribute it and/or modify
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd it under the terms of the GNU General Public License as published by
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding the Free Software Foundation; either version 3 of the License, or
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd (at your option) any later version.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd This program is distributed in the hope that it will be useful,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd but WITHOUT ANY WARRANTY; without even the implied warranty of
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding GNU General Public License for more details.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding You should have received a copy of the GNU General Public License
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding along with this program. If not, see <http://www.gnu.org/licenses/>.
2261031aa94be82d7e6b1b8c367afc1b282317f5ianh*/
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
2261031aa94be82d7e6b1b8c367afc1b282317f5ianh#include <errno.h>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include <tevent.h>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include <talloc.h>
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein#include <sys/types.h>
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein#include <sys/socket.h>
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein#include <arpa/inet.h>
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein#include <ifaddrs.h>
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "util/util.h"
be3223a6a18d9a3a3cf7155d5430a5d92bcddceegstein#include "providers/ldap/sdap.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "providers/ldap/sdap_id_op.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "providers/ldap/sdap_sudo.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gsteinstatic int sdap_sudo_get_ip_addresses(TALLOC_CTX *mem_ctx, char ***_ip_addr);
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein
cccd31fa4a72fe23cc3249c06db181b274a55a69gsteinstruct sdap_sudo_get_hostinfo_state {
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein char **hostnames;
cccd31fa4a72fe23cc3249c06db181b274a55a69gstein char **ip_addr;
1b21d7b3d97def358b2e923655edeb16613a1c31gstein};
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gsteinstruct tevent_req * sdap_sudo_get_hostinfo_send(TALLOC_CTX *mem_ctx,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding struct sdap_options *opts,
b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44crbb struct be_ctx *be_ctx)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding struct tevent_req *req = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding struct sdap_sudo_get_hostinfo_state *state = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *conf_hostnames = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *conf_ip_addr = NULL;
cf8fe49d9d89cfa4b62cb2b1376ca6f25b81b362trawick int ret;
874fa3c6bbef1b4ab4bed0a2ff9852b21ea1b187trawick
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* create request */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_get_hostinfo_state);
963f8b44ac95132458ea3b6aaa8ebc135188e473takashi if (req == NULL) {
963f8b44ac95132458ea3b6aaa8ebc135188e473takashi DEBUG(SSSDBG_FATAL_FAILURE, ("tevent_req_create() failed\n"));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
a7318397b355119d990d1f311b951dea2aafc53arbb
a7318397b355119d990d1f311b951dea2aafc53arbb state->hostnames = NULL;
a7318397b355119d990d1f311b951dea2aafc53arbb state->ip_addr = NULL;
a7318397b355119d990d1f311b951dea2aafc53arbb
cb9e6e5c78f5a1690214e9548250fc6af1fc73b5wrowe /* load info from configuration */
cb9e6e5c78f5a1690214e9548250fc6af1fc73b5wrowe conf_hostnames = dp_opt_get_string(opts->basic, SDAP_SUDO_HOSTNAMES);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding conf_ip_addr = dp_opt_get_string(opts->basic, SDAP_SUDO_IP);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (conf_hostnames != NULL) {
8a3228198adb03e6996f7738c361a612777ecab6aaron ret = split_on_separator(state, conf_hostnames, ' ', true,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding &state->hostnames, NULL);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (ret != EOK) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding DEBUG(SSSDBG_MINOR_FAILURE,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ("Unable to parse hostnames [%d]: %s\n", ret, strerror(ret)));
8a3228198adb03e6996f7738c361a612777ecab6aaron goto done;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding } else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding DEBUG(SSSDBG_CONF_SETTINGS, ("Hostnames set to: %s\n", conf_hostnames));
8a3228198adb03e6996f7738c361a612777ecab6aaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
8a3228198adb03e6996f7738c361a612777ecab6aaron if (conf_ip_addr != NULL) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ret = split_on_separator(state, conf_ip_addr, ' ', true,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding &state->ip_addr, NULL);
8a3228198adb03e6996f7738c361a612777ecab6aaron if (ret != EOK) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding DEBUG(SSSDBG_MINOR_FAILURE,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ("Unable to parse IP addresses [%d]: %s\n", ret, strerror(ret)));
8a3228198adb03e6996f7738c361a612777ecab6aaron goto done;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding } else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding DEBUG(SSSDBG_CONF_SETTINGS, ("IP addresses set to: %s\n", conf_ip_addr));
8a3228198adb03e6996f7738c361a612777ecab6aaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
8a3228198adb03e6996f7738c361a612777ecab6aaron /* if IP addresses are not specified, configure it automatically */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (state->ip_addr == NULL) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ret = sdap_sudo_get_ip_addresses(state, &state->ip_addr);
8a3228198adb03e6996f7738c361a612777ecab6aaron if (ret != EOK) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
8a3228198adb03e6996f7738c361a612777ecab6aaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingdone:
8a3228198adb03e6996f7738c361a612777ecab6aaron if (ret != EAGAIN) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (ret == EOK) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding tevent_req_done(req);
8a3228198adb03e6996f7738c361a612777ecab6aaron } else {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding tevent_req_error(req, ret);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
8a3228198adb03e6996f7738c361a612777ecab6aaron tevent_req_post(req, be_ctx->ev);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
8a3228198adb03e6996f7738c361a612777ecab6aaron return req;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
8a3228198adb03e6996f7738c361a612777ecab6aaronint sdap_sudo_get_hostinfo_recv(TALLOC_CTX *mem_ctx,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding struct tevent_req *req,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char ***hostnames, char ***ip_addr)
8a3228198adb03e6996f7738c361a612777ecab6aaron{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding struct sdap_sudo_get_hostinfo_state *state = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding state = tevent_req_data(req, struct sdap_sudo_get_hostinfo_state);
8a3228198adb03e6996f7738c361a612777ecab6aaron
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding TEVENT_REQ_RETURN_ON_ERROR(req);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
8a3228198adb03e6996f7738c361a612777ecab6aaron *hostnames = talloc_steal(mem_ctx, state->hostnames);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *ip_addr = talloc_steal(mem_ctx, state->ip_addr);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
8a3228198adb03e6996f7738c361a612777ecab6aaron return EOK;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
8a3228198adb03e6996f7738c361a612777ecab6aaron
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int sdap_sudo_get_ip_addresses(TALLOC_CTX *mem_ctx, char ***_ip_addr_list)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding TALLOC_CTX *tmp_ctx = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char **ip_addr_list = NULL;
8a3228198adb03e6996f7738c361a612777ecab6aaron struct ifaddrs *ifaces = NULL;
8a3228198adb03e6996f7738c361a612777ecab6aaron struct ifaddrs *iface = NULL;
8a3228198adb03e6996f7738c361a612777ecab6aaron struct sockaddr_in *ip4_addr = NULL;
8a3228198adb03e6996f7738c361a612777ecab6aaron struct sockaddr_in *ip4_network = NULL;
8a3228198adb03e6996f7738c361a612777ecab6aaron struct sockaddr_in6 *ip6_addr = NULL;
8a3228198adb03e6996f7738c361a612777ecab6aaron struct sockaddr_in6 *ip6_network = NULL;
8a3228198adb03e6996f7738c361a612777ecab6aaron char ip_addr[INET6_ADDRSTRLEN + 1];
8a3228198adb03e6996f7738c361a612777ecab6aaron char network_addr[INET6_ADDRSTRLEN + 1];
8a3228198adb03e6996f7738c361a612777ecab6aaron in_addr_t ip4_netmask = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding uint32_t ip6_netmask = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned int netmask = 0;
649c9dd342a580016d29c2866de88a4609eb6457wrowe void *sinx_addr = NULL;
649c9dd342a580016d29c2866de88a4609eb6457wrowe void *sinx_network = NULL;
0f57a69cc84c59eacda1179763ae42ca88f1a577wrowe int addr_count = 0;
18b197c6ba1d5b8bb5b2fb372ef752f87e56912atrawick int ret;
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick int i;
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick tmp_ctx = talloc_new(NULL);
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick if (tmp_ctx == NULL) {
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_new() failed\n"));
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick return ENOMEM;
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick }
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton errno = 0;
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton ret = getifaddrs(&ifaces);
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton if (ret == -1) {
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton ret = errno;
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton DEBUG(SSSDBG_CRIT_FAILURE, ("Could not read interfaces [%d][%s]\n",
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton ret, strerror(ret)));
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton goto done;
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton }
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton for (iface = ifaces; iface != NULL; iface = iface->ifa_next) {
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton /* Some interfaces don't have an ifa_addr */
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton if (!iface->ifa_addr) continue;
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton netmask = 0;
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton switch (iface->ifa_addr->sa_family) {
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton case AF_INET:
7117ace448072813fa6eb7180ef206fc2e8fcfc7wrowe ip4_addr = (struct sockaddr_in*)(iface->ifa_addr);
7117ace448072813fa6eb7180ef206fc2e8fcfc7wrowe ip4_network = (struct sockaddr_in*)(iface->ifa_netmask);
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton /* ignore loopback */
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton if (inet_netof(ip4_addr->sin_addr) == IN_LOOPBACKNET) {
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton continue;
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton }
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton /* ignore multicast */
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton if (IN_MULTICAST(ip4_addr->sin_addr.s_addr)) {
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton continue;
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton }
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton /* ignore broadcast */
f3a0be90718c1fa5da1bf25e38974d2db3ef8a30jorton if (ntohl(ip4_addr->sin_addr.s_addr) == INADDR_BROADCAST) {
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick continue;
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick }
649c9dd342a580016d29c2866de88a4609eb6457wrowe
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick /* get network mask length */
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick ip4_netmask = ntohl(ip4_network->sin_addr.s_addr);
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick while (ip4_netmask) {
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick netmask++;
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick ip4_netmask <<= 1;
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick }
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe /* get network address */
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe ip4_network->sin_addr.s_addr = ip4_addr->sin_addr.s_addr
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe & ip4_network->sin_addr.s_addr;
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe sinx_addr = &ip4_addr->sin_addr;
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe sinx_network = &ip4_network->sin_addr;
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe break;
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe case AF_INET6:
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe ip6_addr = (struct sockaddr_in6*)(iface->ifa_addr);
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe ip6_network = (struct sockaddr_in6*)(iface->ifa_netmask);
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick /* ignore loopback */
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe if (IN6_IS_ADDR_LOOPBACK(&ip6_addr->sin6_addr)) {
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick continue;
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick }
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick /* ignore multicast */
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick if (IN6_IS_ADDR_MULTICAST(&ip6_addr->sin6_addr)) {
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick continue;
d7b781abdcdc6fc4d4fcd513d5babd3c42dff43dwrowe }
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick /* get network mask length */
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick for (i = 0; i < 4; i++) {
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick ip6_netmask = ntohl(((uint32_t*)(&ip6_network->sin6_addr))[i]);
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick while (ip6_netmask) {
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick netmask++;
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick ip6_netmask <<= 1;
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick }
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick }
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick /* get network address */
82d2a5debc5a6ed2118ac5916d9ba36ad0b5d78btrawick for (i = 0; i < 4; i++) {
ebc18d48bea83ee5ed7a1b4e30007e5192539829wrowe ((uint32_t*)(&ip6_network->sin6_addr))[i] =
18b197c6ba1d5b8bb5b2fb372ef752f87e56912atrawick ((uint32_t*)(&ip6_addr->sin6_addr))[i]
066877f1a045103acfdd376d48cdd473c33f409bdougm & ((uint32_t*)(&ip6_network->sin6_addr))[i];
18b197c6ba1d5b8bb5b2fb372ef752f87e56912atrawick }
18b197c6ba1d5b8bb5b2fb372ef752f87e56912atrawick
e8f95a682820a599fe41b22977010636be5c2717jim sinx_addr = &ip6_addr->sin6_addr;
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe sinx_network = &ip6_network->sin6_addr;
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe break;
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe default:
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe /* skip other families */
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe continue;
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe }
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe /* ip address */
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe errno = 0;
85c435ceda98eab940615e4466d9c5955252e745wrowe if (inet_ntop(iface->ifa_addr->sa_family, sinx_addr,
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe ip_addr, INET6_ADDRSTRLEN) == NULL) {
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe ret = errno;
92eed7d377d8be7cd8e48d1299412fc8c742b7f5jorton DEBUG(SSSDBG_MINOR_FAILURE, ("inet_ntop() failed [%d]: %s\n",
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe ret, strerror(ret)));
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe goto done;
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe }
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe /* network */
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe errno = 0;
649c9dd342a580016d29c2866de88a4609eb6457wrowe if (inet_ntop(iface->ifa_addr->sa_family, sinx_network,
649c9dd342a580016d29c2866de88a4609eb6457wrowe network_addr, INET6_ADDRSTRLEN) == NULL) {
649c9dd342a580016d29c2866de88a4609eb6457wrowe ret = errno;
649c9dd342a580016d29c2866de88a4609eb6457wrowe DEBUG(SSSDBG_MINOR_FAILURE, ("inet_ntop() failed [%d]: %s\n",
649c9dd342a580016d29c2866de88a4609eb6457wrowe ret, strerror(ret)));
649c9dd342a580016d29c2866de88a4609eb6457wrowe goto done;
649c9dd342a580016d29c2866de88a4609eb6457wrowe }
649c9dd342a580016d29c2866de88a4609eb6457wrowe
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe addr_count += 2;
649c9dd342a580016d29c2866de88a4609eb6457wrowe ip_addr_list = talloc_realloc(tmp_ctx, ip_addr_list, char*,
649c9dd342a580016d29c2866de88a4609eb6457wrowe addr_count + 1);
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe if (ip_addr_list == NULL) {
649c9dd342a580016d29c2866de88a4609eb6457wrowe ret = ENOMEM;
649c9dd342a580016d29c2866de88a4609eb6457wrowe goto done;
649c9dd342a580016d29c2866de88a4609eb6457wrowe }
649c9dd342a580016d29c2866de88a4609eb6457wrowe
649c9dd342a580016d29c2866de88a4609eb6457wrowe ip_addr_list[addr_count - 2] = talloc_strdup(ip_addr_list, ip_addr);
649c9dd342a580016d29c2866de88a4609eb6457wrowe if (ip_addr_list[addr_count - 2] == NULL) {
649c9dd342a580016d29c2866de88a4609eb6457wrowe ret = ENOMEM;
649c9dd342a580016d29c2866de88a4609eb6457wrowe goto done;
649c9dd342a580016d29c2866de88a4609eb6457wrowe }
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe ip_addr_list[addr_count - 1] = talloc_asprintf(ip_addr_list, "%s/%d",
649c9dd342a580016d29c2866de88a4609eb6457wrowe network_addr, netmask);
649c9dd342a580016d29c2866de88a4609eb6457wrowe if (ip_addr_list[addr_count - 1] == NULL) {
649c9dd342a580016d29c2866de88a4609eb6457wrowe ret = ENOMEM;
649c9dd342a580016d29c2866de88a4609eb6457wrowe goto done;
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe }
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe
760b90f5b0bb414646deb6a3a2a53c14bf49bf3dwrowe DEBUG(SSSDBG_TRACE_INTERNAL,
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe ("Found IP address: %s in network %s/%d\n",
15405e91bb3fad5a80f7abe828a00b44a3a65bf8jerenkrantz ip_addr, network_addr, netmask));
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe }
b3edf21d591bfd0e64bbec0dda73c0e41d7ecdb6wrowe
76185d819b745e953dd2cd636fbdd515c333e4a4trawick if (ip_addr_list) {
76185d819b745e953dd2cd636fbdd515c333e4a4trawick ip_addr_list[addr_count] = NULL;
76185d819b745e953dd2cd636fbdd515c333e4a4trawick }
76185d819b745e953dd2cd636fbdd515c333e4a4trawick *_ip_addr_list = talloc_steal(mem_ctx, ip_addr_list);
76185d819b745e953dd2cd636fbdd515c333e4a4trawick
76185d819b745e953dd2cd636fbdd515c333e4a4trawickdone:
76185d819b745e953dd2cd636fbdd515c333e4a4trawick freeifaddrs(ifaces);
399cf0e5e061b49593817421b94305889fa6bc1fjorton talloc_free(tmp_ctx);
399cf0e5e061b49593817421b94305889fa6bc1fjorton
399cf0e5e061b49593817421b94305889fa6bc1fjorton return ret;
399cf0e5e061b49593817421b94305889fa6bc1fjorton}
399cf0e5e061b49593817421b94305889fa6bc1fjorton