2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/socket.h>
25 #include "../../libraries/liblber/lber-int.h"
27 static SLAP_CTRL_PARSE_FN parseAssert;
28 static SLAP_CTRL_PARSE_FN parsePreRead;
29 static SLAP_CTRL_PARSE_FN parsePostRead;
30 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
32 static SLAP_CTRL_PARSE_FN parseManageDIT;
34 static SLAP_CTRL_PARSE_FN parseManageDSAit;
35 static SLAP_CTRL_PARSE_FN parseNoOp;
36 static SLAP_CTRL_PARSE_FN parsePagedResults;
38 static SLAP_CTRL_PARSE_FN parseSortedResults;
40 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
41 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
42 static SLAP_CTRL_PARSE_FN parseDomainScope;
43 #ifdef SLAP_CONTROL_X_TREE_DELETE
44 static SLAP_CTRL_PARSE_FN parseTreeDelete;
46 static SLAP_CTRL_PARSE_FN parseSearchOptions;
47 static SLAP_CTRL_PARSE_FN parseSubentries;
49 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
51 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
52 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
54 struct slap_control_ids slap_cids;
60 /* The controlID for this control */
63 /* Operations supported by control */
66 /* Extended operations supported by control */
67 char **sc_extendedops;
69 /* Control parsing callback */
70 SLAP_CTRL_PARSE_FN *sc_parse;
72 LDAP_SLIST_ENTRY(slap_control) sc_next;
75 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
76 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
79 * all known request control OIDs should be added to this list
82 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
83 * addresses an undefined control; this allows to safely test for
84 * well known controls even if they are not registered, e.g. if
85 * they get moved to modules. An example is sc_LDAPsync, which
86 * is implemented in the syncprov overlay and thus, if configured
87 * as dynamic module, may not be registered. One side effect is that
88 * slap_known_controls[0] == NULL, so it should always be used
90 * FIXME: should we define the "undefined control" oid?
92 char *slap_known_controls[SLAP_MAX_CIDS+1];
93 static int num_known_controls = 1;
95 static char *proxy_authz_extops[] = {
96 LDAP_EXOP_MODIFY_PASSWD,
101 static struct slap_control control_defs[] = {
102 { LDAP_CONTROL_ASSERT,
103 (int)offsetof(struct slap_control_ids, sc_assert),
104 SLAP_CTRL_ACCESS, NULL,
105 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
106 { LDAP_CONTROL_PRE_READ,
107 (int)offsetof(struct slap_control_ids, sc_preRead),
108 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
109 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
110 { LDAP_CONTROL_POST_READ,
111 (int)offsetof(struct slap_control_ids, sc_postRead),
112 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
113 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
114 { LDAP_CONTROL_VALUESRETURNFILTER,
115 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
116 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
117 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
118 { LDAP_CONTROL_PAGEDRESULTS,
119 (int)offsetof(struct slap_control_ids, sc_pagedResults),
120 SLAP_CTRL_SEARCH, NULL,
121 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
123 { LDAP_CONTROL_SORTREQUEST,
124 (int)offsetof(struct slap_control_ids, sc_sortedResults),
125 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE, NULL,
126 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
128 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
129 { LDAP_CONTROL_X_DOMAIN_SCOPE,
130 (int)offsetof(struct slap_control_ids, sc_domainScope),
131 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
132 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
134 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
135 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
136 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
137 SLAP_CTRL_MODIFY, NULL,
138 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
140 #ifdef SLAP_CONTROL_X_TREE_DELETE
141 { LDAP_CONTROL_X_TREE_DELETE,
142 (int)offsetof(struct slap_control_ids, sc_treeDelete),
143 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
144 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
146 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
147 { LDAP_CONTROL_X_SEARCH_OPTIONS,
148 (int)offsetof(struct slap_control_ids, sc_searchOptions),
149 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
150 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
152 #ifdef LDAP_CONTROL_SUBENTRIES
153 { LDAP_CONTROL_SUBENTRIES,
154 (int)offsetof(struct slap_control_ids, sc_subentries),
155 SLAP_CTRL_SEARCH, NULL,
156 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
159 (int)offsetof(struct slap_control_ids, sc_noOp),
160 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
161 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
163 { LDAP_CONTROL_MANAGEDIT,
164 (int)offsetof(struct slap_control_ids, sc_manageDIT),
165 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE, NULL,
166 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
168 { LDAP_CONTROL_MANAGEDSAIT,
169 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
170 SLAP_CTRL_ACCESS, NULL,
171 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
172 { LDAP_CONTROL_PROXY_AUTHZ,
173 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
174 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS, proxy_authz_extops,
175 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
176 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
180 * Register a supported control.
182 * This can be called by an OpenLDAP plugin or, indirectly, by a
183 * SLAPI plugin calling slapi_register_supported_control().
186 register_supported_control(const char *controloid,
187 slap_mask_t controlmask,
189 SLAP_CTRL_PARSE_FN *controlparsefn,
192 struct slap_control *sc;
195 if ( num_known_controls >= SLAP_MAX_CIDS ) {
196 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
197 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
198 SLAP_MAX_CIDS, 0, 0 );
202 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
204 /* sanity check - should never happen */
205 for ( i = 0; slap_known_controls[ i ]; i++ ) {
206 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
207 Debug( LDAP_DEBUG_ANY,
208 "Control %s already registered.\n",
210 return LDAP_PARAM_ERROR;
214 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
215 if ( sc == NULL ) return LDAP_NO_MEMORY;
217 sc->sc_oid = ch_strdup( controloid );
218 sc->sc_mask = controlmask;
219 if ( controlexops != NULL ) {
220 sc->sc_extendedops = ldap_charray_dup( controlexops );
221 if ( sc->sc_extendedops == NULL ) {
223 return LDAP_NO_MEMORY;
226 sc->sc_extendedops = NULL;
228 sc->sc_parse = controlparsefn;
230 if ( controlcid ) *controlcid = num_known_controls;
231 sc->sc_cid = num_known_controls;
233 /* Update slap_known_controls, too. */
234 slap_known_controls[num_known_controls-1] = sc->sc_oid;
235 slap_known_controls[num_known_controls++] = NULL;
237 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
238 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
243 * One-time initialization of internal controls.
246 slap_controls_init( void )
252 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
253 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
254 rc = register_supported_control( control_defs[i].sc_oid,
255 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
256 control_defs[i].sc_parse, cid );
257 if ( rc != LDAP_SUCCESS ) break;
264 * Free memory associated with list of supported controls.
267 controls_destroy( void )
269 struct slap_control *sc;
271 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
272 sc = LDAP_SLIST_FIRST(&controls_list);
273 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
275 ch_free( sc->sc_oid );
276 if ( sc->sc_extendedops != NULL ) {
277 ldap_charray_free( sc->sc_extendedops );
284 * Format the supportedControl attribute of the root DSE,
285 * detailing which controls are supported by the directory
289 controls_root_dse_info( Entry *e )
291 AttributeDescription *ad_supportedControl
292 = slap_schema.si_ad_supportedControl;
293 struct berval vals[2];
294 struct slap_control *sc;
296 vals[1].bv_val = NULL;
299 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
300 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
302 vals[0].bv_val = sc->sc_oid;
303 vals[0].bv_len = strlen( sc->sc_oid );
305 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
314 * Return a list of OIDs and operation masks for supported
315 * controls. Used by SLAPI.
318 get_supported_controls(char ***ctrloidsp,
319 slap_mask_t **ctrlmasks)
324 struct slap_control *sc;
328 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
338 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
339 if ( oids == NULL ) {
340 return LDAP_NO_MEMORY;
342 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
343 if ( masks == NULL ) {
345 return LDAP_NO_MEMORY;
350 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
351 oids[n] = ch_strdup( sc->sc_oid );
352 masks[n] = sc->sc_mask;
365 * Find a control given its OID.
367 static struct slap_control *
368 find_ctrl( const char *oid )
370 struct slap_control *sc;
372 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
373 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
382 slap_find_control_id(
386 struct slap_control *ctrl = find_ctrl( oid );
388 if ( cid ) *cid = ctrl->sc_cid;
391 return LDAP_CONTROL_NOT_FOUND;
395 slap_global_control( Operation *op, const char *oid, int *cid )
397 struct slap_control *ctrl = find_ctrl( oid );
399 if ( ctrl == NULL ) {
400 /* should not be reachable */
401 Debug( LDAP_DEBUG_ANY,
402 "slap_global_control: unrecognized control: %s\n",
404 return LDAP_CONTROL_NOT_FOUND;
407 if ( cid ) *cid = ctrl->sc_cid;
409 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
410 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
411 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
413 return LDAP_COMPARE_TRUE;
416 Debug( LDAP_DEBUG_TRACE,
417 "slap_global_control: unavailable control: %s\n",
420 return LDAP_COMPARE_FALSE;
423 void slap_free_ctrls(
425 LDAPControl **ctrls )
429 for (i=0; ctrls[i]; i++) {
430 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
432 op->o_tmpfree( ctrls, op->o_tmpmemctx );
438 LDAPControl *control,
441 struct slap_control *sc;
443 sc = find_ctrl( control->ldctl_oid );
445 /* recognized control */
447 switch( op->o_tag ) {
449 tagmask = SLAP_CTRL_ADD;
452 tagmask = SLAP_CTRL_BIND;
454 case LDAP_REQ_COMPARE:
455 tagmask = SLAP_CTRL_COMPARE;
457 case LDAP_REQ_DELETE:
458 tagmask = SLAP_CTRL_DELETE;
460 case LDAP_REQ_MODIFY:
461 tagmask = SLAP_CTRL_MODIFY;
463 case LDAP_REQ_RENAME:
464 tagmask = SLAP_CTRL_RENAME;
466 case LDAP_REQ_SEARCH:
467 tagmask = SLAP_CTRL_SEARCH;
469 case LDAP_REQ_UNBIND:
470 tagmask = SLAP_CTRL_UNBIND;
472 case LDAP_REQ_ABANDON:
473 tagmask = SLAP_CTRL_ABANDON;
475 case LDAP_REQ_EXTENDED:
477 assert( op->ore_reqoid.bv_val != NULL );
478 if( sc->sc_extendedops != NULL ) {
480 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
481 if( strcmp( op->ore_reqoid.bv_val,
482 sc->sc_extendedops[i] ) == 0 )
491 *text = "controls internal error";
495 if (( sc->sc_mask & tagmask ) == tagmask ) {
496 /* available extension */
499 if( !sc->sc_parse ) {
500 *text = "not yet implemented";
504 rc = sc->sc_parse( op, rs, control );
506 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
510 } else if( control->ldctl_iscritical ) {
511 /* unavailable CRITICAL control */
512 *text = "critical extension is unavailable";
513 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
515 } else if( control->ldctl_iscritical ) {
516 /* unrecognized CRITICAL control */
517 *text = "critical extension is not recognized";
518 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
533 BerElement *ber = op->o_ber;
536 len = ber_pvt_ber_remaining(ber);
540 rs->sr_err = LDAP_SUCCESS;
544 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
545 if( tag == LBER_ERROR ) {
546 rs->sr_err = SLAPD_DISCONNECT;
547 rs->sr_text = "unexpected data in PDU";
553 Debug( LDAP_DEBUG_TRACE,
554 "=> get_ctrls\n", 0, 0, 0 );
556 if( op->o_protocol < LDAP_VERSION3 ) {
557 rs->sr_err = SLAPD_DISCONNECT;
558 rs->sr_text = "controls require LDAPv3";
562 /* one for first control, one for termination */
563 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
566 if( op->ctrls == NULL ) {
567 rs->sr_err = LDAP_NO_MEMORY;
568 rs->sr_text = "no memory";
573 op->o_ctrls[nctrls] = NULL;
575 /* step through each element */
576 for( tag = ber_first_element( ber, &len, &opaque );
578 tag = ber_next_element( ber, &len, opaque ) )
581 LDAPControl **tctrls;
583 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
584 memset(c, 0, sizeof(LDAPControl));
586 /* allocate pointer space for current controls (nctrls)
587 * + this control + extra NULL
589 tctrls = op->o_tmprealloc( op->o_ctrls,
590 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
593 if( tctrls == NULL ) {
595 ldap_controls_free(op->o_ctrls);
598 rs->sr_err = LDAP_NO_MEMORY;
599 rs->sr_text = "no memory";
603 op->o_ctrls = tctrls;
605 op->o_ctrls[nctrls++] = c;
606 op->o_ctrls[nctrls] = NULL;
608 tag = ber_scanf( ber, "{m" /*}*/, &bv );
609 c->ldctl_oid = bv.bv_val;
611 if( tag == LBER_ERROR ) {
612 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
615 slap_free_ctrls( op, op->o_ctrls );
617 rs->sr_err = SLAPD_DISCONNECT;
618 rs->sr_text = "decoding controls error";
621 } else if( c->ldctl_oid == NULL ) {
622 Debug( LDAP_DEBUG_TRACE,
623 "get_ctrls: conn %lu got emtpy OID.\n",
624 op->o_connid, 0, 0 );
626 slap_free_ctrls( op, op->o_ctrls );
628 rs->sr_err = LDAP_PROTOCOL_ERROR;
629 rs->sr_text = "OID field is empty";
633 tag = ber_peek_tag( ber, &len );
635 if( tag == LBER_BOOLEAN ) {
637 tag = ber_scanf( ber, "b", &crit );
639 if( tag == LBER_ERROR ) {
640 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
642 slap_free_ctrls( op, op->o_ctrls );
644 rs->sr_err = SLAPD_DISCONNECT;
645 rs->sr_text = "decoding controls error";
649 c->ldctl_iscritical = (crit != 0);
650 tag = ber_peek_tag( ber, &len );
653 if( tag == LBER_OCTETSTRING ) {
654 tag = ber_scanf( ber, "m", &c->ldctl_value );
656 if( tag == LBER_ERROR ) {
657 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
658 "%s (%scritical): get value failed.\n",
659 op->o_connid, c->ldctl_oid,
660 c->ldctl_iscritical ? "" : "non" );
661 slap_free_ctrls( op, op->o_ctrls );
663 rs->sr_err = SLAPD_DISCONNECT;
664 rs->sr_text = "decoding controls error";
669 Debug( LDAP_DEBUG_TRACE,
670 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
671 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
673 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
674 if ( rs->sr_err != LDAP_SUCCESS ) {
680 Debug( LDAP_DEBUG_TRACE,
681 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
682 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
684 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
685 if( rs->sr_err == SLAPD_DISCONNECT ) {
686 rs->sr_err = LDAP_PROTOCOL_ERROR;
687 send_ldap_disconnect( op, rs );
688 rs->sr_err = SLAPD_DISCONNECT;
690 send_ldap_result( op, rs );
698 static int parseManageDIT (
703 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
704 rs->sr_text = "manageDIT control specified multiple times";
705 return LDAP_PROTOCOL_ERROR;
708 if ( ctrl->ldctl_value.bv_len ) {
709 rs->sr_text = "manageDIT control value not empty";
710 return LDAP_PROTOCOL_ERROR;
713 op->o_managedit = ctrl->ldctl_iscritical
714 ? SLAP_CONTROL_CRITICAL
715 : SLAP_CONTROL_NONCRITICAL;
721 static int parseManageDSAit (
726 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
727 rs->sr_text = "manageDSAit control specified multiple times";
728 return LDAP_PROTOCOL_ERROR;
731 if ( ctrl->ldctl_value.bv_len ) {
732 rs->sr_text = "manageDSAit control value not empty";
733 return LDAP_PROTOCOL_ERROR;
736 op->o_managedsait = ctrl->ldctl_iscritical
737 ? SLAP_CONTROL_CRITICAL
738 : SLAP_CONTROL_NONCRITICAL;
743 static int parseProxyAuthz (
749 struct berval dn = BER_BVNULL;
751 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
752 rs->sr_text = "proxy authorization control specified multiple times";
753 return LDAP_PROTOCOL_ERROR;
756 op->o_proxy_authz = ctrl->ldctl_iscritical
757 ? SLAP_CONTROL_CRITICAL
758 : SLAP_CONTROL_NONCRITICAL;
760 Debug( LDAP_DEBUG_ARGS,
761 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
763 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
766 if ( ctrl->ldctl_value.bv_len == 0 ) {
767 Debug( LDAP_DEBUG_TRACE,
768 "parseProxyAuthz: conn=%lu anonymous\n",
769 op->o_connid, 0, 0 );
772 op->o_ndn.bv_val[ 0 ] = '\0';
773 op->o_ndn.bv_len = 0;
775 op->o_dn.bv_val[ 0 ] = '\0';
781 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
782 NULL, &dn, SLAP_GETDN_AUTHZID );
784 /* FIXME: empty DN in proxyAuthz control should be legal... */
785 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
787 ch_free( dn.bv_val );
789 rs->sr_text = "authzId mapping failed";
790 return LDAP_PROXY_AUTHZ_FAILURE;
793 Debug( LDAP_DEBUG_TRACE,
794 "parseProxyAuthz: conn=%lu \"%s\"\n",
796 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
798 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
801 ch_free( dn.bv_val );
802 rs->sr_text = "not authorized to assume identity";
803 return LDAP_PROXY_AUTHZ_FAILURE;
806 ch_free( op->o_ndn.bv_val );
807 ch_free( op->o_dn.bv_val );
810 * NOTE: since slap_sasl_getdn() returns a normalized dn,
811 * from now on op->o_dn is normalized
814 ber_dupbv( &op->o_dn, &dn );
817 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
818 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
823 static int parseNoOp (
828 if ( op->o_noop != SLAP_CONTROL_NONE ) {
829 rs->sr_text = "noop control specified multiple times";
830 return LDAP_PROTOCOL_ERROR;
833 if ( ctrl->ldctl_value.bv_len ) {
834 rs->sr_text = "noop control value not empty";
835 return LDAP_PROTOCOL_ERROR;
838 op->o_noop = ctrl->ldctl_iscritical
839 ? SLAP_CONTROL_CRITICAL
840 : SLAP_CONTROL_NONCRITICAL;
845 static int parsePagedResults (
850 int rc = LDAP_SUCCESS;
854 struct berval cookie = BER_BVNULL;
855 PagedResultsState *ps;
857 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
858 rs->sr_text = "paged results control specified multiple times";
859 return LDAP_PROTOCOL_ERROR;
862 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
863 rs->sr_text = "paged results control value is empty (or absent)";
864 return LDAP_PROTOCOL_ERROR;
867 /* Parse the control value
868 * realSearchControlValue ::= SEQUENCE {
869 * size INTEGER (0..maxInt),
870 * -- requested page size from client
871 * -- result set size estimate from server
872 * cookie OCTET STRING
875 ber = ber_init( &ctrl->ldctl_value );
877 rs->sr_text = "internal error";
881 tag = ber_scanf( ber, "{im}", &size, &cookie );
883 if ( tag == LBER_ERROR ) {
884 rs->sr_text = "paged results control could not be decoded";
885 rc = LDAP_PROTOCOL_ERROR;
890 rs->sr_text = "paged results control size invalid";
891 rc = LDAP_PROTOCOL_ERROR;
896 /* defer cookie decoding/checks to backend... */
897 if ( cookie.bv_len ) {
898 PagedResultsCookie reqcookie;
899 if( cookie.bv_len != sizeof( reqcookie ) ) {
901 rs->sr_text = "paged results cookie is invalid";
902 rc = LDAP_PROTOCOL_ERROR;
906 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
908 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
910 rs->sr_text = "paged results cookie is invalid";
911 rc = LDAP_PROTOCOL_ERROR;
914 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
915 rs->sr_text = "paged results cookie is invalid or old";
916 rc = LDAP_UNWILLING_TO_PERFORM;
921 /* Initial request. Initialize state. */
923 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
924 /* There's another pagedResults control on the
925 * same connection; reject new pagedResults controls
926 * (allowed by RFC2696) */
927 rs->sr_text = "paged results cookie unavailable; try later";
928 rc = LDAP_UNWILLING_TO_PERFORM;
932 op->o_pagedresults_state.ps_cookie = 0;
933 op->o_pagedresults_state.ps_count = 0;
937 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
938 *ps = op->o_conn->c_pagedresults_state;
940 op->o_pagedresults_state = ps;
942 /* NOTE: according to RFC 2696 3.:
944 If the page size is greater than or equal to the sizeLimit value, the
945 server should ignore the control as the request can be satisfied in a
948 * NOTE: this assumes that the op->ors_slimit be set
949 * before the controls are parsed.
952 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
953 op->o_pagedresults = SLAP_CONTROL_IGNORED;
955 } else if ( ctrl->ldctl_iscritical ) {
956 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
959 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
963 (void)ber_free( ber, 1 );
968 static int parseSortedResults (
973 int rc = LDAP_SUCCESS;
975 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
976 rs->sr_text = "sorted results control specified multiple times";
977 return LDAP_PROTOCOL_ERROR;
980 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
981 rs->sr_text = "sorted results control value is empty (or absent)";
982 return LDAP_PROTOCOL_ERROR;
985 /* blow off parsing the value */
987 op->o_sortedresults = ctrl->ldctl_iscritical
988 ? SLAP_CONTROL_CRITICAL
989 : SLAP_CONTROL_NONCRITICAL;
995 static int parseAssert (
1001 struct berval fstr = BER_BVNULL;
1003 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1004 rs->sr_text = "assert control specified multiple times";
1005 return LDAP_PROTOCOL_ERROR;
1008 if ( ctrl->ldctl_value.bv_len == 0 ) {
1009 rs->sr_text = "assert control value is empty (or absent)";
1010 return LDAP_PROTOCOL_ERROR;
1013 ber = ber_init( &(ctrl->ldctl_value) );
1015 rs->sr_text = "assert control: internal error";
1019 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
1021 if( rs->sr_err != LDAP_SUCCESS ) {
1022 if( rs->sr_err == SLAPD_DISCONNECT ) {
1023 rs->sr_err = LDAP_PROTOCOL_ERROR;
1024 send_ldap_disconnect( op, rs );
1025 rs->sr_err = SLAPD_DISCONNECT;
1027 send_ldap_result( op, rs );
1029 if( op->o_assertion != NULL ) {
1030 filter_free_x( op, op->o_assertion );
1036 filter2bv_x( op, op->o_assertion, &fstr );
1038 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1039 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1040 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1043 op->o_assert = ctrl->ldctl_iscritical
1044 ? SLAP_CONTROL_CRITICAL
1045 : SLAP_CONTROL_NONCRITICAL;
1047 rs->sr_err = LDAP_SUCCESS;
1048 return LDAP_SUCCESS;
1051 static int parsePreRead (
1056 ber_len_t siz, off, i;
1057 AttributeName *an = NULL;
1060 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1061 rs->sr_text = "preread control specified multiple times";
1062 return LDAP_PROTOCOL_ERROR;
1065 if ( ctrl->ldctl_value.bv_len == 0 ) {
1066 rs->sr_text = "preread control value is empty (or absent)";
1067 return LDAP_PROTOCOL_ERROR;
1070 ber = ber_init( &(ctrl->ldctl_value) );
1072 rs->sr_text = "preread control: internal error";
1076 siz = sizeof( AttributeName );
1077 off = offsetof( AttributeName, an_name );
1078 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1079 rs->sr_text = "preread control: decoding error";
1080 return LDAP_PROTOCOL_ERROR;
1083 for( i=0; i<siz; i++ ) {
1084 int rc = LDAP_SUCCESS;
1085 const char *dummy = NULL;
1087 an[i].an_desc = NULL;
1089 an[i].an_oc_exclude = 0;
1090 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1091 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1094 : "postread control: unknown attributeType";
1099 op->o_preread = ctrl->ldctl_iscritical
1100 ? SLAP_CONTROL_CRITICAL
1101 : SLAP_CONTROL_NONCRITICAL;
1103 op->o_preread_attrs = an;
1105 rs->sr_err = LDAP_SUCCESS;
1106 return LDAP_SUCCESS;
1109 static int parsePostRead (
1114 ber_len_t siz, off, i;
1115 AttributeName *an = NULL;
1118 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1119 rs->sr_text = "postread control specified multiple times";
1120 return LDAP_PROTOCOL_ERROR;
1123 if ( ctrl->ldctl_value.bv_len == 0 ) {
1124 rs->sr_text = "postread control value is empty (or absent)";
1125 return LDAP_PROTOCOL_ERROR;
1128 ber = ber_init( &(ctrl->ldctl_value) );
1130 rs->sr_text = "postread control: internal error";
1134 siz = sizeof( AttributeName );
1135 off = offsetof( AttributeName, an_name );
1136 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1137 rs->sr_text = "postread control: decoding error";
1138 return LDAP_PROTOCOL_ERROR;
1141 for( i=0; i<siz; i++ ) {
1142 int rc = LDAP_SUCCESS;
1143 const char *dummy = NULL;
1145 an[i].an_desc = NULL;
1147 an[i].an_oc_exclude = 0;
1148 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1149 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1152 : "postread control: unknown attributeType";
1157 op->o_postread = ctrl->ldctl_iscritical
1158 ? SLAP_CONTROL_CRITICAL
1159 : SLAP_CONTROL_NONCRITICAL;
1161 op->o_postread_attrs = an;
1163 rs->sr_err = LDAP_SUCCESS;
1164 return LDAP_SUCCESS;
1167 static int parseValuesReturnFilter (
1173 struct berval fstr = BER_BVNULL;
1175 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1176 rs->sr_text = "valuesReturnFilter control specified multiple times";
1177 return LDAP_PROTOCOL_ERROR;
1180 if ( ctrl->ldctl_value.bv_len == 0 ) {
1181 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1182 return LDAP_PROTOCOL_ERROR;
1185 ber = ber_init( &(ctrl->ldctl_value) );
1187 rs->sr_text = "internal error";
1191 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1193 if( rs->sr_err != LDAP_SUCCESS ) {
1194 if( rs->sr_err == SLAPD_DISCONNECT ) {
1195 rs->sr_err = LDAP_PROTOCOL_ERROR;
1196 send_ldap_disconnect( op, rs );
1197 rs->sr_err = SLAPD_DISCONNECT;
1199 send_ldap_result( op, rs );
1201 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1205 vrFilter2bv( op, op->o_vrFilter, &fstr );
1208 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1209 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1210 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1213 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1214 ? SLAP_CONTROL_CRITICAL
1215 : SLAP_CONTROL_NONCRITICAL;
1217 rs->sr_err = LDAP_SUCCESS;
1218 return LDAP_SUCCESS;
1221 #ifdef LDAP_CONTROL_SUBENTRIES
1222 static int parseSubentries (
1227 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1228 rs->sr_text = "subentries control specified multiple times";
1229 return LDAP_PROTOCOL_ERROR;
1232 /* FIXME: should use BER library */
1233 if( ( ctrl->ldctl_value.bv_len != 3 )
1234 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1235 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1237 rs->sr_text = "subentries control value encoding is bogus";
1238 return LDAP_PROTOCOL_ERROR;
1241 op->o_subentries = ctrl->ldctl_iscritical
1242 ? SLAP_CONTROL_CRITICAL
1243 : SLAP_CONTROL_NONCRITICAL;
1245 if (ctrl->ldctl_value.bv_val[2]) {
1246 set_subentries_visibility( op );
1249 return LDAP_SUCCESS;
1253 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1254 static int parsePermissiveModify (
1259 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1260 rs->sr_text = "permissiveModify control specified multiple times";
1261 return LDAP_PROTOCOL_ERROR;
1264 if ( ctrl->ldctl_value.bv_len ) {
1265 rs->sr_text = "permissiveModify control value not empty";
1266 return LDAP_PROTOCOL_ERROR;
1269 op->o_permissive_modify = ctrl->ldctl_iscritical
1270 ? SLAP_CONTROL_CRITICAL
1271 : SLAP_CONTROL_NONCRITICAL;
1273 return LDAP_SUCCESS;
1277 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1278 static int parseDomainScope (
1283 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1284 rs->sr_text = "domainScope control specified multiple times";
1285 return LDAP_PROTOCOL_ERROR;
1288 if ( ctrl->ldctl_value.bv_len ) {
1289 rs->sr_text = "domainScope control value not empty";
1290 return LDAP_PROTOCOL_ERROR;
1293 op->o_domain_scope = ctrl->ldctl_iscritical
1294 ? SLAP_CONTROL_CRITICAL
1295 : SLAP_CONTROL_NONCRITICAL;
1297 return LDAP_SUCCESS;
1301 #ifdef SLAP_CONTROL_X_TREE_DELETE
1302 static int parseTreeDelete (
1307 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1308 rs->sr_text = "treeDelete control specified multiple times";
1309 return LDAP_PROTOCOL_ERROR;
1312 if ( ctrl->ldctl_value.bv_len ) {
1313 rs->sr_text = "treeDelete control value not empty";
1314 return LDAP_PROTOCOL_ERROR;
1317 op->o_tree_delete = ctrl->ldctl_iscritical
1318 ? SLAP_CONTROL_CRITICAL
1319 : SLAP_CONTROL_NONCRITICAL;
1321 return LDAP_SUCCESS;
1325 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
1326 static int parseSearchOptions (
1332 ber_int_t search_flags;
1335 if ( ctrl->ldctl_value.bv_len == 0 ) {
1336 rs->sr_text = "searchOptions control value not empty";
1337 return LDAP_PROTOCOL_ERROR;
1340 ber = ber_init( &ctrl->ldctl_value );
1342 rs->sr_text = "internal error";
1346 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1347 rs->sr_text = "searchOptions control decoding error";
1348 return LDAP_PROTOCOL_ERROR;
1351 (void) ber_free( ber, 1 );
1353 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1354 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1355 rs->sr_text = "searchOptions control specified multiple times "
1356 "or with domainScope control";
1357 return LDAP_PROTOCOL_ERROR;
1360 op->o_domain_scope = ctrl->ldctl_iscritical
1361 ? SLAP_CONTROL_CRITICAL
1362 : SLAP_CONTROL_NONCRITICAL;
1365 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1366 /* Other search flags not recognised so far,
1368 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1370 rs->sr_text = "searchOptions contained unrecongized flag";
1371 return LDAP_UNWILLING_TO_PERFORM;
1374 return LDAP_SUCCESS;