dbus_new_handlers_wps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * WPA Supplicant / dbus-based control interface (WPS)
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "../config.h"
  12. #include "../wpa_supplicant_i.h"
  13. #include "../wps_supplicant.h"
  14. #include "../driver_i.h"
  15. #include "../ap.h"
  16. #include "dbus_new_helpers.h"
  17. #include "dbus_new.h"
  18. #include "dbus_new_handlers.h"
  19. #include "dbus_dict_helpers.h"
  20. struct wps_start_params {
  21. int role; /* 0 - not set, 1 - enrollee, 2 - registrar */
  22. int type; /* 0 - not set, 1 - pin, 2 - pbc */
  23. u8 *bssid;
  24. char *pin;
  25. u8 *p2p_dev_addr;
  26. };
  27. static int wpas_dbus_handler_wps_role(DBusMessage *message,
  28. DBusMessageIter *entry_iter,
  29. struct wps_start_params *params,
  30. DBusMessage **reply)
  31. {
  32. DBusMessageIter variant_iter;
  33. char *val;
  34. dbus_message_iter_recurse(entry_iter, &variant_iter);
  35. if (dbus_message_iter_get_arg_type(&variant_iter) !=
  36. DBUS_TYPE_STRING) {
  37. wpa_printf(MSG_DEBUG,
  38. "dbus: WPS.Start - Wrong Role type, string required");
  39. *reply = wpas_dbus_error_invalid_args(message,
  40. "Role must be a string");
  41. return -1;
  42. }
  43. dbus_message_iter_get_basic(&variant_iter, &val);
  44. if (os_strcmp(val, "enrollee") == 0)
  45. params->role = 1;
  46. else if (os_strcmp(val, "registrar") == 0)
  47. params->role = 2;
  48. else {
  49. wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Uknown role %s", val);
  50. *reply = wpas_dbus_error_invalid_args(message, val);
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. static int wpas_dbus_handler_wps_type(DBusMessage *message,
  56. DBusMessageIter *entry_iter,
  57. struct wps_start_params *params,
  58. DBusMessage **reply)
  59. {
  60. DBusMessageIter variant_iter;
  61. char *val;
  62. dbus_message_iter_recurse(entry_iter, &variant_iter);
  63. if (dbus_message_iter_get_arg_type(&variant_iter) != DBUS_TYPE_STRING) {
  64. wpa_printf(MSG_DEBUG,
  65. "dbus: WPS.Start - Wrong Type type, string required");
  66. *reply = wpas_dbus_error_invalid_args(message,
  67. "Type must be a string");
  68. return -1;
  69. }
  70. dbus_message_iter_get_basic(&variant_iter, &val);
  71. if (os_strcmp(val, "pin") == 0)
  72. params->type = 1;
  73. else if (os_strcmp(val, "pbc") == 0)
  74. params->type = 2;
  75. else {
  76. wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Unknown type %s",
  77. val);
  78. *reply = wpas_dbus_error_invalid_args(message, val);
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. static int wpas_dbus_handler_wps_bssid(DBusMessage *message,
  84. DBusMessageIter *entry_iter,
  85. struct wps_start_params *params,
  86. DBusMessage **reply)
  87. {
  88. DBusMessageIter variant_iter, array_iter;
  89. int len;
  90. dbus_message_iter_recurse(entry_iter, &variant_iter);
  91. if (dbus_message_iter_get_arg_type(&variant_iter) != DBUS_TYPE_ARRAY ||
  92. dbus_message_iter_get_element_type(&variant_iter) !=
  93. DBUS_TYPE_BYTE) {
  94. wpa_printf(MSG_DEBUG,
  95. "dbus: WPS.Start - Wrong Bssid type, byte array required");
  96. *reply = wpas_dbus_error_invalid_args(
  97. message, "Bssid must be a byte array");
  98. return -1;
  99. }
  100. dbus_message_iter_recurse(&variant_iter, &array_iter);
  101. dbus_message_iter_get_fixed_array(&array_iter, &params->bssid, &len);
  102. if (len != ETH_ALEN) {
  103. wpa_printf(MSG_DEBUG, "dbus: WPS.Stsrt - Wrong Bssid length %d",
  104. len);
  105. *reply = wpas_dbus_error_invalid_args(message,
  106. "Bssid is wrong length");
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static int wpas_dbus_handler_wps_pin(DBusMessage *message,
  112. DBusMessageIter *entry_iter,
  113. struct wps_start_params *params,
  114. DBusMessage **reply)
  115. {
  116. DBusMessageIter variant_iter;
  117. dbus_message_iter_recurse(entry_iter, &variant_iter);
  118. if (dbus_message_iter_get_arg_type(&variant_iter) != DBUS_TYPE_STRING) {
  119. wpa_printf(MSG_DEBUG,
  120. "dbus: WPS.Start - Wrong Pin type, string required");
  121. *reply = wpas_dbus_error_invalid_args(message,
  122. "Pin must be a string");
  123. return -1;
  124. }
  125. dbus_message_iter_get_basic(&variant_iter, &params->pin);
  126. return 0;
  127. }
  128. #ifdef CONFIG_P2P
  129. static int wpas_dbus_handler_wps_p2p_dev_addr(DBusMessage *message,
  130. DBusMessageIter *entry_iter,
  131. struct wps_start_params *params,
  132. DBusMessage **reply)
  133. {
  134. DBusMessageIter variant_iter, array_iter;
  135. int len;
  136. dbus_message_iter_recurse(entry_iter, &variant_iter);
  137. if (dbus_message_iter_get_arg_type(&variant_iter) != DBUS_TYPE_ARRAY ||
  138. dbus_message_iter_get_element_type(&variant_iter) !=
  139. DBUS_TYPE_BYTE) {
  140. wpa_printf(MSG_DEBUG,
  141. "dbus: WPS.Start - Wrong P2PDeviceAddress type, byte array required");
  142. *reply = wpas_dbus_error_invalid_args(
  143. message, "P2PDeviceAddress must be a byte array");
  144. return -1;
  145. }
  146. dbus_message_iter_recurse(&variant_iter, &array_iter);
  147. dbus_message_iter_get_fixed_array(&array_iter, &params->p2p_dev_addr,
  148. &len);
  149. if (len != ETH_ALEN) {
  150. wpa_printf(MSG_DEBUG,
  151. "dbus: WPS.Start - Wrong P2PDeviceAddress length %d",
  152. len);
  153. *reply = wpas_dbus_error_invalid_args(
  154. message, "P2PDeviceAddress has wrong length");
  155. return -1;
  156. }
  157. return 0;
  158. }
  159. #endif /* CONFIG_P2P */
  160. static int wpas_dbus_handler_wps_start_entry(DBusMessage *message, char *key,
  161. DBusMessageIter *entry_iter,
  162. struct wps_start_params *params,
  163. DBusMessage **reply)
  164. {
  165. if (os_strcmp(key, "Role") == 0)
  166. return wpas_dbus_handler_wps_role(message, entry_iter,
  167. params, reply);
  168. else if (os_strcmp(key, "Type") == 0)
  169. return wpas_dbus_handler_wps_type(message, entry_iter,
  170. params, reply);
  171. else if (os_strcmp(key, "Bssid") == 0)
  172. return wpas_dbus_handler_wps_bssid(message, entry_iter,
  173. params, reply);
  174. else if (os_strcmp(key, "Pin") == 0)
  175. return wpas_dbus_handler_wps_pin(message, entry_iter,
  176. params, reply);
  177. #ifdef CONFIG_P2P
  178. else if (os_strcmp(key, "P2PDeviceAddress") == 0)
  179. return wpas_dbus_handler_wps_p2p_dev_addr(message, entry_iter,
  180. params, reply);
  181. #endif /* CONFIG_P2P */
  182. wpa_printf(MSG_DEBUG, "dbus: WPS.Start - unknown key %s", key);
  183. *reply = wpas_dbus_error_invalid_args(message, key);
  184. return -1;
  185. }
  186. /**
  187. * wpas_dbus_handler_wps_start - Start WPS configuration
  188. * @message: Pointer to incoming dbus message
  189. * @wpa_s: %wpa_supplicant data structure
  190. * Returns: DBus message dictionary on success or DBus error on failure
  191. *
  192. * Handler for "Start" method call. DBus dictionary argument contains
  193. * information about role (enrollee or registrar), authorization method
  194. * (pin or push button) and optionally pin and bssid. Returned message
  195. * has a dictionary argument which may contain newly generated pin (optional).
  196. */
  197. DBusMessage * wpas_dbus_handler_wps_start(DBusMessage *message,
  198. struct wpa_supplicant *wpa_s)
  199. {
  200. DBusMessage *reply = NULL;
  201. DBusMessageIter iter, dict_iter, entry_iter;
  202. struct wps_start_params params;
  203. char *key;
  204. char npin[9] = { '\0' };
  205. int ret;
  206. os_memset(&params, 0, sizeof(params));
  207. dbus_message_iter_init(message, &iter);
  208. dbus_message_iter_recurse(&iter, &dict_iter);
  209. while (dbus_message_iter_get_arg_type(&dict_iter) ==
  210. DBUS_TYPE_DICT_ENTRY) {
  211. dbus_message_iter_recurse(&dict_iter, &entry_iter);
  212. dbus_message_iter_get_basic(&entry_iter, &key);
  213. dbus_message_iter_next(&entry_iter);
  214. if (wpas_dbus_handler_wps_start_entry(message, key,
  215. &entry_iter,
  216. &params, &reply))
  217. return reply;
  218. dbus_message_iter_next(&dict_iter);
  219. }
  220. if (params.role == 0) {
  221. wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Role not specified");
  222. return wpas_dbus_error_invalid_args(message,
  223. "Role not specified");
  224. } else if (params.role == 1 && params.type == 0) {
  225. wpa_printf(MSG_DEBUG, "dbus: WPS.Start - Type not specified");
  226. return wpas_dbus_error_invalid_args(message,
  227. "Type not specified");
  228. } else if (params.role == 2 && params.pin == NULL) {
  229. wpa_printf(MSG_DEBUG,
  230. "dbus: WPS.Start - Pin required for registrar role");
  231. return wpas_dbus_error_invalid_args(
  232. message, "Pin required for registrar role.");
  233. }
  234. if (params.role == 2)
  235. ret = wpas_wps_start_reg(wpa_s, params.bssid, params.pin,
  236. NULL);
  237. else if (params.type == 1) {
  238. #ifdef CONFIG_AP
  239. if (wpa_s->ap_iface)
  240. ret = wpa_supplicant_ap_wps_pin(wpa_s,
  241. params.bssid,
  242. params.pin,
  243. npin, sizeof(npin), 0);
  244. else
  245. #endif /* CONFIG_AP */
  246. {
  247. ret = wpas_wps_start_pin(wpa_s, params.bssid,
  248. params.pin, 0,
  249. DEV_PW_DEFAULT);
  250. if (ret > 0)
  251. os_snprintf(npin, sizeof(npin), "%08d", ret);
  252. }
  253. } else {
  254. #ifdef CONFIG_AP
  255. if (wpa_s->ap_iface)
  256. ret = wpa_supplicant_ap_wps_pbc(wpa_s,
  257. params.bssid,
  258. params.p2p_dev_addr);
  259. else
  260. #endif /* CONFIG_AP */
  261. ret = wpas_wps_start_pbc(wpa_s, params.bssid, 0);
  262. }
  263. if (ret < 0) {
  264. wpa_printf(MSG_DEBUG,
  265. "dbus: WPS.Start wpas_wps_failed in role %s and key %s",
  266. (params.role == 1 ? "enrollee" : "registrar"),
  267. (params.type == 0 ? "" :
  268. (params.type == 1 ? "pin" : "pbc")));
  269. return wpas_dbus_error_unknown_error(message,
  270. "WPS start failed");
  271. }
  272. reply = dbus_message_new_method_return(message);
  273. if (!reply) {
  274. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  275. NULL);
  276. }
  277. dbus_message_iter_init_append(reply, &iter);
  278. if (!wpa_dbus_dict_open_write(&iter, &dict_iter) ||
  279. (os_strlen(npin) > 0 &&
  280. !wpa_dbus_dict_append_string(&dict_iter, "Pin", npin)) ||
  281. !wpa_dbus_dict_close_write(&iter, &dict_iter)) {
  282. dbus_message_unref(reply);
  283. return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
  284. NULL);
  285. }
  286. return reply;
  287. }
  288. /**
  289. * wpas_dbus_getter_process_credentials - Check if credentials are processed
  290. * @message: Pointer to incoming dbus message
  291. * @wpa_s: %wpa_supplicant data structure
  292. * Returns: TRUE on success, FALSE on failure
  293. *
  294. * Getter for "ProcessCredentials" property. Returns returned boolean will be
  295. * true if wps_cred_processing configuration field is not equal to 1 or false
  296. * if otherwise.
  297. */
  298. dbus_bool_t wpas_dbus_getter_process_credentials(DBusMessageIter *iter,
  299. DBusError *error,
  300. void *user_data)
  301. {
  302. struct wpa_supplicant *wpa_s = user_data;
  303. dbus_bool_t process = wpa_s->conf->wps_cred_processing != 1;
  304. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
  305. &process, error);
  306. }
  307. /**
  308. * wpas_dbus_setter_process_credentials - Set credentials_processed conf param
  309. * @iter: Pointer to incoming dbus message iter
  310. * @error: Location to store error on failure
  311. * @user_data: Function specific data
  312. * Returns: TRUE on success, FALSE on failure
  313. *
  314. * Setter for "ProcessCredentials" property. Sets credentials_processed on 2
  315. * if boolean argument is true or on 1 if otherwise.
  316. */
  317. dbus_bool_t wpas_dbus_setter_process_credentials(DBusMessageIter *iter,
  318. DBusError *error,
  319. void *user_data)
  320. {
  321. struct wpa_supplicant *wpa_s = user_data;
  322. dbus_bool_t process_credentials, old_pc;
  323. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
  324. &process_credentials))
  325. return FALSE;
  326. old_pc = wpa_s->conf->wps_cred_processing != 1;
  327. wpa_s->conf->wps_cred_processing = (process_credentials ? 2 : 1);
  328. if ((wpa_s->conf->wps_cred_processing != 1) != old_pc)
  329. wpa_dbus_mark_property_changed(wpa_s->global->dbus,
  330. wpa_s->dbus_new_path,
  331. WPAS_DBUS_NEW_IFACE_WPS,
  332. "ProcessCredentials");
  333. return TRUE;
  334. }
  335. /**
  336. * wpas_dbus_getter_config_methods - Get current WPS configuration methods
  337. * @iter: Pointer to incoming dbus message iter
  338. * @error: Location to store error on failure
  339. * @user_data: Function specific data
  340. * Returns: TRUE on success, FALSE on failure
  341. *
  342. * Getter for "ConfigMethods" property. Returned boolean will be true if
  343. * providing the relevant string worked, or false otherwise.
  344. */
  345. dbus_bool_t wpas_dbus_getter_config_methods(DBusMessageIter *iter,
  346. DBusError *error,
  347. void *user_data)
  348. {
  349. struct wpa_supplicant *wpa_s = user_data;
  350. char *methods = wpa_s->conf->config_methods;
  351. if (methods == NULL)
  352. methods = "";
  353. return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
  354. &methods, error);
  355. }
  356. /**
  357. * wpas_dbus_setter_config_methods - Set WPS configuration methods
  358. * @iter: Pointer to incoming dbus message iter
  359. * @error: Location to store error on failure
  360. * @user_data: Function specific data
  361. * Returns: TRUE on success, FALSE on failure
  362. *
  363. * Setter for "ConfigMethods" property. Sets the methods string, apply such
  364. * change and returns true on success. Returns false otherwise.
  365. */
  366. dbus_bool_t wpas_dbus_setter_config_methods(DBusMessageIter *iter,
  367. DBusError *error,
  368. void *user_data)
  369. {
  370. struct wpa_supplicant *wpa_s = user_data;
  371. char *methods, *new_methods;
  372. if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
  373. &methods))
  374. return FALSE;
  375. new_methods = os_strdup(methods);
  376. if (!new_methods)
  377. return FALSE;
  378. os_free(wpa_s->conf->config_methods);
  379. wpa_s->conf->config_methods = new_methods;
  380. wpa_s->conf->changed_parameters |= CFG_CHANGED_CONFIG_METHODS;
  381. wpa_supplicant_update_config(wpa_s);
  382. return TRUE;
  383. }