3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include "proto-slap.h"
18 #ifdef HAVE_CYRUS_SASL
28 static char *sasl_host = NULL;
29 static sasl_security_properties_t sasl_secprops;
38 Connection *conn = context;
42 if ( message == NULL ) {
48 level = LDAP_DEBUG_ANY;
51 case SASL_LOG_WARNING:
52 level = LDAP_DEBUG_TRACE;
56 level = LDAP_DEBUG_TRACE;
63 Debug( level, "SASL [conn=%d] %s: %s\n",
64 conn ? conn->c_connid: -1,
78 Connection *conn = context;
82 if ( authcid == NULL || *authcid == '\0' ) {
83 *errstr = "empty authentication identity";
85 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
86 "empty authentication identity\n",
87 (long) (conn ? conn->c_connid : -1),
92 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
93 "authcid=\"%s\" authzid=\"%s\"\n",
94 (long) (conn ? conn->c_connid : -1),
95 authcid ? authcid : "<empty>",
96 authcid ? authcid : "<empty>" );
98 if ( authzid == NULL || *authzid == '\0' ||
99 strcmp( authcid, authzid ) == 0 )
102 size_t len = sizeof("u:") + strlen( authcid );
104 cuser = ch_malloc( len );
105 strcpy( cuser, "u:" );
106 strcpy( &cuser[sizeof("u:")-1], authcid );
110 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
111 "\"%s\" as \"%s\"\n",
112 (long) (conn ? conn->c_connid : -1),
118 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
119 "\"%s\" as \"%s\" disallowed. No policy.\n",
120 (long) (conn ? conn->c_connid : -1),
123 *errstr = "no proxy policy";
129 slap_sasl_err2ldap( int saslerr )
135 rc = LDAP_SASL_BIND_IN_PROGRESS;
144 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
147 rc = LDAP_INVALID_CREDENTIALS;
150 rc = LDAP_INSUFFICIENT_ACCESS;
154 rc = LDAP_INAPPROPRIATE_AUTH;
166 int slap_sasl_init( void )
168 #ifdef HAVE_CYRUS_SASL
170 sasl_conn_t *server = NULL;
171 static sasl_callback_t server_callbacks[] = {
172 { SASL_CB_LOG, &slap_sasl_log, NULL },
173 { SASL_CB_LIST_END, NULL, NULL }
183 ldap_pvt_sasl_mutex_new,
184 ldap_pvt_sasl_mutex_lock,
185 ldap_pvt_sasl_mutex_unlock,
186 ldap_pvt_sasl_mutex_dispose );
188 /* should provide callbacks for logging */
189 /* server name should be configurable */
190 rc = sasl_server_init( server_callbacks, "slapd" );
192 if( rc != SASL_OK ) {
193 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
198 if( sasl_host == NULL ) {
199 static char hostname[MAXHOSTNAMELEN+1];
201 if( gethostname( hostname, MAXHOSTNAMELEN ) == 0 ) {
202 hostname[MAXHOSTNAMELEN] = '\0';
203 sasl_host = hostname;
207 Debug( LDAP_DEBUG_TRACE,
208 "slap_sasl_init: %s initialized!\n",
211 /* default security properties */
212 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
213 sasl_secprops.max_ssf = UINT_MAX;
214 sasl_secprops.maxbufsize = 65536;
215 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
218 lutil_passwd_sasl_conn = server;
220 sasl_dispose( &server );
227 int slap_sasl_destroy( void )
229 #ifdef HAVE_CYRUS_SASL
231 sasl_dispose( &lutil_passwd_sasl_conn );
238 int slap_sasl_open( Connection *conn )
240 int sc = LDAP_SUCCESS;
242 #ifdef HAVE_CYRUS_SASL
243 sasl_conn_t *ctx = NULL;
244 sasl_callback_t *session_callbacks;
246 assert( conn->c_sasl_context == NULL );
247 assert( conn->c_sasl_extra == NULL );
250 ch_calloc( 3, sizeof(sasl_callback_t));
251 conn->c_sasl_extra = session_callbacks;
253 session_callbacks[0].id = SASL_CB_LOG;
254 session_callbacks[0].proc = &slap_sasl_log;
255 session_callbacks[0].context = conn;
257 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
258 session_callbacks[1].proc = &slap_sasl_authorize;
259 session_callbacks[1].context = conn;
261 session_callbacks[2].id = SASL_CB_LIST_END;
262 session_callbacks[2].proc = NULL;
263 session_callbacks[2].context = NULL;
265 /* create new SASL context */
266 sc = sasl_server_new( "ldap", sasl_host, global_realm,
268 #ifdef LDAP_SASL_SECURITY_LAYER
276 if( sc != SASL_OK ) {
277 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
282 conn->c_sasl_context = ctx;
284 if( sc == SASL_OK ) {
285 sc = sasl_setprop( ctx,
286 SASL_SEC_PROPS, &sasl_secprops );
288 if( sc != SASL_OK ) {
289 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
291 slap_sasl_close( conn );
296 sc = slap_sasl_err2ldap( sc );
301 int slap_sasl_external(
306 #ifdef HAVE_CYRUS_SASL
308 sasl_conn_t *ctx = conn->c_sasl_context;
309 sasl_external_properties_t extprops;
312 return LDAP_UNAVAILABLE;
315 memset( &extprops, 0L, sizeof(extprops) );
317 extprops.auth_id = auth_id;
319 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
320 (void *) &extprops );
322 if ( sc != SASL_OK ) {
330 int slap_sasl_reset( Connection *conn )
332 #ifdef HAVE_CYRUS_SASL
333 sasl_conn_t *ctx = conn->c_sasl_context;
338 /* must return "anonymous" */
342 char ** slap_sasl_mechs( Connection *conn )
346 #ifdef HAVE_CYRUS_SASL
347 sasl_conn_t *ctx = conn->c_sasl_context;
353 sc = sasl_listmech( ctx,
354 NULL, NULL, ",", NULL,
355 &mechstr, NULL, NULL );
357 if( sc != SASL_OK ) {
358 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
363 mechs = str2charray( mechstr, "," );
372 int slap_sasl_close( Connection *conn )
374 #ifdef HAVE_CYRUS_SASL
375 sasl_conn_t *ctx = conn->c_sasl_context;
378 sasl_dispose( &ctx );
381 conn->c_sasl_context = NULL;
383 free( conn->c_sasl_extra );
384 conn->c_sasl_extra = NULL;
401 #ifdef HAVE_CYRUS_SASL
402 sasl_conn_t *ctx = conn->c_sasl_context;
403 struct berval response;
408 Debug(LDAP_DEBUG_ARGS,
409 "==> sasl_bind: dn=\"%s\" mech=%s cred->bv_len=%d\n",
410 dn, mech ? mech : "<continuing>", cred ? cred->bv_len : 0 );
413 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
414 NULL, "SASL unavailable on this session", NULL, NULL );
418 if ( mech != NULL ) {
419 sc = sasl_server_start( ctx,
421 cred->bv_val, cred->bv_len,
422 (char **)&response.bv_val, &reslen, &errstr );
425 sc = sasl_server_step( ctx,
426 cred->bv_val, cred->bv_len,
427 (char **)&response.bv_val, &reslen, &errstr );
430 response.bv_len = reslen;
432 if ( sc == SASL_OK ) {
433 char *username = NULL;
435 sc = sasl_getprop( ctx,
436 SASL_USERNAME, (void **)&username );
438 if ( sc != SASL_OK ) {
439 Debug(LDAP_DEBUG_TRACE,
440 "slap_sasl_bind: getprop(USERNAME) failed!\n",
443 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
444 NULL, "no SASL username", NULL, NULL );
446 } else if ( username == NULL || *username == '\0' ) {
447 Debug(LDAP_DEBUG_TRACE,
448 "slap_sasl_bind: getprop(USERNAME) returned NULL!\n",
451 send_ldap_result( conn, op, rc = LDAP_INSUFFICIENT_ACCESS,
452 NULL, "no SASL username", NULL, NULL );
456 sasl_ssf_t *ssf = NULL;
458 (void) sasl_getprop( ctx,
459 SASL_REALM, (void **)&realm );
461 (void) sasl_getprop( ctx,
462 SASL_SSF, (void *)&ssf );
464 Debug(LDAP_DEBUG_TRACE,
465 "slap_sasl_bind: username=\"%s\" realm=\"%s\" ssf=%lu\n",
466 username ? username : "",
468 (unsigned long) ( ssf ? *ssf : 0 ) );
473 if( username == NULL || (
474 !strncasecmp( username, "anonymous", sizeof("anonyous")-1 ) &&
475 ( ( username[sizeof("anonymous")] == '\0' ) ||
476 ( username[sizeof("anonymous")] == '@' ) ) ) )
478 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: anonymous\n",
481 } else if ( username[0] == 'u' && username[1] == ':'
482 && username[2] != '\0'
483 && strpbrk( &username[2], "=,;\"\\") == NULL )
485 *edn = ch_malloc( sizeof( "uid= + realm=" )
486 + strlen( &username[2] )
487 + ( realm ? strlen( realm ) : 0 ) );
489 strcpy( *edn, "uid=" );
490 strcat( *edn, &username[2] );
492 if( realm && *realm ) {
493 strcat( *edn, " + realm=" );
494 strcat( *edn, realm );
497 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: authzdn: \"%s\"\n",
501 rc = LDAP_INAPPROPRIATE_AUTH;
502 errstr = "authorization disallowed";
503 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: %s\n",
507 if( rc == LDAP_SUCCESS ) {
508 send_ldap_sasl( conn, op, rc,
509 NULL, NULL, NULL, NULL,
510 response.bv_len ? &response : NULL );
513 send_ldap_result( conn, op, rc,
514 NULL, errstr, NULL, NULL );
518 } else if ( sc == SASL_CONTINUE ) {
519 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
520 NULL, NULL, NULL, NULL, &response );
523 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
524 NULL, errstr, NULL, NULL );
527 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
530 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
531 NULL, "SASL not supported", NULL, NULL );
537 char* slap_sasl_secprops( const char *in )
539 #ifdef HAVE_CYRUS_SASL
540 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
542 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
544 return "SASL not supported";