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 char *sasl_host = NULL;
29 static sasl_security_properties_t sasl_secprops;
38 Connection *conn = context;
42 if ( message == NULL ) {
48 level = LDAP_DEBUG_ANY;
51 case SASL_LOG_WARNING:
52 level = LDAP_DEBUG_TRACE;
56 level = LDAP_DEBUG_TRACE;
63 Debug( level, "SASL [conn=%d] %s: %s\n",
64 conn ? conn->c_connid: -1,
78 Connection *conn = context;
82 if ( authcid == NULL || *authcid == '\0' ) {
83 *errstr = "empty authentication identity";
85 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
86 "empty authentication identity\n",
87 (long) (conn ? conn->c_connid : -1),
92 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
93 "authcid=\"%s\" authzid=\"%s\"\n",
94 (long) (conn ? conn->c_connid : -1),
95 authcid ? authcid : "<empty>",
96 authcid ? authcid : "<empty>" );
98 if ( authzid == NULL || *authzid == '\0' ||
99 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 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
119 "\"%s\" as \"%s\" disallowed. No policy.\n",
120 (long) (conn ? conn->c_connid : -1),
123 *errstr = "no proxy policy";
129 slap_sasl_err2ldap( int saslerr )
135 rc = LDAP_SASL_BIND_IN_PROGRESS;
144 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
147 rc = LDAP_INVALID_CREDENTIALS;
150 rc = LDAP_INSUFFICIENT_ACCESS;
154 rc = LDAP_INAPPROPRIATE_AUTH;
166 int slap_sasl_init( void )
168 #ifdef HAVE_CYRUS_SASL
170 sasl_conn_t *server = NULL;
171 static sasl_callback_t server_callbacks[] = {
172 { SASL_CB_LOG, &slap_sasl_log, NULL },
173 { SASL_CB_LIST_END, NULL, NULL }
183 ldap_pvt_sasl_mutex_new,
184 ldap_pvt_sasl_mutex_lock,
185 ldap_pvt_sasl_mutex_unlock,
186 ldap_pvt_sasl_mutex_dispose );
188 /* should provide callbacks for logging */
189 /* server name should be configurable */
190 rc = sasl_server_init( server_callbacks, "slapd" );
192 if( rc != SASL_OK ) {
193 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
198 if( sasl_host == NULL ) {
199 sasl_host = ldap_pvt_get_fqdn( NULL );
202 Debug( LDAP_DEBUG_TRACE,
203 "slap_sasl_init: %s initialized!\n",
206 /* default security properties */
207 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
208 sasl_secprops.max_ssf = INT_MAX;
209 sasl_secprops.maxbufsize = 65536;
210 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
213 lutil_passwd_sasl_conn = server;
215 sasl_dispose( &server );
222 int slap_sasl_destroy( void )
224 #ifdef HAVE_CYRUS_SASL
226 sasl_dispose( &lutil_passwd_sasl_conn );
233 int slap_sasl_open( Connection *conn )
235 int sc = LDAP_SUCCESS;
237 #ifdef HAVE_CYRUS_SASL
238 sasl_conn_t *ctx = NULL;
239 sasl_callback_t *session_callbacks;
241 assert( conn->c_sasl_context == NULL );
242 assert( conn->c_sasl_extra == NULL );
244 conn->c_sasl_layers = 0;
247 ch_calloc( 3, sizeof(sasl_callback_t));
248 conn->c_sasl_extra = session_callbacks;
250 session_callbacks[0].id = SASL_CB_LOG;
251 session_callbacks[0].proc = &slap_sasl_log;
252 session_callbacks[0].context = conn;
254 session_callbacks[1].id = SASL_CB_PROXY_POLICY;
255 session_callbacks[1].proc = &slap_sasl_authorize;
256 session_callbacks[1].context = conn;
258 session_callbacks[2].id = SASL_CB_LIST_END;
259 session_callbacks[2].proc = NULL;
260 session_callbacks[2].context = NULL;
262 /* create new SASL context */
263 sc = sasl_server_new( "ldap", sasl_host, global_realm,
264 session_callbacks, SASL_SECURITY_LAYER, &ctx );
266 if( sc != SASL_OK ) {
267 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
272 conn->c_sasl_context = ctx;
274 if( sc == SASL_OK ) {
275 sc = sasl_setprop( ctx,
276 SASL_SEC_PROPS, &sasl_secprops );
278 if( sc != SASL_OK ) {
279 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
281 slap_sasl_close( conn );
286 sc = slap_sasl_err2ldap( sc );
291 int slap_sasl_external(
296 #ifdef HAVE_CYRUS_SASL
298 sasl_conn_t *ctx = conn->c_sasl_context;
299 sasl_external_properties_t extprops;
302 return LDAP_UNAVAILABLE;
305 memset( &extprops, 0L, sizeof(extprops) );
307 extprops.auth_id = auth_id;
309 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
310 (void *) &extprops );
312 if ( sc != SASL_OK ) {
320 int slap_sasl_reset( Connection *conn )
322 #ifdef HAVE_CYRUS_SASL
323 sasl_conn_t *ctx = conn->c_sasl_context;
328 /* must return "anonymous" */
332 char ** slap_sasl_mechs( Connection *conn )
336 #ifdef HAVE_CYRUS_SASL
337 sasl_conn_t *ctx = conn->c_sasl_context;
343 sc = sasl_listmech( ctx,
344 NULL, NULL, ",", NULL,
345 &mechstr, NULL, NULL );
347 if( sc != SASL_OK ) {
348 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
353 mechs = str2charray( mechstr, "," );
362 int slap_sasl_close( Connection *conn )
364 #ifdef HAVE_CYRUS_SASL
365 sasl_conn_t *ctx = conn->c_sasl_context;
368 sasl_dispose( &ctx );
371 conn->c_sasl_context = NULL;
373 free( conn->c_sasl_extra );
374 conn->c_sasl_extra = NULL;
388 unsigned long *ssfp )
392 #ifdef HAVE_CYRUS_SASL
393 sasl_conn_t *ctx = conn->c_sasl_context;
394 struct berval response;
399 Debug(LDAP_DEBUG_ARGS,
400 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%d\n",
401 dn, mech ? mech : "<continuing>", cred ? cred->bv_len : 0 );
404 send_ldap_result( conn, op, LDAP_UNAVAILABLE,
405 NULL, "SASL unavailable on this session", NULL, NULL );
409 if ( mech != NULL ) {
410 sc = sasl_server_start( ctx,
412 cred->bv_val, cred->bv_len,
413 (char **)&response.bv_val, &reslen, &errstr );
416 sc = sasl_server_step( ctx,
417 cred->bv_val, cred->bv_len,
418 (char **)&response.bv_val, &reslen, &errstr );
421 response.bv_len = reslen;
423 if ( sc == SASL_OK ) {
424 char *username = NULL;
426 sc = sasl_getprop( ctx,
427 SASL_USERNAME, (void **)&username );
429 if ( sc != SASL_OK ) {
430 Debug(LDAP_DEBUG_TRACE,
431 "slap_sasl_bind: getprop(USERNAME) failed!\n",
434 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
435 NULL, "no SASL username", NULL, NULL );
437 } else if ( username == NULL || *username == '\0' ) {
438 Debug(LDAP_DEBUG_TRACE,
439 "slap_sasl_bind: getprop(USERNAME) returned NULL!\n",
442 send_ldap_result( conn, op, rc = LDAP_INSUFFICIENT_ACCESS,
443 NULL, "no SASL username", NULL, NULL );
447 sasl_ssf_t *ssf = NULL;
449 (void) sasl_getprop( ctx,
450 SASL_REALM, (void **)&realm );
452 (void) sasl_getprop( ctx,
453 SASL_SSF, (void *)&ssf );
455 Debug(LDAP_DEBUG_TRACE,
456 "slap_sasl_bind: username=\"%s\" realm=\"%s\" ssf=%lu\n",
457 username ? username : "",
459 (unsigned long) ( ssf ? *ssf : 0 ) );
461 *ssfp = ssf ? *ssf : 0;
465 if( username == NULL || (
466 !strncasecmp( username, "anonymous", sizeof("anonymous")-1 ) &&
467 ( username[sizeof("anonymous")-1] == '\0' ||
468 username[sizeof("anonymous")-1] == '@' ) ) )
470 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: anonymous\n",
473 } else if ( username[0] == 'u' && username[1] == ':'
474 && username[2] != '\0'
475 && strpbrk( &username[2], "+=,;\"\\ \t") == NULL )
477 *edn = ch_malloc( sizeof( "uid= + realm=" )
478 + strlen( &username[2] )
479 + ( realm ? strlen( realm ) : 0 ) );
481 strcpy( *edn, "uid=" );
482 strcat( *edn, &username[2] );
484 if( realm && *realm ) {
485 strcat( *edn, " + realm=" );
486 strcat( *edn, realm );
489 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: authzdn: \"%s\"\n",
493 rc = LDAP_INAPPROPRIATE_AUTH;
494 errstr = "authorization disallowed";
495 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: %s\n",
499 if( rc == LDAP_SUCCESS ) {
500 send_ldap_sasl( conn, op, rc,
501 NULL, NULL, NULL, NULL,
502 response.bv_len ? &response : NULL );
505 send_ldap_result( conn, op, rc,
506 NULL, errstr, NULL, NULL );
510 } else if ( sc == SASL_CONTINUE ) {
511 send_ldap_sasl( conn, op, rc = LDAP_SASL_BIND_IN_PROGRESS,
512 NULL, NULL, NULL, NULL, &response );
515 send_ldap_result( conn, op, rc = slap_sasl_err2ldap( sc ),
516 NULL, errstr, NULL, NULL );
519 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rc, 0, 0);
522 send_ldap_result( conn, op, rc = LDAP_UNAVAILABLE,
523 NULL, "SASL not supported", NULL, NULL );
529 char* slap_sasl_secprops( const char *in )
531 #ifdef HAVE_CYRUS_SASL
532 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
534 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
536 return "SASL not supported";