ctrl_iface_dbus_handlers.c 45 KB

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