dbus_old.c 25 KB

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