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