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;
36 Connection *conn = context;
40 if ( message == NULL ) {
46 level = LDAP_DEBUG_ANY;
49 case SASL_LOG_WARNING:
50 level = LDAP_DEBUG_TRACE;
54 level = LDAP_DEBUG_TRACE;
62 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
63 "SASL [conn=%d] %s: %s\n",
64 conn ? conn->c_connid : -1,
67 Debug( level, "SASL [conn=%d] %s: %s\n",
68 conn ? conn->c_connid: -1,
77 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
78 string returned in *dnptr is in its own allocated memory, and must be free'd
79 by the calling process.
80 -Mark Adamson, Carnegie Mellon
83 int slap_sasl_getdn( Connection *conn, char *id, char **dnptr, int flags )
85 char *c=NULL, *c1, *dn=NULL;
91 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
92 "slap_sasl_getdn: conn %d id=%s\n",
93 conn ? conn->c_connid : -1,
94 id ? (*id ? id : "<empty>") : "NULL" ));
96 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
97 id?(*id?id:"<empty>"):"NULL",0,0 );
101 /* Blatantly anonymous ID */
102 len = strlen( "anonymous" );
103 if( id && !strncasecmp( id, "anonymous", len) &&
104 ( id[len] == '\0' || id[len] == '@' ) ) {
106 return( LDAP_SUCCESS );
108 ctx = conn->c_sasl_context;
111 /* An authcID needs to be converted to authzID form */
112 if( flags & FLAG_GETDN_AUTHCID ) {
113 if( sasl_external_x509dn_convert && conn->c_sasl_bind_mech
114 && ( strcasecmp( "EXTERNAL", conn->c_sasl_bind_mech ) == 0 )
115 && len && dn[0] == '/' && dn[len-1]== '/' )
117 /* check SASL external for X.509 style DN and */
118 /* convert to dn:<dn> form */
119 char *tmpdn = ldap_dcedn2dn( id );
120 len = strlen( tmpdn );
122 dn = ch_malloc( len+4 );
126 memmove( &dn[3], tmpdn, len+1 );
130 /* convert to u:<username> form */
131 dn = ch_malloc( len+3 );
134 memmove( &dn[2], id, len+1 );
138 dn = ch_strdup( id );
141 /* An authzID must be properly prefixed */
142 if( flags & FLAG_GETDN_AUTHZID
143 && strncasecmp( dn, "u:", 2 )
144 && strncasecmp( dn, "dn:", 3 ) )
148 return( LDAP_INAPPROPRIATE_AUTH );
151 /* Username strings */
152 if( !strncasecmp( dn, "u:", 2 ) ) {
153 int len1 = strlen( ",cn=auth" );
154 len += strlen( "dn:uid=" ) + len1;
156 /* Figure out how much data we have for the dn */
157 rc = sasl_getprop( ctx, SASL_REALM, (void **)&c );
158 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
160 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
161 "slap_sasl_getdn: getprop(REALM) failed.\n" ));
163 Debug(LDAP_DEBUG_TRACE,
164 "getdn: getprop(REALM) failed!\n", 0,0,0);
169 return( LDAP_OPERATIONS_ERROR );
173 len += strlen( c ) + strlen(",cn=" );
176 if( conn->c_sasl_bind_mech ) {
177 len += strlen( conn->c_sasl_bind_mech ) + strlen( ",cn=" );
180 /* Build the new dn */
182 dn = ch_malloc( len );
183 len = sprintf( dn, "dn:uid=%s", c1+2 );
187 len += sprintf( dn+len, ",cn=%s", c );
189 if( conn->c_sasl_bind_mech ) {
190 len += sprintf( dn+len, ",cn=%s", conn->c_sasl_bind_mech );
192 strcpy( dn+len, ",cn=auth" );
196 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
197 "slap_sasl_getdn: u:id converted to %s.\n", dn ));
199 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn,0,0 );
203 /* DN strings that are a cn=auth identity to run through regexp */
204 if( !strncasecmp( dn, "dn:", 3) && ( ( flags & FLAG_GETDN_FINAL ) == 0 ) ) {
205 c1 = slap_sasl2dn( dn + 3 );
209 /* Reaffix the dn: prefix if it was removed */
210 if( strncasecmp( dn, "dn:", 3) ) {
212 dn = ch_malloc( strlen( c1 ) + 4 );
213 sprintf( dn, "dn:%s", c1 );
218 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
219 "slap_sasl_getdn: dn:id converted to %s.\n", dn ));
221 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n", dn,0,0 );
226 if( ( flags & FLAG_GETDN_FINAL ) == 0 ) {
231 return( LDAP_SUCCESS );
244 char *authcDN, *authzDN;
246 Connection *conn = context;
251 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
252 "slap_sas_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
253 conn ? conn->c_connid : -1,
254 authcid ? authcid : "<empty>",
255 authzid ? authzid : "<empty>" ));
257 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
258 "authcid=\"%s\" authzid=\"%s\"\n",
259 (long) (conn ? conn->c_connid : -1),
260 authcid ? authcid : "<empty>",
261 authzid ? authzid : "<empty>" );
265 /* Convert the identities to DN's. If no authzid was given, client will
266 be bound as the DN matching their username */
267 rc = slap_sasl_getdn( conn, (char *)authcid, &authcDN, FLAG_GETDN_AUTHCID );
268 if( rc != LDAP_SUCCESS ) {
269 *errstr = ldap_err2string( rc );
272 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
274 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
275 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
276 conn ? conn->c_connid : -1, authcDN ));
278 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
279 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN,0 );
286 rc = slap_sasl_getdn( conn, (char *)authzid, &authzDN, FLAG_GETDN_AUTHZID );
287 if( rc != LDAP_SUCCESS ) {
289 *errstr = ldap_err2string( rc );
293 rc = slap_sasl_authorized( authcDN, authzDN );
296 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
297 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
298 (long)(conn ? conn->c_connid : -1), rc ));
300 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
301 " authorization disallowed (%d)\n",
302 (long) (conn ? conn->c_connid : -1), rc, 0 );
305 *errstr = "not authorized";
312 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
313 "slap_sasl_authorize: conn %d authorization allowed\n",
314 (long)(conn ? conn->c_connid : -1 ) ));
316 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
317 " authorization allowed\n",
318 (long) (conn ? conn->c_connid : -1), 0, 0 );
330 slap_sasl_err2ldap( int saslerr )
336 rc = LDAP_SASL_BIND_IN_PROGRESS;
345 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
348 rc = LDAP_INVALID_CREDENTIALS;
351 rc = LDAP_INSUFFICIENT_ACCESS;
355 rc = LDAP_INAPPROPRIATE_AUTH;
367 int slap_sasl_init( void )
369 #ifdef HAVE_CYRUS_SASL
371 static sasl_callback_t server_callbacks[] = {
372 { SASL_CB_LOG, &slap_sasl_log, NULL },
373 { SASL_CB_LIST_END, NULL, NULL }
383 ldap_pvt_sasl_mutex_new,
384 ldap_pvt_sasl_mutex_lock,
385 ldap_pvt_sasl_mutex_unlock,
386 ldap_pvt_sasl_mutex_dispose );
388 /* should provide callbacks for logging */
389 /* server name should be configurable */
390 rc = sasl_server_init( server_callbacks, "slapd" );
392 if( rc != SASL_OK ) {
394 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
395 "slap_sasl_init: init failed.\n" ));
397 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
405 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
406 "slap_sasl_init: initialized!\n"));
408 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
413 /* default security properties */
414 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
415 sasl_secprops.max_ssf = INT_MAX;
416 sasl_secprops.maxbufsize = 65536;
417 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
423 int slap_sasl_destroy( void )
425 #ifdef HAVE_CYRUS_SASL
431 int slap_sasl_open( Connection *conn )
433 int sc = LDAP_SUCCESS;
435 #ifdef HAVE_CYRUS_SASL
436 sasl_conn_t *ctx = NULL;
437 sasl_callback_t *session_callbacks;
439 assert( conn->c_sasl_context == NULL );
440 assert( conn->c_sasl_extra == NULL );
442 conn->c_sasl_layers = 0;
445 ch_calloc( 3, sizeof(sasl_callback_t));
446 conn->c_sasl_extra = session_callbacks;
448 session_callbacks[0].id = SASL_CB_LOG;
449 session_callbacks[0].proc = &slap_sasl_log;
450 session_callbacks[0].context = conn;
452 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
453 session_callbacks[1].proc = &slap_sasl_authorize;
454 session_callbacks[1].context = conn;
456 session_callbacks[2].id = SASL_CB_LIST_END;
457 session_callbacks[2].proc = NULL;
458 session_callbacks[2].context = NULL;
460 if( global_host == NULL ) {
461 global_host = ldap_pvt_get_fqdn( NULL );
464 /* create new SASL context */
465 sc = sasl_server_new( "ldap", global_host, global_realm,
466 session_callbacks, SASL_SECURITY_LAYER, &ctx );
468 if( sc != SASL_OK ) {
470 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
471 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
473 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
480 conn->c_sasl_context = ctx;
482 if( sc == SASL_OK ) {
483 sc = sasl_setprop( ctx,
484 SASL_SEC_PROPS, &sasl_secprops );
486 if( sc != SASL_OK ) {
488 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
489 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
491 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
495 slap_sasl_close( conn );
500 sc = slap_sasl_err2ldap( sc );
505 int slap_sasl_external(
508 const char *auth_id )
510 #ifdef HAVE_CYRUS_SASL
512 sasl_conn_t *ctx = conn->c_sasl_context;
513 sasl_external_properties_t extprops;
516 return LDAP_UNAVAILABLE;
519 memset( &extprops, '\0', sizeof(extprops) );
521 extprops.auth_id = (char *) auth_id;
523 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
524 (void *) &extprops );
526 if ( sc != SASL_OK ) {
534 int slap_sasl_reset( Connection *conn )
536 #ifdef HAVE_CYRUS_SASL
537 sasl_conn_t *ctx = conn->c_sasl_context;
542 /* must return "anonymous" */
546 char ** slap_sasl_mechs( Connection *conn )
550 #ifdef HAVE_CYRUS_SASL
551 sasl_conn_t *ctx = conn->c_sasl_context;
557 sc = sasl_listmech( ctx,
558 NULL, NULL, ",", NULL,
559 &mechstr, NULL, NULL );
561 if( sc != SASL_OK ) {
563 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
564 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
566 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
573 mechs = str2charray( mechstr, "," );
582 int slap_sasl_close( Connection *conn )
584 #ifdef HAVE_CYRUS_SASL
585 sasl_conn_t *ctx = conn->c_sasl_context;
588 sasl_dispose( &ctx );
591 conn->c_sasl_context = NULL;
593 free( conn->c_sasl_extra );
594 conn->c_sasl_extra = NULL;
611 #ifdef HAVE_CYRUS_SASL
612 sasl_conn_t *ctx = conn->c_sasl_context;
613 struct berval response;
619 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
620 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%d\n",
622 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech,
623 cred ? cred->bv_len : 0 ));
625 Debug(LDAP_DEBUG_ARGS,
626 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%d\n", dn,
627 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech,
628 cred ? cred->bv_len : 0 );
633 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
634 NULL, "SASL unavailable on this session", NULL, NULL );
638 if ( !conn->c_sasl_bind_in_progress ) {
639 sc = sasl_server_start( ctx,
640 conn->c_sasl_bind_mech,
641 cred->bv_val, cred->bv_len,
642 (char **)&response.bv_val, &reslen, &errstr );
645 sc = sasl_server_step( ctx,
646 cred->bv_val, cred->bv_len,
647 (char **)&response.bv_val, &reslen, &errstr );
650 response.bv_len = reslen;
652 if ( sc == SASL_OK ) {
653 char *username = NULL;
655 sc = sasl_getprop( ctx,
656 SASL_USERNAME, (void **)&username );
658 if ( sc != SASL_OK ) {
660 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
661 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
663 Debug(LDAP_DEBUG_TRACE,
664 "slap_sasl_bind: getprop(USERNAME) failed!\n",
669 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
670 NULL, "no SASL username", NULL, NULL );
673 rc = slap_sasl_getdn( conn, username, edn, FLAG_GETDN_FINAL );
675 if( rc == LDAP_SUCCESS ) {
677 sasl_ssf_t *ssf = NULL;
678 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
679 *ssfp = ssf ? *ssf : 0;
682 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
683 conn->c_sasl_layers++;
684 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
687 /* Store the authorization DN as a subjectDN */
692 (*edn)[i-3] = (*edn)[i];
693 } while( (*edn)[i] );
696 send_ldap_sasl( conn, op, rc,
697 NULL, NULL, NULL, NULL,
698 response.bv_len ? &response : NULL );
701 send_ldap_result( conn, op, rc,
702 NULL, errstr, NULL, NULL );
706 } else if ( sc == SASL_CONTINUE ) {
707 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
708 NULL, NULL, NULL, NULL, &response );
711 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
712 NULL, errstr, NULL, NULL );
716 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
717 "slap_sasl_bind: rc=%d\n", rc ));
719 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
724 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
725 NULL, "SASL not supported", NULL, NULL );
731 char* slap_sasl_secprops( const char *in )
733 #ifdef HAVE_CYRUS_SASL
734 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
736 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
738 return "SASL not supported";