samba-tool: refuse to demote if the current DC has still roles
[Samba/gebeck_regimport.git] / source4 / auth / gensec / cyrus_sasl.c
blob136bb8d43a990eecd65d4d5aa57411bf1c82144f
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 "auth/gensec/gensec_toplevel_proto.h"
28 #include <sasl/sasl.h>
30 NTSTATUS gensec_sasl_init(void);
32 struct gensec_sasl_state {
33 sasl_conn_t *conn;
34 int step;
35 bool wrap;
38 static NTSTATUS sasl_nt_status(int sasl_ret)
40 switch (sasl_ret) {
41 case SASL_CONTINUE:
42 return NT_STATUS_MORE_PROCESSING_REQUIRED;
43 case SASL_NOMEM:
44 return NT_STATUS_NO_MEMORY;
45 case SASL_BADPARAM:
46 case SASL_NOMECH:
47 return NT_STATUS_INVALID_PARAMETER;
48 case SASL_BADMAC:
49 return NT_STATUS_ACCESS_DENIED;
50 case SASL_OK:
51 return NT_STATUS_OK;
52 default:
53 return NT_STATUS_UNSUCCESSFUL;
57 static int gensec_sasl_get_user(void *context, int id,
58 const char **result, unsigned *len)
60 struct gensec_security *gensec_security = talloc_get_type(context, struct gensec_security);
61 const char *username = cli_credentials_get_username(gensec_get_credentials(gensec_security));
62 if (id != SASL_CB_USER && id != SASL_CB_AUTHNAME) {
63 return SASL_FAIL;
66 *result = username;
67 return SASL_OK;
70 static int gensec_sasl_get_realm(void *context, int id,
71 const char **availrealms,
72 const char **result)
74 struct gensec_security *gensec_security = talloc_get_type(context, struct gensec_security);
75 const char *realm = cli_credentials_get_realm(gensec_get_credentials(gensec_security));
76 int i;
77 if (id != SASL_CB_GETREALM) {
78 return SASL_FAIL;
81 for (i=0; availrealms && availrealms[i]; i++) {
82 if (strcasecmp_m(realm, availrealms[i]) == 0) {
83 result[i] = availrealms[i];
84 return SASL_OK;
87 /* None of the realms match, so lets not specify one */
88 *result = "";
89 return SASL_OK;
92 static int gensec_sasl_get_password(sasl_conn_t *conn, void *context, int id,
93 sasl_secret_t **psecret)
95 struct gensec_security *gensec_security = talloc_get_type(context, struct gensec_security);
96 const char *password = cli_credentials_get_password(gensec_get_credentials(gensec_security));
98 sasl_secret_t *secret;
99 if (!password) {
100 *psecret = NULL;
101 return SASL_OK;
103 secret = talloc_size(gensec_security, sizeof(sasl_secret_t)+strlen(password)+1);
104 if (!secret) {
105 return SASL_NOMEM;
107 secret->len = strlen(password);
108 strlcpy((char*)secret->data, password, secret->len+1);
109 *psecret = secret;
110 return SASL_OK;
113 static int gensec_sasl_dispose(struct gensec_sasl_state *gensec_sasl_state)
115 sasl_dispose(&gensec_sasl_state->conn);
116 return SASL_OK;
119 static NTSTATUS gensec_sasl_client_start(struct gensec_security *gensec_security)
121 struct gensec_sasl_state *gensec_sasl_state;
122 const char *service = gensec_get_target_service(gensec_security);
123 const char *target_name = gensec_get_target_hostname(gensec_security);
124 const struct tsocket_address *tlocal_addr = gensec_get_local_address(gensec_security);
125 const struct tsocket_address *tremote_addr = gensec_get_remote_address(gensec_security);
126 char *local_addr = NULL;
127 char *remote_addr = NULL;
128 int sasl_ret;
130 sasl_callback_t *callbacks;
132 gensec_sasl_state = talloc_zero(gensec_security, struct gensec_sasl_state);
133 if (!gensec_sasl_state) {
134 return NT_STATUS_NO_MEMORY;
137 callbacks = talloc_array(gensec_sasl_state, sasl_callback_t, 5);
138 callbacks[0].id = SASL_CB_USER;
139 callbacks[0].proc = gensec_sasl_get_user;
140 callbacks[0].context = gensec_security;
142 callbacks[1].id = SASL_CB_AUTHNAME;
143 callbacks[1].proc = gensec_sasl_get_user;
144 callbacks[1].context = gensec_security;
146 callbacks[2].id = SASL_CB_GETREALM;
147 callbacks[2].proc = gensec_sasl_get_realm;
148 callbacks[2].context = gensec_security;
150 callbacks[3].id = SASL_CB_PASS;
151 callbacks[3].proc = gensec_sasl_get_password;
152 callbacks[3].context = gensec_security;
154 callbacks[4].id = SASL_CB_LIST_END;
155 callbacks[4].proc = NULL;
156 callbacks[4].context = NULL;
158 gensec_security->private_data = gensec_sasl_state;
160 if (tlocal_addr) {
161 local_addr = talloc_asprintf(gensec_sasl_state,
162 "%s;%d",
163 tsocket_address_inet_addr_string(tlocal_addr, gensec_sasl_state),
164 tsocket_address_inet_port(tlocal_addr));
167 if (tremote_addr) {
168 remote_addr = talloc_asprintf(gensec_sasl_state,
169 "%s;%d",
170 tsocket_address_inet_addr_string(tremote_addr, gensec_sasl_state),
171 tsocket_address_inet_port(tremote_addr));
173 gensec_sasl_state->step = 0;
175 sasl_ret = sasl_client_new(service,
176 target_name,
177 local_addr, remote_addr, callbacks, 0,
178 &gensec_sasl_state->conn);
180 if (sasl_ret == SASL_OK) {
181 sasl_security_properties_t props;
182 talloc_set_destructor(gensec_sasl_state, gensec_sasl_dispose);
184 ZERO_STRUCT(props);
185 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
186 props.min_ssf = 1;
187 props.max_ssf = 1;
188 props.maxbufsize = 65536;
189 gensec_sasl_state->wrap = true;
191 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
192 props.min_ssf = 40;
193 props.max_ssf = UINT_MAX;
194 props.maxbufsize = 65536;
195 gensec_sasl_state->wrap = true;
198 sasl_ret = sasl_setprop(gensec_sasl_state->conn, SASL_SEC_PROPS, &props);
200 if (sasl_ret != SASL_OK) {
201 DEBUG(1, ("GENSEC SASL: client_new failed: %s\n", sasl_errdetail(gensec_sasl_state->conn)));
203 return sasl_nt_status(sasl_ret);
206 static NTSTATUS gensec_sasl_update(struct gensec_security *gensec_security,
207 TALLOC_CTX *out_mem_ctx,
208 const DATA_BLOB in, DATA_BLOB *out)
210 struct gensec_sasl_state *gensec_sasl_state = talloc_get_type(gensec_security->private_data,
211 struct gensec_sasl_state);
212 int sasl_ret;
213 const char *out_data;
214 unsigned int out_len;
216 if (gensec_sasl_state->step == 0) {
217 const char *mech;
218 sasl_ret = sasl_client_start(gensec_sasl_state->conn, gensec_security->ops->sasl_name,
219 NULL, &out_data, &out_len, &mech);
220 } else {
221 sasl_ret = sasl_client_step(gensec_sasl_state->conn,
222 (char*)in.data, in.length, NULL,
223 &out_data, &out_len);
225 if (sasl_ret == SASL_OK || sasl_ret == SASL_CONTINUE) {
226 *out = data_blob_talloc(out_mem_ctx, out_data, out_len);
227 } else {
228 DEBUG(1, ("GENSEC SASL: step %d update failed: %s\n", gensec_sasl_state->step,
229 sasl_errdetail(gensec_sasl_state->conn)));
231 gensec_sasl_state->step++;
232 return sasl_nt_status(sasl_ret);
235 static NTSTATUS gensec_sasl_unwrap_packets(struct gensec_security *gensec_security,
236 TALLOC_CTX *out_mem_ctx,
237 const DATA_BLOB *in,
238 DATA_BLOB *out,
239 size_t *len_processed)
241 struct gensec_sasl_state *gensec_sasl_state = talloc_get_type(gensec_security->private_data,
242 struct gensec_sasl_state);
243 const char *out_data;
244 unsigned int out_len;
246 int sasl_ret = sasl_decode(gensec_sasl_state->conn,
247 (char*)in->data, in->length, &out_data,
248 &out_len);
249 if (sasl_ret == SASL_OK) {
250 *out = data_blob_talloc(out_mem_ctx, out_data, out_len);
251 *len_processed = in->length;
252 } else {
253 DEBUG(1, ("GENSEC SASL: unwrap failed: %s\n", sasl_errdetail(gensec_sasl_state->conn)));
255 return sasl_nt_status(sasl_ret);
259 static NTSTATUS gensec_sasl_wrap_packets(struct gensec_security *gensec_security,
260 TALLOC_CTX *out_mem_ctx,
261 const DATA_BLOB *in,
262 DATA_BLOB *out,
263 size_t *len_processed)
265 struct gensec_sasl_state *gensec_sasl_state = talloc_get_type(gensec_security->private_data,
266 struct gensec_sasl_state);
267 const char *out_data;
268 unsigned int out_len;
269 unsigned len_permitted;
270 int sasl_ret = sasl_getprop(gensec_sasl_state->conn, SASL_SSF,
271 (const void**)&len_permitted);
272 if (sasl_ret != SASL_OK) {
273 return sasl_nt_status(sasl_ret);
275 len_permitted = MIN(len_permitted, in->length);
277 sasl_ret = sasl_encode(gensec_sasl_state->conn,
278 (char*)in->data, len_permitted, &out_data,
279 &out_len);
280 if (sasl_ret == SASL_OK) {
281 *out = data_blob_talloc(out_mem_ctx, out_data, out_len);
282 *len_processed = in->length;
283 } else {
284 DEBUG(1, ("GENSEC SASL: wrap failed: %s\n", sasl_errdetail(gensec_sasl_state->conn)));
286 return sasl_nt_status(sasl_ret);
289 /* Try to figure out what features we actually got on the connection */
290 static bool gensec_sasl_have_feature(struct gensec_security *gensec_security,
291 uint32_t feature)
293 struct gensec_sasl_state *gensec_sasl_state = talloc_get_type(gensec_security->private_data,
294 struct gensec_sasl_state);
295 sasl_ssf_t ssf;
296 int sasl_ret;
298 /* If we did not elect to wrap, then we have neither sign nor seal, no matter what the SSF claims */
299 if (!gensec_sasl_state->wrap) {
300 return false;
303 sasl_ret = sasl_getprop(gensec_sasl_state->conn, SASL_SSF,
304 (const void**)&ssf);
305 if (sasl_ret != SASL_OK) {
306 return false;
308 if (feature & GENSEC_FEATURE_SIGN) {
309 if (ssf == 0) {
310 return false;
312 if (ssf >= 1) {
313 return true;
316 if (feature & GENSEC_FEATURE_SEAL) {
317 if (ssf <= 1) {
318 return false;
320 if (ssf > 1) {
321 return true;
324 return false;
327 /* This could in theory work with any SASL mech */
328 static const struct gensec_security_ops gensec_sasl_security_ops = {
329 .name = "sasl-DIGEST-MD5",
330 .sasl_name = "DIGEST-MD5",
331 .client_start = gensec_sasl_client_start,
332 .update = gensec_sasl_update,
333 .wrap_packets = gensec_sasl_wrap_packets,
334 .unwrap_packets = gensec_sasl_unwrap_packets,
335 .have_feature = gensec_sasl_have_feature,
336 .enabled = true,
337 .priority = GENSEC_SASL
340 static int gensec_sasl_log(void *context,
341 int sasl_log_level,
342 const char *message)
344 int dl;
345 switch (sasl_log_level) {
346 case SASL_LOG_NONE:
347 dl = 0;
348 break;
349 case SASL_LOG_ERR:
350 dl = 1;
351 break;
352 case SASL_LOG_FAIL:
353 dl = 2;
354 break;
355 case SASL_LOG_WARN:
356 dl = 3;
357 break;
358 case SASL_LOG_NOTE:
359 dl = 5;
360 break;
361 case SASL_LOG_DEBUG:
362 dl = 10;
363 break;
364 case SASL_LOG_TRACE:
365 dl = 11;
366 break;
367 #if DEBUG_PASSWORD
368 case SASL_LOG_PASS:
369 dl = 100;
370 break;
371 #endif
372 default:
373 dl = 0;
374 break;
376 DEBUG(dl, ("gensec_sasl: %s\n", message));
378 return SASL_OK;
381 NTSTATUS gensec_sasl_init(void)
383 NTSTATUS ret;
384 int sasl_ret;
385 #if 0
386 int i;
387 const char **sasl_mechs;
388 #endif
390 static const sasl_callback_t callbacks[] = {
392 .id = SASL_CB_LOG,
393 .proc = gensec_sasl_log,
394 .context = NULL,
397 .id = SASL_CB_LIST_END,
398 .proc = gensec_sasl_log,
399 .context = NULL,
402 sasl_ret = sasl_client_init(callbacks);
404 if (sasl_ret == SASL_NOMECH) {
405 /* Nothing to do here */
406 return NT_STATUS_OK;
409 if (sasl_ret != SASL_OK) {
410 return sasl_nt_status(sasl_ret);
413 /* For now, we just register DIGEST-MD5 */
414 #if 1
415 ret = gensec_register(&gensec_sasl_security_ops);
416 if (!NT_STATUS_IS_OK(ret)) {
417 DEBUG(0,("Failed to register '%s' gensec backend!\n",
418 gensec_sasl_security_ops.name));
419 return ret;
421 #else
422 sasl_mechs = sasl_global_listmech();
423 for (i = 0; sasl_mechs && sasl_mechs[i]; i++) {
424 const struct gensec_security_ops *oldmech;
425 struct gensec_security_ops *newmech;
426 oldmech = gensec_security_by_sasl_name(NULL, sasl_mechs[i]);
427 if (oldmech) {
428 continue;
430 newmech = talloc(talloc_autofree_context(), struct gensec_security_ops);
431 if (!newmech) {
432 return NT_STATUS_NO_MEMORY;
434 *newmech = gensec_sasl_security_ops;
435 newmech->sasl_name = talloc_strdup(newmech, sasl_mechs[i]);
436 newmech->name = talloc_asprintf(newmech, "sasl-%s", sasl_mechs[i]);
437 if (!newmech->sasl_name || !newmech->name) {
438 return NT_STATUS_NO_MEMORY;
441 ret = gensec_register(newmech);
442 if (!NT_STATUS_IS_OK(ret)) {
443 DEBUG(0,("Failed to register '%s' gensec backend!\n",
444 gensec_sasl_security_ops.name));
445 return ret;
448 #endif
449 return NT_STATUS_OK;