2 * Copyright (c) 2000, Mark Adamson, Carnegie Mellon. All rights reserved.
3 * This software is not subject to any license of Carnegie Mellon University.
5 * Redistribution and use in source and binary forms are permitted without
6 * restriction or fee of any kind as long as this notice is preserved.
8 * The name "Carnegie Mellon" must not be used to endorse or promote
9 * products derived from this software without prior written permission.
15 #include <ac/stdlib.h>
18 #include <ac/string.h>
22 #ifdef HAVE_CYRUS_SASL
28 /* URI format: ldap://<host>/<base>[?[<attrs>][?[<scope>][?[<filter>]]]] */
30 int slap_parseURI( char *uri, struct berval *searchbase, int *scope, Filter **filter )
33 struct berval bv, *nbase = NULL;
37 assert( uri != NULL );
38 searchbase->bv_val = NULL;
39 searchbase->bv_len = 0;
44 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
45 "slap_parseURI: parsing %s\n", uri ));
47 Debug( LDAP_DEBUG_TRACE, "slap_parseURI: parsing %s\n", uri, 0, 0 );
51 /* If it does not look like a URI, assume it is a DN */
52 if( !strncasecmp( uri, "dn:", 3 ) ) {
54 uri += strspn( uri, " " );
56 /* FIXME: if dnNormalize actually uses input bv_len we
57 * will have to make this right.
60 rc = dnNormalize( NULL, &bv, &nbase );
61 if (rc == LDAP_SUCCESS) {
62 *scope = LDAP_SCOPE_BASE;
68 if( strncasecmp( uri, "ldap://", 7 ) ) {
73 end = strchr( uri + 7, '/' );
75 return( LDAP_PROTOCOL_ERROR );
77 /* could check the hostname here */
79 /* Grab the searchbase */
81 end = strchr( start, '?' );
85 rc = dnNormalize( NULL, &bv, &nbase );
86 if (rc == LDAP_SUCCESS) {
93 bv.bv_len = end - start;
94 rc = dnNormalize( NULL, &bv, &nbase );
96 if (rc != LDAP_SUCCESS)
103 end = strchr( start, '?' );
105 return( LDAP_SUCCESS );
110 if( !strncasecmp( start, "base?", 5 )) {
111 *scope = LDAP_SCOPE_BASE;
114 else if( !strncasecmp( start, "one?", 4 )) {
115 *scope = LDAP_SCOPE_ONELEVEL;
118 else if( !strncasecmp( start, "sub?", 3 )) {
119 *scope = LDAP_SCOPE_SUBTREE;
123 free( searchbase->bv_val );
124 searchbase->bv_val = NULL;
125 return( LDAP_PROTOCOL_ERROR );
128 /* Grab the filter */
129 *filter = str2filter( start );
131 return( LDAP_SUCCESS );
138 int slap_sasl_regexp_config( const char *match, const char *replace )
140 #ifdef HAVE_CYRUS_SASL
145 SaslRegexp = (SaslRegexp_t *) ch_realloc( (char *) SaslRegexp,
146 (nSaslRegexp + 1) * sizeof(SaslRegexp_t) );
147 reg = &( SaslRegexp[nSaslRegexp] );
148 reg->match = ch_strdup( match );
149 reg->replace = ch_strdup( replace );
150 dn_normalize( reg->match );
151 dn_normalize( reg->replace );
153 /* Precompile matching pattern */
154 rc = regcomp( ®->workspace, reg->match, REG_EXTENDED|REG_ICASE );
157 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
158 "slap_sasl_regexp_config: \"%s\" could not be compiled.\n",
161 Debug( LDAP_DEBUG_ANY,
162 "SASL match pattern %s could not be compiled by regexp engine\n",
166 return( LDAP_OPERATIONS_ERROR );
169 /* Precompile replace pattern. Find the $<n> placeholders */
172 for ( c = reg->replace; *c; c++ ) {
178 if ( n == SASLREGEX_REPLACE ) {
180 LDAP_LOG(( "sasl", LDAP_LEVEL_ERR,
181 "slap_sasl_regexp_config: \"%s\" has too many $n placeholders (max %d)\n",
182 reg->replace, SASLREGEX_REPLACE ));
184 Debug( LDAP_DEBUG_ANY,
185 "SASL replace pattern %s has too many $n placeholders (max %d)\n",
186 reg->replace, SASLREGEX_REPLACE, 0 );
189 return( LDAP_OPERATIONS_ERROR );
191 reg->offset[n] = c - reg->replace;
196 /* Final placeholder, after the last $n */
197 reg->offset[n] = c - reg->replace;
203 return( LDAP_SUCCESS );
210 #ifdef HAVE_CYRUS_SASL
214 /* Take the passed in SASL name and attempt to convert it into an
215 LDAP URI to find the matching LDAP entry, using the pattern matching
216 strings given in the saslregexp config file directive(s) */
218 char *slap_sasl_regexp( char *saslname )
221 int i, n, len, insert;
226 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
227 "slap_sasl_regexp: converting SASL name %s\n", saslname ));
229 Debug( LDAP_DEBUG_TRACE, "slap_sasl_regexp: converting SASL name %s\n",
233 if (( saslname == NULL ) || ( nSaslRegexp == 0 ))
236 /* Match the normalized SASL name to the saslregexp patterns */
237 for( reg = SaslRegexp,i=0; i<nSaslRegexp; i++,reg++ ) {
238 if ( regexec( ®->workspace, saslname, SASLREGEX_REPLACE,
239 reg->strings, 0) == 0 )
243 if( i >= nSaslRegexp )
247 * The match pattern may have been of the form "a(b.*)c(d.*)e" and the
248 * replace pattern of the form "x$1y$2z". The returned string needs
249 * to replace the $1,$2 with the strings that matched (b.*) and (d.*)
253 /* Get the total length of the final URI */
257 while( reg->offset[n] >= 0 ) {
258 /* Len of next section from replacement string (x,y,z above) */
259 len += reg->offset[n] - reg->offset[n-1] - 2;
260 if( reg->offset[n+1] < 0)
263 /* Len of string from saslname that matched next $i (b,d above) */
264 i = reg->replace[ reg->offset[n] + 1 ] - '0';
265 len += reg->strings[i].rm_eo - reg->strings[i].rm_so;
268 uri = ch_malloc( len + 1 );
270 /* Fill in URI with replace string, replacing $i as we go */
273 while( reg->offset[n] >= 0) {
274 /* Paste in next section from replacement string (x,y,z above) */
275 len = reg->offset[n] - reg->offset[n-1] - 2;
276 strncpy( uri+insert, reg->replace + reg->offset[n-1] + 2, len);
278 if( reg->offset[n+1] < 0)
281 /* Paste in string from saslname that matched next $i (b,d above) */
282 i = reg->replace[ reg->offset[n] + 1 ] - '0';
283 len = reg->strings[i].rm_eo - reg->strings[i].rm_so;
284 strncpy( uri+insert, saslname + reg->strings[i].rm_so, len );
292 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
293 "slap_sasl_regexp: converted SASL name to %s\n", uri ));
295 Debug( LDAP_DEBUG_TRACE,
296 "slap_sasl_regexp: converted SASL name to %s\n", uri, 0, 0 );
307 * Given a SASL name (e.g. "UID=name,cn=REALM,cn=MECH,cn=AUTH")
308 * return the LDAP DN to which it matches. The SASL regexp rules in the config
309 * file turn the SASL name into an LDAP URI. If the URI is just a DN (or a
310 * search with scope=base), just return the URI (or its searchbase). Otherwise
311 * an internal search must be done, and if that search returns exactly one
312 * entry, return the DN of that one entry.
315 char *slap_sasl2dn( char *saslname )
317 char *uri=NULL, *DN=NULL;
318 struct berval searchbase = {0, NULL};
322 Connection *conn=NULL;
324 LDAPMessage *res=NULL, *msg;
328 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
329 "slap_sasl2dn: converting SASL name %s to DN.\n", saslname ));
331 Debug( LDAP_DEBUG_TRACE,
332 "==>slap_sasl2dn: Converting SASL name %s to a DN\n", saslname, 0,0 );
336 /* Convert the SASL name into an LDAP URI */
337 uri = slap_sasl_regexp( saslname );
341 rc = slap_parseURI( uri, &searchbase, &scope, &filter );
345 /* Massive shortcut: search scope == base */
346 if( scope == LDAP_SCOPE_BASE ) {
347 DN = searchbase.bv_val;
348 searchbase.bv_len = 0;
349 searchbase.bv_val = NULL;
353 /* Must do an internal search */
356 LDAP_LOG(( "sasl", LDAP_LEVEL_DETAIL1,
357 "slap_sasl2dn: performing internal search (base=%s, scope=%d)\n",
358 searchbase.bv_val, scope ));
360 Debug( LDAP_DEBUG_TRACE,
361 "slap_sasl2dn: performing internal search (base=%s, scope=%d)\n",
362 searchbase.bv_val, scope, 0 );
366 be = select_backend( &searchbase, 0, 1 );
367 if(( be == NULL ) || ( be->be_search == NULL))
369 suffix_alias( be, &searchbase );
371 rc = connection_internal_open( &conn, &client, saslname );
372 if( rc != LDAP_SUCCESS )
375 (*be->be_search)( be, conn, conn->c_ops, /*base*/NULL, &searchbase,
376 scope, /*deref=*/1, /*sizelimit=*/1, /*time=*/0, filter, /*fstr=*/NULL,
377 /*attrs=*/NULL, /*attrsonly=*/0 );
380 /* Read the client side of the internal search */
381 rc = ldap_result( client, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
385 /* Make sure exactly one entry was returned */
386 rc = ldap_count_entries( client, res );
388 LDAP_LOG(( "sasl", LDAP_LEVEL_DETAIL1,
389 "slap_sasl2dn: search DN returned %d entries\n", rc ));
391 Debug( LDAP_DEBUG_TRACE,
392 "slap_sasl2dn: search DN returned %d entries\n", rc,0,0 );
398 msg = ldap_first_entry( client, res );
399 DN = ldap_get_dn( client, msg );
400 if( DN ) dn_normalize( DN );
403 if( searchbase.bv_len ) ch_free( searchbase.bv_val );
404 if( filter ) filter_free( filter );
405 if( uri ) ch_free( uri );
406 if( conn ) connection_internal_close( conn );
407 if( res ) ldap_msgfree( res );
408 if( client ) ldap_unbind( client );
410 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
411 "slap_sasl2dn: Converted SASL name to %s\n", DN ? DN : "<nothing>" ));
413 Debug( LDAP_DEBUG_TRACE, "<==slap_sasl2dn: Converted SASL name to %s\n",
414 DN ? DN : "<nothing>", 0, 0 );
425 * Map a SASL regexp rule to a DN. If the rule is just a DN or a scope=base
426 * URI, just strcmp the rule (or its searchbase) to the *assertDN. Otherwise,
427 * the rule must be used as an internal search for entries. If that search
428 * returns the *assertDN entry, the match is successful.
430 * The assertDN should not have the dn: prefix
434 int slap_sasl_match( char *rule, char *assertDN, char *authc )
437 struct berval searchbase = {0, NULL};
441 Connection *conn=NULL;
443 LDAPMessage *res=NULL, *msg;
448 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
449 "slap_sasl_match: comparing DN %s to rule %s\n", assertDN, rule ));
451 Debug( LDAP_DEBUG_TRACE,
452 "===>slap_sasl_match: comparing DN %s to rule %s\n", assertDN, rule, 0 );
456 rc = slap_parseURI( rule, &searchbase, &scope, &filter );
457 if( rc != LDAP_SUCCESS )
460 /* Massive shortcut: search scope == base */
461 if( scope == LDAP_SCOPE_BASE ) {
462 rc = regcomp(®, searchbase.bv_val, REG_EXTENDED|REG_ICASE|REG_NOSUB);
464 rc = regexec(®, assertDN, 0, NULL, 0);
470 rc = LDAP_INAPPROPRIATE_AUTH;
474 /* Must run an internal search. */
477 LDAP_LOG(( "sasl", LDAP_LEVEL_DETAIL1,
478 "slap_sasl_match: performing internal search (base=%s, scope=%d)\n",
479 searchbase.bv_val, scope ));
481 Debug( LDAP_DEBUG_TRACE,
482 "slap_sasl_match: performing internal search (base=%s, scope=%d)\n",
483 searchbase.bv_val, scope, 0 );
487 be = select_backend( &searchbase, 0, 1 );
488 if(( be == NULL ) || ( be->be_search == NULL)) {
489 rc = LDAP_INAPPROPRIATE_AUTH;
492 suffix_alias( be, &searchbase );
494 /* Make an internal connection on which to run the search */
495 rc = connection_internal_open( &conn, &client, authc );
496 if( rc != LDAP_SUCCESS )
499 (*be->be_search)( be, conn, conn->c_ops, /*base=*/NULL, &searchbase,
500 scope, /*deref=*/1, /*sizelimit=*/0, /*time=*/0, filter, /*fstr=*/NULL,
501 /*attrs=*/NULL, /*attrsonly=*/0 );
504 /* On the client side of the internal search, read the results. Check
505 if the assertDN matches any of the DN's returned by the search */
506 rc = ldap_result( client, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
510 for( msg=ldap_first_entry( client, res );
512 msg=ldap_next_entry( client, msg ) ) {
513 dn = ldap_get_dn( client, msg );
515 rc = strcmp( dn, assertDN );
522 rc = LDAP_INAPPROPRIATE_AUTH;
525 if( searchbase.bv_len ) ch_free( searchbase.bv_val );
526 if( filter ) filter_free( filter );
527 if( conn ) connection_internal_close( conn );
528 if( res ) ldap_msgfree( res );
529 if( client ) ldap_unbind( client );
531 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
532 "slap_sasl_match: comparison returned %d\n", rc ));
534 Debug( LDAP_DEBUG_TRACE,
535 "<===slap_sasl_match: comparison returned %d\n", rc, 0, 0);
546 * This function answers the question, "Can this ID authorize to that ID?",
547 * based on authorization rules. The rules are stored in the *searchDN, in the
548 * attribute named by *attr. If any of those rules map to the *assertDN, the
549 * authorization is approved.
551 * DN's passed in should have a dn: prefix
555 slap_sasl_check_authz(char *searchDN, char *assertDN, char *attr, char *authc)
559 struct berval **vals=NULL;
560 AttributeDescription *ad=NULL;
565 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
566 "slap_sasl_check_authz: does %s match %s rule in %s?\n",
567 assertDN, attr, searchDN ));
569 Debug( LDAP_DEBUG_TRACE,
570 "==>slap_sasl_check_authz: does %s match %s rule in %s?\n",
571 assertDN, attr, searchDN);
574 rc = slap_str2ad( attr, &ad, &errmsg );
575 if( rc != LDAP_SUCCESS )
578 bv.bv_val = searchDN+3;
579 bv.bv_len = strlen(bv.bv_val);
580 rc = backend_attribute( NULL, NULL, NULL, NULL, &bv, ad, &vals );
581 if( rc != LDAP_SUCCESS )
584 /* Check if the *assertDN matches any **vals */
585 for( i=0; vals[i] != NULL; i++ ) {
586 rc = slap_sasl_match( vals[i]->bv_val, assertDN+3, authc );
587 if ( rc == LDAP_SUCCESS )
590 rc = LDAP_INAPPROPRIATE_AUTH;
593 if( vals ) ber_bvecfree( vals );
596 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
597 "slap_sasl_check_authz: %s check returning %s\n", attr, rc ));
599 Debug( LDAP_DEBUG_TRACE,
600 "<==slap_sasl_check_authz: %s check returning %d\n", attr, rc, 0);
608 #endif /* HAVE_CYRUS_SASL */
614 /* Check if a bind can SASL authorize to another identity.
615 Accepts authorization DN's with "dn:" prefix */
617 int slap_sasl_authorized( char *authcDN, char *authzDN )
619 int rc = LDAP_INAPPROPRIATE_AUTH;
621 #ifdef HAVE_CYRUS_SASL
622 /* User binding as anonymous */
623 if ( authzDN == NULL ) {
629 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
630 "slap_sasl_authorized: can %s become %s?\n", authcDN, authzDN ));
632 Debug( LDAP_DEBUG_TRACE,
633 "==>slap_sasl_authorized: can %s become %s?\n", authcDN, authzDN, 0 );
637 /* If person is authorizing to self, succeed */
638 if ( !strcmp( authcDN, authzDN ) ) {
643 /* Check source rules */
644 rc = slap_sasl_check_authz( authcDN, authzDN, SASL_AUTHZ_SOURCE_ATTR,
646 if( rc == LDAP_SUCCESS ) {
650 /* Check destination rules */
651 rc = slap_sasl_check_authz( authzDN, authcDN, SASL_AUTHZ_DEST_ATTR,
653 if( rc == LDAP_SUCCESS ) {
657 rc = LDAP_INAPPROPRIATE_AUTH;
663 LDAP_LOG(( "sasl", LDAP_LEVEL_ENTRY,
664 "slap_sasl_authorized: return %d\n", rc ));
666 Debug( LDAP_DEBUG_TRACE, "<== slap_sasl_authorized: return %d\n",rc,0,0 );