dbus_old.c 22 KB

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