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 sasl_security_properties_t sasl_secprops;
37 Connection *conn = context;
41 if ( message == NULL ) {
47 level = LDAP_DEBUG_ANY;
50 case SASL_LOG_WARNING:
51 level = LDAP_DEBUG_TRACE;
55 level = LDAP_DEBUG_TRACE;
63 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
64 "SASL [conn=%d] %s: %s\n",
65 conn ? conn->c_connid : -1,
68 Debug( level, "SASL [conn=%d] %s: %s\n",
69 conn ? conn->c_connid: -1,
78 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
79 string returned in *dnptr is in its own allocated memory, and must be free'd
80 by the calling process.
81 -Mark Adamson, Carnegie Mellon
84 int slap_sasl_getdn( Connection *conn, char *id, char **dnptr, int flags )
86 char *c, *c1, *dn=NULL;
92 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
93 "slap_sasl_getdn: conn %d id=%s\n",
94 conn ? conn->c_connid : -1,
95 id ? (*id ? id : "<empty>") : "NULL" ));
97 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
98 id?(*id?id:"<empty>"):"NULL",0,0 );
102 /* Blatantly anonymous ID */
103 len = strlen( "anonymous" );
104 if( id && !strncasecmp( id, "anonymous", len) &&
105 ( id[len] == '\0' || id[len] == '@' ) ) {
107 return( LDAP_SUCCESS );
109 ctx = conn->c_sasl_context;
110 dn = ch_strdup( id );
113 /* An authcID will need to be prefixed with u: */
114 if( flags & FLAG_GETDN_AUTHCID ) {
115 dn = ch_realloc( dn, len+3 );
116 memmove( dn+2, dn, len+1 );
122 /* An authzID must be properly prefixed */
123 if( flags & FLAG_GETDN_AUTHZID && strncasecmp( dn, "u:", 2 ) &&
124 strncasecmp( dn, "dn:", 3 ) ) {
127 return( LDAP_INAPPROPRIATE_AUTH );
130 /* Username strings */
131 if( !strncasecmp( dn, "u:", 2 ) ) {
132 int len1 = strlen( ",cn=auth" );
133 len += strlen( "dn:uid=" ) + len1;
135 /* Figure out how much data we have for the dn */
136 rc = sasl_getprop( ctx, SASL_REALM, (void **)&c );
137 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
139 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
140 "slap_sasl_getdn: getprop(REALM) failed.\n" ));
142 Debug(LDAP_DEBUG_TRACE,
143 "getdn: getprop(REALM) failed!\n", 0,0,0);
148 return( LDAP_OPERATIONS_ERROR );
152 len += strlen( c ) + strlen(",cn=" );
155 if( conn->c_sasl_bind_mech ) {
156 len += strlen( conn->c_sasl_bind_mech ) + strlen( ",cn=" );
159 /* Build the new dn */
161 dn = ch_malloc( len );
162 len = sprintf( dn, "dn:uid=%s", c1+2 );
166 len += sprintf( dn+len, ",cn=%s", c );
168 if( conn->c_sasl_bind_mech ) {
169 len += sprintf( dn+len, ",cn=%s", conn->c_sasl_bind_mech );
171 strcpy( dn+len, ",cn=auth" );
175 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
176 "slap_sasl_getdn: u:id converted to %s.\n", dn ));
178 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn,0,0 );
182 /* DN strings that are a cn=auth identity to run through regexp */
183 if( !strncasecmp( dn, "dn:", 3) && ( ( flags & FLAG_GETDN_FINAL ) == 0 ) ) {
184 c1 = slap_sasl2dn( dn + 3 );
188 /* Reaffix the dn: prefix if it was removed */
189 if( strncasecmp( dn, "dn:", 3) ) {
191 dn = ch_malloc( strlen( c1 ) + 4 );
192 sprintf( dn, "dn:%s", c1 );
197 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
198 "slap_sasl_getdn: dn:id converted to %s.\n", dn ));
200 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n", dn,0,0 );
205 if( ( flags & FLAG_GETDN_FINAL ) == 0 ) {
210 return( LDAP_SUCCESS );
223 char *authcDN, *authzDN;
225 Connection *conn = context;
230 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
231 "slap_sas_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
232 conn ? conn->c_connid : -1,
233 authcid ? authcid : "<empty>",
234 authzid ? authzid : "<empty>" ));
236 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
237 "authcid=\"%s\" authzid=\"%s\"\n",
238 (long) (conn ? conn->c_connid : -1),
239 authcid ? authcid : "<empty>",
240 authzid ? authzid : "<empty>" );
244 /* Convert the identities to DN's. If no authzid was given, client will
245 be bound as the DN matching their username */
246 rc = slap_sasl_getdn( conn, (char *)authcid, &authcDN, FLAG_GETDN_AUTHCID );
247 if( rc != LDAP_SUCCESS ) {
248 *errstr = ldap_err2string( rc );
251 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
253 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
254 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
255 conn ? conn->c_connid : -1, authcDN ));
257 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
258 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN,0 );
265 rc = slap_sasl_getdn( conn, (char *)authzid, &authzDN, FLAG_GETDN_AUTHZID );
266 if( rc != LDAP_SUCCESS ) {
268 *errstr = ldap_err2string( rc );
272 rc = slap_sasl_authorized( authcDN, authzDN );
275 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
276 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
277 (long)(conn ? conn->c_connid : -1), rc ));
279 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
280 " authorization disallowed (%d)\n",
281 (long) (conn ? conn->c_connid : -1), rc, 0 );
284 *errstr = "not authorized";
291 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
292 "slap_sasl_authorize: conn %d authorization allowed\n",
293 (long)(conn ? conn->c_connid : -1 ) );
295 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
296 " authorization allowed\n",
297 (long) (conn ? conn->c_connid : -1), 0, 0 );
309 slap_sasl_err2ldap( int saslerr )
315 rc = LDAP_SASL_BIND_IN_PROGRESS;
324 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
327 rc = LDAP_INVALID_CREDENTIALS;
330 rc = LDAP_INSUFFICIENT_ACCESS;
334 rc = LDAP_INAPPROPRIATE_AUTH;
346 int slap_sasl_init( void )
348 #ifdef HAVE_CYRUS_SASL
350 static sasl_callback_t server_callbacks[] = {
351 { SASL_CB_LOG, &slap_sasl_log, NULL },
352 { SASL_CB_LIST_END, NULL, NULL }
362 ldap_pvt_sasl_mutex_new,
363 ldap_pvt_sasl_mutex_lock,
364 ldap_pvt_sasl_mutex_unlock,
365 ldap_pvt_sasl_mutex_dispose );
367 /* should provide callbacks for logging */
368 /* server name should be configurable */
369 rc = sasl_server_init( server_callbacks, "slapd" );
371 if( rc != SASL_OK ) {
373 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
374 "slap_sasl_init: init failed.\n" ));
376 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
384 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
385 "slap_sasl_init: initialized!\n"));
387 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
392 /* default security properties */
393 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
394 sasl_secprops.max_ssf = INT_MAX;
395 sasl_secprops.maxbufsize = 65536;
396 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
402 int slap_sasl_destroy( void )
404 #ifdef HAVE_CYRUS_SASL
410 int slap_sasl_open( Connection *conn )
412 int sc = LDAP_SUCCESS;
414 #ifdef HAVE_CYRUS_SASL
415 sasl_conn_t *ctx = NULL;
416 sasl_callback_t *session_callbacks;
418 assert( conn->c_sasl_context == NULL );
419 assert( conn->c_sasl_extra == NULL );
421 conn->c_sasl_layers = 0;
424 ch_calloc( 3, sizeof(sasl_callback_t));
425 conn->c_sasl_extra = session_callbacks;
427 session_callbacks[0].id = SASL_CB_LOG;
428 session_callbacks[0].proc = &slap_sasl_log;
429 session_callbacks[0].context = conn;
431 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
432 session_callbacks[1].proc = &slap_sasl_authorize;
433 session_callbacks[1].context = conn;
435 session_callbacks[2].id = SASL_CB_LIST_END;
436 session_callbacks[2].proc = NULL;
437 session_callbacks[2].context = NULL;
439 if( global_host == NULL ) {
440 global_host = ldap_pvt_get_fqdn( NULL );
443 /* create new SASL context */
444 sc = sasl_server_new( "ldap", global_host, global_realm,
445 session_callbacks, SASL_SECURITY_LAYER, &ctx );
447 if( sc != SASL_OK ) {
449 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
450 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
452 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
459 conn->c_sasl_context = ctx;
461 if( sc == SASL_OK ) {
462 sc = sasl_setprop( ctx,
463 SASL_SEC_PROPS, &sasl_secprops );
465 if( sc != SASL_OK ) {
467 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
468 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
470 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
474 slap_sasl_close( conn );
479 sc = slap_sasl_err2ldap( sc );
484 int slap_sasl_external(
487 const char *auth_id )
489 #ifdef HAVE_CYRUS_SASL
491 sasl_conn_t *ctx = conn->c_sasl_context;
492 sasl_external_properties_t extprops;
495 return LDAP_UNAVAILABLE;
498 memset( &extprops, '\0', sizeof(extprops) );
500 extprops.auth_id = (char *) auth_id;
502 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
503 (void *) &extprops );
505 if ( sc != SASL_OK ) {
513 int slap_sasl_reset( Connection *conn )
515 #ifdef HAVE_CYRUS_SASL
516 sasl_conn_t *ctx = conn->c_sasl_context;
521 /* must return "anonymous" */
525 char ** slap_sasl_mechs( Connection *conn )
529 #ifdef HAVE_CYRUS_SASL
530 sasl_conn_t *ctx = conn->c_sasl_context;
536 sc = sasl_listmech( ctx,
537 NULL, NULL, ",", NULL,
538 &mechstr, NULL, NULL );
540 if( sc != SASL_OK ) {
542 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
543 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
545 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
552 mechs = str2charray( mechstr, "," );
561 int slap_sasl_close( Connection *conn )
563 #ifdef HAVE_CYRUS_SASL
564 sasl_conn_t *ctx = conn->c_sasl_context;
567 sasl_dispose( &ctx );
570 conn->c_sasl_context = NULL;
572 free( conn->c_sasl_extra );
573 conn->c_sasl_extra = NULL;
590 #ifdef HAVE_CYRUS_SASL
591 sasl_conn_t *ctx = conn->c_sasl_context;
592 struct berval response;
598 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
599 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%d\n",
601 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech,
602 cred ? cred->bv_len : 0 ));
604 Debug(LDAP_DEBUG_ARGS,
605 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%d\n", dn,
606 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech,
607 cred ? cred->bv_len : 0 );
612 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
613 NULL, "SASL unavailable on this session", NULL, NULL );
617 if ( !conn->c_sasl_bind_in_progress ) {
618 sc = sasl_server_start( ctx,
619 conn->c_sasl_bind_mech,
620 cred->bv_val, cred->bv_len,
621 (char **)&response.bv_val, &reslen, &errstr );
624 sc = sasl_server_step( ctx,
625 cred->bv_val, cred->bv_len,
626 (char **)&response.bv_val, &reslen, &errstr );
629 response.bv_len = reslen;
631 if ( sc == SASL_OK ) {
632 char *username = NULL;
634 sc = sasl_getprop( ctx,
635 SASL_USERNAME, (void **)&username );
637 if ( sc != SASL_OK ) {
639 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
640 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
642 Debug(LDAP_DEBUG_TRACE,
643 "slap_sasl_bind: getprop(USERNAME) failed!\n",
648 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
649 NULL, "no SASL username", NULL, NULL );
652 rc = slap_sasl_getdn( conn, username, edn, FLAG_GETDN_FINAL );
654 if( rc == LDAP_SUCCESS ) {
656 sasl_ssf_t *ssf = NULL;
657 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
658 *ssfp = ssf ? *ssf : 0;
661 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
662 conn->c_sasl_layers++;
663 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
666 /* Store the authorization DN as a subjectDN */
671 (*edn)[i-3] = (*edn)[i];
672 } while( (*edn)[i] );
675 send_ldap_sasl( conn, op, rc,
676 NULL, NULL, NULL, NULL,
677 response.bv_len ? &response : NULL );
680 send_ldap_result( conn, op, rc,
681 NULL, errstr, NULL, NULL );
685 } else if ( sc == SASL_CONTINUE ) {
686 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
687 NULL, NULL, NULL, NULL, &response );
690 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
691 NULL, errstr, NULL, NULL );
695 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
696 "slap_sasl_bind: rc=%d\n", rc ));
698 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
703 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
704 NULL, "SASL not supported", NULL, NULL );
710 char* slap_sasl_secprops( const char *in )
712 #ifdef HAVE_CYRUS_SASL
713 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
715 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
717 return "SASL not supported";