ctrl_iface_dbus.c 34 KB

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