known-networks.c (11524B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2017-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 _XOPEN_SOURCE 28 #include <time.h> 29 #include <ell/ell.h> 30 31 #include "client/command.h" 32 #include "client/dbus-proxy.h" 33 #include "client/display.h" 34 #include "client/properties.h" 35 #include "client/network.h" 36 37 struct known_network { 38 char *identity; 39 char *name; 40 char *type; 41 char *last_connected; 42 bool hidden; 43 bool autoconnect; 44 }; 45 46 static const char *format_iso8601(const char *time_str, const char *format) 47 { 48 struct tm tm; 49 time_t time; 50 static char buf[255]; 51 52 if (!time_str) 53 return NULL; 54 55 memset(&tm, 0, sizeof(struct tm)); 56 57 strptime(time_str, "%FT%TZ", &tm); 58 59 time = mktime(&tm); 60 61 strftime(buf, sizeof(buf), format, localtime(&time)); 62 63 return buf; 64 } 65 66 static void update_name(void *data, struct l_dbus_message_iter *variant) 67 { 68 struct known_network *network = data; 69 const char *value; 70 71 l_free(network->name); 72 73 if (!l_dbus_message_iter_get_variant(variant, "s", &value)) { 74 network->name = NULL; 75 76 return; 77 } 78 79 network->name = l_strdup(value); 80 } 81 82 static const char *get_name(const void *data) 83 { 84 const struct known_network *network = data; 85 86 return network->name; 87 } 88 89 static void update_type(void *data, struct l_dbus_message_iter *variant) 90 { 91 struct known_network *network = data; 92 const char *value; 93 94 l_free(network->type); 95 96 if (!l_dbus_message_iter_get_variant(variant, "s", &value)) { 97 network->type = NULL; 98 99 return; 100 } 101 102 network->type = l_strdup(value); 103 } 104 105 static void update_last_connected(void *data, struct l_dbus_message_iter *variant) 106 { 107 struct known_network *network = data; 108 const char *value; 109 110 l_free(network->last_connected); 111 112 if (!l_dbus_message_iter_get_variant(variant, "s", &value)) { 113 network->last_connected = NULL; 114 115 return; 116 } 117 118 network->last_connected = l_strdup(value); 119 } 120 121 static void update_hidden(void *data, struct l_dbus_message_iter *variant) 122 { 123 struct known_network *network = data; 124 bool value; 125 126 if (!l_dbus_message_iter_get_variant(variant, "b", &value)) { 127 network->hidden = false; 128 129 return; 130 } 131 132 network->hidden = value; 133 } 134 135 static const char *get_hidden_tostr(const void *data) 136 { 137 const struct known_network *network = data; 138 139 return network->hidden ? "yes" : ""; 140 } 141 142 static void update_autoconnect(void *data, struct l_dbus_message_iter *variant) 143 { 144 struct known_network *network = data; 145 bool value; 146 147 if (!l_dbus_message_iter_get_variant(variant, "b", &value)) { 148 network->autoconnect = false; 149 150 return; 151 } 152 153 network->autoconnect = value; 154 } 155 156 static const char *get_autoconnect_tostr(const void *data) 157 { 158 const struct known_network *network = data; 159 160 return network->autoconnect ? "yes" : "no"; 161 } 162 163 static const struct proxy_interface_property known_network_properties[] = { 164 { "Name", "s", update_name, get_name }, 165 { "Type", "s", update_type }, 166 { "LastConnectedTime", "s", update_last_connected }, 167 { "Hidden", "b", update_hidden, get_hidden_tostr }, 168 { "AutoConnect", "b", update_autoconnect, get_autoconnect_tostr, 169 true, properties_builder_append_yes_no_variant, 170 properties_yes_no_opts }, 171 { }, 172 }; 173 174 static void *known_network_create(void) 175 { 176 return l_new(struct known_network, 1); 177 } 178 179 static void known_network_destroy(void *data) 180 { 181 struct known_network *network = data; 182 183 l_free(network->last_connected); 184 l_free(network->name); 185 l_free(network->type); 186 l_free(network->identity); 187 188 l_free(network); 189 } 190 191 static void known_network_display(const struct proxy_interface *proxy) 192 { 193 const struct known_network *known_network = 194 proxy_interface_get_data(proxy); 195 char *caption = l_strdup_printf("%s: %s", "Known Network", 196 known_network->name); 197 198 proxy_properties_display(proxy, caption, MARGIN, 18, 50); 199 200 l_free(caption); 201 202 display_table_footer(); 203 } 204 205 static void known_network_display_inline(const char *margin, const void *data) 206 { 207 const struct known_network *network = data; 208 char *last_connected = 209 l_strdup(format_iso8601(network->last_connected, 210 "%b %e, %l:%M %p")); 211 212 display_table_row(margin, 4, 32, network->name, 11, network->type, 213 9, get_hidden_tostr(network), 214 19, last_connected ? : "-"); 215 216 l_free(last_connected); 217 } 218 219 static const char *known_network_identity(void *data) 220 { 221 struct known_network *network = data; 222 223 if (!network->identity) 224 network->identity = 225 l_strdup_printf("%s %s", network->name, network->type); 226 227 return network->identity; 228 } 229 230 static const struct proxy_interface_type_ops known_network_ops = { 231 .create = known_network_create, 232 .destroy = known_network_destroy, 233 .display = known_network_display_inline, 234 .identity = known_network_identity, 235 }; 236 237 static struct proxy_interface_type known_network_interface_type = { 238 .interface = IWD_KNOWN_NETWORK_INTREFACE, 239 .properties = known_network_properties, 240 .ops = &known_network_ops, 241 }; 242 243 static bool known_network_match(const void *a, const void *b) 244 { 245 const struct known_network *network = a; 246 const struct network_args *args = b; 247 248 if (strcmp(network->name, args->name)) 249 return false; 250 251 if (args->type && strcmp(network->type, args->type)) 252 return false; 253 254 return true; 255 } 256 257 static void check_errors_method_callback(struct l_dbus_message *message, 258 void *user_data) 259 { 260 dbus_message_has_error(message); 261 } 262 263 static enum cmd_status cmd_list(const char *entity, char **args, int argc) 264 { 265 display_table_header("Known Networks", MARGIN "%-*s %-*s %-*s %-*s", 266 32, "Name", 11, "Security", 9, "Hidden", 267 19, "Last connected"); 268 269 proxy_interface_display_list(known_network_interface_type.interface); 270 271 display_table_footer(); 272 273 return CMD_STATUS_DONE; 274 } 275 276 static const struct proxy_interface *known_network_proxy_find_by_name( 277 const char *name) 278 { 279 struct network_args network_args; 280 struct l_queue *match; 281 const struct proxy_interface *proxy; 282 size_t name_len; 283 char *args_name; 284 285 if (!name) 286 return NULL; 287 288 name_len = strlen(name); 289 290 if (l_str_has_suffix(name, ".psk")) { 291 network_args.type = "psk"; 292 name_len -= 4; 293 } else if (l_str_has_suffix(name, ".8021x")) { 294 network_args.type = "8021x"; 295 name_len -= 6; 296 } else if (l_str_has_suffix(name, ".open")) { 297 network_args.type = "open"; 298 name_len -= 5; 299 } else 300 network_args.type = NULL; 301 302 args_name = l_strndup(name, name_len); 303 network_args.name = args_name; 304 305 match = proxy_interface_find_all(known_network_interface_type.interface, 306 known_network_match, 307 &network_args); 308 309 l_free(args_name); 310 311 if (!match) { 312 display("No network with specified parameters was found\n"); 313 return NULL; 314 } 315 316 if (l_queue_length(match) > 1) { 317 if (!network_args.type) { 318 display("Provided network name is ambiguous. " 319 "Specify network security type as follows:\n"); 320 display("<\"network name" COLOR_BOLDGRAY(".security") 321 "\">\n"); 322 display("\twhere '.security' is [.psk | .8021x | " 323 ".open]\n"); 324 } 325 326 l_queue_destroy(match, NULL); 327 return NULL; 328 } 329 330 proxy = l_queue_pop_head(match); 331 l_queue_destroy(match, NULL); 332 333 return proxy; 334 } 335 336 static enum cmd_status cmd_forget(const char *network_name, char **argv, 337 int argc) 338 { 339 const struct proxy_interface *proxy = 340 known_network_proxy_find_by_name(network_name); 341 342 if (!proxy) 343 return CMD_STATUS_INVALID_ARGS; 344 345 proxy_interface_method_call(proxy, "Forget", "", 346 check_errors_method_callback); 347 348 return CMD_STATUS_TRIGGERED; 349 } 350 351 static enum cmd_status cmd_show(const char *network_name, char **argv, int argc) 352 { 353 const struct proxy_interface *proxy = 354 known_network_proxy_find_by_name(network_name); 355 356 if (!proxy) 357 return CMD_STATUS_INVALID_ARGS; 358 359 known_network_display(proxy); 360 361 return CMD_STATUS_DONE; 362 } 363 364 static void property_set_callback(struct l_dbus_message *message, 365 void *user_data) 366 { 367 dbus_message_has_error(message); 368 } 369 370 static enum cmd_status cmd_set_property(const char *network_name, 371 char **argv, int argc) 372 { 373 const struct proxy_interface *proxy = 374 known_network_proxy_find_by_name(network_name); 375 376 if (!proxy) 377 return CMD_STATUS_INVALID_VALUE; 378 379 if (argc != 2) 380 return CMD_STATUS_INVALID_ARGS; 381 382 if (!proxy_property_set(proxy, argv[0], argv[1], 383 property_set_callback)) 384 return CMD_STATUS_INVALID_VALUE; 385 386 return CMD_STATUS_TRIGGERED; 387 } 388 389 static char *set_property_cmd_arg_completion(const char *text, int state, 390 const char *network_name) 391 { 392 return proxy_property_completion(known_network_properties, text, state); 393 } 394 395 static bool match_by_partial_name(const void *a, const void *b) 396 { 397 const struct known_network *network = a; 398 const char *text = b; 399 400 return !strncmp(network->name, text, strlen(text)); 401 } 402 403 static const struct command known_networks_commands[] = { 404 { NULL, "list", NULL, cmd_list, "List known networks", true }, 405 { "<\"network name\">", "forget", NULL, cmd_forget, 406 "Forget known network" }, 407 { "<\"network name\">", "show", NULL, cmd_show, "Show known network", 408 true }, 409 { "<\"network name\">", "set-property", "<name> <value>", 410 cmd_set_property, "Set property", false, 411 set_property_cmd_arg_completion }, 412 { } 413 }; 414 static char *family_arg_completion(const char *text, int state) 415 { 416 static bool first_pass; 417 static size_t index; 418 static size_t len; 419 const char *cmd; 420 421 if (!state) { 422 index = 0; 423 len = strlen(text); 424 first_pass = true; 425 } 426 427 while ((cmd = known_networks_commands[index].cmd)) { 428 if (known_networks_commands[index++].entity) 429 continue; 430 431 if (!strncmp(cmd, text, len)) 432 return l_strdup(cmd); 433 } 434 435 if (first_pass) { 436 state = 0; 437 first_pass = false; 438 } 439 440 return proxy_property_str_completion(&known_network_interface_type, 441 match_by_partial_name, "Name", 442 text, state, NULL); 443 } 444 445 static char *entity_arg_completion(const char *text, int state) 446 { 447 return command_entity_arg_completion(text, state, 448 known_networks_commands); 449 } 450 451 static struct command_family known_networks_command_family = { 452 .caption = "Known Networks", 453 .name = "known-networks", 454 .command_list = known_networks_commands, 455 .family_arg_completion = family_arg_completion, 456 .entity_arg_completion = entity_arg_completion, 457 }; 458 459 static int known_networks_command_family_init(void) 460 { 461 command_family_register(&known_networks_command_family); 462 463 return 0; 464 } 465 466 static void known_networks_command_family_exit(void) 467 { 468 command_family_unregister(&known_networks_command_family); 469 } 470 471 COMMAND_FAMILY(known_networks_command_family, 472 known_networks_command_family_init, 473 known_networks_command_family_exit) 474 475 static int known_network_interface_init(void) 476 { 477 proxy_interface_type_register(&known_network_interface_type); 478 479 return 0; 480 } 481 482 static void known_network_interface_exit(void) 483 { 484 proxy_interface_type_unregister(&known_network_interface_type); 485 } 486 487 INTERFACE_TYPE(known_network_interface_type, known_network_interface_init, 488 known_network_interface_exit)