dbus_handlers_wps.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * WPA Supplicant / dbus-based control interface (WPS)
  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 "../wps_supplicant.h"
  19. #include "dbus.h"
  20. #include "dbus_handlers.h"
  21. /**
  22. * wpas_dbus_iface_wps_pbc - Request credentials using WPS PBC method
  23. * @message: Pointer to incoming dbus message
  24. * @wpa_s: %wpa_supplicant data structure
  25. * Returns: A dbus message containing a UINT32 indicating success (1) or
  26. * failure (0)
  27. *
  28. * Handler function for "wpsPbc" method call
  29. */
  30. DBusMessage * wpas_dbus_iface_wps_pbc(DBusMessage *message,
  31. struct wpa_supplicant *wpa_s)
  32. {
  33. char *arg_bssid = NULL;
  34. u8 bssid[ETH_ALEN];
  35. int ret = 0;
  36. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
  37. DBUS_TYPE_INVALID))
  38. return wpas_dbus_new_invalid_opts_error(message, NULL);
  39. if (!os_strcmp(arg_bssid, "any"))
  40. ret = wpas_wps_start_pbc(wpa_s, NULL);
  41. else if (!hwaddr_aton(arg_bssid, bssid))
  42. ret = wpas_wps_start_pbc(wpa_s, bssid);
  43. else {
  44. return wpas_dbus_new_invalid_opts_error(message,
  45. "Invalid BSSID");
  46. }
  47. if (ret < 0) {
  48. return dbus_message_new_error(message,
  49. WPAS_ERROR_WPS_PBC_ERROR,
  50. "Could not start PBC "
  51. "negotiation");
  52. }
  53. return wpas_dbus_new_success_reply(message);
  54. }
  55. /**
  56. * wpas_dbus_iface_wps_pin - Establish the PIN number of the enrollee
  57. * @message: Pointer to incoming dbus message
  58. * @wpa_s: %wpa_supplicant data structure
  59. * Returns: A dbus message containing a UINT32 indicating success (1) or
  60. * failure (0)
  61. *
  62. * Handler function for "wpsPin" method call
  63. */
  64. DBusMessage * wpas_dbus_iface_wps_pin(DBusMessage *message,
  65. struct wpa_supplicant *wpa_s)
  66. {
  67. DBusMessage *reply = NULL;
  68. char *arg_bssid;
  69. char *pin = NULL;
  70. u8 bssid[ETH_ALEN], *_bssid = NULL;
  71. int ret = 0;
  72. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
  73. DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
  74. return wpas_dbus_new_invalid_opts_error(message, NULL);
  75. if (!os_strcmp(arg_bssid, "any"))
  76. _bssid = NULL;
  77. else if (!hwaddr_aton(arg_bssid, bssid))
  78. _bssid = bssid;
  79. else {
  80. return wpas_dbus_new_invalid_opts_error(message,
  81. "Invalid BSSID");
  82. }
  83. if (os_strlen(pin) > 0)
  84. ret = wpas_wps_start_pin(wpa_s, _bssid, pin);
  85. else
  86. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL);
  87. if (ret < 0) {
  88. return dbus_message_new_error(message,
  89. WPAS_ERROR_WPS_PIN_ERROR,
  90. "Could not init PIN");
  91. }
  92. reply = dbus_message_new_method_return(message);
  93. if (reply == NULL)
  94. return NULL;
  95. if (ret == 0) {
  96. dbus_message_append_args(reply, DBUS_TYPE_STRING, &pin,
  97. DBUS_TYPE_INVALID);
  98. } else {
  99. char npin[9];
  100. os_snprintf(npin, sizeof(npin), "%08d", ret);
  101. dbus_message_append_args(reply, DBUS_TYPE_STRING, &npin,
  102. DBUS_TYPE_INVALID);
  103. }
  104. return reply;
  105. }
  106. /**
  107. * wpas_dbus_iface_wps_reg - Request credentials using the PIN of the AP
  108. * @message: Pointer to incoming dbus message
  109. * @wpa_s: %wpa_supplicant data structure
  110. * Returns: A dbus message containing a UINT32 indicating success (1) or
  111. * failure (0)
  112. *
  113. * Handler function for "wpsReg" method call
  114. */
  115. DBusMessage * wpas_dbus_iface_wps_reg(DBusMessage *message,
  116. struct wpa_supplicant *wpa_s)
  117. {
  118. char *arg_bssid;
  119. char *pin = NULL;
  120. u8 bssid[ETH_ALEN];
  121. int ret = 0;
  122. if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg_bssid,
  123. DBUS_TYPE_STRING, &pin, DBUS_TYPE_INVALID))
  124. return wpas_dbus_new_invalid_opts_error(message, NULL);
  125. if (!os_strcmp(arg_bssid, "any"))
  126. ret = wpas_wps_start_reg(wpa_s, NULL, pin, NULL);
  127. else if (!hwaddr_aton(arg_bssid, bssid))
  128. ret = wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  129. else {
  130. return wpas_dbus_new_invalid_opts_error(message,
  131. "Invalid BSSID");
  132. }
  133. if (ret < 0) {
  134. return dbus_message_new_error(message,
  135. WPAS_ERROR_WPS_PBC_ERROR,
  136. "Could not request credentials");
  137. }
  138. return wpas_dbus_new_success_reply(message);
  139. }