1995-05-02 03:31:59 +00:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2017-12-03 17:53:40 -07:00
|
|
|
* Copyright (c) 2010-2016 Todd C. Miller <Todd.Miller@sudo.ws>
|
1995-05-02 03:31:59 +00:00
|
|
|
*
|
2004-02-13 21:36:43 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
1999-07-31 16:19:50 +00:00
|
|
|
*
|
2004-02-13 21:36:43 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1995-05-02 03:31:59 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-26 08:39:09 -06:00
|
|
|
/*
|
|
|
|
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
|
|
|
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
*/
|
2018-10-21 08:46:05 -06:00
|
|
|
|
2004-11-19 18:39:14 +00:00
|
|
|
#include <config.h>
|
1995-05-02 03:31:59 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2001-12-14 19:52:47 +00:00
|
|
|
#include <stdio.h>
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2020-05-18 07:59:24 -06:00
|
|
|
#include <string.h>
|
2010-09-08 15:07:40 -04:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2015-07-09 10:11:25 -06:00
|
|
|
#ifdef NEED_RESOLV_H
|
|
|
|
# include <arpa/nameser.h>
|
|
|
|
# include <resolv.h>
|
|
|
|
#endif /* NEED_RESOLV_H */
|
2001-12-14 19:52:47 +00:00
|
|
|
#include <netdb.h>
|
2007-06-14 16:23:57 +00:00
|
|
|
#include <errno.h>
|
1995-05-02 03:31:59 +00:00
|
|
|
|
2023-09-25 10:13:28 -06:00
|
|
|
#include <sudoers.h>
|
|
|
|
#include <interfaces.h>
|
1995-05-02 03:31:59 +00:00
|
|
|
|
2017-01-07 19:50:05 -07:00
|
|
|
static struct interface_list interfaces = SLIST_HEAD_INITIALIZER(interfaces);
|
2012-10-25 13:15:52 -04:00
|
|
|
|
2000-06-04 23:57:22 +00:00
|
|
|
/*
|
2010-09-08 15:07:40 -04:00
|
|
|
* Parse a space-delimited list of IP address/netmask pairs and
|
2016-12-03 16:39:43 -07:00
|
|
|
* store in a list of interface structures. Returns true on
|
|
|
|
* success and false on parse error or memory allocation error.
|
2000-06-04 23:57:22 +00:00
|
|
|
*/
|
2015-06-17 06:49:59 -06:00
|
|
|
bool
|
2010-09-08 15:07:40 -04:00
|
|
|
set_interfaces(const char *ai)
|
2000-06-04 23:57:22 +00:00
|
|
|
{
|
2015-06-19 12:35:51 -06:00
|
|
|
char *addrinfo, *addr, *mask, *last;
|
2010-09-08 15:07:40 -04:00
|
|
|
struct interface *ifp;
|
2016-09-08 16:38:08 -06:00
|
|
|
bool ret = false;
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(set_interfaces, SUDOERS_DEBUG_NETIF);
|
2000-06-04 23:57:22 +00:00
|
|
|
|
2015-06-17 06:49:59 -06:00
|
|
|
if ((addrinfo = strdup(ai)) == NULL)
|
|
|
|
debug_return_bool(false);
|
2015-06-19 12:35:51 -06:00
|
|
|
for (addr = strtok_r(addrinfo, " \t", &last); addr != NULL; addr = strtok_r(NULL, " \t", &last)) {
|
2010-09-08 15:07:40 -04:00
|
|
|
/* Separate addr and mask. */
|
|
|
|
if ((mask = strchr(addr, '/')) == NULL)
|
2000-06-04 23:57:22 +00:00
|
|
|
continue;
|
2010-09-08 15:07:40 -04:00
|
|
|
*mask++ = '\0';
|
2000-06-04 23:57:22 +00:00
|
|
|
|
2010-09-08 15:07:40 -04:00
|
|
|
/* Parse addr and store in list. */
|
2015-07-14 15:28:01 -06:00
|
|
|
if ((ifp = calloc(1, sizeof(*ifp))) == NULL) {
|
2016-12-03 16:39:43 -07:00
|
|
|
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
|
2015-06-17 06:49:59 -06:00
|
|
|
goto done;
|
2015-07-14 15:28:01 -06:00
|
|
|
}
|
2010-09-08 15:07:40 -04:00
|
|
|
if (strchr(addr, ':')) {
|
|
|
|
/* IPv6 */
|
2011-12-01 11:07:17 -05:00
|
|
|
#ifdef HAVE_STRUCT_IN6_ADDR
|
2010-09-08 15:07:40 -04:00
|
|
|
ifp->family = AF_INET6;
|
2016-12-03 16:39:43 -07:00
|
|
|
if (inet_pton(AF_INET6, addr, &ifp->addr.ip6) != 1) {
|
|
|
|
sudo_warnx(U_("unable to parse IP address \"%s\""), addr);
|
|
|
|
free(ifp);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (inet_pton(AF_INET6, mask, &ifp->netmask.ip6) != 1) {
|
|
|
|
sudo_warnx(U_("unable to parse netmask \"%s\""), mask);
|
2015-06-17 06:49:59 -06:00
|
|
|
free(ifp);
|
2016-12-03 16:39:43 -07:00
|
|
|
goto done;
|
2010-09-08 15:07:40 -04:00
|
|
|
}
|
2016-12-03 16:39:43 -07:00
|
|
|
#else
|
|
|
|
free(ifp);
|
|
|
|
continue;
|
|
|
|
#endif
|
1995-05-02 03:31:59 +00:00
|
|
|
} else {
|
2010-09-08 15:07:40 -04:00
|
|
|
/* IPv4 */
|
2010-10-11 15:43:59 -04:00
|
|
|
ifp->family = AF_INET;
|
2016-12-03 16:39:43 -07:00
|
|
|
if (inet_pton(AF_INET, addr, &ifp->addr.ip4) != 1) {
|
|
|
|
sudo_warnx(U_("unable to parse IP address \"%s\""), addr);
|
|
|
|
free(ifp);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (inet_pton(AF_INET, mask, &ifp->netmask.ip4) != 1) {
|
|
|
|
sudo_warnx(U_("unable to parse netmask \"%s\""), mask);
|
2015-06-17 06:49:59 -06:00
|
|
|
free(ifp);
|
2016-12-03 16:39:43 -07:00
|
|
|
goto done;
|
2010-09-08 15:07:40 -04:00
|
|
|
}
|
1995-05-02 03:31:59 +00:00
|
|
|
}
|
2013-10-22 09:08:09 -06:00
|
|
|
SLIST_INSERT_HEAD(&interfaces, ifp, entries);
|
1995-07-11 19:41:52 +00:00
|
|
|
}
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = true;
|
2015-06-17 06:49:59 -06:00
|
|
|
|
|
|
|
done:
|
|
|
|
free(addrinfo);
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_bool(ret);
|
1995-05-02 03:31:59 +00:00
|
|
|
}
|
1995-07-01 19:49:17 +00:00
|
|
|
|
2013-10-22 09:08:09 -06:00
|
|
|
struct interface_list *
|
2012-10-25 13:15:52 -04:00
|
|
|
get_interfaces(void)
|
|
|
|
{
|
2013-10-22 09:08:09 -06:00
|
|
|
return &interfaces;
|
2012-10-25 13:15:52 -04:00
|
|
|
}
|
|
|
|
|
1999-04-10 04:49:03 +00:00
|
|
|
void
|
2010-09-08 15:07:40 -04:00
|
|
|
dump_interfaces(const char *ai)
|
1995-07-03 18:16:16 +00:00
|
|
|
{
|
2015-06-19 08:57:54 -06:00
|
|
|
const char *cp, *ep;
|
|
|
|
const char *ai_end = ai + strlen(ai);
|
2019-12-22 08:48:16 -07:00
|
|
|
debug_decl(set_interfaces, SUDOERS_DEBUG_NETIF);
|
1995-07-03 18:16:16 +00:00
|
|
|
|
2015-06-19 08:57:54 -06:00
|
|
|
sudo_printf(SUDO_CONV_INFO_MSG,
|
|
|
|
_("Local IP address and netmask pairs:\n"));
|
|
|
|
cp = sudo_strsplit(ai, ai_end, " \t", &ep);
|
|
|
|
while (cp != NULL) {
|
|
|
|
sudo_printf(SUDO_CONV_INFO_MSG, "\t%.*s\n", (int)(ep - cp), cp);
|
|
|
|
cp = sudo_strsplit(NULL, ai_end, " \t", &ep);
|
2015-06-17 06:49:59 -06:00
|
|
|
}
|
2015-06-19 08:57:54 -06:00
|
|
|
|
2011-10-22 14:40:21 -04:00
|
|
|
debug_return;
|
2000-06-04 23:57:22 +00:00
|
|
|
}
|