eiwd

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

dpp.c (8121B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2021  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 <stdio.h>
     29 #include <linux/limits.h>
     30 
     31 #include <ell/ell.h>
     32 
     33 #include "client/command.h"
     34 #include "client/dbus-proxy.h"
     35 #include "client/device.h"
     36 #include "client/display.h"
     37 
     38 struct dpp {
     39 	bool started;
     40 	char *role;
     41 	char *uri;
     42 };
     43 
     44 static void *dpp_create(void)
     45 {
     46 	return l_new(struct dpp, 1);
     47 }
     48 
     49 static void dpp_destroy(void *data)
     50 {
     51 	struct dpp *dpp = data;
     52 
     53 	if (dpp->role)
     54 		l_free(dpp->role);
     55 	if (dpp->uri)
     56 		l_free(dpp->uri);
     57 
     58 	l_free(dpp);
     59 }
     60 
     61 static void update_started(void *data, struct l_dbus_message_iter *variant)
     62 {
     63 	struct dpp *dpp = data;
     64 	bool value;
     65 
     66 	if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
     67 		dpp->started = false;
     68 		return;
     69 	}
     70 
     71 	dpp->started = value;
     72 }
     73 
     74 static const char *started_tostr(const void *data)
     75 {
     76 	const struct dpp *dpp = data;
     77 
     78 	return dpp->started ? "yes" : "no";
     79 }
     80 
     81 static void update_role(void *data, struct l_dbus_message_iter *variant)
     82 {
     83 	struct dpp *dpp = data;
     84 	const char *value;
     85 
     86 	if (dpp->role)
     87 		l_free(dpp->role);
     88 
     89 	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
     90 		dpp->role = NULL;
     91 		return;
     92 	}
     93 
     94 	dpp->role = l_strdup(value);
     95 }
     96 
     97 static const char *role_tostr(const void *data)
     98 {
     99 	const struct dpp *dpp = data;
    100 
    101 	return dpp->role;
    102 }
    103 
    104 static void update_uri(void *data, struct l_dbus_message_iter *variant)
    105 {
    106 	struct dpp *dpp = data;
    107 	const char *value;
    108 
    109 	if (dpp->uri)
    110 		l_free(dpp->uri);
    111 
    112 	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
    113 		dpp->uri = NULL;
    114 		return;
    115 	}
    116 
    117 	dpp->uri = l_strdup(value);
    118 }
    119 
    120 static const char *uri_tostr(const void *data)
    121 {
    122 	const struct dpp *dpp = data;
    123 
    124 	return dpp->uri;
    125 }
    126 
    127 static const struct proxy_interface_property dpp_properties[] = {
    128 	{ "Started",	"b", update_started,	started_tostr },
    129 	{ "Role",	"s", update_role, 	role_tostr },
    130 	{ "URI",	"s", update_uri,	uri_tostr },
    131 	{ }
    132 };
    133 
    134 static const struct proxy_interface_type_ops dpp_ops = {
    135 	.create = dpp_create,
    136 	.destroy = dpp_destroy,
    137 };
    138 
    139 static struct proxy_interface_type dpp_interface_type = {
    140 	.interface = IWD_DPP_INTERFACE,
    141 	.properties = dpp_properties,
    142 	.ops = &dpp_ops,
    143 };
    144 
    145 static void check_errors_method_callback(struct l_dbus_message *message,
    146 								void *user_data)
    147 {
    148 	dbus_message_has_error(message);
    149 }
    150 
    151 static void get_uri_method_callback(struct l_dbus_message *message,
    152 								void *user_data)
    153 {
    154 	FILE *f;
    155 	L_AUTO_FREE_VAR(char *, cmd) = NULL;
    156 	char *uri;
    157 	char readbuf[PATH_MAX];
    158 	char *s = readbuf;
    159 
    160 	if (dbus_message_has_error(message))
    161 		return;
    162 
    163 	if (!l_dbus_message_get_arguments(message, "s", &uri)) {
    164 		display("Error getting URI");
    165 		return;
    166 	}
    167 
    168 	display("%s\n\n", uri);
    169 
    170 	if (system("which qrencode > /dev/null 2>&1"))
    171 		return;
    172 
    173 	cmd = l_strdup_printf("qrencode -t UTF8 -o - \"%s\"", uri);
    174 
    175 	f = popen(cmd, "r");
    176 
    177 	while ((s = fgets(s, PATH_MAX, f)))
    178 		display("%s", s);
    179 
    180 	display("\n");
    181 
    182 	pclose(f);
    183 }
    184 
    185 static void display_dpp_inline(const char *margin, const void *data)
    186 {
    187 	const struct proxy_interface *dpp_i = data;
    188 	struct proxy_interface *device_i =
    189 		proxy_interface_find(IWD_DEVICE_INTERFACE,
    190 					proxy_interface_get_path(dpp_i));
    191 	const char *identity;
    192 
    193 	if (!device_i)
    194 		return;
    195 
    196 	identity = proxy_interface_get_identity_str(device_i);
    197 	if (!identity)
    198 		return;
    199 
    200 	display("%s%-*s\n", margin, 20, identity);
    201 }
    202 
    203 static enum cmd_status cmd_list(const char *device_name, char **argv, int argc)
    204 {
    205 	const struct l_queue_entry *entry;
    206 	struct l_queue *match =
    207 		proxy_interface_find_all(IWD_DPP_INTERFACE, NULL, NULL);
    208 
    209 	display_table_header("DPP-capable Devices", MARGIN "%-*s", 20, "Name");
    210 
    211 	if (!match) {
    212 		display("No DPP-capable devices available\n");
    213 		display_table_footer();
    214 
    215 		return CMD_STATUS_DONE;
    216 	}
    217 
    218 	for (entry = l_queue_get_entries(match); entry; entry = entry->next) {
    219 		const struct proxy_interface *dpp = entry->data;
    220 		display_dpp_inline(MARGIN, dpp);
    221 	}
    222 
    223 	display_table_footer();
    224 
    225 	l_queue_destroy(match, NULL);
    226 
    227 	return CMD_STATUS_DONE;
    228 }
    229 
    230 static enum cmd_status cmd_start_enrollee(const char *device_name,
    231 							char **argv, int argc)
    232 {
    233 	const struct proxy_interface *dpp_i;
    234 
    235 	dpp_i = device_proxy_find(device_name, IWD_DPP_INTERFACE);
    236 	if (!dpp_i) {
    237 		display("No dpp on device: '%s'\n", device_name);
    238 		return CMD_STATUS_INVALID_VALUE;
    239 	}
    240 
    241 	proxy_interface_method_call(dpp_i, "StartEnrollee", "",
    242 					get_uri_method_callback);
    243 
    244 	return CMD_STATUS_TRIGGERED;
    245 }
    246 
    247 static enum cmd_status cmd_start_configurator(const char *device_name,
    248 							char **argv, int argc)
    249 {
    250 	const struct proxy_interface *dpp_i;
    251 
    252 	dpp_i = device_proxy_find(device_name, IWD_DPP_INTERFACE);
    253 	if (!dpp_i) {
    254 		display("No dpp on device: '%s'\n", device_name);
    255 		return CMD_STATUS_INVALID_VALUE;
    256 	}
    257 
    258 	proxy_interface_method_call(dpp_i, "StartConfigurator", "",
    259 					get_uri_method_callback);
    260 
    261 	return CMD_STATUS_TRIGGERED;
    262 }
    263 
    264 static enum cmd_status cmd_stop(const char *device_name,
    265 						char **argv, int argc)
    266 {
    267 	const struct proxy_interface *dpp_i =
    268 		device_proxy_find(device_name, IWD_DPP_INTERFACE);
    269 
    270 	if (!dpp_i) {
    271 		display("No dpp on device: '%s'\n", device_name);
    272 		return CMD_STATUS_INVALID_VALUE;
    273 	}
    274 
    275 	proxy_interface_method_call(dpp_i, "Stop", "",
    276 						check_errors_method_callback);
    277 
    278 	return CMD_STATUS_TRIGGERED;
    279 }
    280 
    281 static enum cmd_status cmd_show(const char *device_name,
    282 						char **argv, int argc)
    283 {
    284 	const struct proxy_interface *proxy =
    285 			device_proxy_find(device_name, IWD_DPP_INTERFACE);
    286 	char *caption = l_strdup_printf("%s: %s", "DPP", device_name);
    287 
    288 	if (!proxy) {
    289 		display("No DPP interface on device: '%s'\n", device_name);
    290 		return CMD_STATUS_INVALID_VALUE;
    291 	}
    292 
    293 	proxy_properties_display(proxy, caption, MARGIN, 20, 47);
    294 	l_free(caption);
    295 
    296 	display_table_footer();
    297 
    298 	return CMD_STATUS_DONE;
    299 }
    300 
    301 static const struct command dpp_commands[] = {
    302 	{ NULL, "list", NULL, cmd_list, "List DPP-capable devices", true },
    303 	{ "<wlan>", "start-enrollee", NULL, cmd_start_enrollee,
    304 							"Starts a DPP Enrollee" },
    305 	{ "<wlan>", "start-configurator", NULL, cmd_start_configurator,
    306 							"Starts a DPP Configurator" },
    307 	{ "<wlan>", "stop", NULL, cmd_stop, "Aborts DPP operations" },
    308 	{ "<wlan>", "show", NULL, cmd_show, "Shows the DPP state", true },
    309 	{ }
    310 };
    311 
    312 static char *family_arg_completion(const char *text, int state)
    313 {
    314 	return device_arg_completion(text, state, dpp_commands,
    315 						IWD_DPP_INTERFACE);
    316 }
    317 
    318 static char *entity_arg_completion(const char *text, int state)
    319 {
    320 	return command_entity_arg_completion(text, state, dpp_commands);
    321 }
    322 
    323 static struct command_family dpp_command_family = {
    324 	.caption = "Device Provisioning",
    325 	.name = "dpp",
    326 	.command_list = dpp_commands,
    327 	.family_arg_completion = family_arg_completion,
    328 	.entity_arg_completion = entity_arg_completion,
    329 };
    330 
    331 static int dpp_command_family_init(void)
    332 {
    333 	command_family_register(&dpp_command_family);
    334 
    335 	return 0;
    336 }
    337 
    338 static void dpp_command_family_exit(void)
    339 {
    340 	command_family_unregister(&dpp_command_family);
    341 }
    342 
    343 COMMAND_FAMILY(dpp_command_family, dpp_command_family_init,
    344 						dpp_command_family_exit)
    345 
    346 static int dpp_interface_init(void)
    347 {
    348 	proxy_interface_type_register(&dpp_interface_type);
    349 
    350 	return 0;
    351 }
    352 
    353 static void dpp_interface_exit(void)
    354 {
    355 	proxy_interface_type_unregister(&dpp_interface_type);
    356 }
    357 
    358 INTERFACE_TYPE(dpp_interface_type, dpp_interface_init, dpp_interface_exit)