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;
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;
62 Debug( level, "SASL [conn=%d] %s: %s\n",
63 conn ? conn->c_connid: -1,
79 Connection *conn = context;
83 if ( authcid == NULL || *authcid == '\0' ) {
84 *errstr = "empty authentication identity";
86 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
87 "empty authentication identity\n",
88 (long) (conn ? conn->c_connid : -1),
93 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
94 "authcid=\"%s\" authzid=\"%s\"\n",
95 (long) (conn ? conn->c_connid : -1),
96 authcid ? authcid : "<empty>",
97 authzid ? authzid : "<empty>" );
99 if ( authzid == NULL || *authzid == '\0' ||
100 strcmp( authcid, authzid ) == 0 )
102 size_t len = sizeof("u:") + strlen( authcid );
104 cuser = ch_malloc( len );
105 strcpy( cuser, "u:" );
106 strcpy( &cuser[sizeof("u:")-1], authcid );
110 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
111 "\"%s\" as \"%s\"\n",
112 (long) (conn ? conn->c_connid : -1),
118 rc = slap_sasl_authorized( conn, authcid, authzid );
119 Debug( LDAP_DEBUG_TRACE, "SASL Authorization returned %d\n", rc,0,0);
121 *errstr = "not authorized";
125 cuser = ch_strdup( authzid );
126 dn_normalize( cuser );
134 slap_sasl_err2ldap( int saslerr )
140 rc = LDAP_SASL_BIND_IN_PROGRESS;
149 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
152 rc = LDAP_INVALID_CREDENTIALS;
155 rc = LDAP_INSUFFICIENT_ACCESS;
159 rc = LDAP_INAPPROPRIATE_AUTH;
171 int slap_sasl_init( void )
173 #ifdef HAVE_CYRUS_SASL
175 static sasl_callback_t server_callbacks[] = {
176 { SASL_CB_LOG, &slap_sasl_log, NULL },
177 { SASL_CB_LIST_END, NULL, NULL }
187 ldap_pvt_sasl_mutex_new,
188 ldap_pvt_sasl_mutex_lock,
189 ldap_pvt_sasl_mutex_unlock,
190 ldap_pvt_sasl_mutex_dispose );
192 /* should provide callbacks for logging */
193 /* server name should be configurable */
194 rc = sasl_server_init( server_callbacks, "slapd" );
196 if( rc != SASL_OK ) {
197 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
202 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
205 /* default security properties */
206 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
207 sasl_secprops.max_ssf = INT_MAX;
208 sasl_secprops.maxbufsize = 65536;
209 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
215 int slap_sasl_destroy( void )
217 #ifdef HAVE_CYRUS_SASL
223 int slap_sasl_open( Connection *conn )
225 int sc = LDAP_SUCCESS;
227 #ifdef HAVE_CYRUS_SASL
228 sasl_conn_t *ctx = NULL;
229 sasl_callback_t *session_callbacks;
231 assert( conn->c_sasl_context == NULL );
232 assert( conn->c_sasl_extra == NULL );
234 conn->c_sasl_layers = 0;
237 ch_calloc( 3, sizeof(sasl_callback_t));
238 conn->c_sasl_extra = session_callbacks;
240 session_callbacks[0].id = SASL_CB_LOG;
241 session_callbacks[0].proc = &slap_sasl_log;
242 session_callbacks[0].context = conn;
244 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
245 session_callbacks[1].proc = &slap_sasl_authorize;
246 session_callbacks[1].context = conn;
248 session_callbacks[2].id = SASL_CB_LIST_END;
249 session_callbacks[2].proc = NULL;
250 session_callbacks[2].context = NULL;
252 if( global_host == NULL ) {
253 global_host = ldap_pvt_get_fqdn( NULL );
256 /* create new SASL context */
257 sc = sasl_server_new( "ldap", global_host, global_realm,
258 session_callbacks, SASL_SECURITY_LAYER, &ctx );
260 if( sc != SASL_OK ) {
261 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
266 conn->c_sasl_context = ctx;
268 if( sc == SASL_OK ) {
269 sc = sasl_setprop( ctx,
270 SASL_SEC_PROPS, &sasl_secprops );
272 if( sc != SASL_OK ) {
273 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
275 slap_sasl_close( conn );
280 sc = slap_sasl_err2ldap( sc );
285 int slap_sasl_external(
288 const char *auth_id )
290 #ifdef HAVE_CYRUS_SASL
292 sasl_conn_t *ctx = conn->c_sasl_context;
293 sasl_external_properties_t extprops;
296 return LDAP_UNAVAILABLE;
299 memset( &extprops, '\0', sizeof(extprops) );
301 extprops.auth_id = (char *) auth_id;
303 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
304 (void *) &extprops );
306 if ( sc != SASL_OK ) {
314 int slap_sasl_reset( Connection *conn )
316 #ifdef HAVE_CYRUS_SASL
317 sasl_conn_t *ctx = conn->c_sasl_context;
322 /* must return "anonymous" */
326 char ** slap_sasl_mechs( Connection *conn )
330 #ifdef HAVE_CYRUS_SASL
331 sasl_conn_t *ctx = conn->c_sasl_context;
337 sc = sasl_listmech( ctx,
338 NULL, NULL, ",", NULL,
339 &mechstr, NULL, NULL );
341 if( sc != SASL_OK ) {
342 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
347 mechs = str2charray( mechstr, "," );
356 int slap_sasl_close( Connection *conn )
358 #ifdef HAVE_CYRUS_SASL
359 sasl_conn_t *ctx = conn->c_sasl_context;
362 sasl_dispose( &ctx );
365 conn->c_sasl_context = NULL;
367 free( conn->c_sasl_extra );
368 conn->c_sasl_extra = NULL;
385 #ifdef HAVE_CYRUS_SASL
386 sasl_conn_t *ctx = conn->c_sasl_context;
387 struct berval response;
392 Debug(LDAP_DEBUG_ARGS,
393 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%d\n", dn,
394 conn->c_sasl_bind_in_progress ? "<continuing>":conn->c_sasl_bind_mech,
395 cred ? cred->bv_len : 0 );
398 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
399 NULL, "SASL unavailable on this session", NULL, NULL );
403 if ( !conn->c_sasl_bind_in_progress ) {
404 sc = sasl_server_start( ctx,
405 conn->c_sasl_bind_mech,
406 cred->bv_val, cred->bv_len,
407 (char **)&response.bv_val, &reslen, &errstr );
410 sc = sasl_server_step( ctx,
411 cred->bv_val, cred->bv_len,
412 (char **)&response.bv_val, &reslen, &errstr );
415 response.bv_len = reslen;
417 if ( sc == SASL_OK ) {
418 char *username = NULL;
420 sc = sasl_getprop( ctx,
421 SASL_USERNAME, (void **)&username );
423 if ( sc != SASL_OK ) {
424 Debug(LDAP_DEBUG_TRACE,
425 "slap_sasl_bind: getprop(USERNAME) failed!\n",
428 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
429 NULL, "no SASL username", NULL, NULL );
431 } else if ( username == NULL || *username == '\0' ) {
432 Debug(LDAP_DEBUG_TRACE,
433 "slap_sasl_bind: getprop(USERNAME) returned NULL!\n",
436 send_ldap_result( conn, op, rc = LDAP_INSUFFICIENT_ACCESS,
437 NULL, "no SASL username", NULL, NULL );
441 sasl_ssf_t *ssf = NULL;
443 (void) sasl_getprop( ctx,
444 SASL_REALM, (void **)&realm );
446 (void) sasl_getprop( ctx,
447 SASL_SSF, (void *)&ssf );
449 Debug(LDAP_DEBUG_TRACE,
450 "slap_sasl_bind: username=\"%s\" realm=\"%s\" ssf=%lu\n",
451 username ? username : "",
453 (unsigned long) ( ssf ? *ssf : 0 ) );
455 *ssfp = ssf ? *ssf : 0;
459 if( username == NULL || (
460 !strncasecmp( username, "anonymous", sizeof("anonymous")-1 ) &&
461 ( username[sizeof("anonymous")-1] == '\0' ||
462 username[sizeof("anonymous")-1] == '@' ) ) )
464 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: anonymous\n",
467 } else if ( username[0] == 'u' && username[1] == ':'
468 && username[2] != '\0'
469 && strpbrk( &username[2], "+=,;\"\\ \t") == NULL )
471 *edn = ch_malloc( sizeof( "uid= + realm=" )
472 + strlen( &username[2] )
473 + ( realm ? strlen( realm ) : 0 ) );
475 strcpy( *edn, "uid=" );
476 strcat( *edn, &username[2] );
478 if( realm && *realm ) {
479 strcat( *edn, " + realm=" );
480 strcat( *edn, realm );
483 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: authzdn: \"%s\"\n",
488 if( rc == LDAP_SUCCESS ) {
489 send_ldap_sasl( conn, op, rc,
490 NULL, NULL, NULL, NULL,
491 response.bv_len ? &response : NULL );
494 send_ldap_result( conn, op, rc,
495 NULL, errstr, NULL, NULL );
499 } else if ( sc == SASL_CONTINUE ) {
500 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
501 NULL, NULL, NULL, NULL, &response );
504 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
505 NULL, errstr, NULL, NULL );
508 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
511 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
512 NULL, "SASL not supported", NULL, NULL );
518 char* slap_sasl_secprops( const char *in )
520 #ifdef HAVE_CYRUS_SASL
521 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
523 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
525 return "SASL not supported";