ctrl_iface_dbus.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /*
  2. * WPA Supplicant / dbus-based control interface
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "config.h"
  18. #include "wpa_supplicant_i.h"
  19. #include "ctrl_iface_dbus.h"
  20. #include "ctrl_iface_dbus_handlers.h"
  21. #define _DBUS_VERSION (DBUS_VERSION_MAJOR << 8 | DBUS_VERSION_MINOR)
  22. #define DBUS_VER(major, minor) ((major) << 8 | (minor))
  23. #if _DBUS_VERSION < DBUS_VER(1,1)
  24. #define dbus_watch_get_unix_fd dbus_watch_get_fd
  25. #endif
  26. struct ctrl_iface_dbus_priv {
  27. DBusConnection *con;
  28. int should_dispatch;
  29. struct wpa_global *global;
  30. u32 next_objid;
  31. };
  32. static void process_watch(struct ctrl_iface_dbus_priv *iface,
  33. DBusWatch *watch, eloop_event_type type)
  34. {
  35. dbus_connection_ref(iface->con);
  36. iface->should_dispatch = 0;
  37. if (type == EVENT_TYPE_READ)
  38. dbus_watch_handle(watch, DBUS_WATCH_READABLE);
  39. else if (type == EVENT_TYPE_WRITE)
  40. dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
  41. else if (type == EVENT_TYPE_EXCEPTION)
  42. dbus_watch_handle(watch, DBUS_WATCH_ERROR);
  43. if (iface->should_dispatch) {
  44. while (dbus_connection_get_dispatch_status(iface->con) ==
  45. DBUS_DISPATCH_DATA_REMAINS)
  46. dbus_connection_dispatch(iface->con);
  47. iface->should_dispatch = 0;
  48. }
  49. dbus_connection_unref(iface->con);
  50. }
  51. static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
  52. {
  53. process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
  54. }
  55. static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
  56. {
  57. process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
  58. }
  59. static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
  60. {
  61. process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
  62. }
  63. static void connection_setup_add_watch(struct ctrl_iface_dbus_priv *iface,
  64. DBusWatch *watch)
  65. {
  66. unsigned int flags;
  67. int fd;
  68. if (!dbus_watch_get_enabled(watch))
  69. return;
  70. flags = dbus_watch_get_flags(watch);
  71. fd = dbus_watch_get_unix_fd(watch);
  72. eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
  73. iface, watch);
  74. if (flags & DBUS_WATCH_READABLE) {
  75. eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
  76. iface, watch);
  77. }
  78. if (flags & DBUS_WATCH_WRITABLE) {
  79. eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
  80. iface, watch);
  81. }
  82. dbus_watch_set_data(watch, iface, NULL);
  83. }
  84. static void connection_setup_remove_watch(struct ctrl_iface_dbus_priv *iface,
  85. DBusWatch *watch)
  86. {
  87. unsigned int flags;
  88. int fd;
  89. flags = dbus_watch_get_flags(watch);
  90. fd = dbus_watch_get_unix_fd(watch);
  91. eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
  92. if (flags & DBUS_WATCH_READABLE)
  93. eloop_unregister_sock(fd, EVENT_TYPE_READ);
  94. if (flags & DBUS_WATCH_WRITABLE)
  95. eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
  96. dbus_watch_set_data(watch, NULL, NULL);
  97. }
  98. static dbus_bool_t add_watch(DBusWatch *watch, void *data)
  99. {
  100. connection_setup_add_watch(data, watch);
  101. return TRUE;
  102. }
  103. static void remove_watch(DBusWatch *watch, void *data)
  104. {
  105. connection_setup_remove_watch(data, watch);
  106. }
  107. static void watch_toggled(DBusWatch *watch, void *data)
  108. {
  109. if (dbus_watch_get_enabled(watch))
  110. add_watch(watch, data);
  111. else
  112. remove_watch(watch, data);
  113. }
  114. static void process_timeout(void *eloop_ctx, void *sock_ctx)
  115. {
  116. DBusTimeout *timeout = sock_ctx;
  117. dbus_timeout_handle(timeout);
  118. }
  119. static void connection_setup_add_timeout(struct ctrl_iface_dbus_priv *iface,
  120. DBusTimeout *timeout)
  121. {
  122. if (!dbus_timeout_get_enabled(timeout))
  123. return;
  124. eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
  125. process_timeout, iface, timeout);
  126. dbus_timeout_set_data(timeout, iface, NULL);
  127. }
  128. static void connection_setup_remove_timeout(struct ctrl_iface_dbus_priv *iface,
  129. DBusTimeout *timeout)
  130. {
  131. eloop_cancel_timeout(process_timeout, iface, timeout);
  132. dbus_timeout_set_data(timeout, NULL, NULL);
  133. }
  134. static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
  135. {
  136. if (!dbus_timeout_get_enabled(timeout))
  137. return TRUE;
  138. connection_setup_add_timeout(data, timeout);
  139. return TRUE;
  140. }
  141. static void remove_timeout(DBusTimeout *timeout, void *data)
  142. {
  143. connection_setup_remove_timeout(data, timeout);
  144. }
  145. static void timeout_toggled(DBusTimeout *timeout, void *data)
  146. {
  147. if (dbus_timeout_get_enabled(timeout))
  148. add_timeout(timeout, data);
  149. else
  150. remove_timeout(timeout, data);
  151. }
  152. static void process_wakeup_main(int sig, void *eloop_ctx, void *signal_ctx)
  153. {
  154. struct ctrl_iface_dbus_priv *iface = signal_ctx;
  155. if (sig != SIGPOLL || !iface->con)
  156. return;
  157. if (dbus_connection_get_dispatch_status(iface->con) !=
  158. DBUS_DISPATCH_DATA_REMAINS)
  159. return;
  160. /* Only dispatch once - we do not want to starve other events */
  161. dbus_connection_ref(iface->con);
  162. dbus_connection_dispatch(iface->con);
  163. dbus_connection_unref(iface->con);
  164. }
  165. /**
  166. * wakeup_main - Attempt to wake our mainloop up
  167. * @data: dbus control interface private data
  168. *
  169. * Try to wake up the main eloop so it will process
  170. * dbus events that may have happened.
  171. */
  172. static void wakeup_main(void *data)
  173. {
  174. struct ctrl_iface_dbus_priv *iface = data;
  175. /* Use SIGPOLL to break out of the eloop select() */
  176. raise(SIGPOLL);
  177. iface->should_dispatch = 1;
  178. }
  179. /**
  180. * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
  181. * @iface: dbus control interface private data
  182. * Returns: 0 on success, -1 on failure
  183. *
  184. * Register our wakeup_main handler with dbus
  185. */
  186. static int connection_setup_wakeup_main(struct ctrl_iface_dbus_priv *iface)
  187. {
  188. if (eloop_register_signal(SIGPOLL, process_wakeup_main, iface))
  189. return -1;
  190. dbus_connection_set_wakeup_main_function(iface->con, wakeup_main,
  191. iface, NULL);
  192. return 0;
  193. }
  194. /**
  195. * wpa_supplicant_dbus_next_objid - Return next available object id
  196. * @iface: dbus control interface private data
  197. * Returns: Object id
  198. */
  199. u32 wpa_supplicant_dbus_next_objid (struct ctrl_iface_dbus_priv *iface)
  200. {
  201. return iface->next_objid++;
  202. }
  203. /**
  204. * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
  205. * @path: The dbus object path
  206. * @network: (out) the configured network this object path refers to, if any
  207. * @bssid: (out) the scanned bssid this object path refers to, if any
  208. * Returns: The object path of the network interface this path refers to
  209. *
  210. * For a given object path, decomposes the object path into object id, network,
  211. * and BSSID parts, if those parts exist.
  212. */
  213. char * wpas_dbus_decompose_object_path(const char *path, char **network,
  214. char **bssid)
  215. {
  216. const unsigned int dev_path_prefix_len =
  217. strlen(WPAS_DBUS_PATH_INTERFACES "/");
  218. char *obj_path_only;
  219. char *next_sep;
  220. /* Be a bit paranoid about path */
  221. if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
  222. dev_path_prefix_len))
  223. return NULL;
  224. /* Ensure there's something at the end of the path */
  225. if ((path + dev_path_prefix_len)[0] == '\0')
  226. return NULL;
  227. obj_path_only = strdup(path);
  228. if (obj_path_only == NULL)
  229. return NULL;
  230. next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
  231. if (next_sep != NULL) {
  232. const char *net_part = strstr(next_sep,
  233. WPAS_DBUS_NETWORKS_PART "/");
  234. const char *bssid_part = strstr(next_sep,
  235. WPAS_DBUS_BSSIDS_PART "/");
  236. if (network && net_part) {
  237. /* Deal with a request for a configured network */
  238. const char *net_name = net_part +
  239. strlen(WPAS_DBUS_NETWORKS_PART "/");
  240. *network = NULL;
  241. if (strlen(net_name))
  242. *network = strdup(net_name);
  243. } else if (bssid && bssid_part) {
  244. /* Deal with a request for a scanned BSSID */
  245. const char *bssid_name = bssid_part +
  246. strlen(WPAS_DBUS_BSSIDS_PART "/");
  247. if (strlen(bssid_name))
  248. *bssid = strdup(bssid_name);
  249. else
  250. *bssid = NULL;
  251. }
  252. /* Cut off interface object path before "/" */
  253. *next_sep = '\0';
  254. }
  255. return obj_path_only;
  256. }
  257. /**
  258. * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
  259. * @message: Pointer to incoming dbus message this error refers to
  260. * Returns: A dbus error message
  261. *
  262. * Convenience function to create and return an invalid interface error
  263. */
  264. DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
  265. {
  266. return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
  267. "wpa_supplicant knows nothing about "
  268. "this interface.");
  269. }
  270. /**
  271. * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
  272. * @message: Pointer to incoming dbus message this error refers to
  273. * Returns: a dbus error message
  274. *
  275. * Convenience function to create and return an invalid network error
  276. */
  277. DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
  278. {
  279. return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
  280. "The requested network does not exist.");
  281. }
  282. /**
  283. * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
  284. * @message: Pointer to incoming dbus message this error refers to
  285. * Returns: a dbus error message
  286. *
  287. * Convenience function to create and return an invalid bssid error
  288. */
  289. static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
  290. {
  291. return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
  292. "The BSSID requested was invalid.");
  293. }
  294. /**
  295. * wpas_dispatch_network_method - dispatch messages for configured networks
  296. * @message: the incoming dbus message
  297. * @wpa_s: a network interface's data
  298. * @network_id: id of the configured network we're interested in
  299. * Returns: a reply dbus message, or a dbus error message
  300. *
  301. * This function dispatches all incoming dbus messages for configured networks.
  302. */
  303. static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
  304. struct wpa_supplicant *wpa_s,
  305. int network_id)
  306. {
  307. DBusMessage *reply = NULL;
  308. const char *method = dbus_message_get_member(message);
  309. struct wpa_ssid *ssid;
  310. ssid = wpa_config_get_network(wpa_s->conf, network_id);
  311. if (ssid == NULL)
  312. return wpas_dbus_new_invalid_network_error(message);
  313. if (!strcmp(method, "set"))
  314. reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
  315. else if (!strcmp(method, "enable"))
  316. reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
  317. else if (!strcmp(method, "disable"))
  318. reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
  319. return reply;
  320. }
  321. /**
  322. * wpas_dispatch_bssid_method - dispatch messages for scanned networks
  323. * @message: the incoming dbus message
  324. * @wpa_s: a network interface's data
  325. * @bssid: bssid of the scanned network we're interested in
  326. * Returns: a reply dbus message, or a dbus error message
  327. *
  328. * This function dispatches all incoming dbus messages for scanned networks.
  329. */
  330. static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
  331. struct wpa_supplicant *wpa_s,
  332. const char *bssid)
  333. {
  334. DBusMessage *reply = NULL;
  335. const char *method = dbus_message_get_member(message);
  336. struct wpa_scan_res *res = NULL;
  337. size_t i;
  338. /* Ensure we actually have scan data */
  339. if (wpa_s->scan_res == NULL &&
  340. wpa_supplicant_get_scan_results(wpa_s) < 0) {
  341. reply = wpas_dbus_new_invalid_bssid_error(message);
  342. goto out;
  343. }
  344. /* Find the bssid's scan data */
  345. for (i = 0; i < wpa_s->scan_res->num; i++) {
  346. struct wpa_scan_res *search_res = wpa_s->scan_res->res[i];
  347. char mac_str[18];
  348. memset(mac_str, 0, sizeof(mac_str));
  349. snprintf(mac_str, sizeof(mac_str) - 1, WPAS_DBUS_BSSID_FORMAT,
  350. MAC2STR(search_res->bssid));
  351. if (!strcmp(bssid, mac_str)) {
  352. res = search_res;
  353. break;
  354. }
  355. }
  356. if (!res) {
  357. reply = wpas_dbus_new_invalid_bssid_error(message);
  358. goto out;
  359. }
  360. /* Dispatch the method call against the scanned bssid */
  361. if (!strcmp(method, "properties"))
  362. reply = wpas_dbus_bssid_properties(message, wpa_s, res);
  363. out:
  364. return reply;
  365. }
  366. /**
  367. * wpas_iface_message_handler - Dispatch messages for interfaces or networks
  368. * @connection: Connection to the system message bus
  369. * @message: An incoming dbus message
  370. * @user_data: A pointer to a dbus control interface data structure
  371. * Returns: Whether or not the message was handled
  372. *
  373. * This function dispatches all incoming dbus messages for network interfaces,
  374. * or objects owned by them, such as scanned BSSIDs and configured networks.
  375. */
  376. static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
  377. DBusMessage *message,
  378. void *user_data)
  379. {
  380. struct wpa_supplicant *wpa_s = user_data;
  381. const char *method = dbus_message_get_member(message);
  382. const char *path = dbus_message_get_path(message);
  383. const char *msg_interface = dbus_message_get_interface(message);
  384. char *iface_obj_path = NULL;
  385. char *network = NULL;
  386. char *bssid = NULL;
  387. DBusMessage *reply = NULL;
  388. /* Caller must specify a message interface */
  389. if (!msg_interface)
  390. goto out;
  391. iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
  392. &bssid);
  393. if (iface_obj_path == NULL) {
  394. reply = wpas_dbus_new_invalid_iface_error(message);
  395. goto out;
  396. }
  397. /* Make sure the message's object path actually refers to the
  398. * wpa_supplicant structure it's supposed to (which is wpa_s)
  399. */
  400. if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
  401. iface_obj_path) != wpa_s) {
  402. reply = wpas_dbus_new_invalid_iface_error(message);
  403. goto out;
  404. }
  405. if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
  406. /* A method for one of this interface's configured networks */
  407. int nid = strtoul(network, NULL, 10);
  408. if (errno != EINVAL)
  409. reply = wpas_dispatch_network_method(message, wpa_s,
  410. nid);
  411. else
  412. reply = wpas_dbus_new_invalid_network_error(message);
  413. } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
  414. /* A method for one of this interface's scanned BSSIDs */
  415. reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
  416. } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
  417. /* A method for an interface only. */
  418. if (!strcmp(method, "scan"))
  419. reply = wpas_dbus_iface_scan(message, wpa_s);
  420. else if (!strcmp(method, "scanResults"))
  421. reply = wpas_dbus_iface_scan_results(message, wpa_s);
  422. else if (!strcmp(method, "addNetwork"))
  423. reply = wpas_dbus_iface_add_network(message, wpa_s);
  424. else if (!strcmp(method, "removeNetwork"))
  425. reply = wpas_dbus_iface_remove_network(message, wpa_s);
  426. else if (!strcmp(method, "selectNetwork"))
  427. reply = wpas_dbus_iface_select_network(message, wpa_s);
  428. else if (!strcmp(method, "capabilities"))
  429. reply = wpas_dbus_iface_capabilities(message, wpa_s);
  430. else if (!strcmp(method, "disconnect"))
  431. reply = wpas_dbus_iface_disconnect(message, wpa_s);
  432. else if (!strcmp(method, "setAPScan"))
  433. reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
  434. else if (!strcmp(method, "state"))
  435. reply = wpas_dbus_iface_get_state(message, wpa_s);
  436. else if (!strcmp(method, "setBlobs"))
  437. reply = wpas_dbus_iface_set_blobs(message, wpa_s);
  438. else if (!strcmp(method, "removeBlobs"))
  439. reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
  440. }
  441. /* If the message was handled, send back the reply */
  442. if (reply) {
  443. dbus_connection_send(connection, reply, NULL);
  444. dbus_message_unref(reply);
  445. }
  446. out:
  447. free(iface_obj_path);
  448. free(network);
  449. free(bssid);
  450. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  451. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  452. }
  453. /**
  454. * wpas_message_handler - dispatch incoming dbus messages
  455. * @connection: connection to the system message bus
  456. * @message: an incoming dbus message
  457. * @user_data: a pointer to a dbus control interface data structure
  458. * Returns: whether or not the message was handled
  459. *
  460. * This function dispatches all incoming dbus messages to the correct
  461. * handlers, depending on what the message's target object path is,
  462. * and what the method call is.
  463. */
  464. static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
  465. DBusMessage *message, void *user_data)
  466. {
  467. struct ctrl_iface_dbus_priv *ctrl_iface = user_data;
  468. const char *method;
  469. const char *path;
  470. const char *msg_interface;
  471. DBusMessage *reply = NULL;
  472. method = dbus_message_get_member(message);
  473. path = dbus_message_get_path(message);
  474. msg_interface = dbus_message_get_interface(message);
  475. if (!method || !path || !ctrl_iface || !msg_interface)
  476. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  477. /* Validate the method interface */
  478. if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
  479. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  480. if (!strcmp(path, WPAS_DBUS_PATH)) {
  481. /* dispatch methods against our global dbus interface here */
  482. if (!strcmp(method, "addInterface")) {
  483. reply = wpas_dbus_global_add_interface(
  484. message, ctrl_iface->global);
  485. } else if (!strcmp(method, "removeInterface")) {
  486. reply = wpas_dbus_global_remove_interface(
  487. message, ctrl_iface->global);
  488. } else if (!strcmp(method, "getInterface")) {
  489. reply = wpas_dbus_global_get_interface(
  490. message, ctrl_iface->global);
  491. }
  492. }
  493. /* If the message was handled, send back the reply */
  494. if (reply) {
  495. dbus_connection_send(connection, reply, NULL);
  496. dbus_message_unref(reply);
  497. }
  498. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  499. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  500. }
  501. /**
  502. * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
  503. * @wpa_s: %wpa_supplicant network interface data
  504. * Returns: 0 on success, -1 on failure
  505. *
  506. * Notify listeners that this interface has updated scan results.
  507. */
  508. void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
  509. {
  510. struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
  511. DBusMessage *_signal;
  512. const char *path;
  513. /* Do nothing if the control interface is not turned on */
  514. if (iface == NULL)
  515. return;
  516. path = wpa_supplicant_get_dbus_path(wpa_s);
  517. if (path == NULL) {
  518. perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
  519. "interface didn't have a dbus path");
  520. wpa_printf(MSG_ERROR,
  521. "wpa_supplicant_dbus_notify_scan_results[dbus]: "
  522. "interface didn't have a dbus path; can't send "
  523. "scan result signal.");
  524. return;
  525. }
  526. _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
  527. "ScanResultsAvailable");
  528. if (_signal == NULL) {
  529. perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
  530. "couldn't create dbus signal; likely out of memory");
  531. wpa_printf(MSG_ERROR, "dbus control interface: not enough "
  532. "memory to send scan results signal.");
  533. return;
  534. }
  535. dbus_connection_send(iface->con, _signal, NULL);
  536. dbus_message_unref(_signal);
  537. }
  538. /**
  539. * wpa_supplicant_dbus_notify_state_change - Send a state change signal
  540. * @wpa_s: %wpa_supplicant network interface data
  541. * @new_state: new state wpa_supplicant is entering
  542. * @old_state: old state wpa_supplicant is leaving
  543. * Returns: 0 on success, -1 on failure
  544. *
  545. * Notify listeners that wpa_supplicant has changed state
  546. */
  547. void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
  548. wpa_states new_state,
  549. wpa_states old_state)
  550. {
  551. struct ctrl_iface_dbus_priv *iface;
  552. DBusMessage *_signal = NULL;
  553. const char *path;
  554. const char *new_state_str, *old_state_str;
  555. /* Do nothing if the control interface is not turned on */
  556. if (wpa_s->global == NULL)
  557. return;
  558. iface = wpa_s->global->dbus_ctrl_iface;
  559. if (iface == NULL)
  560. return;
  561. /* Only send signal if state really changed */
  562. if (new_state == old_state)
  563. return;
  564. path = wpa_supplicant_get_dbus_path(wpa_s);
  565. if (path == NULL) {
  566. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  567. "interface didn't have a dbus path");
  568. wpa_printf(MSG_ERROR,
  569. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  570. "interface didn't have a dbus path; can't send "
  571. "signal.");
  572. return;
  573. }
  574. _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
  575. "StateChange");
  576. if (_signal == NULL) {
  577. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  578. "couldn't create dbus signal; likely out of memory");
  579. wpa_printf(MSG_ERROR,
  580. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  581. "couldn't create dbus signal; likely out of "
  582. "memory.");
  583. return;
  584. }
  585. new_state_str = wpa_supplicant_state_txt(new_state);
  586. old_state_str = wpa_supplicant_state_txt(old_state);
  587. if (new_state_str == NULL || old_state_str == NULL) {
  588. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  589. "couldn't convert state strings");
  590. wpa_printf(MSG_ERROR,
  591. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  592. "couldn't convert state strings.");
  593. goto out;
  594. }
  595. if (!dbus_message_append_args(_signal,
  596. DBUS_TYPE_STRING, &new_state_str,
  597. DBUS_TYPE_STRING, &old_state_str,
  598. DBUS_TYPE_INVALID)) {
  599. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  600. "not enough memory to construct state change signal.");
  601. wpa_printf(MSG_ERROR,
  602. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  603. "not enough memory to construct state change "
  604. "signal.");
  605. goto out;
  606. }
  607. dbus_connection_send(iface->con, _signal, NULL);
  608. out:
  609. dbus_message_unref(_signal);
  610. }
  611. /**
  612. * integrate_with_eloop - Register our mainloop integration with dbus
  613. * @connection: connection to the system message bus
  614. * @iface: a dbus control interface data structure
  615. * Returns: 0 on success, -1 on failure
  616. *
  617. * We register our mainloop integration functions with dbus here.
  618. */
  619. static int integrate_with_eloop(DBusConnection *connection,
  620. struct ctrl_iface_dbus_priv *iface)
  621. {
  622. if (!dbus_connection_set_watch_functions(connection, add_watch,
  623. remove_watch, watch_toggled,
  624. iface, NULL)) {
  625. perror("dbus_connection_set_watch_functions[dbus]");
  626. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  627. return -1;
  628. }
  629. if (!dbus_connection_set_timeout_functions(connection, add_timeout,
  630. remove_timeout,
  631. timeout_toggled, iface,
  632. NULL)) {
  633. perror("dbus_connection_set_timeout_functions[dbus]");
  634. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  635. return -1;
  636. }
  637. if (connection_setup_wakeup_main(iface) < 0) {
  638. perror("connection_setup_wakeup_main[dbus]");
  639. wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
  640. return -1;
  641. }
  642. return 0;
  643. }
  644. /**
  645. * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
  646. * claiming bus name
  647. * @eloop_ctx: the DBusConnection to dispatch on
  648. * @timeout_ctx: unused
  649. *
  650. * If clients are quick to notice that wpa_supplicant claimed its bus name,
  651. * there may have been messages that came in before initialization was
  652. * all finished. Dispatch those here.
  653. */
  654. static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
  655. {
  656. DBusConnection *con = eloop_ctx;
  657. while (dbus_connection_get_dispatch_status(con) ==
  658. DBUS_DISPATCH_DATA_REMAINS)
  659. dbus_connection_dispatch(con);
  660. }
  661. /**
  662. * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
  663. * @global: Pointer to global data from wpa_supplicant_init()
  664. * Returns: Pointer to dbus_ctrl_iface date or %NULL on failure
  665. *
  666. * Initialize the dbus control interface and start receiving commands from
  667. * external programs over the bus.
  668. */
  669. struct ctrl_iface_dbus_priv *
  670. wpa_supplicant_dbus_ctrl_iface_init(struct wpa_global *global)
  671. {
  672. struct ctrl_iface_dbus_priv *iface;
  673. DBusError error;
  674. int ret = -1;
  675. DBusObjectPathVTable wpas_vtable = {
  676. NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
  677. };
  678. iface = os_zalloc(sizeof(struct ctrl_iface_dbus_priv));
  679. if (iface == NULL)
  680. return NULL;
  681. iface->global = global;
  682. /* Get a reference to the system bus */
  683. dbus_error_init(&error);
  684. iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
  685. dbus_error_free(&error);
  686. if (!iface->con) {
  687. perror("dbus_bus_get[ctrl_iface_dbus]");
  688. wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
  689. goto fail;
  690. }
  691. /* Tell dbus about our mainloop integration functions */
  692. if (integrate_with_eloop(iface->con, iface))
  693. goto fail;
  694. /* Register the message handler for the global dbus interface */
  695. if (!dbus_connection_register_object_path(iface->con,
  696. WPAS_DBUS_PATH, &wpas_vtable,
  697. iface)) {
  698. perror("dbus_connection_register_object_path[dbus]");
  699. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  700. "handler.");
  701. goto fail;
  702. }
  703. /* Register our service with the message bus */
  704. dbus_error_init(&error);
  705. switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
  706. 0, &error)) {
  707. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  708. ret = 0;
  709. break;
  710. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  711. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  712. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  713. perror("dbus_bus_request_name[dbus]");
  714. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  715. "already registered.");
  716. break;
  717. default:
  718. perror("dbus_bus_request_name[dbus]");
  719. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  720. "%s %s.", error.name, error.message);
  721. break;
  722. }
  723. dbus_error_free(&error);
  724. if (ret != 0)
  725. goto fail;
  726. wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
  727. "'.");
  728. /*
  729. * Dispatch initial DBus messages that may have come in since the bus
  730. * name was claimed above. Happens when clients are quick to notice the
  731. * wpa_supplicant service.
  732. *
  733. * FIXME: is there a better solution to this problem?
  734. */
  735. eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
  736. iface->con, NULL);
  737. return iface;
  738. fail:
  739. wpa_supplicant_dbus_ctrl_iface_deinit(iface);
  740. return NULL;
  741. }
  742. /**
  743. * wpa_supplicant_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
  744. * @iface: Pointer to dbus private data from
  745. * wpa_supplicant_dbus_ctrl_iface_init()
  746. *
  747. * Deinitialize the dbus control interface that was initialized with
  748. * wpa_supplicant_dbus_ctrl_iface_init().
  749. */
  750. void wpa_supplicant_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_priv *iface)
  751. {
  752. if (iface == NULL)
  753. return;
  754. if (iface->con) {
  755. eloop_cancel_timeout(dispatch_initial_dbus_messages,
  756. iface->con, NULL);
  757. dbus_connection_set_watch_functions(iface->con, NULL, NULL,
  758. NULL, NULL, NULL);
  759. dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
  760. NULL, NULL, NULL);
  761. dbus_connection_unref(iface->con);
  762. }
  763. memset(iface, 0, sizeof(struct ctrl_iface_dbus_priv));
  764. free(iface);
  765. }
  766. /**
  767. * wpas_dbus_register_new_iface - Register a new interface with dbus
  768. * @global: Global %wpa_supplicant data
  769. * @wpa_s: %wpa_supplicant interface description structure to register
  770. * Returns: 0 on success, -1 on error
  771. *
  772. * Registers a new interface with dbus and assigns it a dbus object path.
  773. */
  774. int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
  775. {
  776. struct ctrl_iface_dbus_priv *ctrl_iface =
  777. wpa_s->global->dbus_ctrl_iface;
  778. DBusConnection * con;
  779. u32 next;
  780. DBusObjectPathVTable vtable = {
  781. NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
  782. };
  783. char *path;
  784. int ret = -1;
  785. /* Do nothing if the control interface is not turned on */
  786. if (ctrl_iface == NULL)
  787. return 0;
  788. con = ctrl_iface->con;
  789. next = wpa_supplicant_dbus_next_objid(ctrl_iface);
  790. /* Create and set the interface's object path */
  791. path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  792. if (path == NULL)
  793. return -1;
  794. snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  795. WPAS_DBUS_PATH_INTERFACES "/%u",
  796. next);
  797. if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
  798. wpa_printf(MSG_DEBUG,
  799. "Failed to set dbus path for interface %s",
  800. wpa_s->ifname);
  801. goto out;
  802. }
  803. /* Register the message handler for the interface functions */
  804. if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
  805. perror("wpas_dbus_register_iface [dbus]");
  806. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  807. "handler for interface %s.", wpa_s->ifname);
  808. goto out;
  809. }
  810. ret = 0;
  811. out:
  812. free(path);
  813. return ret;
  814. }
  815. /**
  816. * wpas_dbus_unregister_iface - Unregister an interface from dbus
  817. * @wpa_s: wpa_supplicant interface structure
  818. * Returns: 0 on success, -1 on failure
  819. *
  820. * Unregisters the interface with dbus
  821. */
  822. int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
  823. {
  824. struct ctrl_iface_dbus_priv *ctrl_iface;
  825. DBusConnection *con;
  826. const char *path;
  827. /* Do nothing if the control interface is not turned on */
  828. if (wpa_s == NULL || wpa_s->global == NULL)
  829. return 0;
  830. ctrl_iface = wpa_s->global->dbus_ctrl_iface;
  831. if (ctrl_iface == NULL)
  832. return 0;
  833. con = ctrl_iface->con;
  834. path = wpa_supplicant_get_dbus_path(wpa_s);
  835. if (!dbus_connection_unregister_object_path(con, path))
  836. return -1;
  837. free(wpa_s->dbus_path);
  838. wpa_s->dbus_path = NULL;
  839. return 0;
  840. }
  841. /**
  842. * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
  843. * @global: Pointer to global data from wpa_supplicant_init()
  844. * @path: Pointer to a dbus object path representing an interface
  845. * Returns: Pointer to the interface or %NULL if not found
  846. */
  847. struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
  848. struct wpa_global *global, const char *path)
  849. {
  850. struct wpa_supplicant *wpa_s;
  851. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  852. if (strcmp(wpa_s->dbus_path, path) == 0)
  853. return wpa_s;
  854. }
  855. return NULL;
  856. }
  857. /**
  858. * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
  859. * @wpa_s: wpa_supplicant interface structure
  860. * @path: dbus path to set on the interface
  861. * Returns: 0 on succes, -1 on error
  862. */
  863. int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
  864. const char *path)
  865. {
  866. u32 len = strlen (path);
  867. if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
  868. return -1;
  869. if (wpa_s->dbus_path)
  870. return -1;
  871. wpa_s->dbus_path = strdup(path);
  872. return 0;
  873. }
  874. /**
  875. * wpa_supplicant_get_dbus_path - Get an interface's dbus path
  876. * @wpa_s: %wpa_supplicant interface structure
  877. * Returns: Interface's dbus object path, or %NULL on error
  878. */
  879. const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
  880. {
  881. return wpa_s->dbus_path;
  882. }