ctrl_iface_dbus.c 31 KB

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