2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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 parseDontUseCopy;
33 static SLAP_CTRL_PARSE_FN parseManageDIT;
35 static SLAP_CTRL_PARSE_FN parseManageDSAit;
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; /* input */
69 BerVarray sc_extendedopsbv; /* run-time use */
71 /* Control parsing callback */
72 SLAP_CTRL_PARSE_FN *sc_parse;
74 LDAP_SLIST_ENTRY(slap_control) sc_next;
77 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
78 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
81 * all known request control OIDs should be added to this list
84 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
85 * addresses an undefined control; this allows to safely test for
86 * well known controls even if they are not registered, e.g. if
87 * they get moved to modules. An example is sc_LDAPsync, which
88 * is implemented in the syncprov overlay and thus, if configured
89 * as dynamic module, may not be registered. One side effect is that
90 * slap_known_controls[0] == NULL, so it should always be used
92 * FIXME: should we define the "undefined control" oid?
94 char *slap_known_controls[SLAP_MAX_CIDS+1];
95 static int num_known_controls = 1;
97 static char *proxy_authz_extops[] = {
98 LDAP_EXOP_MODIFY_PASSWD,
104 static char *manageDSAit_extops[] = {
109 static struct slap_control control_defs[] = {
110 { LDAP_CONTROL_ASSERT,
111 (int)offsetof(struct slap_control_ids, sc_assert),
112 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME|
113 SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
115 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
116 { LDAP_CONTROL_PRE_READ,
117 (int)offsetof(struct slap_control_ids, sc_preRead),
118 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
120 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
121 { LDAP_CONTROL_POST_READ,
122 (int)offsetof(struct slap_control_ids, sc_postRead),
123 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
125 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
126 { LDAP_CONTROL_VALUESRETURNFILTER,
127 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
128 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
130 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
131 { LDAP_CONTROL_PAGEDRESULTS,
132 (int)offsetof(struct slap_control_ids, sc_pagedResults),
135 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
137 { LDAP_CONTROL_SORTREQUEST,
138 (int)offsetof(struct slap_control_ids, sc_sortedResults),
139 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
141 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
143 { LDAP_CONTROL_X_DOMAIN_SCOPE,
144 (int)offsetof(struct slap_control_ids, sc_domainScope),
145 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
147 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
148 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
149 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
150 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
152 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
153 #ifdef SLAP_CONTROL_X_TREE_DELETE
154 { LDAP_CONTROL_X_TREE_DELETE,
155 (int)offsetof(struct slap_control_ids, sc_treeDelete),
156 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
158 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
160 { LDAP_CONTROL_X_SEARCH_OPTIONS,
161 (int)offsetof(struct slap_control_ids, sc_searchOptions),
162 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
164 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
165 { LDAP_CONTROL_SUBENTRIES,
166 (int)offsetof(struct slap_control_ids, sc_subentries),
169 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
171 (int)offsetof(struct slap_control_ids, sc_noOp),
172 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
174 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
176 { LDAP_CONTROL_DONTUSECOPY,
177 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
178 SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
180 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
181 { LDAP_CONTROL_MANAGEDIT,
182 (int)offsetof(struct slap_control_ids, sc_manageDIT),
183 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
185 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
187 { LDAP_CONTROL_MANAGEDSAIT,
188 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
190 manageDSAit_extops, NULL,
191 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
192 { LDAP_CONTROL_PROXY_AUTHZ,
193 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
194 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
195 proxy_authz_extops, NULL,
196 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
197 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
200 static struct slap_control *
201 find_ctrl( const char *oid );
204 * Register a supported control.
206 * This can be called by an OpenLDAP plugin or, indirectly, by a
207 * SLAPI plugin calling slapi_register_supported_control().
209 * NOTE: if flags == 1 the control is replaced if already registered;
210 * otherwise registering an already registered control is not allowed.
213 register_supported_control2(const char *controloid,
214 slap_mask_t controlmask,
216 SLAP_CTRL_PARSE_FN *controlparsefn,
220 struct slap_control *sc = NULL;
222 BerVarray extendedopsbv = NULL;
224 if ( num_known_controls >= SLAP_MAX_CIDS ) {
225 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
226 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
227 SLAP_MAX_CIDS, 0, 0 );
231 if ( controloid == NULL ) {
232 return LDAP_PARAM_ERROR;
235 /* check if already registered */
236 for ( i = 0; slap_known_controls[ i ]; i++ ) {
237 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
239 Debug( LDAP_DEBUG_TRACE,
240 "Control %s already registered; replacing.\n",
242 /* (find and) replace existing handler */
243 sc = find_ctrl( controloid );
244 assert( sc != NULL );
248 Debug( LDAP_DEBUG_ANY,
249 "Control %s already registered.\n",
251 return LDAP_PARAM_ERROR;
255 /* turn compatible extended operations into bervals */
256 if ( controlexops != NULL ) {
259 for ( i = 0; controlexops[ i ]; i++ );
261 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
262 if ( extendedopsbv == NULL ) {
263 return LDAP_NO_MEMORY;
266 for ( i = 0; controlexops[ i ]; i++ ) {
267 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
272 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
274 return LDAP_NO_MEMORY;
277 sc->sc_oid = ch_strdup( controloid );
278 sc->sc_cid = num_known_controls;
280 /* Update slap_known_controls, too. */
281 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
282 slap_known_controls[num_known_controls++] = NULL;
284 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
285 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
288 if ( sc->sc_extendedopsbv ) {
289 ber_bvarray_free( sc->sc_extendedopsbv );
290 sc->sc_extendedopsbv = NULL;
291 sc->sc_extendedops = NULL;
295 sc->sc_extendedopsbv = extendedopsbv;
296 sc->sc_mask = controlmask;
297 sc->sc_parse = controlparsefn;
299 *controlcid = sc->sc_cid;
306 * One-time initialization of internal controls.
309 slap_controls_init( void )
315 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
316 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
317 rc = register_supported_control( control_defs[i].sc_oid,
318 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
319 control_defs[i].sc_parse, cid );
320 if ( rc != LDAP_SUCCESS ) break;
327 * Free memory associated with list of supported controls.
330 controls_destroy( void )
332 struct slap_control *sc;
334 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
335 sc = LDAP_SLIST_FIRST(&controls_list);
336 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
338 ch_free( sc->sc_oid );
339 if ( sc->sc_extendedopsbv != NULL ) {
340 ber_bvarray_free( sc->sc_extendedopsbv );
347 * Format the supportedControl attribute of the root DSE,
348 * detailing which controls are supported by the directory
352 controls_root_dse_info( Entry *e )
354 AttributeDescription *ad_supportedControl
355 = slap_schema.si_ad_supportedControl;
356 struct berval vals[2];
357 struct slap_control *sc;
359 vals[1].bv_val = NULL;
362 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
363 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
365 vals[0].bv_val = sc->sc_oid;
366 vals[0].bv_len = strlen( sc->sc_oid );
368 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
377 * Return a list of OIDs and operation masks for supported
378 * controls. Used by SLAPI.
381 get_supported_controls(char ***ctrloidsp,
382 slap_mask_t **ctrlmasks)
387 struct slap_control *sc;
391 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
401 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
402 if ( oids == NULL ) {
403 return LDAP_NO_MEMORY;
405 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
406 if ( masks == NULL ) {
408 return LDAP_NO_MEMORY;
413 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
414 oids[n] = ch_strdup( sc->sc_oid );
415 masks[n] = sc->sc_mask;
428 * Find a control given its OID.
430 static struct slap_control *
431 find_ctrl( const char *oid )
433 struct slap_control *sc;
435 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
436 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
445 slap_find_control_id(
449 struct slap_control *ctrl = find_ctrl( oid );
451 if ( cid ) *cid = ctrl->sc_cid;
454 return LDAP_CONTROL_NOT_FOUND;
458 slap_global_control( Operation *op, const char *oid, int *cid )
460 struct slap_control *ctrl = find_ctrl( oid );
462 if ( ctrl == NULL ) {
463 /* should not be reachable */
464 Debug( LDAP_DEBUG_ANY,
465 "slap_global_control: unrecognized control: %s\n",
467 return LDAP_CONTROL_NOT_FOUND;
470 if ( cid ) *cid = ctrl->sc_cid;
472 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
473 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
474 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
476 return LDAP_COMPARE_TRUE;
479 Debug( LDAP_DEBUG_TRACE,
480 "slap_global_control: unavailable control: %s\n",
483 return LDAP_COMPARE_FALSE;
486 void slap_free_ctrls(
488 LDAPControl **ctrls )
492 for (i=0; ctrls[i]; i++) {
493 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
495 op->o_tmpfree( ctrls, op->o_tmpmemctx );
501 LDAPControl *control,
504 struct slap_control *sc;
506 sc = find_ctrl( control->ldctl_oid );
508 /* recognized control */
510 switch( op->o_tag ) {
512 tagmask = SLAP_CTRL_ADD;
515 tagmask = SLAP_CTRL_BIND;
517 case LDAP_REQ_COMPARE:
518 tagmask = SLAP_CTRL_COMPARE;
520 case LDAP_REQ_DELETE:
521 tagmask = SLAP_CTRL_DELETE;
523 case LDAP_REQ_MODIFY:
524 tagmask = SLAP_CTRL_MODIFY;
526 case LDAP_REQ_RENAME:
527 tagmask = SLAP_CTRL_RENAME;
529 case LDAP_REQ_SEARCH:
530 tagmask = SLAP_CTRL_SEARCH;
532 case LDAP_REQ_UNBIND:
533 tagmask = SLAP_CTRL_UNBIND;
535 case LDAP_REQ_ABANDON:
536 tagmask = SLAP_CTRL_ABANDON;
538 case LDAP_REQ_EXTENDED:
540 assert( op->ore_reqoid.bv_val != NULL );
541 if( sc->sc_extendedopsbv != NULL ) {
543 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
544 if( bvmatch( &op->ore_reqoid,
545 &sc->sc_extendedopsbv[i] ) )
554 *text = "controls internal error";
558 if (( sc->sc_mask & tagmask ) == tagmask ) {
559 /* available extension */
562 if( !sc->sc_parse ) {
563 *text = "not yet implemented";
567 rc = sc->sc_parse( op, rs, control );
569 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
573 } else if( control->ldctl_iscritical ) {
574 /* unavailable CRITICAL control */
575 *text = "critical extension is unavailable";
576 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
578 } else if( control->ldctl_iscritical ) {
579 /* unrecognized CRITICAL control */
580 *text = "critical extension is not recognized";
581 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
596 BerElement *ber = op->o_ber;
599 len = ber_pvt_ber_remaining(ber);
603 rs->sr_err = LDAP_SUCCESS;
607 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
608 if( tag == LBER_ERROR ) {
609 rs->sr_err = SLAPD_DISCONNECT;
610 rs->sr_text = "unexpected data in PDU";
616 Debug( LDAP_DEBUG_TRACE,
617 "=> get_ctrls\n", 0, 0, 0 );
619 if( op->o_protocol < LDAP_VERSION3 ) {
620 rs->sr_err = SLAPD_DISCONNECT;
621 rs->sr_text = "controls require LDAPv3";
625 /* one for first control, one for termination */
626 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
629 if( op->ctrls == NULL ) {
630 rs->sr_err = LDAP_NO_MEMORY;
631 rs->sr_text = "no memory";
636 op->o_ctrls[nctrls] = NULL;
638 /* step through each element */
639 for( tag = ber_first_element( ber, &len, &opaque );
641 tag = ber_next_element( ber, &len, opaque ) )
644 LDAPControl **tctrls;
646 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
647 memset(c, 0, sizeof(LDAPControl));
649 /* allocate pointer space for current controls (nctrls)
650 * + this control + extra NULL
652 tctrls = op->o_tmprealloc( op->o_ctrls,
653 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
656 if( tctrls == NULL ) {
658 ldap_controls_free(op->o_ctrls);
661 rs->sr_err = LDAP_NO_MEMORY;
662 rs->sr_text = "no memory";
666 op->o_ctrls = tctrls;
668 op->o_ctrls[nctrls++] = c;
669 op->o_ctrls[nctrls] = NULL;
671 tag = ber_scanf( ber, "{m" /*}*/, &bv );
672 c->ldctl_oid = bv.bv_val;
674 if( tag == LBER_ERROR ) {
675 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
678 slap_free_ctrls( op, op->o_ctrls );
680 rs->sr_err = SLAPD_DISCONNECT;
681 rs->sr_text = "decoding controls error";
684 } else if( c->ldctl_oid == NULL ) {
685 Debug( LDAP_DEBUG_TRACE,
686 "get_ctrls: conn %lu got emtpy OID.\n",
687 op->o_connid, 0, 0 );
689 slap_free_ctrls( op, op->o_ctrls );
691 rs->sr_err = LDAP_PROTOCOL_ERROR;
692 rs->sr_text = "OID field is empty";
696 tag = ber_peek_tag( ber, &len );
698 if( tag == LBER_BOOLEAN ) {
700 tag = ber_scanf( ber, "b", &crit );
702 if( tag == LBER_ERROR ) {
703 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
705 slap_free_ctrls( op, op->o_ctrls );
707 rs->sr_err = SLAPD_DISCONNECT;
708 rs->sr_text = "decoding controls error";
712 c->ldctl_iscritical = (crit != 0);
713 tag = ber_peek_tag( ber, &len );
716 if( tag == LBER_OCTETSTRING ) {
717 tag = ber_scanf( ber, "m", &c->ldctl_value );
719 if( tag == LBER_ERROR ) {
720 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
721 "%s (%scritical): get value failed.\n",
722 op->o_connid, c->ldctl_oid,
723 c->ldctl_iscritical ? "" : "non" );
724 slap_free_ctrls( op, op->o_ctrls );
726 rs->sr_err = SLAPD_DISCONNECT;
727 rs->sr_text = "decoding controls error";
732 Debug( LDAP_DEBUG_TRACE,
733 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
734 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
736 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
737 if ( rs->sr_err != LDAP_SUCCESS ) {
743 Debug( LDAP_DEBUG_TRACE,
744 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
745 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
747 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
748 if( rs->sr_err == SLAPD_DISCONNECT ) {
749 rs->sr_err = LDAP_PROTOCOL_ERROR;
750 send_ldap_disconnect( op, rs );
751 rs->sr_err = SLAPD_DISCONNECT;
753 send_ldap_result( op, rs );
765 BI_chk_controls fnc )
769 switch ( op->o_ctrlflag[ ctrl ] ) {
770 case SLAP_CONTROL_NONCRITICAL:
771 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
772 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
773 slap_known_controls[ ctrl - 1 ] ) == 0 )
780 rs->sr_err = LDAP_OTHER;
788 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
791 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
792 ( i - j ) * sizeof( LDAPControl * ) );
795 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
799 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
801 Debug( LDAP_DEBUG_ANY, "%s: "
802 "non-critical control \"%s\" not supported; stripped.\n",
803 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
806 case SLAP_CONTROL_IGNORED:
807 case SLAP_CONTROL_NONE:
808 rs->sr_err = SLAP_CB_CONTINUE;
811 case SLAP_CONTROL_CRITICAL:
812 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
816 Debug( LDAP_DEBUG_ANY, "%s: "
817 "critical control \"%s\" not supported.\n",
818 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
822 /* handle all cases! */
830 static int parseDontUseCopy (
835 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
836 rs->sr_text = "dontUseCopy control specified multiple times";
837 return LDAP_PROTOCOL_ERROR;
840 if ( ctrl->ldctl_value.bv_len ) {
841 rs->sr_text = "dontUseCopy control value not empty";
842 return LDAP_PROTOCOL_ERROR;
845 if ( ctrl->ldctl_iscritical != SLAP_CONTROL_CRITICAL ) {
846 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
847 return LDAP_PROTOCOL_ERROR;
850 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
854 static int parseManageDIT (
859 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
860 rs->sr_text = "manageDIT control specified multiple times";
861 return LDAP_PROTOCOL_ERROR;
864 if ( ctrl->ldctl_value.bv_len ) {
865 rs->sr_text = "manageDIT control value not empty";
866 return LDAP_PROTOCOL_ERROR;
869 op->o_managedit = ctrl->ldctl_iscritical
870 ? SLAP_CONTROL_CRITICAL
871 : SLAP_CONTROL_NONCRITICAL;
877 static int parseManageDSAit (
882 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
883 rs->sr_text = "manageDSAit control specified multiple times";
884 return LDAP_PROTOCOL_ERROR;
887 if ( ctrl->ldctl_value.bv_len ) {
888 rs->sr_text = "manageDSAit control value not empty";
889 return LDAP_PROTOCOL_ERROR;
892 op->o_managedsait = ctrl->ldctl_iscritical
893 ? SLAP_CONTROL_CRITICAL
894 : SLAP_CONTROL_NONCRITICAL;
899 static int parseProxyAuthz (
905 struct berval dn = BER_BVNULL;
907 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
908 rs->sr_text = "proxy authorization control specified multiple times";
909 return LDAP_PROTOCOL_ERROR;
912 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
913 && BER_BVISEMPTY( &op->o_ndn ) )
915 rs->sr_text = "anonymous proxyAuthz not allowed";
916 return LDAP_PROXY_AUTHZ_FAILURE;
919 op->o_proxy_authz = ctrl->ldctl_iscritical
920 ? SLAP_CONTROL_CRITICAL
921 : SLAP_CONTROL_NONCRITICAL;
923 Debug( LDAP_DEBUG_ARGS,
924 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
926 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
929 if ( ctrl->ldctl_value.bv_len == 0 ) {
930 Debug( LDAP_DEBUG_TRACE,
931 "parseProxyAuthz: conn=%lu anonymous\n",
932 op->o_connid, 0, 0 );
935 if ( !BER_BVISNULL( &op->o_ndn ) ) {
936 op->o_ndn.bv_val[ 0 ] = '\0';
938 op->o_ndn.bv_len = 0;
940 if ( !BER_BVISNULL( &op->o_dn ) ) {
941 op->o_dn.bv_val[ 0 ] = '\0';
948 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
949 NULL, &dn, SLAP_GETDN_AUTHZID );
951 /* FIXME: empty DN in proxyAuthz control should be legal... */
952 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
954 ch_free( dn.bv_val );
956 rs->sr_text = "authzId mapping failed";
957 return LDAP_PROXY_AUTHZ_FAILURE;
960 Debug( LDAP_DEBUG_TRACE,
961 "parseProxyAuthz: conn=%lu \"%s\"\n",
963 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
965 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
968 ch_free( dn.bv_val );
969 rs->sr_text = "not authorized to assume identity";
970 return LDAP_PROXY_AUTHZ_FAILURE;
973 ch_free( op->o_ndn.bv_val );
974 ch_free( op->o_dn.bv_val );
977 * NOTE: since slap_sasl_getdn() returns a normalized dn,
978 * from now on op->o_dn is normalized
981 ber_dupbv( &op->o_dn, &dn );
984 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
985 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
990 static int parseNoOp (
995 if ( op->o_noop != SLAP_CONTROL_NONE ) {
996 rs->sr_text = "noop control specified multiple times";
997 return LDAP_PROTOCOL_ERROR;
1000 if ( ctrl->ldctl_value.bv_len ) {
1001 rs->sr_text = "noop control value not empty";
1002 return LDAP_PROTOCOL_ERROR;
1005 op->o_noop = ctrl->ldctl_iscritical
1006 ? SLAP_CONTROL_CRITICAL
1007 : SLAP_CONTROL_NONCRITICAL;
1009 return LDAP_SUCCESS;
1012 static int parsePagedResults (
1017 int rc = LDAP_SUCCESS;
1021 struct berval cookie = BER_BVNULL;
1022 PagedResultsState *ps;
1024 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1025 rs->sr_text = "paged results control specified multiple times";
1026 return LDAP_PROTOCOL_ERROR;
1029 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1030 rs->sr_text = "paged results control value is empty (or absent)";
1031 return LDAP_PROTOCOL_ERROR;
1034 /* Parse the control value
1035 * realSearchControlValue ::= SEQUENCE {
1036 * size INTEGER (0..maxInt),
1037 * -- requested page size from client
1038 * -- result set size estimate from server
1039 * cookie OCTET STRING
1042 ber = ber_init( &ctrl->ldctl_value );
1043 if ( ber == NULL ) {
1044 rs->sr_text = "internal error";
1048 tag = ber_scanf( ber, "{im}", &size, &cookie );
1050 if ( tag == LBER_ERROR ) {
1051 rs->sr_text = "paged results control could not be decoded";
1052 rc = LDAP_PROTOCOL_ERROR;
1057 rs->sr_text = "paged results control size invalid";
1058 rc = LDAP_PROTOCOL_ERROR;
1062 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1063 *ps = op->o_conn->c_pagedresults_state;
1065 op->o_pagedresults_state = ps;
1067 /* NOTE: according to RFC 2696 3.:
1069 If the page size is greater than or equal to the sizeLimit value, the
1070 server should ignore the control as the request can be satisfied in a
1073 * NOTE: this assumes that the op->ors_slimit be set
1074 * before the controls are parsed.
1077 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1078 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1080 } else if ( ctrl->ldctl_iscritical ) {
1081 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1084 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1088 (void)ber_free( ber, 1 );
1093 static int parseSortedResults (
1098 int rc = LDAP_SUCCESS;
1100 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1101 rs->sr_text = "sorted results control specified multiple times";
1102 return LDAP_PROTOCOL_ERROR;
1105 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1106 rs->sr_text = "sorted results control value is empty (or absent)";
1107 return LDAP_PROTOCOL_ERROR;
1110 /* blow off parsing the value */
1112 op->o_sortedresults = ctrl->ldctl_iscritical
1113 ? SLAP_CONTROL_CRITICAL
1114 : SLAP_CONTROL_NONCRITICAL;
1120 static int parseAssert (
1126 struct berval fstr = BER_BVNULL;
1128 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1129 rs->sr_text = "assert control specified multiple times";
1130 return LDAP_PROTOCOL_ERROR;
1133 if ( ctrl->ldctl_value.bv_len == 0 ) {
1134 rs->sr_text = "assert control value is empty (or absent)";
1135 return LDAP_PROTOCOL_ERROR;
1138 ber = ber_init( &(ctrl->ldctl_value) );
1140 rs->sr_text = "assert control: internal error";
1144 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1146 if( rs->sr_err != LDAP_SUCCESS ) {
1147 if( rs->sr_err == SLAPD_DISCONNECT ) {
1148 rs->sr_err = LDAP_PROTOCOL_ERROR;
1149 send_ldap_disconnect( op, rs );
1150 rs->sr_err = SLAPD_DISCONNECT;
1152 send_ldap_result( op, rs );
1154 if( op->o_assertion != NULL ) {
1155 filter_free_x( op, op->o_assertion );
1161 filter2bv_x( op, op->o_assertion, &fstr );
1163 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1164 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1165 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1168 op->o_assert = ctrl->ldctl_iscritical
1169 ? SLAP_CONTROL_CRITICAL
1170 : SLAP_CONTROL_NONCRITICAL;
1172 rs->sr_err = LDAP_SUCCESS;
1173 return LDAP_SUCCESS;
1176 static int parsePreRead (
1181 ber_len_t siz, off, i;
1182 AttributeName *an = NULL;
1185 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1186 rs->sr_text = "preread control specified multiple times";
1187 return LDAP_PROTOCOL_ERROR;
1190 if ( ctrl->ldctl_value.bv_len == 0 ) {
1191 rs->sr_text = "preread control value is empty (or absent)";
1192 return LDAP_PROTOCOL_ERROR;
1195 ber = ber_init( &(ctrl->ldctl_value) );
1197 rs->sr_text = "preread control: internal error";
1201 siz = sizeof( AttributeName );
1202 off = offsetof( AttributeName, an_name );
1203 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1204 rs->sr_text = "preread control: decoding error";
1205 return LDAP_PROTOCOL_ERROR;
1208 for( i=0; i<siz; i++ ) {
1209 int rc = LDAP_SUCCESS;
1210 const char *dummy = NULL;
1212 an[i].an_desc = NULL;
1214 an[i].an_oc_exclude = 0;
1215 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1216 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1219 : "postread control: unknown attributeType";
1224 op->o_preread = ctrl->ldctl_iscritical
1225 ? SLAP_CONTROL_CRITICAL
1226 : SLAP_CONTROL_NONCRITICAL;
1228 op->o_preread_attrs = an;
1230 rs->sr_err = LDAP_SUCCESS;
1231 return LDAP_SUCCESS;
1234 static int parsePostRead (
1239 ber_len_t siz, off, i;
1240 AttributeName *an = NULL;
1243 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1244 rs->sr_text = "postread control specified multiple times";
1245 return LDAP_PROTOCOL_ERROR;
1248 if ( ctrl->ldctl_value.bv_len == 0 ) {
1249 rs->sr_text = "postread control value is empty (or absent)";
1250 return LDAP_PROTOCOL_ERROR;
1253 ber = ber_init( &(ctrl->ldctl_value) );
1255 rs->sr_text = "postread control: internal error";
1259 siz = sizeof( AttributeName );
1260 off = offsetof( AttributeName, an_name );
1261 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1262 rs->sr_text = "postread control: decoding error";
1263 return LDAP_PROTOCOL_ERROR;
1266 for( i=0; i<siz; i++ ) {
1267 int rc = LDAP_SUCCESS;
1268 const char *dummy = NULL;
1270 an[i].an_desc = NULL;
1272 an[i].an_oc_exclude = 0;
1273 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1274 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1277 : "postread control: unknown attributeType";
1282 op->o_postread = ctrl->ldctl_iscritical
1283 ? SLAP_CONTROL_CRITICAL
1284 : SLAP_CONTROL_NONCRITICAL;
1286 op->o_postread_attrs = an;
1288 rs->sr_err = LDAP_SUCCESS;
1289 return LDAP_SUCCESS;
1292 static int parseValuesReturnFilter (
1298 struct berval fstr = BER_BVNULL;
1300 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1301 rs->sr_text = "valuesReturnFilter control specified multiple times";
1302 return LDAP_PROTOCOL_ERROR;
1305 if ( ctrl->ldctl_value.bv_len == 0 ) {
1306 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1307 return LDAP_PROTOCOL_ERROR;
1310 ber = ber_init( &(ctrl->ldctl_value) );
1312 rs->sr_text = "internal error";
1316 rs->sr_err = get_vrFilter( op, ber,
1317 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1319 if( rs->sr_err != LDAP_SUCCESS ) {
1320 if( rs->sr_err == SLAPD_DISCONNECT ) {
1321 rs->sr_err = LDAP_PROTOCOL_ERROR;
1322 send_ldap_disconnect( op, rs );
1323 rs->sr_err = SLAPD_DISCONNECT;
1325 send_ldap_result( op, rs );
1327 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1331 vrFilter2bv( op, op->o_vrFilter, &fstr );
1334 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1335 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1336 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1339 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1340 ? SLAP_CONTROL_CRITICAL
1341 : SLAP_CONTROL_NONCRITICAL;
1343 rs->sr_err = LDAP_SUCCESS;
1344 return LDAP_SUCCESS;
1347 static int parseSubentries (
1352 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1353 rs->sr_text = "subentries control specified multiple times";
1354 return LDAP_PROTOCOL_ERROR;
1357 /* FIXME: should use BER library */
1358 if( ( ctrl->ldctl_value.bv_len != 3 )
1359 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1360 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1362 rs->sr_text = "subentries control value encoding is bogus";
1363 return LDAP_PROTOCOL_ERROR;
1366 op->o_subentries = ctrl->ldctl_iscritical
1367 ? SLAP_CONTROL_CRITICAL
1368 : SLAP_CONTROL_NONCRITICAL;
1370 if (ctrl->ldctl_value.bv_val[2]) {
1371 set_subentries_visibility( op );
1374 return LDAP_SUCCESS;
1377 static int parsePermissiveModify (
1382 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1383 rs->sr_text = "permissiveModify control specified multiple times";
1384 return LDAP_PROTOCOL_ERROR;
1387 if ( ctrl->ldctl_value.bv_len ) {
1388 rs->sr_text = "permissiveModify control value not empty";
1389 return LDAP_PROTOCOL_ERROR;
1392 op->o_permissive_modify = ctrl->ldctl_iscritical
1393 ? SLAP_CONTROL_CRITICAL
1394 : SLAP_CONTROL_NONCRITICAL;
1396 return LDAP_SUCCESS;
1399 static int parseDomainScope (
1404 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1405 rs->sr_text = "domainScope control specified multiple times";
1406 return LDAP_PROTOCOL_ERROR;
1409 if ( ctrl->ldctl_value.bv_len ) {
1410 rs->sr_text = "domainScope control value not empty";
1411 return LDAP_PROTOCOL_ERROR;
1414 op->o_domain_scope = ctrl->ldctl_iscritical
1415 ? SLAP_CONTROL_CRITICAL
1416 : SLAP_CONTROL_NONCRITICAL;
1418 return LDAP_SUCCESS;
1421 #ifdef SLAP_CONTROL_X_TREE_DELETE
1422 static int parseTreeDelete (
1427 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1428 rs->sr_text = "treeDelete control specified multiple times";
1429 return LDAP_PROTOCOL_ERROR;
1432 if ( ctrl->ldctl_value.bv_len ) {
1433 rs->sr_text = "treeDelete control value not empty";
1434 return LDAP_PROTOCOL_ERROR;
1437 op->o_tree_delete = ctrl->ldctl_iscritical
1438 ? SLAP_CONTROL_CRITICAL
1439 : SLAP_CONTROL_NONCRITICAL;
1441 return LDAP_SUCCESS;
1445 static int parseSearchOptions (
1451 ber_int_t search_flags;
1454 if ( ctrl->ldctl_value.bv_len == 0 ) {
1455 rs->sr_text = "searchOptions control value is empty (or absent)";
1456 return LDAP_PROTOCOL_ERROR;
1459 ber = ber_init( &ctrl->ldctl_value );
1461 rs->sr_text = "internal error";
1465 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1466 rs->sr_text = "searchOptions control decoding error";
1467 return LDAP_PROTOCOL_ERROR;
1470 (void) ber_free( ber, 1 );
1472 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1473 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1474 rs->sr_text = "searchOptions control specified multiple times "
1475 "or with domainScope control";
1476 return LDAP_PROTOCOL_ERROR;
1479 op->o_domain_scope = ctrl->ldctl_iscritical
1480 ? SLAP_CONTROL_CRITICAL
1481 : SLAP_CONTROL_NONCRITICAL;
1484 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1485 /* Other search flags not recognised so far,
1487 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1489 rs->sr_text = "searchOptions contained unrecognized flag";
1490 return LDAP_UNWILLING_TO_PERFORM;
1493 return LDAP_SUCCESS;