2 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 2000, Mark Adamson, Carnegie Mellon. All rights reserved.
7 * This software is not subject to any license of Carnegie Mellon University.
9 * Redistribution and use in source and binary forms are permitted without
10 * restriction or fee of any kind as long as this notice is preserved.
12 * The name "Carnegie Mellon" must not be used to endorse or promote
13 * products derived from this software without prior written permission.
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
30 #define SASLREGEX_REPLACE 10
32 typedef struct sasl_regexp {
33 char *sr_match; /* regexp match pattern */
34 char *sr_replace; /* regexp replace pattern */
35 regex_t sr_workspace; /* workspace for regexp engine */
36 regmatch_t sr_strings[SASLREGEX_REPLACE]; /* strings matching $1,$2 ... */
37 int sr_offset[SASLREGEX_REPLACE+2]; /* offsets of $1,$2... in *replace */
40 static int nSaslRegexp = 0;
41 static SaslRegexp_t *SaslRegexp = NULL;
43 /* What SASL proxy authorization policies are allowed? */
44 #define SASL_AUTHZ_NONE 0
45 #define SASL_AUTHZ_FROM 1
46 #define SASL_AUTHZ_TO 2
48 static int authz_policy = SASL_AUTHZ_NONE;
50 int slap_sasl_setpolicy( const char *arg )
52 int rc = LDAP_SUCCESS;
54 if ( strcasecmp( arg, "none" ) == 0 ) {
55 authz_policy = SASL_AUTHZ_NONE;
56 } else if ( strcasecmp( arg, "from" ) == 0 ) {
57 authz_policy = SASL_AUTHZ_FROM;
58 } else if ( strcasecmp( arg, "to" ) == 0 ) {
59 authz_policy = SASL_AUTHZ_TO;
60 } else if ( strcasecmp( arg, "both" ) == 0 ) {
61 authz_policy = SASL_AUTHZ_FROM | SASL_AUTHZ_TO;
68 /* URI format: ldap://<host>/<base>[?[<attrs>][?[<scope>][?[<filter>]]]] */
70 static int slap_parseURI( struct berval *uri,
71 struct berval *searchbase, int *scope, Filter **filter )
77 assert( uri != NULL && uri->bv_val != NULL );
78 searchbase->bv_val = NULL;
79 searchbase->bv_len = 0;
84 LDAP_LOG( TRANSPORT, ENTRY,
85 "slap_parseURI: parsing %s\n", uri->bv_val, 0, 0 );
87 Debug( LDAP_DEBUG_TRACE, "slap_parseURI: parsing %s\n", uri->bv_val, 0, 0 );
90 /* If it does not look like a URI, assume it is a DN */
91 if( !strncasecmp( uri->bv_val, "dn:", sizeof("dn:")-1 ) ) {
92 bv.bv_val = uri->bv_val + sizeof("dn:")-1;
93 bv.bv_val += strspn( bv.bv_val, " " );
95 is_dn: bv.bv_len = uri->bv_len - (bv.bv_val - uri->bv_val);
97 rc = dnNormalize2( NULL, &bv, searchbase );
98 if( rc == LDAP_SUCCESS ) {
99 *scope = LDAP_SCOPE_BASE;
104 rc = ldap_url_parse( uri->bv_val, &ludp );
105 if ( rc == LDAP_URL_ERR_BADSCHEME ) {
106 bv.bv_val = uri->bv_val;
110 if ( rc != LDAP_URL_SUCCESS ) {
111 return LDAP_PROTOCOL_ERROR;
114 if (( ludp->lud_host && *ludp->lud_host )
115 || ludp->lud_attrs || ludp->lud_exts )
117 /* host part should be empty */
118 /* attrs and extensions parts should be empty */
119 return LDAP_PROTOCOL_ERROR;
123 *scope = ludp->lud_scope;
125 /* Grab the filter */
126 if ( ludp->lud_filter ) {
127 *filter = str2filter( ludp->lud_filter );
128 if ( *filter == NULL ) {
129 rc = LDAP_PROTOCOL_ERROR;
134 /* Grab the searchbase */
135 bv.bv_val = ludp->lud_dn;
136 bv.bv_len = strlen( bv.bv_val );
137 rc = dnNormalize2( NULL, &bv, searchbase );
140 if( rc != LDAP_SUCCESS ) {
141 if( *filter ) filter_free( *filter );
144 ldap_free_urldesc( ludp );
148 static int slap_sasl_rx_off(char *rep, int *off)
153 /* Precompile replace pattern. Find the $<n> placeholders */
156 for ( c = rep; *c; c++ ) {
157 if ( *c == '\\' && c[1] ) {
162 if ( n == SASLREGEX_REPLACE ) {
164 LDAP_LOG( TRANSPORT, ERR,
165 "slap_sasl_rx_off: \"%s\" has too many $n "
166 "placeholders (max %d)\n", rep, SASLREGEX_REPLACE, 0 );
168 Debug( LDAP_DEBUG_ANY,
169 "SASL replace pattern %s has too many $n "
170 "placeholders (max %d)\n",
171 rep, SASLREGEX_REPLACE, 0 );
174 return( LDAP_OTHER );
181 /* Final placeholder, after the last $n */
185 return( LDAP_SUCCESS );
188 int slap_sasl_regexp_config( const char *match, const char *replace )
193 SaslRegexp = (SaslRegexp_t *) ch_realloc( (char *) SaslRegexp,
194 (nSaslRegexp + 1) * sizeof(SaslRegexp_t) );
196 reg = &SaslRegexp[nSaslRegexp];
198 reg->sr_match = ch_strdup( match );
199 reg->sr_replace = ch_strdup( replace );
201 /* Precompile matching pattern */
202 rc = regcomp( ®->sr_workspace, reg->sr_match, REG_EXTENDED|REG_ICASE );
205 LDAP_LOG( TRANSPORT, ERR,
206 "slap_sasl_regexp_config: \"%s\" could not be compiled.\n",
207 reg->sr_match, 0, 0 );
209 Debug( LDAP_DEBUG_ANY,
210 "SASL match pattern %s could not be compiled by regexp engine\n",
211 reg->sr_match, 0, 0 );
214 return( LDAP_OTHER );
217 rc = slap_sasl_rx_off( reg->sr_replace, reg->sr_offset );
218 if ( rc != LDAP_SUCCESS ) return rc;
221 return( LDAP_SUCCESS );
225 /* Perform replacement on regexp matches */
226 static void slap_sasl_rx_exp(
230 const char *saslname,
233 int i, n, len, insert;
235 /* Get the total length of the final URI */
239 while( off[n] >= 0 ) {
240 /* Len of next section from replacement string (x,y,z above) */
241 len += off[n] - off[n-1] - 2;
245 /* Len of string from saslname that matched next $i (b,d above) */
246 i = rep[ off[n] + 1 ] - '0';
247 len += str[i].rm_eo - str[i].rm_so;
250 out->bv_val = ch_malloc( len + 1 );
253 /* Fill in URI with replace string, replacing $i as we go */
256 while( off[n] >= 0) {
257 /* Paste in next section from replacement string (x,y,z above) */
258 len = off[n] - off[n-1] - 2;
259 strncpy( out->bv_val+insert, rep + off[n-1] + 2, len);
264 /* Paste in string from saslname that matched next $i (b,d above) */
265 i = rep[ off[n] + 1 ] - '0';
266 len = str[i].rm_eo - str[i].rm_so;
267 strncpy( out->bv_val+insert, saslname + str[i].rm_so, len );
273 out->bv_val[insert] = '\0';
276 /* Take the passed in SASL name and attempt to convert it into an
277 LDAP URI to find the matching LDAP entry, using the pattern matching
278 strings given in the saslregexp config file directive(s) */
280 static int slap_sasl_regexp( struct berval *in, struct berval *out )
282 char *saslname = in->bv_val;
286 memset( out, 0, sizeof( *out ) );
289 LDAP_LOG( TRANSPORT, ENTRY,
290 "slap_sasl_regexp: converting SASL name %s\n", saslname, 0, 0 );
292 Debug( LDAP_DEBUG_TRACE, "slap_sasl_regexp: converting SASL name %s\n",
296 if (( saslname == NULL ) || ( nSaslRegexp == 0 )) {
300 /* Match the normalized SASL name to the saslregexp patterns */
301 for( reg = SaslRegexp,i=0; i<nSaslRegexp; i++,reg++ ) {
302 if ( regexec( ®->sr_workspace, saslname, SASLREGEX_REPLACE,
303 reg->sr_strings, 0) == 0 )
307 if( i >= nSaslRegexp ) return( 0 );
310 * The match pattern may have been of the form "a(b.*)c(d.*)e" and the
311 * replace pattern of the form "x$1y$2z". The returned string needs
312 * to replace the $1,$2 with the strings that matched (b.*) and (d.*)
314 slap_sasl_rx_exp( reg->sr_replace, reg->sr_offset,
315 reg->sr_strings, saslname, out );
318 LDAP_LOG( TRANSPORT, ENTRY,
319 "slap_sasl_regexp: converted SASL name to %s\n",
320 out->bv_len ? out->bv_val : "", 0, 0 );
322 Debug( LDAP_DEBUG_TRACE,
323 "slap_sasl_regexp: converted SASL name to %s\n",
324 out->bv_len ? out->bv_val : "", 0, 0 );
330 /* Two empty callback functions to avoid sending results */
331 void slap_cb_null_response( Connection *conn, Operation *o, ber_tag_t tag,
332 ber_int_t msgid, ber_int_t err, const char *matched,
333 const char *text, BerVarray ref, const char *resoid,
334 struct berval *resdata, struct berval *sasldata, LDAPControl **c)
338 void slap_cb_null_sresult( Connection *conn, Operation *o, ber_int_t err,
339 const char *matched, const char *text, BerVarray refs, LDAPControl **c,
344 int slap_cb_null_sreference( BackendDB *db, Connection *conn, Operation *o,
345 Entry *e, BerVarray r, LDAPControl **c, BerVarray *v2)
350 /* This callback actually does some work...*/
351 static int sasl_sc_sasl2dn( BackendDB *be, Connection *conn, Operation *o,
352 Entry *e, AttributeName *an, int ao, LDAPControl **c)
354 struct berval *ndn = o->o_callback->sc_private;
356 /* We only want to be called once */
362 LDAP_LOG( TRANSPORT, DETAIL1,
363 "slap_sasl2dn: search DN returned more than 1 entry\n", 0, 0, 0 );
365 Debug( LDAP_DEBUG_TRACE,
366 "slap_sasl2dn: search DN returned more than 1 entry\n", 0,0,0 );
371 ber_dupbv(ndn, &e->e_nname);
376 typedef struct smatch_info {
381 static int sasl_sc_smatch( BackendDB *be, Connection *conn, Operation *o,
382 Entry *e, AttributeName *an, int ao, LDAPControl **c)
384 smatch_info *sm = o->o_callback->sc_private;
386 if (dn_match(sm->dn, &e->e_nname)) {
388 return -1; /* short-circuit the search */
395 * Map a SASL regexp rule to a DN. If the rule is just a DN or a scope=base
396 * URI, just strcmp the rule (or its searchbase) to the *assertDN. Otherwise,
397 * the rule must be used as an internal search for entries. If that search
398 * returns the *assertDN entry, the match is successful.
400 * The assertDN should not have the dn: prefix
404 int slap_sasl_match(Connection *conn, struct berval *rule, struct berval *assertDN, struct berval *authc )
406 struct berval searchbase = {0, NULL};
413 slap_cb_null_response,
414 slap_cb_null_sresult,
421 LDAP_LOG( TRANSPORT, ENTRY,
422 "slap_sasl_match: comparing DN %s to rule %s\n",
423 assertDN->bv_val, rule->bv_val,0 );
425 Debug( LDAP_DEBUG_TRACE,
426 "===>slap_sasl_match: comparing DN %s to rule %s\n",
427 assertDN->bv_val, rule->bv_val, 0 );
430 rc = slap_parseURI( rule, &searchbase, &scope, &filter );
431 if( rc != LDAP_SUCCESS ) goto CONCLUDED;
433 /* Massive shortcut: search scope == base */
434 if( scope == LDAP_SCOPE_BASE ) {
435 rc = regcomp(®, searchbase.bv_val,
436 REG_EXTENDED|REG_ICASE|REG_NOSUB);
438 rc = regexec(®, assertDN->bv_val, 0, NULL, 0);
444 rc = LDAP_INAPPROPRIATE_AUTH;
449 /* Must run an internal search. */
452 LDAP_LOG( TRANSPORT, DETAIL1,
453 "slap_sasl_match: performing internal search (base=%s, scope=%d)\n",
454 searchbase.bv_val, scope,0 );
456 Debug( LDAP_DEBUG_TRACE,
457 "slap_sasl_match: performing internal search (base=%s, scope=%d)\n",
458 searchbase.bv_val, scope, 0 );
461 be = select_backend( &searchbase, 0, 1 );
462 if(( be == NULL ) || ( be->be_search == NULL)) {
463 rc = LDAP_INAPPROPRIATE_AUTH;
471 op.o_tag = LDAP_REQ_SEARCH;
472 op.o_protocol = LDAP_VERSION3;
475 op.o_time = slap_get_time();
476 op.o_do_not_cache = 1;
477 op.o_is_auth_check = 1;
478 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
480 (*be->be_search)( be, conn, &op, /*base=*/NULL, &searchbase,
481 scope, /*deref=*/1, /*sizelimit=*/0, /*time=*/0, filter, /*fstr=*/NULL,
482 /*attrs=*/NULL, /*attrsonly=*/0 );
487 rc = LDAP_INAPPROPRIATE_AUTH;
491 if( searchbase.bv_len ) ch_free( searchbase.bv_val );
492 if( filter ) filter_free( filter );
495 LDAP_LOG( TRANSPORT, ENTRY,
496 "slap_sasl_match: comparison returned %d\n", rc, 0, 0 );
498 Debug( LDAP_DEBUG_TRACE,
499 "<===slap_sasl_match: comparison returned %d\n", rc, 0, 0);
507 * This function answers the question, "Can this ID authorize to that ID?",
508 * based on authorization rules. The rules are stored in the *searchDN, in the
509 * attribute named by *attr. If any of those rules map to the *assertDN, the
510 * authorization is approved.
512 * The DNs should not have the dn: prefix
515 slap_sasl_check_authz( Connection *conn,
516 struct berval *searchDN,
517 struct berval *assertDN,
518 AttributeDescription *ad,
519 struct berval *authc )
525 LDAP_LOG( TRANSPORT, ENTRY,
526 "slap_sasl_check_authz: does %s match %s rule in %s?\n",
527 assertDN->bv_val, ad->ad_cname.bv_val, searchDN->bv_val);
529 Debug( LDAP_DEBUG_TRACE,
530 "==>slap_sasl_check_authz: does %s match %s rule in %s?\n",
531 assertDN->bv_val, ad->ad_cname.bv_val, searchDN->bv_val);
534 rc = backend_attribute( NULL, NULL, conn->c_sasl_bindop, NULL,
535 searchDN, ad, &vals );
536 if( rc != LDAP_SUCCESS ) goto COMPLETE;
538 /* Check if the *assertDN matches any **vals */
539 for( i=0; vals[i].bv_val != NULL; i++ ) {
540 rc = slap_sasl_match( conn, &vals[i], assertDN, authc );
541 if ( rc == LDAP_SUCCESS ) goto COMPLETE;
543 rc = LDAP_INAPPROPRIATE_AUTH;
546 if( vals ) ber_bvarray_free( vals );
549 LDAP_LOG( TRANSPORT, RESULTS,
550 "slap_sasl_check_authz: %s check returning %s\n",
551 ad->ad_cname.bv_val, rc, 0 );
553 Debug( LDAP_DEBUG_TRACE,
554 "<==slap_sasl_check_authz: %s check returning %d\n",
555 ad->ad_cname.bv_val, rc, 0);
562 * Given a SASL name (e.g. "UID=name,cn=REALM,cn=MECH,cn=AUTH")
563 * return the LDAP DN to which it matches. The SASL regexp rules in the config
564 * file turn the SASL name into an LDAP URI. If the URI is just a DN (or a
565 * search with scope=base), just return the URI (or its searchbase). Otherwise
566 * an internal search must be done, and if that search returns exactly one
567 * entry, return the DN of that one entry.
569 void slap_sasl2dn( Connection *conn,
570 struct berval *saslname, struct berval *sasldn )
574 struct berval dn = { 0, NULL };
575 int scope = LDAP_SCOPE_BASE;
576 Filter *filter = NULL;
577 slap_callback cb = { slap_cb_null_response,
578 slap_cb_null_sresult, sasl_sc_sasl2dn, slap_cb_null_sreference, NULL};
580 struct berval regout = { 0, NULL };
583 LDAP_LOG( TRANSPORT, ENTRY,
584 "slap_sasl2dn: converting SASL name %s to DN.\n",
585 saslname->bv_val, 0, 0 );
587 Debug( LDAP_DEBUG_TRACE, "==>slap_sasl2dn: "
588 "converting SASL name %s to a DN\n",
589 saslname->bv_val, 0,0 );
592 sasldn->bv_val = NULL;
594 cb.sc_private = sasldn;
596 /* Convert the SASL name into a minimal URI */
597 if( !slap_sasl_regexp( saslname, ®out ) ) {
601 rc = slap_parseURI( ®out, &dn, &scope, &filter );
602 if( regout.bv_val ) ch_free( regout.bv_val );
603 if( rc != LDAP_SUCCESS ) {
607 /* Must do an internal search */
608 be = select_backend( &dn, 0, 1 );
610 /* Massive shortcut: search scope == base */
611 if( scope == LDAP_SCOPE_BASE ) {
619 LDAP_LOG( TRANSPORT, DETAIL1,
620 "slap_sasl2dn: performing internal search (base=%s, scope=%d)\n",
621 dn.bv_val, scope, 0 );
623 Debug( LDAP_DEBUG_TRACE,
624 "slap_sasl2dn: performing internal search (base=%s, scope=%d)\n",
625 dn.bv_val, scope, 0 );
628 if(( be == NULL ) || ( be->be_search == NULL)) {
632 op.o_tag = LDAP_REQ_SEARCH;
633 op.o_protocol = LDAP_VERSION3;
634 op.o_ndn = conn->c_ndn;
636 op.o_time = slap_get_time();
637 op.o_do_not_cache = 1;
638 op.o_is_auth_check = 1;
639 op.o_threadctx = conn->c_sasl_bindop ? conn->c_sasl_bindop->o_threadctx:
640 ldap_pvt_thread_pool_context( &connection_pool );
642 (*be->be_search)( be, conn, &op, NULL, &dn,
643 scope, LDAP_DEREF_NEVER, 1, 0,
644 filter, NULL, NULL, 1 );
647 if( sasldn->bv_len ) {
648 conn->c_authz_backend = be;
650 if( dn.bv_len ) ch_free( dn.bv_val );
651 if( filter ) filter_free( filter );
654 LDAP_LOG( TRANSPORT, ENTRY,
655 "slap_sasl2dn: Converted SASL name to %s\n",
656 sasldn->bv_len ? sasldn->bv_val : "<nothing>", 0, 0 );
658 Debug( LDAP_DEBUG_TRACE, "<==slap_sasl2dn: Converted SASL name to %s\n",
659 sasldn->bv_len ? sasldn->bv_val : "<nothing>", 0, 0 );
666 /* Check if a bind can SASL authorize to another identity.
667 * The DNs should not have the dn: prefix
670 int slap_sasl_authorized( Connection *conn,
671 struct berval *authcDN, struct berval *authzDN )
673 int rc = LDAP_INAPPROPRIATE_AUTH;
675 /* User binding as anonymous */
676 if ( authzDN == NULL ) {
682 LDAP_LOG( TRANSPORT, ENTRY,
683 "slap_sasl_authorized: can %s become %s?\n",
684 authcDN->bv_val, authzDN->bv_val, 0 );
686 Debug( LDAP_DEBUG_TRACE,
687 "==>slap_sasl_authorized: can %s become %s?\n",
688 authcDN->bv_val, authzDN->bv_val, 0 );
691 /* If person is authorizing to self, succeed */
692 if ( dn_match( authcDN, authzDN ) ) {
697 /* Allow the manager to authorize as any DN. */
698 if( conn->c_authz_backend && be_isroot( conn->c_authz_backend, authcDN )) {
703 /* Check source rules */
704 if( authz_policy & SASL_AUTHZ_TO ) {
705 rc = slap_sasl_check_authz( conn, authcDN, authzDN,
706 slap_schema.si_ad_saslAuthzTo, authcDN );
707 if( rc == LDAP_SUCCESS ) {
712 /* Check destination rules */
713 if( authz_policy & SASL_AUTHZ_FROM ) {
714 rc = slap_sasl_check_authz( conn, authzDN, authcDN,
715 slap_schema.si_ad_saslAuthzFrom, authcDN );
716 if( rc == LDAP_SUCCESS ) {
721 rc = LDAP_INAPPROPRIATE_AUTH;
726 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_authorized: return %d\n", rc,0,0 );
728 Debug( LDAP_DEBUG_TRACE,
729 "<== slap_sasl_authorized: return %d\n", rc, 0, 0 );