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
29 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=%ld] %s: %s\n",
65 conn ? conn->c_connid : -1,
68 Debug( level, "SASL [conn=%ld] %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=NULL, *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 */
104 ( id[sizeof( "anonymous" )-1] == '\0'
105 || id[sizeof( "anonymous" )-1] == '@' ) &&
106 !strncasecmp( id, "anonymous", sizeof( "anonymous" )-1) ) {
108 return( LDAP_SUCCESS );
110 ctx = conn->c_sasl_context;
113 /* An authcID needs to be converted to authzID form */
114 if( flags & FLAG_GETDN_AUTHCID ) {
115 if( sasl_external_x509dn_convert && conn->c_sasl_bind_mech
116 && ( strcasecmp( "EXTERNAL", conn->c_sasl_bind_mech ) == 0 )
117 && len && id[0] == '/' /* && id[len-1]== '/' */)
119 /* check SASL external for X.509 style DN and */
120 /* convert to dn:<dn> form */
121 char *tmpdn = ldap_dcedn2dn( id );
122 len = strlen( tmpdn );
124 dn = ch_malloc( len+4 );
128 AC_MEMCPY( &dn[3], tmpdn, len+1 );
132 /* convert to u:<username> form */
133 dn = ch_malloc( len+3 );
136 AC_MEMCPY( &dn[2], id, len+1 );
140 dn = ch_strdup( id );
143 /* An authzID must be properly prefixed */
144 if( flags & FLAG_GETDN_AUTHZID
145 && strncasecmp( dn, "u:", sizeof("u:")-1 )
146 && strncasecmp( dn, "dn:", sizeof("dn:")-1 ) )
150 return( LDAP_INAPPROPRIATE_AUTH );
153 /* Username strings */
154 if( !strncasecmp( dn, "u:", sizeof("u:")-1 ) ) {
155 len += (sizeof("dn:uid=")-1) + (sizeof(",cn=auth")-1);
157 /* Figure out how much data we have for the dn */
158 rc = sasl_getprop( ctx, SASL_REALM, (void **)&c );
159 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
161 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
162 "slap_sasl_getdn: getprop(REALM) failed.\n" ));
164 Debug(LDAP_DEBUG_TRACE,
165 "getdn: getprop(REALM) failed!\n", 0,0,0);
170 return( LDAP_OPERATIONS_ERROR );
174 len += strlen( c ) + (sizeof(",cn=")-1);
177 if( conn->c_sasl_bind_mech ) {
178 len += strlen( conn->c_sasl_bind_mech ) + (sizeof(",cn=")-1);
181 /* Build the new dn */
183 dn = ch_malloc( len );
184 len = sprintf( dn, "dn:uid=%s", c1+2 );
188 len += sprintf( dn+len, ",cn=%s", c );
190 if( conn->c_sasl_bind_mech ) {
191 len += sprintf( dn+len, ",cn=%s", conn->c_sasl_bind_mech );
193 strcpy( dn+len, ",cn=auth" );
194 len += (sizeof(",cn=auth")-1);
197 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
198 "slap_sasl_getdn: u:id converted to %s.\n", dn ));
200 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn,0,0 );
204 /* DN strings that are a cn=auth identity to run through regexp */
205 if( !strncasecmp( dn, "dn:", sizeof("dn:")-1) &&
206 ( ( flags & FLAG_GETDN_FINAL ) == 0 ) )
208 c1 = slap_sasl2dn( dn + (sizeof("dn:")-1) );
212 /* Reaffix the dn: prefix if it was removed */
213 if( strncasecmp( dn, "dn:", sizeof("dn:")-1) ) {
215 dn = ch_malloc( strlen( c1 ) + sizeof("dn:") );
216 sprintf( dn, "dn:%s", c1 );
221 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
222 "slap_sasl_getdn: dn:id converted to %s.\n", dn ));
224 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
230 if( ( flags & FLAG_GETDN_FINAL ) == 0 ) {
231 dn_normalize( dn+(sizeof("dn:")-1) );
235 return( LDAP_SUCCESS );
248 char *authcDN, *authzDN;
250 Connection *conn = context;
255 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
256 "slap_sas_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
257 conn ? conn->c_connid : -1,
258 authcid ? authcid : "<empty>",
259 authzid ? authzid : "<empty>" ));
261 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
262 "authcid=\"%s\" authzid=\"%s\"\n",
263 (long) (conn ? conn->c_connid : -1),
264 authcid ? authcid : "<empty>",
265 authzid ? authzid : "<empty>" );
269 /* Convert the identities to DN's. If no authzid was given, client will
270 be bound as the DN matching their username */
271 rc = slap_sasl_getdn( conn, (char *)authcid, &authcDN, FLAG_GETDN_AUTHCID );
272 if( rc != LDAP_SUCCESS ) {
273 *errstr = ldap_err2string( rc );
276 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
278 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
279 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
280 conn ? conn->c_connid : -1, authcDN ));
282 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
283 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN,0 );
290 rc = slap_sasl_getdn( conn, (char *)authzid, &authzDN, FLAG_GETDN_AUTHZID );
291 if( rc != LDAP_SUCCESS ) {
293 *errstr = ldap_err2string( rc );
297 rc = slap_sasl_authorized( authcDN, authzDN );
300 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
301 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
302 (long)(conn ? conn->c_connid : -1), rc ));
304 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
305 " authorization disallowed (%d)\n",
306 (long) (conn ? conn->c_connid : -1), rc, 0 );
309 *errstr = "not authorized";
316 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
317 "slap_sasl_authorize: conn %d authorization allowed\n",
318 (long)(conn ? conn->c_connid : -1 ) ));
320 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
321 " authorization allowed\n",
322 (long) (conn ? conn->c_connid : -1), 0, 0 );
334 slap_sasl_err2ldap( int saslerr )
340 rc = LDAP_SASL_BIND_IN_PROGRESS;
349 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
352 rc = LDAP_INVALID_CREDENTIALS;
355 rc = LDAP_INSUFFICIENT_ACCESS;
359 rc = LDAP_INAPPROPRIATE_AUTH;
371 int slap_sasl_init( void )
373 #ifdef HAVE_CYRUS_SASL
375 static sasl_callback_t server_callbacks[] = {
376 { SASL_CB_LOG, &slap_sasl_log, NULL },
377 { SASL_CB_LIST_END, NULL, NULL }
387 ldap_pvt_sasl_mutex_new,
388 ldap_pvt_sasl_mutex_lock,
389 ldap_pvt_sasl_mutex_unlock,
390 ldap_pvt_sasl_mutex_dispose );
392 /* should provide callbacks for logging */
393 /* server name should be configurable */
394 rc = sasl_server_init( server_callbacks, "slapd" );
396 if( rc != SASL_OK ) {
398 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
399 "slap_sasl_init: init failed.\n" ));
401 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
409 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
410 "slap_sasl_init: initialized!\n"));
412 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
417 /* default security properties */
418 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
419 sasl_secprops.max_ssf = INT_MAX;
420 sasl_secprops.maxbufsize = 65536;
421 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
427 int slap_sasl_destroy( void )
429 #ifdef HAVE_CYRUS_SASL
438 int slap_sasl_open( Connection *conn )
440 int sc = LDAP_SUCCESS;
442 #ifdef HAVE_CYRUS_SASL
443 sasl_conn_t *ctx = NULL;
444 sasl_callback_t *session_callbacks;
446 assert( conn->c_sasl_context == NULL );
447 assert( conn->c_sasl_extra == NULL );
449 conn->c_sasl_layers = 0;
452 ch_calloc( 3, sizeof(sasl_callback_t));
453 conn->c_sasl_extra = session_callbacks;
455 session_callbacks[0].id = SASL_CB_LOG;
456 session_callbacks[0].proc = &slap_sasl_log;
457 session_callbacks[0].context = conn;
459 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
460 session_callbacks[1].proc = &slap_sasl_authorize;
461 session_callbacks[1].context = conn;
463 session_callbacks[2].id = SASL_CB_LIST_END;
464 session_callbacks[2].proc = NULL;
465 session_callbacks[2].context = NULL;
467 if( global_host == NULL ) {
468 global_host = ldap_pvt_get_fqdn( NULL );
471 /* create new SASL context */
472 sc = sasl_server_new( "ldap", global_host, global_realm,
473 session_callbacks, SASL_SECURITY_LAYER, &ctx );
475 if( sc != SASL_OK ) {
477 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
478 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
480 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
487 conn->c_sasl_context = ctx;
489 if( sc == SASL_OK ) {
490 sc = sasl_setprop( ctx,
491 SASL_SEC_PROPS, &sasl_secprops );
493 if( sc != SASL_OK ) {
495 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
496 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
498 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
502 slap_sasl_close( conn );
507 sc = slap_sasl_err2ldap( sc );
512 int slap_sasl_external(
515 const char *auth_id )
517 #ifdef HAVE_CYRUS_SASL
519 sasl_conn_t *ctx = conn->c_sasl_context;
520 sasl_external_properties_t extprops;
523 return LDAP_UNAVAILABLE;
526 memset( &extprops, '\0', sizeof(extprops) );
528 extprops.auth_id = (char *) auth_id;
530 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
531 (void *) &extprops );
533 if ( sc != SASL_OK ) {
541 int slap_sasl_reset( Connection *conn )
543 #ifdef HAVE_CYRUS_SASL
544 sasl_conn_t *ctx = conn->c_sasl_context;
549 /* must return "anonymous" */
553 char ** slap_sasl_mechs( Connection *conn )
557 #ifdef HAVE_CYRUS_SASL
558 sasl_conn_t *ctx = conn->c_sasl_context;
564 sc = sasl_listmech( ctx,
565 NULL, NULL, ",", NULL,
566 &mechstr, NULL, NULL );
568 if( sc != SASL_OK ) {
570 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
571 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
573 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
580 mechs = str2charray( mechstr, "," );
589 int slap_sasl_close( Connection *conn )
591 #ifdef HAVE_CYRUS_SASL
592 sasl_conn_t *ctx = conn->c_sasl_context;
595 sasl_dispose( &ctx );
598 conn->c_sasl_context = NULL;
600 free( conn->c_sasl_extra );
601 conn->c_sasl_extra = NULL;
618 #ifdef HAVE_CYRUS_SASL
619 sasl_conn_t *ctx = conn->c_sasl_context;
620 struct berval response;
626 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
627 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
629 dn->bv_len ? dn->bv_val : "",
630 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech,
631 cred ? cred->bv_len : 0 ));
633 Debug(LDAP_DEBUG_ARGS,
634 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
635 dn->bv_len ? dn->bv_val : "",
636 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech,
637 cred ? cred->bv_len : 0 );
642 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
643 NULL, "SASL unavailable on this session", NULL, NULL );
647 if ( !conn->c_sasl_bind_in_progress ) {
648 sc = sasl_server_start( ctx,
649 conn->c_sasl_bind_mech,
650 cred->bv_len ? cred->bv_val : "",
652 (char **)&response.bv_val, &reslen, &errstr );
655 sc = sasl_server_step( ctx,
656 cred->bv_val, cred->bv_len,
657 (char **)&response.bv_val, &reslen, &errstr );
660 response.bv_len = reslen;
662 if ( sc == SASL_OK ) {
663 char *username = NULL;
665 sc = sasl_getprop( ctx,
666 SASL_USERNAME, (void **)&username );
668 if ( sc != SASL_OK ) {
670 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
671 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
673 Debug(LDAP_DEBUG_TRACE,
674 "slap_sasl_bind: getprop(USERNAME) failed!\n",
679 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
680 NULL, "no SASL username", NULL, NULL );
683 rc = slap_sasl_getdn( conn, username, edn, FLAG_GETDN_FINAL );
685 if( rc == LDAP_SUCCESS ) {
687 sasl_ssf_t *ssf = NULL;
688 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
689 *ssfp = ssf ? *ssf : 0;
692 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
693 conn->c_sasl_layers++;
694 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
697 /* Store the authorization DN as a subjectDN */
702 (*edn)[i-3] = (*edn)[i];
703 } while( (*edn)[i] );
706 send_ldap_sasl( conn, op, rc,
707 NULL, NULL, NULL, NULL,
708 response.bv_len ? &response : NULL );
711 send_ldap_result( conn, op, rc,
712 NULL, errstr, NULL, NULL );
716 } else if ( sc == SASL_CONTINUE ) {
717 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
718 NULL, NULL, NULL, NULL, &response );
721 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
722 NULL, errstr, NULL, NULL );
725 if( response.bv_len ) {
726 ch_free( response.bv_val );
730 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
731 "slap_sasl_bind: rc=%d\n", rc ));
733 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
738 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
739 NULL, "SASL not supported", NULL, NULL );
745 char* slap_sasl_secprops( const char *in )
747 #ifdef HAVE_CYRUS_SASL
748 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
750 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
752 return "SASL not supported";