ctrl_iface_dbus.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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 "config.h"
  18. #include "wpa_supplicant_i.h"
  19. #include "drivers/driver.h"
  20. #include "wps/wps.h"
  21. #include "ctrl_iface_dbus.h"
  22. #include "ctrl_iface_dbus_handlers.h"
  23. #define _DBUS_VERSION (DBUS_VERSION_MAJOR << 8 | DBUS_VERSION_MINOR)
  24. #define DBUS_VER(major, minor) ((major) << 8 | (minor))
  25. #if _DBUS_VERSION < DBUS_VER(1,1)
  26. #define dbus_watch_get_unix_fd dbus_watch_get_fd
  27. #endif
  28. struct ctrl_iface_dbus_priv {
  29. DBusConnection *con;
  30. int should_dispatch;
  31. struct wpa_global *global;
  32. u32 next_objid;
  33. };
  34. static void process_watch(struct ctrl_iface_dbus_priv *iface,
  35. DBusWatch *watch, eloop_event_type type)
  36. {
  37. dbus_connection_ref(iface->con);
  38. iface->should_dispatch = 0;
  39. if (type == EVENT_TYPE_READ)
  40. dbus_watch_handle(watch, DBUS_WATCH_READABLE);
  41. else if (type == EVENT_TYPE_WRITE)
  42. dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
  43. else if (type == EVENT_TYPE_EXCEPTION)
  44. dbus_watch_handle(watch, DBUS_WATCH_ERROR);
  45. if (iface->should_dispatch) {
  46. while (dbus_connection_get_dispatch_status(iface->con) ==
  47. DBUS_DISPATCH_DATA_REMAINS)
  48. dbus_connection_dispatch(iface->con);
  49. iface->should_dispatch = 0;
  50. }
  51. dbus_connection_unref(iface->con);
  52. }
  53. static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
  54. {
  55. process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
  56. }
  57. static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
  58. {
  59. process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
  60. }
  61. static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
  62. {
  63. process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
  64. }
  65. static void connection_setup_add_watch(struct ctrl_iface_dbus_priv *iface,
  66. DBusWatch *watch)
  67. {
  68. unsigned int flags;
  69. int fd;
  70. if (!dbus_watch_get_enabled(watch))
  71. return;
  72. flags = dbus_watch_get_flags(watch);
  73. fd = dbus_watch_get_unix_fd(watch);
  74. eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
  75. iface, watch);
  76. if (flags & DBUS_WATCH_READABLE) {
  77. eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
  78. iface, watch);
  79. }
  80. if (flags & DBUS_WATCH_WRITABLE) {
  81. eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
  82. iface, watch);
  83. }
  84. dbus_watch_set_data(watch, iface, NULL);
  85. }
  86. static void connection_setup_remove_watch(struct ctrl_iface_dbus_priv *iface,
  87. DBusWatch *watch)
  88. {
  89. unsigned int flags;
  90. int fd;
  91. flags = dbus_watch_get_flags(watch);
  92. fd = dbus_watch_get_unix_fd(watch);
  93. eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
  94. if (flags & DBUS_WATCH_READABLE)
  95. eloop_unregister_sock(fd, EVENT_TYPE_READ);
  96. if (flags & DBUS_WATCH_WRITABLE)
  97. eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
  98. dbus_watch_set_data(watch, NULL, NULL);
  99. }
  100. static dbus_bool_t add_watch(DBusWatch *watch, void *data)
  101. {
  102. connection_setup_add_watch(data, watch);
  103. return TRUE;
  104. }
  105. static void remove_watch(DBusWatch *watch, void *data)
  106. {
  107. connection_setup_remove_watch(data, watch);
  108. }
  109. static void watch_toggled(DBusWatch *watch, void *data)
  110. {
  111. if (dbus_watch_get_enabled(watch))
  112. add_watch(watch, data);
  113. else
  114. remove_watch(watch, data);
  115. }
  116. static void process_timeout(void *eloop_ctx, void *sock_ctx)
  117. {
  118. DBusTimeout *timeout = sock_ctx;
  119. dbus_timeout_handle(timeout);
  120. }
  121. static void connection_setup_add_timeout(struct ctrl_iface_dbus_priv *iface,
  122. DBusTimeout *timeout)
  123. {
  124. if (!dbus_timeout_get_enabled(timeout))
  125. return;
  126. eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
  127. process_timeout, iface, timeout);
  128. dbus_timeout_set_data(timeout, iface, NULL);
  129. }
  130. static void connection_setup_remove_timeout(struct ctrl_iface_dbus_priv *iface,
  131. DBusTimeout *timeout)
  132. {
  133. eloop_cancel_timeout(process_timeout, iface, timeout);
  134. dbus_timeout_set_data(timeout, NULL, NULL);
  135. }
  136. static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
  137. {
  138. if (!dbus_timeout_get_enabled(timeout))
  139. return TRUE;
  140. connection_setup_add_timeout(data, timeout);
  141. return TRUE;
  142. }
  143. static void remove_timeout(DBusTimeout *timeout, void *data)
  144. {
  145. connection_setup_remove_timeout(data, timeout);
  146. }
  147. static void timeout_toggled(DBusTimeout *timeout, void *data)
  148. {
  149. if (dbus_timeout_get_enabled(timeout))
  150. add_timeout(timeout, data);
  151. else
  152. remove_timeout(timeout, data);
  153. }
  154. static void process_wakeup_main(int sig, void *eloop_ctx, void *signal_ctx)
  155. {
  156. struct ctrl_iface_dbus_priv *iface = signal_ctx;
  157. if (sig != SIGPOLL || !iface->con)
  158. return;
  159. if (dbus_connection_get_dispatch_status(iface->con) !=
  160. DBUS_DISPATCH_DATA_REMAINS)
  161. return;
  162. /* Only dispatch once - we do not want to starve other events */
  163. dbus_connection_ref(iface->con);
  164. dbus_connection_dispatch(iface->con);
  165. dbus_connection_unref(iface->con);
  166. }
  167. /**
  168. * wakeup_main - Attempt to wake our mainloop up
  169. * @data: dbus control interface private data
  170. *
  171. * Try to wake up the main eloop so it will process
  172. * dbus events that may have happened.
  173. */
  174. static void wakeup_main(void *data)
  175. {
  176. struct ctrl_iface_dbus_priv *iface = data;
  177. /* Use SIGPOLL to break out of the eloop select() */
  178. raise(SIGPOLL);
  179. iface->should_dispatch = 1;
  180. }
  181. /**
  182. * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
  183. * @iface: dbus control interface private data
  184. * Returns: 0 on success, -1 on failure
  185. *
  186. * Register our wakeup_main handler with dbus
  187. */
  188. static int connection_setup_wakeup_main(struct ctrl_iface_dbus_priv *iface)
  189. {
  190. if (eloop_register_signal(SIGPOLL, process_wakeup_main, iface))
  191. return -1;
  192. dbus_connection_set_wakeup_main_function(iface->con, wakeup_main,
  193. iface, NULL);
  194. return 0;
  195. }
  196. /**
  197. * wpa_supplicant_dbus_next_objid - Return next available object id
  198. * @iface: dbus control interface private data
  199. * Returns: Object id
  200. */
  201. u32 wpa_supplicant_dbus_next_objid (struct ctrl_iface_dbus_priv *iface)
  202. {
  203. return iface->next_objid++;
  204. }
  205. /**
  206. * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
  207. * @path: The dbus object path
  208. * @network: (out) the configured network this object path refers to, if any
  209. * @bssid: (out) the scanned bssid this object path refers to, if any
  210. * Returns: The object path of the network interface this path refers to
  211. *
  212. * For a given object path, decomposes the object path into object id, network,
  213. * and BSSID parts, if those parts exist.
  214. */
  215. char * wpas_dbus_decompose_object_path(const char *path, char **network,
  216. char **bssid)
  217. {
  218. const unsigned int dev_path_prefix_len =
  219. strlen(WPAS_DBUS_PATH_INTERFACES "/");
  220. char *obj_path_only;
  221. char *next_sep;
  222. /* Be a bit paranoid about path */
  223. if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
  224. dev_path_prefix_len))
  225. return NULL;
  226. /* Ensure there's something at the end of the path */
  227. if ((path + dev_path_prefix_len)[0] == '\0')
  228. return NULL;
  229. obj_path_only = strdup(path);
  230. if (obj_path_only == NULL)
  231. return NULL;
  232. next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
  233. if (next_sep != NULL) {
  234. const char *net_part = strstr(next_sep,
  235. WPAS_DBUS_NETWORKS_PART "/");
  236. const char *bssid_part = strstr(next_sep,
  237. WPAS_DBUS_BSSIDS_PART "/");
  238. if (network && net_part) {
  239. /* Deal with a request for a configured network */
  240. const char *net_name = net_part +
  241. strlen(WPAS_DBUS_NETWORKS_PART "/");
  242. *network = NULL;
  243. if (strlen(net_name))
  244. *network = strdup(net_name);
  245. } else if (bssid && bssid_part) {
  246. /* Deal with a request for a scanned BSSID */
  247. const char *bssid_name = bssid_part +
  248. strlen(WPAS_DBUS_BSSIDS_PART "/");
  249. if (strlen(bssid_name))
  250. *bssid = strdup(bssid_name);
  251. else
  252. *bssid = NULL;
  253. }
  254. /* Cut off interface object path before "/" */
  255. *next_sep = '\0';
  256. }
  257. return obj_path_only;
  258. }
  259. /**
  260. * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
  261. * @message: Pointer to incoming dbus message this error refers to
  262. * Returns: A dbus error message
  263. *
  264. * Convenience function to create and return an invalid interface error
  265. */
  266. DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
  267. {
  268. return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
  269. "wpa_supplicant knows nothing about "
  270. "this interface.");
  271. }
  272. /**
  273. * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
  274. * @message: Pointer to incoming dbus message this error refers to
  275. * Returns: a dbus error message
  276. *
  277. * Convenience function to create and return an invalid network error
  278. */
  279. DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
  280. {
  281. return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
  282. "The requested network does not exist.");
  283. }
  284. /**
  285. * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
  286. * @message: Pointer to incoming dbus message this error refers to
  287. * Returns: a dbus error message
  288. *
  289. * Convenience function to create and return an invalid bssid error
  290. */
  291. static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
  292. {
  293. return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
  294. "The BSSID requested was invalid.");
  295. }
  296. /**
  297. * wpas_dispatch_network_method - dispatch messages for configured networks
  298. * @message: the incoming dbus message
  299. * @wpa_s: a network interface's data
  300. * @network_id: id of the configured network we're interested in
  301. * Returns: a reply dbus message, or a dbus error message
  302. *
  303. * This function dispatches all incoming dbus messages for configured networks.
  304. */
  305. static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
  306. struct wpa_supplicant *wpa_s,
  307. int network_id)
  308. {
  309. DBusMessage *reply = NULL;
  310. const char *method = dbus_message_get_member(message);
  311. struct wpa_ssid *ssid;
  312. ssid = wpa_config_get_network(wpa_s->conf, network_id);
  313. if (ssid == NULL)
  314. return wpas_dbus_new_invalid_network_error(message);
  315. if (!strcmp(method, "set"))
  316. reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
  317. else if (!strcmp(method, "enable"))
  318. reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
  319. else if (!strcmp(method, "disable"))
  320. reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
  321. return reply;
  322. }
  323. /**
  324. * wpas_dispatch_bssid_method - dispatch messages for scanned networks
  325. * @message: the incoming dbus message
  326. * @wpa_s: a network interface's data
  327. * @bssid: bssid of the scanned network we're interested in
  328. * Returns: a reply dbus message, or a dbus error message
  329. *
  330. * This function dispatches all incoming dbus messages for scanned networks.
  331. */
  332. static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
  333. struct wpa_supplicant *wpa_s,
  334. const char *bssid)
  335. {
  336. DBusMessage *reply = NULL;
  337. const char *method = dbus_message_get_member(message);
  338. struct wpa_scan_res *res = NULL;
  339. size_t i;
  340. /* Ensure we actually have scan data */
  341. if (wpa_s->scan_res == NULL &&
  342. wpa_supplicant_get_scan_results(wpa_s) < 0) {
  343. reply = wpas_dbus_new_invalid_bssid_error(message);
  344. goto out;
  345. }
  346. /* Find the bssid's scan data */
  347. for (i = 0; i < wpa_s->scan_res->num; i++) {
  348. struct wpa_scan_res *search_res = wpa_s->scan_res->res[i];
  349. char mac_str[18];
  350. memset(mac_str, 0, sizeof(mac_str));
  351. snprintf(mac_str, sizeof(mac_str) - 1, WPAS_DBUS_BSSID_FORMAT,
  352. MAC2STR(search_res->bssid));
  353. if (!strcmp(bssid, mac_str)) {
  354. res = search_res;
  355. break;
  356. }
  357. }
  358. if (!res) {
  359. reply = wpas_dbus_new_invalid_bssid_error(message);
  360. goto out;
  361. }
  362. /* Dispatch the method call against the scanned bssid */
  363. if (!strcmp(method, "properties"))
  364. reply = wpas_dbus_bssid_properties(message, wpa_s, res);
  365. out:
  366. return reply;
  367. }
  368. /**
  369. * wpas_iface_message_handler - Dispatch messages for interfaces or networks
  370. * @connection: Connection to the system message bus
  371. * @message: An incoming dbus message
  372. * @user_data: A pointer to a dbus control interface data structure
  373. * Returns: Whether or not the message was handled
  374. *
  375. * This function dispatches all incoming dbus messages for network interfaces,
  376. * or objects owned by them, such as scanned BSSIDs and configured networks.
  377. */
  378. static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
  379. DBusMessage *message,
  380. void *user_data)
  381. {
  382. struct wpa_supplicant *wpa_s = user_data;
  383. const char *method = dbus_message_get_member(message);
  384. const char *path = dbus_message_get_path(message);
  385. const char *msg_interface = dbus_message_get_interface(message);
  386. char *iface_obj_path = NULL;
  387. char *network = NULL;
  388. char *bssid = NULL;
  389. DBusMessage *reply = NULL;
  390. /* Caller must specify a message interface */
  391. if (!msg_interface)
  392. goto out;
  393. iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
  394. &bssid);
  395. if (iface_obj_path == NULL) {
  396. reply = wpas_dbus_new_invalid_iface_error(message);
  397. goto out;
  398. }
  399. /* Make sure the message's object path actually refers to the
  400. * wpa_supplicant structure it's supposed to (which is wpa_s)
  401. */
  402. if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
  403. iface_obj_path) != wpa_s) {
  404. reply = wpas_dbus_new_invalid_iface_error(message);
  405. goto out;
  406. }
  407. if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
  408. /* A method for one of this interface's configured networks */
  409. int nid = strtoul(network, NULL, 10);
  410. if (errno != EINVAL)
  411. reply = wpas_dispatch_network_method(message, wpa_s,
  412. nid);
  413. else
  414. reply = wpas_dbus_new_invalid_network_error(message);
  415. } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
  416. /* A method for one of this interface's scanned BSSIDs */
  417. reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
  418. } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
  419. /* A method for an interface only. */
  420. if (!strcmp(method, "scan"))
  421. reply = wpas_dbus_iface_scan(message, wpa_s);
  422. else if (!strcmp(method, "scanResults"))
  423. reply = wpas_dbus_iface_scan_results(message, wpa_s);
  424. else if (!strcmp(method, "addNetwork"))
  425. reply = wpas_dbus_iface_add_network(message, wpa_s);
  426. else if (!strcmp(method, "removeNetwork"))
  427. reply = wpas_dbus_iface_remove_network(message, wpa_s);
  428. else if (!strcmp(method, "selectNetwork"))
  429. reply = wpas_dbus_iface_select_network(message, wpa_s);
  430. else if (!strcmp(method, "capabilities"))
  431. reply = wpas_dbus_iface_capabilities(message, wpa_s);
  432. else if (!strcmp(method, "disconnect"))
  433. reply = wpas_dbus_iface_disconnect(message, wpa_s);
  434. else if (!strcmp(method, "setAPScan"))
  435. reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
  436. else if (!strcmp(method, "setSmartcardModules"))
  437. reply = wpas_dbus_iface_set_smartcard_modules(message,
  438. wpa_s);
  439. else if (!strcmp(method, "state"))
  440. reply = wpas_dbus_iface_get_state(message, wpa_s);
  441. else if (!strcmp(method, "setBlobs"))
  442. reply = wpas_dbus_iface_set_blobs(message, wpa_s);
  443. else if (!strcmp(method, "removeBlobs"))
  444. reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
  445. }
  446. /* If the message was handled, send back the reply */
  447. if (reply) {
  448. if (!dbus_message_get_no_reply(message))
  449. dbus_connection_send(connection, reply, NULL);
  450. dbus_message_unref(reply);
  451. }
  452. out:
  453. free(iface_obj_path);
  454. free(network);
  455. free(bssid);
  456. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  457. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  458. }
  459. /**
  460. * wpas_message_handler - dispatch incoming dbus messages
  461. * @connection: connection to the system message bus
  462. * @message: an incoming dbus message
  463. * @user_data: a pointer to a dbus control interface data structure
  464. * Returns: whether or not the message was handled
  465. *
  466. * This function dispatches all incoming dbus messages to the correct
  467. * handlers, depending on what the message's target object path is,
  468. * and what the method call is.
  469. */
  470. static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
  471. DBusMessage *message, void *user_data)
  472. {
  473. struct ctrl_iface_dbus_priv *ctrl_iface = user_data;
  474. const char *method;
  475. const char *path;
  476. const char *msg_interface;
  477. DBusMessage *reply = NULL;
  478. method = dbus_message_get_member(message);
  479. path = dbus_message_get_path(message);
  480. msg_interface = dbus_message_get_interface(message);
  481. if (!method || !path || !ctrl_iface || !msg_interface)
  482. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  483. /* Validate the method interface */
  484. if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
  485. return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  486. if (!strcmp(path, WPAS_DBUS_PATH)) {
  487. /* dispatch methods against our global dbus interface here */
  488. if (!strcmp(method, "addInterface")) {
  489. reply = wpas_dbus_global_add_interface(
  490. message, ctrl_iface->global);
  491. } else if (!strcmp(method, "removeInterface")) {
  492. reply = wpas_dbus_global_remove_interface(
  493. message, ctrl_iface->global);
  494. } else if (!strcmp(method, "getInterface")) {
  495. reply = wpas_dbus_global_get_interface(
  496. message, ctrl_iface->global);
  497. }
  498. }
  499. /* If the message was handled, send back the reply */
  500. if (reply) {
  501. if (!dbus_message_get_no_reply(message))
  502. dbus_connection_send(connection, reply, NULL);
  503. dbus_message_unref(reply);
  504. }
  505. return reply ? DBUS_HANDLER_RESULT_HANDLED :
  506. DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  507. }
  508. /**
  509. * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
  510. * @wpa_s: %wpa_supplicant network interface data
  511. * Returns: 0 on success, -1 on failure
  512. *
  513. * Notify listeners that this interface has updated scan results.
  514. */
  515. void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
  516. {
  517. struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
  518. DBusMessage *_signal;
  519. const char *path;
  520. /* Do nothing if the control interface is not turned on */
  521. if (iface == NULL)
  522. return;
  523. path = wpa_supplicant_get_dbus_path(wpa_s);
  524. if (path == NULL) {
  525. perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
  526. "interface didn't have a dbus path");
  527. wpa_printf(MSG_ERROR,
  528. "wpa_supplicant_dbus_notify_scan_results[dbus]: "
  529. "interface didn't have a dbus path; can't send "
  530. "scan result signal.");
  531. return;
  532. }
  533. _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
  534. "ScanResultsAvailable");
  535. if (_signal == NULL) {
  536. perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
  537. "couldn't create dbus signal; likely out of memory");
  538. wpa_printf(MSG_ERROR, "dbus control interface: not enough "
  539. "memory to send scan results signal.");
  540. return;
  541. }
  542. dbus_connection_send(iface->con, _signal, NULL);
  543. dbus_message_unref(_signal);
  544. }
  545. /**
  546. * wpa_supplicant_dbus_notify_state_change - Send a state change signal
  547. * @wpa_s: %wpa_supplicant network interface data
  548. * @new_state: new state wpa_supplicant is entering
  549. * @old_state: old state wpa_supplicant is leaving
  550. * Returns: 0 on success, -1 on failure
  551. *
  552. * Notify listeners that wpa_supplicant has changed state
  553. */
  554. void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
  555. wpa_states new_state,
  556. wpa_states old_state)
  557. {
  558. struct ctrl_iface_dbus_priv *iface;
  559. DBusMessage *_signal = NULL;
  560. const char *path;
  561. const char *new_state_str, *old_state_str;
  562. /* Do nothing if the control interface is not turned on */
  563. if (wpa_s->global == NULL)
  564. return;
  565. iface = wpa_s->global->dbus_ctrl_iface;
  566. if (iface == NULL)
  567. return;
  568. /* Only send signal if state really changed */
  569. if (new_state == old_state)
  570. return;
  571. path = wpa_supplicant_get_dbus_path(wpa_s);
  572. if (path == NULL) {
  573. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  574. "interface didn't have a dbus path");
  575. wpa_printf(MSG_ERROR,
  576. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  577. "interface didn't have a dbus path; can't send "
  578. "signal.");
  579. return;
  580. }
  581. _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
  582. "StateChange");
  583. if (_signal == NULL) {
  584. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  585. "couldn't create dbus signal; likely out of memory");
  586. wpa_printf(MSG_ERROR,
  587. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  588. "couldn't create dbus signal; likely out of "
  589. "memory.");
  590. return;
  591. }
  592. new_state_str = wpa_supplicant_state_txt(new_state);
  593. old_state_str = wpa_supplicant_state_txt(old_state);
  594. if (new_state_str == NULL || old_state_str == NULL) {
  595. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  596. "couldn't convert state strings");
  597. wpa_printf(MSG_ERROR,
  598. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  599. "couldn't convert state strings.");
  600. goto out;
  601. }
  602. if (!dbus_message_append_args(_signal,
  603. DBUS_TYPE_STRING, &new_state_str,
  604. DBUS_TYPE_STRING, &old_state_str,
  605. DBUS_TYPE_INVALID)) {
  606. perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
  607. "not enough memory to construct state change signal.");
  608. wpa_printf(MSG_ERROR,
  609. "wpa_supplicant_dbus_notify_state_change[dbus]: "
  610. "not enough memory to construct state change "
  611. "signal.");
  612. goto out;
  613. }
  614. dbus_connection_send(iface->con, _signal, NULL);
  615. out:
  616. dbus_message_unref(_signal);
  617. }
  618. #ifdef CONFIG_WPS
  619. void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
  620. const struct wps_credential *cred)
  621. {
  622. struct ctrl_iface_dbus_priv *iface;
  623. DBusMessage *_signal = NULL;
  624. const char *path;
  625. /* Do nothing if the control interface is not turned on */
  626. if (wpa_s->global == NULL)
  627. return;
  628. iface = wpa_s->global->dbus_ctrl_iface;
  629. if (iface == NULL)
  630. return;
  631. path = wpa_supplicant_get_dbus_path(wpa_s);
  632. if (path == NULL) {
  633. perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  634. "interface didn't have a dbus path");
  635. wpa_printf(MSG_ERROR,
  636. "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  637. "interface didn't have a dbus path; can't send "
  638. "signal.");
  639. return;
  640. }
  641. _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
  642. "WpsCred");
  643. if (_signal == NULL) {
  644. perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  645. "couldn't create dbus signal; likely out of memory");
  646. wpa_printf(MSG_ERROR,
  647. "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  648. "couldn't create dbus signal; likely out of "
  649. "memory.");
  650. return;
  651. }
  652. if (!dbus_message_append_args(_signal,
  653. DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
  654. &cred->cred_attr, cred->cred_attr_len,
  655. DBUS_TYPE_INVALID)) {
  656. perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  657. "not enough memory to construct signal.");
  658. wpa_printf(MSG_ERROR,
  659. "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
  660. "not enough memory to construct signal.");
  661. goto out;
  662. }
  663. dbus_connection_send(iface->con, _signal, NULL);
  664. out:
  665. dbus_message_unref(_signal);
  666. }
  667. #endif /* CONFIG_WPS */
  668. /**
  669. * integrate_with_eloop - Register our mainloop integration with dbus
  670. * @connection: connection to the system message bus
  671. * @iface: a dbus control interface data structure
  672. * Returns: 0 on success, -1 on failure
  673. *
  674. * We register our mainloop integration functions with dbus here.
  675. */
  676. static int integrate_with_eloop(DBusConnection *connection,
  677. struct ctrl_iface_dbus_priv *iface)
  678. {
  679. if (!dbus_connection_set_watch_functions(connection, add_watch,
  680. remove_watch, watch_toggled,
  681. iface, NULL)) {
  682. perror("dbus_connection_set_watch_functions[dbus]");
  683. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  684. return -1;
  685. }
  686. if (!dbus_connection_set_timeout_functions(connection, add_timeout,
  687. remove_timeout,
  688. timeout_toggled, iface,
  689. NULL)) {
  690. perror("dbus_connection_set_timeout_functions[dbus]");
  691. wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
  692. return -1;
  693. }
  694. if (connection_setup_wakeup_main(iface) < 0) {
  695. perror("connection_setup_wakeup_main[dbus]");
  696. wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
  697. return -1;
  698. }
  699. return 0;
  700. }
  701. /**
  702. * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
  703. * claiming bus name
  704. * @eloop_ctx: the DBusConnection to dispatch on
  705. * @timeout_ctx: unused
  706. *
  707. * If clients are quick to notice that wpa_supplicant claimed its bus name,
  708. * there may have been messages that came in before initialization was
  709. * all finished. Dispatch those here.
  710. */
  711. static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
  712. {
  713. DBusConnection *con = eloop_ctx;
  714. while (dbus_connection_get_dispatch_status(con) ==
  715. DBUS_DISPATCH_DATA_REMAINS)
  716. dbus_connection_dispatch(con);
  717. }
  718. /**
  719. * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
  720. * @global: Pointer to global data from wpa_supplicant_init()
  721. * Returns: Pointer to dbus_ctrl_iface date or %NULL on failure
  722. *
  723. * Initialize the dbus control interface and start receiving commands from
  724. * external programs over the bus.
  725. */
  726. struct ctrl_iface_dbus_priv *
  727. wpa_supplicant_dbus_ctrl_iface_init(struct wpa_global *global)
  728. {
  729. struct ctrl_iface_dbus_priv *iface;
  730. DBusError error;
  731. int ret = -1;
  732. DBusObjectPathVTable wpas_vtable = {
  733. NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
  734. };
  735. iface = os_zalloc(sizeof(struct ctrl_iface_dbus_priv));
  736. if (iface == NULL)
  737. return NULL;
  738. iface->global = global;
  739. /* Get a reference to the system bus */
  740. dbus_error_init(&error);
  741. iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
  742. dbus_error_free(&error);
  743. if (!iface->con) {
  744. perror("dbus_bus_get[ctrl_iface_dbus]");
  745. wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
  746. goto fail;
  747. }
  748. /* Tell dbus about our mainloop integration functions */
  749. if (integrate_with_eloop(iface->con, iface))
  750. goto fail;
  751. /* Register the message handler for the global dbus interface */
  752. if (!dbus_connection_register_object_path(iface->con,
  753. WPAS_DBUS_PATH, &wpas_vtable,
  754. iface)) {
  755. perror("dbus_connection_register_object_path[dbus]");
  756. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  757. "handler.");
  758. goto fail;
  759. }
  760. /* Register our service with the message bus */
  761. dbus_error_init(&error);
  762. switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
  763. 0, &error)) {
  764. case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
  765. ret = 0;
  766. break;
  767. case DBUS_REQUEST_NAME_REPLY_EXISTS:
  768. case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
  769. case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
  770. perror("dbus_bus_request_name[dbus]");
  771. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  772. "already registered.");
  773. break;
  774. default:
  775. perror("dbus_bus_request_name[dbus]");
  776. wpa_printf(MSG_ERROR, "Could not request DBus service name: "
  777. "%s %s.", error.name, error.message);
  778. break;
  779. }
  780. dbus_error_free(&error);
  781. if (ret != 0)
  782. goto fail;
  783. wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
  784. "'.");
  785. /*
  786. * Dispatch initial DBus messages that may have come in since the bus
  787. * name was claimed above. Happens when clients are quick to notice the
  788. * wpa_supplicant service.
  789. *
  790. * FIXME: is there a better solution to this problem?
  791. */
  792. eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
  793. iface->con, NULL);
  794. return iface;
  795. fail:
  796. wpa_supplicant_dbus_ctrl_iface_deinit(iface);
  797. return NULL;
  798. }
  799. /**
  800. * wpa_supplicant_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
  801. * @iface: Pointer to dbus private data from
  802. * wpa_supplicant_dbus_ctrl_iface_init()
  803. *
  804. * Deinitialize the dbus control interface that was initialized with
  805. * wpa_supplicant_dbus_ctrl_iface_init().
  806. */
  807. void wpa_supplicant_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_priv *iface)
  808. {
  809. if (iface == NULL)
  810. return;
  811. if (iface->con) {
  812. eloop_cancel_timeout(dispatch_initial_dbus_messages,
  813. iface->con, NULL);
  814. dbus_connection_set_watch_functions(iface->con, NULL, NULL,
  815. NULL, NULL, NULL);
  816. dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
  817. NULL, NULL, NULL);
  818. dbus_connection_unref(iface->con);
  819. }
  820. memset(iface, 0, sizeof(struct ctrl_iface_dbus_priv));
  821. free(iface);
  822. }
  823. /**
  824. * wpas_dbus_register_new_iface - Register a new interface with dbus
  825. * @wpa_s: %wpa_supplicant interface description structure to register
  826. * Returns: 0 on success, -1 on error
  827. *
  828. * Registers a new interface with dbus and assigns it a dbus object path.
  829. */
  830. int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
  831. {
  832. struct ctrl_iface_dbus_priv *ctrl_iface =
  833. wpa_s->global->dbus_ctrl_iface;
  834. DBusConnection * con;
  835. u32 next;
  836. DBusObjectPathVTable vtable = {
  837. NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
  838. };
  839. char *path;
  840. int ret = -1;
  841. /* Do nothing if the control interface is not turned on */
  842. if (ctrl_iface == NULL)
  843. return 0;
  844. con = ctrl_iface->con;
  845. next = wpa_supplicant_dbus_next_objid(ctrl_iface);
  846. /* Create and set the interface's object path */
  847. path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  848. if (path == NULL)
  849. return -1;
  850. snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  851. WPAS_DBUS_PATH_INTERFACES "/%u",
  852. next);
  853. if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
  854. wpa_printf(MSG_DEBUG,
  855. "Failed to set dbus path for interface %s",
  856. wpa_s->ifname);
  857. goto out;
  858. }
  859. /* Register the message handler for the interface functions */
  860. if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
  861. perror("wpas_dbus_register_iface [dbus]");
  862. wpa_printf(MSG_ERROR, "Could not set up DBus message "
  863. "handler for interface %s.", wpa_s->ifname);
  864. goto out;
  865. }
  866. ret = 0;
  867. out:
  868. free(path);
  869. return ret;
  870. }
  871. /**
  872. * wpas_dbus_unregister_iface - Unregister an interface from dbus
  873. * @wpa_s: wpa_supplicant interface structure
  874. * Returns: 0 on success, -1 on failure
  875. *
  876. * Unregisters the interface with dbus
  877. */
  878. int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
  879. {
  880. struct ctrl_iface_dbus_priv *ctrl_iface;
  881. DBusConnection *con;
  882. const char *path;
  883. /* Do nothing if the control interface is not turned on */
  884. if (wpa_s == NULL || wpa_s->global == NULL)
  885. return 0;
  886. ctrl_iface = wpa_s->global->dbus_ctrl_iface;
  887. if (ctrl_iface == NULL)
  888. return 0;
  889. con = ctrl_iface->con;
  890. path = wpa_supplicant_get_dbus_path(wpa_s);
  891. if (!dbus_connection_unregister_object_path(con, path))
  892. return -1;
  893. free(wpa_s->dbus_path);
  894. wpa_s->dbus_path = NULL;
  895. return 0;
  896. }
  897. /**
  898. * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
  899. * @global: Pointer to global data from wpa_supplicant_init()
  900. * @path: Pointer to a dbus object path representing an interface
  901. * Returns: Pointer to the interface or %NULL if not found
  902. */
  903. struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
  904. struct wpa_global *global, const char *path)
  905. {
  906. struct wpa_supplicant *wpa_s;
  907. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  908. if (strcmp(wpa_s->dbus_path, path) == 0)
  909. return wpa_s;
  910. }
  911. return NULL;
  912. }
  913. /**
  914. * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
  915. * @wpa_s: wpa_supplicant interface structure
  916. * @path: dbus path to set on the interface
  917. * Returns: 0 on succes, -1 on error
  918. */
  919. int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
  920. const char *path)
  921. {
  922. u32 len = strlen (path);
  923. if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
  924. return -1;
  925. if (wpa_s->dbus_path)
  926. return -1;
  927. wpa_s->dbus_path = strdup(path);
  928. return 0;
  929. }
  930. /**
  931. * wpa_supplicant_get_dbus_path - Get an interface's dbus path
  932. * @wpa_s: %wpa_supplicant interface structure
  933. * Returns: Interface's dbus object path, or %NULL on error
  934. */
  935. const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
  936. {
  937. return wpa_s->dbus_path;
  938. }