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 len1 = strlen( ",cn=auth" );
132 if( !strncasecmp( dn, "u:", 2 ) ) {
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 ) {
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 );
151 len += strlen( c ) + strlen(",cn=" );
153 if( conn->c_sasl_bind_mech ) {
154 len += strlen( conn->c_sasl_bind_mech ) + strlen( ",cn=mech" );
157 /* Build the new dn */
159 dn = ch_malloc( len );
160 len = sprintf( dn, "dn:uid=%s", c1+2 );
164 len += sprintf( dn+len, ",cn=%s", c );
166 if( conn->c_sasl_bind_mech ) {
167 len += sprintf( dn+len, ",cn=%s", conn->c_sasl_bind_mech );
169 strcpy( dn+len, ",cn=auth" );
172 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
173 "getdn: u:id converted to %s.\n", dn ));
175 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn,0,0 );
180 /* DN strings that are a cn=auth identity to run through regexp */
181 if( !strncasecmp( dn, "dn:", 3) && ( ( flags & FLAG_GETDN_FINAL ) == 0 ) ) {
182 c1 = slap_sasl2dn( dn + 3 );
186 /* Reaffix the dn: prefix if it was removed */
187 if( strncasecmp( dn, "dn:", 3) ) {
189 dn = ch_malloc( strlen( c1 ) + 4 );
190 sprintf( dn, "dn:%s", c1 );
194 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
195 "slap_sasl_getdn: dn:id converted to %s.\n", dn ));
197 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n", dn,0,0 );
203 if( ( flags & FLAG_GETDN_FINAL ) == 0 ) {
208 return( LDAP_SUCCESS );
221 char *authcDN, *authzDN;
223 Connection *conn = context;
228 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
229 "slap_sas_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
230 conn ? conn->c_connid : -1,
231 authcid ? authcid : "<empty>",
232 authzid ? authzid : "<empty>" ));
234 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
235 "authcid=\"%s\" authzid=\"%s\"\n",
236 (long) (conn ? conn->c_connid : -1),
237 authcid ? authcid : "<empty>",
238 authzid ? authzid : "<empty>" );
242 /* Convert the identities to DN's. If no authzid was given, client will
243 be bound as the DN matching their username */
244 rc = slap_sasl_getdn( conn, (char *)authcid, &authcDN, FLAG_GETDN_AUTHCID );
245 if( rc != LDAP_SUCCESS ) {
246 *errstr = ldap_err2string( rc );
249 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
251 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
252 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
253 conn ? conn->c_connid : -1, authcDN ));
255 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
256 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN,0 );
263 rc = slap_sasl_getdn( conn, (char *)authzid, &authzDN, FLAG_GETDN_AUTHZID );
264 if( rc != LDAP_SUCCESS ) {
266 *errstr = ldap_err2string( rc );
270 rc = slap_sasl_authorized( authcDN, authzDN );
273 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
274 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
275 (long)(conn ? conn->c_connid : -1), rc ));
277 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
278 " authorization disallowed (%d)\n",
279 (long) (conn ? conn->c_connid : -1), rc, 0 );
282 *errstr = "not authorized";
289 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
290 "slap_sasl_authorize: conn %d authorization allowed\n",
291 (long)(conn ? conn->c_connid : -1 ) );
293 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
294 " authorization allowed\n",
295 (long) (conn ? conn->c_connid : -1), 0, 0 );
307 slap_sasl_err2ldap( int saslerr )
313 rc = LDAP_SASL_BIND_IN_PROGRESS;
322 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
325 rc = LDAP_INVALID_CREDENTIALS;
328 rc = LDAP_INSUFFICIENT_ACCESS;
332 rc = LDAP_INAPPROPRIATE_AUTH;
344 int slap_sasl_init( void )
346 #ifdef HAVE_CYRUS_SASL
348 static sasl_callback_t server_callbacks[] = {
349 { SASL_CB_LOG, &slap_sasl_log, NULL },
350 { SASL_CB_LIST_END, NULL, NULL }
360 ldap_pvt_sasl_mutex_new,
361 ldap_pvt_sasl_mutex_lock,
362 ldap_pvt_sasl_mutex_unlock,
363 ldap_pvt_sasl_mutex_dispose );
365 /* should provide callbacks for logging */
366 /* server name should be configurable */
367 rc = sasl_server_init( server_callbacks, "slapd" );
369 if( rc != SASL_OK ) {
371 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
372 "slap_sasl_init: init failed.\n" ));
374 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
382 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
383 "slap_sasl_init: initialized!\n"));
385 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
390 /* default security properties */
391 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
392 sasl_secprops.max_ssf = INT_MAX;
393 sasl_secprops.maxbufsize = 65536;
394 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
400 int slap_sasl_destroy( void )
402 #ifdef HAVE_CYRUS_SASL
408 int slap_sasl_open( Connection *conn )
410 int sc = LDAP_SUCCESS;
412 #ifdef HAVE_CYRUS_SASL
413 sasl_conn_t *ctx = NULL;
414 sasl_callback_t *session_callbacks;
416 assert( conn->c_sasl_context == NULL );
417 assert( conn->c_sasl_extra == NULL );
419 conn->c_sasl_layers = 0;
422 ch_calloc( 3, sizeof(sasl_callback_t));
423 conn->c_sasl_extra = session_callbacks;
425 session_callbacks[0].id = SASL_CB_LOG;
426 session_callbacks[0].proc = &slap_sasl_log;
427 session_callbacks[0].context = conn;
429 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
430 session_callbacks[1].proc = &slap_sasl_authorize;
431 session_callbacks[1].context = conn;
433 session_callbacks[2].id = SASL_CB_LIST_END;
434 session_callbacks[2].proc = NULL;
435 session_callbacks[2].context = NULL;
437 if( global_host == NULL ) {
438 global_host = ldap_pvt_get_fqdn( NULL );
441 /* create new SASL context */
442 sc = sasl_server_new( "ldap", global_host, global_realm,
443 session_callbacks, SASL_SECURITY_LAYER, &ctx );
445 if( sc != SASL_OK ) {
447 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
448 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
450 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
457 conn->c_sasl_context = ctx;
459 if( sc == SASL_OK ) {
460 sc = sasl_setprop( ctx,
461 SASL_SEC_PROPS, &sasl_secprops );
463 if( sc != SASL_OK ) {
465 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
466 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
468 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
472 slap_sasl_close( conn );
477 sc = slap_sasl_err2ldap( sc );
482 int slap_sasl_external(
485 const char *auth_id )
487 #ifdef HAVE_CYRUS_SASL
489 sasl_conn_t *ctx = conn->c_sasl_context;
490 sasl_external_properties_t extprops;
493 return LDAP_UNAVAILABLE;
496 memset( &extprops, '\0', sizeof(extprops) );
498 extprops.auth_id = (char *) auth_id;
500 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
501 (void *) &extprops );
503 if ( sc != SASL_OK ) {
511 int slap_sasl_reset( Connection *conn )
513 #ifdef HAVE_CYRUS_SASL
514 sasl_conn_t *ctx = conn->c_sasl_context;
519 /* must return "anonymous" */
523 char ** slap_sasl_mechs( Connection *conn )
527 #ifdef HAVE_CYRUS_SASL
528 sasl_conn_t *ctx = conn->c_sasl_context;
534 sc = sasl_listmech( ctx,
535 NULL, NULL, ",", NULL,
536 &mechstr, NULL, NULL );
538 if( sc != SASL_OK ) {
540 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
541 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
543 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
550 mechs = str2charray( mechstr, "," );
559 int slap_sasl_close( Connection *conn )
561 #ifdef HAVE_CYRUS_SASL
562 sasl_conn_t *ctx = conn->c_sasl_context;
565 sasl_dispose( &ctx );
568 conn->c_sasl_context = NULL;
570 free( conn->c_sasl_extra );
571 conn->c_sasl_extra = NULL;
588 #ifdef HAVE_CYRUS_SASL
589 sasl_conn_t *ctx = conn->c_sasl_context;
590 struct berval response;
596 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
597 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%d\n",
599 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech,
600 cred ? cred->bv_len : 0 ));
602 Debug(LDAP_DEBUG_ARGS,
603 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%d\n", dn,
604 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech,
605 cred ? cred->bv_len : 0 );
610 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
611 NULL, "SASL unavailable on this session", NULL, NULL );
615 if ( !conn->c_sasl_bind_in_progress ) {
616 sc = sasl_server_start( ctx,
617 conn->c_sasl_bind_mech,
618 cred->bv_val, cred->bv_len,
619 (char **)&response.bv_val, &reslen, &errstr );
622 sc = sasl_server_step( ctx,
623 cred->bv_val, cred->bv_len,
624 (char **)&response.bv_val, &reslen, &errstr );
627 response.bv_len = reslen;
629 if ( sc == SASL_OK ) {
630 char *username = NULL;
632 sc = sasl_getprop( ctx,
633 SASL_USERNAME, (void **)&username );
635 if ( sc != SASL_OK ) {
637 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
638 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
640 Debug(LDAP_DEBUG_TRACE,
641 "slap_sasl_bind: getprop(USERNAME) failed!\n",
646 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
647 NULL, "no SASL username", NULL, NULL );
650 rc = slap_sasl_getdn( conn, username, edn, FLAG_GETDN_FINAL );
652 if( rc == LDAP_SUCCESS ) {
654 sasl_ssf_t *ssf = NULL;
655 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
656 *ssfp = ssf ? *ssf : 0;
659 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
660 conn->c_sasl_layers++;
661 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
664 /* Store the authorization DN as a subjectDN */
669 (*edn)[i-3] = (*edn)[i];
670 } while( (*edn)[i] );
673 send_ldap_sasl( conn, op, rc,
674 NULL, NULL, NULL, NULL,
675 response.bv_len ? &response : NULL );
678 send_ldap_result( conn, op, rc,
679 NULL, errstr, NULL, NULL );
683 } else if ( sc == SASL_CONTINUE ) {
684 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
685 NULL, NULL, NULL, NULL, &response );
688 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
689 NULL, errstr, NULL, NULL );
693 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
694 "slap_sasl_bind: rc=%d\n", rc ));
696 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
701 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
702 NULL, "SASL not supported", NULL, NULL );
708 char* slap_sasl_secprops( const char *in )
710 #ifdef HAVE_CYRUS_SASL
711 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
713 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
715 return "SASL not supported";