ctrl_iface_dbus.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. #endif /* CONFIG_WPS */
  728. /**
  729. * integrate_with_eloop - Register our mainloop integration with dbus
  730. * @connection: connection to the system message bus
  731. * @iface: a dbus control interface data structure
  732. * Returns: 0 on success, -1 on failure
  733. *
  734. * We register our mainloop integration functions with dbus here.
  735. */
  736. static int integrate_with_eloop(DBusConnection *connection,
  737. struct ctrl_iface_dbus_priv *iface)
  738. {
  739. if (!dbus_connection_set_watch_functions(connection, add_watch,
  740. remove_watch, watch_toggled,
  741. iface, NULL)) {
  742. perror("dbus_connection_set_watch_functions[dbus]");
  743. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  744. return -1;
  745. }
  746. if (!dbus_connection_set_timeout_functions(connection, add_timeout,
  747. remove_timeout,
  748. timeout_toggled, iface,
  749. NULL)) {
  750. perror("dbus_connection_set_timeout_functions[dbus]");
  751. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  752. return -1;
  753. }
  754. if (connection_setup_wakeup_main(iface) < 0) {
  755. perror("connection_setup_wakeup_main[dbus]");
  756. wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
  757. return -1;
  758. }
  759. return 0;
  760. }
  761. /**
  762. * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
  763. * claiming bus name
  764. * @eloop_ctx: the DBusConnection to dispatch on
  765. * @timeout_ctx: unused
  766. *
  767. * If clients are quick to notice that wpa_supplicant claimed its bus name,
  768. * there may have been messages that came in before initialization was
  769. * all finished. Dispatch those here.
  770. */
  771. static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
  772. {
  773. DBusConnection *con = eloop_ctx;
  774. while (dbus_connection_get_dispatch_status(con) ==
  775. DBUS_DISPATCH_DATA_REMAINS)
  776. dbus_connection_dispatch(con);
  777. }
  778. /**
  779. * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
  780. * @global: Pointer to global data from wpa_supplicant_init()
  781. * Returns: Pointer to dbus_ctrl_iface date or %NULL on failure
  782. *
  783. * Initialize the dbus control interface and start receiving commands from
  784. * external programs over the bus.
  785. */
  786. struct ctrl_iface_dbus_priv *
  787. wpa_supplicant_dbus_ctrl_iface_init(struct wpa_global *global)
  788. {
  789. struct ctrl_iface_dbus_priv *iface;
  790. DBusError error;
  791. int ret = -1;
  792. DBusObjectPathVTable wpas_vtable = {
  793. NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
  794. };
  795. iface = os_zalloc(sizeof(struct ctrl_iface_dbus_priv));
  796. if (iface == NULL)
  797. return NULL;
  798. iface->global = global;
  799. /* Get a reference to the system bus */
  800. dbus_error_init(&error);
  801. iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
  802. dbus_error_free(&error);
  803. if (!iface->con) {
  804. perror("dbus_bus_get[ctrl_iface_dbus]");
  805. wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
  806. goto fail;
  807. }
  808. /* Tell dbus about our mainloop integration functions */
  809. if (integrate_with_eloop(iface->con, iface))
  810. goto fail;
  811. /* Register the message handler for the global dbus interface */
  812. if (!dbus_connection_register_object_path(iface->con,
  813. WPAS_DBUS_PATH, &wpas_vtable,
  814. iface)) {
  815. perror("dbus_connection_register_object_path[dbus]");
  816. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  817. "handler.");
  818. goto fail;
  819. }
  820. /* Register our service with the message bus */
  821. dbus_error_init(&error);
  822. switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
  823. 0, &error)) {
  824. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  825. ret = 0;
  826. break;
  827. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  828. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  829. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  830. perror("dbus_bus_request_name[dbus]");
  831. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  832. "already registered.");
  833. break;
  834. default:
  835. perror("dbus_bus_request_name[dbus]");
  836. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  837. "%s %s.", error.name, error.message);
  838. break;
  839. }
  840. dbus_error_free(&error);
  841. if (ret != 0)
  842. goto fail;
  843. wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
  844. "'.");
  845. /*
  846. * Dispatch initial DBus messages that may have come in since the bus
  847. * name was claimed above. Happens when clients are quick to notice the
  848. * wpa_supplicant service.
  849. *
  850. * FIXME: is there a better solution to this problem?
  851. */
  852. eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
  853. iface->con, NULL);
  854. return iface;
  855. fail:
  856. wpa_supplicant_dbus_ctrl_iface_deinit(iface);
  857. return NULL;
  858. }
  859. /**
  860. * wpa_supplicant_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
  861. * @iface: Pointer to dbus private data from
  862. * wpa_supplicant_dbus_ctrl_iface_init()
  863. *
  864. * Deinitialize the dbus control interface that was initialized with
  865. * wpa_supplicant_dbus_ctrl_iface_init().
  866. */
  867. void wpa_supplicant_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_priv *iface)
  868. {
  869. if (iface == NULL)
  870. return;
  871. if (iface->con) {
  872. eloop_cancel_timeout(dispatch_initial_dbus_messages,
  873. iface->con, NULL);
  874. dbus_connection_set_watch_functions(iface->con, NULL, NULL,
  875. NULL, NULL, NULL);
  876. dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
  877. NULL, NULL, NULL);
  878. dbus_connection_unref(iface->con);
  879. }
  880. memset(iface, 0, sizeof(struct ctrl_iface_dbus_priv));
  881. free(iface);
  882. }
  883. /**
  884. * wpas_dbus_register_new_iface - Register a new interface with dbus
  885. * @wpa_s: %wpa_supplicant interface description structure to register
  886. * Returns: 0 on success, -1 on error
  887. *
  888. * Registers a new interface with dbus and assigns it a dbus object path.
  889. */
  890. int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
  891. {
  892. struct ctrl_iface_dbus_priv *ctrl_iface =
  893. wpa_s->global->dbus_ctrl_iface;
  894. DBusConnection * con;
  895. u32 next;
  896. DBusObjectPathVTable vtable = {
  897. NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
  898. };
  899. char *path;
  900. int ret = -1;
  901. /* Do nothing if the control interface is not turned on */
  902. if (ctrl_iface == NULL)
  903. return 0;
  904. con = ctrl_iface->con;
  905. next = wpa_supplicant_dbus_next_objid(ctrl_iface);
  906. /* Create and set the interface's object path */
  907. path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  908. if (path == NULL)
  909. return -1;
  910. snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  911. WPAS_DBUS_PATH_INTERFACES "/%u",
  912. next);
  913. if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
  914. wpa_printf(MSG_DEBUG,
  915. "Failed to set dbus path for interface %s",
  916. wpa_s->ifname);
  917. goto out;
  918. }
  919. /* Register the message handler for the interface functions */
  920. if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
  921. perror("wpas_dbus_register_iface [dbus]");
  922. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  923. "handler for interface %s.", wpa_s->ifname);
  924. goto out;
  925. }
  926. ret = 0;
  927. out:
  928. free(path);
  929. return ret;
  930. }
  931. /**
  932. * wpas_dbus_unregister_iface - Unregister an interface from dbus
  933. * @wpa_s: wpa_supplicant interface structure
  934. * Returns: 0 on success, -1 on failure
  935. *
  936. * Unregisters the interface with dbus
  937. */
  938. int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
  939. {
  940. struct ctrl_iface_dbus_priv *ctrl_iface;
  941. DBusConnection *con;
  942. const char *path;
  943. /* Do nothing if the control interface is not turned on */
  944. if (wpa_s == NULL || wpa_s->global == NULL)
  945. return 0;
  946. ctrl_iface = wpa_s->global->dbus_ctrl_iface;
  947. if (ctrl_iface == NULL)
  948. return 0;
  949. con = ctrl_iface->con;
  950. path = wpa_supplicant_get_dbus_path(wpa_s);
  951. if (!dbus_connection_unregister_object_path(con, path))
  952. return -1;
  953. free(wpa_s->dbus_path);
  954. wpa_s->dbus_path = NULL;
  955. return 0;
  956. }
  957. /**
  958. * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
  959. * @global: Pointer to global data from wpa_supplicant_init()
  960. * @path: Pointer to a dbus object path representing an interface
  961. * Returns: Pointer to the interface or %NULL if not found
  962. */
  963. struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
  964. struct wpa_global *global, const char *path)
  965. {
  966. struct wpa_supplicant *wpa_s;
  967. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  968. if (strcmp(wpa_s->dbus_path, path) == 0)
  969. return wpa_s;
  970. }
  971. return NULL;
  972. }
  973. /**
  974. * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
  975. * @wpa_s: wpa_supplicant interface structure
  976. * @path: dbus path to set on the interface
  977. * Returns: 0 on succes, -1 on error
  978. */
  979. int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
  980. const char *path)
  981. {
  982. u32 len = strlen (path);
  983. if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
  984. return -1;
  985. if (wpa_s->dbus_path)
  986. return -1;
  987. wpa_s->dbus_path = strdup(path);
  988. return 0;
  989. }
  990. /**
  991. * wpa_supplicant_get_dbus_path - Get an interface's dbus path
  992. * @wpa_s: %wpa_supplicant interface structure
  993. * Returns: Interface's dbus object path, or %NULL on error
  994. */
  995. const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
  996. {
  997. return wpa_s->dbus_path;
  998. }