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 parseModifyIncrement;
36 static SLAP_CTRL_PARSE_FN parseNoOp;
37 static SLAP_CTRL_PARSE_FN parsePagedResults;
39 static SLAP_CTRL_PARSE_FN parseSortedResults;
41 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
42 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
43 static SLAP_CTRL_PARSE_FN parseDomainScope;
44 #ifdef SLAP_CONTROL_X_TREE_DELETE
45 static SLAP_CTRL_PARSE_FN parseTreeDelete;
47 static SLAP_CTRL_PARSE_FN parseSearchOptions;
48 static SLAP_CTRL_PARSE_FN parseSubentries;
50 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
52 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
53 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
55 struct slap_control_ids slap_cids;
61 /* The controlID for this control */
64 /* Operations supported by control */
67 /* Extended operations supported by control */
68 char **sc_extendedops;
70 /* Control parsing callback */
71 SLAP_CTRL_PARSE_FN *sc_parse;
73 LDAP_SLIST_ENTRY(slap_control) sc_next;
76 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
77 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
80 * all known request control OIDs should be added to this list
83 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
84 * addresses an undefined control; this allows to safely test for
85 * well known controls even if they are not registered, e.g. if
86 * they get moved to modules. An example is sc_LDAPsync, which
87 * is implemented in the syncprov overlay and thus, if configured
88 * as dynamic module, may not be registered. One side effect is that
89 * slap_known_controls[0] == NULL, so it should always be used
91 * FIXME: should we define the "undefined control" oid?
93 char *slap_known_controls[SLAP_MAX_CIDS+1];
94 static int num_known_controls = 1;
96 static char *proxy_authz_extops[] = {
97 LDAP_EXOP_MODIFY_PASSWD,
102 static struct slap_control control_defs[] = {
103 { LDAP_CONTROL_ASSERT,
104 (int)offsetof(struct slap_control_ids, sc_assert),
105 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
106 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
107 { LDAP_CONTROL_PRE_READ,
108 (int)offsetof(struct slap_control_ids, sc_preRead),
109 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
110 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
111 { LDAP_CONTROL_POST_READ,
112 (int)offsetof(struct slap_control_ids, sc_postRead),
113 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
114 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
115 { LDAP_CONTROL_VALUESRETURNFILTER,
116 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
117 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
118 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
119 { LDAP_CONTROL_PAGEDRESULTS,
120 (int)offsetof(struct slap_control_ids, sc_pagedResults),
121 SLAP_CTRL_SEARCH, NULL,
122 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
124 { LDAP_CONTROL_SORTREQUEST,
125 (int)offsetof(struct slap_control_ids, sc_sortedResults),
126 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE, NULL,
127 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
129 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
130 { LDAP_CONTROL_X_DOMAIN_SCOPE,
131 (int)offsetof(struct slap_control_ids, sc_domainScope),
132 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
133 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
135 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
136 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
137 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
138 SLAP_CTRL_MODIFY, NULL,
139 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
141 #ifdef SLAP_CONTROL_X_TREE_DELETE
142 { LDAP_CONTROL_X_TREE_DELETE,
143 (int)offsetof(struct slap_control_ids, sc_treeDelete),
144 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
145 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
147 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
148 { LDAP_CONTROL_X_SEARCH_OPTIONS,
149 (int)offsetof(struct slap_control_ids, sc_searchOptions),
150 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
151 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
153 #ifdef LDAP_CONTROL_SUBENTRIES
154 { LDAP_CONTROL_SUBENTRIES,
155 (int)offsetof(struct slap_control_ids, sc_subentries),
156 SLAP_CTRL_SEARCH, NULL,
157 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
160 (int)offsetof(struct slap_control_ids, sc_noOp),
161 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
162 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
163 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
164 { LDAP_CONTROL_MODIFY_INCREMENT,
165 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
166 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
167 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
170 { LDAP_CONTROL_MANAGEDIT,
171 (int)offsetof(struct slap_control_ids, sc_manageDIT),
172 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE, NULL,
173 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
175 { LDAP_CONTROL_MANAGEDSAIT,
176 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
177 SLAP_CTRL_ACCESS, NULL,
178 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
179 { LDAP_CONTROL_PROXY_AUTHZ,
180 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
181 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS, proxy_authz_extops,
182 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
183 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
187 * Register a supported control.
189 * This can be called by an OpenLDAP plugin or, indirectly, by a
190 * SLAPI plugin calling slapi_register_supported_control().
193 register_supported_control(const char *controloid,
194 slap_mask_t controlmask,
196 SLAP_CTRL_PARSE_FN *controlparsefn,
199 struct slap_control *sc;
202 if ( num_known_controls >= SLAP_MAX_CIDS ) {
203 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
204 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
205 SLAP_MAX_CIDS, 0, 0 );
209 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
211 /* sanity check - should never happen */
212 for ( i = 0; slap_known_controls[ i ]; i++ ) {
213 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
214 Debug( LDAP_DEBUG_ANY,
215 "Control %s already registered.\n",
217 return LDAP_PARAM_ERROR;
221 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
222 if ( sc == NULL ) return LDAP_NO_MEMORY;
224 sc->sc_oid = ch_strdup( controloid );
225 sc->sc_mask = controlmask;
226 if ( controlexops != NULL ) {
227 sc->sc_extendedops = ldap_charray_dup( controlexops );
228 if ( sc->sc_extendedops == NULL ) {
230 return LDAP_NO_MEMORY;
233 sc->sc_extendedops = NULL;
235 sc->sc_parse = controlparsefn;
237 if ( controlcid ) *controlcid = num_known_controls;
238 sc->sc_cid = num_known_controls;
240 /* Update slap_known_controls, too. */
241 slap_known_controls[num_known_controls-1] = sc->sc_oid;
242 slap_known_controls[num_known_controls++] = NULL;
244 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
245 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
250 * One-time initialization of internal controls.
253 slap_controls_init( void )
259 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
260 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
261 rc = register_supported_control( control_defs[i].sc_oid,
262 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
263 control_defs[i].sc_parse, cid );
264 if ( rc != LDAP_SUCCESS ) break;
271 * Free memory associated with list of supported controls.
274 controls_destroy( void )
276 struct slap_control *sc;
278 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
279 sc = LDAP_SLIST_FIRST(&controls_list);
280 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
282 ch_free( sc->sc_oid );
283 if ( sc->sc_extendedops != NULL ) {
284 ldap_charray_free( sc->sc_extendedops );
291 * Format the supportedControl attribute of the root DSE,
292 * detailing which controls are supported by the directory
296 controls_root_dse_info( Entry *e )
298 AttributeDescription *ad_supportedControl
299 = slap_schema.si_ad_supportedControl;
300 struct berval vals[2];
301 struct slap_control *sc;
303 vals[1].bv_val = NULL;
306 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
307 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
309 vals[0].bv_val = sc->sc_oid;
310 vals[0].bv_len = strlen( sc->sc_oid );
312 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
321 * Return a list of OIDs and operation masks for supported
322 * controls. Used by SLAPI.
325 get_supported_controls(char ***ctrloidsp,
326 slap_mask_t **ctrlmasks)
331 struct slap_control *sc;
335 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
345 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
346 if ( oids == NULL ) {
347 return LDAP_NO_MEMORY;
349 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
350 if ( masks == NULL ) {
352 return LDAP_NO_MEMORY;
357 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
358 oids[n] = ch_strdup( sc->sc_oid );
359 masks[n] = sc->sc_mask;
372 * Find a control given its OID.
374 static struct slap_control *
375 find_ctrl( const char *oid )
377 struct slap_control *sc;
379 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
380 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
389 slap_find_control_id(
393 struct slap_control *ctrl = find_ctrl( oid );
395 if ( cid ) *cid = ctrl->sc_cid;
398 return LDAP_CONTROL_NOT_FOUND;
402 slap_global_control( Operation *op, const char *oid, int *cid )
404 struct slap_control *ctrl = find_ctrl( oid );
406 if ( ctrl == NULL ) {
407 /* should not be reachable */
408 Debug( LDAP_DEBUG_ANY,
409 "slap_global_control: unrecognized control: %s\n",
411 return LDAP_CONTROL_NOT_FOUND;
414 if ( cid ) *cid = ctrl->sc_cid;
416 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
417 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
418 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
420 return LDAP_COMPARE_TRUE;
423 Debug( LDAP_DEBUG_TRACE,
424 "slap_global_control: unavailable control: %s\n",
427 return LDAP_COMPARE_FALSE;
430 void slap_free_ctrls(
432 LDAPControl **ctrls )
436 for (i=0; ctrls[i]; i++) {
437 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
439 op->o_tmpfree( ctrls, op->o_tmpmemctx );
451 BerElement *ber = op->o_ber;
452 struct slap_control *sc;
455 len = ber_pvt_ber_remaining(ber);
459 rs->sr_err = LDAP_SUCCESS;
463 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
464 if( tag == LBER_ERROR ) {
465 rs->sr_err = SLAPD_DISCONNECT;
466 rs->sr_text = "unexpected data in PDU";
472 Debug( LDAP_DEBUG_TRACE,
473 "=> get_ctrls\n", 0, 0, 0 );
475 if( op->o_protocol < LDAP_VERSION3 ) {
476 rs->sr_err = SLAPD_DISCONNECT;
477 rs->sr_text = "controls require LDAPv3";
481 /* one for first control, one for termination */
482 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
485 if( op->ctrls == NULL ) {
486 rs->sr_err = LDAP_NO_MEMORY;
487 rs->sr_text = "no memory";
492 op->o_ctrls[nctrls] = NULL;
494 /* step through each element */
495 for( tag = ber_first_element( ber, &len, &opaque );
497 tag = ber_next_element( ber, &len, opaque ) )
500 LDAPControl **tctrls;
502 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
503 memset(c, 0, sizeof(LDAPControl));
505 /* allocate pointer space for current controls (nctrls)
506 * + this control + extra NULL
508 tctrls = op->o_tmprealloc( op->o_ctrls,
509 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
512 if( tctrls == NULL ) {
514 ldap_controls_free(op->o_ctrls);
517 rs->sr_err = LDAP_NO_MEMORY;
518 rs->sr_text = "no memory";
522 op->o_ctrls = tctrls;
524 op->o_ctrls[nctrls++] = c;
525 op->o_ctrls[nctrls] = NULL;
527 tag = ber_scanf( ber, "{m" /*}*/, &bv );
528 c->ldctl_oid = bv.bv_val;
530 if( tag == LBER_ERROR ) {
531 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
534 slap_free_ctrls( op, op->o_ctrls );
536 rs->sr_err = SLAPD_DISCONNECT;
537 rs->sr_text = "decoding controls error";
540 } else if( c->ldctl_oid == NULL ) {
541 Debug( LDAP_DEBUG_TRACE,
542 "get_ctrls: conn %lu got emtpy OID.\n",
543 op->o_connid, 0, 0 );
545 slap_free_ctrls( op, op->o_ctrls );
547 rs->sr_err = LDAP_PROTOCOL_ERROR;
548 rs->sr_text = "OID field is empty";
552 tag = ber_peek_tag( ber, &len );
554 if( tag == LBER_BOOLEAN ) {
556 tag = ber_scanf( ber, "b", &crit );
558 if( tag == LBER_ERROR ) {
559 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
561 slap_free_ctrls( op, op->o_ctrls );
563 rs->sr_err = SLAPD_DISCONNECT;
564 rs->sr_text = "decoding controls error";
568 c->ldctl_iscritical = (crit != 0);
569 tag = ber_peek_tag( ber, &len );
572 if( tag == LBER_OCTETSTRING ) {
573 tag = ber_scanf( ber, "m", &c->ldctl_value );
575 if( tag == LBER_ERROR ) {
576 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
577 "%s (%scritical): get value failed.\n",
578 op->o_connid, c->ldctl_oid,
579 c->ldctl_iscritical ? "" : "non" );
580 slap_free_ctrls( op, op->o_ctrls );
582 rs->sr_err = SLAPD_DISCONNECT;
583 rs->sr_text = "decoding controls error";
588 Debug( LDAP_DEBUG_TRACE,
589 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
590 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
592 sc = find_ctrl( c->ldctl_oid );
594 /* recognized control */
596 switch( op->o_tag ) {
598 tagmask = SLAP_CTRL_ADD;
601 tagmask = SLAP_CTRL_BIND;
603 case LDAP_REQ_COMPARE:
604 tagmask = SLAP_CTRL_COMPARE;
606 case LDAP_REQ_DELETE:
607 tagmask = SLAP_CTRL_DELETE;
609 case LDAP_REQ_MODIFY:
610 tagmask = SLAP_CTRL_MODIFY;
612 case LDAP_REQ_RENAME:
613 tagmask = SLAP_CTRL_RENAME;
615 case LDAP_REQ_SEARCH:
616 tagmask = SLAP_CTRL_SEARCH;
618 case LDAP_REQ_UNBIND:
619 tagmask = SLAP_CTRL_UNBIND;
621 case LDAP_REQ_ABANDON:
622 tagmask = SLAP_CTRL_ABANDON;
624 case LDAP_REQ_EXTENDED:
626 assert( op->ore_reqoid.bv_val != NULL );
627 if( sc->sc_extendedops != NULL ) {
629 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
630 if( strcmp( op->ore_reqoid.bv_val,
631 sc->sc_extendedops[i] ) == 0 )
640 rs->sr_err = LDAP_OTHER;
641 rs->sr_text = "controls internal error";
645 if (( sc->sc_mask & tagmask ) == tagmask ) {
646 /* available extension */
649 if( !sc->sc_parse ) {
650 rs->sr_err = LDAP_OTHER;
651 rs->sr_text = "not yet implemented";
655 rc = sc->sc_parse( op, rs, c );
657 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
662 } else if( c->ldctl_iscritical ) {
663 /* unavailable CRITICAL control */
664 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
665 rs->sr_text = "critical extension is unavailable";
669 } else if( c->ldctl_iscritical ) {
670 /* unrecognized CRITICAL control */
671 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
672 rs->sr_text = "critical extension is not recognized";
679 Debug( LDAP_DEBUG_TRACE,
680 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
681 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
683 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
684 if( rs->sr_err == SLAPD_DISCONNECT ) {
685 rs->sr_err = LDAP_PROTOCOL_ERROR;
686 send_ldap_disconnect( op, rs );
687 rs->sr_err = SLAPD_DISCONNECT;
689 send_ldap_result( op, rs );
696 static int parseModifyIncrement (
702 if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
703 rs->sr_text = "modifyIncrement control specified multiple times";
704 return LDAP_PROTOCOL_ERROR;
708 if ( ctrl->ldctl_value.bv_len ) {
709 rs->sr_text = "modifyIncrement control value not empty";
710 return LDAP_PROTOCOL_ERROR;
714 op->o_modifyIncrement = ctrl->ldctl_iscritical
715 ? SLAP_CONTROL_CRITICAL
716 : SLAP_CONTROL_NONCRITICAL;
723 static int parseManageDIT (
728 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
729 rs->sr_text = "manageDIT control specified multiple times";
730 return LDAP_PROTOCOL_ERROR;
733 if ( ctrl->ldctl_value.bv_len ) {
734 rs->sr_text = "manageDIT control value not empty";
735 return LDAP_PROTOCOL_ERROR;
738 op->o_managedit = ctrl->ldctl_iscritical
739 ? SLAP_CONTROL_CRITICAL
740 : SLAP_CONTROL_NONCRITICAL;
746 static int parseManageDSAit (
751 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
752 rs->sr_text = "manageDSAit control specified multiple times";
753 return LDAP_PROTOCOL_ERROR;
756 if ( ctrl->ldctl_value.bv_len ) {
757 rs->sr_text = "manageDSAit control value not empty";
758 return LDAP_PROTOCOL_ERROR;
761 op->o_managedsait = ctrl->ldctl_iscritical
762 ? SLAP_CONTROL_CRITICAL
763 : SLAP_CONTROL_NONCRITICAL;
768 static int parseProxyAuthz (
774 struct berval dn = BER_BVNULL;
776 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
777 rs->sr_text = "proxy authorization control specified multiple times";
778 return LDAP_PROTOCOL_ERROR;
781 op->o_proxy_authz = ctrl->ldctl_iscritical
782 ? SLAP_CONTROL_CRITICAL
783 : SLAP_CONTROL_NONCRITICAL;
785 Debug( LDAP_DEBUG_ARGS,
786 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
788 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
791 if ( ctrl->ldctl_value.bv_len == 0 ) {
792 Debug( LDAP_DEBUG_TRACE,
793 "parseProxyAuthz: conn=%lu anonymous\n",
794 op->o_connid, 0, 0 );
797 op->o_ndn.bv_val[ 0 ] = '\0';
798 op->o_ndn.bv_len = 0;
800 op->o_dn.bv_val[ 0 ] = '\0';
806 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
807 NULL, &dn, SLAP_GETDN_AUTHZID );
809 /* FIXME: empty DN in proxyAuthz control should be legal... */
810 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
812 ch_free( dn.bv_val );
814 rs->sr_text = "authzId mapping failed";
815 return LDAP_PROXY_AUTHZ_FAILURE;
818 Debug( LDAP_DEBUG_TRACE,
819 "parseProxyAuthz: conn=%lu \"%s\"\n",
821 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
823 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
826 ch_free( dn.bv_val );
827 rs->sr_text = "not authorized to assume identity";
828 return LDAP_PROXY_AUTHZ_FAILURE;
831 ch_free( op->o_ndn.bv_val );
832 ch_free( op->o_dn.bv_val );
835 * NOTE: since slap_sasl_getdn() returns a normalized dn,
836 * from now on op->o_dn is normalized
839 ber_dupbv( &op->o_dn, &dn );
842 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
843 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
848 static int parseNoOp (
853 if ( op->o_noop != SLAP_CONTROL_NONE ) {
854 rs->sr_text = "noop control specified multiple times";
855 return LDAP_PROTOCOL_ERROR;
858 if ( ctrl->ldctl_value.bv_len ) {
859 rs->sr_text = "noop control value not empty";
860 return LDAP_PROTOCOL_ERROR;
863 op->o_noop = ctrl->ldctl_iscritical
864 ? SLAP_CONTROL_CRITICAL
865 : SLAP_CONTROL_NONCRITICAL;
870 static int parsePagedResults (
875 int rc = LDAP_SUCCESS;
879 struct berval cookie = BER_BVNULL;
880 PagedResultsState *ps;
882 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
883 rs->sr_text = "paged results control specified multiple times";
884 return LDAP_PROTOCOL_ERROR;
887 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
888 rs->sr_text = "paged results control value is empty (or absent)";
889 return LDAP_PROTOCOL_ERROR;
892 /* Parse the control value
893 * realSearchControlValue ::= SEQUENCE {
894 * size INTEGER (0..maxInt),
895 * -- requested page size from client
896 * -- result set size estimate from server
897 * cookie OCTET STRING
900 ber = ber_init( &ctrl->ldctl_value );
902 rs->sr_text = "internal error";
906 tag = ber_scanf( ber, "{im}", &size, &cookie );
908 if ( tag == LBER_ERROR ) {
909 rs->sr_text = "paged results control could not be decoded";
910 rc = LDAP_PROTOCOL_ERROR;
915 rs->sr_text = "paged results control size invalid";
916 rc = LDAP_PROTOCOL_ERROR;
921 /* defer cookie decoding/checks to backend... */
922 if ( cookie.bv_len ) {
923 PagedResultsCookie reqcookie;
924 if( cookie.bv_len != sizeof( reqcookie ) ) {
926 rs->sr_text = "paged results cookie is invalid";
927 rc = LDAP_PROTOCOL_ERROR;
931 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
933 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
935 rs->sr_text = "paged results cookie is invalid";
936 rc = LDAP_PROTOCOL_ERROR;
939 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
940 rs->sr_text = "paged results cookie is invalid or old";
941 rc = LDAP_UNWILLING_TO_PERFORM;
946 /* Initial request. Initialize state. */
948 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
949 /* There's another pagedResults control on the
950 * same connection; reject new pagedResults controls
951 * (allowed by RFC2696) */
952 rs->sr_text = "paged results cookie unavailable; try later";
953 rc = LDAP_UNWILLING_TO_PERFORM;
957 op->o_pagedresults_state.ps_cookie = 0;
958 op->o_pagedresults_state.ps_count = 0;
962 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
963 *ps = op->o_conn->c_pagedresults_state;
965 op->o_pagedresults_state = ps;
967 /* NOTE: according to RFC 2696 3.:
969 If the page size is greater than or equal to the sizeLimit value, the
970 server should ignore the control as the request can be satisfied in a
973 * NOTE: this assumes that the op->ors_slimit be set
974 * before the controls are parsed.
977 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
978 op->o_pagedresults = SLAP_CONTROL_IGNORED;
980 } else if ( ctrl->ldctl_iscritical ) {
981 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
984 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
988 (void)ber_free( ber, 1 );
993 static int parseSortedResults (
998 int rc = LDAP_SUCCESS;
1000 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1001 rs->sr_text = "sorted results control specified multiple times";
1002 return LDAP_PROTOCOL_ERROR;
1005 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1006 rs->sr_text = "sorted results control value is empty (or absent)";
1007 return LDAP_PROTOCOL_ERROR;
1010 /* blow off parsing the value */
1012 op->o_sortedresults = ctrl->ldctl_iscritical
1013 ? SLAP_CONTROL_CRITICAL
1014 : SLAP_CONTROL_NONCRITICAL;
1020 static int parseAssert (
1026 struct berval fstr = BER_BVNULL;
1028 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1029 rs->sr_text = "assert control specified multiple times";
1030 return LDAP_PROTOCOL_ERROR;
1033 if ( ctrl->ldctl_value.bv_len == 0 ) {
1034 rs->sr_text = "assert control value is empty (or absent)";
1035 return LDAP_PROTOCOL_ERROR;
1038 ber = ber_init( &(ctrl->ldctl_value) );
1040 rs->sr_text = "assert control: internal error";
1044 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
1046 if( rs->sr_err != LDAP_SUCCESS ) {
1047 if( rs->sr_err == SLAPD_DISCONNECT ) {
1048 rs->sr_err = LDAP_PROTOCOL_ERROR;
1049 send_ldap_disconnect( op, rs );
1050 rs->sr_err = SLAPD_DISCONNECT;
1052 send_ldap_result( op, rs );
1054 if( op->o_assertion != NULL ) {
1055 filter_free_x( op, op->o_assertion );
1061 filter2bv_x( op, op->o_assertion, &fstr );
1063 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1064 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1065 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1068 op->o_assert = ctrl->ldctl_iscritical
1069 ? SLAP_CONTROL_CRITICAL
1070 : SLAP_CONTROL_NONCRITICAL;
1072 rs->sr_err = LDAP_SUCCESS;
1073 return LDAP_SUCCESS;
1076 static int parsePreRead (
1081 ber_len_t siz, off, i;
1082 AttributeName *an = NULL;
1085 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1086 rs->sr_text = "preread control specified multiple times";
1087 return LDAP_PROTOCOL_ERROR;
1090 if ( ctrl->ldctl_value.bv_len == 0 ) {
1091 rs->sr_text = "preread control value is empty (or absent)";
1092 return LDAP_PROTOCOL_ERROR;
1095 ber = ber_init( &(ctrl->ldctl_value) );
1097 rs->sr_text = "preread control: internal error";
1101 siz = sizeof( AttributeName );
1102 off = offsetof( AttributeName, an_name );
1103 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1104 rs->sr_text = "preread control: decoding error";
1105 return LDAP_PROTOCOL_ERROR;
1108 for( i=0; i<siz; i++ ) {
1109 int rc = LDAP_SUCCESS;
1110 const char *dummy = NULL;
1112 an[i].an_desc = NULL;
1114 an[i].an_oc_exclude = 0;
1115 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1116 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1119 : "postread control: unknown attributeType";
1124 op->o_preread = ctrl->ldctl_iscritical
1125 ? SLAP_CONTROL_CRITICAL
1126 : SLAP_CONTROL_NONCRITICAL;
1128 op->o_preread_attrs = an;
1130 rs->sr_err = LDAP_SUCCESS;
1131 return LDAP_SUCCESS;
1134 static int parsePostRead (
1139 ber_len_t siz, off, i;
1140 AttributeName *an = NULL;
1143 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1144 rs->sr_text = "postread control specified multiple times";
1145 return LDAP_PROTOCOL_ERROR;
1148 if ( ctrl->ldctl_value.bv_len == 0 ) {
1149 rs->sr_text = "postread control value is empty (or absent)";
1150 return LDAP_PROTOCOL_ERROR;
1153 ber = ber_init( &(ctrl->ldctl_value) );
1155 rs->sr_text = "postread control: internal error";
1159 siz = sizeof( AttributeName );
1160 off = offsetof( AttributeName, an_name );
1161 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1162 rs->sr_text = "postread control: decoding error";
1163 return LDAP_PROTOCOL_ERROR;
1166 for( i=0; i<siz; i++ ) {
1167 int rc = LDAP_SUCCESS;
1168 const char *dummy = NULL;
1170 an[i].an_desc = NULL;
1172 an[i].an_oc_exclude = 0;
1173 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1174 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1177 : "postread control: unknown attributeType";
1182 op->o_postread = ctrl->ldctl_iscritical
1183 ? SLAP_CONTROL_CRITICAL
1184 : SLAP_CONTROL_NONCRITICAL;
1186 op->o_postread_attrs = an;
1188 rs->sr_err = LDAP_SUCCESS;
1189 return LDAP_SUCCESS;
1192 int parseValuesReturnFilter (
1198 struct berval fstr = BER_BVNULL;
1200 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1201 rs->sr_text = "valuesReturnFilter control specified multiple times";
1202 return LDAP_PROTOCOL_ERROR;
1205 if ( ctrl->ldctl_value.bv_len == 0 ) {
1206 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1207 return LDAP_PROTOCOL_ERROR;
1210 ber = ber_init( &(ctrl->ldctl_value) );
1212 rs->sr_text = "internal error";
1216 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1218 if( rs->sr_err != LDAP_SUCCESS ) {
1219 if( rs->sr_err == SLAPD_DISCONNECT ) {
1220 rs->sr_err = LDAP_PROTOCOL_ERROR;
1221 send_ldap_disconnect( op, rs );
1222 rs->sr_err = SLAPD_DISCONNECT;
1224 send_ldap_result( op, rs );
1226 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1230 vrFilter2bv( op, op->o_vrFilter, &fstr );
1233 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1234 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1235 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1238 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1239 ? SLAP_CONTROL_CRITICAL
1240 : SLAP_CONTROL_NONCRITICAL;
1242 rs->sr_err = LDAP_SUCCESS;
1243 return LDAP_SUCCESS;
1246 #ifdef LDAP_CONTROL_SUBENTRIES
1247 static int parseSubentries (
1252 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1253 rs->sr_text = "subentries control specified multiple times";
1254 return LDAP_PROTOCOL_ERROR;
1257 /* FIXME: should use BER library */
1258 if( ( ctrl->ldctl_value.bv_len != 3 )
1259 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1260 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1262 rs->sr_text = "subentries control value encoding is bogus";
1263 return LDAP_PROTOCOL_ERROR;
1266 op->o_subentries = ctrl->ldctl_iscritical
1267 ? SLAP_CONTROL_CRITICAL
1268 : SLAP_CONTROL_NONCRITICAL;
1270 if (ctrl->ldctl_value.bv_val[2]) {
1271 set_subentries_visibility( op );
1274 return LDAP_SUCCESS;
1278 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1279 static int parsePermissiveModify (
1284 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1285 rs->sr_text = "permissiveModify control specified multiple times";
1286 return LDAP_PROTOCOL_ERROR;
1289 if ( ctrl->ldctl_value.bv_len ) {
1290 rs->sr_text = "permissiveModify control value not empty";
1291 return LDAP_PROTOCOL_ERROR;
1294 op->o_permissive_modify = ctrl->ldctl_iscritical
1295 ? SLAP_CONTROL_CRITICAL
1296 : SLAP_CONTROL_NONCRITICAL;
1298 return LDAP_SUCCESS;
1302 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1303 static int parseDomainScope (
1308 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1309 rs->sr_text = "domainScope control specified multiple times";
1310 return LDAP_PROTOCOL_ERROR;
1313 if ( ctrl->ldctl_value.bv_len ) {
1314 rs->sr_text = "domainScope control value not empty";
1315 return LDAP_PROTOCOL_ERROR;
1318 op->o_domain_scope = ctrl->ldctl_iscritical
1319 ? SLAP_CONTROL_CRITICAL
1320 : SLAP_CONTROL_NONCRITICAL;
1322 return LDAP_SUCCESS;
1326 #ifdef SLAP_CONTROL_X_TREE_DELETE
1327 static int parseTreeDelete (
1332 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1333 rs->sr_text = "treeDelete control specified multiple times";
1334 return LDAP_PROTOCOL_ERROR;
1337 if ( ctrl->ldctl_value.bv_len ) {
1338 rs->sr_text = "treeDelete control value not empty";
1339 return LDAP_PROTOCOL_ERROR;
1342 op->o_tree_delete = ctrl->ldctl_iscritical
1343 ? SLAP_CONTROL_CRITICAL
1344 : SLAP_CONTROL_NONCRITICAL;
1346 return LDAP_SUCCESS;
1350 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
1351 static int parseSearchOptions (
1357 ber_int_t search_flags;
1360 if ( ctrl->ldctl_value.bv_len == 0 ) {
1361 rs->sr_text = "searchOptions control value not empty";
1362 return LDAP_PROTOCOL_ERROR;
1365 ber = ber_init( &ctrl->ldctl_value );
1367 rs->sr_text = "internal error";
1371 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1372 rs->sr_text = "searchOptions control decoding error";
1373 return LDAP_PROTOCOL_ERROR;
1376 (void) ber_free( ber, 1 );
1378 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1379 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1380 rs->sr_text = "searchOptions control specified multiple times "
1381 "or with domainScope control";
1382 return LDAP_PROTOCOL_ERROR;
1385 op->o_domain_scope = ctrl->ldctl_iscritical
1386 ? SLAP_CONTROL_CRITICAL
1387 : SLAP_CONTROL_NONCRITICAL;
1390 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1391 /* Other search flags not recognised so far,
1393 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1395 rs->sr_text = "searchOptions contained unrecongized flag";
1396 return LDAP_UNWILLING_TO_PERFORM;
1399 return LDAP_SUCCESS;