dbus_old.c 22 KB

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