dbus.c 25 KB

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