ctrl_iface_dbus.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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, "setSmartcardModules"))
  435. reply = wpas_dbus_iface_set_smartcard_modules(message,
  436. wpa_s);
  437. else if (!strcmp(method, "state"))
  438. reply = wpas_dbus_iface_get_state(message, wpa_s);
  439. else if (!strcmp(method, "setBlobs"))
  440. reply = wpas_dbus_iface_set_blobs(message, wpa_s);
  441. else if (!strcmp(method, "removeBlobs"))
  442. reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
  443. }
  444. /* If the message was handled, send back the reply */
  445. if (reply) {
  446. dbus_connection_send(connection, reply, NULL);
  447. dbus_message_unref(reply);
  448. }
  449. out:
  450. free(iface_obj_path);
  451. free(network);
  452. free(bssid);
  453. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  454. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  455. }
  456. /**
  457. * wpas_message_handler - dispatch incoming dbus messages
  458. * @connection: connection to the system message bus
  459. * @message: an incoming dbus message
  460. * @user_data: a pointer to a dbus control interface data structure
  461. * Returns: whether or not the message was handled
  462. *
  463. * This function dispatches all incoming dbus messages to the correct
  464. * handlers, depending on what the message's target object path is,
  465. * and what the method call is.
  466. */
  467. static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
  468. DBusMessage *message, void *user_data)
  469. {
  470. struct ctrl_iface_dbus_priv *ctrl_iface = user_data;
  471. const char *method;
  472. const char *path;
  473. const char *msg_interface;
  474. DBusMessage *reply = NULL;
  475. method = dbus_message_get_member(message);
  476. path = dbus_message_get_path(message);
  477. msg_interface = dbus_message_get_interface(message);
  478. if (!method || !path || !ctrl_iface || !msg_interface)
  479. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  480. /* Validate the method interface */
  481. if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
  482. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  483. if (!strcmp(path, WPAS_DBUS_PATH)) {
  484. /* dispatch methods against our global dbus interface here */
  485. if (!strcmp(method, "addInterface")) {
  486. reply = wpas_dbus_global_add_interface(
  487. message, ctrl_iface->global);
  488. } else if (!strcmp(method, "removeInterface")) {
  489. reply = wpas_dbus_global_remove_interface(
  490. message, ctrl_iface->global);
  491. } else if (!strcmp(method, "getInterface")) {
  492. reply = wpas_dbus_global_get_interface(
  493. message, ctrl_iface->global);
  494. }
  495. }
  496. /* If the message was handled, send back the reply */
  497. if (reply) {
  498. dbus_connection_send(connection, reply, NULL);
  499. dbus_message_unref(reply);
  500. }
  501. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  502. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  503. }
  504. /**
  505. * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
  506. * @wpa_s: %wpa_supplicant network interface data
  507. * Returns: 0 on success, -1 on failure
  508. *
  509. * Notify listeners that this interface has updated scan results.
  510. */
  511. void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
  512. {
  513. struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
  514. DBusMessage *_signal;
  515. const char *path;
  516. /* Do nothing if the control interface is not turned on */
  517. if (iface == NULL)
  518. return;
  519. path = wpa_supplicant_get_dbus_path(wpa_s);
  520. if (path == NULL) {
  521. perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
  522. "interface didn't have a dbus path");
  523. wpa_printf(MSG_ERROR,
  524. "wpa_supplicant_dbus_notify_scan_results[dbus]: "
  525. "interface didn't have a dbus path; can't send "
  526. "scan result signal.");
  527. return;
  528. }
  529. _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
  530. "ScanResultsAvailable");
  531. if (_signal == NULL) {
  532. perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
  533. "couldn't create dbus signal; likely out of memory");
  534. wpa_printf(MSG_ERROR, "dbus control interface: not enough "
  535. "memory to send scan results signal.");
  536. return;
  537. }
  538. dbus_connection_send(iface->con, _signal, NULL);
  539. dbus_message_unref(_signal);
  540. }
  541. /**
  542. * wpa_supplicant_dbus_notify_state_change - Send a state change signal
  543. * @wpa_s: %wpa_supplicant network interface data
  544. * @new_state: new state wpa_supplicant is entering
  545. * @old_state: old state wpa_supplicant is leaving
  546. * Returns: 0 on success, -1 on failure
  547. *
  548. * Notify listeners that wpa_supplicant has changed state
  549. */
  550. void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
  551. wpa_states new_state,
  552. wpa_states old_state)
  553. {
  554. struct ctrl_iface_dbus_priv *iface;
  555. DBusMessage *_signal = NULL;
  556. const char *path;
  557. const char *new_state_str, *old_state_str;
  558. /* Do nothing if the control interface is not turned on */
  559. if (wpa_s->global == NULL)
  560. return;
  561. iface = wpa_s->global->dbus_ctrl_iface;
  562. if (iface == NULL)
  563. return;
  564. /* Only send signal if state really changed */
  565. if (new_state == old_state)
  566. return;
  567. path = wpa_supplicant_get_dbus_path(wpa_s);
  568. if (path == NULL) {
  569. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  570. "interface didn't have a dbus path");
  571. wpa_printf(MSG_ERROR,
  572. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  573. "interface didn't have a dbus path; can't send "
  574. "signal.");
  575. return;
  576. }
  577. _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
  578. "StateChange");
  579. if (_signal == NULL) {
  580. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  581. "couldn't create dbus signal; likely out of memory");
  582. wpa_printf(MSG_ERROR,
  583. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  584. "couldn't create dbus signal; likely out of "
  585. "memory.");
  586. return;
  587. }
  588. new_state_str = wpa_supplicant_state_txt(new_state);
  589. old_state_str = wpa_supplicant_state_txt(old_state);
  590. if (new_state_str == NULL || old_state_str == NULL) {
  591. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  592. "couldn't convert state strings");
  593. wpa_printf(MSG_ERROR,
  594. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  595. "couldn't convert state strings.");
  596. goto out;
  597. }
  598. if (!dbus_message_append_args(_signal,
  599. DBUS_TYPE_STRING, &new_state_str,
  600. DBUS_TYPE_STRING, &old_state_str,
  601. DBUS_TYPE_INVALID)) {
  602. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  603. "not enough memory to construct state change signal.");
  604. wpa_printf(MSG_ERROR,
  605. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  606. "not enough memory to construct state change "
  607. "signal.");
  608. goto out;
  609. }
  610. dbus_connection_send(iface->con, _signal, NULL);
  611. out:
  612. dbus_message_unref(_signal);
  613. }
  614. /**
  615. * integrate_with_eloop - Register our mainloop integration with dbus
  616. * @connection: connection to the system message bus
  617. * @iface: a dbus control interface data structure
  618. * Returns: 0 on success, -1 on failure
  619. *
  620. * We register our mainloop integration functions with dbus here.
  621. */
  622. static int integrate_with_eloop(DBusConnection *connection,
  623. struct ctrl_iface_dbus_priv *iface)
  624. {
  625. if (!dbus_connection_set_watch_functions(connection, add_watch,
  626. remove_watch, watch_toggled,
  627. iface, NULL)) {
  628. perror("dbus_connection_set_watch_functions[dbus]");
  629. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  630. return -1;
  631. }
  632. if (!dbus_connection_set_timeout_functions(connection, add_timeout,
  633. remove_timeout,
  634. timeout_toggled, iface,
  635. NULL)) {
  636. perror("dbus_connection_set_timeout_functions[dbus]");
  637. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  638. return -1;
  639. }
  640. if (connection_setup_wakeup_main(iface) < 0) {
  641. perror("connection_setup_wakeup_main[dbus]");
  642. wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
  643. return -1;
  644. }
  645. return 0;
  646. }
  647. /**
  648. * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
  649. * claiming bus name
  650. * @eloop_ctx: the DBusConnection to dispatch on
  651. * @timeout_ctx: unused
  652. *
  653. * If clients are quick to notice that wpa_supplicant claimed its bus name,
  654. * there may have been messages that came in before initialization was
  655. * all finished. Dispatch those here.
  656. */
  657. static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
  658. {
  659. DBusConnection *con = eloop_ctx;
  660. while (dbus_connection_get_dispatch_status(con) ==
  661. DBUS_DISPATCH_DATA_REMAINS)
  662. dbus_connection_dispatch(con);
  663. }
  664. /**
  665. * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
  666. * @global: Pointer to global data from wpa_supplicant_init()
  667. * Returns: Pointer to dbus_ctrl_iface date or %NULL on failure
  668. *
  669. * Initialize the dbus control interface and start receiving commands from
  670. * external programs over the bus.
  671. */
  672. struct ctrl_iface_dbus_priv *
  673. wpa_supplicant_dbus_ctrl_iface_init(struct wpa_global *global)
  674. {
  675. struct ctrl_iface_dbus_priv *iface;
  676. DBusError error;
  677. int ret = -1;
  678. DBusObjectPathVTable wpas_vtable = {
  679. NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
  680. };
  681. iface = os_zalloc(sizeof(struct ctrl_iface_dbus_priv));
  682. if (iface == NULL)
  683. return NULL;
  684. iface->global = global;
  685. /* Get a reference to the system bus */
  686. dbus_error_init(&error);
  687. iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
  688. dbus_error_free(&error);
  689. if (!iface->con) {
  690. perror("dbus_bus_get[ctrl_iface_dbus]");
  691. wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
  692. goto fail;
  693. }
  694. /* Tell dbus about our mainloop integration functions */
  695. if (integrate_with_eloop(iface->con, iface))
  696. goto fail;
  697. /* Register the message handler for the global dbus interface */
  698. if (!dbus_connection_register_object_path(iface->con,
  699. WPAS_DBUS_PATH, &wpas_vtable,
  700. iface)) {
  701. perror("dbus_connection_register_object_path[dbus]");
  702. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  703. "handler.");
  704. goto fail;
  705. }
  706. /* Register our service with the message bus */
  707. dbus_error_init(&error);
  708. switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
  709. 0, &error)) {
  710. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  711. ret = 0;
  712. break;
  713. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  714. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  715. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  716. perror("dbus_bus_request_name[dbus]");
  717. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  718. "already registered.");
  719. break;
  720. default:
  721. perror("dbus_bus_request_name[dbus]");
  722. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  723. "%s %s.", error.name, error.message);
  724. break;
  725. }
  726. dbus_error_free(&error);
  727. if (ret != 0)
  728. goto fail;
  729. wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
  730. "'.");
  731. /*
  732. * Dispatch initial DBus messages that may have come in since the bus
  733. * name was claimed above. Happens when clients are quick to notice the
  734. * wpa_supplicant service.
  735. *
  736. * FIXME: is there a better solution to this problem?
  737. */
  738. eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
  739. iface->con, NULL);
  740. return iface;
  741. fail:
  742. wpa_supplicant_dbus_ctrl_iface_deinit(iface);
  743. return NULL;
  744. }
  745. /**
  746. * wpa_supplicant_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
  747. * @iface: Pointer to dbus private data from
  748. * wpa_supplicant_dbus_ctrl_iface_init()
  749. *
  750. * Deinitialize the dbus control interface that was initialized with
  751. * wpa_supplicant_dbus_ctrl_iface_init().
  752. */
  753. void wpa_supplicant_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_priv *iface)
  754. {
  755. if (iface == NULL)
  756. return;
  757. if (iface->con) {
  758. eloop_cancel_timeout(dispatch_initial_dbus_messages,
  759. iface->con, NULL);
  760. dbus_connection_set_watch_functions(iface->con, NULL, NULL,
  761. NULL, NULL, NULL);
  762. dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
  763. NULL, NULL, NULL);
  764. dbus_connection_unref(iface->con);
  765. }
  766. memset(iface, 0, sizeof(struct ctrl_iface_dbus_priv));
  767. free(iface);
  768. }
  769. /**
  770. * wpas_dbus_register_new_iface - Register a new interface with dbus
  771. * @global: Global %wpa_supplicant data
  772. * @wpa_s: %wpa_supplicant interface description structure to register
  773. * Returns: 0 on success, -1 on error
  774. *
  775. * Registers a new interface with dbus and assigns it a dbus object path.
  776. */
  777. int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
  778. {
  779. struct ctrl_iface_dbus_priv *ctrl_iface =
  780. wpa_s->global->dbus_ctrl_iface;
  781. DBusConnection * con;
  782. u32 next;
  783. DBusObjectPathVTable vtable = {
  784. NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
  785. };
  786. char *path;
  787. int ret = -1;
  788. /* Do nothing if the control interface is not turned on */
  789. if (ctrl_iface == NULL)
  790. return 0;
  791. con = ctrl_iface->con;
  792. next = wpa_supplicant_dbus_next_objid(ctrl_iface);
  793. /* Create and set the interface's object path */
  794. path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  795. if (path == NULL)
  796. return -1;
  797. snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  798. WPAS_DBUS_PATH_INTERFACES "/%u",
  799. next);
  800. if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
  801. wpa_printf(MSG_DEBUG,
  802. "Failed to set dbus path for interface %s",
  803. wpa_s->ifname);
  804. goto out;
  805. }
  806. /* Register the message handler for the interface functions */
  807. if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
  808. perror("wpas_dbus_register_iface [dbus]");
  809. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  810. "handler for interface %s.", wpa_s->ifname);
  811. goto out;
  812. }
  813. ret = 0;
  814. out:
  815. free(path);
  816. return ret;
  817. }
  818. /**
  819. * wpas_dbus_unregister_iface - Unregister an interface from dbus
  820. * @wpa_s: wpa_supplicant interface structure
  821. * Returns: 0 on success, -1 on failure
  822. *
  823. * Unregisters the interface with dbus
  824. */
  825. int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
  826. {
  827. struct ctrl_iface_dbus_priv *ctrl_iface;
  828. DBusConnection *con;
  829. const char *path;
  830. /* Do nothing if the control interface is not turned on */
  831. if (wpa_s == NULL || wpa_s->global == NULL)
  832. return 0;
  833. ctrl_iface = wpa_s->global->dbus_ctrl_iface;
  834. if (ctrl_iface == NULL)
  835. return 0;
  836. con = ctrl_iface->con;
  837. path = wpa_supplicant_get_dbus_path(wpa_s);
  838. if (!dbus_connection_unregister_object_path(con, path))
  839. return -1;
  840. free(wpa_s->dbus_path);
  841. wpa_s->dbus_path = NULL;
  842. return 0;
  843. }
  844. /**
  845. * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
  846. * @global: Pointer to global data from wpa_supplicant_init()
  847. * @path: Pointer to a dbus object path representing an interface
  848. * Returns: Pointer to the interface or %NULL if not found
  849. */
  850. struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
  851. struct wpa_global *global, const char *path)
  852. {
  853. struct wpa_supplicant *wpa_s;
  854. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  855. if (strcmp(wpa_s->dbus_path, path) == 0)
  856. return wpa_s;
  857. }
  858. return NULL;
  859. }
  860. /**
  861. * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
  862. * @wpa_s: wpa_supplicant interface structure
  863. * @path: dbus path to set on the interface
  864. * Returns: 0 on succes, -1 on error
  865. */
  866. int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
  867. const char *path)
  868. {
  869. u32 len = strlen (path);
  870. if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
  871. return -1;
  872. if (wpa_s->dbus_path)
  873. return -1;
  874. wpa_s->dbus_path = strdup(path);
  875. return 0;
  876. }
  877. /**
  878. * wpa_supplicant_get_dbus_path - Get an interface's dbus path
  879. * @wpa_s: %wpa_supplicant interface structure
  880. * Returns: Interface's dbus object path, or %NULL on error
  881. */
  882. const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
  883. {
  884. return wpa_s->dbus_path;
  885. }