s3: Fix bug 7578
[Samba/gebeck_regimport.git] / source4 / auth / gensec / cyrus_sasl.c
blobc4f954442c0df90eb26c972967c572d250497d25
1 /*
2 Unix SMB/CIFS implementation.
4 Connect GENSEC to an external SASL lib
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "lib/tsocket/tsocket.h"
24 #include "auth/credentials/credentials.h"
25 #include "auth/gensec/gensec.h"
26 #include "auth/gensec/gensec_proto.h"
27 #include <sasl/sasl.h>
29 struct gensec_sasl_state {
30 sasl_conn_t *conn;
31 int step;
34 static NTSTATUS sasl_nt_status(int sasl_ret)
36 switch (sasl_ret) {
37 case SASL_CONTINUE:
38 return NT_STATUS_MORE_PROCESSING_REQUIRED;
39 case SASL_NOMEM:
40 return NT_STATUS_NO_MEMORY;
41 case SASL_BADPARAM:
42 case SASL_NOMECH:
43 return NT_STATUS_INVALID_PARAMETER;
44 case SASL_BADMAC:
45 return NT_STATUS_ACCESS_DENIED;
46 case SASL_OK:
47 return NT_STATUS_OK;
48 default:
49 return NT_STATUS_UNSUCCESSFUL;
53 static int gensec_sasl_get_user(void *context, int id,
54 const char **result, unsigned *len)
56 struct gensec_security *gensec_security = talloc_get_type(context, struct gensec_security);
57 const char *username = cli_credentials_get_username(gensec_get_credentials(gensec_security));
58 if (id != SASL_CB_USER && id != SASL_CB_AUTHNAME) {
59 return SASL_FAIL;
62 *result = username;
63 return SASL_OK;
66 static int gensec_sasl_get_realm(void *context, int id,
67 const char **availrealms,
68 const char **result)
70 struct gensec_security *gensec_security = talloc_get_type(context, struct gensec_security);
71 const char *realm = cli_credentials_get_realm(gensec_get_credentials(gensec_security));
72 int i;
73 if (id != SASL_CB_GETREALM) {
74 return SASL_FAIL;
77 for (i=0; availrealms && availrealms[i]; i++) {
78 if (strcasecmp_m(realm, availrealms[i]) == 0) {
79 result[i] = availrealms[i];
80 return SASL_OK;
83 /* None of the realms match, so lets not specify one */
84 *result = "";
85 return SASL_OK;
88 static int gensec_sasl_get_password(sasl_conn_t *conn, void *context, int id,
89 sasl_secret_t **psecret)
91 struct gensec_security *gensec_security = talloc_get_type(context, struct gensec_security);
92 const char *password = cli_credentials_get_password(gensec_get_credentials(gensec_security));
94 sasl_secret_t *secret;
95 if (!password) {
96 *psecret = NULL;
97 return SASL_OK;
99 secret = talloc_size(gensec_security, sizeof(sasl_secret_t)+strlen(password));
100 if (!secret) {
101 return SASL_NOMEM;
103 secret->len = strlen(password);
104 safe_strcpy((char*)secret->data, password, secret->len+1);
105 *psecret = secret;
106 return SASL_OK;
109 static int gensec_sasl_dispose(struct gensec_sasl_state *gensec_sasl_state)
111 sasl_dispose(&gensec_sasl_state->conn);
112 return SASL_OK;
115 static NTSTATUS gensec_sasl_client_start(struct gensec_security *gensec_security)
117 struct gensec_sasl_state *gensec_sasl_state;
118 const char *service = gensec_get_target_service(gensec_security);
119 const char *target_name = gensec_get_target_hostname(gensec_security);
120 const struct tsocket_address *tlocal_addr = gensec_get_local_address(gensec_security);
121 const struct tsocket_address *tremote_addr = gensec_get_remote_address(gensec_security);
122 char *local_addr = NULL;
123 char *remote_addr = NULL;
124 int sasl_ret;
126 sasl_callback_t *callbacks;
128 gensec_sasl_state = talloc(gensec_security, struct gensec_sasl_state);
129 if (!gensec_sasl_state) {
130 return NT_STATUS_NO_MEMORY;
133 callbacks = talloc_array(gensec_sasl_state, sasl_callback_t, 5);
134 callbacks[0].id = SASL_CB_USER;
135 callbacks[0].proc = gensec_sasl_get_user;
136 callbacks[0].context = gensec_security;
138 callbacks[1].id = SASL_CB_AUTHNAME;
139 callbacks[1].proc = gensec_sasl_get_user;
140 callbacks[1].context = gensec_security;
142 callbacks[2].id = SASL_CB_GETREALM;
143 callbacks[2].proc = gensec_sasl_get_realm;
144 callbacks[2].context = gensec_security;
146 callbacks[3].id = SASL_CB_PASS;
147 callbacks[3].proc = gensec_sasl_get_password;
148 callbacks[3].context = gensec_security;
150 callbacks[4].id = SASL_CB_LIST_END;
151 callbacks[4].proc = NULL;
152 callbacks[4].context = NULL;
154 gensec_security->private_data = gensec_sasl_state;
156 if (tlocal_addr) {
157 local_addr = talloc_asprintf(gensec_sasl_state,
158 "%s;%d",
159 tsocket_address_inet_addr_string(tlocal_addr, gensec_sasl_state),
160 tsocket_address_inet_port(tlocal_addr));
163 if (tremote_addr) {
164 remote_addr = talloc_asprintf(gensec_sasl_state,
165 "%s;%d",
166 tsocket_address_inet_addr_string(tremote_addr, gensec_sasl_state),
167 tsocket_address_inet_port(tremote_addr));
169 gensec_sasl_state->step = 0;
171 sasl_ret = sasl_client_new(service,
172 target_name,
173 local_addr, remote_addr, callbacks, 0,
174 &gensec_sasl_state->conn);
176 if (sasl_ret == SASL_OK || sasl_ret == SASL_CONTINUE) {
177 sasl_security_properties_t props;
178 talloc_set_destructor(gensec_sasl_state, gensec_sasl_dispose);
180 ZERO_STRUCT(props);
181 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
182 props.min_ssf = 1;
184 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
185 props.min_ssf = 40;
188 props.max_ssf = UINT_MAX;
189 props.maxbufsize = 65536;
190 sasl_ret = sasl_setprop(gensec_sasl_state->conn, SASL_SEC_PROPS, &props);
191 if (sasl_ret != SASL_OK) {
192 return sasl_nt_status(sasl_ret);
195 } else {
196 DEBUG(1, ("GENSEC SASL: client_new failed: %s\n", sasl_errdetail(gensec_sasl_state->conn)));
198 return sasl_nt_status(sasl_ret);
201 static NTSTATUS gensec_sasl_update(struct gensec_security *gensec_security,
202 TALLOC_CTX *out_mem_ctx,
203 const DATA_BLOB in, DATA_BLOB *out)
205 struct gensec_sasl_state *gensec_sasl_state = talloc_get_type(gensec_security->private_data,
206 struct gensec_sasl_state);
207 int sasl_ret;
208 const char *out_data;
209 unsigned int out_len;
211 if (gensec_sasl_state->step == 0) {
212 const char *mech;
213 sasl_ret = sasl_client_start(gensec_sasl_state->conn, gensec_security->ops->sasl_name,
214 NULL, &out_data, &out_len, &mech);
215 } else {
216 sasl_ret = sasl_client_step(gensec_sasl_state->conn,
217 (char*)in.data, in.length, NULL,
218 &out_data, &out_len);
220 if (sasl_ret == SASL_OK || sasl_ret == SASL_CONTINUE) {
221 *out = data_blob_talloc(out_mem_ctx, out_data, out_len);
222 } else {
223 DEBUG(1, ("GENSEC SASL: step %d update failed: %s\n", gensec_sasl_state->step,
224 sasl_errdetail(gensec_sasl_state->conn)));
226 gensec_sasl_state->step++;
227 return sasl_nt_status(sasl_ret);
230 static NTSTATUS gensec_sasl_unwrap_packets(struct gensec_security *gensec_security,
231 TALLOC_CTX *out_mem_ctx,
232 const DATA_BLOB *in,
233 DATA_BLOB *out,
234 size_t *len_processed)
236 struct gensec_sasl_state *gensec_sasl_state = talloc_get_type(gensec_security->private_data,
237 struct gensec_sasl_state);
238 const char *out_data;
239 unsigned int out_len;
241 int sasl_ret = sasl_decode(gensec_sasl_state->conn,
242 (char*)in->data, in->length, &out_data,
243 &out_len);
244 if (sasl_ret == SASL_OK) {
245 *out = data_blob_talloc(out_mem_ctx, out_data, out_len);
246 *len_processed = in->length;
247 } else {
248 DEBUG(1, ("GENSEC SASL: unwrap failed: %s\n", sasl_errdetail(gensec_sasl_state->conn)));
250 return sasl_nt_status(sasl_ret);
254 static NTSTATUS gensec_sasl_wrap_packets(struct gensec_security *gensec_security,
255 TALLOC_CTX *out_mem_ctx,
256 const DATA_BLOB *in,
257 DATA_BLOB *out,
258 size_t *len_processed)
260 struct gensec_sasl_state *gensec_sasl_state = talloc_get_type(gensec_security->private_data,
261 struct gensec_sasl_state);
262 const char *out_data;
263 unsigned int out_len;
265 int sasl_ret = sasl_encode(gensec_sasl_state->conn,
266 (char*)in->data, in->length, &out_data,
267 &out_len);
268 if (sasl_ret == SASL_OK) {
269 *out = data_blob_talloc(out_mem_ctx, out_data, out_len);
270 *len_processed = in->length;
271 } else {
272 DEBUG(1, ("GENSEC SASL: wrap failed: %s\n", sasl_errdetail(gensec_sasl_state->conn)));
274 return sasl_nt_status(sasl_ret);
277 /* Try to figure out what features we actually got on the connection */
278 static bool gensec_sasl_have_feature(struct gensec_security *gensec_security,
279 uint32_t feature)
281 struct gensec_sasl_state *gensec_sasl_state = talloc_get_type(gensec_security->private_data,
282 struct gensec_sasl_state);
283 sasl_ssf_t ssf;
284 int sasl_ret = sasl_getprop(gensec_sasl_state->conn, SASL_SSF,
285 (const void**)&ssf);
286 if (sasl_ret != SASL_OK) {
287 return false;
289 if (feature & GENSEC_FEATURE_SIGN) {
290 if (ssf == 0) {
291 return false;
293 if (ssf >= 1) {
294 return true;
297 if (feature & GENSEC_FEATURE_SEAL) {
298 if (ssf <= 1) {
299 return false;
301 if (ssf > 1) {
302 return true;
305 return false;
308 /* This could in theory work with any SASL mech */
309 static const struct gensec_security_ops gensec_sasl_security_ops = {
310 .name = "sasl-DIGEST-MD5",
311 .sasl_name = "DIGEST-MD5",
312 .client_start = gensec_sasl_client_start,
313 .update = gensec_sasl_update,
314 .wrap_packets = gensec_sasl_wrap_packets,
315 .unwrap_packets = gensec_sasl_unwrap_packets,
316 .have_feature = gensec_sasl_have_feature,
317 .enabled = true,
318 .priority = GENSEC_SASL
321 static int gensec_sasl_log(void *context,
322 int sasl_log_level,
323 const char *message)
325 int dl;
326 switch (sasl_log_level) {
327 case SASL_LOG_NONE:
328 dl = 0;
329 break;
330 case SASL_LOG_ERR:
331 dl = 1;
332 break;
333 case SASL_LOG_FAIL:
334 dl = 2;
335 break;
336 case SASL_LOG_WARN:
337 dl = 3;
338 break;
339 case SASL_LOG_NOTE:
340 dl = 5;
341 break;
342 case SASL_LOG_DEBUG:
343 dl = 10;
344 break;
345 case SASL_LOG_TRACE:
346 dl = 11;
347 break;
348 #if DEBUG_PASSWORD
349 case SASL_LOG_PASS:
350 dl = 100;
351 break;
352 #endif
353 default:
354 dl = 0;
355 break;
357 DEBUG(dl, ("gensec_sasl: %s\n", message));
359 return SASL_OK;
362 NTSTATUS gensec_sasl_init(void)
364 NTSTATUS ret;
365 int sasl_ret;
366 #if 0
367 int i;
368 const char **sasl_mechs;
369 #endif
371 static const sasl_callback_t callbacks[] = {
373 .id = SASL_CB_LOG,
374 .proc = gensec_sasl_log,
375 .context = NULL,
378 .id = SASL_CB_LIST_END,
379 .proc = gensec_sasl_log,
380 .context = NULL,
383 sasl_ret = sasl_client_init(callbacks);
385 if (sasl_ret == SASL_NOMECH) {
386 /* Nothing to do here */
387 return NT_STATUS_OK;
390 if (sasl_ret != SASL_OK) {
391 return sasl_nt_status(sasl_ret);
394 /* For now, we just register DIGEST-MD5 */
395 #if 1
396 ret = gensec_register(&gensec_sasl_security_ops);
397 if (!NT_STATUS_IS_OK(ret)) {
398 DEBUG(0,("Failed to register '%s' gensec backend!\n",
399 gensec_sasl_security_ops.name));
400 return ret;
402 #else
403 sasl_mechs = sasl_global_listmech();
404 for (i = 0; sasl_mechs && sasl_mechs[i]; i++) {
405 const struct gensec_security_ops *oldmech;
406 struct gensec_security_ops *newmech;
407 oldmech = gensec_security_by_sasl_name(NULL, sasl_mechs[i]);
408 if (oldmech) {
409 continue;
411 newmech = talloc(talloc_autofree_context(), struct gensec_security_ops);
412 if (!newmech) {
413 return NT_STATUS_NO_MEMORY;
415 *newmech = gensec_sasl_security_ops;
416 newmech->sasl_name = talloc_strdup(newmech, sasl_mechs[i]);
417 newmech->name = talloc_asprintf(newmech, "sasl-%s", sasl_mechs[i]);
418 if (!newmech->sasl_name || !newmech->name) {
419 return NT_STATUS_NO_MEMORY;
422 ret = gensec_register(newmech);
423 if (!NT_STATUS_IS_OK(ret)) {
424 DEBUG(0,("Failed to register '%s' gensec backend!\n",
425 gensec_sasl_security_ops.name));
426 return ret;
429 #endif
430 return NT_STATUS_OK;