eap-radius: support plain XAuth RADIUS authentication using User-Password
[strongswan.git] / src / libcharon / plugins / eap_radius / eap_radius_xauth.c
1 /*
2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "eap_radius_xauth.h"
17 #include "eap_radius_plugin.h"
18 #include "eap_radius.h"
19
20 #include <daemon.h>
21 #include <radius_client.h>
22
23
24 typedef struct private_eap_radius_xauth_t private_eap_radius_xauth_t;
25
26 /**
27 * Private data of an eap_radius_xauth_t object.
28 */
29 struct private_eap_radius_xauth_t {
30
31 /**
32 * Public interface.
33 */
34 eap_radius_xauth_t public;
35
36 /**
37 * ID of the server
38 */
39 identification_t *server;
40
41 /**
42 * ID of the peer
43 */
44 identification_t *peer;
45
46 /**
47 * RADIUS connection
48 */
49 radius_client_t *client;
50 };
51
52 METHOD(xauth_method_t, initiate, status_t,
53 private_eap_radius_xauth_t *this, cp_payload_t **out)
54 {
55 cp_payload_t *cp;
56
57 cp = cp_payload_create_type(CONFIGURATION_V1, CFG_REQUEST);
58 cp->add_attribute(cp, configuration_attribute_create_chunk(
59 CONFIGURATION_ATTRIBUTE_V1, XAUTH_USER_NAME, chunk_empty));
60 cp->add_attribute(cp, configuration_attribute_create_chunk(
61 CONFIGURATION_ATTRIBUTE_V1, XAUTH_USER_PASSWORD, chunk_empty));
62 *out = cp;
63 return NEED_MORE;
64 }
65
66 /**
67 * Verify a password using RADIUS User-Name/User-Password attributes
68 */
69 static status_t verify_radius(private_eap_radius_xauth_t *this, chunk_t pass)
70 {
71 radius_message_t *request, *response;
72 status_t status = FAILED;
73
74 request = radius_message_create(RMC_ACCESS_REQUEST);
75 request->add(request, RAT_USER_NAME, this->peer->get_encoding(this->peer));
76 request->add(request, RAT_USER_PASSWORD, pass);
77
78 eap_radius_build_attributes(request);
79
80 response = this->client->request(this->client, request);
81 if (response)
82 {
83 switch (response->get_code(response))
84 {
85 case RMC_ACCESS_ACCEPT:
86 eap_radius_process_attributes(response);
87 status = SUCCESS;
88 break;
89 case RMC_ACCESS_CHALLENGE:
90 DBG1(DBG_IKE, "RADIUS Access-Challenge not supported");
91 /* FALL */
92 case RMC_ACCESS_REJECT:
93 default:
94 DBG1(DBG_IKE, "RADIUS authentication of '%Y' failed",
95 this->peer);
96 break;
97 }
98 response->destroy(response);
99 }
100 else
101 {
102 eap_radius_handle_timeout(NULL);
103 }
104 request->destroy(request);
105 return status;
106 }
107
108 METHOD(xauth_method_t, process, status_t,
109 private_eap_radius_xauth_t *this, cp_payload_t *in, cp_payload_t **out)
110 {
111 configuration_attribute_t *attr;
112 enumerator_t *enumerator;
113 identification_t *id;
114 chunk_t user = chunk_empty, pass = chunk_empty;
115
116 enumerator = in->create_attribute_enumerator(in);
117 while (enumerator->enumerate(enumerator, &attr))
118 {
119 switch (attr->get_type(attr))
120 {
121 case XAUTH_USER_NAME:
122 user = attr->get_chunk(attr);
123 break;
124 case XAUTH_USER_PASSWORD:
125 pass = attr->get_chunk(attr);
126 /* trim password to any null termination. As User-Password
127 * uses null padding, we can't have any null in it, and some
128 * clients actually send null terminated strings (Android). */
129 pass.len = strnlen(pass.ptr, pass.len);
130 break;
131 default:
132 break;
133 }
134 }
135 enumerator->destroy(enumerator);
136
137 if (!user.ptr || !pass.ptr)
138 {
139 DBG1(DBG_IKE, "peer did not respond to our XAuth request");
140 return FAILED;
141 }
142 if (user.len)
143 {
144 id = identification_create_from_data(user);
145 if (!id)
146 {
147 DBG1(DBG_IKE, "failed to parse provided XAuth username");
148 return FAILED;
149 }
150 this->peer->destroy(this->peer);
151 this->peer = id;
152 }
153 return verify_radius(this, pass);
154 }
155
156 METHOD(xauth_method_t, get_identity, identification_t*,
157 private_eap_radius_xauth_t *this)
158 {
159 return this->peer;
160 }
161
162 METHOD(xauth_method_t, destroy, void,
163 private_eap_radius_xauth_t *this)
164 {
165 DESTROY_IF(this->client);
166 this->server->destroy(this->server);
167 this->peer->destroy(this->peer);
168 free(this);
169 }
170
171 /*
172 * Described in header.
173 */
174 eap_radius_xauth_t *eap_radius_xauth_create_server(identification_t *server,
175 identification_t *peer)
176 {
177 private_eap_radius_xauth_t *this;
178
179 INIT(this,
180 .public = {
181 .xauth_method = {
182 .initiate = _initiate,
183 .process = _process,
184 .get_identity = _get_identity,
185 .destroy = _destroy,
186 },
187 },
188 .server = server->clone(server),
189 .peer = peer->clone(peer),
190 .client = eap_radius_create_client(),
191 );
192
193 if (!this->client)
194 {
195 destroy(this);
196 return NULL;
197 }
198 return &this->public;
199 }