eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

resolve.c (15140B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <errno.h>
     29 #include <arpa/inet.h>
     30 #include <sys/stat.h>
     31 #include <stdio.h>
     32 #include <unistd.h>
     33 
     34 #include <ell/ell.h>
     35 
     36 #include "src/iwd.h"
     37 #include "src/module.h"
     38 #include "src/dbus.h"
     39 #include "src/netconfig.h"
     40 #include "src/resolve.h"
     41 
     42 struct resolve_ops {
     43 	void (*set_dns)(struct resolve *resolve, char **dns_list);
     44 	void (*set_domains)(struct resolve *resolve, char **domain_list);
     45 	void (*set_mdns)(struct resolve *resolve, const char *mdns);
     46 	void (*revert)(struct resolve *resolve);
     47 	void (*destroy)(struct resolve *resolve);
     48 };
     49 
     50 struct resolve {
     51 	const struct resolve_ops *ops;
     52 	uint32_t ifindex;
     53 };
     54 
     55 static inline void _resolve_init(struct resolve *resolve, uint32_t ifindex,
     56 					const struct resolve_ops *ops)
     57 {
     58 	resolve->ifindex = ifindex;
     59 	resolve->ops = ops;
     60 }
     61 
     62 void resolve_set_dns(struct resolve *resolve, char **dns_list)
     63 {
     64 	if (!dns_list || !*dns_list)
     65 		return;
     66 
     67 	if (!resolve->ops->set_dns)
     68 		return;
     69 
     70 	resolve->ops->set_dns(resolve, dns_list);
     71 }
     72 
     73 void resolve_set_domains(struct resolve *resolve, char **domain_list)
     74 {
     75 	if (!domain_list)
     76 		return;
     77 
     78 	if (!resolve->ops->set_domains)
     79 		return;
     80 
     81 	resolve->ops->set_domains(resolve, domain_list);
     82 }
     83 
     84 void resolve_set_mdns(struct resolve *resolve, const char *mdns)
     85 {
     86 	if (!mdns)
     87 		return;
     88 
     89 	if (!resolve->ops->set_mdns)
     90 		return;
     91 
     92 	resolve->ops->set_mdns(resolve, mdns);
     93 }
     94 
     95 void resolve_revert(struct resolve *resolve)
     96 {
     97 	if (!resolve->ops->revert)
     98 		return;
     99 
    100 	resolve->ops->revert(resolve);
    101 }
    102 
    103 void resolve_free(struct resolve *resolve)
    104 {
    105 	resolve->ops->destroy(resolve);
    106 }
    107 
    108 struct resolve_method_ops {
    109 	int (*init)(void);
    110 	void (*exit)(void);
    111 	struct resolve *(*alloc)(uint32_t ifindex);
    112 };
    113 
    114 #define SYSTEMD_RESOLVED_SERVICE           "org.freedesktop.resolve1"
    115 #define SYSTEMD_RESOLVED_MANAGER_PATH      "/org/freedesktop/resolve1"
    116 #define SYSTEMD_RESOLVED_MANAGER_INTERFACE "org.freedesktop.resolve1.Manager"
    117 
    118 struct systemd_method_state {
    119 	uint32_t service_watch;
    120 	bool is_ready:1;
    121 };
    122 
    123 struct systemd_method_state systemd_state;
    124 
    125 struct systemd {
    126 	struct resolve super;
    127 };
    128 
    129 static void systemd_link_generic_reply(struct l_dbus_message *message,
    130 								void *user_data)
    131 {
    132 	const char *type = user_data;
    133 	const char *name;
    134 	const char *text;
    135 
    136 	if (!l_dbus_message_get_error(message, &name, &text))
    137 		return;
    138 
    139 	l_error("resolve-systemd: Failed to modify the %s entries. %s: %s",
    140 							type, name, text);
    141 }
    142 
    143 static bool systemd_builder_add_dns(struct l_dbus_message_builder *builder,
    144 							const char *dns)
    145 {
    146 	uint8_t buf[16];
    147 	uint8_t buf_size;
    148 	uint8_t i;
    149 	int t;
    150 
    151 	l_debug("installing DNS: %s", dns);
    152 
    153 	if (inet_pton(AF_INET, dns, buf) == 1) {
    154 		t = AF_INET;
    155 		buf_size = 4;
    156 	} else if (inet_pton(AF_INET6, dns, buf) == 1) {
    157 		t = AF_INET6;
    158 		buf_size = 16;
    159 	} else
    160 		return false;
    161 
    162 	l_dbus_message_builder_append_basic(builder, 'i', &t);
    163 	l_dbus_message_builder_enter_array(builder, "y");
    164 
    165 	for (i = 0; i < buf_size; i++)
    166 		l_dbus_message_builder_append_basic(builder, 'y', &buf[i]);
    167 
    168 	l_dbus_message_builder_leave_array(builder);
    169 
    170 	return true;
    171 }
    172 
    173 static void resolve_systemd_set_dns(struct resolve *resolve, char **dns_list)
    174 {
    175 	struct l_dbus_message_builder *builder;
    176 	struct l_dbus_message *message;
    177 
    178 	l_debug("ifindex: %u", resolve->ifindex);
    179 
    180 	if (L_WARN_ON(!systemd_state.is_ready))
    181 		return;
    182 
    183 	message = l_dbus_message_new_method_call(dbus_get_bus(),
    184 					SYSTEMD_RESOLVED_SERVICE,
    185 					SYSTEMD_RESOLVED_MANAGER_PATH,
    186 					SYSTEMD_RESOLVED_MANAGER_INTERFACE,
    187 					"SetLinkDNS");
    188 
    189 	if (!message)
    190 		return;
    191 
    192 	builder = l_dbus_message_builder_new(message);
    193 	if (!builder) {
    194 		l_dbus_message_unref(message);
    195 		return;
    196 	}
    197 
    198 	l_dbus_message_builder_append_basic(builder, 'i', &resolve->ifindex);
    199 
    200 	l_dbus_message_builder_enter_array(builder, "(iay)");
    201 
    202 	for (; *dns_list; dns_list++) {
    203 		l_dbus_message_builder_enter_struct(builder, "iay");
    204 
    205 		if (systemd_builder_add_dns(builder, *dns_list)) {
    206 			l_dbus_message_builder_leave_struct(builder);
    207 			continue;
    208 		}
    209 
    210 		l_dbus_message_builder_destroy(builder);
    211 		l_dbus_message_unref(message);
    212 
    213 		return;
    214 	}
    215 
    216 	l_dbus_message_builder_leave_array(builder);
    217 
    218 	l_dbus_message_builder_finalize(builder);
    219 	l_dbus_message_builder_destroy(builder);
    220 
    221 	l_dbus_send_with_reply(dbus_get_bus(), message,
    222 				systemd_link_generic_reply, "DNS", NULL);
    223 }
    224 
    225 static void resolve_systemd_set_domains(struct resolve *resolve,
    226 						char **domain_list)
    227 {
    228 	struct l_dbus_message *message;
    229 	struct l_dbus_message_builder *builder;
    230 	bool f = false;
    231 
    232 	l_debug("ifindex: %u", resolve->ifindex);
    233 
    234 	if (L_WARN_ON(!systemd_state.is_ready))
    235 		return;
    236 
    237 	message = l_dbus_message_new_method_call(dbus_get_bus(),
    238 					SYSTEMD_RESOLVED_SERVICE,
    239 					SYSTEMD_RESOLVED_MANAGER_PATH,
    240 					SYSTEMD_RESOLVED_MANAGER_INTERFACE,
    241 					"SetLinkDomains");
    242 
    243 	if (!message)
    244 		return;
    245 
    246 	builder = l_dbus_message_builder_new(message);
    247 	if (!builder) {
    248 		l_dbus_message_unref(message);
    249 		return;
    250 	}
    251 
    252 	l_dbus_message_builder_append_basic(builder, 'i', &resolve->ifindex);
    253 	l_dbus_message_builder_enter_array(builder, "(sb)");
    254 
    255 	for (; *domain_list; domain_list++) {
    256 		l_dbus_message_builder_enter_struct(builder, "sb");
    257 		l_dbus_message_builder_append_basic(builder, 's', *domain_list);
    258 		l_dbus_message_builder_append_basic(builder, 'b', &f);
    259 		l_dbus_message_builder_leave_struct(builder);
    260 	}
    261 
    262 	l_dbus_message_builder_leave_array(builder);
    263 
    264 	l_dbus_message_builder_finalize(builder);
    265 	l_dbus_message_builder_destroy(builder);
    266 
    267 	l_dbus_send_with_reply(dbus_get_bus(), message,
    268 				systemd_link_generic_reply, "domains", NULL);
    269 }
    270 
    271 static void resolve_systemd_set_mdns(struct resolve *resolve, const char *mdns)
    272 {
    273 	struct l_dbus_message *message;
    274 
    275 	l_debug("ifindex: %u", resolve->ifindex);
    276 
    277 	if (L_WARN_ON(!systemd_state.is_ready))
    278 		return;
    279 
    280 	message = l_dbus_message_new_method_call(dbus_get_bus(),
    281 					SYSTEMD_RESOLVED_SERVICE,
    282 					SYSTEMD_RESOLVED_MANAGER_PATH,
    283 					SYSTEMD_RESOLVED_MANAGER_INTERFACE,
    284 					"SetLinkMulticastDNS");
    285 
    286 	if (!message)
    287 		return;
    288 
    289 	l_dbus_message_set_arguments(message, "is", resolve->ifindex, mdns);
    290 	l_dbus_send_with_reply(dbus_get_bus(), message,
    291 				systemd_link_generic_reply,
    292 				"MulticastDNS", NULL);
    293 }
    294 
    295 static void resolve_systemd_revert(struct resolve *resolve)
    296 {
    297 	struct l_dbus_message *message;
    298 
    299 	l_debug("ifindex: %u", resolve->ifindex);
    300 
    301 	if (L_WARN_ON(!systemd_state.is_ready))
    302 		return;
    303 
    304 	message = l_dbus_message_new_method_call(dbus_get_bus(),
    305 					SYSTEMD_RESOLVED_SERVICE,
    306 					SYSTEMD_RESOLVED_MANAGER_PATH,
    307 					SYSTEMD_RESOLVED_MANAGER_INTERFACE,
    308 					"RevertLink");
    309 	if (!message)
    310 		return;
    311 
    312 	l_dbus_message_set_arguments(message, "i", resolve->ifindex);
    313 	l_dbus_send_with_reply(dbus_get_bus(), message,
    314 				systemd_link_generic_reply, "DNS", NULL);
    315 }
    316 
    317 static void resolve_systemd_destroy(struct resolve *resolve)
    318 {
    319 	struct systemd *sd = l_container_of(resolve, struct systemd, super);
    320 
    321 	l_free(sd);
    322 }
    323 
    324 static const struct resolve_ops systemd_ops = {
    325 	.set_dns = resolve_systemd_set_dns,
    326 	.set_domains = resolve_systemd_set_domains,
    327 	.set_mdns = resolve_systemd_set_mdns,
    328 	.revert = resolve_systemd_revert,
    329 	.destroy = resolve_systemd_destroy,
    330 };
    331 
    332 static void systemd_appeared(struct l_dbus *dbus, void *user_data)
    333 {
    334 	systemd_state.is_ready = true;
    335 }
    336 
    337 static void systemd_disappeared(struct l_dbus *dbus, void *user_data)
    338 {
    339 	systemd_state.is_ready = false;
    340 }
    341 
    342 static int resolve_systemd_init(void)
    343 {
    344 	systemd_state.service_watch =
    345 				l_dbus_add_service_watch(dbus_get_bus(),
    346 						SYSTEMD_RESOLVED_SERVICE,
    347 						systemd_appeared,
    348 						systemd_disappeared,
    349 						NULL, NULL);
    350 
    351 	return 0;
    352 }
    353 
    354 static void resolve_systemd_exit(void)
    355 {
    356 	l_dbus_remove_watch(dbus_get_bus(), systemd_state.service_watch);
    357 	memset(&systemd_state, 0, sizeof(systemd_state));
    358 }
    359 
    360 static struct resolve *resolve_systemd_alloc(uint32_t ifindex)
    361 {
    362 	struct systemd *sd = l_new(struct systemd, 1);
    363 
    364 	_resolve_init(&sd->super, ifindex, &systemd_ops);
    365 
    366 	return &sd->super;
    367 }
    368 
    369 static const struct resolve_method_ops resolve_method_systemd_ops = {
    370 	.init = resolve_systemd_init,
    371 	.exit = resolve_systemd_exit,
    372 	.alloc = resolve_systemd_alloc,
    373 };
    374 
    375 char *resolvconf_path;
    376 
    377 static bool resolvconf_invoke(const char *ifname, const char *type,
    378 						const char *content)
    379 {
    380 	FILE *resolvconf;
    381 	L_AUTO_FREE_VAR(char *, cmd) = NULL;
    382 	int error;
    383 
    384 	if (content) {
    385 		cmd = l_strdup_printf("%s -a %s.%s", resolvconf_path,
    386 						ifname, type);
    387 		resolvconf = popen(cmd, "w");
    388 	} else {
    389 		cmd = l_strdup_printf("%s -d %s.%s", resolvconf_path,
    390 						ifname, type);
    391 		resolvconf = popen(cmd, "r");
    392 	}
    393 
    394 	if (!resolvconf) {
    395 		l_error("resolve: Failed to start %s (%s).", resolvconf_path,
    396 							strerror(errno));
    397 		return false;
    398 	}
    399 
    400 	if (content && fprintf(resolvconf, "%s", content) < 0)
    401 		l_error("resolve: Failed to print into %s stdin.",
    402 							resolvconf_path);
    403 
    404 	error = pclose(resolvconf);
    405 	if (error < 0)
    406 		l_error("resolve: Failed to close pipe to %s (%s).",
    407 					resolvconf_path, strerror(errno));
    408 	else if (error > 0)
    409 		l_info("resolve: %s exited with status (%d).", resolvconf_path,
    410 									error);
    411 
    412 	return !error;
    413 }
    414 
    415 struct resolvconf {
    416 	struct resolve super;
    417 	bool have_domain : 1;
    418 	bool have_dns : 1;
    419 	char *ifname;
    420 };
    421 
    422 static void resolve_resolvconf_set_dns(struct resolve *resolve, char **dns_list)
    423 {
    424 	struct resolvconf *rc =
    425 			l_container_of(resolve, struct resolvconf, super);
    426 	struct l_string *content;
    427 	L_AUTO_FREE_VAR(char *, str) = NULL;
    428 
    429 	if (L_WARN_ON(!resolvconf_path))
    430 		return;
    431 
    432 	if (!dns_list || !dns_list[0]) {
    433 		if (rc->have_dns)
    434 			resolvconf_invoke(rc->ifname, "dns", NULL);
    435 
    436 		rc->have_dns = false;
    437 		return;
    438 	}
    439 
    440 	content = l_string_new(0);
    441 
    442 	for (; *dns_list; dns_list++)
    443 		l_string_append_printf(content, "nameserver %s\n", *dns_list);
    444 
    445 	str = l_string_unwrap(content);
    446 
    447 	if (resolvconf_invoke(rc->ifname, "dns", str))
    448 		rc->have_dns = true;
    449 }
    450 
    451 static void resolve_resolvconf_set_domains(struct resolve *resolve,
    452 							char **domain_list)
    453 {
    454 	struct resolvconf *rc =
    455 			l_container_of(resolve, struct resolvconf, super);
    456 	struct l_string *content;
    457 	L_AUTO_FREE_VAR(char *, str) = NULL;
    458 
    459 	if (L_WARN_ON(!resolvconf_path))
    460 		return;
    461 
    462 	if (!domain_list || !domain_list[0]) {
    463 		if (rc->have_domain)
    464 			resolvconf_invoke(rc->ifname, "domain", NULL);
    465 
    466 		rc->have_domain = false;
    467 		return;
    468 	}
    469 
    470 	content = l_string_new(0);
    471 
    472 	for (; *domain_list; domain_list++)
    473 		l_string_append_printf(content, "search %s\n", *domain_list);
    474 
    475 	str = l_string_unwrap(content);
    476 
    477 	if (resolvconf_invoke(rc->ifname, "domain", str))
    478 		rc->have_domain = true;
    479 }
    480 
    481 static void resolve_resolvconf_revert(struct resolve *resolve)
    482 {
    483 	struct resolvconf *rc =
    484 			l_container_of(resolve, struct resolvconf, super);
    485 
    486 	if (rc->have_dns)
    487 		resolvconf_invoke(rc->ifname, "dns", NULL);
    488 
    489 	if (rc->have_domain)
    490 		resolvconf_invoke(rc->ifname, "domain", NULL);
    491 
    492 	rc->have_dns = false;
    493 	rc->have_domain = false;
    494 }
    495 
    496 static void resolve_resolvconf_destroy(struct resolve *resolve)
    497 {
    498 	struct resolvconf *rc =
    499 			l_container_of(resolve, struct resolvconf, super);
    500 
    501 	l_free(rc->ifname);
    502 	l_free(rc);
    503 }
    504 
    505 static struct resolve_ops resolvconf_ops = {
    506 	.set_dns = resolve_resolvconf_set_dns,
    507 	.set_domains = resolve_resolvconf_set_domains,
    508 	.revert = resolve_resolvconf_revert,
    509 	.destroy = resolve_resolvconf_destroy,
    510 };
    511 
    512 static int resolve_resolvconf_init(void)
    513 {
    514 	static const char *default_path = "/sbin:/usr/sbin";
    515 	const char *path;
    516 
    517 	l_debug("Trying to find resolvconf in $PATH");
    518 
    519 	path = getenv("PATH");
    520 	if (path)
    521 		resolvconf_path = l_path_find("resolvconf", path, X_OK);
    522 
    523 	if (!resolvconf_path) {
    524 		l_debug("Trying to find resolvconf in default paths");
    525 		resolvconf_path = l_path_find("resolvconf", default_path, X_OK);
    526 	}
    527 
    528 	if (!resolvconf_path) {
    529 		l_error("No usable resolvconf found on system");
    530 		/* return -ENOENT; */
    531 		return 0;
    532 	}
    533 
    534 	l_debug("resolvconf found as: %s", resolvconf_path);
    535 	return 0;
    536 }
    537 
    538 static void resolve_resolvconf_exit(void)
    539 {
    540 	l_free(resolvconf_path);
    541 	resolvconf_path = NULL;
    542 }
    543 
    544 static struct resolve *resolve_resolvconf_alloc(uint32_t ifindex)
    545 {
    546 	struct resolvconf *rc = l_new(struct resolvconf, 1);
    547 
    548 	_resolve_init(&rc->super, ifindex, &resolvconf_ops);
    549 
    550 	rc->ifname = l_net_get_name(ifindex);
    551 	if (!rc->ifname)
    552 		rc->ifname = l_strdup_printf("%u", ifindex);
    553 
    554 	return &rc->super;
    555 }
    556 
    557 static const struct resolve_method_ops resolve_method_resolvconf_ops = {
    558 	.init = resolve_resolvconf_init,
    559 	.exit = resolve_resolvconf_exit,
    560 	.alloc = resolve_resolvconf_alloc,
    561 };
    562 
    563 static void resolve_none_destroy(struct resolve *resolve)
    564 {
    565 	l_free(resolve);
    566 }
    567 
    568 static struct resolve_ops none_ops = {
    569 	.destroy = resolve_none_destroy,
    570 };
    571 
    572 static int resolve_none_init(void)
    573 {
    574 	return 0;
    575 }
    576 
    577 static void resolve_none_exit(void)
    578 {
    579 }
    580 
    581 static struct resolve *resolve_none_alloc(uint32_t ifindex)
    582 {
    583 	struct resolve *r = l_new(struct resolve, 1);
    584 
    585 	_resolve_init(r, ifindex, &none_ops);
    586 	return r;
    587 }
    588 
    589 static const struct resolve_method_ops resolve_method_none_ops = {
    590 	.init = resolve_none_init,
    591 	.exit = resolve_none_exit,
    592 	.alloc = resolve_none_alloc,
    593 };
    594 
    595 static const struct resolve_method_ops *configured_method;
    596 
    597 struct resolve *resolve_new(uint32_t ifindex)
    598 {
    599 	if (L_WARN_ON(!configured_method))
    600 		return NULL;
    601 
    602 	return configured_method->alloc(ifindex);
    603 }
    604 
    605 static const struct {
    606 	const char *name;
    607 	const struct resolve_method_ops *method_ops;
    608 } resolve_method_ops_list[] = {
    609 #ifdef HAVE_DBUS
    610 	{ "systemd", &resolve_method_systemd_ops },
    611 #endif
    612 	{ "resolvconf", &resolve_method_resolvconf_ops },
    613 	{ "none", &resolve_method_none_ops },
    614 	{ }
    615 };
    616 
    617 static int resolve_init(void)
    618 {
    619 	const char *method_name;
    620 	uint8_t i;
    621 
    622 	if (!netconfig_enabled())
    623 		return 0;
    624 
    625 	method_name = l_settings_get_value(iwd_get_config(), "Network",
    626 						"NameResolvingService");
    627 	if (!method_name)
    628 		method_name = "resolvconf";
    629 
    630 	for (i = 0; resolve_method_ops_list[i].name; i++) {
    631 		if (strcmp(resolve_method_ops_list[i].name, method_name))
    632 			continue;
    633 
    634 		configured_method = resolve_method_ops_list[i].method_ops;
    635 		break;
    636 	}
    637 
    638 	if (!configured_method) {
    639 		l_error("Unknown resolution method: %s", method_name);
    640 		return -EINVAL;
    641 	}
    642 
    643 	return configured_method->init();
    644 }
    645 
    646 static void resolve_exit(void)
    647 {
    648 	if (!configured_method)
    649 		return;
    650 
    651 	configured_method->exit();
    652 	configured_method = NULL;
    653 }
    654 
    655 IWD_MODULE(resolve, resolve_init, resolve_exit)