您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

dbus_old.c 21KB

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