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 *dnptr is in its own allocated memory, and must be free'd
84 by the calling process.
85 -Mark Adamson, Carnegie Mellon
88 int slap_sasl_getdn( Connection *conn, char *id, char **dnptr, int flags )
90 char *c=NULL, *c1, *dn=NULL;
96 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
97 "slap_sasl_getdn: conn %d id=%s\n",
98 conn ? conn->c_connid : -1,
99 id ? (*id ? id : "<empty>") : "NULL" ));
101 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s\n",
102 id?(*id?id:"<empty>"):"NULL",0,0 );
106 /* Blatantly anonymous ID */
108 ( id[sizeof( "anonymous" )-1] == '\0'
109 || id[sizeof( "anonymous" )-1] == '@' ) &&
110 !strncasecmp( id, "anonymous", sizeof( "anonymous" )-1) ) {
112 return( LDAP_SUCCESS );
114 ctx = conn->c_sasl_context;
117 /* An authcID needs to be converted to authzID form */
118 if( flags & FLAG_GETDN_AUTHCID ) {
119 if( sasl_external_x509dn_convert && conn->c_sasl_bind_mech
120 && ( strcasecmp( "EXTERNAL", conn->c_sasl_bind_mech ) == 0 )
121 && len && id[0] == '/' /* && id[len-1]== '/' */)
123 /* check SASL external for X.509 style DN and */
124 /* convert to dn:<dn> form */
125 char *tmpdn = ldap_dcedn2dn( id );
126 len = strlen( tmpdn );
128 dn = ch_malloc( len+4 );
132 AC_MEMCPY( &dn[3], tmpdn, len+1 );
136 /* convert to u:<username> form */
137 dn = ch_malloc( len+3 );
140 AC_MEMCPY( &dn[2], id, len+1 );
144 dn = ch_strdup( id );
147 /* An authzID must be properly prefixed */
148 if( flags & FLAG_GETDN_AUTHZID
149 && strncasecmp( dn, "u:", sizeof("u:")-1 )
150 && strncasecmp( dn, "dn:", sizeof("dn:")-1 ) )
154 return( LDAP_INAPPROPRIATE_AUTH );
157 /* Username strings */
158 if( !strncasecmp( dn, "u:", sizeof("u:")-1 ) ) {
159 len += (sizeof("dn:uid=")-1) + (sizeof(",cn=auth")-1);
161 /* Figure out how much data we have for the dn */
162 rc = sasl_getprop( ctx, SASL_REALM, (void **)&c );
163 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
165 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
166 "slap_sasl_getdn: getprop(REALM) failed.\n" ));
168 Debug(LDAP_DEBUG_TRACE,
169 "getdn: getprop(REALM) failed!\n", 0,0,0);
174 return( LDAP_OPERATIONS_ERROR );
178 len += strlen( c ) + (sizeof(",cn=")-1);
181 if( conn->c_sasl_bind_mech ) {
182 len += strlen( conn->c_sasl_bind_mech ) + (sizeof(",cn=")-1);
185 /* Build the new dn */
187 dn = ch_malloc( len );
188 len = sprintf( dn, "dn:uid=%s", c1+2 );
192 len += sprintf( dn+len, ",cn=%s", c );
194 if( conn->c_sasl_bind_mech ) {
195 len += sprintf( dn+len, ",cn=%s", conn->c_sasl_bind_mech );
197 strcpy( dn+len, ",cn=auth" );
198 len += (sizeof(",cn=auth")-1);
201 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
202 "slap_sasl_getdn: u:id converted to %s.\n", dn ));
204 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn,0,0 );
208 /* DN strings that are a cn=auth identity to run through regexp */
209 if( !strncasecmp( dn, "dn:", sizeof("dn:")-1) &&
210 ( ( flags & FLAG_GETDN_FINAL ) == 0 ) )
212 c1 = slap_sasl2dn( dn + (sizeof("dn:")-1) );
216 /* Reaffix the dn: prefix if it was removed */
217 if( strncasecmp( dn, "dn:", sizeof("dn:")-1) ) {
219 dn = ch_malloc( strlen( c1 ) + sizeof("dn:") );
220 sprintf( dn, "dn:%s", c1 );
225 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
226 "slap_sasl_getdn: dn:id converted to %s.\n", dn ));
228 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
234 if( ( flags & FLAG_GETDN_FINAL ) == 0 ) {
235 dn_normalize( dn+(sizeof("dn:")-1) );
239 return( LDAP_SUCCESS );
252 char *authcDN, *authzDN;
254 Connection *conn = context;
259 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
260 "slap_sas_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
261 conn ? conn->c_connid : -1,
262 authcid ? authcid : "<empty>",
263 authzid ? authzid : "<empty>" ));
265 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
266 "authcid=\"%s\" authzid=\"%s\"\n",
267 (long) (conn ? conn->c_connid : -1),
268 authcid ? authcid : "<empty>",
269 authzid ? authzid : "<empty>" );
273 /* Convert the identities to DN's. If no authzid was given, client will
274 be bound as the DN matching their username */
275 rc = slap_sasl_getdn( conn, (char *)authcid, &authcDN, FLAG_GETDN_AUTHCID );
276 if( rc != LDAP_SUCCESS ) {
277 *errstr = ldap_err2string( rc );
280 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
282 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
283 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
284 conn ? conn->c_connid : -1, authcDN ));
286 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
287 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN,0 );
294 rc = slap_sasl_getdn( conn, (char *)authzid, &authzDN, FLAG_GETDN_AUTHZID );
295 if( rc != LDAP_SUCCESS ) {
297 *errstr = ldap_err2string( rc );
301 rc = slap_sasl_authorized( authcDN, authzDN );
304 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
305 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
306 (long)(conn ? conn->c_connid : -1), rc ));
308 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
309 " authorization disallowed (%d)\n",
310 (long) (conn ? conn->c_connid : -1), rc, 0 );
313 *errstr = "not authorized";
320 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
321 "slap_sasl_authorize: conn %d authorization allowed\n",
322 (long)(conn ? conn->c_connid : -1 ) ));
324 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
325 " authorization allowed\n",
326 (long) (conn ? conn->c_connid : -1), 0, 0 );
338 slap_sasl_err2ldap( int saslerr )
344 rc = LDAP_SASL_BIND_IN_PROGRESS;
353 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
356 rc = LDAP_INVALID_CREDENTIALS;
359 rc = LDAP_INSUFFICIENT_ACCESS;
363 rc = LDAP_INAPPROPRIATE_AUTH;
375 int slap_sasl_init( void )
377 #ifdef HAVE_CYRUS_SASL
379 static sasl_callback_t server_callbacks[] = {
380 { SASL_CB_LOG, &slap_sasl_log, NULL },
381 { SASL_CB_LIST_END, NULL, NULL }
391 ldap_pvt_sasl_mutex_new,
392 ldap_pvt_sasl_mutex_lock,
393 ldap_pvt_sasl_mutex_unlock,
394 ldap_pvt_sasl_mutex_dispose );
396 /* should provide callbacks for logging */
397 /* server name should be configurable */
398 rc = sasl_server_init( server_callbacks, "slapd" );
400 if( rc != SASL_OK ) {
402 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
403 "slap_sasl_init: init failed.\n" ));
405 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
413 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
414 "slap_sasl_init: initialized!\n"));
416 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
421 /* default security properties */
422 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
423 sasl_secprops.max_ssf = INT_MAX;
424 sasl_secprops.maxbufsize = 65536;
425 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
431 int slap_sasl_destroy( void )
433 #ifdef HAVE_CYRUS_SASL
442 int slap_sasl_open( Connection *conn )
444 int sc = LDAP_SUCCESS;
446 #ifdef HAVE_CYRUS_SASL
447 sasl_conn_t *ctx = NULL;
448 sasl_callback_t *session_callbacks;
450 assert( conn->c_sasl_context == NULL );
451 assert( conn->c_sasl_extra == NULL );
453 conn->c_sasl_layers = 0;
456 ch_calloc( 3, sizeof(sasl_callback_t));
457 conn->c_sasl_extra = session_callbacks;
459 session_callbacks[0].id = SASL_CB_LOG;
460 session_callbacks[0].proc = &slap_sasl_log;
461 session_callbacks[0].context = conn;
463 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
464 session_callbacks[1].proc = &slap_sasl_authorize;
465 session_callbacks[1].context = conn;
467 session_callbacks[2].id = SASL_CB_LIST_END;
468 session_callbacks[2].proc = NULL;
469 session_callbacks[2].context = NULL;
471 if( global_host == NULL ) {
472 global_host = ldap_pvt_get_fqdn( NULL );
475 /* create new SASL context */
476 sc = sasl_server_new( "ldap", global_host, global_realm,
477 session_callbacks, SASL_SECURITY_LAYER, &ctx );
479 if( sc != SASL_OK ) {
481 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
482 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
484 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
491 conn->c_sasl_context = ctx;
493 if( sc == SASL_OK ) {
494 sc = sasl_setprop( ctx,
495 SASL_SEC_PROPS, &sasl_secprops );
497 if( sc != SASL_OK ) {
499 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
500 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
502 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
506 slap_sasl_close( conn );
511 sc = slap_sasl_err2ldap( sc );
516 int slap_sasl_external(
519 const char *auth_id )
521 #ifdef HAVE_CYRUS_SASL
523 sasl_conn_t *ctx = conn->c_sasl_context;
524 sasl_external_properties_t extprops;
527 return LDAP_UNAVAILABLE;
530 memset( &extprops, '\0', sizeof(extprops) );
532 extprops.auth_id = (char *) auth_id;
534 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
535 (void *) &extprops );
537 if ( sc != SASL_OK ) {
545 int slap_sasl_reset( Connection *conn )
547 #ifdef HAVE_CYRUS_SASL
548 sasl_conn_t *ctx = conn->c_sasl_context;
553 /* must return "anonymous" */
557 char ** slap_sasl_mechs( Connection *conn )
561 #ifdef HAVE_CYRUS_SASL
562 sasl_conn_t *ctx = conn->c_sasl_context;
568 sc = sasl_listmech( ctx,
569 NULL, NULL, ",", NULL,
570 &mechstr, NULL, NULL );
572 if( sc != SASL_OK ) {
574 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
575 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
577 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
584 mechs = str2charray( mechstr, "," );
593 int slap_sasl_close( Connection *conn )
595 #ifdef HAVE_CYRUS_SASL
596 sasl_conn_t *ctx = conn->c_sasl_context;
599 sasl_dispose( &ctx );
602 conn->c_sasl_context = NULL;
604 free( conn->c_sasl_extra );
605 conn->c_sasl_extra = NULL;
622 #ifdef HAVE_CYRUS_SASL
623 sasl_conn_t *ctx = conn->c_sasl_context;
624 struct berval response;
630 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
631 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
633 dn->bv_len ? dn->bv_val : "",
634 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech,
635 cred ? cred->bv_len : 0 ));
637 Debug(LDAP_DEBUG_ARGS,
638 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
639 dn->bv_len ? dn->bv_val : "",
640 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech,
641 cred ? cred->bv_len : 0 );
646 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
647 NULL, "SASL unavailable on this session", NULL, NULL );
651 if ( !conn->c_sasl_bind_in_progress ) {
652 sc = sasl_server_start( ctx,
653 conn->c_sasl_bind_mech,
654 cred->bv_len ? cred->bv_val : "",
656 (char **)&response.bv_val, &reslen, &errstr );
659 sc = sasl_server_step( ctx,
660 cred->bv_val, cred->bv_len,
661 (char **)&response.bv_val, &reslen, &errstr );
664 response.bv_len = reslen;
666 if ( sc == SASL_OK ) {
667 char *username = NULL;
669 sc = sasl_getprop( ctx,
670 SASL_USERNAME, (void **)&username );
672 if ( sc != SASL_OK ) {
674 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
675 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
677 Debug(LDAP_DEBUG_TRACE,
678 "slap_sasl_bind: getprop(USERNAME) failed!\n",
683 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
684 NULL, "no SASL username", NULL, NULL );
687 rc = slap_sasl_getdn( conn, username, edn, FLAG_GETDN_FINAL );
689 if( rc == LDAP_SUCCESS ) {
691 sasl_ssf_t *ssf = NULL;
692 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
693 *ssfp = ssf ? *ssf : 0;
696 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
697 conn->c_sasl_layers++;
698 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
701 /* Store the authorization DN as a subjectDN */
706 (*edn)[i-3] = (*edn)[i];
707 } while( (*edn)[i] );
710 send_ldap_sasl( conn, op, rc,
711 NULL, NULL, NULL, NULL,
712 response.bv_len ? &response : NULL );
715 send_ldap_result( conn, op, rc,
716 NULL, errstr, NULL, NULL );
720 } else if ( sc == SASL_CONTINUE ) {
721 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
722 NULL, NULL, NULL, NULL, &response );
725 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
726 NULL, errstr, NULL, NULL );
729 if( response.bv_len ) {
730 ch_free( response.bv_val );
734 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
735 "slap_sasl_bind: rc=%d\n", rc ));
737 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
742 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
743 NULL, "SASL not supported", NULL, NULL );
749 char* slap_sasl_secprops( const char *in )
751 #ifdef HAVE_CYRUS_SASL
752 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
754 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
756 return "SASL not supported";