agent.c (18385B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2015-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 #include <errno.h> 28 29 #include <ell/ell.h> 30 #include "src/dbus.h" 31 #include "src/netconfig.h" 32 #include "src/agent.h" 33 #include "src/iwd.h" 34 #include "src/module.h" 35 36 static unsigned int next_request_id = 0; 37 38 enum agent_request_type { 39 AGENT_REQUEST_TYPE_PASSPHRASE, 40 AGENT_REQUEST_TYPE_USER_NAME_PASSWD, 41 }; 42 43 /* Agent dbus request is done from iwd towards the agent */ 44 struct agent_request { 45 enum agent_request_type type; 46 struct l_dbus_message *message; 47 unsigned int id; 48 void *user_data; 49 void *user_callback; 50 struct l_dbus_message *trigger; 51 agent_request_destroy_func_t destroy; 52 }; 53 54 struct agent { 55 char *owner; 56 char *path; 57 unsigned int disconnect_watch; 58 uint32_t pending_id; 59 struct l_timeout *timeout; 60 int timeout_secs; 61 struct l_queue *requests; 62 }; 63 64 static struct l_queue *agents; 65 66 /* 67 * How long we wait for user to input things. 68 * Return value is in seconds. 69 * 70 * This should probably be configurable by user via 71 * config file/command line option/env variable. 72 */ 73 #ifdef HAVE_DBUS 74 static unsigned int agent_timeout_input_request(void) 75 { 76 return 120; 77 } 78 79 static void send_cancel_request(void *user_data, int reason) 80 { 81 struct agent *agent = user_data; 82 struct l_dbus_message *message; 83 const char *reasonstr; 84 85 switch (reason) { 86 case -ECANCELED: 87 reasonstr = "user-canceled"; 88 break; 89 case -ETIMEDOUT: 90 reasonstr = "timed-out"; 91 break; 92 case -ERANGE: 93 reasonstr = "out-of-range"; 94 break; 95 case -ESHUTDOWN: 96 reasonstr = "shutdown"; 97 break; 98 default: 99 reasonstr = "unknown"; 100 } 101 102 l_debug("send a Cancel(%s) to %s %s", reasonstr, 103 agent->owner, agent->path); 104 105 message = l_dbus_message_new_method_call(dbus_get_bus(), 106 agent->owner, 107 agent->path, 108 IWD_AGENT_INTERFACE, 109 "Cancel"); 110 111 l_dbus_message_set_arguments(message, "s", reasonstr); 112 l_dbus_message_set_no_reply(message, true); 113 114 l_dbus_send(dbus_get_bus(), message); 115 } 116 #endif 117 118 static void agent_request_free(void *user_data) 119 { 120 struct agent_request *request = user_data; 121 122 #ifdef HAVE_DBUS 123 l_dbus_message_unref(request->message); 124 125 if (request->trigger) 126 dbus_pending_reply(&request->trigger, 127 dbus_error_aborted(request->trigger)); 128 129 if (request->destroy) 130 request->destroy(request->user_data); 131 #endif 132 133 l_free(request); 134 } 135 136 #ifdef HAVE_DBUS 137 static void passphrase_reply(struct l_dbus_message *reply, 138 struct agent_request *request) 139 { 140 const char *error, *text; 141 char *passphrase = NULL; 142 enum agent_result result = AGENT_RESULT_FAILED; 143 agent_request_passphrase_func_t user_callback = request->user_callback; 144 145 if (l_dbus_message_get_error(reply, &error, &text)) 146 goto done; 147 148 if (!l_dbus_message_get_arguments(reply, "s", &passphrase)) 149 goto done; 150 151 result = AGENT_RESULT_OK; 152 153 done: 154 user_callback(result, passphrase, request->trigger, request->user_data); 155 } 156 157 static void user_name_passwd_reply(struct l_dbus_message *reply, 158 struct agent_request *request) 159 { 160 const char *error, *text; 161 char *username = NULL; 162 char *passwd = NULL; 163 enum agent_result result = AGENT_RESULT_FAILED; 164 agent_request_user_name_passwd_func_t user_callback = 165 request->user_callback; 166 167 if (l_dbus_message_get_error(reply, &error, &text)) 168 goto done; 169 170 if (!l_dbus_message_get_arguments(reply, "ss", &username, &passwd)) 171 goto done; 172 173 result = AGENT_RESULT_OK; 174 175 done: 176 user_callback(result, username, passwd, 177 request->trigger, request->user_data); 178 } 179 #endif 180 181 static void agent_finalize_pending(struct agent *agent, 182 struct l_dbus_message *reply) 183 { 184 struct agent_request *pending; 185 186 if (agent->timeout) { 187 l_timeout_remove(agent->timeout); 188 agent->timeout = NULL; 189 } 190 191 #ifdef HAVE_DBUS 192 pending = l_queue_pop_head(agent->requests); 193 194 switch (pending->type) { 195 case AGENT_REQUEST_TYPE_PASSPHRASE: 196 passphrase_reply(reply, pending); 197 break; 198 case AGENT_REQUEST_TYPE_USER_NAME_PASSWD: 199 user_name_passwd_reply(reply, pending); 200 break; 201 } 202 203 if (pending->trigger) { 204 l_dbus_message_unref(pending->trigger); 205 pending->trigger = NULL; 206 } 207 208 agent_request_free(pending); 209 #endif 210 } 211 212 static void agent_free(void *data) 213 { 214 struct agent *agent = data; 215 216 l_debug("agent free %p", agent); 217 218 if (agent->timeout) 219 l_timeout_remove(agent->timeout); 220 221 #ifdef HAVE_DBUS 222 if (agent->pending_id) 223 l_dbus_cancel(dbus_get_bus(), agent->pending_id); 224 225 l_queue_destroy(agent->requests, agent_request_free); 226 #endif 227 228 #ifdef HAVE_DBUS 229 if (agent->disconnect_watch) 230 l_dbus_remove_watch(dbus_get_bus(), agent->disconnect_watch); 231 #endif 232 233 l_free(agent->owner); 234 l_free(agent->path); 235 l_free(agent); 236 } 237 238 static void agent_send_next_request(struct agent *agent); 239 240 static void request_timeout(struct l_timeout *timeout, void *user_data) 241 { 242 struct agent *agent = user_data; 243 244 #ifdef HAVE_DBUS 245 l_dbus_cancel(dbus_get_bus(), agent->pending_id); 246 247 send_cancel_request(agent, -ETIMEDOUT); 248 #endif 249 250 agent_finalize_pending(agent, NULL); 251 252 agent_send_next_request(agent); 253 } 254 255 #ifdef HAVE_DBUS 256 static void agent_receive_reply(struct l_dbus_message *message, 257 void *user_data) 258 { 259 struct agent *agent = user_data; 260 261 l_debug("agent %p request id %u", agent, agent->pending_id); 262 263 agent->pending_id = 0; 264 265 agent_finalize_pending(agent, message); 266 267 if (!agent->pending_id) 268 agent_send_next_request(agent); 269 } 270 #endif 271 272 static void agent_send_next_request(struct agent *agent) 273 { 274 struct agent_request *pending; 275 276 pending = l_queue_peek_head(agent->requests); 277 if (!pending) 278 return; 279 280 agent->timeout = l_timeout_create(agent->timeout_secs, 281 request_timeout, 282 agent, NULL); 283 284 l_debug("send request to %s %s", agent->owner, agent->path); 285 286 #ifdef HAVE_DBUS 287 agent->pending_id = l_dbus_send_with_reply(dbus_get_bus(), 288 pending->message, 289 agent_receive_reply, 290 agent, NULL); 291 #endif 292 293 pending->message = NULL; 294 } 295 296 #ifdef HAVE_DBUS 297 static unsigned int agent_queue_request(struct agent *agent, 298 enum agent_request_type type, 299 struct l_dbus_message *message, 300 int timeout, void *callback, 301 struct l_dbus_message *trigger, 302 void *user_data, 303 agent_request_destroy_func_t destroy) 304 { 305 struct agent_request *request; 306 307 request = l_new(struct agent_request, 1); 308 309 request->type = type; 310 request->message = message; 311 request->id = ++next_request_id; 312 request->user_data = user_data; 313 request->user_callback = callback; 314 request->trigger = l_dbus_message_ref(trigger); 315 request->destroy = destroy; 316 317 agent->timeout_secs = timeout; 318 319 l_queue_push_tail(agent->requests, request); 320 321 if (l_queue_length(agent->requests) == 1) 322 agent_send_next_request(agent); 323 324 return request->id; 325 } 326 327 static struct agent *agent_lookup(const char *owner) 328 { 329 const struct l_queue_entry *entry; 330 331 if (!owner) 332 return NULL; 333 334 for (entry = l_queue_get_entries(agents); entry; entry = entry->next) { 335 struct agent *agent = entry->data; 336 337 if (strcmp(agent->owner, owner)) 338 continue; 339 340 return agent; 341 } 342 343 return NULL; 344 } 345 346 static struct agent *get_agent(const char *owner) 347 { 348 struct agent *agent = agent_lookup(owner); 349 350 if (agent) 351 return agent; 352 353 return l_queue_peek_head(agents); 354 } 355 #endif 356 357 /** 358 * agent_request_passphrase: 359 * @path: object path related to this request (like network object path) 360 * @callback: user callback called when the request is ready 361 * @trigger: Message associated with (e.g. that triggered) this request 362 * @user_data: user defined data 363 * @destroy: callback to release @user_data when this request finishes 364 * 365 * Called when a passphrase information is needed from the user. Returns an 366 * id that can be used to cancel the request. 367 * 368 * If @trigger is not NULL, then a reference is taken automatically. If 369 * agent_cancel_request is called subsequently, a dbus_aborted error is 370 * automatically generated for @trigger. Otherwise, after @callback is 371 * called, the reference to @trigger is dropped. It is assumed that the 372 * caller will take ownership of @trigger in the callback if needed. 373 */ 374 unsigned int agent_request_passphrase(const char *path, 375 agent_request_passphrase_func_t callback, 376 struct l_dbus_message *trigger, 377 void *user_data, 378 agent_request_destroy_func_t destroy) 379 { 380 #ifdef HAVE_DBUS 381 struct agent *agent = get_agent(l_dbus_message_get_sender(trigger)); 382 struct l_dbus_message *message; 383 384 if (!agent || !callback) 385 return 0; 386 387 l_debug("agent %p owner %s path %s", agent, agent->owner, agent->path); 388 389 message = l_dbus_message_new_method_call(dbus_get_bus(), 390 agent->owner, 391 agent->path, 392 IWD_AGENT_INTERFACE, 393 "RequestPassphrase"); 394 395 l_dbus_message_set_arguments(message, "o", path); 396 397 return agent_queue_request(agent, AGENT_REQUEST_TYPE_PASSPHRASE, 398 message, agent_timeout_input_request(), 399 callback, trigger, user_data, destroy); 400 #else 401 return 0; 402 #endif 403 } 404 405 unsigned int agent_request_pkey_passphrase(const char *path, 406 agent_request_passphrase_func_t callback, 407 struct l_dbus_message *trigger, 408 void *user_data, 409 agent_request_destroy_func_t destroy) 410 { 411 #ifdef HAVE_DBUS 412 struct agent *agent = get_agent(l_dbus_message_get_sender(trigger)); 413 struct l_dbus_message *message; 414 415 if (!agent || !callback) 416 return 0; 417 418 l_debug("agent %p owner %s path %s", agent, agent->owner, agent->path); 419 420 message = l_dbus_message_new_method_call(dbus_get_bus(), 421 agent->owner, agent->path, 422 IWD_AGENT_INTERFACE, 423 "RequestPrivateKeyPassphrase"); 424 425 l_dbus_message_set_arguments(message, "o", path); 426 427 return agent_queue_request(agent, AGENT_REQUEST_TYPE_PASSPHRASE, 428 message, agent_timeout_input_request(), 429 callback, trigger, user_data, destroy); 430 #else 431 return 0; 432 #endif 433 } 434 435 unsigned int agent_request_user_name_password(const char *path, 436 agent_request_user_name_passwd_func_t callback, 437 struct l_dbus_message *trigger, 438 void *user_data, 439 agent_request_destroy_func_t destroy) 440 { 441 #ifdef HAVE_DBUS 442 struct agent *agent = get_agent(l_dbus_message_get_sender(trigger)); 443 struct l_dbus_message *message; 444 445 if (!agent || !callback) 446 return 0; 447 448 l_debug("agent %p owner %s path %s", agent, agent->owner, agent->path); 449 450 message = l_dbus_message_new_method_call(dbus_get_bus(), 451 agent->owner, agent->path, 452 IWD_AGENT_INTERFACE, 453 "RequestUserNameAndPassword"); 454 455 l_dbus_message_set_arguments(message, "o", path); 456 457 return agent_queue_request(agent, AGENT_REQUEST_TYPE_USER_NAME_PASSWD, 458 message, agent_timeout_input_request(), 459 callback, trigger, user_data, destroy); 460 #else 461 return 0; 462 #endif 463 } 464 465 unsigned int agent_request_user_password(const char *path, const char *user, 466 agent_request_passphrase_func_t callback, 467 struct l_dbus_message *trigger, void *user_data, 468 agent_request_destroy_func_t destroy) 469 { 470 #ifdef HAVE_DBUS 471 struct agent *agent = get_agent(l_dbus_message_get_sender(trigger)); 472 struct l_dbus_message *message; 473 474 if (!agent || !callback) 475 return 0; 476 477 l_debug("agent %p owner %s path %s", agent, agent->owner, agent->path); 478 479 message = l_dbus_message_new_method_call(dbus_get_bus(), 480 agent->owner, agent->path, 481 IWD_AGENT_INTERFACE, 482 "RequestUserPassword"); 483 484 l_dbus_message_set_arguments(message, "os", path, user ?: ""); 485 486 return agent_queue_request(agent, AGENT_REQUEST_TYPE_PASSPHRASE, 487 message, agent_timeout_input_request(), 488 callback, trigger, user_data, destroy); 489 #else 490 return 0; 491 #endif 492 } 493 494 static bool find_request(const void *a, const void *b) 495 { 496 const struct agent_request *request = a; 497 unsigned int id = L_PTR_TO_UINT(b); 498 499 return request->id == id; 500 } 501 502 bool agent_request_cancel(unsigned int req_id, int reason) 503 { 504 struct agent_request *request = NULL; 505 struct agent *agent; 506 const struct l_queue_entry *entry; 507 508 for (entry = l_queue_get_entries(agents); entry; entry = entry->next) { 509 agent = entry->data; 510 511 request = l_queue_remove_if(agent->requests, find_request, 512 L_UINT_TO_PTR(req_id)); 513 if (request) 514 break; 515 } 516 517 if (!request) 518 return false; 519 520 if (!request->message) { 521 #ifdef HAVE_DBUS 522 send_cancel_request(agent, reason); 523 524 l_dbus_cancel(dbus_get_bus(), agent->pending_id); 525 #endif 526 527 agent->pending_id = 0; 528 529 if (agent->timeout) { 530 l_timeout_remove(agent->timeout); 531 agent->timeout = NULL; 532 } 533 534 agent_send_next_request(agent); 535 } 536 537 agent_request_free(request); 538 539 return true; 540 } 541 542 #ifdef HAVE_DBUS 543 static void agent_disconnect(struct l_dbus *dbus, void *user_data) 544 { 545 struct agent *agent = user_data; 546 547 l_debug("agent %s disconnected", agent->owner); 548 549 if (agent->pending_id) 550 agent_finalize_pending(agent, NULL); 551 552 l_queue_remove(agents, agent); 553 554 l_idle_oneshot(agent_free, agent, NULL); 555 } 556 557 static struct agent *agent_create(struct l_dbus *dbus, const char *name, 558 const char *path) 559 { 560 struct agent *agent; 561 562 agent = l_new(struct agent, 1); 563 564 agent->owner = l_strdup(name); 565 agent->path = l_strdup(path); 566 agent->requests = l_queue_new(); 567 agent->disconnect_watch = l_dbus_add_disconnect_watch(dbus, name, 568 agent_disconnect, 569 agent, NULL); 570 return agent; 571 } 572 573 static struct l_dbus_message *agent_register(struct l_dbus *dbus, 574 struct l_dbus_message *message, 575 void *user_data) 576 { 577 struct agent *agent = agent_lookup(l_dbus_message_get_sender(message)); 578 struct l_dbus_message *reply; 579 const char *path; 580 581 if (agent) 582 return dbus_error_already_exists(message); 583 584 l_debug("agent register called"); 585 586 if (!l_dbus_message_get_arguments(message, "o", &path)) 587 return dbus_error_invalid_args(message); 588 589 agent = agent_create(dbus, l_dbus_message_get_sender(message), path); 590 if (!agent) 591 return dbus_error_failed(message); 592 593 l_queue_push_tail(agents, agent); 594 595 l_debug("agent %s path %s", agent->owner, agent->path); 596 597 reply = l_dbus_message_new_method_return(message); 598 599 l_dbus_message_set_arguments(reply, ""); 600 601 return reply; 602 } 603 604 static struct l_dbus_message *agent_unregister(struct l_dbus *dbus, 605 struct l_dbus_message *message, 606 void *user_data) 607 { 608 struct agent *agent = agent_lookup(l_dbus_message_get_sender(message)); 609 struct l_dbus_message *reply; 610 611 l_debug("agent unregister"); 612 613 if (!agent) 614 return dbus_error_not_found(message); 615 616 l_queue_remove(agents, agent); 617 618 agent_free(agent); 619 620 reply = l_dbus_message_new_method_return(message); 621 622 l_dbus_message_set_arguments(reply, ""); 623 624 return reply; 625 } 626 627 static struct l_dbus_message *netconfig_agent_register(struct l_dbus *dbus, 628 struct l_dbus_message *message, 629 void *user_data) 630 { 631 struct l_dbus_message *reply; 632 const char *path; 633 int r; 634 635 l_debug(""); 636 637 if (!l_dbus_message_get_arguments(message, "o", &path)) 638 return dbus_error_invalid_args(message); 639 640 if (!netconfig_enabled()) 641 return dbus_error_not_supported(message); 642 643 r = netconfig_register_agent(l_dbus_message_get_sender(message), path); 644 if (r) 645 return dbus_error_from_errno(r, message); 646 647 l_debug("agent %s path %s", 648 l_dbus_message_get_sender(message), path); 649 650 reply = l_dbus_message_new_method_return(message); 651 l_dbus_message_set_arguments(reply, ""); 652 return reply; 653 } 654 655 static struct l_dbus_message *netconfig_agent_unregister(struct l_dbus *dbus, 656 struct l_dbus_message *message, 657 void *user_data) 658 { 659 struct l_dbus_message *reply; 660 const char *path; 661 int r; 662 663 l_debug(""); 664 665 if (!l_dbus_message_get_arguments(message, "o", &path)) 666 return dbus_error_invalid_args(message); 667 668 if (!netconfig_enabled()) 669 return dbus_error_not_supported(message); 670 671 r = netconfig_unregister_agent(l_dbus_message_get_sender(message), 672 path); 673 if (r) 674 return dbus_error_from_errno(r, message); 675 676 reply = l_dbus_message_new_method_return(message); 677 l_dbus_message_set_arguments(reply, ""); 678 return reply; 679 } 680 681 static void setup_agent_interface(struct l_dbus_interface *interface) 682 { 683 l_dbus_interface_method(interface, "RegisterAgent", 0, 684 agent_register, 685 "", "o", "path"); 686 l_dbus_interface_method(interface, "UnregisterAgent", 0, 687 agent_unregister, 688 "", "o", "path"); 689 690 l_dbus_interface_method(interface, 691 "RegisterNetworkConfigurationAgent", 0, 692 netconfig_agent_register, "", "o", "path"); 693 l_dbus_interface_method(interface, 694 "UnregisterNetworkConfigurationAgent", 0, 695 netconfig_agent_unregister, "", "o", "path"); 696 } 697 #endif 698 699 static bool release_agent(void *data, void *user_data) 700 { 701 struct agent *agent = data; 702 #ifdef HAVE_DBUS 703 struct l_dbus_message *message; 704 705 l_debug("send Release to %s %s", agent->owner, agent->path); 706 707 message = l_dbus_message_new_method_call(dbus_get_bus(), 708 agent->owner, 709 agent->path, 710 IWD_AGENT_INTERFACE, 711 "Release"); 712 713 l_dbus_message_set_arguments(message, ""); 714 l_dbus_message_set_no_reply(message, true); 715 l_dbus_send(dbus_get_bus(), message); 716 #endif 717 718 agent_free(agent); 719 720 return true; 721 } 722 723 static int agent_init(void) 724 { 725 #ifdef HAVE_DBUS 726 struct l_dbus *dbus = dbus_get_bus(); 727 #endif 728 729 agents = l_queue_new(); 730 731 #ifdef HAVE_DBUS 732 if (!l_dbus_register_interface(dbus, IWD_AGENT_MANAGER_INTERFACE, 733 setup_agent_interface, 734 NULL, false)) { 735 l_info("Unable to register %s interface", 736 IWD_AGENT_MANAGER_INTERFACE); 737 return -EIO; 738 } 739 740 if (!l_dbus_object_add_interface(dbus, IWD_AGENT_MANAGER_PATH, 741 IWD_AGENT_MANAGER_INTERFACE, 742 NULL)) { 743 l_info("Unable to register the agent manager object on '%s'", 744 IWD_AGENT_MANAGER_PATH); 745 l_dbus_unregister_interface(dbus, IWD_AGENT_MANAGER_INTERFACE); 746 return -EIO; 747 } 748 #endif 749 750 return 0; 751 } 752 753 static void agent_exit(void) 754 { 755 #ifdef HAVE_DBUS 756 struct l_dbus *dbus = dbus_get_bus(); 757 758 l_dbus_unregister_interface(dbus, IWD_AGENT_MANAGER_INTERFACE); 759 #endif 760 761 l_queue_destroy(agents, agent_free); 762 agents = NULL; 763 } 764 765 void agent_shutdown(void) 766 { 767 l_queue_foreach_remove(agents, release_agent, NULL); 768 } 769 770 IWD_MODULE(agent, agent_init, agent_exit);