2019-07-03 01:14:50 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
2019-07-03 01:14:53 +02:00
|
|
|
#include <arpa/inet.h>
|
2019-08-02 02:36:23 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdio.h>
|
2019-08-07 17:46:30 +02:00
|
|
|
#include <unistd.h>
|
2019-07-03 01:14:50 +02:00
|
|
|
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
#include "src/iwd.h"
|
2019-11-07 23:33:51 +01:00
|
|
|
#include "src/module.h"
|
2019-07-03 01:14:52 +02:00
|
|
|
#include "src/dbus.h"
|
2021-10-21 10:50:30 +02:00
|
|
|
#include "src/netconfig.h"
|
2019-07-03 01:14:50 +02:00
|
|
|
#include "src/resolve.h"
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
struct resolve_ops {
|
2020-11-18 16:58:28 +01:00
|
|
|
void (*set_dns)(struct resolve *resolve, char **dns_list);
|
2020-11-18 19:00:17 +01:00
|
|
|
void (*set_domains)(struct resolve *resolve, char **domain_list);
|
2021-02-11 21:54:17 +01:00
|
|
|
void (*set_mdns)(struct resolve *resolve, const char *mdns);
|
2020-08-21 21:50:44 +02:00
|
|
|
void (*revert)(struct resolve *resolve);
|
|
|
|
void (*destroy)(struct resolve *resolve);
|
2019-07-03 01:14:51 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
struct resolve {
|
|
|
|
const struct resolve_ops *ops;
|
|
|
|
uint32_t ifindex;
|
2019-07-03 01:14:51 +02:00
|
|
|
};
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static inline void _resolve_init(struct resolve *resolve, uint32_t ifindex,
|
|
|
|
const struct resolve_ops *ops)
|
|
|
|
{
|
|
|
|
resolve->ifindex = ifindex;
|
|
|
|
resolve->ops = ops;
|
|
|
|
}
|
|
|
|
|
2020-11-18 16:58:28 +01:00
|
|
|
void resolve_set_dns(struct resolve *resolve, char **dns_list)
|
2020-08-21 21:50:44 +02:00
|
|
|
{
|
|
|
|
if (!dns_list || !*dns_list)
|
|
|
|
return;
|
|
|
|
|
2020-11-18 16:58:28 +01:00
|
|
|
if (!resolve->ops->set_dns)
|
2020-08-21 21:50:44 +02:00
|
|
|
return;
|
|
|
|
|
2020-11-18 16:58:28 +01:00
|
|
|
resolve->ops->set_dns(resolve, dns_list);
|
2020-08-21 21:50:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-18 19:00:17 +01:00
|
|
|
void resolve_set_domains(struct resolve *resolve, char **domain_list)
|
2020-08-21 21:50:44 +02:00
|
|
|
{
|
2020-11-18 19:00:17 +01:00
|
|
|
if (!domain_list)
|
2020-08-21 21:50:44 +02:00
|
|
|
return;
|
|
|
|
|
2020-11-18 19:00:17 +01:00
|
|
|
if (!resolve->ops->set_domains)
|
2020-08-21 21:50:44 +02:00
|
|
|
return;
|
|
|
|
|
2020-11-18 19:00:17 +01:00
|
|
|
resolve->ops->set_domains(resolve, domain_list);
|
2020-08-21 21:50:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-11 21:54:17 +01:00
|
|
|
void resolve_set_mdns(struct resolve *resolve, const char *mdns)
|
|
|
|
{
|
|
|
|
if (!mdns)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!resolve->ops->set_mdns)
|
|
|
|
return;
|
|
|
|
|
|
|
|
resolve->ops->set_mdns(resolve, mdns);
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
void resolve_revert(struct resolve *resolve)
|
|
|
|
{
|
|
|
|
if (!resolve->ops->revert)
|
|
|
|
return;
|
|
|
|
|
|
|
|
resolve->ops->revert(resolve);
|
|
|
|
}
|
|
|
|
|
|
|
|
void resolve_free(struct resolve *resolve)
|
|
|
|
{
|
|
|
|
resolve->ops->destroy(resolve);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct resolve_method_ops {
|
|
|
|
int (*init)(void);
|
|
|
|
void (*exit)(void);
|
|
|
|
struct resolve *(*alloc)(uint32_t ifindex);
|
|
|
|
};
|
2019-07-03 01:14:51 +02:00
|
|
|
|
2019-07-03 01:14:52 +02:00
|
|
|
#define SYSTEMD_RESOLVED_SERVICE "org.freedesktop.resolve1"
|
2019-07-03 01:14:53 +02:00
|
|
|
#define SYSTEMD_RESOLVED_MANAGER_PATH "/org/freedesktop/resolve1"
|
|
|
|
#define SYSTEMD_RESOLVED_MANAGER_INTERFACE "org.freedesktop.resolve1.Manager"
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
struct systemd_method_state {
|
2019-07-03 01:14:52 +02:00
|
|
|
uint32_t service_watch;
|
|
|
|
bool is_ready:1;
|
|
|
|
};
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
struct systemd_method_state systemd_state;
|
|
|
|
|
|
|
|
struct systemd {
|
|
|
|
struct resolve super;
|
|
|
|
};
|
|
|
|
|
2021-02-11 21:54:17 +01:00
|
|
|
static void systemd_link_generic_reply(struct l_dbus_message *message,
|
2019-07-03 01:14:53 +02:00
|
|
|
void *user_data)
|
|
|
|
{
|
2021-02-11 21:54:17 +01:00
|
|
|
const char *type = user_data;
|
2019-07-03 01:14:53 +02:00
|
|
|
const char *name;
|
|
|
|
const char *text;
|
|
|
|
|
2021-04-30 18:04:33 +02:00
|
|
|
if (!l_dbus_message_get_error(message, &name, &text))
|
2019-07-03 01:14:53 +02:00
|
|
|
return;
|
|
|
|
|
2021-02-11 21:54:17 +01:00
|
|
|
l_error("resolve-systemd: Failed to modify the %s entries. %s: %s",
|
|
|
|
type, name, text);
|
2019-07-03 01:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool systemd_builder_add_dns(struct l_dbus_message_builder *builder,
|
2020-11-18 16:58:28 +01:00
|
|
|
const char *dns)
|
2019-07-03 01:14:53 +02:00
|
|
|
{
|
|
|
|
uint8_t buf[16];
|
|
|
|
uint8_t buf_size;
|
|
|
|
uint8_t i;
|
2020-11-18 16:58:28 +01:00
|
|
|
int t;
|
2019-07-03 01:14:53 +02:00
|
|
|
|
2020-11-18 16:58:28 +01:00
|
|
|
l_debug("installing DNS: %s", dns);
|
2019-07-03 01:14:53 +02:00
|
|
|
|
2020-11-18 16:58:28 +01:00
|
|
|
if (inet_pton(AF_INET, dns, buf) == 1) {
|
|
|
|
t = AF_INET;
|
2019-07-03 01:14:53 +02:00
|
|
|
buf_size = 4;
|
2020-11-18 16:58:28 +01:00
|
|
|
} else if (inet_pton(AF_INET6, dns, buf) == 1) {
|
|
|
|
t = AF_INET6;
|
2019-10-10 01:28:32 +02:00
|
|
|
buf_size = 16;
|
2020-11-18 16:58:28 +01:00
|
|
|
} else
|
2019-07-03 01:14:53 +02:00
|
|
|
return false;
|
2020-11-18 16:58:28 +01:00
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'i', &t);
|
|
|
|
l_dbus_message_builder_enter_array(builder, "y");
|
2019-07-03 01:14:53 +02:00
|
|
|
|
|
|
|
for (i = 0; i < buf_size; i++)
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'y', &buf[i]);
|
|
|
|
|
|
|
|
l_dbus_message_builder_leave_array(builder);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-18 16:58:28 +01:00
|
|
|
static void resolve_systemd_set_dns(struct resolve *resolve, char **dns_list)
|
2019-07-03 01:14:52 +02:00
|
|
|
{
|
2019-07-03 01:14:53 +02:00
|
|
|
struct l_dbus_message_builder *builder;
|
|
|
|
struct l_dbus_message *message;
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
l_debug("ifindex: %u", resolve->ifindex);
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
if (L_WARN_ON(!systemd_state.is_ready))
|
2019-07-03 01:14:52 +02:00
|
|
|
return;
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
message = l_dbus_message_new_method_call(dbus_get_bus(),
|
2019-07-03 01:14:53 +02:00
|
|
|
SYSTEMD_RESOLVED_SERVICE,
|
|
|
|
SYSTEMD_RESOLVED_MANAGER_PATH,
|
|
|
|
SYSTEMD_RESOLVED_MANAGER_INTERFACE,
|
|
|
|
"SetLinkDNS");
|
|
|
|
|
|
|
|
if (!message)
|
|
|
|
return;
|
|
|
|
|
|
|
|
builder = l_dbus_message_builder_new(message);
|
|
|
|
if (!builder) {
|
|
|
|
l_dbus_message_unref(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
l_dbus_message_builder_append_basic(builder, 'i', &resolve->ifindex);
|
2019-07-03 01:14:53 +02:00
|
|
|
|
|
|
|
l_dbus_message_builder_enter_array(builder, "(iay)");
|
|
|
|
|
|
|
|
for (; *dns_list; dns_list++) {
|
|
|
|
l_dbus_message_builder_enter_struct(builder, "iay");
|
|
|
|
|
2020-11-18 16:58:28 +01:00
|
|
|
if (systemd_builder_add_dns(builder, *dns_list)) {
|
2019-07-03 01:14:53 +02:00
|
|
|
l_dbus_message_builder_leave_struct(builder);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_dbus_message_builder_destroy(builder);
|
|
|
|
l_dbus_message_unref(message);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_dbus_message_builder_leave_array(builder);
|
|
|
|
|
|
|
|
l_dbus_message_builder_finalize(builder);
|
|
|
|
l_dbus_message_builder_destroy(builder);
|
|
|
|
|
2021-02-11 21:54:17 +01:00
|
|
|
l_dbus_send_with_reply(dbus_get_bus(), message,
|
|
|
|
systemd_link_generic_reply, "DNS", NULL);
|
2019-12-11 01:34:54 +01:00
|
|
|
}
|
|
|
|
|
2020-11-18 19:00:17 +01:00
|
|
|
static void resolve_systemd_set_domains(struct resolve *resolve,
|
|
|
|
char **domain_list)
|
2019-12-11 01:34:54 +01:00
|
|
|
{
|
|
|
|
struct l_dbus_message *message;
|
2020-11-18 19:00:17 +01:00
|
|
|
struct l_dbus_message_builder *builder;
|
|
|
|
bool f = false;
|
2019-12-11 01:34:54 +01:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
l_debug("ifindex: %u", resolve->ifindex);
|
2019-12-11 01:34:54 +01:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
if (L_WARN_ON(!systemd_state.is_ready))
|
2019-12-11 01:34:54 +01:00
|
|
|
return;
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
message = l_dbus_message_new_method_call(dbus_get_bus(),
|
2019-12-11 01:34:54 +01:00
|
|
|
SYSTEMD_RESOLVED_SERVICE,
|
|
|
|
SYSTEMD_RESOLVED_MANAGER_PATH,
|
|
|
|
SYSTEMD_RESOLVED_MANAGER_INTERFACE,
|
|
|
|
"SetLinkDomains");
|
|
|
|
|
|
|
|
if (!message)
|
|
|
|
return;
|
|
|
|
|
2020-11-18 19:00:17 +01:00
|
|
|
builder = l_dbus_message_builder_new(message);
|
|
|
|
if (!builder) {
|
|
|
|
l_dbus_message_unref(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'i', &resolve->ifindex);
|
|
|
|
l_dbus_message_builder_enter_array(builder, "(sb)");
|
|
|
|
|
|
|
|
for (; *domain_list; domain_list++) {
|
|
|
|
l_dbus_message_builder_enter_struct(builder, "sb");
|
|
|
|
l_dbus_message_builder_append_basic(builder, 's', *domain_list);
|
|
|
|
l_dbus_message_builder_append_basic(builder, 'b', &f);
|
|
|
|
l_dbus_message_builder_leave_struct(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
l_dbus_message_builder_leave_array(builder);
|
|
|
|
|
|
|
|
l_dbus_message_builder_finalize(builder);
|
|
|
|
l_dbus_message_builder_destroy(builder);
|
2019-12-11 01:34:54 +01:00
|
|
|
|
|
|
|
l_dbus_send_with_reply(dbus_get_bus(), message,
|
2021-02-11 21:54:17 +01:00
|
|
|
systemd_link_generic_reply, "domains", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void resolve_systemd_set_mdns(struct resolve *resolve, const char *mdns)
|
|
|
|
{
|
|
|
|
struct l_dbus_message *message;
|
|
|
|
|
|
|
|
l_debug("ifindex: %u", resolve->ifindex);
|
|
|
|
|
|
|
|
if (L_WARN_ON(!systemd_state.is_ready))
|
|
|
|
return;
|
|
|
|
|
|
|
|
message = l_dbus_message_new_method_call(dbus_get_bus(),
|
|
|
|
SYSTEMD_RESOLVED_SERVICE,
|
|
|
|
SYSTEMD_RESOLVED_MANAGER_PATH,
|
|
|
|
SYSTEMD_RESOLVED_MANAGER_INTERFACE,
|
|
|
|
"SetLinkMulticastDNS");
|
|
|
|
|
|
|
|
if (!message)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l_dbus_message_set_arguments(message, "is", resolve->ifindex, mdns);
|
|
|
|
l_dbus_send_with_reply(dbus_get_bus(), message,
|
|
|
|
systemd_link_generic_reply,
|
|
|
|
"MulticastDNS", NULL);
|
2019-12-11 01:34:54 +01:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static void resolve_systemd_revert(struct resolve *resolve)
|
2019-07-03 01:14:52 +02:00
|
|
|
{
|
2019-07-03 22:41:00 +02:00
|
|
|
struct l_dbus_message *message;
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
l_debug("ifindex: %u", resolve->ifindex);
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
if (L_WARN_ON(!systemd_state.is_ready))
|
2019-07-03 01:14:52 +02:00
|
|
|
return;
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
message = l_dbus_message_new_method_call(dbus_get_bus(),
|
2019-07-03 22:41:00 +02:00
|
|
|
SYSTEMD_RESOLVED_SERVICE,
|
|
|
|
SYSTEMD_RESOLVED_MANAGER_PATH,
|
|
|
|
SYSTEMD_RESOLVED_MANAGER_INTERFACE,
|
|
|
|
"RevertLink");
|
|
|
|
if (!message)
|
|
|
|
return;
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
l_dbus_message_set_arguments(message, "i", resolve->ifindex);
|
2021-02-11 21:54:17 +01:00
|
|
|
l_dbus_send_with_reply(dbus_get_bus(), message,
|
|
|
|
systemd_link_generic_reply, "DNS", NULL);
|
2019-07-03 01:14:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static void resolve_systemd_destroy(struct resolve *resolve)
|
2019-07-03 01:14:52 +02:00
|
|
|
{
|
2020-08-21 21:50:44 +02:00
|
|
|
struct systemd *sd = l_container_of(resolve, struct systemd, super);
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
l_free(sd);
|
2019-07-03 01:14:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static const struct resolve_ops systemd_ops = {
|
2020-11-18 16:58:28 +01:00
|
|
|
.set_dns = resolve_systemd_set_dns,
|
2020-11-18 19:00:17 +01:00
|
|
|
.set_domains = resolve_systemd_set_domains,
|
2021-02-11 21:54:17 +01:00
|
|
|
.set_mdns = resolve_systemd_set_mdns,
|
2020-08-21 21:50:44 +02:00
|
|
|
.revert = resolve_systemd_revert,
|
|
|
|
.destroy = resolve_systemd_destroy,
|
|
|
|
};
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static void systemd_appeared(struct l_dbus *dbus, void *user_data)
|
|
|
|
{
|
|
|
|
systemd_state.is_ready = true;
|
2019-07-03 01:14:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static void systemd_disappeared(struct l_dbus *dbus, void *user_data)
|
2019-07-03 01:14:52 +02:00
|
|
|
{
|
2020-08-21 21:50:44 +02:00
|
|
|
systemd_state.is_ready = false;
|
|
|
|
}
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static int resolve_systemd_init(void)
|
|
|
|
{
|
|
|
|
systemd_state.service_watch =
|
|
|
|
l_dbus_add_service_watch(dbus_get_bus(),
|
2019-07-03 01:14:52 +02:00
|
|
|
SYSTEMD_RESOLVED_SERVICE,
|
|
|
|
systemd_appeared,
|
|
|
|
systemd_disappeared,
|
2020-08-21 21:50:44 +02:00
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static void resolve_systemd_exit(void)
|
|
|
|
{
|
|
|
|
l_dbus_remove_watch(dbus_get_bus(), systemd_state.service_watch);
|
|
|
|
memset(&systemd_state, 0, sizeof(systemd_state));
|
2019-07-03 01:14:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static struct resolve *resolve_systemd_alloc(uint32_t ifindex)
|
2019-07-03 01:14:52 +02:00
|
|
|
{
|
2020-08-21 21:50:44 +02:00
|
|
|
struct systemd *sd = l_new(struct systemd, 1);
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
_resolve_init(&sd->super, ifindex, &systemd_ops);
|
2019-07-03 01:14:52 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
return &sd->super;
|
2019-07-03 01:14:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static const struct resolve_method_ops resolve_method_systemd_ops = {
|
2019-07-03 01:14:52 +02:00
|
|
|
.init = resolve_systemd_init,
|
|
|
|
.exit = resolve_systemd_exit,
|
2020-08-21 21:50:44 +02:00
|
|
|
.alloc = resolve_systemd_alloc,
|
|
|
|
};
|
|
|
|
|
|
|
|
char *resolvconf_path;
|
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
static bool resolvconf_invoke(const char *ifname, const char *type,
|
|
|
|
const char *content)
|
2019-08-02 02:36:23 +02:00
|
|
|
{
|
|
|
|
FILE *resolvconf;
|
|
|
|
L_AUTO_FREE_VAR(char *, cmd) = NULL;
|
2020-08-21 22:34:29 +02:00
|
|
|
int error;
|
2019-08-02 02:36:23 +02:00
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
if (content) {
|
|
|
|
cmd = l_strdup_printf("%s -a %s.%s", resolvconf_path,
|
|
|
|
ifname, type);
|
|
|
|
resolvconf = popen(cmd, "w");
|
|
|
|
} else {
|
|
|
|
cmd = l_strdup_printf("%s -d %s.%s", resolvconf_path,
|
|
|
|
ifname, type);
|
|
|
|
resolvconf = popen(cmd, "r");
|
|
|
|
}
|
2019-08-02 02:36:23 +02:00
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
if (!resolvconf) {
|
2019-08-07 18:09:50 +02:00
|
|
|
l_error("resolve: Failed to start %s (%s).", resolvconf_path,
|
2019-08-02 02:36:23 +02:00
|
|
|
strerror(errno));
|
2020-08-21 22:34:29 +02:00
|
|
|
return false;
|
2019-08-02 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
if (content && fprintf(resolvconf, "%s", content) < 0)
|
2019-08-02 02:36:23 +02:00
|
|
|
l_error("resolve: Failed to print into %s stdin.",
|
2019-08-07 18:09:50 +02:00
|
|
|
resolvconf_path);
|
2019-08-02 02:36:23 +02:00
|
|
|
|
|
|
|
error = pclose(resolvconf);
|
|
|
|
if (error < 0)
|
|
|
|
l_error("resolve: Failed to close pipe to %s (%s).",
|
2019-08-07 18:09:50 +02:00
|
|
|
resolvconf_path, strerror(errno));
|
2019-08-02 02:36:23 +02:00
|
|
|
else if (error > 0)
|
2019-08-07 18:09:50 +02:00
|
|
|
l_info("resolve: %s exited with status (%d).", resolvconf_path,
|
2019-08-02 02:36:23 +02:00
|
|
|
error);
|
2020-08-21 22:34:29 +02:00
|
|
|
|
|
|
|
return !error;
|
2019-08-02 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
struct resolvconf {
|
|
|
|
struct resolve super;
|
2020-08-21 22:41:55 +02:00
|
|
|
bool have_domain : 1;
|
2020-08-21 22:34:29 +02:00
|
|
|
bool have_dns : 1;
|
|
|
|
char *ifname;
|
|
|
|
};
|
|
|
|
|
2020-11-18 16:58:28 +01:00
|
|
|
static void resolve_resolvconf_set_dns(struct resolve *resolve, char **dns_list)
|
2019-08-02 02:36:23 +02:00
|
|
|
{
|
2020-08-21 22:34:29 +02:00
|
|
|
struct resolvconf *rc =
|
|
|
|
l_container_of(resolve, struct resolvconf, super);
|
|
|
|
struct l_string *content;
|
|
|
|
L_AUTO_FREE_VAR(char *, str) = NULL;
|
2019-08-02 02:36:23 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
if (L_WARN_ON(!resolvconf_path))
|
2019-08-02 02:36:23 +02:00
|
|
|
return;
|
|
|
|
|
2020-12-02 17:57:57 +01:00
|
|
|
if (!dns_list || !dns_list[0]) {
|
|
|
|
if (rc->have_dns)
|
|
|
|
resolvconf_invoke(rc->ifname, "dns", NULL);
|
|
|
|
|
|
|
|
rc->have_dns = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
content = l_string_new(0);
|
2019-08-02 02:36:23 +02:00
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
for (; *dns_list; dns_list++)
|
|
|
|
l_string_append_printf(content, "nameserver %s\n", *dns_list);
|
2019-08-02 02:36:23 +02:00
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
str = l_string_unwrap(content);
|
|
|
|
|
|
|
|
if (resolvconf_invoke(rc->ifname, "dns", str))
|
|
|
|
rc->have_dns = true;
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:00:17 +01:00
|
|
|
static void resolve_resolvconf_set_domains(struct resolve *resolve,
|
|
|
|
char **domain_list)
|
2020-08-21 22:41:55 +02:00
|
|
|
{
|
|
|
|
struct resolvconf *rc =
|
|
|
|
l_container_of(resolve, struct resolvconf, super);
|
2020-11-18 19:00:17 +01:00
|
|
|
struct l_string *content;
|
|
|
|
L_AUTO_FREE_VAR(char *, str) = NULL;
|
2020-08-21 22:41:55 +02:00
|
|
|
|
|
|
|
if (L_WARN_ON(!resolvconf_path))
|
|
|
|
return;
|
|
|
|
|
2020-12-02 17:57:57 +01:00
|
|
|
if (!domain_list || !domain_list[0]) {
|
|
|
|
if (rc->have_domain)
|
|
|
|
resolvconf_invoke(rc->ifname, "domain", NULL);
|
|
|
|
|
|
|
|
rc->have_domain = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:00:17 +01:00
|
|
|
content = l_string_new(0);
|
|
|
|
|
|
|
|
for (; *domain_list; domain_list++)
|
|
|
|
l_string_append_printf(content, "search %s\n", *domain_list);
|
|
|
|
|
|
|
|
str = l_string_unwrap(content);
|
2020-08-21 22:41:55 +02:00
|
|
|
|
2020-11-18 19:00:17 +01:00
|
|
|
if (resolvconf_invoke(rc->ifname, "domain", str))
|
2020-08-21 22:41:55 +02:00
|
|
|
rc->have_domain = true;
|
|
|
|
}
|
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
static void resolve_resolvconf_revert(struct resolve *resolve)
|
|
|
|
{
|
|
|
|
struct resolvconf *rc =
|
|
|
|
l_container_of(resolve, struct resolvconf, super);
|
|
|
|
|
|
|
|
if (rc->have_dns)
|
|
|
|
resolvconf_invoke(rc->ifname, "dns", NULL);
|
|
|
|
|
2020-08-21 22:41:55 +02:00
|
|
|
if (rc->have_domain)
|
|
|
|
resolvconf_invoke(rc->ifname, "domain", NULL);
|
|
|
|
|
2020-08-21 22:34:29 +02:00
|
|
|
rc->have_dns = false;
|
2020-08-21 22:41:55 +02:00
|
|
|
rc->have_domain = false;
|
2019-08-02 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static void resolve_resolvconf_destroy(struct resolve *resolve)
|
|
|
|
{
|
|
|
|
struct resolvconf *rc =
|
|
|
|
l_container_of(resolve, struct resolvconf, super);
|
|
|
|
|
|
|
|
l_free(rc->ifname);
|
|
|
|
l_free(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct resolve_ops resolvconf_ops = {
|
2020-11-18 16:58:28 +01:00
|
|
|
.set_dns = resolve_resolvconf_set_dns,
|
2020-11-18 19:00:17 +01:00
|
|
|
.set_domains = resolve_resolvconf_set_domains,
|
2020-08-21 21:50:44 +02:00
|
|
|
.revert = resolve_resolvconf_revert,
|
|
|
|
.destroy = resolve_resolvconf_destroy,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int resolve_resolvconf_init(void)
|
2019-08-02 02:36:23 +02:00
|
|
|
{
|
2019-08-07 17:46:30 +02:00
|
|
|
static const char *default_path = "/sbin:/usr/sbin";
|
|
|
|
const char *path;
|
2019-08-02 02:36:23 +02:00
|
|
|
|
2019-08-07 17:46:30 +02:00
|
|
|
l_debug("Trying to find resolvconf in $PATH");
|
2020-08-21 21:50:44 +02:00
|
|
|
|
2019-08-07 17:46:30 +02:00
|
|
|
path = getenv("PATH");
|
|
|
|
if (path)
|
2019-08-07 18:09:50 +02:00
|
|
|
resolvconf_path = l_path_find("resolvconf", path, X_OK);
|
2019-08-07 17:46:30 +02:00
|
|
|
|
2019-08-07 18:09:50 +02:00
|
|
|
if (!resolvconf_path) {
|
2019-08-07 17:46:30 +02:00
|
|
|
l_debug("Trying to find resolvconf in default paths");
|
2019-08-07 18:09:50 +02:00
|
|
|
resolvconf_path = l_path_find("resolvconf", default_path, X_OK);
|
2019-08-02 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 18:09:50 +02:00
|
|
|
if (!resolvconf_path) {
|
2019-08-07 17:46:30 +02:00
|
|
|
l_error("No usable resolvconf found on system");
|
2020-08-21 21:50:44 +02:00
|
|
|
return -ENOENT;
|
2019-08-02 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 18:09:50 +02:00
|
|
|
l_debug("resolvconf found as: %s", resolvconf_path);
|
2020-08-21 21:50:44 +02:00
|
|
|
return 0;
|
2019-08-02 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static void resolve_resolvconf_exit(void)
|
2019-08-02 02:36:23 +02:00
|
|
|
{
|
2019-08-07 18:13:10 +02:00
|
|
|
l_free(resolvconf_path);
|
2019-08-07 18:09:50 +02:00
|
|
|
resolvconf_path = NULL;
|
2019-08-02 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static struct resolve *resolve_resolvconf_alloc(uint32_t ifindex)
|
2019-07-03 01:14:50 +02:00
|
|
|
{
|
2020-08-21 21:50:44 +02:00
|
|
|
struct resolvconf *rc = l_new(struct resolvconf, 1);
|
2019-07-03 01:14:51 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
_resolve_init(&rc->super, ifindex, &resolvconf_ops);
|
2019-07-03 01:14:51 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
rc->ifname = l_net_get_name(ifindex);
|
|
|
|
if (!rc->ifname)
|
|
|
|
rc->ifname = l_strdup_printf("%u", ifindex);
|
2019-07-03 01:14:50 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
return &rc->super;
|
|
|
|
}
|
2019-12-11 01:34:54 +01:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static const struct resolve_method_ops resolve_method_resolvconf_ops = {
|
|
|
|
.init = resolve_resolvconf_init,
|
|
|
|
.exit = resolve_resolvconf_exit,
|
|
|
|
.alloc = resolve_resolvconf_alloc,
|
|
|
|
};
|
2019-12-11 01:34:54 +01:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
static const struct resolve_method_ops *configured_method;
|
2019-12-11 01:34:54 +01:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
struct resolve *resolve_new(uint32_t ifindex)
|
2019-07-03 01:14:50 +02:00
|
|
|
{
|
2020-08-21 21:50:44 +02:00
|
|
|
if (L_WARN_ON(!configured_method))
|
|
|
|
return NULL;
|
2019-07-03 01:14:51 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
return configured_method->alloc(ifindex);
|
2019-07-03 01:14:50 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 01:14:51 +02:00
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
const struct resolve_method_ops *method_ops;
|
|
|
|
} resolve_method_ops_list[] = {
|
2020-08-21 21:50:44 +02:00
|
|
|
{ "systemd", &resolve_method_systemd_ops },
|
|
|
|
{ "resolvconf", &resolve_method_resolvconf_ops },
|
2019-07-03 01:14:51 +02:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2019-07-03 01:14:50 +02:00
|
|
|
static int resolve_init(void)
|
|
|
|
{
|
2019-07-03 01:14:51 +02:00
|
|
|
const char *method_name;
|
|
|
|
uint8_t i;
|
2019-07-03 01:14:50 +02:00
|
|
|
|
2021-10-21 10:50:30 +02:00
|
|
|
if (!netconfig_enabled())
|
2019-07-03 01:14:50 +02:00
|
|
|
return 0;
|
|
|
|
|
2019-10-25 20:13:40 +02:00
|
|
|
method_name = l_settings_get_value(iwd_get_config(), "Network",
|
|
|
|
"NameResolvingService");
|
2021-06-01 17:28:47 +02:00
|
|
|
if (!method_name)
|
|
|
|
method_name = "systemd";
|
2019-07-03 01:14:51 +02:00
|
|
|
|
|
|
|
for (i = 0; resolve_method_ops_list[i].name; i++) {
|
|
|
|
if (strcmp(resolve_method_ops_list[i].name, method_name))
|
|
|
|
continue;
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
configured_method = resolve_method_ops_list[i].method_ops;
|
2019-07-03 01:14:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
if (!configured_method) {
|
2019-10-25 20:13:40 +02:00
|
|
|
l_error("Unknown resolution method: %s", method_name);
|
2019-07-03 01:14:51 +02:00
|
|
|
return -EINVAL;
|
2019-10-25 20:13:40 +02:00
|
|
|
}
|
2019-07-03 01:14:51 +02:00
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
return configured_method->init();
|
2019-07-03 01:14:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void resolve_exit(void)
|
|
|
|
{
|
2020-08-21 21:50:44 +02:00
|
|
|
if (!configured_method)
|
2019-07-03 01:14:51 +02:00
|
|
|
return;
|
|
|
|
|
2020-08-21 21:50:44 +02:00
|
|
|
configured_method->exit();
|
|
|
|
configured_method = NULL;
|
2019-07-03 01:14:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IWD_MODULE(resolve, resolve_init, resolve_exit)
|