3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
11 #include <ac/string.h>
18 #ifdef HAVE_CYRUS_SASL
28 /* Flags for telling slap_sasl_getdn() what type of identity is being passed */
29 #define FLAG_GETDN_FINAL 1
30 #define FLAG_GETDN_AUTHCID 2
31 #define FLAG_GETDN_AUTHZID 4
33 static sasl_security_properties_t sasl_secprops;
41 Connection *conn = context;
45 if ( message == NULL ) {
51 level = LDAP_DEBUG_ANY;
54 case SASL_LOG_WARNING:
55 level = LDAP_DEBUG_TRACE;
59 level = LDAP_DEBUG_TRACE;
67 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
68 "SASL [conn=%ld] %s: %s\n",
69 conn ? conn->c_connid : -1,
72 Debug( level, "SASL [conn=%ld] %s: %s\n",
73 conn ? conn->c_connid: -1,
82 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
83 string returned in *dn is in its own allocated memory, and must be free'd
84 by the calling process.
85 -Mark Adamson, Carnegie Mellon
91 static struct berval ext_bv = { sizeof("EXTERNAL")-1, "EXTERNAL" };
93 int slap_sasl_getdn( Connection *conn, char *id, struct berval *dn, int flags )
96 int rc, len, is_dn = 0;
101 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
102 "slap_sasl_getdn: conn %d id=%s\n",
103 conn ? conn->c_connid : -1,
104 id ? (*id ? id : "<empty>") : "NULL" ));
106 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
107 id?(*id?id:"<empty>"):"NULL",0,0 );
113 /* Blatantly anonymous ID */
115 ( id[sizeof( "anonymous" )-1] == '\0'
116 || id[sizeof( "anonymous" )-1] == '@' ) &&
117 !strncasecmp( id, "anonymous", sizeof( "anonymous" )-1) ) {
118 return( LDAP_SUCCESS );
120 ctx = conn->c_sasl_context;
123 /* An authcID needs to be converted to authzID form */
124 if( flags & FLAG_GETDN_AUTHCID ) {
125 if( sasl_external_x509dn_convert
126 && conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len
127 && ( strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
130 /* check SASL external for X.509 style DN and */
131 /* convert to dn:<dn> form */
132 dn->bv_val = ldap_dcedn2dn( id );
133 dn->bv_len = strlen(dn->bv_val);
137 /* convert to u:<username> form */
138 ber_str2bv( id, len, 1, dn );
143 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
145 ber_str2bv( id+2, len-2, 1, dn );
146 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
148 ber_str2bv( id+3, len-3, 1, dn );
152 /* An authzID must be properly prefixed */
153 if( (flags & FLAG_GETDN_AUTHZID) && !is_dn ) {
157 return( LDAP_INAPPROPRIATE_AUTH );
160 /* Username strings */
161 if( is_dn == SET_U ) {
163 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
165 /* Figure out how much data we have for the dn */
166 rc = sasl_getprop( ctx, SASL_REALM, (void **)&c );
167 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
169 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
170 "slap_sasl_getdn: getprop(REALM) failed.\n" ));
172 Debug(LDAP_DEBUG_TRACE,
173 "getdn: getprop(REALM) failed!\n", 0,0,0);
176 ch_free( dn->bv_val );
178 return( LDAP_OPERATIONS_ERROR );
182 len += strlen( c ) + sizeof(",cn=")-1;
185 if( conn->c_sasl_bind_mech.bv_len ) {
186 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
189 /* Build the new dn */
191 dn->bv_val = ch_malloc( len );
192 p = slap_strcopy( dn->bv_val, "uid=" );
193 p = slap_strcopy( p, c1 );
197 p = slap_strcopy( p, ",cn=" );
198 p = slap_strcopy( p, c );
200 if( conn->c_sasl_bind_mech.bv_len ) {
201 p = slap_strcopy( p, ",cn=" );
202 p = slap_strcopy( p, conn->c_sasl_bind_mech.bv_val );
204 p = slap_strcopy( p, ",cn=auth" );
205 dn->bv_len = p - dn->bv_val;
209 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
210 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val ));
212 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
216 /* DN strings that are a cn=auth identity to run through regexp */
217 if( is_dn == SET_DN && ( ( flags & FLAG_GETDN_FINAL ) == 0 ) )
219 slap_sasl2dn( dn, &dn2 );
221 ch_free( dn->bv_val );
224 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
225 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val ));
227 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
233 if( flags & FLAG_GETDN_FINAL ) {
234 /* omit "dn:" prefix */
237 rc = dnNormalize2( NULL, dn, &dn2 );
239 if ( rc != LDAP_SUCCESS ) {
246 /* Attach the "dn:" prefix if needed */
247 if ( is_dn == SET_DN ) {
248 c1 = ch_malloc( dn->bv_len + sizeof("dn:") );
250 strcpy( c1 + 3, dn->bv_val );
256 return( LDAP_SUCCESS );
269 struct berval authcDN, authzDN;
271 Connection *conn = context;
276 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
277 "slap_sas_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
278 conn ? conn->c_connid : -1,
279 authcid ? authcid : "<empty>",
280 authzid ? authzid : "<empty>" ));
282 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
283 "authcid=\"%s\" authzid=\"%s\"\n",
284 (long) (conn ? conn->c_connid : -1),
285 authcid ? authcid : "<empty>",
286 authzid ? authzid : "<empty>" );
290 /* Convert the identities to DN's. If no authzid was given, client will
291 be bound as the DN matching their username */
292 rc = slap_sasl_getdn( conn, (char *)authcid, &authcDN, FLAG_GETDN_AUTHCID );
293 if( rc != LDAP_SUCCESS ) {
294 *errstr = ldap_err2string( rc );
297 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
299 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
300 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
301 conn ? conn->c_connid : -1, authcDN.bv_val ));
303 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
304 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
307 *user = authcDN.bv_val;
311 rc = slap_sasl_getdn( conn, (char *)authzid, &authzDN, FLAG_GETDN_AUTHZID );
312 if( rc != LDAP_SUCCESS ) {
313 ch_free( authcDN.bv_val );
314 *errstr = ldap_err2string( rc );
318 rc = slap_sasl_authorized( &authcDN, &authzDN );
321 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
322 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
323 (long)(conn ? conn->c_connid : -1), rc ));
325 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
326 " authorization disallowed (%d)\n",
327 (long) (conn ? conn->c_connid : -1), rc, 0 );
330 *errstr = "not authorized";
331 ch_free( authcDN.bv_val );
332 ch_free( authzDN.bv_val );
337 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
338 "slap_sasl_authorize: conn %d authorization allowed\n",
339 (long)(conn ? conn->c_connid : -1 ) ));
341 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
342 " authorization allowed\n",
343 (long) (conn ? conn->c_connid : -1), 0, 0 );
347 ch_free( authcDN.bv_val );
348 *user = authzDN.bv_val;
355 slap_sasl_err2ldap( int saslerr )
361 rc = LDAP_SASL_BIND_IN_PROGRESS;
370 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
373 rc = LDAP_INVALID_CREDENTIALS;
376 rc = LDAP_INSUFFICIENT_ACCESS;
380 rc = LDAP_INAPPROPRIATE_AUTH;
392 int slap_sasl_init( void )
394 #ifdef HAVE_CYRUS_SASL
396 static sasl_callback_t server_callbacks[] = {
397 { SASL_CB_LOG, &slap_sasl_log, NULL },
398 { SASL_CB_LIST_END, NULL, NULL }
408 ldap_pvt_sasl_mutex_new,
409 ldap_pvt_sasl_mutex_lock,
410 ldap_pvt_sasl_mutex_unlock,
411 ldap_pvt_sasl_mutex_dispose );
413 /* should provide callbacks for logging */
414 /* server name should be configurable */
415 rc = sasl_server_init( server_callbacks, "slapd" );
417 if( rc != SASL_OK ) {
419 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
420 "slap_sasl_init: init failed.\n" ));
422 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
430 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
431 "slap_sasl_init: initialized!\n"));
433 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
438 /* default security properties */
439 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
440 sasl_secprops.max_ssf = INT_MAX;
441 sasl_secprops.maxbufsize = 65536;
442 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
448 int slap_sasl_destroy( void )
450 #ifdef HAVE_CYRUS_SASL
459 int slap_sasl_open( Connection *conn )
461 int sc = LDAP_SUCCESS;
463 #ifdef HAVE_CYRUS_SASL
464 sasl_conn_t *ctx = NULL;
465 sasl_callback_t *session_callbacks;
467 assert( conn->c_sasl_context == NULL );
468 assert( conn->c_sasl_extra == NULL );
470 conn->c_sasl_layers = 0;
473 ch_calloc( 3, sizeof(sasl_callback_t));
474 conn->c_sasl_extra = session_callbacks;
476 session_callbacks[0].id = SASL_CB_LOG;
477 session_callbacks[0].proc = &slap_sasl_log;
478 session_callbacks[0].context = conn;
480 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
481 session_callbacks[1].proc = &slap_sasl_authorize;
482 session_callbacks[1].context = conn;
484 session_callbacks[2].id = SASL_CB_LIST_END;
485 session_callbacks[2].proc = NULL;
486 session_callbacks[2].context = NULL;
488 if( global_host == NULL ) {
489 global_host = ldap_pvt_get_fqdn( NULL );
492 /* create new SASL context */
493 sc = sasl_server_new( "ldap", global_host, global_realm,
494 session_callbacks, SASL_SECURITY_LAYER, &ctx );
496 if( sc != SASL_OK ) {
498 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
499 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
501 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
508 conn->c_sasl_context = ctx;
510 if( sc == SASL_OK ) {
511 sc = sasl_setprop( ctx,
512 SASL_SEC_PROPS, &sasl_secprops );
514 if( sc != SASL_OK ) {
516 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
517 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
519 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
523 slap_sasl_close( conn );
528 sc = slap_sasl_err2ldap( sc );
533 int slap_sasl_external(
536 const char *auth_id )
538 #ifdef HAVE_CYRUS_SASL
540 sasl_conn_t *ctx = conn->c_sasl_context;
541 sasl_external_properties_t extprops;
544 return LDAP_UNAVAILABLE;
547 memset( &extprops, '\0', sizeof(extprops) );
549 extprops.auth_id = (char *) auth_id;
551 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
552 (void *) &extprops );
554 if ( sc != SASL_OK ) {
562 int slap_sasl_reset( Connection *conn )
564 #ifdef HAVE_CYRUS_SASL
565 sasl_conn_t *ctx = conn->c_sasl_context;
570 /* must return "anonymous" */
574 char ** slap_sasl_mechs( Connection *conn )
578 #ifdef HAVE_CYRUS_SASL
579 sasl_conn_t *ctx = conn->c_sasl_context;
585 sc = sasl_listmech( ctx,
586 NULL, NULL, ",", NULL,
587 &mechstr, NULL, NULL );
589 if( sc != SASL_OK ) {
591 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
592 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
594 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
601 mechs = str2charray( mechstr, "," );
610 int slap_sasl_close( Connection *conn )
612 #ifdef HAVE_CYRUS_SASL
613 sasl_conn_t *ctx = conn->c_sasl_context;
616 sasl_dispose( &ctx );
619 conn->c_sasl_context = NULL;
621 free( conn->c_sasl_extra );
622 conn->c_sasl_extra = NULL;
639 #ifdef HAVE_CYRUS_SASL
640 sasl_conn_t *ctx = conn->c_sasl_context;
641 struct berval response;
647 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
648 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
650 dn->bv_len ? dn->bv_val : "",
651 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech.bv_val,
652 cred ? cred->bv_len : 0 ));
654 Debug(LDAP_DEBUG_ARGS,
655 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
656 dn->bv_len ? dn->bv_val : "",
657 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech.bv_val,
658 cred ? cred->bv_len : 0 );
663 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
664 NULL, "SASL unavailable on this session", NULL, NULL );
668 if ( !conn->c_sasl_bind_in_progress ) {
669 sc = sasl_server_start( ctx,
670 conn->c_sasl_bind_mech.bv_val,
671 cred->bv_len ? cred->bv_val : "",
673 (char **)&response.bv_val, &reslen, &errstr );
676 sc = sasl_server_step( ctx,
677 cred->bv_val, cred->bv_len,
678 (char **)&response.bv_val, &reslen, &errstr );
681 response.bv_len = reslen;
683 if ( sc == SASL_OK ) {
684 char *username = NULL;
686 sc = sasl_getprop( ctx,
687 SASL_USERNAME, (void **)&username );
689 if ( sc != SASL_OK ) {
691 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
692 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
694 Debug(LDAP_DEBUG_TRACE,
695 "slap_sasl_bind: getprop(USERNAME) failed!\n",
700 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
701 NULL, "no SASL username", NULL, NULL );
704 rc = slap_sasl_getdn( conn, username, edn, FLAG_GETDN_FINAL );
706 if( rc == LDAP_SUCCESS ) {
707 sasl_ssf_t *ssf = NULL;
708 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
709 *ssfp = ssf ? *ssf : 0;
712 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
713 conn->c_sasl_layers++;
714 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
717 send_ldap_sasl( conn, op, rc,
718 NULL, NULL, NULL, NULL,
719 response.bv_len ? &response : NULL );
722 send_ldap_result( conn, op, rc,
723 NULL, errstr, NULL, NULL );
727 } else if ( sc == SASL_CONTINUE ) {
728 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
729 NULL, NULL, NULL, NULL, &response );
732 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
733 NULL, errstr, NULL, NULL );
736 if( response.bv_len ) {
737 ch_free( response.bv_val );
741 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
742 "slap_sasl_bind: rc=%d\n", rc ));
744 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
749 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
750 NULL, "SASL not supported", NULL, NULL );
756 char* slap_sasl_secprops( const char *in )
758 #ifdef HAVE_CYRUS_SASL
759 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
761 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
763 return "SASL not supported";