ctrl_iface_dbus_handlers.c 44 KB

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