123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113 |
- /*
- * WPA Supplicant / dbus-based control interface
- * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
- */
- #include "includes.h"
- #include "common.h"
- #include "eloop.h"
- #include "config.h"
- #include "wpa_supplicant_i.h"
- #include "wps/wps.h"
- #include "ctrl_iface_dbus.h"
- #include "ctrl_iface_dbus_handlers.h"
- #define _DBUS_VERSION (DBUS_VERSION_MAJOR << 8 | DBUS_VERSION_MINOR)
- #define DBUS_VER(major, minor) ((major) << 8 | (minor))
- #if _DBUS_VERSION < DBUS_VER(1,1)
- #define dbus_watch_get_unix_fd dbus_watch_get_fd
- #endif
- struct ctrl_iface_dbus_priv {
- DBusConnection *con;
- int should_dispatch;
- struct wpa_global *global;
- u32 next_objid;
- };
- static void process_watch(struct ctrl_iface_dbus_priv *iface,
- DBusWatch *watch, eloop_event_type type)
- {
- dbus_connection_ref(iface->con);
- iface->should_dispatch = 0;
- if (type == EVENT_TYPE_READ)
- dbus_watch_handle(watch, DBUS_WATCH_READABLE);
- else if (type == EVENT_TYPE_WRITE)
- dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
- else if (type == EVENT_TYPE_EXCEPTION)
- dbus_watch_handle(watch, DBUS_WATCH_ERROR);
- if (iface->should_dispatch) {
- while (dbus_connection_get_dispatch_status(iface->con) ==
- DBUS_DISPATCH_DATA_REMAINS)
- dbus_connection_dispatch(iface->con);
- iface->should_dispatch = 0;
- }
- dbus_connection_unref(iface->con);
- }
- static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
- {
- process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
- }
- static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
- {
- process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
- }
- static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
- {
- process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
- }
- static void connection_setup_add_watch(struct ctrl_iface_dbus_priv *iface,
- DBusWatch *watch)
- {
- unsigned int flags;
- int fd;
- if (!dbus_watch_get_enabled(watch))
- return;
- flags = dbus_watch_get_flags(watch);
- fd = dbus_watch_get_unix_fd(watch);
- eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
- iface, watch);
- if (flags & DBUS_WATCH_READABLE) {
- eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
- iface, watch);
- }
- if (flags & DBUS_WATCH_WRITABLE) {
- eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
- iface, watch);
- }
- dbus_watch_set_data(watch, iface, NULL);
- }
- static void connection_setup_remove_watch(struct ctrl_iface_dbus_priv *iface,
- DBusWatch *watch)
- {
- unsigned int flags;
- int fd;
- flags = dbus_watch_get_flags(watch);
- fd = dbus_watch_get_unix_fd(watch);
- eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
- if (flags & DBUS_WATCH_READABLE)
- eloop_unregister_sock(fd, EVENT_TYPE_READ);
- if (flags & DBUS_WATCH_WRITABLE)
- eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
- dbus_watch_set_data(watch, NULL, NULL);
- }
- static dbus_bool_t add_watch(DBusWatch *watch, void *data)
- {
- connection_setup_add_watch(data, watch);
- return TRUE;
- }
- static void remove_watch(DBusWatch *watch, void *data)
- {
- connection_setup_remove_watch(data, watch);
- }
- static void watch_toggled(DBusWatch *watch, void *data)
- {
- if (dbus_watch_get_enabled(watch))
- add_watch(watch, data);
- else
- remove_watch(watch, data);
- }
- static void process_timeout(void *eloop_ctx, void *sock_ctx)
- {
- DBusTimeout *timeout = sock_ctx;
- dbus_timeout_handle(timeout);
- }
- static void connection_setup_add_timeout(struct ctrl_iface_dbus_priv *iface,
- DBusTimeout *timeout)
- {
- if (!dbus_timeout_get_enabled(timeout))
- return;
- eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
- process_timeout, iface, timeout);
- dbus_timeout_set_data(timeout, iface, NULL);
- }
- static void connection_setup_remove_timeout(struct ctrl_iface_dbus_priv *iface,
- DBusTimeout *timeout)
- {
- eloop_cancel_timeout(process_timeout, iface, timeout);
- dbus_timeout_set_data(timeout, NULL, NULL);
- }
- static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
- {
- if (!dbus_timeout_get_enabled(timeout))
- return TRUE;
- connection_setup_add_timeout(data, timeout);
- return TRUE;
- }
- static void remove_timeout(DBusTimeout *timeout, void *data)
- {
- connection_setup_remove_timeout(data, timeout);
- }
- static void timeout_toggled(DBusTimeout *timeout, void *data)
- {
- if (dbus_timeout_get_enabled(timeout))
- add_timeout(timeout, data);
- else
- remove_timeout(timeout, data);
- }
- static void process_wakeup_main(int sig, void *eloop_ctx, void *signal_ctx)
- {
- struct ctrl_iface_dbus_priv *iface = signal_ctx;
- if (sig != SIGPOLL || !iface->con)
- return;
- if (dbus_connection_get_dispatch_status(iface->con) !=
- DBUS_DISPATCH_DATA_REMAINS)
- return;
- /* Only dispatch once - we do not want to starve other events */
- dbus_connection_ref(iface->con);
- dbus_connection_dispatch(iface->con);
- dbus_connection_unref(iface->con);
- }
- /**
- * wakeup_main - Attempt to wake our mainloop up
- * @data: dbus control interface private data
- *
- * Try to wake up the main eloop so it will process
- * dbus events that may have happened.
- */
- static void wakeup_main(void *data)
- {
- struct ctrl_iface_dbus_priv *iface = data;
- /* Use SIGPOLL to break out of the eloop select() */
- raise(SIGPOLL);
- iface->should_dispatch = 1;
- }
- /**
- * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
- * @iface: dbus control interface private data
- * Returns: 0 on success, -1 on failure
- *
- * Register our wakeup_main handler with dbus
- */
- static int connection_setup_wakeup_main(struct ctrl_iface_dbus_priv *iface)
- {
- if (eloop_register_signal(SIGPOLL, process_wakeup_main, iface))
- return -1;
- dbus_connection_set_wakeup_main_function(iface->con, wakeup_main,
- iface, NULL);
- return 0;
- }
- /**
- * wpa_supplicant_dbus_next_objid - Return next available object id
- * @iface: dbus control interface private data
- * Returns: Object id
- */
- u32 wpa_supplicant_dbus_next_objid (struct ctrl_iface_dbus_priv *iface)
- {
- return iface->next_objid++;
- }
- /**
- * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
- * @path: The dbus object path
- * @network: (out) the configured network this object path refers to, if any
- * @bssid: (out) the scanned bssid this object path refers to, if any
- * Returns: The object path of the network interface this path refers to
- *
- * For a given object path, decomposes the object path into object id, network,
- * and BSSID parts, if those parts exist.
- */
- char * wpas_dbus_decompose_object_path(const char *path, char **network,
- char **bssid)
- {
- const unsigned int dev_path_prefix_len =
- strlen(WPAS_DBUS_PATH_INTERFACES "/");
- char *obj_path_only;
- char *next_sep;
- /* Be a bit paranoid about path */
- if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
- dev_path_prefix_len))
- return NULL;
- /* Ensure there's something at the end of the path */
- if ((path + dev_path_prefix_len)[0] == '\0')
- return NULL;
- obj_path_only = strdup(path);
- if (obj_path_only == NULL)
- return NULL;
- next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
- if (next_sep != NULL) {
- const char *net_part = strstr(next_sep,
- WPAS_DBUS_NETWORKS_PART "/");
- const char *bssid_part = strstr(next_sep,
- WPAS_DBUS_BSSIDS_PART "/");
- if (network && net_part) {
- /* Deal with a request for a configured network */
- const char *net_name = net_part +
- strlen(WPAS_DBUS_NETWORKS_PART "/");
- *network = NULL;
- if (strlen(net_name))
- *network = strdup(net_name);
- } else if (bssid && bssid_part) {
- /* Deal with a request for a scanned BSSID */
- const char *bssid_name = bssid_part +
- strlen(WPAS_DBUS_BSSIDS_PART "/");
- if (strlen(bssid_name))
- *bssid = strdup(bssid_name);
- else
- *bssid = NULL;
- }
- /* Cut off interface object path before "/" */
- *next_sep = '\0';
- }
- return obj_path_only;
- }
- /**
- * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
- * @message: Pointer to incoming dbus message this error refers to
- * Returns: A dbus error message
- *
- * Convenience function to create and return an invalid interface error
- */
- DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
- {
- return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
- "wpa_supplicant knows nothing about "
- "this interface.");
- }
- /**
- * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
- * @message: Pointer to incoming dbus message this error refers to
- * Returns: a dbus error message
- *
- * Convenience function to create and return an invalid network error
- */
- DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
- {
- return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
- "The requested network does not exist.");
- }
- /**
- * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
- * @message: Pointer to incoming dbus message this error refers to
- * Returns: a dbus error message
- *
- * Convenience function to create and return an invalid bssid error
- */
- static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
- {
- return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
- "The BSSID requested was invalid.");
- }
- /**
- * wpas_dispatch_network_method - dispatch messages for configured networks
- * @message: the incoming dbus message
- * @wpa_s: a network interface's data
- * @network_id: id of the configured network we're interested in
- * Returns: a reply dbus message, or a dbus error message
- *
- * This function dispatches all incoming dbus messages for configured networks.
- */
- static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
- struct wpa_supplicant *wpa_s,
- int network_id)
- {
- DBusMessage *reply = NULL;
- const char *method = dbus_message_get_member(message);
- struct wpa_ssid *ssid;
- ssid = wpa_config_get_network(wpa_s->conf, network_id);
- if (ssid == NULL)
- return wpas_dbus_new_invalid_network_error(message);
- if (!strcmp(method, "set"))
- reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
- else if (!strcmp(method, "enable"))
- reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
- else if (!strcmp(method, "disable"))
- reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
- return reply;
- }
- /**
- * wpas_dispatch_bssid_method - dispatch messages for scanned networks
- * @message: the incoming dbus message
- * @wpa_s: a network interface's data
- * @bssid: bssid of the scanned network we're interested in
- * Returns: a reply dbus message, or a dbus error message
- *
- * This function dispatches all incoming dbus messages for scanned networks.
- */
- static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
- struct wpa_supplicant *wpa_s,
- const char *bssid)
- {
- DBusMessage *reply = NULL;
- const char *method = dbus_message_get_member(message);
- struct wpa_scan_res *res = NULL;
- size_t i;
- /* Ensure we actually have scan data */
- if (wpa_s->scan_res == NULL &&
- wpa_supplicant_get_scan_results(wpa_s) < 0) {
- reply = wpas_dbus_new_invalid_bssid_error(message);
- goto out;
- }
- /* Find the bssid's scan data */
- for (i = 0; i < wpa_s->scan_res->num; i++) {
- struct wpa_scan_res *search_res = wpa_s->scan_res->res[i];
- char mac_str[18];
- memset(mac_str, 0, sizeof(mac_str));
- snprintf(mac_str, sizeof(mac_str) - 1, WPAS_DBUS_BSSID_FORMAT,
- MAC2STR(search_res->bssid));
- if (!strcmp(bssid, mac_str)) {
- res = search_res;
- break;
- }
- }
- if (!res) {
- reply = wpas_dbus_new_invalid_bssid_error(message);
- goto out;
- }
- /* Dispatch the method call against the scanned bssid */
- if (!strcmp(method, "properties"))
- reply = wpas_dbus_bssid_properties(message, wpa_s, res);
- out:
- return reply;
- }
- /**
- * wpas_iface_message_handler - Dispatch messages for interfaces or networks
- * @connection: Connection to the system message bus
- * @message: An incoming dbus message
- * @user_data: A pointer to a dbus control interface data structure
- * Returns: Whether or not the message was handled
- *
- * This function dispatches all incoming dbus messages for network interfaces,
- * or objects owned by them, such as scanned BSSIDs and configured networks.
- */
- static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
- DBusMessage *message,
- void *user_data)
- {
- struct wpa_supplicant *wpa_s = user_data;
- const char *method = dbus_message_get_member(message);
- const char *path = dbus_message_get_path(message);
- const char *msg_interface = dbus_message_get_interface(message);
- char *iface_obj_path = NULL;
- char *network = NULL;
- char *bssid = NULL;
- DBusMessage *reply = NULL;
- /* Caller must specify a message interface */
- if (!msg_interface)
- goto out;
- iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
- &bssid);
- if (iface_obj_path == NULL) {
- reply = wpas_dbus_new_invalid_iface_error(message);
- goto out;
- }
- /* Make sure the message's object path actually refers to the
- * wpa_supplicant structure it's supposed to (which is wpa_s)
- */
- if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
- iface_obj_path) != wpa_s) {
- reply = wpas_dbus_new_invalid_iface_error(message);
- goto out;
- }
- if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
- /* A method for one of this interface's configured networks */
- int nid = strtoul(network, NULL, 10);
- if (errno != EINVAL)
- reply = wpas_dispatch_network_method(message, wpa_s,
- nid);
- else
- reply = wpas_dbus_new_invalid_network_error(message);
- } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
- /* A method for one of this interface's scanned BSSIDs */
- reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
- } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
- /* A method for an interface only. */
- if (!strcmp(method, "scan"))
- reply = wpas_dbus_iface_scan(message, wpa_s);
- else if (!strcmp(method, "scanResults"))
- reply = wpas_dbus_iface_scan_results(message, wpa_s);
- else if (!strcmp(method, "addNetwork"))
- reply = wpas_dbus_iface_add_network(message, wpa_s);
- else if (!strcmp(method, "removeNetwork"))
- reply = wpas_dbus_iface_remove_network(message, wpa_s);
- else if (!strcmp(method, "selectNetwork"))
- reply = wpas_dbus_iface_select_network(message, wpa_s);
- else if (!strcmp(method, "capabilities"))
- reply = wpas_dbus_iface_capabilities(message, wpa_s);
- else if (!strcmp(method, "disconnect"))
- reply = wpas_dbus_iface_disconnect(message, wpa_s);
- else if (!strcmp(method, "setAPScan"))
- reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
- else if (!strcmp(method, "setSmartcardModules"))
- reply = wpas_dbus_iface_set_smartcard_modules(message,
- wpa_s);
- else if (!strcmp(method, "state"))
- reply = wpas_dbus_iface_get_state(message, wpa_s);
- else if (!strcmp(method, "setBlobs"))
- reply = wpas_dbus_iface_set_blobs(message, wpa_s);
- else if (!strcmp(method, "removeBlobs"))
- reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
- }
- /* If the message was handled, send back the reply */
- if (reply) {
- dbus_connection_send(connection, reply, NULL);
- dbus_message_unref(reply);
- }
- out:
- free(iface_obj_path);
- free(network);
- free(bssid);
- return reply ? DBUS_HANDLER_RESULT_HANDLED :
- DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- }
- /**
- * wpas_message_handler - dispatch incoming dbus messages
- * @connection: connection to the system message bus
- * @message: an incoming dbus message
- * @user_data: a pointer to a dbus control interface data structure
- * Returns: whether or not the message was handled
- *
- * This function dispatches all incoming dbus messages to the correct
- * handlers, depending on what the message's target object path is,
- * and what the method call is.
- */
- static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
- DBusMessage *message, void *user_data)
- {
- struct ctrl_iface_dbus_priv *ctrl_iface = user_data;
- const char *method;
- const char *path;
- const char *msg_interface;
- DBusMessage *reply = NULL;
- method = dbus_message_get_member(message);
- path = dbus_message_get_path(message);
- msg_interface = dbus_message_get_interface(message);
- if (!method || !path || !ctrl_iface || !msg_interface)
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- /* Validate the method interface */
- if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- if (!strcmp(path, WPAS_DBUS_PATH)) {
- /* dispatch methods against our global dbus interface here */
- if (!strcmp(method, "addInterface")) {
- reply = wpas_dbus_global_add_interface(
- message, ctrl_iface->global);
- } else if (!strcmp(method, "removeInterface")) {
- reply = wpas_dbus_global_remove_interface(
- message, ctrl_iface->global);
- } else if (!strcmp(method, "getInterface")) {
- reply = wpas_dbus_global_get_interface(
- message, ctrl_iface->global);
- }
- }
- /* If the message was handled, send back the reply */
- if (reply) {
- dbus_connection_send(connection, reply, NULL);
- dbus_message_unref(reply);
- }
- return reply ? DBUS_HANDLER_RESULT_HANDLED :
- DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
- }
- /**
- * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
- * @wpa_s: %wpa_supplicant network interface data
- * Returns: 0 on success, -1 on failure
- *
- * Notify listeners that this interface has updated scan results.
- */
- void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
- {
- struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
- DBusMessage *_signal;
- const char *path;
- /* Do nothing if the control interface is not turned on */
- if (iface == NULL)
- return;
- path = wpa_supplicant_get_dbus_path(wpa_s);
- if (path == NULL) {
- perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
- "interface didn't have a dbus path");
- wpa_printf(MSG_ERROR,
- "wpa_supplicant_dbus_notify_scan_results[dbus]: "
- "interface didn't have a dbus path; can't send "
- "scan result signal.");
- return;
- }
- _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
- "ScanResultsAvailable");
- if (_signal == NULL) {
- perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
- "couldn't create dbus signal; likely out of memory");
- wpa_printf(MSG_ERROR, "dbus control interface: not enough "
- "memory to send scan results signal.");
- return;
- }
- dbus_connection_send(iface->con, _signal, NULL);
- dbus_message_unref(_signal);
- }
- /**
- * wpa_supplicant_dbus_notify_state_change - Send a state change signal
- * @wpa_s: %wpa_supplicant network interface data
- * @new_state: new state wpa_supplicant is entering
- * @old_state: old state wpa_supplicant is leaving
- * Returns: 0 on success, -1 on failure
- *
- * Notify listeners that wpa_supplicant has changed state
- */
- void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
- wpa_states new_state,
- wpa_states old_state)
- {
- struct ctrl_iface_dbus_priv *iface;
- DBusMessage *_signal = NULL;
- const char *path;
- const char *new_state_str, *old_state_str;
- /* Do nothing if the control interface is not turned on */
- if (wpa_s->global == NULL)
- return;
- iface = wpa_s->global->dbus_ctrl_iface;
- if (iface == NULL)
- return;
- /* Only send signal if state really changed */
- if (new_state == old_state)
- return;
- path = wpa_supplicant_get_dbus_path(wpa_s);
- if (path == NULL) {
- perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
- "interface didn't have a dbus path");
- wpa_printf(MSG_ERROR,
- "wpa_supplicant_dbus_notify_state_change[dbus]: "
- "interface didn't have a dbus path; can't send "
- "signal.");
- return;
- }
- _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
- "StateChange");
- if (_signal == NULL) {
- perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
- "couldn't create dbus signal; likely out of memory");
- wpa_printf(MSG_ERROR,
- "wpa_supplicant_dbus_notify_state_change[dbus]: "
- "couldn't create dbus signal; likely out of "
- "memory.");
- return;
- }
- new_state_str = wpa_supplicant_state_txt(new_state);
- old_state_str = wpa_supplicant_state_txt(old_state);
- if (new_state_str == NULL || old_state_str == NULL) {
- perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
- "couldn't convert state strings");
- wpa_printf(MSG_ERROR,
- "wpa_supplicant_dbus_notify_state_change[dbus]: "
- "couldn't convert state strings.");
- goto out;
- }
- if (!dbus_message_append_args(_signal,
- DBUS_TYPE_STRING, &new_state_str,
- DBUS_TYPE_STRING, &old_state_str,
- DBUS_TYPE_INVALID)) {
- perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
- "not enough memory to construct state change signal.");
- wpa_printf(MSG_ERROR,
- "wpa_supplicant_dbus_notify_state_change[dbus]: "
- "not enough memory to construct state change "
- "signal.");
- goto out;
- }
- dbus_connection_send(iface->con, _signal, NULL);
- out:
- dbus_message_unref(_signal);
- }
- #ifdef CONFIG_WPS
- void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
- const struct wps_credential *cred)
- {
- struct ctrl_iface_dbus_priv *iface;
- DBusMessage *_signal = NULL;
- const char *path;
- /* Do nothing if the control interface is not turned on */
- if (wpa_s->global == NULL)
- return;
- iface = wpa_s->global->dbus_ctrl_iface;
- if (iface == NULL)
- return;
- path = wpa_supplicant_get_dbus_path(wpa_s);
- if (path == NULL) {
- perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
- "interface didn't have a dbus path");
- wpa_printf(MSG_ERROR,
- "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
- "interface didn't have a dbus path; can't send "
- "signal.");
- return;
- }
- _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
- "WpsCred");
- if (_signal == NULL) {
- perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
- "couldn't create dbus signal; likely out of memory");
- wpa_printf(MSG_ERROR,
- "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
- "couldn't create dbus signal; likely out of "
- "memory.");
- return;
- }
- if (!dbus_message_append_args(_signal,
- DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
- &cred->cred_attr, cred->cred_attr_len,
- DBUS_TYPE_INVALID)) {
- perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
- "not enough memory to construct signal.");
- wpa_printf(MSG_ERROR,
- "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
- "not enough memory to construct signal.");
- goto out;
- }
- dbus_connection_send(iface->con, _signal, NULL);
- out:
- dbus_message_unref(_signal);
- }
- #endif /* CONFIG_WPS */
- /**
- * integrate_with_eloop - Register our mainloop integration with dbus
- * @connection: connection to the system message bus
- * @iface: a dbus control interface data structure
- * Returns: 0 on success, -1 on failure
- *
- * We register our mainloop integration functions with dbus here.
- */
- static int integrate_with_eloop(DBusConnection *connection,
- struct ctrl_iface_dbus_priv *iface)
- {
- if (!dbus_connection_set_watch_functions(connection, add_watch,
- remove_watch, watch_toggled,
- iface, NULL)) {
- perror("dbus_connection_set_watch_functions[dbus]");
- wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
- return -1;
- }
- if (!dbus_connection_set_timeout_functions(connection, add_timeout,
- remove_timeout,
- timeout_toggled, iface,
- NULL)) {
- perror("dbus_connection_set_timeout_functions[dbus]");
- wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
- return -1;
- }
- if (connection_setup_wakeup_main(iface) < 0) {
- perror("connection_setup_wakeup_main[dbus]");
- wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
- return -1;
- }
- return 0;
- }
- /**
- * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
- * claiming bus name
- * @eloop_ctx: the DBusConnection to dispatch on
- * @timeout_ctx: unused
- *
- * If clients are quick to notice that wpa_supplicant claimed its bus name,
- * there may have been messages that came in before initialization was
- * all finished. Dispatch those here.
- */
- static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
- {
- DBusConnection *con = eloop_ctx;
- while (dbus_connection_get_dispatch_status(con) ==
- DBUS_DISPATCH_DATA_REMAINS)
- dbus_connection_dispatch(con);
- }
- /**
- * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
- * @global: Pointer to global data from wpa_supplicant_init()
- * Returns: Pointer to dbus_ctrl_iface date or %NULL on failure
- *
- * Initialize the dbus control interface and start receiving commands from
- * external programs over the bus.
- */
- struct ctrl_iface_dbus_priv *
- wpa_supplicant_dbus_ctrl_iface_init(struct wpa_global *global)
- {
- struct ctrl_iface_dbus_priv *iface;
- DBusError error;
- int ret = -1;
- DBusObjectPathVTable wpas_vtable = {
- NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
- };
- iface = os_zalloc(sizeof(struct ctrl_iface_dbus_priv));
- if (iface == NULL)
- return NULL;
- iface->global = global;
- /* Get a reference to the system bus */
- dbus_error_init(&error);
- iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
- dbus_error_free(&error);
- if (!iface->con) {
- perror("dbus_bus_get[ctrl_iface_dbus]");
- wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
- goto fail;
- }
- /* Tell dbus about our mainloop integration functions */
- if (integrate_with_eloop(iface->con, iface))
- goto fail;
- /* Register the message handler for the global dbus interface */
- if (!dbus_connection_register_object_path(iface->con,
- WPAS_DBUS_PATH, &wpas_vtable,
- iface)) {
- perror("dbus_connection_register_object_path[dbus]");
- wpa_printf(MSG_ERROR, "Could not set up DBus message "
- "handler.");
- goto fail;
- }
- /* Register our service with the message bus */
- dbus_error_init(&error);
- switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
- 0, &error)) {
- case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
- ret = 0;
- break;
- case DBUS_REQUEST_NAME_REPLY_EXISTS:
- case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
- case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
- perror("dbus_bus_request_name[dbus]");
- wpa_printf(MSG_ERROR, "Could not request DBus service name: "
- "already registered.");
- break;
- default:
- perror("dbus_bus_request_name[dbus]");
- wpa_printf(MSG_ERROR, "Could not request DBus service name: "
- "%s %s.", error.name, error.message);
- break;
- }
- dbus_error_free(&error);
- if (ret != 0)
- goto fail;
- wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
- "'.");
- /*
- * Dispatch initial DBus messages that may have come in since the bus
- * name was claimed above. Happens when clients are quick to notice the
- * wpa_supplicant service.
- *
- * FIXME: is there a better solution to this problem?
- */
- eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
- iface->con, NULL);
- return iface;
- fail:
- wpa_supplicant_dbus_ctrl_iface_deinit(iface);
- return NULL;
- }
- /**
- * wpa_supplicant_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
- * @iface: Pointer to dbus private data from
- * wpa_supplicant_dbus_ctrl_iface_init()
- *
- * Deinitialize the dbus control interface that was initialized with
- * wpa_supplicant_dbus_ctrl_iface_init().
- */
- void wpa_supplicant_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_priv *iface)
- {
- if (iface == NULL)
- return;
- if (iface->con) {
- eloop_cancel_timeout(dispatch_initial_dbus_messages,
- iface->con, NULL);
- dbus_connection_set_watch_functions(iface->con, NULL, NULL,
- NULL, NULL, NULL);
- dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
- NULL, NULL, NULL);
- dbus_connection_unref(iface->con);
- }
- memset(iface, 0, sizeof(struct ctrl_iface_dbus_priv));
- free(iface);
- }
- /**
- * wpas_dbus_register_new_iface - Register a new interface with dbus
- * @wpa_s: %wpa_supplicant interface description structure to register
- * Returns: 0 on success, -1 on error
- *
- * Registers a new interface with dbus and assigns it a dbus object path.
- */
- int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
- {
- struct ctrl_iface_dbus_priv *ctrl_iface =
- wpa_s->global->dbus_ctrl_iface;
- DBusConnection * con;
- u32 next;
- DBusObjectPathVTable vtable = {
- NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
- };
- char *path;
- int ret = -1;
- /* Do nothing if the control interface is not turned on */
- if (ctrl_iface == NULL)
- return 0;
- con = ctrl_iface->con;
- next = wpa_supplicant_dbus_next_objid(ctrl_iface);
- /* Create and set the interface's object path */
- path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
- if (path == NULL)
- return -1;
- snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
- WPAS_DBUS_PATH_INTERFACES "/%u",
- next);
- if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
- wpa_printf(MSG_DEBUG,
- "Failed to set dbus path for interface %s",
- wpa_s->ifname);
- goto out;
- }
- /* Register the message handler for the interface functions */
- if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
- perror("wpas_dbus_register_iface [dbus]");
- wpa_printf(MSG_ERROR, "Could not set up DBus message "
- "handler for interface %s.", wpa_s->ifname);
- goto out;
- }
- ret = 0;
- out:
- free(path);
- return ret;
- }
- /**
- * wpas_dbus_unregister_iface - Unregister an interface from dbus
- * @wpa_s: wpa_supplicant interface structure
- * Returns: 0 on success, -1 on failure
- *
- * Unregisters the interface with dbus
- */
- int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
- {
- struct ctrl_iface_dbus_priv *ctrl_iface;
- DBusConnection *con;
- const char *path;
- /* Do nothing if the control interface is not turned on */
- if (wpa_s == NULL || wpa_s->global == NULL)
- return 0;
- ctrl_iface = wpa_s->global->dbus_ctrl_iface;
- if (ctrl_iface == NULL)
- return 0;
- con = ctrl_iface->con;
- path = wpa_supplicant_get_dbus_path(wpa_s);
- if (!dbus_connection_unregister_object_path(con, path))
- return -1;
- free(wpa_s->dbus_path);
- wpa_s->dbus_path = NULL;
- return 0;
- }
- /**
- * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
- * @global: Pointer to global data from wpa_supplicant_init()
- * @path: Pointer to a dbus object path representing an interface
- * Returns: Pointer to the interface or %NULL if not found
- */
- struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
- struct wpa_global *global, const char *path)
- {
- struct wpa_supplicant *wpa_s;
- for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
- if (strcmp(wpa_s->dbus_path, path) == 0)
- return wpa_s;
- }
- return NULL;
- }
- /**
- * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
- * @wpa_s: wpa_supplicant interface structure
- * @path: dbus path to set on the interface
- * Returns: 0 on succes, -1 on error
- */
- int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
- const char *path)
- {
- u32 len = strlen (path);
- if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
- return -1;
- if (wpa_s->dbus_path)
- return -1;
- wpa_s->dbus_path = strdup(path);
- return 0;
- }
- /**
- * wpa_supplicant_get_dbus_path - Get an interface's dbus path
- * @wpa_s: %wpa_supplicant interface structure
- * Returns: Interface's dbus object path, or %NULL on error
- */
- const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
- {
- return wpa_s->dbus_path;
- }
|