dbus_old.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 "drivers/driver.h"
  19. #include "wps/wps.h"
  20. #include "../config.h"
  21. #include "../wpa_supplicant_i.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)
  156. {
  157. DBusMessage *reply = NULL;
  158. const char *method = dbus_message_get_member(message);
  159. struct wpa_scan_res *res = NULL;
  160. size_t i;
  161. /* Ensure we actually have scan data */
  162. if (wpa_s->scan_res == NULL &&
  163. wpa_supplicant_get_scan_results(wpa_s) < 0) {
  164. reply = wpas_dbus_new_invalid_bssid_error(message);
  165. goto out;
  166. }
  167. /* Find the bssid's scan data */
  168. for (i = 0; i < wpa_s->scan_res->num; i++) {
  169. struct wpa_scan_res *search_res = wpa_s->scan_res->res[i];
  170. char mac_str[18];
  171. memset(mac_str, 0, sizeof(mac_str));
  172. snprintf(mac_str, sizeof(mac_str) - 1, WPAS_DBUS_BSSID_FORMAT,
  173. MAC2STR(search_res->bssid));
  174. if (!strcmp(bssid, mac_str)) {
  175. res = search_res;
  176. break;
  177. }
  178. }
  179. if (!res) {
  180. reply = wpas_dbus_new_invalid_bssid_error(message);
  181. goto out;
  182. }
  183. /* Dispatch the method call against the scanned bssid */
  184. if (!strcmp(method, "properties"))
  185. reply = wpas_dbus_bssid_properties(message, wpa_s, res);
  186. out:
  187. return reply;
  188. }
  189. /**
  190. * wpas_iface_message_handler - Dispatch messages for interfaces or networks
  191. * @connection: Connection to the system message bus
  192. * @message: An incoming dbus message
  193. * @user_data: A pointer to a dbus control interface data structure
  194. * Returns: Whether or not the message was handled
  195. *
  196. * This function dispatches all incoming dbus messages for network interfaces,
  197. * or objects owned by them, such as scanned BSSIDs and configured networks.
  198. */
  199. static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
  200. DBusMessage *message,
  201. void *user_data)
  202. {
  203. struct wpa_supplicant *wpa_s = user_data;
  204. const char *method = dbus_message_get_member(message);
  205. const char *path = dbus_message_get_path(message);
  206. const char *msg_interface = dbus_message_get_interface(message);
  207. char *iface_obj_path = NULL;
  208. char *network = NULL;
  209. char *bssid = NULL;
  210. DBusMessage *reply = NULL;
  211. /* Caller must specify a message interface */
  212. if (!msg_interface)
  213. goto out;
  214. iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
  215. &bssid);
  216. if (iface_obj_path == NULL) {
  217. reply = wpas_dbus_new_invalid_iface_error(message);
  218. goto out;
  219. }
  220. /* Make sure the message's object path actually refers to the
  221. * wpa_supplicant structure it's supposed to (which is wpa_s)
  222. */
  223. if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
  224. iface_obj_path) != wpa_s) {
  225. reply = wpas_dbus_new_invalid_iface_error(message);
  226. goto out;
  227. }
  228. if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
  229. /* A method for one of this interface's configured networks */
  230. int nid = strtoul(network, NULL, 10);
  231. if (errno != EINVAL)
  232. reply = wpas_dispatch_network_method(message, wpa_s,
  233. nid);
  234. else
  235. reply = wpas_dbus_new_invalid_network_error(message);
  236. } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
  237. /* A method for one of this interface's scanned BSSIDs */
  238. reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
  239. } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
  240. /* A method for an interface only. */
  241. if (!strcmp(method, "scan"))
  242. reply = wpas_dbus_iface_scan(message, wpa_s);
  243. else if (!strcmp(method, "scanResults"))
  244. reply = wpas_dbus_iface_scan_results(message, wpa_s);
  245. else if (!strcmp(method, "addNetwork"))
  246. reply = wpas_dbus_iface_add_network(message, wpa_s);
  247. else if (!strcmp(method, "removeNetwork"))
  248. reply = wpas_dbus_iface_remove_network(message, wpa_s);
  249. else if (!strcmp(method, "selectNetwork"))
  250. reply = wpas_dbus_iface_select_network(message, wpa_s);
  251. else if (!strcmp(method, "capabilities"))
  252. reply = wpas_dbus_iface_capabilities(message, wpa_s);
  253. else if (!strcmp(method, "disconnect"))
  254. reply = wpas_dbus_iface_disconnect(message, wpa_s);
  255. else if (!strcmp(method, "setAPScan"))
  256. reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
  257. else if (!strcmp(method, "setSmartcardModules"))
  258. reply = wpas_dbus_iface_set_smartcard_modules(message,
  259. wpa_s);
  260. else if (!strcmp(method, "state"))
  261. reply = wpas_dbus_iface_get_state(message, wpa_s);
  262. else if (!strcmp(method, "scanning"))
  263. reply = wpas_dbus_iface_get_scanning(message, wpa_s);
  264. else if (!strcmp(method, "setBlobs"))
  265. reply = wpas_dbus_iface_set_blobs(message, wpa_s);
  266. else if (!strcmp(method, "removeBlobs"))
  267. reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
  268. #ifdef CONFIG_WPS
  269. else if (!os_strcmp(method, "wpsPbc"))
  270. reply = wpas_dbus_iface_wps_pbc(message, wpa_s);
  271. else if (!os_strcmp(method, "wpsPin"))
  272. reply = wpas_dbus_iface_wps_pin(message, wpa_s);
  273. else if (!os_strcmp(method, "wpsReg"))
  274. reply = wpas_dbus_iface_wps_reg(message, wpa_s);
  275. #endif /* CONFIG_WPS */
  276. }
  277. /* If the message was handled, send back the reply */
  278. if (reply) {
  279. if (!dbus_message_get_no_reply(message))
  280. dbus_connection_send(connection, reply, NULL);
  281. dbus_message_unref(reply);
  282. }
  283. out:
  284. os_free(iface_obj_path);
  285. os_free(network);
  286. os_free(bssid);
  287. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  288. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  289. }
  290. /**
  291. * wpas_message_handler - dispatch incoming dbus messages
  292. * @connection: connection to the system message bus
  293. * @message: an incoming dbus message
  294. * @user_data: a pointer to a dbus control interface data structure
  295. * Returns: whether or not the message was handled
  296. *
  297. * This function dispatches all incoming dbus messages to the correct
  298. * handlers, depending on what the message's target object path is,
  299. * and what the method call is.
  300. */
  301. static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
  302. DBusMessage *message, void *user_data)
  303. {
  304. struct wpas_dbus_priv *ctrl_iface = user_data;
  305. const char *method;
  306. const char *path;
  307. const char *msg_interface;
  308. DBusMessage *reply = NULL;
  309. method = dbus_message_get_member(message);
  310. path = dbus_message_get_path(message);
  311. msg_interface = dbus_message_get_interface(message);
  312. if (!method || !path || !ctrl_iface || !msg_interface)
  313. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  314. /* Validate the method interface */
  315. if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
  316. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  317. if (!strcmp(path, WPAS_DBUS_PATH)) {
  318. /* dispatch methods against our global dbus interface here */
  319. if (!strcmp(method, "addInterface")) {
  320. reply = wpas_dbus_global_add_interface(
  321. message, ctrl_iface->global);
  322. } else if (!strcmp(method, "removeInterface")) {
  323. reply = wpas_dbus_global_remove_interface(
  324. message, ctrl_iface->global);
  325. } else if (!strcmp(method, "getInterface")) {
  326. reply = wpas_dbus_global_get_interface(
  327. message, ctrl_iface->global);
  328. } else if (!strcmp(method, "setDebugParams")) {
  329. reply = wpas_dbus_global_set_debugparams(
  330. message, ctrl_iface->global);
  331. }
  332. }
  333. /* If the message was handled, send back the reply */
  334. if (reply) {
  335. if (!dbus_message_get_no_reply(message))
  336. dbus_connection_send(connection, reply, NULL);
  337. dbus_message_unref(reply);
  338. }
  339. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  340. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  341. }
  342. /**
  343. * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
  344. * @wpa_s: %wpa_supplicant network interface data
  345. * Returns: 0 on success, -1 on failure
  346. *
  347. * Notify listeners that this interface has updated scan results.
  348. */
  349. void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
  350. {
  351. struct wpas_dbus_priv *iface = wpa_s->global->dbus;
  352. DBusMessage *_signal;
  353. /* Do nothing if the control interface is not turned on */
  354. if (iface == NULL)
  355. return;
  356. _signal = dbus_message_new_signal(wpa_s->dbus_path,
  357. WPAS_DBUS_IFACE_INTERFACE,
  358. "ScanResultsAvailable");
  359. if (_signal == NULL) {
  360. perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
  361. "couldn't create dbus signal; likely out of memory");
  362. wpa_printf(MSG_ERROR, "dbus control interface: not enough "
  363. "memory to send scan results signal.");
  364. return;
  365. }
  366. dbus_connection_send(iface->con, _signal, NULL);
  367. dbus_message_unref(_signal);
  368. }
  369. /**
  370. * wpa_supplicant_dbus_notify_state_change - Send a state change signal
  371. * @wpa_s: %wpa_supplicant network interface data
  372. * @new_state: new state wpa_supplicant is entering
  373. * @old_state: old state wpa_supplicant is leaving
  374. * Returns: 0 on success, -1 on failure
  375. *
  376. * Notify listeners that wpa_supplicant has changed state
  377. */
  378. void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
  379. enum wpa_states new_state,
  380. enum wpa_states old_state)
  381. {
  382. struct wpas_dbus_priv *iface;
  383. DBusMessage *_signal = NULL;
  384. const char *new_state_str, *old_state_str;
  385. /* Do nothing if the control interface is not turned on */
  386. if (wpa_s->global == NULL)
  387. return;
  388. iface = wpa_s->global->dbus;
  389. if (iface == NULL)
  390. return;
  391. /* Only send signal if state really changed */
  392. if (new_state == old_state)
  393. return;
  394. _signal = dbus_message_new_signal(wpa_s->dbus_path,
  395. WPAS_DBUS_IFACE_INTERFACE,
  396. "StateChange");
  397. if (_signal == NULL) {
  398. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  399. "couldn't create dbus signal; likely out of memory");
  400. wpa_printf(MSG_ERROR,
  401. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  402. "couldn't create dbus signal; likely out of "
  403. "memory.");
  404. return;
  405. }
  406. new_state_str = wpa_supplicant_state_txt(new_state);
  407. old_state_str = wpa_supplicant_state_txt(old_state);
  408. if (new_state_str == NULL || old_state_str == NULL) {
  409. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  410. "couldn't convert state strings");
  411. wpa_printf(MSG_ERROR,
  412. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  413. "couldn't convert state strings.");
  414. goto out;
  415. }
  416. if (!dbus_message_append_args(_signal,
  417. DBUS_TYPE_STRING, &new_state_str,
  418. DBUS_TYPE_STRING, &old_state_str,
  419. DBUS_TYPE_INVALID)) {
  420. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  421. "not enough memory to construct state change signal.");
  422. wpa_printf(MSG_ERROR,
  423. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  424. "not enough memory to construct state change "
  425. "signal.");
  426. goto out;
  427. }
  428. dbus_connection_send(iface->con, _signal, NULL);
  429. out:
  430. dbus_message_unref(_signal);
  431. }
  432. /**
  433. * wpa_supplicant_dbus_notify_scanning - send scanning status
  434. * @wpa_s: %wpa_supplicant network interface data
  435. * Returns: 0 on success, -1 on failure
  436. *
  437. * Notify listeners of interface scanning state changes
  438. */
  439. void wpa_supplicant_dbus_notify_scanning(struct wpa_supplicant *wpa_s)
  440. {
  441. struct wpas_dbus_priv *iface = wpa_s->global->dbus;
  442. DBusMessage *_signal;
  443. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  444. /* Do nothing if the control interface is not turned on */
  445. if (iface == NULL)
  446. return;
  447. _signal = dbus_message_new_signal(wpa_s->dbus_path,
  448. WPAS_DBUS_IFACE_INTERFACE,
  449. "Scanning");
  450. if (_signal == NULL) {
  451. perror("wpa_supplicant_dbus_notify_scanning[dbus]: couldn't "
  452. "create dbus signal; likely out of memory");
  453. wpa_printf(MSG_ERROR, "%s[dbus]: dbus control interface: not "
  454. "enough memory to send scan results signal.",
  455. __FUNCTION__);
  456. return;
  457. }
  458. if (dbus_message_append_args(_signal,
  459. DBUS_TYPE_BOOLEAN, &scanning,
  460. DBUS_TYPE_INVALID)) {
  461. dbus_connection_send(iface->con, _signal, NULL);
  462. } else {
  463. perror("wpa_supplicant_dbus_notify_scanning[dbus]: not enough "
  464. "memory to construct signal.");
  465. wpa_printf(MSG_ERROR, "%s[dbus]: not enough memory to "
  466. "construct signal.", __FUNCTION__);
  467. }
  468. dbus_message_unref(_signal);
  469. }
  470. #ifdef CONFIG_WPS
  471. void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
  472. const struct wps_credential *cred)
  473. {
  474. struct wpas_dbus_priv *iface;
  475. DBusMessage *_signal = NULL;
  476. /* Do nothing if the control interface is not turned on */
  477. if (wpa_s->global == NULL)
  478. return;
  479. iface = wpa_s->global->dbus;
  480. if (iface == NULL)
  481. return;
  482. _signal = dbus_message_new_signal(wpa_s->dbus_path,
  483. WPAS_DBUS_IFACE_INTERFACE,
  484. "WpsCred");
  485. if (_signal == NULL) {
  486. perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  487. "couldn't create dbus signal; likely out of memory");
  488. wpa_printf(MSG_ERROR,
  489. "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  490. "couldn't create dbus signal; likely out of "
  491. "memory.");
  492. return;
  493. }
  494. if (!dbus_message_append_args(_signal,
  495. DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
  496. &cred->cred_attr, cred->cred_attr_len,
  497. DBUS_TYPE_INVALID)) {
  498. perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  499. "not enough memory to construct signal.");
  500. wpa_printf(MSG_ERROR,
  501. "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  502. "not enough memory to construct signal.");
  503. goto out;
  504. }
  505. dbus_connection_send(iface->con, _signal, NULL);
  506. out:
  507. dbus_message_unref(_signal);
  508. }
  509. #else /* CONFIG_WPS */
  510. void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
  511. const struct wps_credential *cred)
  512. {
  513. }
  514. #endif /* CONFIG_WPS */
  515. /**
  516. * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
  517. * @global: Pointer to global data from wpa_supplicant_init()
  518. * Returns: 0 on success, -1 on failure
  519. *
  520. * Initialize the dbus control interface and start receiving commands from
  521. * external programs over the bus.
  522. */
  523. int wpa_supplicant_dbus_ctrl_iface_init(struct wpas_dbus_priv *iface)
  524. {
  525. DBusError error;
  526. int ret = -1;
  527. DBusObjectPathVTable wpas_vtable = {
  528. NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
  529. };
  530. /* Register the message handler for the global dbus interface */
  531. if (!dbus_connection_register_object_path(iface->con,
  532. WPAS_DBUS_PATH, &wpas_vtable,
  533. iface)) {
  534. perror("dbus_connection_register_object_path[dbus]");
  535. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  536. "handler.");
  537. return -1;
  538. }
  539. /* Register our service with the message bus */
  540. dbus_error_init(&error);
  541. switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
  542. 0, &error)) {
  543. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  544. ret = 0;
  545. break;
  546. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  547. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  548. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  549. perror("dbus_bus_request_name[dbus]");
  550. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  551. "already registered.");
  552. break;
  553. default:
  554. perror("dbus_bus_request_name[dbus]");
  555. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  556. "%s %s.", error.name, error.message);
  557. break;
  558. }
  559. dbus_error_free(&error);
  560. if (ret != 0)
  561. return -1;
  562. wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
  563. "'.");
  564. return 0;
  565. }
  566. /**
  567. * wpas_dbus_register_new_iface - Register a new interface with dbus
  568. * @wpa_s: %wpa_supplicant interface description structure to register
  569. * Returns: 0 on success, -1 on error
  570. *
  571. * Registers a new interface with dbus and assigns it a dbus object path.
  572. */
  573. int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
  574. {
  575. struct wpas_dbus_priv *ctrl_iface = wpa_s->global->dbus;
  576. DBusConnection * con;
  577. u32 next;
  578. DBusObjectPathVTable vtable = {
  579. NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
  580. };
  581. int ret = -1;
  582. /* Do nothing if the control interface is not turned on */
  583. if (ctrl_iface == NULL)
  584. return 0;
  585. con = ctrl_iface->con;
  586. next = ctrl_iface->next_objid++;
  587. /* Create and set the interface's object path */
  588. wpa_s->dbus_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  589. if (wpa_s->dbus_path == NULL)
  590. return -1;
  591. os_snprintf(wpa_s->dbus_path, WPAS_DBUS_OBJECT_PATH_MAX,
  592. WPAS_DBUS_PATH_INTERFACES "/%u",
  593. next);
  594. /* Register the message handler for the interface functions */
  595. if (!dbus_connection_register_fallback(con, wpa_s->dbus_path, &vtable,
  596. wpa_s)) {
  597. perror("wpas_dbus_register_iface [dbus]");
  598. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  599. "handler for interface %s.", wpa_s->ifname);
  600. goto out;
  601. }
  602. ret = 0;
  603. out:
  604. return ret;
  605. }
  606. /**
  607. * wpas_dbus_unregister_iface - Unregister an interface from dbus
  608. * @wpa_s: wpa_supplicant interface structure
  609. * Returns: 0 on success, -1 on failure
  610. *
  611. * Unregisters the interface with dbus
  612. */
  613. int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
  614. {
  615. struct wpas_dbus_priv *ctrl_iface;
  616. DBusConnection *con;
  617. /* Do nothing if the control interface is not turned on */
  618. if (wpa_s == NULL || wpa_s->global == NULL)
  619. return 0;
  620. ctrl_iface = wpa_s->global->dbus;
  621. if (ctrl_iface == NULL)
  622. return 0;
  623. con = ctrl_iface->con;
  624. if (!dbus_connection_unregister_object_path(con, wpa_s->dbus_path))
  625. return -1;
  626. os_free(wpa_s->dbus_path);
  627. wpa_s->dbus_path = NULL;
  628. return 0;
  629. }
  630. /**
  631. * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
  632. * @global: Pointer to global data from wpa_supplicant_init()
  633. * @path: Pointer to a dbus object path representing an interface
  634. * Returns: Pointer to the interface or %NULL if not found
  635. */
  636. struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
  637. struct wpa_global *global, const char *path)
  638. {
  639. struct wpa_supplicant *wpa_s;
  640. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  641. if (strcmp(wpa_s->dbus_path, path) == 0)
  642. return wpa_s;
  643. }
  644. return NULL;
  645. }