ctrl_iface_dbus_handlers.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  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 "config.h"
  17. #include "wpa_supplicant_i.h"
  18. #include "driver_i.h"
  19. #include "ctrl_iface_dbus.h"
  20. #include "ctrl_iface_dbus_handlers.h"
  21. #include "notify.h"
  22. #include "eap_peer/eap_methods.h"
  23. #include "dbus_dict_helpers.h"
  24. #include "ieee802_11_defs.h"
  25. #include "wpas_glue.h"
  26. #include "eapol_supp/eapol_supp_sm.h"
  27. #include "wps_supplicant.h"
  28. extern int wpa_debug_level;
  29. extern int wpa_debug_show_keys;
  30. extern int wpa_debug_timestamp;
  31. /**
  32. * wpas_dbus_new_invalid_opts_error - Return a new invalid options error message
  33. * @message: Pointer to incoming dbus message this error refers to
  34. * Returns: a dbus error message
  35. *
  36. * Convenience function to create and return an invalid options error
  37. */
  38. static DBusMessage * wpas_dbus_new_invalid_opts_error(DBusMessage *message,
  39. const char *arg)
  40. {
  41. DBusMessage *reply;
  42. reply = dbus_message_new_error(message, WPAS_ERROR_INVALID_OPTS,
  43. "Did not receive correct message "
  44. "arguments.");
  45. if (arg != NULL)
  46. dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
  47. DBUS_TYPE_INVALID);
  48. return reply;
  49. }
  50. /**
  51. * wpas_dbus_new_success_reply - Return a new success reply message
  52. * @message: Pointer to incoming dbus message this reply refers to
  53. * Returns: a dbus message containing a single UINT32 that indicates
  54. * success (ie, a value of 1)
  55. *
  56. * Convenience function to create and return a success reply message
  57. */
  58. static DBusMessage * wpas_dbus_new_success_reply(DBusMessage *message)
  59. {
  60. DBusMessage *reply;
  61. unsigned int success = 1;
  62. reply = dbus_message_new_method_return(message);
  63. dbus_message_append_args(reply, DBUS_TYPE_UINT32, &success,
  64. DBUS_TYPE_INVALID);
  65. return reply;
  66. }
  67. static void wpas_dbus_free_wpa_interface(struct wpa_interface *iface)
  68. {
  69. free((char *) iface->driver);
  70. free((char *) iface->driver_param);
  71. free((char *) iface->confname);
  72. free((char *) iface->bridge_ifname);
  73. }
  74. /**
  75. * wpas_dbus_global_add_interface - Request registration of a network interface
  76. * @message: Pointer to incoming dbus message
  77. * @global: %wpa_supplicant global data structure
  78. * Returns: The object path of the new interface object,
  79. * or a dbus error message with more information
  80. *
  81. * Handler function for "addInterface" method call. Handles requests
  82. * by dbus clients to register a network interface that wpa_supplicant
  83. * will manage.
  84. */
  85. DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,
  86. struct wpa_global *global)
  87. {
  88. struct wpa_interface iface;
  89. char *ifname = NULL;
  90. DBusMessage *reply = NULL;
  91. DBusMessageIter iter;
  92. memset(&iface, 0, sizeof(iface));
  93. dbus_message_iter_init(message, &iter);
  94. /* First argument: interface name (DBUS_TYPE_STRING)
  95. * Required; must be non-zero length
  96. */
  97. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
  98. goto error;
  99. dbus_message_iter_get_basic(&iter, &ifname);
  100. if (!strlen(ifname))
  101. goto error;
  102. iface.ifname = ifname;
  103. /* Second argument: dict of options */
  104. if (dbus_message_iter_next(&iter)) {
  105. DBusMessageIter iter_dict;
  106. struct wpa_dbus_dict_entry entry;
  107. if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
  108. goto error;
  109. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  110. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  111. goto error;
  112. if (!strcmp(entry.key, "driver") &&
  113. (entry.type == DBUS_TYPE_STRING)) {
  114. iface.driver = strdup(entry.str_value);
  115. if (iface.driver == NULL)
  116. goto error;
  117. } else if (!strcmp(entry.key, "driver-params") &&
  118. (entry.type == DBUS_TYPE_STRING)) {
  119. iface.driver_param = strdup(entry.str_value);
  120. if (iface.driver_param == NULL)
  121. goto error;
  122. } else if (!strcmp(entry.key, "config-file") &&
  123. (entry.type == DBUS_TYPE_STRING)) {
  124. iface.confname = strdup(entry.str_value);
  125. if (iface.confname == NULL)
  126. goto error;
  127. } else if (!strcmp(entry.key, "bridge-ifname") &&
  128. (entry.type == DBUS_TYPE_STRING)) {
  129. iface.bridge_ifname = strdup(entry.str_value);
  130. if (iface.bridge_ifname == NULL)
  131. goto error;
  132. } else {
  133. wpa_dbus_dict_entry_clear(&entry);
  134. goto error;
  135. }
  136. wpa_dbus_dict_entry_clear(&entry);
  137. }
  138. }
  139. /*
  140. * Try to get the wpa_supplicant record for this iface, return
  141. * an error if we already control it.
  142. */
  143. if (wpa_supplicant_get_iface(global, iface.ifname) != NULL) {
  144. reply = dbus_message_new_error(message,
  145. WPAS_ERROR_EXISTS_ERROR,
  146. "wpa_supplicant already "
  147. "controls this interface.");
  148. } else {
  149. struct wpa_supplicant *wpa_s;
  150. /* Otherwise, have wpa_supplicant attach to it. */
  151. if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
  152. const char *path = wpa_supplicant_get_dbus_path(wpa_s);
  153. reply = dbus_message_new_method_return(message);
  154. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  155. &path, DBUS_TYPE_INVALID);
  156. } else {
  157. reply = dbus_message_new_error(message,
  158. WPAS_ERROR_ADD_ERROR,
  159. "wpa_supplicant "
  160. "couldn't grab this "
  161. "interface.");
  162. }
  163. }
  164. wpas_dbus_free_wpa_interface(&iface);
  165. return reply;
  166. error:
  167. wpas_dbus_free_wpa_interface(&iface);
  168. return wpas_dbus_new_invalid_opts_error(message, NULL);
  169. }
  170. /**
  171. * wpas_dbus_global_remove_interface - Request deregistration of an interface
  172. * @message: Pointer to incoming dbus message
  173. * @global: wpa_supplicant global data structure
  174. * Returns: a dbus message containing a UINT32 indicating success (1) or
  175. * failure (0), or returns a dbus error message with more information
  176. *
  177. * Handler function for "removeInterface" method call. Handles requests
  178. * by dbus clients to deregister a network interface that wpa_supplicant
  179. * currently manages.
  180. */
  181. DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,
  182. struct wpa_global *global)
  183. {
  184. struct wpa_supplicant *wpa_s;
  185. char *path;
  186. DBusMessage *reply = NULL;
  187. if (!dbus_message_get_args(message, NULL,
  188. DBUS_TYPE_OBJECT_PATH, &path,
  189. DBUS_TYPE_INVALID)) {
  190. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  191. goto out;
  192. }
  193. wpa_s = wpa_supplicant_get_iface_by_dbus_path(global, path);
  194. if (wpa_s == NULL) {
  195. reply = wpas_dbus_new_invalid_iface_error(message);
  196. goto out;
  197. }
  198. if (!wpa_supplicant_remove_iface(global, wpa_s)) {
  199. reply = wpas_dbus_new_success_reply(message);
  200. } else {
  201. reply = dbus_message_new_error(message,
  202. WPAS_ERROR_REMOVE_ERROR,
  203. "wpa_supplicant couldn't "
  204. "remove this interface.");
  205. }
  206. out:
  207. return reply;
  208. }
  209. /**
  210. * wpas_dbus_global_get_interface - Get the object path for an interface name
  211. * @message: Pointer to incoming dbus message
  212. * @global: %wpa_supplicant global data structure
  213. * Returns: The object path of the interface object,
  214. * or a dbus error message with more information
  215. *
  216. * Handler function for "getInterface" method call. Handles requests
  217. * by dbus clients for the object path of an specific network interface.
  218. */
  219. DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,
  220. struct wpa_global *global)
  221. {
  222. DBusMessage *reply = NULL;
  223. const char *ifname;
  224. const char *path;
  225. struct wpa_supplicant *wpa_s;
  226. if (!dbus_message_get_args(message, NULL,
  227. DBUS_TYPE_STRING, &ifname,
  228. DBUS_TYPE_INVALID)) {
  229. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  230. goto out;
  231. }
  232. wpa_s = wpa_supplicant_get_iface(global, ifname);
  233. if (wpa_s == NULL) {
  234. reply = wpas_dbus_new_invalid_iface_error(message);
  235. goto out;
  236. }
  237. path = wpa_supplicant_get_dbus_path(wpa_s);
  238. if (path == NULL) {
  239. reply = dbus_message_new_error(message,
  240. WPAS_ERROR_INTERNAL_ERROR,
  241. "an internal error occurred "
  242. "getting the interface.");
  243. goto out;
  244. }
  245. reply = dbus_message_new_method_return(message);
  246. dbus_message_append_args(reply,
  247. DBUS_TYPE_OBJECT_PATH, &path,
  248. DBUS_TYPE_INVALID);
  249. out:
  250. return reply;
  251. }
  252. /**
  253. * wpas_dbus_global_set_debugparams- Set the debug params
  254. * @message: Pointer to incoming dbus message
  255. * @global: %wpa_supplicant global data structure
  256. * Returns: a dbus message containing a UINT32 indicating success (1) or
  257. * failure (0), or returns a dbus error message with more information
  258. *
  259. * Handler function for "setDebugParams" method call. Handles requests
  260. * by dbus clients for the object path of an specific network interface.
  261. */
  262. DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,
  263. struct wpa_global *global)
  264. {
  265. DBusMessage *reply = NULL;
  266. int debug_level;
  267. dbus_bool_t debug_timestamp;
  268. dbus_bool_t debug_show_keys;
  269. if (!dbus_message_get_args(message, NULL,
  270. DBUS_TYPE_INT32, &debug_level,
  271. DBUS_TYPE_BOOLEAN, &debug_timestamp,
  272. DBUS_TYPE_BOOLEAN, &debug_show_keys,
  273. DBUS_TYPE_INVALID)) {
  274. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  275. goto out;
  276. }
  277. /* check for allowed debuglevels */
  278. if (debug_level != MSG_MSGDUMP &&
  279. debug_level != MSG_DEBUG &&
  280. debug_level != MSG_INFO &&
  281. debug_level != MSG_WARNING &&
  282. debug_level != MSG_ERROR) {
  283. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  284. goto out;
  285. }
  286. wpa_debug_level = debug_level;
  287. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  288. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  289. reply = wpas_dbus_new_success_reply(message);
  290. out:
  291. return reply;
  292. }
  293. /**
  294. * wpas_dbus_iface_scan - Request a wireless scan on an interface
  295. * @message: Pointer to incoming dbus message
  296. * @wpa_s: wpa_supplicant structure for a network interface
  297. * Returns: a dbus message containing a UINT32 indicating success (1) or
  298. * failure (0)
  299. *
  300. * Handler function for "scan" method call of a network device. Requests
  301. * that wpa_supplicant perform a wireless scan as soon as possible
  302. * on a particular wireless interface.
  303. */
  304. DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,
  305. struct wpa_supplicant *wpa_s)
  306. {
  307. wpa_s->scan_req = 2;
  308. wpa_supplicant_req_scan(wpa_s, 0, 0);
  309. return wpas_dbus_new_success_reply(message);
  310. }
  311. /**
  312. * wpas_dbus_iface_scan_results - Get the results of a recent scan request
  313. * @message: Pointer to incoming dbus message
  314. * @wpa_s: wpa_supplicant structure for a network interface
  315. * Returns: a dbus message containing a dbus array of objects paths, or returns
  316. * a dbus error message if not scan results could be found
  317. *
  318. * Handler function for "scanResults" method call of a network device. Returns
  319. * a dbus message containing the object paths of wireless networks found.
  320. */
  321. DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,
  322. struct wpa_supplicant *wpa_s)
  323. {
  324. DBusMessage *reply = NULL;
  325. DBusMessageIter iter;
  326. DBusMessageIter sub_iter;
  327. size_t i;
  328. /* Ensure we've actually got scan results to return */
  329. if (wpa_s->scan_res == NULL &&
  330. wpa_supplicant_get_scan_results(wpa_s) < 0) {
  331. reply = dbus_message_new_error(message, WPAS_ERROR_SCAN_ERROR,
  332. "An error ocurred getting scan "
  333. "results.");
  334. goto out;
  335. }
  336. /* Create and initialize the return message */
  337. reply = dbus_message_new_method_return(message);
  338. dbus_message_iter_init_append(reply, &iter);
  339. dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
  340. DBUS_TYPE_OBJECT_PATH_AS_STRING,
  341. &sub_iter);
  342. /* Loop through scan results and append each result's object path */
  343. for (i = 0; i < wpa_s->scan_res->num; i++) {
  344. struct wpa_scan_res *res = wpa_s->scan_res->res[i];
  345. char *path;
  346. path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  347. if (path == NULL) {
  348. perror("wpas_dbus_iface_scan_results[dbus]: out of "
  349. "memory.");
  350. wpa_printf(MSG_ERROR, "dbus control interface: not "
  351. "enough memory to send scan results "
  352. "signal.");
  353. break;
  354. }
  355. /* Construct the object path for this network. Note that ':'
  356. * is not a valid character in dbus object paths.
  357. */
  358. snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  359. "%s/" WPAS_DBUS_BSSIDS_PART "/"
  360. WPAS_DBUS_BSSID_FORMAT,
  361. wpa_supplicant_get_dbus_path(wpa_s),
  362. MAC2STR(res->bssid));
  363. dbus_message_iter_append_basic(&sub_iter,
  364. DBUS_TYPE_OBJECT_PATH, &path);
  365. free(path);
  366. }
  367. dbus_message_iter_close_container(&iter, &sub_iter);
  368. out:
  369. return reply;
  370. }
  371. /**
  372. * wpas_dbus_bssid_properties - Return the properties of a scanned network
  373. * @message: Pointer to incoming dbus message
  374. * @wpa_s: wpa_supplicant structure for a network interface
  375. * @res: wpa_supplicant scan result for which to get properties
  376. * Returns: a dbus message containing the properties for the requested network
  377. *
  378. * Handler function for "properties" method call of a scanned network.
  379. * Returns a dbus message containing the the properties.
  380. */
  381. DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,
  382. struct wpa_supplicant *wpa_s,
  383. struct wpa_scan_res *res)
  384. {
  385. DBusMessage *reply = NULL;
  386. DBusMessageIter iter, iter_dict;
  387. const u8 *ie;
  388. /* Dump the properties into a dbus message */
  389. reply = dbus_message_new_method_return(message);
  390. dbus_message_iter_init_append(reply, &iter);
  391. if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
  392. goto error;
  393. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "bssid",
  394. (const char *) res->bssid,
  395. ETH_ALEN))
  396. goto error;
  397. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  398. if (ie) {
  399. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "ssid",
  400. (const char *) (ie + 2),
  401. ie[1]))
  402. goto error;
  403. }
  404. ie = wpa_scan_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
  405. if (ie) {
  406. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpaie",
  407. (const char *) ie,
  408. ie[1] + 2))
  409. goto error;
  410. }
  411. ie = wpa_scan_get_ie(res, WLAN_EID_RSN);
  412. if (ie) {
  413. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "rsnie",
  414. (const char *) ie,
  415. ie[1] + 2))
  416. goto error;
  417. }
  418. ie = wpa_scan_get_vendor_ie(res, WPS_IE_VENDOR_TYPE);
  419. if (ie) {
  420. if (!wpa_dbus_dict_append_byte_array(&iter_dict, "wpsie",
  421. (const char *) ie,
  422. ie[1] + 2))
  423. goto error;
  424. }
  425. if (res->freq) {
  426. if (!wpa_dbus_dict_append_int32(&iter_dict, "frequency",
  427. res->freq))
  428. goto error;
  429. }
  430. if (!wpa_dbus_dict_append_uint16(&iter_dict, "capabilities",
  431. res->caps))
  432. goto error;
  433. if (!(res->flags & WPA_SCAN_QUAL_INVALID) &&
  434. !wpa_dbus_dict_append_int32(&iter_dict, "quality", res->qual))
  435. goto error;
  436. if (!(res->flags & WPA_SCAN_NOISE_INVALID) &&
  437. !wpa_dbus_dict_append_int32(&iter_dict, "noise", res->noise))
  438. goto error;
  439. if (!(res->flags & WPA_SCAN_LEVEL_INVALID) &&
  440. !wpa_dbus_dict_append_int32(&iter_dict, "level", res->level))
  441. goto error;
  442. if (!wpa_dbus_dict_append_int32(&iter_dict, "maxrate",
  443. wpa_scan_get_max_rate(res) * 500000))
  444. goto error;
  445. if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
  446. goto error;
  447. return reply;
  448. error:
  449. if (reply)
  450. dbus_message_unref(reply);
  451. return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
  452. "an internal error occurred returning "
  453. "BSSID properties.");
  454. }
  455. /**
  456. * wpas_dbus_iface_capabilities - Return interface capabilities
  457. * @message: Pointer to incoming dbus message
  458. * @wpa_s: wpa_supplicant structure for a network interface
  459. * Returns: A dbus message containing a dict of strings
  460. *
  461. * Handler function for "capabilities" method call of an interface.
  462. */
  463. DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,
  464. struct wpa_supplicant *wpa_s)
  465. {
  466. DBusMessage *reply = NULL;
  467. struct wpa_driver_capa capa;
  468. int res;
  469. DBusMessageIter iter, iter_dict;
  470. char **eap_methods;
  471. size_t num_items;
  472. dbus_bool_t strict = FALSE;
  473. DBusMessageIter iter_dict_entry, iter_dict_val, iter_array;
  474. if (!dbus_message_get_args(message, NULL,
  475. DBUS_TYPE_BOOLEAN, &strict,
  476. DBUS_TYPE_INVALID))
  477. strict = FALSE;
  478. reply = dbus_message_new_method_return(message);
  479. dbus_message_iter_init_append(reply, &iter);
  480. if (!wpa_dbus_dict_open_write(&iter, &iter_dict))
  481. goto error;
  482. /* EAP methods */
  483. eap_methods = eap_get_names_as_string_array(&num_items);
  484. if (eap_methods) {
  485. dbus_bool_t success = FALSE;
  486. size_t i = 0;
  487. success = wpa_dbus_dict_append_string_array(
  488. &iter_dict, "eap", (const char **) eap_methods,
  489. num_items);
  490. /* free returned method array */
  491. while (eap_methods[i])
  492. free(eap_methods[i++]);
  493. free(eap_methods);
  494. if (!success)
  495. goto error;
  496. }
  497. res = wpa_drv_get_capa(wpa_s, &capa);
  498. /***** pairwise cipher */
  499. if (res < 0) {
  500. if (!strict) {
  501. const char *args[] = {"CCMP", "TKIP", "NONE"};
  502. if (!wpa_dbus_dict_append_string_array(
  503. &iter_dict, "pairwise", args,
  504. sizeof(args) / sizeof(char*)))
  505. goto error;
  506. }
  507. } else {
  508. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "pairwise",
  509. &iter_dict_entry,
  510. &iter_dict_val,
  511. &iter_array))
  512. goto error;
  513. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  514. if (!wpa_dbus_dict_string_array_add_element(
  515. &iter_array, "CCMP"))
  516. goto error;
  517. }
  518. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  519. if (!wpa_dbus_dict_string_array_add_element(
  520. &iter_array, "TKIP"))
  521. goto error;
  522. }
  523. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  524. if (!wpa_dbus_dict_string_array_add_element(
  525. &iter_array, "NONE"))
  526. goto error;
  527. }
  528. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  529. &iter_dict_entry,
  530. &iter_dict_val,
  531. &iter_array))
  532. goto error;
  533. }
  534. /***** group cipher */
  535. if (res < 0) {
  536. if (!strict) {
  537. const char *args[] = {
  538. "CCMP", "TKIP", "WEP104", "WEP40"
  539. };
  540. if (!wpa_dbus_dict_append_string_array(
  541. &iter_dict, "group", args,
  542. sizeof(args) / sizeof(char*)))
  543. goto error;
  544. }
  545. } else {
  546. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "group",
  547. &iter_dict_entry,
  548. &iter_dict_val,
  549. &iter_array))
  550. goto error;
  551. if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  552. if (!wpa_dbus_dict_string_array_add_element(
  553. &iter_array, "CCMP"))
  554. goto error;
  555. }
  556. if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  557. if (!wpa_dbus_dict_string_array_add_element(
  558. &iter_array, "TKIP"))
  559. goto error;
  560. }
  561. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  562. if (!wpa_dbus_dict_string_array_add_element(
  563. &iter_array, "WEP104"))
  564. goto error;
  565. }
  566. if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  567. if (!wpa_dbus_dict_string_array_add_element(
  568. &iter_array, "WEP40"))
  569. goto error;
  570. }
  571. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  572. &iter_dict_entry,
  573. &iter_dict_val,
  574. &iter_array))
  575. goto error;
  576. }
  577. /***** key management */
  578. if (res < 0) {
  579. if (!strict) {
  580. const char *args[] = {
  581. "WPA-PSK", "WPA-EAP", "IEEE8021X", "WPA-NONE",
  582. "NONE"
  583. };
  584. if (!wpa_dbus_dict_append_string_array(
  585. &iter_dict, "key_mgmt", args,
  586. sizeof(args) / sizeof(char*)))
  587. goto error;
  588. }
  589. } else {
  590. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "key_mgmt",
  591. &iter_dict_entry,
  592. &iter_dict_val,
  593. &iter_array))
  594. goto error;
  595. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  596. "NONE"))
  597. goto error;
  598. if (!wpa_dbus_dict_string_array_add_element(&iter_array,
  599. "IEEE8021X"))
  600. goto error;
  601. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  602. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  603. if (!wpa_dbus_dict_string_array_add_element(
  604. &iter_array, "WPA-EAP"))
  605. goto error;
  606. }
  607. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  608. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  609. if (!wpa_dbus_dict_string_array_add_element(
  610. &iter_array, "WPA-PSK"))
  611. goto error;
  612. }
  613. if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  614. if (!wpa_dbus_dict_string_array_add_element(
  615. &iter_array, "WPA-NONE"))
  616. goto error;
  617. }
  618. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  619. &iter_dict_entry,
  620. &iter_dict_val,
  621. &iter_array))
  622. goto error;
  623. }
  624. /***** WPA protocol */
  625. if (res < 0) {
  626. if (!strict) {
  627. const char *args[] = { "RSN", "WPA" };
  628. if (!wpa_dbus_dict_append_string_array(
  629. &iter_dict, "proto", args,
  630. sizeof(args) / sizeof(char*)))
  631. goto error;
  632. }
  633. } else {
  634. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "proto",
  635. &iter_dict_entry,
  636. &iter_dict_val,
  637. &iter_array))
  638. goto error;
  639. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  640. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  641. if (!wpa_dbus_dict_string_array_add_element(
  642. &iter_array, "RSN"))
  643. goto error;
  644. }
  645. if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  646. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  647. if (!wpa_dbus_dict_string_array_add_element(
  648. &iter_array, "WPA"))
  649. goto error;
  650. }
  651. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  652. &iter_dict_entry,
  653. &iter_dict_val,
  654. &iter_array))
  655. goto error;
  656. }
  657. /***** auth alg */
  658. if (res < 0) {
  659. if (!strict) {
  660. const char *args[] = { "OPEN", "SHARED", "LEAP" };
  661. if (!wpa_dbus_dict_append_string_array(
  662. &iter_dict, "auth_alg", args,
  663. sizeof(args) / sizeof(char*)))
  664. goto error;
  665. }
  666. } else {
  667. if (!wpa_dbus_dict_begin_string_array(&iter_dict, "auth_alg",
  668. &iter_dict_entry,
  669. &iter_dict_val,
  670. &iter_array))
  671. goto error;
  672. if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
  673. if (!wpa_dbus_dict_string_array_add_element(
  674. &iter_array, "OPEN"))
  675. goto error;
  676. }
  677. if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
  678. if (!wpa_dbus_dict_string_array_add_element(
  679. &iter_array, "SHARED"))
  680. goto error;
  681. }
  682. if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
  683. if (!wpa_dbus_dict_string_array_add_element(
  684. &iter_array, "LEAP"))
  685. goto error;
  686. }
  687. if (!wpa_dbus_dict_end_string_array(&iter_dict,
  688. &iter_dict_entry,
  689. &iter_dict_val,
  690. &iter_array))
  691. goto error;
  692. }
  693. if (!wpa_dbus_dict_close_write(&iter, &iter_dict))
  694. goto error;
  695. return reply;
  696. error:
  697. if (reply)
  698. dbus_message_unref(reply);
  699. return dbus_message_new_error(message, WPAS_ERROR_INTERNAL_ERROR,
  700. "an internal error occurred returning "
  701. "interface capabilities.");
  702. }
  703. /**
  704. * wpas_dbus_iface_add_network - Add a new configured network
  705. * @message: Pointer to incoming dbus message
  706. * @wpa_s: wpa_supplicant structure for a network interface
  707. * Returns: A dbus message containing the object path of the new network
  708. *
  709. * Handler function for "addNetwork" method call of a network interface.
  710. */
  711. DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,
  712. struct wpa_supplicant *wpa_s)
  713. {
  714. DBusMessage *reply = NULL;
  715. struct wpa_ssid *ssid;
  716. char *path = NULL;
  717. path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
  718. if (path == NULL) {
  719. perror("wpas_dbus_iface_scan_results[dbus]: out of "
  720. "memory.");
  721. wpa_printf(MSG_ERROR, "dbus control interface: not "
  722. "enough memory to send scan results "
  723. "signal.");
  724. goto out;
  725. }
  726. ssid = wpa_config_add_network(wpa_s->conf);
  727. if (ssid == NULL) {
  728. reply = dbus_message_new_error(message,
  729. WPAS_ERROR_ADD_NETWORK_ERROR,
  730. "wpa_supplicant could not add "
  731. "a network on this interface.");
  732. goto out;
  733. }
  734. wpas_notify_network_added(wpa_s, ssid);
  735. ssid->disabled = 1;
  736. wpa_config_set_network_defaults(ssid);
  737. /* Construct the object path for this network. */
  738. snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
  739. "%s/" WPAS_DBUS_NETWORKS_PART "/%d",
  740. wpa_supplicant_get_dbus_path(wpa_s),
  741. ssid->id);
  742. reply = dbus_message_new_method_return(message);
  743. dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
  744. &path, DBUS_TYPE_INVALID);
  745. out:
  746. free(path);
  747. return reply;
  748. }
  749. /**
  750. * wpas_dbus_iface_remove_network - Remove a configured network
  751. * @message: Pointer to incoming dbus message
  752. * @wpa_s: wpa_supplicant structure for a network interface
  753. * Returns: A dbus message containing a UINT32 indicating success (1) or
  754. * failure (0)
  755. *
  756. * Handler function for "removeNetwork" method call of a network interface.
  757. */
  758. DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,
  759. struct wpa_supplicant *wpa_s)
  760. {
  761. DBusMessage *reply = NULL;
  762. const char *op;
  763. char *iface = NULL, *net_id = NULL;
  764. int id;
  765. struct wpa_ssid *ssid;
  766. if (!dbus_message_get_args(message, NULL,
  767. DBUS_TYPE_OBJECT_PATH, &op,
  768. DBUS_TYPE_INVALID)) {
  769. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  770. goto out;
  771. }
  772. /* Extract the network ID */
  773. iface = wpas_dbus_decompose_object_path(op, &net_id, NULL);
  774. if (iface == NULL) {
  775. reply = wpas_dbus_new_invalid_network_error(message);
  776. goto out;
  777. }
  778. /* Ensure the network is actually a child of this interface */
  779. if (strcmp(iface, wpa_supplicant_get_dbus_path(wpa_s)) != 0) {
  780. reply = wpas_dbus_new_invalid_network_error(message);
  781. goto out;
  782. }
  783. id = strtoul(net_id, NULL, 10);
  784. ssid = wpa_config_get_network(wpa_s->conf, id);
  785. if (ssid == NULL) {
  786. reply = wpas_dbus_new_invalid_network_error(message);
  787. goto out;
  788. }
  789. wpas_notify_network_removed(wpa_s, ssid);
  790. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  791. reply = dbus_message_new_error(message,
  792. WPAS_ERROR_REMOVE_NETWORK_ERROR,
  793. "error removing the specified "
  794. "on this interface.");
  795. goto out;
  796. }
  797. if (ssid == wpa_s->current_ssid)
  798. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  799. reply = wpas_dbus_new_success_reply(message);
  800. out:
  801. free(iface);
  802. free(net_id);
  803. return reply;
  804. }
  805. static const char *dont_quote[] = {
  806. "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
  807. "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
  808. "bssid", NULL
  809. };
  810. static dbus_bool_t should_quote_opt(const char *key)
  811. {
  812. int i = 0;
  813. while (dont_quote[i] != NULL) {
  814. if (strcmp(key, dont_quote[i]) == 0)
  815. return FALSE;
  816. i++;
  817. }
  818. return TRUE;
  819. }
  820. /**
  821. * wpas_dbus_iface_set_network - Set options for a configured network
  822. * @message: Pointer to incoming dbus message
  823. * @wpa_s: wpa_supplicant structure for a network interface
  824. * @ssid: wpa_ssid structure for a configured network
  825. * Returns: a dbus message containing a UINT32 indicating success (1) or
  826. * failure (0)
  827. *
  828. * Handler function for "set" method call of a configured network.
  829. */
  830. DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,
  831. struct wpa_supplicant *wpa_s,
  832. struct wpa_ssid *ssid)
  833. {
  834. DBusMessage *reply = NULL;
  835. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  836. DBusMessageIter iter, iter_dict;
  837. dbus_message_iter_init(message, &iter);
  838. if (!wpa_dbus_dict_open_read(&iter, &iter_dict)) {
  839. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  840. goto out;
  841. }
  842. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  843. char *value = NULL;
  844. size_t size = 50;
  845. int ret;
  846. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  847. reply = wpas_dbus_new_invalid_opts_error(message,
  848. NULL);
  849. goto out;
  850. }
  851. /* Type conversions, since wpa_supplicant wants strings */
  852. if (entry.type == DBUS_TYPE_ARRAY &&
  853. entry.array_type == DBUS_TYPE_BYTE) {
  854. if (entry.array_len <= 0)
  855. goto error;
  856. size = entry.array_len * 2 + 1;
  857. value = os_zalloc(size);
  858. if (value == NULL)
  859. goto error;
  860. ret = wpa_snprintf_hex(value, size,
  861. (u8 *) entry.bytearray_value,
  862. entry.array_len);
  863. if (ret <= 0)
  864. goto error;
  865. } else if (entry.type == DBUS_TYPE_STRING) {
  866. if (should_quote_opt(entry.key)) {
  867. size = strlen(entry.str_value);
  868. /* Zero-length option check */
  869. if (size <= 0)
  870. goto error;
  871. size += 3; /* For quotes and terminator */
  872. value = os_zalloc(size);
  873. if (value == NULL)
  874. goto error;
  875. ret = snprintf(value, size, "\"%s\"",
  876. entry.str_value);
  877. if (ret < 0 || (size_t) ret != (size - 1))
  878. goto error;
  879. } else {
  880. value = strdup(entry.str_value);
  881. if (value == NULL)
  882. goto error;
  883. }
  884. } else if (entry.type == DBUS_TYPE_UINT32) {
  885. value = os_zalloc(size);
  886. if (value == NULL)
  887. goto error;
  888. ret = snprintf(value, size, "%u", entry.uint32_value);
  889. if (ret <= 0)
  890. goto error;
  891. } else if (entry.type == DBUS_TYPE_INT32) {
  892. value = os_zalloc(size);
  893. if (value == NULL)
  894. goto error;
  895. ret = snprintf(value, size, "%d", entry.int32_value);
  896. if (ret <= 0)
  897. goto error;
  898. } else
  899. goto error;
  900. if (wpa_config_set(ssid, entry.key, value, 0) < 0)
  901. goto error;
  902. if ((strcmp(entry.key, "psk") == 0 &&
  903. value[0] == '"' && ssid->ssid_len) ||
  904. (strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
  905. wpa_config_update_psk(ssid);
  906. free(value);
  907. wpa_dbus_dict_entry_clear(&entry);
  908. continue;
  909. error:
  910. free(value);
  911. reply = wpas_dbus_new_invalid_opts_error(message, entry.key);
  912. wpa_dbus_dict_entry_clear(&entry);
  913. break;
  914. }
  915. if (!reply)
  916. reply = wpas_dbus_new_success_reply(message);
  917. out:
  918. return reply;
  919. }
  920. /**
  921. * wpas_dbus_iface_enable_network - Mark a configured network as enabled
  922. * @message: Pointer to incoming dbus message
  923. * @wpa_s: wpa_supplicant structure for a network interface
  924. * @ssid: wpa_ssid structure for a configured network
  925. * Returns: A dbus message containing a UINT32 indicating success (1) or
  926. * failure (0)
  927. *
  928. * Handler function for "enable" method call of a configured network.
  929. */
  930. DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,
  931. struct wpa_supplicant *wpa_s,
  932. struct wpa_ssid *ssid)
  933. {
  934. if (wpa_s->current_ssid == NULL && ssid->disabled) {
  935. /*
  936. * Try to reassociate since there is no current configuration
  937. * and a new network was made available.
  938. */
  939. wpa_s->reassociate = 1;
  940. wpa_supplicant_req_scan(wpa_s, 0, 0);
  941. }
  942. ssid->disabled = 0;
  943. return wpas_dbus_new_success_reply(message);
  944. }
  945. /**
  946. * wpas_dbus_iface_disable_network - Mark a configured network as disabled
  947. * @message: Pointer to incoming dbus message
  948. * @wpa_s: wpa_supplicant structure for a network interface
  949. * @ssid: wpa_ssid structure for a configured network
  950. * Returns: A dbus message containing a UINT32 indicating success (1) or
  951. * failure (0)
  952. *
  953. * Handler function for "disable" method call of a configured network.
  954. */
  955. DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,
  956. struct wpa_supplicant *wpa_s,
  957. struct wpa_ssid *ssid)
  958. {
  959. if (ssid == wpa_s->current_ssid)
  960. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  961. ssid->disabled = 1;
  962. return wpas_dbus_new_success_reply(message);
  963. }
  964. /**
  965. * wpas_dbus_iface_select_network - Attempt association with a configured network
  966. * @message: Pointer to incoming dbus message
  967. * @wpa_s: wpa_supplicant structure for a network interface
  968. * Returns: A dbus message containing a UINT32 indicating success (1) or
  969. * failure (0)
  970. *
  971. * Handler function for "selectNetwork" method call of network interface.
  972. */
  973. DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,
  974. struct wpa_supplicant *wpa_s)
  975. {
  976. DBusMessage *reply = NULL;
  977. const char *op;
  978. struct wpa_ssid *ssid;
  979. char *iface_obj_path = NULL;
  980. char *network = NULL;
  981. if (strlen(dbus_message_get_signature(message)) == 0) {
  982. /* Any network */
  983. ssid = wpa_s->conf->ssid;
  984. while (ssid) {
  985. ssid->disabled = 0;
  986. ssid = ssid->next;
  987. }
  988. wpa_s->reassociate = 1;
  989. wpa_supplicant_req_scan(wpa_s, 0, 0);
  990. } else {
  991. const char *obj_path;
  992. int nid;
  993. if (!dbus_message_get_args(message, NULL,
  994. DBUS_TYPE_OBJECT_PATH, &op,
  995. DBUS_TYPE_INVALID)) {
  996. reply = wpas_dbus_new_invalid_opts_error(message,
  997. NULL);
  998. goto out;
  999. }
  1000. /* Extract the network number */
  1001. iface_obj_path = wpas_dbus_decompose_object_path(op,
  1002. &network,
  1003. NULL);
  1004. if (iface_obj_path == NULL) {
  1005. reply = wpas_dbus_new_invalid_iface_error(message);
  1006. goto out;
  1007. }
  1008. /* Ensure the object path really points to this interface */
  1009. obj_path = wpa_supplicant_get_dbus_path(wpa_s);
  1010. if (strcmp(iface_obj_path, obj_path) != 0) {
  1011. reply = wpas_dbus_new_invalid_network_error(message);
  1012. goto out;
  1013. }
  1014. nid = strtoul(network, NULL, 10);
  1015. if (errno == EINVAL) {
  1016. reply = wpas_dbus_new_invalid_network_error(message);
  1017. goto out;
  1018. }
  1019. ssid = wpa_config_get_network(wpa_s->conf, nid);
  1020. if (ssid == NULL) {
  1021. reply = wpas_dbus_new_invalid_network_error(message);
  1022. goto out;
  1023. }
  1024. /* Finally, associate with the network */
  1025. if (ssid != wpa_s->current_ssid && wpa_s->current_ssid)
  1026. wpa_supplicant_disassociate(
  1027. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1028. /* Mark all other networks disabled and trigger reassociation
  1029. */
  1030. ssid = wpa_s->conf->ssid;
  1031. while (ssid) {
  1032. ssid->disabled = (nid != ssid->id);
  1033. ssid = ssid->next;
  1034. }
  1035. wpa_s->disconnected = 0;
  1036. wpa_s->reassociate = 1;
  1037. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1038. }
  1039. reply = wpas_dbus_new_success_reply(message);
  1040. out:
  1041. free(iface_obj_path);
  1042. free(network);
  1043. return reply;
  1044. }
  1045. /**
  1046. * wpas_dbus_iface_disconnect - Terminate the current connection
  1047. * @message: Pointer to incoming dbus message
  1048. * @wpa_s: wpa_supplicant structure for a network interface
  1049. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1050. * failure (0)
  1051. *
  1052. * Handler function for "disconnect" method call of network interface.
  1053. */
  1054. DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,
  1055. struct wpa_supplicant *wpa_s)
  1056. {
  1057. wpa_s->disconnected = 1;
  1058. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1059. return wpas_dbus_new_success_reply(message);
  1060. }
  1061. /**
  1062. * wpas_dbus_iface_set_ap_scan - Control roaming mode
  1063. * @message: Pointer to incoming dbus message
  1064. * @wpa_s: wpa_supplicant structure for a network interface
  1065. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1066. * failure (0)
  1067. *
  1068. * Handler function for "setAPScan" method call.
  1069. */
  1070. DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,
  1071. struct wpa_supplicant *wpa_s)
  1072. {
  1073. DBusMessage *reply = NULL;
  1074. dbus_uint32_t ap_scan = 1;
  1075. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &ap_scan,
  1076. DBUS_TYPE_INVALID)) {
  1077. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  1078. goto out;
  1079. }
  1080. if (ap_scan > 2) {
  1081. reply = wpas_dbus_new_invalid_opts_error(message, NULL);
  1082. goto out;
  1083. }
  1084. wpa_s->conf->ap_scan = ap_scan;
  1085. reply = wpas_dbus_new_success_reply(message);
  1086. out:
  1087. return reply;
  1088. }
  1089. /**
  1090. * wpas_dbus_iface_set_smartcard_modules - Set smartcard related module paths
  1091. * @message: Pointer to incoming dbus message
  1092. * @wpa_s: wpa_supplicant structure for a network interface
  1093. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1094. * failure (0)
  1095. *
  1096. * Handler function for "setSmartcardModules" method call.
  1097. */
  1098. DBusMessage * wpas_dbus_iface_set_smartcard_modules(
  1099. DBusMessage *message, struct wpa_supplicant *wpa_s)
  1100. {
  1101. DBusMessageIter iter, iter_dict;
  1102. char *opensc_engine_path = NULL;
  1103. char *pkcs11_engine_path = NULL;
  1104. char *pkcs11_module_path = NULL;
  1105. struct wpa_dbus_dict_entry entry;
  1106. if (!dbus_message_iter_init(message, &iter))
  1107. goto error;
  1108. if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
  1109. goto error;
  1110. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1111. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
  1112. goto error;
  1113. if (!strcmp(entry.key, "opensc_engine_path") &&
  1114. (entry.type == DBUS_TYPE_STRING)) {
  1115. opensc_engine_path = os_strdup(entry.str_value);
  1116. if (opensc_engine_path == NULL)
  1117. goto error;
  1118. } else if (!strcmp(entry.key, "pkcs11_engine_path") &&
  1119. (entry.type == DBUS_TYPE_STRING)) {
  1120. pkcs11_engine_path = os_strdup(entry.str_value);
  1121. if (pkcs11_engine_path == NULL)
  1122. goto error;
  1123. } else if (!strcmp(entry.key, "pkcs11_module_path") &&
  1124. (entry.type == DBUS_TYPE_STRING)) {
  1125. pkcs11_module_path = os_strdup(entry.str_value);
  1126. if (pkcs11_module_path == NULL)
  1127. goto error;
  1128. } else {
  1129. wpa_dbus_dict_entry_clear(&entry);
  1130. goto error;
  1131. }
  1132. wpa_dbus_dict_entry_clear(&entry);
  1133. }
  1134. #ifdef EAP_TLS_OPENSSL
  1135. os_free(wpa_s->conf->opensc_engine_path);
  1136. wpa_s->conf->opensc_engine_path = opensc_engine_path;
  1137. os_free(wpa_s->conf->pkcs11_engine_path);
  1138. wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path;
  1139. os_free(wpa_s->conf->pkcs11_module_path);
  1140. wpa_s->conf->pkcs11_module_path = pkcs11_module_path;
  1141. #endif /* EAP_TLS_OPENSSL */
  1142. eapol_sm_deinit(wpa_s->eapol);
  1143. wpa_supplicant_init_eapol(wpa_s);
  1144. return wpas_dbus_new_success_reply(message);
  1145. error:
  1146. os_free(opensc_engine_path);
  1147. os_free(pkcs11_engine_path);
  1148. os_free(pkcs11_module_path);
  1149. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1150. }
  1151. /**
  1152. * wpas_dbus_iface_get_state - Get interface state
  1153. * @message: Pointer to incoming dbus message
  1154. * @wpa_s: wpa_supplicant structure for a network interface
  1155. * Returns: A dbus message containing a STRING representing the current
  1156. * interface state
  1157. *
  1158. * Handler function for "state" method call.
  1159. */
  1160. DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,
  1161. struct wpa_supplicant *wpa_s)
  1162. {
  1163. DBusMessage *reply = NULL;
  1164. const char *str_state;
  1165. reply = dbus_message_new_method_return(message);
  1166. if (reply != NULL) {
  1167. str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
  1168. dbus_message_append_args(reply, DBUS_TYPE_STRING, &str_state,
  1169. DBUS_TYPE_INVALID);
  1170. }
  1171. return reply;
  1172. }
  1173. /**
  1174. * wpas_dbus_iface_get_scanning - Get interface scanning state
  1175. * @message: Pointer to incoming dbus message
  1176. * @wpa_s: wpa_supplicant structure for a network interface
  1177. * Returns: A dbus message containing whether the interface is scanning
  1178. *
  1179. * Handler function for "scanning" method call.
  1180. */
  1181. DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,
  1182. struct wpa_supplicant *wpa_s)
  1183. {
  1184. DBusMessage *reply = NULL;
  1185. dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
  1186. reply = dbus_message_new_method_return(message);
  1187. if (reply != NULL) {
  1188. dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &scanning,
  1189. DBUS_TYPE_INVALID);
  1190. } else {
  1191. perror("wpas_dbus_iface_get_scanning[dbus]: out of "
  1192. "memory.");
  1193. wpa_printf(MSG_ERROR, "dbus control interface: not enough "
  1194. "memory to return scanning state.");
  1195. }
  1196. return reply;
  1197. }
  1198. /**
  1199. * wpas_dbus_iface_set_blobs - Store named binary blobs (ie, for certificates)
  1200. * @message: Pointer to incoming dbus message
  1201. * @wpa_s: %wpa_supplicant data structure
  1202. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1203. * failure (0)
  1204. *
  1205. * Asks wpa_supplicant to internally store a one or more binary blobs.
  1206. */
  1207. DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,
  1208. struct wpa_supplicant *wpa_s)
  1209. {
  1210. DBusMessage *reply = NULL;
  1211. struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
  1212. DBusMessageIter iter, iter_dict;
  1213. dbus_message_iter_init(message, &iter);
  1214. if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
  1215. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1216. while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
  1217. struct wpa_config_blob *blob;
  1218. if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
  1219. reply = wpas_dbus_new_invalid_opts_error(message,
  1220. NULL);
  1221. break;
  1222. }
  1223. if (entry.type != DBUS_TYPE_ARRAY ||
  1224. entry.array_type != DBUS_TYPE_BYTE) {
  1225. reply = wpas_dbus_new_invalid_opts_error(
  1226. message, "Byte array expected.");
  1227. break;
  1228. }
  1229. if ((entry.array_len <= 0) || (entry.array_len > 65536) ||
  1230. !strlen(entry.key)) {
  1231. reply = wpas_dbus_new_invalid_opts_error(
  1232. message, "Invalid array size.");
  1233. break;
  1234. }
  1235. blob = os_zalloc(sizeof(*blob));
  1236. if (blob == NULL) {
  1237. reply = dbus_message_new_error(
  1238. message, WPAS_ERROR_ADD_ERROR,
  1239. "Not enough memory to add blob.");
  1240. break;
  1241. }
  1242. blob->data = os_zalloc(entry.array_len);
  1243. if (blob->data == NULL) {
  1244. reply = dbus_message_new_error(
  1245. message, WPAS_ERROR_ADD_ERROR,
  1246. "Not enough memory to add blob data.");
  1247. os_free(blob);
  1248. break;
  1249. }
  1250. blob->name = os_strdup(entry.key);
  1251. blob->len = entry.array_len;
  1252. os_memcpy(blob->data, (u8 *) entry.bytearray_value,
  1253. entry.array_len);
  1254. if (blob->name == NULL || blob->data == NULL) {
  1255. wpa_config_free_blob(blob);
  1256. reply = dbus_message_new_error(
  1257. message, WPAS_ERROR_ADD_ERROR,
  1258. "Error adding blob.");
  1259. break;
  1260. }
  1261. /* Success */
  1262. if (!wpa_config_remove_blob(wpa_s->conf, blob->name))
  1263. wpas_notify_blob_removed(wpa_s, blob->name);
  1264. wpa_config_set_blob(wpa_s->conf, blob);
  1265. wpas_notify_blob_added(wpa_s, blob->name);
  1266. wpa_dbus_dict_entry_clear(&entry);
  1267. }
  1268. wpa_dbus_dict_entry_clear(&entry);
  1269. return reply ? reply : wpas_dbus_new_success_reply(message);
  1270. }
  1271. /**
  1272. * wpas_dbus_iface_remove_blob - Remove named binary blobs
  1273. * @message: Pointer to incoming dbus message
  1274. * @wpa_s: %wpa_supplicant data structure
  1275. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1276. * failure (0)
  1277. *
  1278. * Asks wpa_supplicant to remove one or more previously stored binary blobs.
  1279. */
  1280. DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,
  1281. struct wpa_supplicant *wpa_s)
  1282. {
  1283. DBusMessageIter iter, array;
  1284. char *err_msg = NULL;
  1285. dbus_message_iter_init(message, &iter);
  1286. if ((dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) ||
  1287. (dbus_message_iter_get_element_type (&iter) != DBUS_TYPE_STRING))
  1288. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1289. dbus_message_iter_recurse(&iter, &array);
  1290. while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
  1291. const char *name;
  1292. dbus_message_iter_get_basic(&array, &name);
  1293. if (!strlen(name))
  1294. err_msg = "Invalid blob name.";
  1295. if (wpa_config_remove_blob(wpa_s->conf, name) != 0)
  1296. err_msg = "Error removing blob.";
  1297. else
  1298. wpas_notify_blob_removed(wpa_s, name);
  1299. dbus_message_iter_next(&array);
  1300. }
  1301. if (err_msg) {
  1302. return dbus_message_new_error(message, WPAS_ERROR_REMOVE_ERROR,
  1303. err_msg);
  1304. }
  1305. return wpas_dbus_new_success_reply(message);
  1306. }
  1307. #ifdef CONFIG_WPS
  1308. /**
  1309. * wpas_dbus_iface_wps_pbc - Request credentials using WPS PBC method
  1310. * @message: Pointer to incoming dbus message
  1311. * @wpa_s: %wpa_supplicant data structure
  1312. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1313. * failure (0)
  1314. *
  1315. * Handler function for "wpsPbc" method call
  1316. */
  1317. DBusMessage * wpas_dbus_iface_wps_pbc(DBusMessage *message,
  1318. struct wpa_supplicant *wpa_s)
  1319. {
  1320. char *arg_bssid = NULL;
  1321. u8 bssid[ETH_ALEN];
  1322. int ret = 0;
  1323. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
  1324. DBUS_TYPE_INVALID))
  1325. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1326. if (!os_strcmp(arg_bssid, "any"))
  1327. ret = wpas_wps_start_pbc(wpa_s, NULL);
  1328. else if (!hwaddr_aton(arg_bssid, bssid))
  1329. ret = wpas_wps_start_pbc(wpa_s, bssid);
  1330. else {
  1331. return wpas_dbus_new_invalid_opts_error(message,
  1332. "Invalid BSSID");
  1333. }
  1334. if (ret < 0) {
  1335. return dbus_message_new_error(message,
  1336. WPAS_ERROR_WPS_PBC_ERROR,
  1337. "Could not start PBC "
  1338. "negotiation");
  1339. }
  1340. return wpas_dbus_new_success_reply(message);
  1341. }
  1342. /**
  1343. * wpas_dbus_iface_wps_pin - Establish the PIN number of the enrollee
  1344. * @message: Pointer to incoming dbus message
  1345. * @wpa_s: %wpa_supplicant data structure
  1346. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1347. * failure (0)
  1348. *
  1349. * Handler function for "wpsPin" method call
  1350. */
  1351. DBusMessage * wpas_dbus_iface_wps_pin(DBusMessage *message,
  1352. struct wpa_supplicant *wpa_s)
  1353. {
  1354. DBusMessage *reply = NULL;
  1355. char *arg_bssid;
  1356. char *pin = NULL;
  1357. u8 bssid[ETH_ALEN], *_bssid = NULL;
  1358. int ret = 0;
  1359. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
  1360. DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
  1361. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1362. if (!os_strcmp(arg_bssid, "any"))
  1363. _bssid = NULL;
  1364. else if (!hwaddr_aton(arg_bssid, bssid))
  1365. _bssid = bssid;
  1366. else {
  1367. return wpas_dbus_new_invalid_opts_error(message,
  1368. "Invalid BSSID");
  1369. }
  1370. if (os_strlen(pin) > 0)
  1371. ret = wpas_wps_start_pin(wpa_s, _bssid, pin);
  1372. else
  1373. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL);
  1374. if (ret < 0) {
  1375. return dbus_message_new_error(message,
  1376. WPAS_ERROR_WPS_PIN_ERROR,
  1377. "Could not init PIN");
  1378. }
  1379. reply = dbus_message_new_method_return(message);
  1380. if (reply == NULL)
  1381. return NULL;
  1382. if (ret == 0) {
  1383. dbus_message_append_args(reply, DBUS_TYPE_STRING, &pin,
  1384. DBUS_TYPE_INVALID);
  1385. } else {
  1386. char npin[9];
  1387. sprintf(npin, "%08d", ret);
  1388. dbus_message_append_args(reply, DBUS_TYPE_STRING, &npin,
  1389. DBUS_TYPE_INVALID);
  1390. }
  1391. return reply;
  1392. }
  1393. /**
  1394. * wpas_dbus_iface_wps_reg - Request credentials using the PIN of the AP
  1395. * @message: Pointer to incoming dbus message
  1396. * @wpa_s: %wpa_supplicant data structure
  1397. * Returns: A dbus message containing a UINT32 indicating success (1) or
  1398. * failure (0)
  1399. *
  1400. * Handler function for "wpsReg" method call
  1401. */
  1402. DBusMessage * wpas_dbus_iface_wps_reg(DBusMessage *message,
  1403. struct wpa_supplicant *wpa_s)
  1404. {
  1405. char *arg_bssid;
  1406. char *pin = NULL;
  1407. u8 bssid[ETH_ALEN];
  1408. int ret = 0;
  1409. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
  1410. DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
  1411. return wpas_dbus_new_invalid_opts_error(message, NULL);
  1412. if (!os_strcmp(arg_bssid, "any"))
  1413. ret = wpas_wps_start_reg(wpa_s, NULL, pin, NULL);
  1414. else if (!hwaddr_aton(arg_bssid, bssid))
  1415. ret = wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1416. else {
  1417. return wpas_dbus_new_invalid_opts_error(message,
  1418. "Invalid BSSID");
  1419. }
  1420. if (ret < 0) {
  1421. return dbus_message_new_error(message,
  1422. WPAS_ERROR_WPS_PBC_ERROR,
  1423. "Could not request credentials");
  1424. }
  1425. return wpas_dbus_new_success_reply(message);
  1426. }
  1427. #endif /* CONFIG_WPS */