dbus_old.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 <dbus/dbus.h>
  16. #include "common.h"
  17. #include "eloop.h"
  18. #include "wps/wps.h"
  19. #include "../config.h"
  20. #include "../wpa_supplicant_i.h"
  21. #include "../bss.h"
  22. #include "dbus_old.h"
  23. #include "dbus_old_handlers.h"
  24. #include "dbus_common.h"
  25. #include "dbus_common_i.h"
  26. /**
  27. * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
  28. * @path: The dbus object path
  29. * @network: (out) the configured network this object path refers to, if any
  30. * @bssid: (out) the scanned bssid this object path refers to, if any
  31. * Returns: The object path of the network interface this path refers to
  32. *
  33. * For a given object path, decomposes the object path into object id, network,
  34. * and BSSID parts, if those parts exist.
  35. */
  36. char * wpas_dbus_decompose_object_path(const char *path, char **network,
  37. char **bssid)
  38. {
  39. const unsigned int dev_path_prefix_len =
  40. strlen(WPAS_DBUS_PATH_INTERFACES "/");
  41. char *obj_path_only;
  42. char *next_sep;
  43. /* Be a bit paranoid about path */
  44. if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
  45. dev_path_prefix_len))
  46. return NULL;
  47. /* Ensure there's something at the end of the path */
  48. if ((path + dev_path_prefix_len)[0] == '\0')
  49. return NULL;
  50. obj_path_only = os_strdup(path);
  51. if (obj_path_only == NULL)
  52. return NULL;
  53. next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
  54. if (next_sep != NULL) {
  55. const char *net_part = strstr(next_sep,
  56. WPAS_DBUS_NETWORKS_PART "/");
  57. const char *bssid_part = strstr(next_sep,
  58. WPAS_DBUS_BSSIDS_PART "/");
  59. if (network && net_part) {
  60. /* Deal with a request for a configured network */
  61. const char *net_name = net_part +
  62. strlen(WPAS_DBUS_NETWORKS_PART "/");
  63. *network = NULL;
  64. if (strlen(net_name))
  65. *network = os_strdup(net_name);
  66. } else if (bssid && bssid_part) {
  67. /* Deal with a request for a scanned BSSID */
  68. const char *bssid_name = bssid_part +
  69. strlen(WPAS_DBUS_BSSIDS_PART "/");
  70. if (strlen(bssid_name))
  71. *bssid = os_strdup(bssid_name);
  72. else
  73. *bssid = NULL;
  74. }
  75. /* Cut off interface object path before "/" */
  76. *next_sep = '\0';
  77. }
  78. return obj_path_only;
  79. }
  80. /**
  81. * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
  82. * @message: Pointer to incoming dbus message this error refers to
  83. * Returns: A dbus error message
  84. *
  85. * Convenience function to create and return an invalid interface error
  86. */
  87. DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
  88. {
  89. return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
  90. "wpa_supplicant knows nothing about "
  91. "this interface.");
  92. }
  93. /**
  94. * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
  95. * @message: Pointer to incoming dbus message this error refers to
  96. * Returns: a dbus error message
  97. *
  98. * Convenience function to create and return an invalid network error
  99. */
  100. DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
  101. {
  102. return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
  103. "The requested network does not exist.");
  104. }
  105. /**
  106. * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
  107. * @message: Pointer to incoming dbus message this error refers to
  108. * Returns: a dbus error message
  109. *
  110. * Convenience function to create and return an invalid bssid error
  111. */
  112. static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
  113. {
  114. return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
  115. "The BSSID requested was invalid.");
  116. }
  117. /**
  118. * wpas_dispatch_network_method - dispatch messages for configured networks
  119. * @message: the incoming dbus message
  120. * @wpa_s: a network interface's data
  121. * @network_id: id of the configured network we're interested in
  122. * Returns: a reply dbus message, or a dbus error message
  123. *
  124. * This function dispatches all incoming dbus messages for configured networks.
  125. */
  126. static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
  127. struct wpa_supplicant *wpa_s,
  128. int network_id)
  129. {
  130. DBusMessage *reply = NULL;
  131. const char *method = dbus_message_get_member(message);
  132. struct wpa_ssid *ssid;
  133. ssid = wpa_config_get_network(wpa_s->conf, network_id);
  134. if (ssid == NULL)
  135. return wpas_dbus_new_invalid_network_error(message);
  136. if (!strcmp(method, "set"))
  137. reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
  138. else if (!strcmp(method, "enable"))
  139. reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
  140. else if (!strcmp(method, "disable"))
  141. reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
  142. return reply;
  143. }
  144. /**
  145. * wpas_dispatch_bssid_method - dispatch messages for scanned networks
  146. * @message: the incoming dbus message
  147. * @wpa_s: a network interface's data
  148. * @bssid: bssid of the scanned network we're interested in
  149. * Returns: a reply dbus message, or a dbus error message
  150. *
  151. * This function dispatches all incoming dbus messages for scanned networks.
  152. */
  153. static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
  154. struct wpa_supplicant *wpa_s,
  155. const char *bssid_txt)
  156. {
  157. u8 bssid[ETH_ALEN];
  158. struct wpa_bss *bss;
  159. if (hexstr2bin(bssid_txt, bssid, ETH_ALEN) < 0)
  160. return wpas_dbus_new_invalid_bssid_error(message);
  161. bss = wpa_bss_get_bssid(wpa_s, bssid);
  162. if (bss == NULL)
  163. return wpas_dbus_new_invalid_bssid_error(message);
  164. /* Dispatch the method call against the scanned bssid */
  165. if (os_strcmp(dbus_message_get_member(message), "properties") == 0)
  166. return wpas_dbus_bssid_properties(message, wpa_s, bss);
  167. return NULL;
  168. }
  169. /**
  170. * wpas_iface_message_handler - Dispatch messages for interfaces or networks
  171. * @connection: Connection to the system message bus
  172. * @message: An incoming dbus message
  173. * @user_data: A pointer to a dbus control interface data structure
  174. * Returns: Whether or not the message was handled
  175. *
  176. * This function dispatches all incoming dbus messages for network interfaces,
  177. * or objects owned by them, such as scanned BSSIDs and configured networks.
  178. */
  179. static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
  180. DBusMessage *message,
  181. void *user_data)
  182. {
  183. struct wpa_supplicant *wpa_s = user_data;
  184. const char *method = dbus_message_get_member(message);
  185. const char *path = dbus_message_get_path(message);
  186. const char *msg_interface = dbus_message_get_interface(message);
  187. char *iface_obj_path = NULL;
  188. char *network = NULL;
  189. char *bssid = NULL;
  190. DBusMessage *reply = NULL;
  191. /* Caller must specify a message interface */
  192. if (!msg_interface)
  193. goto out;
  194. iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
  195. &bssid);
  196. if (iface_obj_path == NULL) {
  197. reply = wpas_dbus_new_invalid_iface_error(message);
  198. goto out;
  199. }
  200. /* Make sure the message's object path actually refers to the
  201. * wpa_supplicant structure it's supposed to (which is wpa_s)
  202. */
  203. if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
  204. iface_obj_path) != wpa_s) {
  205. reply = wpas_dbus_new_invalid_iface_error(message);
  206. goto out;
  207. }
  208. if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
  209. /* A method for one of this interface's configured networks */
  210. int nid = strtoul(network, NULL, 10);
  211. if (errno != EINVAL)
  212. reply = wpas_dispatch_network_method(message, wpa_s,
  213. nid);
  214. else
  215. reply = wpas_dbus_new_invalid_network_error(message);
  216. } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
  217. /* A method for one of this interface's scanned BSSIDs */
  218. reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
  219. } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
  220. /* A method for an interface only. */
  221. if (!strcmp(method, "scan"))
  222. reply = wpas_dbus_iface_scan(message, wpa_s);
  223. else if (!strcmp(method, "scanResults"))
  224. reply = wpas_dbus_iface_scan_results(message, wpa_s);
  225. else if (!strcmp(method, "addNetwork"))
  226. reply = wpas_dbus_iface_add_network(message, wpa_s);
  227. else if (!strcmp(method, "removeNetwork"))
  228. reply = wpas_dbus_iface_remove_network(message, wpa_s);
  229. else if (!strcmp(method, "selectNetwork"))
  230. reply = wpas_dbus_iface_select_network(message, wpa_s);
  231. else if (!strcmp(method, "capabilities"))
  232. reply = wpas_dbus_iface_capabilities(message, wpa_s);
  233. else if (!strcmp(method, "disconnect"))
  234. reply = wpas_dbus_iface_disconnect(message, wpa_s);
  235. else if (!strcmp(method, "setAPScan"))
  236. reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
  237. else if (!strcmp(method, "setSmartcardModules"))
  238. reply = wpas_dbus_iface_set_smartcard_modules(message,
  239. wpa_s);
  240. else if (!strcmp(method, "state"))
  241. reply = wpas_dbus_iface_get_state(message, wpa_s);
  242. else if (!strcmp(method, "scanning"))
  243. reply = wpas_dbus_iface_get_scanning(message, wpa_s);
  244. else if (!strcmp(method, "setBlobs"))
  245. reply = wpas_dbus_iface_set_blobs(message, wpa_s);
  246. else if (!strcmp(method, "removeBlobs"))
  247. reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
  248. #ifdef CONFIG_WPS
  249. else if (!os_strcmp(method, "wpsPbc"))
  250. reply = wpas_dbus_iface_wps_pbc(message, wpa_s);
  251. else if (!os_strcmp(method, "wpsPin"))
  252. reply = wpas_dbus_iface_wps_pin(message, wpa_s);
  253. else if (!os_strcmp(method, "wpsReg"))
  254. reply = wpas_dbus_iface_wps_reg(message, wpa_s);
  255. #endif /* CONFIG_WPS */
  256. }
  257. /* If the message was handled, send back the reply */
  258. if (reply) {
  259. if (!dbus_message_get_no_reply(message))
  260. dbus_connection_send(connection, reply, NULL);
  261. dbus_message_unref(reply);
  262. }
  263. out:
  264. os_free(iface_obj_path);
  265. os_free(network);
  266. os_free(bssid);
  267. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  268. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  269. }
  270. /**
  271. * wpas_message_handler - dispatch incoming dbus messages
  272. * @connection: connection to the system message bus
  273. * @message: an incoming dbus message
  274. * @user_data: a pointer to a dbus control interface data structure
  275. * Returns: whether or not the message was handled
  276. *
  277. * This function dispatches all incoming dbus messages to the correct
  278. * handlers, depending on what the message's target object path is,
  279. * and what the method call is.
  280. */
  281. static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
  282. DBusMessage *message, void *user_data)
  283. {
  284. struct wpas_dbus_priv *ctrl_iface = user_data;
  285. const char *method;
  286. const char *path;
  287. const char *msg_interface;
  288. DBusMessage *reply = NULL;
  289. method = dbus_message_get_member(message);
  290. path = dbus_message_get_path(message);
  291. msg_interface = dbus_message_get_interface(message);
  292. if (!method || !path || !ctrl_iface || !msg_interface)
  293. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  294. /* Validate the method interface */
  295. if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
  296. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  297. if (!strcmp(path, WPAS_DBUS_PATH)) {
  298. /* dispatch methods against our global dbus interface here */
  299. if (!strcmp(method, "addInterface")) {
  300. reply = wpas_dbus_global_add_interface(
  301. message, ctrl_iface->global);
  302. } else if (!strcmp(method, "removeInterface")) {
  303. reply = wpas_dbus_global_remove_interface(
  304. message, ctrl_iface->global);
  305. } else if (!strcmp(method, "getInterface")) {
  306. reply = wpas_dbus_global_get_interface(
  307. message, ctrl_iface->global);
  308. } else if (!strcmp(method, "setDebugParams")) {
  309. reply = wpas_dbus_global_set_debugparams(
  310. message, ctrl_iface->global);
  311. }
  312. }
  313. /* If the message was handled, send back the reply */
  314. if (reply) {
  315. if (!dbus_message_get_no_reply(message))
  316. dbus_connection_send(connection, reply, NULL);
  317. dbus_message_unref(reply);
  318. }
  319. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  320. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  321. }
  322. /**
  323. * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
  324. * @wpa_s: %wpa_supplicant network interface data
  325. * Returns: 0 on success, -1 on failure
  326. *
  327. * Notify listeners that this interface has updated scan results.
  328. */
  329. void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
  330. {
  331. struct wpas_dbus_priv *iface = wpa_s->global->dbus;
  332. DBusMessage *_signal;
  333. /* Do nothing if the control interface is not turned on */
  334. if (iface == NULL)
  335. return;
  336. _signal = dbus_message_new_signal(wpa_s->dbus_path,
  337. WPAS_DBUS_IFACE_INTERFACE,
  338. "ScanResultsAvailable");
  339. if (_signal == NULL) {
  340. wpa_printf(MSG_ERROR, "dbus: Not enough memory to send scan "
  341. "results signal");
  342. return;
  343. }
  344. dbus_connection_send(iface->con, _signal, NULL);
  345. dbus_message_unref(_signal);
  346. }
  347. /**
  348. * wpa_supplicant_dbus_notify_state_change - Send a state change signal
  349. * @wpa_s: %wpa_supplicant network interface data
  350. * @new_state: new state wpa_supplicant is entering
  351. * @old_state: old state wpa_supplicant is leaving
  352. * Returns: 0 on success, -1 on failure
  353. *
  354. * Notify listeners that wpa_supplicant has changed state
  355. */
  356. void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
  357. enum wpa_states new_state,
  358. enum wpa_states old_state)
  359. {
  360. struct wpas_dbus_priv *iface;
  361. DBusMessage *_signal = NULL;
  362. const char *new_state_str, *old_state_str;
  363. if (wpa_s->dbus_path == NULL)
  364. return; /* Skip signal since D-Bus setup is not yet ready */
  365. /* Do nothing if the control interface is not turned on */
  366. if (wpa_s->global == NULL)
  367. return;
  368. iface = wpa_s->global->dbus;
  369. if (iface == NULL)
  370. return;
  371. /* Only send signal if state really changed */
  372. if (new_state == old_state)
  373. return;
  374. _signal = dbus_message_new_signal(wpa_s->dbus_path,
  375. WPAS_DBUS_IFACE_INTERFACE,
  376. "StateChange");
  377. if (_signal == NULL) {
  378. wpa_printf(MSG_ERROR,
  379. "dbus: wpa_supplicant_dbus_notify_state_change: "
  380. "could not create dbus signal; likely out of "
  381. "memory");
  382. return;
  383. }
  384. new_state_str = wpa_supplicant_state_txt(new_state);
  385. old_state_str = wpa_supplicant_state_txt(old_state);
  386. if (new_state_str == NULL || old_state_str == NULL) {
  387. wpa_printf(MSG_ERROR,
  388. "dbus: wpa_supplicant_dbus_notify_state_change: "
  389. "Could not convert state strings");
  390. goto out;
  391. }
  392. if (!dbus_message_append_args(_signal,
  393. DBUS_TYPE_STRING, &new_state_str,
  394. DBUS_TYPE_STRING, &old_state_str,
  395. DBUS_TYPE_INVALID)) {
  396. wpa_printf(MSG_ERROR,
  397. "dbus: wpa_supplicant_dbus_notify_state_change: "
  398. "Not enough memory to construct state change "
  399. "signal");
  400. goto out;
  401. }
  402. dbus_connection_send(iface->con, _signal, NULL);
  403. out:
  404. dbus_message_unref(_signal);
  405. }
  406. /**
  407. * wpa_supplicant_dbus_notify_scanning - send scanning status
  408. * @wpa_s: %wpa_supplicant network interface data
  409. * Returns: 0 on success, -1 on failure
  410. *
  411. * Notify listeners of interface scanning state changes
  412. */
  413. void wpa_supplicant_dbus_notify_scanning(struct wpa_supplicant *wpa_s)
  414. {
  415. struct wpas_dbus_priv *iface = wpa_s->global->dbus;
  416. DBusMessage *_signal;
  417. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  418. /* Do nothing if the control interface is not turned on */
  419. if (iface == NULL)
  420. return;
  421. _signal = dbus_message_new_signal(wpa_s->dbus_path,
  422. WPAS_DBUS_IFACE_INTERFACE,
  423. "Scanning");
  424. if (_signal == NULL) {
  425. wpa_printf(MSG_ERROR, "dbus: Not enough memory to send scan "
  426. "results signal");
  427. return;
  428. }
  429. if (dbus_message_append_args(_signal,
  430. DBUS_TYPE_BOOLEAN, &scanning,
  431. DBUS_TYPE_INVALID)) {
  432. dbus_connection_send(iface->con, _signal, NULL);
  433. } else {
  434. wpa_printf(MSG_ERROR, "dbus: Not enough memory to construct "
  435. "signal");
  436. }
  437. dbus_message_unref(_signal);
  438. }
  439. #ifdef CONFIG_WPS
  440. void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
  441. const struct wps_credential *cred)
  442. {
  443. struct wpas_dbus_priv *iface;
  444. DBusMessage *_signal = NULL;
  445. /* Do nothing if the control interface is not turned on */
  446. if (wpa_s->global == NULL)
  447. return;
  448. iface = wpa_s->global->dbus;
  449. if (iface == NULL)
  450. return;
  451. _signal = dbus_message_new_signal(wpa_s->dbus_path,
  452. WPAS_DBUS_IFACE_INTERFACE,
  453. "WpsCred");
  454. if (_signal == NULL) {
  455. wpa_printf(MSG_ERROR,
  456. "dbus: wpa_supplicant_dbus_notify_wps_cred: "
  457. "Could not create dbus signal; likely out of "
  458. "memory");
  459. return;
  460. }
  461. if (!dbus_message_append_args(_signal,
  462. DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
  463. &cred->cred_attr, cred->cred_attr_len,
  464. DBUS_TYPE_INVALID)) {
  465. wpa_printf(MSG_ERROR,
  466. "dbus: wpa_supplicant_dbus_notify_wps_cred: "
  467. "Not enough memory to construct signal");
  468. goto out;
  469. }
  470. dbus_connection_send(iface->con, _signal, NULL);
  471. out:
  472. dbus_message_unref(_signal);
  473. }
  474. #else /* CONFIG_WPS */
  475. void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
  476. const struct wps_credential *cred)
  477. {
  478. }
  479. #endif /* CONFIG_WPS */
  480. /**
  481. * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
  482. * @global: Pointer to global data from wpa_supplicant_init()
  483. * Returns: 0 on success, -1 on failure
  484. *
  485. * Initialize the dbus control interface and start receiving commands from
  486. * external programs over the bus.
  487. */
  488. int wpa_supplicant_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface)
  489. {
  490. DBusError error;
  491. int ret = -1;
  492. DBusObjectPathVTable wpas_vtable = {
  493. NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
  494. };
  495. /* Register the message handler for the global dbus interface */
  496. if (!dbus_connection_register_object_path(iface->con,
  497. WPAS_DBUS_PATH, &wpas_vtable,
  498. iface)) {
  499. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  500. "handler");
  501. return -1;
  502. }
  503. /* Register our service with the message bus */
  504. dbus_error_init(&error);
  505. switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
  506. 0, &error)) {
  507. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  508. ret = 0;
  509. break;
  510. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  511. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  512. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  513. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  514. "already registered");
  515. break;
  516. default:
  517. wpa_printf(MSG_ERROR, "dbus: Could not request service name: "
  518. "%s %s", error.name, error.message);
  519. break;
  520. }
  521. dbus_error_free(&error);
  522. if (ret != 0)
  523. return -1;
  524. wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
  525. "'.");
  526. return 0;
  527. }
  528. /**
  529. * wpas_dbus_register_new_iface - Register a new interface with dbus
  530. * @wpa_s: %wpa_supplicant interface description structure to register
  531. * Returns: 0 on success, -1 on error
  532. *
  533. * Registers a new interface with dbus and assigns it a dbus object path.
  534. */
  535. int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
  536. {
  537. struct wpas_dbus_priv *ctrl_iface = wpa_s->global->dbus;
  538. DBusConnection * con;
  539. u32 next;
  540. DBusObjectPathVTable vtable = {
  541. NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
  542. };
  543. /* Do nothing if the control interface is not turned on */
  544. if (ctrl_iface == NULL)
  545. return 0;
  546. con = ctrl_iface->con;
  547. next = ctrl_iface->next_objid++;
  548. /* Create and set the interface's object path */
  549. wpa_s->dbus_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  550. if (wpa_s->dbus_path == NULL)
  551. return -1;
  552. os_snprintf(wpa_s->dbus_path, WPAS_DBUS_OBJECT_PATH_MAX,
  553. WPAS_DBUS_PATH_INTERFACES "/%u",
  554. next);
  555. /* Register the message handler for the interface functions */
  556. if (!dbus_connection_register_fallback(con, wpa_s->dbus_path, &vtable,
  557. wpa_s)) {
  558. wpa_printf(MSG_ERROR, "dbus: Could not set up message "
  559. "handler for interface %s", wpa_s->ifname);
  560. return -1;
  561. }
  562. return 0;
  563. }
  564. /**
  565. * wpas_dbus_unregister_iface - Unregister an interface from dbus
  566. * @wpa_s: wpa_supplicant interface structure
  567. * Returns: 0 on success, -1 on failure
  568. *
  569. * Unregisters the interface with dbus
  570. */
  571. int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
  572. {
  573. struct wpas_dbus_priv *ctrl_iface;
  574. DBusConnection *con;
  575. /* Do nothing if the control interface is not turned on */
  576. if (wpa_s == NULL || wpa_s->global == NULL)
  577. return 0;
  578. ctrl_iface = wpa_s->global->dbus;
  579. if (ctrl_iface == NULL)
  580. return 0;
  581. con = ctrl_iface->con;
  582. if (!dbus_connection_unregister_object_path(con, wpa_s->dbus_path))
  583. return -1;
  584. os_free(wpa_s->dbus_path);
  585. wpa_s->dbus_path = NULL;
  586. return 0;
  587. }
  588. /**
  589. * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
  590. * @global: Pointer to global data from wpa_supplicant_init()
  591. * @path: Pointer to a dbus object path representing an interface
  592. * Returns: Pointer to the interface or %NULL if not found
  593. */
  594. struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
  595. struct wpa_global *global, const char *path)
  596. {
  597. struct wpa_supplicant *wpa_s;
  598. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  599. if (strcmp(wpa_s->dbus_path, path) == 0)
  600. return wpa_s;
  601. }
  602. return NULL;
  603. }