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