ctrl_iface_dbus.c 31 KB

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