eiwd

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

dpp-pkex.c (7460B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2023  Locus Robotics. 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 #include <stdio.h>
     28 #include <linux/limits.h>
     29 
     30 #include <ell/ell.h>
     31 
     32 #include "client/command.h"
     33 #include "client/dbus-proxy.h"
     34 #include "client/device.h"
     35 #include "client/display.h"
     36 #include "src/util.h"
     37 
     38 struct pkex {
     39 	bool started;
     40 	char *role;
     41 };
     42 
     43 static void *pkex_create(void)
     44 {
     45 	return l_new(struct pkex, 1);
     46 }
     47 
     48 static void pkex_destroy(void *data)
     49 {
     50 	struct pkex *pkex = data;
     51 
     52 	l_free(pkex->role);
     53 	l_free(pkex);
     54 }
     55 
     56 static void display_pkex_inline(const char *margin, const void *data)
     57 {
     58 	const struct proxy_interface *pkex_i = data;
     59 	struct proxy_interface *device_i =
     60 		proxy_interface_find(IWD_DEVICE_INTERFACE,
     61 					proxy_interface_get_path(pkex_i));
     62 	const char *identity;
     63 
     64 	if (!device_i)
     65 		return;
     66 
     67 	identity = proxy_interface_get_identity_str(device_i);
     68 	if (!identity)
     69 		return;
     70 
     71 	display("%s%-*s\n", margin, 20, identity);
     72 }
     73 
     74 static void check_errors_method_callback(struct l_dbus_message *message,
     75 								void *user_data)
     76 {
     77 	dbus_message_has_error(message);
     78 }
     79 
     80 static enum cmd_status cmd_list(const char *device_name, char **argv, int argc)
     81 {
     82 	const struct l_queue_entry *entry;
     83 	struct l_queue *match =
     84 		proxy_interface_find_all(IWD_DPP_PKEX_INTERFACE, NULL, NULL);
     85 
     86 	display_table_header("DPP-PKEX-capable Devices",
     87 				MARGIN "%-*s", 20, "Name");
     88 
     89 	if (!match) {
     90 		display("No DPP-PKEX-capable devices available\n");
     91 		display_table_footer();
     92 
     93 		return CMD_STATUS_DONE;
     94 	}
     95 
     96 	for (entry = l_queue_get_entries(match); entry; entry = entry->next) {
     97 		const struct proxy_interface *pkex = entry->data;
     98 		display_pkex_inline(MARGIN, pkex);
     99 	}
    100 
    101 	display_table_footer();
    102 
    103 	l_queue_destroy(match, NULL);
    104 
    105 	return CMD_STATUS_DONE;
    106 }
    107 
    108 static enum cmd_status cmd_enroll_or_configure(const char *device_name,
    109 						char **argv, int argc,
    110 						const char *method)
    111 {
    112 	const struct proxy_interface *pkex;
    113 	const char *code;
    114 	const char *id = NULL;
    115 
    116 	pkex = device_proxy_find(device_name, IWD_DPP_PKEX_INTERFACE);
    117 	if (!pkex) {
    118 		display("No pkex pkex on device: '%s'\n", device_name);
    119 		return CMD_STATUS_INVALID_VALUE;
    120 	}
    121 
    122 	if (argc < 1)
    123 		return CMD_STATUS_INVALID_ARGS;
    124 
    125 	code = argv[0];
    126 
    127 	if (argc > 1)
    128 		id = argv[1];
    129 
    130 	if (id)
    131 		proxy_interface_method_call(pkex, method, "a{sv}",
    132 					check_errors_method_callback, 2,
    133 					"Code", "s", code,
    134 					"Identifier", "s", id);
    135 	else
    136 		proxy_interface_method_call(pkex, method, "a{sv}",
    137 					check_errors_method_callback, 1,
    138 					"Code", "s", code);
    139 
    140 	return CMD_STATUS_TRIGGERED;
    141 }
    142 
    143 static enum cmd_status cmd_enroll(const char *device_name,
    144 					char **argv, int argc)
    145 {
    146 	return cmd_enroll_or_configure(device_name, argv, argc,
    147 					"StartEnrollee");
    148 }
    149 
    150 static enum cmd_status cmd_configure(const char *device_name,
    151 					char **argv, int argc)
    152 {
    153 	return cmd_enroll_or_configure(device_name, argv, argc,
    154 					"ConfigureEnrollee");
    155 }
    156 
    157 static enum cmd_status cmd_stop(const char *device_name, char **argv, int argc)
    158 {
    159 	const struct proxy_interface *pkex_i =
    160 		device_proxy_find(device_name, IWD_DPP_PKEX_INTERFACE);
    161 
    162 	if (!pkex_i) {
    163 		display("No pkex on device: '%s'\n", device_name);
    164 		return CMD_STATUS_INVALID_VALUE;
    165 	}
    166 
    167 	proxy_interface_method_call(pkex_i, "Stop", "",
    168 						check_errors_method_callback);
    169 
    170 	return CMD_STATUS_TRIGGERED;
    171 }
    172 
    173 static enum cmd_status cmd_show(const char *device_name,
    174 						char **argv, int argc)
    175 {
    176 	const struct proxy_interface *proxy =
    177 			device_proxy_find(device_name, IWD_DPP_PKEX_INTERFACE);
    178 	char *caption = l_strdup_printf("%s: %s", "DPP-PKEX", device_name);
    179 
    180 	if (!proxy) {
    181 		display("No DPP-PKEX interface on device: '%s'\n", device_name);
    182 		return CMD_STATUS_INVALID_VALUE;
    183 	}
    184 
    185 	proxy_properties_display(proxy, caption, MARGIN, 20, 47);
    186 	l_free(caption);
    187 
    188 	display_table_footer();
    189 
    190 	return CMD_STATUS_DONE;
    191 }
    192 
    193 static const struct command pkex_commands[] = {
    194 	{ NULL, "list", NULL, cmd_list,
    195 				"List shared code capable devices", true },
    196 	{ "<wlan>", "stop", NULL, cmd_stop, "Aborts shared code operations" },
    197 	{ "<wlan>", "show", NULL, cmd_show,
    198 				"Shows the shared code state", true },
    199 	{ "<wlan>", "enroll", "key [identifier]",
    200 				cmd_enroll, "Start a shared code enrollee"},
    201 	{ "<wlan>", "configure", "key [identifier]",
    202 				cmd_configure,
    203 				"Start a shared code configurator"},
    204 	{ }
    205 };
    206 
    207 static char *family_arg_completion(const char *text, int state)
    208 {
    209 	return device_arg_completion(text, state, pkex_commands,
    210 						IWD_DPP_PKEX_INTERFACE);
    211 }
    212 
    213 static char *entity_arg_completion(const char *text, int state)
    214 {
    215 	return command_entity_arg_completion(text, state, pkex_commands);
    216 }
    217 
    218 static void update_started(void *data, struct l_dbus_message_iter *variant)
    219 {
    220 	struct pkex *pkex = data;
    221 	bool value;
    222 
    223 	if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
    224 		pkex->started = false;
    225 		return;
    226 	}
    227 
    228 	pkex->started = value;
    229 }
    230 
    231 static const char *started_tostr(const void *data)
    232 {
    233 	const struct pkex *pkex = data;
    234 
    235 	return pkex->started ? "yes" : "no";
    236 }
    237 
    238 static void update_role(void *data, struct l_dbus_message_iter *variant)
    239 {
    240 	struct pkex *pkex = data;
    241 	const char *value;
    242 
    243 	if (pkex->role)
    244 		l_free(pkex->role);
    245 
    246 	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
    247 		pkex->role = NULL;
    248 		return;
    249 	}
    250 
    251 	pkex->role = l_strdup(value);
    252 }
    253 
    254 static const char *role_tostr(const void *data)
    255 {
    256 	const struct pkex *pkex = data;
    257 
    258 	return pkex->role;
    259 }
    260 
    261 static const struct proxy_interface_property pkex_properties[] = {
    262 	{ "Started",	"b", update_started,	started_tostr },
    263 	{ "Role",	"s", update_role, 	role_tostr },
    264 	{ }
    265 };
    266 
    267 static const struct proxy_interface_type_ops pkex_ops = {
    268 	.create = pkex_create,
    269 	.destroy = pkex_destroy,
    270 };
    271 
    272 static struct proxy_interface_type pkex_interface_type = {
    273 	.interface = IWD_DPP_PKEX_INTERFACE,
    274 	.properties = pkex_properties,
    275 	.ops = &pkex_ops,
    276 };
    277 
    278 
    279 static struct command_family pkex_command_family = {
    280 	.caption = "Shared Code Device Provisioning (PKEX)",
    281 	.name = "pkex",
    282 	.command_list = pkex_commands,
    283 	.family_arg_completion = family_arg_completion,
    284 	.entity_arg_completion = entity_arg_completion,
    285 };
    286 
    287 static int pkex_command_family_init(void)
    288 {
    289 	command_family_register(&pkex_command_family);
    290 
    291 	return 0;
    292 }
    293 
    294 static void pkex_command_family_exit(void)
    295 {
    296 	command_family_unregister(&pkex_command_family);
    297 }
    298 
    299 COMMAND_FAMILY(pkex_command_family, pkex_command_family_init,
    300 						pkex_command_family_exit)
    301 
    302 static int pkex_interface_init(void)
    303 {
    304 	proxy_interface_type_register(&pkex_interface_type);
    305 
    306 	return 0;
    307 }
    308 
    309 static void pkex_interface_exit(void)
    310 {
    311 	proxy_interface_type_unregister(&pkex_interface_type);
    312 }
    313 
    314 INTERFACE_TYPE(pkex_interface_type, pkex_interface_init, pkex_interface_exit)