ctrl_iface_dbus.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. * @wpa_s: %wpa_supplicant interface description structure to register
  772. * Returns: 0 on success, -1 on error
  773. *
  774. * Registers a new interface with dbus and assigns it a dbus object path.
  775. */
  776. int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
  777. {
  778. struct ctrl_iface_dbus_priv *ctrl_iface =
  779. wpa_s->global->dbus_ctrl_iface;
  780. DBusConnection * con;
  781. u32 next;
  782. DBusObjectPathVTable vtable = {
  783. NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
  784. };
  785. char *path;
  786. int ret = -1;
  787. /* Do nothing if the control interface is not turned on */
  788. if (ctrl_iface == NULL)
  789. return 0;
  790. con = ctrl_iface->con;
  791. next = wpa_supplicant_dbus_next_objid(ctrl_iface);
  792. /* Create and set the interface's object path */
  793. path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  794. if (path == NULL)
  795. return -1;
  796. snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  797. WPAS_DBUS_PATH_INTERFACES "/%u",
  798. next);
  799. if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
  800. wpa_printf(MSG_DEBUG,
  801. "Failed to set dbus path for interface %s",
  802. wpa_s->ifname);
  803. goto out;
  804. }
  805. /* Register the message handler for the interface functions */
  806. if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
  807. perror("wpas_dbus_register_iface [dbus]");
  808. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  809. "handler for interface %s.", wpa_s->ifname);
  810. goto out;
  811. }
  812. ret = 0;
  813. out:
  814. free(path);
  815. return ret;
  816. }
  817. /**
  818. * wpas_dbus_unregister_iface - Unregister an interface from dbus
  819. * @wpa_s: wpa_supplicant interface structure
  820. * Returns: 0 on success, -1 on failure
  821. *
  822. * Unregisters the interface with dbus
  823. */
  824. int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
  825. {
  826. struct ctrl_iface_dbus_priv *ctrl_iface;
  827. DBusConnection *con;
  828. const char *path;
  829. /* Do nothing if the control interface is not turned on */
  830. if (wpa_s == NULL || wpa_s->global == NULL)
  831. return 0;
  832. ctrl_iface = wpa_s->global->dbus_ctrl_iface;
  833. if (ctrl_iface == NULL)
  834. return 0;
  835. con = ctrl_iface->con;
  836. path = wpa_supplicant_get_dbus_path(wpa_s);
  837. if (!dbus_connection_unregister_object_path(con, path))
  838. return -1;
  839. free(wpa_s->dbus_path);
  840. wpa_s->dbus_path = NULL;
  841. return 0;
  842. }
  843. /**
  844. * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
  845. * @global: Pointer to global data from wpa_supplicant_init()
  846. * @path: Pointer to a dbus object path representing an interface
  847. * Returns: Pointer to the interface or %NULL if not found
  848. */
  849. struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
  850. struct wpa_global *global, const char *path)
  851. {
  852. struct wpa_supplicant *wpa_s;
  853. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  854. if (strcmp(wpa_s->dbus_path, path) == 0)
  855. return wpa_s;
  856. }
  857. return NULL;
  858. }
  859. /**
  860. * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
  861. * @wpa_s: wpa_supplicant interface structure
  862. * @path: dbus path to set on the interface
  863. * Returns: 0 on succes, -1 on error
  864. */
  865. int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
  866. const char *path)
  867. {
  868. u32 len = strlen (path);
  869. if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
  870. return -1;
  871. if (wpa_s->dbus_path)
  872. return -1;
  873. wpa_s->dbus_path = strdup(path);
  874. return 0;
  875. }
  876. /**
  877. * wpa_supplicant_get_dbus_path - Get an interface's dbus path
  878. * @wpa_s: %wpa_supplicant interface structure
  879. * Returns: Interface's dbus object path, or %NULL on error
  880. */
  881. const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
  882. {
  883. return wpa_s->dbus_path;
  884. }