3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
17 #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=%ld] %s: %s\n",
64 conn ? conn->c_connid : -1,
67 Debug( level, "SASL [conn=%ld] %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 = sizeof( "anonymous" ) - 1;
103 if( id && ( id[len] == '\0' || id[len] == '@' ) &&
104 !strncasecmp( id, "anonymous", 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 && id[0] == '/' /* && id[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) &&
205 ( ( flags & FLAG_GETDN_FINAL ) == 0 ) )
207 c1 = slap_sasl2dn( dn + 3 );
211 /* Reaffix the dn: prefix if it was removed */
212 if( strncasecmp( dn, "dn:", 3) ) {
214 dn = ch_malloc( strlen( c1 ) + 4 );
215 sprintf( dn, "dn:%s", c1 );
220 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
221 "slap_sasl_getdn: dn:id converted to %s.\n", dn ));
223 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
229 if( ( flags & FLAG_GETDN_FINAL ) == 0 ) {
230 dn_normalize( dn+3 );
234 return( LDAP_SUCCESS );
247 char *authcDN, *authzDN;
249 Connection *conn = context;
254 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
255 "slap_sas_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
256 conn ? conn->c_connid : -1,
257 authcid ? authcid : "<empty>",
258 authzid ? authzid : "<empty>" ));
260 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
261 "authcid=\"%s\" authzid=\"%s\"\n",
262 (long) (conn ? conn->c_connid : -1),
263 authcid ? authcid : "<empty>",
264 authzid ? authzid : "<empty>" );
268 /* Convert the identities to DN's. If no authzid was given, client will
269 be bound as the DN matching their username */
270 rc = slap_sasl_getdn( conn, (char *)authcid, &authcDN, FLAG_GETDN_AUTHCID );
271 if( rc != LDAP_SUCCESS ) {
272 *errstr = ldap_err2string( rc );
275 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
277 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
278 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
279 conn ? conn->c_connid : -1, authcDN ));
281 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
282 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN,0 );
289 rc = slap_sasl_getdn( conn, (char *)authzid, &authzDN, FLAG_GETDN_AUTHZID );
290 if( rc != LDAP_SUCCESS ) {
292 *errstr = ldap_err2string( rc );
296 rc = slap_sasl_authorized( authcDN, authzDN );
299 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
300 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
301 (long)(conn ? conn->c_connid : -1), rc ));
303 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
304 " authorization disallowed (%d)\n",
305 (long) (conn ? conn->c_connid : -1), rc, 0 );
308 *errstr = "not authorized";
315 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
316 "slap_sasl_authorize: conn %d authorization allowed\n",
317 (long)(conn ? conn->c_connid : -1 ) ));
319 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
320 " authorization allowed\n",
321 (long) (conn ? conn->c_connid : -1), 0, 0 );
333 slap_sasl_err2ldap( int saslerr )
339 rc = LDAP_SASL_BIND_IN_PROGRESS;
348 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
351 rc = LDAP_INVALID_CREDENTIALS;
354 rc = LDAP_INSUFFICIENT_ACCESS;
358 rc = LDAP_INAPPROPRIATE_AUTH;
370 int slap_sasl_init( void )
372 #ifdef HAVE_CYRUS_SASL
374 static sasl_callback_t server_callbacks[] = {
375 { SASL_CB_LOG, &slap_sasl_log, NULL },
376 { SASL_CB_LIST_END, NULL, NULL }
386 ldap_pvt_sasl_mutex_new,
387 ldap_pvt_sasl_mutex_lock,
388 ldap_pvt_sasl_mutex_unlock,
389 ldap_pvt_sasl_mutex_dispose );
391 /* should provide callbacks for logging */
392 /* server name should be configurable */
393 rc = sasl_server_init( server_callbacks, "slapd" );
395 if( rc != SASL_OK ) {
397 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
398 "slap_sasl_init: init failed.\n" ));
400 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
408 LDAP_LOG(( "sasl", LDAP_LEVEL_INFO,
409 "slap_sasl_init: initialized!\n"));
411 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
416 /* default security properties */
417 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
418 sasl_secprops.max_ssf = INT_MAX;
419 sasl_secprops.maxbufsize = 65536;
420 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
426 int slap_sasl_destroy( void )
428 #ifdef HAVE_CYRUS_SASL
437 int slap_sasl_open( Connection *conn )
439 int sc = LDAP_SUCCESS;
441 #ifdef HAVE_CYRUS_SASL
442 sasl_conn_t *ctx = NULL;
443 sasl_callback_t *session_callbacks;
445 assert( conn->c_sasl_context == NULL );
446 assert( conn->c_sasl_extra == NULL );
448 conn->c_sasl_layers = 0;
451 ch_calloc( 3, sizeof(sasl_callback_t));
452 conn->c_sasl_extra = session_callbacks;
454 session_callbacks[0].id = SASL_CB_LOG;
455 session_callbacks[0].proc = &slap_sasl_log;
456 session_callbacks[0].context = conn;
458 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
459 session_callbacks[1].proc = &slap_sasl_authorize;
460 session_callbacks[1].context = conn;
462 session_callbacks[2].id = SASL_CB_LIST_END;
463 session_callbacks[2].proc = NULL;
464 session_callbacks[2].context = NULL;
466 if( global_host == NULL ) {
467 global_host = ldap_pvt_get_fqdn( NULL );
470 /* create new SASL context */
471 sc = sasl_server_new( "ldap", global_host, global_realm,
472 session_callbacks, SASL_SECURITY_LAYER, &ctx );
474 if( sc != SASL_OK ) {
476 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
477 "slap_sasl_open: sasl_server_new failed: %d\n", sc ));
479 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
486 conn->c_sasl_context = ctx;
488 if( sc == SASL_OK ) {
489 sc = sasl_setprop( ctx,
490 SASL_SEC_PROPS, &sasl_secprops );
492 if( sc != SASL_OK ) {
494 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
495 "slap_sasl_open: sasl_setprop failed: %d \n", sc ));
497 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
501 slap_sasl_close( conn );
506 sc = slap_sasl_err2ldap( sc );
511 int slap_sasl_external(
514 const char *auth_id )
516 #ifdef HAVE_CYRUS_SASL
518 sasl_conn_t *ctx = conn->c_sasl_context;
519 sasl_external_properties_t extprops;
522 return LDAP_UNAVAILABLE;
525 memset( &extprops, '\0', sizeof(extprops) );
527 extprops.auth_id = (char *) auth_id;
529 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
530 (void *) &extprops );
532 if ( sc != SASL_OK ) {
540 int slap_sasl_reset( Connection *conn )
542 #ifdef HAVE_CYRUS_SASL
543 sasl_conn_t *ctx = conn->c_sasl_context;
548 /* must return "anonymous" */
552 char ** slap_sasl_mechs( Connection *conn )
556 #ifdef HAVE_CYRUS_SASL
557 sasl_conn_t *ctx = conn->c_sasl_context;
563 sc = sasl_listmech( ctx,
564 NULL, NULL, ",", NULL,
565 &mechstr, NULL, NULL );
567 if( sc != SASL_OK ) {
569 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
570 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc ));
572 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
579 mechs = str2charray( mechstr, "," );
588 int slap_sasl_close( Connection *conn )
590 #ifdef HAVE_CYRUS_SASL
591 sasl_conn_t *ctx = conn->c_sasl_context;
594 sasl_dispose( &ctx );
597 conn->c_sasl_context = NULL;
599 free( conn->c_sasl_extra );
600 conn->c_sasl_extra = NULL;
617 #ifdef HAVE_CYRUS_SASL
618 sasl_conn_t *ctx = conn->c_sasl_context;
619 struct berval response;
625 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
626 "sasl_bind: conn %ld dn=\"%s\" mech=%s datalen=%ld\n",
628 dn->bv_len ? dn->bv_val : "",
629 conn->c_sasl_bind_in_progress ? "<continuing>" : conn->c_sasl_bind_mech,
630 cred ? cred->bv_len : 0 ));
632 Debug(LDAP_DEBUG_ARGS,
633 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
634 dn->bv_len ? dn->bv_val : "",
635 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech,
636 cred ? cred->bv_len : 0 );
641 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
642 NULL, "SASL unavailable on this session", NULL, NULL );
646 if ( !conn->c_sasl_bind_in_progress ) {
647 sc = sasl_server_start( ctx,
648 conn->c_sasl_bind_mech,
649 cred->bv_len ? cred->bv_val : "",
651 (char **)&response.bv_val, &reslen, &errstr );
654 sc = sasl_server_step( ctx,
655 cred->bv_val, cred->bv_len,
656 (char **)&response.bv_val, &reslen, &errstr );
659 response.bv_len = reslen;
661 if ( sc == SASL_OK ) {
662 char *username = NULL;
664 sc = sasl_getprop( ctx,
665 SASL_USERNAME, (void **)&username );
667 if ( sc != SASL_OK ) {
669 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
670 "slap_sasl_bind: getprop(USERNAME) failed: %d\n", sc ));
672 Debug(LDAP_DEBUG_TRACE,
673 "slap_sasl_bind: getprop(USERNAME) failed!\n",
678 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
679 NULL, "no SASL username", NULL, NULL );
682 rc = slap_sasl_getdn( conn, username, edn, FLAG_GETDN_FINAL );
684 if( rc == LDAP_SUCCESS ) {
686 sasl_ssf_t *ssf = NULL;
687 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
688 *ssfp = ssf ? *ssf : 0;
691 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
692 conn->c_sasl_layers++;
693 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
696 /* Store the authorization DN as a subjectDN */
701 (*edn)[i-3] = (*edn)[i];
702 } while( (*edn)[i] );
705 send_ldap_sasl( conn, op, rc,
706 NULL, NULL, NULL, NULL,
707 response.bv_len ? &response : NULL );
710 send_ldap_result( conn, op, rc,
711 NULL, errstr, NULL, NULL );
715 } else if ( sc == SASL_CONTINUE ) {
716 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
717 NULL, NULL, NULL, NULL, &response );
720 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
721 NULL, errstr, NULL, NULL );
724 if( response.bv_len ) {
725 ch_free( response.bv_val );
729 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
730 "slap_sasl_bind: rc=%d\n", rc ));
732 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
737 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
738 NULL, "SASL not supported", NULL, NULL );
744 char* slap_sasl_secprops( const char *in )
746 #ifdef HAVE_CYRUS_SASL
747 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
749 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
751 return "SASL not supported";