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,
103 static char *manageDSAit_extops[] = {
107 static struct slap_control control_defs[] = {
108 { LDAP_CONTROL_ASSERT,
109 (int)offsetof(struct slap_control_ids, sc_assert),
110 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME|
111 SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
113 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
114 { LDAP_CONTROL_PRE_READ,
115 (int)offsetof(struct slap_control_ids, sc_preRead),
116 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
118 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
119 { LDAP_CONTROL_POST_READ,
120 (int)offsetof(struct slap_control_ids, sc_postRead),
121 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
123 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
124 { LDAP_CONTROL_VALUESRETURNFILTER,
125 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
126 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
128 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
129 { LDAP_CONTROL_PAGEDRESULTS,
130 (int)offsetof(struct slap_control_ids, sc_pagedResults),
133 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
135 { LDAP_CONTROL_SORTREQUEST,
136 (int)offsetof(struct slap_control_ids, sc_sortedResults),
137 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
139 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
141 { LDAP_CONTROL_X_DOMAIN_SCOPE,
142 (int)offsetof(struct slap_control_ids, sc_domainScope),
143 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
145 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
146 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
147 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
148 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
150 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
151 #ifdef SLAP_CONTROL_X_TREE_DELETE
152 { LDAP_CONTROL_X_TREE_DELETE,
153 (int)offsetof(struct slap_control_ids, sc_treeDelete),
154 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
156 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
158 { LDAP_CONTROL_X_SEARCH_OPTIONS,
159 (int)offsetof(struct slap_control_ids, sc_searchOptions),
160 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
162 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
163 { LDAP_CONTROL_SUBENTRIES,
164 (int)offsetof(struct slap_control_ids, sc_subentries),
167 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
169 (int)offsetof(struct slap_control_ids, sc_noOp),
170 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
172 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
174 { LDAP_CONTROL_DONTUSECOPY,
175 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
176 SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
178 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
179 { LDAP_CONTROL_MANAGEDIT,
180 (int)offsetof(struct slap_control_ids, sc_manageDIT),
181 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
183 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
185 { LDAP_CONTROL_MANAGEDSAIT,
186 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
188 manageDSAit_extops, NULL,
189 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
190 { LDAP_CONTROL_PROXY_AUTHZ,
191 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
192 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
193 proxy_authz_extops, NULL,
194 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
195 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
199 * Register a supported control.
201 * This can be called by an OpenLDAP plugin or, indirectly, by a
202 * SLAPI plugin calling slapi_register_supported_control().
205 register_supported_control(const char *controloid,
206 slap_mask_t controlmask,
208 SLAP_CTRL_PARSE_FN *controlparsefn,
211 struct slap_control *sc;
214 if ( num_known_controls >= SLAP_MAX_CIDS ) {
215 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
216 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
217 SLAP_MAX_CIDS, 0, 0 );
221 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
223 /* sanity check - should never happen */
224 for ( i = 0; slap_known_controls[ i ]; i++ ) {
225 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
226 Debug( LDAP_DEBUG_ANY,
227 "Control %s already registered.\n",
229 return LDAP_PARAM_ERROR;
233 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
234 if ( sc == NULL ) return LDAP_NO_MEMORY;
236 sc->sc_oid = ch_strdup( controloid );
237 sc->sc_mask = controlmask;
238 if ( controlexops != NULL ) {
241 for ( i = 0; controlexops[ i ]; i++ );
243 sc->sc_extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
244 if ( sc->sc_extendedopsbv == NULL ) {
246 return LDAP_NO_MEMORY;
249 for ( i = 0; controlexops[ i ]; i++ ) {
250 ber_str2bv( controlexops[ i ], 0, 1, &sc->sc_extendedopsbv[ i ] );
254 sc->sc_extendedopsbv = NULL;
256 sc->sc_extendedops = NULL;
257 sc->sc_parse = controlparsefn;
259 if ( controlcid ) *controlcid = num_known_controls;
260 sc->sc_cid = num_known_controls;
262 /* Update slap_known_controls, too. */
263 slap_known_controls[num_known_controls-1] = sc->sc_oid;
264 slap_known_controls[num_known_controls++] = NULL;
266 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
267 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
272 * One-time initialization of internal controls.
275 slap_controls_init( void )
281 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
282 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
283 rc = register_supported_control( control_defs[i].sc_oid,
284 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
285 control_defs[i].sc_parse, cid );
286 if ( rc != LDAP_SUCCESS ) break;
293 * Free memory associated with list of supported controls.
296 controls_destroy( void )
298 struct slap_control *sc;
300 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
301 sc = LDAP_SLIST_FIRST(&controls_list);
302 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
304 ch_free( sc->sc_oid );
305 if ( sc->sc_extendedopsbv != NULL ) {
306 ber_bvarray_free( sc->sc_extendedopsbv );
313 * Format the supportedControl attribute of the root DSE,
314 * detailing which controls are supported by the directory
318 controls_root_dse_info( Entry *e )
320 AttributeDescription *ad_supportedControl
321 = slap_schema.si_ad_supportedControl;
322 struct berval vals[2];
323 struct slap_control *sc;
325 vals[1].bv_val = NULL;
328 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
329 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
331 vals[0].bv_val = sc->sc_oid;
332 vals[0].bv_len = strlen( sc->sc_oid );
334 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
343 * Return a list of OIDs and operation masks for supported
344 * controls. Used by SLAPI.
347 get_supported_controls(char ***ctrloidsp,
348 slap_mask_t **ctrlmasks)
353 struct slap_control *sc;
357 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
367 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
368 if ( oids == NULL ) {
369 return LDAP_NO_MEMORY;
371 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
372 if ( masks == NULL ) {
374 return LDAP_NO_MEMORY;
379 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
380 oids[n] = ch_strdup( sc->sc_oid );
381 masks[n] = sc->sc_mask;
394 * Find a control given its OID.
396 static struct slap_control *
397 find_ctrl( const char *oid )
399 struct slap_control *sc;
401 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
402 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
411 slap_find_control_id(
415 struct slap_control *ctrl = find_ctrl( oid );
417 if ( cid ) *cid = ctrl->sc_cid;
420 return LDAP_CONTROL_NOT_FOUND;
424 slap_global_control( Operation *op, const char *oid, int *cid )
426 struct slap_control *ctrl = find_ctrl( oid );
428 if ( ctrl == NULL ) {
429 /* should not be reachable */
430 Debug( LDAP_DEBUG_ANY,
431 "slap_global_control: unrecognized control: %s\n",
433 return LDAP_CONTROL_NOT_FOUND;
436 if ( cid ) *cid = ctrl->sc_cid;
438 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
439 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
440 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
442 return LDAP_COMPARE_TRUE;
445 Debug( LDAP_DEBUG_TRACE,
446 "slap_global_control: unavailable control: %s\n",
449 return LDAP_COMPARE_FALSE;
452 void slap_free_ctrls(
454 LDAPControl **ctrls )
458 for (i=0; ctrls[i]; i++) {
459 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
461 op->o_tmpfree( ctrls, op->o_tmpmemctx );
467 LDAPControl *control,
470 struct slap_control *sc;
472 sc = find_ctrl( control->ldctl_oid );
474 /* recognized control */
476 switch( op->o_tag ) {
478 tagmask = SLAP_CTRL_ADD;
481 tagmask = SLAP_CTRL_BIND;
483 case LDAP_REQ_COMPARE:
484 tagmask = SLAP_CTRL_COMPARE;
486 case LDAP_REQ_DELETE:
487 tagmask = SLAP_CTRL_DELETE;
489 case LDAP_REQ_MODIFY:
490 tagmask = SLAP_CTRL_MODIFY;
492 case LDAP_REQ_RENAME:
493 tagmask = SLAP_CTRL_RENAME;
495 case LDAP_REQ_SEARCH:
496 tagmask = SLAP_CTRL_SEARCH;
498 case LDAP_REQ_UNBIND:
499 tagmask = SLAP_CTRL_UNBIND;
501 case LDAP_REQ_ABANDON:
502 tagmask = SLAP_CTRL_ABANDON;
504 case LDAP_REQ_EXTENDED:
506 assert( op->ore_reqoid.bv_val != NULL );
507 if( sc->sc_extendedopsbv != NULL ) {
509 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
510 if( bvmatch( &op->ore_reqoid,
511 &sc->sc_extendedopsbv[i] ) )
520 *text = "controls internal error";
524 if (( sc->sc_mask & tagmask ) == tagmask ) {
525 /* available extension */
528 if( !sc->sc_parse ) {
529 *text = "not yet implemented";
533 rc = sc->sc_parse( op, rs, control );
535 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
539 } else if( control->ldctl_iscritical ) {
540 /* unavailable CRITICAL control */
541 *text = "critical extension is unavailable";
542 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
544 } else if( control->ldctl_iscritical ) {
545 /* unrecognized CRITICAL control */
546 *text = "critical extension is not recognized";
547 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
562 BerElement *ber = op->o_ber;
565 len = ber_pvt_ber_remaining(ber);
569 rs->sr_err = LDAP_SUCCESS;
573 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
574 if( tag == LBER_ERROR ) {
575 rs->sr_err = SLAPD_DISCONNECT;
576 rs->sr_text = "unexpected data in PDU";
582 Debug( LDAP_DEBUG_TRACE,
583 "=> get_ctrls\n", 0, 0, 0 );
585 if( op->o_protocol < LDAP_VERSION3 ) {
586 rs->sr_err = SLAPD_DISCONNECT;
587 rs->sr_text = "controls require LDAPv3";
591 /* one for first control, one for termination */
592 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
595 if( op->ctrls == NULL ) {
596 rs->sr_err = LDAP_NO_MEMORY;
597 rs->sr_text = "no memory";
602 op->o_ctrls[nctrls] = NULL;
604 /* step through each element */
605 for( tag = ber_first_element( ber, &len, &opaque );
607 tag = ber_next_element( ber, &len, opaque ) )
610 LDAPControl **tctrls;
612 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
613 memset(c, 0, sizeof(LDAPControl));
615 /* allocate pointer space for current controls (nctrls)
616 * + this control + extra NULL
618 tctrls = op->o_tmprealloc( op->o_ctrls,
619 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
622 if( tctrls == NULL ) {
624 ldap_controls_free(op->o_ctrls);
627 rs->sr_err = LDAP_NO_MEMORY;
628 rs->sr_text = "no memory";
632 op->o_ctrls = tctrls;
634 op->o_ctrls[nctrls++] = c;
635 op->o_ctrls[nctrls] = NULL;
637 tag = ber_scanf( ber, "{m" /*}*/, &bv );
638 c->ldctl_oid = bv.bv_val;
640 if( tag == LBER_ERROR ) {
641 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
644 slap_free_ctrls( op, op->o_ctrls );
646 rs->sr_err = SLAPD_DISCONNECT;
647 rs->sr_text = "decoding controls error";
650 } else if( c->ldctl_oid == NULL ) {
651 Debug( LDAP_DEBUG_TRACE,
652 "get_ctrls: conn %lu got emtpy OID.\n",
653 op->o_connid, 0, 0 );
655 slap_free_ctrls( op, op->o_ctrls );
657 rs->sr_err = LDAP_PROTOCOL_ERROR;
658 rs->sr_text = "OID field is empty";
662 tag = ber_peek_tag( ber, &len );
664 if( tag == LBER_BOOLEAN ) {
666 tag = ber_scanf( ber, "b", &crit );
668 if( tag == LBER_ERROR ) {
669 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
671 slap_free_ctrls( op, op->o_ctrls );
673 rs->sr_err = SLAPD_DISCONNECT;
674 rs->sr_text = "decoding controls error";
678 c->ldctl_iscritical = (crit != 0);
679 tag = ber_peek_tag( ber, &len );
682 if( tag == LBER_OCTETSTRING ) {
683 tag = ber_scanf( ber, "m", &c->ldctl_value );
685 if( tag == LBER_ERROR ) {
686 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
687 "%s (%scritical): get value failed.\n",
688 op->o_connid, c->ldctl_oid,
689 c->ldctl_iscritical ? "" : "non" );
690 slap_free_ctrls( op, op->o_ctrls );
692 rs->sr_err = SLAPD_DISCONNECT;
693 rs->sr_text = "decoding controls error";
698 Debug( LDAP_DEBUG_TRACE,
699 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
700 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
702 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
703 if ( rs->sr_err != LDAP_SUCCESS ) {
709 Debug( LDAP_DEBUG_TRACE,
710 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
711 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
713 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
714 if( rs->sr_err == SLAPD_DISCONNECT ) {
715 rs->sr_err = LDAP_PROTOCOL_ERROR;
716 send_ldap_disconnect( op, rs );
717 rs->sr_err = SLAPD_DISCONNECT;
719 send_ldap_result( op, rs );
731 BI_chk_controls fnc )
735 switch ( op->o_ctrlflag[ ctrl ] ) {
736 case SLAP_CONTROL_NONCRITICAL:
737 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
738 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
739 slap_known_controls[ ctrl - 1 ] ) == 0 )
746 rs->sr_err = LDAP_OTHER;
754 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
757 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
758 ( i - j ) * sizeof( LDAPControl * ) );
761 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
765 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
767 Debug( LDAP_DEBUG_ANY, "%s: "
768 "non-critical control \"%s\" not supported; stripped.\n",
769 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
772 case SLAP_CONTROL_IGNORED:
773 case SLAP_CONTROL_NONE:
774 rs->sr_err = SLAP_CB_CONTINUE;
777 case SLAP_CONTROL_CRITICAL:
778 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
782 Debug( LDAP_DEBUG_ANY, "%s: "
783 "critical control \"%s\" not supported.\n",
784 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
788 /* handle all cases! */
796 static int parseDontUseCopy (
801 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
802 rs->sr_text = "dontUseCopy control specified multiple times";
803 return LDAP_PROTOCOL_ERROR;
806 if ( ctrl->ldctl_value.bv_len ) {
807 rs->sr_text = "dontUseCopy control value not empty";
808 return LDAP_PROTOCOL_ERROR;
811 if ( ctrl->ldctl_iscritical != SLAP_CONTROL_CRITICAL ) {
812 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
813 return LDAP_PROTOCOL_ERROR;
816 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
820 static int parseManageDIT (
825 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
826 rs->sr_text = "manageDIT control specified multiple times";
827 return LDAP_PROTOCOL_ERROR;
830 if ( ctrl->ldctl_value.bv_len ) {
831 rs->sr_text = "manageDIT control value not empty";
832 return LDAP_PROTOCOL_ERROR;
835 op->o_managedit = ctrl->ldctl_iscritical
836 ? SLAP_CONTROL_CRITICAL
837 : SLAP_CONTROL_NONCRITICAL;
843 static int parseManageDSAit (
848 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
849 rs->sr_text = "manageDSAit control specified multiple times";
850 return LDAP_PROTOCOL_ERROR;
853 if ( ctrl->ldctl_value.bv_len ) {
854 rs->sr_text = "manageDSAit control value not empty";
855 return LDAP_PROTOCOL_ERROR;
858 op->o_managedsait = ctrl->ldctl_iscritical
859 ? SLAP_CONTROL_CRITICAL
860 : SLAP_CONTROL_NONCRITICAL;
865 static int parseProxyAuthz (
871 struct berval dn = BER_BVNULL;
873 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
874 rs->sr_text = "proxy authorization control specified multiple times";
875 return LDAP_PROTOCOL_ERROR;
878 op->o_proxy_authz = ctrl->ldctl_iscritical
879 ? SLAP_CONTROL_CRITICAL
880 : SLAP_CONTROL_NONCRITICAL;
882 Debug( LDAP_DEBUG_ARGS,
883 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
885 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
888 if ( ctrl->ldctl_value.bv_len == 0 ) {
889 Debug( LDAP_DEBUG_TRACE,
890 "parseProxyAuthz: conn=%lu anonymous\n",
891 op->o_connid, 0, 0 );
894 op->o_ndn.bv_val[ 0 ] = '\0';
895 op->o_ndn.bv_len = 0;
897 op->o_dn.bv_val[ 0 ] = '\0';
903 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
904 NULL, &dn, SLAP_GETDN_AUTHZID );
906 /* FIXME: empty DN in proxyAuthz control should be legal... */
907 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
909 ch_free( dn.bv_val );
911 rs->sr_text = "authzId mapping failed";
912 return LDAP_PROXY_AUTHZ_FAILURE;
915 Debug( LDAP_DEBUG_TRACE,
916 "parseProxyAuthz: conn=%lu \"%s\"\n",
918 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
920 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
923 ch_free( dn.bv_val );
924 rs->sr_text = "not authorized to assume identity";
925 return LDAP_PROXY_AUTHZ_FAILURE;
928 ch_free( op->o_ndn.bv_val );
929 ch_free( op->o_dn.bv_val );
932 * NOTE: since slap_sasl_getdn() returns a normalized dn,
933 * from now on op->o_dn is normalized
936 ber_dupbv( &op->o_dn, &dn );
939 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
940 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
945 static int parseNoOp (
950 if ( op->o_noop != SLAP_CONTROL_NONE ) {
951 rs->sr_text = "noop control specified multiple times";
952 return LDAP_PROTOCOL_ERROR;
955 if ( ctrl->ldctl_value.bv_len ) {
956 rs->sr_text = "noop control value not empty";
957 return LDAP_PROTOCOL_ERROR;
960 op->o_noop = ctrl->ldctl_iscritical
961 ? SLAP_CONTROL_CRITICAL
962 : SLAP_CONTROL_NONCRITICAL;
967 static int parsePagedResults (
972 int rc = LDAP_SUCCESS;
976 struct berval cookie = BER_BVNULL;
977 PagedResultsState *ps;
979 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
980 rs->sr_text = "paged results control specified multiple times";
981 return LDAP_PROTOCOL_ERROR;
984 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
985 rs->sr_text = "paged results control value is empty (or absent)";
986 return LDAP_PROTOCOL_ERROR;
989 /* Parse the control value
990 * realSearchControlValue ::= SEQUENCE {
991 * size INTEGER (0..maxInt),
992 * -- requested page size from client
993 * -- result set size estimate from server
994 * cookie OCTET STRING
997 ber = ber_init( &ctrl->ldctl_value );
999 rs->sr_text = "internal error";
1003 tag = ber_scanf( ber, "{im}", &size, &cookie );
1005 if ( tag == LBER_ERROR ) {
1006 rs->sr_text = "paged results control could not be decoded";
1007 rc = LDAP_PROTOCOL_ERROR;
1012 rs->sr_text = "paged results control size invalid";
1013 rc = LDAP_PROTOCOL_ERROR;
1017 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1018 *ps = op->o_conn->c_pagedresults_state;
1020 op->o_pagedresults_state = ps;
1022 /* NOTE: according to RFC 2696 3.:
1024 If the page size is greater than or equal to the sizeLimit value, the
1025 server should ignore the control as the request can be satisfied in a
1028 * NOTE: this assumes that the op->ors_slimit be set
1029 * before the controls are parsed.
1032 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1033 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1035 } else if ( ctrl->ldctl_iscritical ) {
1036 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1039 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1043 (void)ber_free( ber, 1 );
1048 static int parseSortedResults (
1053 int rc = LDAP_SUCCESS;
1055 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1056 rs->sr_text = "sorted results control specified multiple times";
1057 return LDAP_PROTOCOL_ERROR;
1060 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1061 rs->sr_text = "sorted results control value is empty (or absent)";
1062 return LDAP_PROTOCOL_ERROR;
1065 /* blow off parsing the value */
1067 op->o_sortedresults = ctrl->ldctl_iscritical
1068 ? SLAP_CONTROL_CRITICAL
1069 : SLAP_CONTROL_NONCRITICAL;
1075 static int parseAssert (
1081 struct berval fstr = BER_BVNULL;
1083 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1084 rs->sr_text = "assert control specified multiple times";
1085 return LDAP_PROTOCOL_ERROR;
1088 if ( ctrl->ldctl_value.bv_len == 0 ) {
1089 rs->sr_text = "assert control value is empty (or absent)";
1090 return LDAP_PROTOCOL_ERROR;
1093 ber = ber_init( &(ctrl->ldctl_value) );
1095 rs->sr_text = "assert control: internal error";
1099 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1101 if( rs->sr_err != LDAP_SUCCESS ) {
1102 if( rs->sr_err == SLAPD_DISCONNECT ) {
1103 rs->sr_err = LDAP_PROTOCOL_ERROR;
1104 send_ldap_disconnect( op, rs );
1105 rs->sr_err = SLAPD_DISCONNECT;
1107 send_ldap_result( op, rs );
1109 if( op->o_assertion != NULL ) {
1110 filter_free_x( op, op->o_assertion );
1116 filter2bv_x( op, op->o_assertion, &fstr );
1118 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1119 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1120 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1123 op->o_assert = ctrl->ldctl_iscritical
1124 ? SLAP_CONTROL_CRITICAL
1125 : SLAP_CONTROL_NONCRITICAL;
1127 rs->sr_err = LDAP_SUCCESS;
1128 return LDAP_SUCCESS;
1131 static int parsePreRead (
1136 ber_len_t siz, off, i;
1137 AttributeName *an = NULL;
1140 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1141 rs->sr_text = "preread control specified multiple times";
1142 return LDAP_PROTOCOL_ERROR;
1145 if ( ctrl->ldctl_value.bv_len == 0 ) {
1146 rs->sr_text = "preread control value is empty (or absent)";
1147 return LDAP_PROTOCOL_ERROR;
1150 ber = ber_init( &(ctrl->ldctl_value) );
1152 rs->sr_text = "preread control: internal error";
1156 siz = sizeof( AttributeName );
1157 off = offsetof( AttributeName, an_name );
1158 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1159 rs->sr_text = "preread control: decoding error";
1160 return LDAP_PROTOCOL_ERROR;
1163 for( i=0; i<siz; i++ ) {
1164 int rc = LDAP_SUCCESS;
1165 const char *dummy = NULL;
1167 an[i].an_desc = NULL;
1169 an[i].an_oc_exclude = 0;
1170 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1171 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1174 : "postread control: unknown attributeType";
1179 op->o_preread = ctrl->ldctl_iscritical
1180 ? SLAP_CONTROL_CRITICAL
1181 : SLAP_CONTROL_NONCRITICAL;
1183 op->o_preread_attrs = an;
1185 rs->sr_err = LDAP_SUCCESS;
1186 return LDAP_SUCCESS;
1189 static int parsePostRead (
1194 ber_len_t siz, off, i;
1195 AttributeName *an = NULL;
1198 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1199 rs->sr_text = "postread control specified multiple times";
1200 return LDAP_PROTOCOL_ERROR;
1203 if ( ctrl->ldctl_value.bv_len == 0 ) {
1204 rs->sr_text = "postread control value is empty (or absent)";
1205 return LDAP_PROTOCOL_ERROR;
1208 ber = ber_init( &(ctrl->ldctl_value) );
1210 rs->sr_text = "postread control: internal error";
1214 siz = sizeof( AttributeName );
1215 off = offsetof( AttributeName, an_name );
1216 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1217 rs->sr_text = "postread control: decoding error";
1218 return LDAP_PROTOCOL_ERROR;
1221 for( i=0; i<siz; i++ ) {
1222 int rc = LDAP_SUCCESS;
1223 const char *dummy = NULL;
1225 an[i].an_desc = NULL;
1227 an[i].an_oc_exclude = 0;
1228 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1229 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1232 : "postread control: unknown attributeType";
1237 op->o_postread = ctrl->ldctl_iscritical
1238 ? SLAP_CONTROL_CRITICAL
1239 : SLAP_CONTROL_NONCRITICAL;
1241 op->o_postread_attrs = an;
1243 rs->sr_err = LDAP_SUCCESS;
1244 return LDAP_SUCCESS;
1247 static int parseValuesReturnFilter (
1253 struct berval fstr = BER_BVNULL;
1255 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1256 rs->sr_text = "valuesReturnFilter control specified multiple times";
1257 return LDAP_PROTOCOL_ERROR;
1260 if ( ctrl->ldctl_value.bv_len == 0 ) {
1261 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1262 return LDAP_PROTOCOL_ERROR;
1265 ber = ber_init( &(ctrl->ldctl_value) );
1267 rs->sr_text = "internal error";
1271 rs->sr_err = get_vrFilter( op, ber,
1272 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1274 if( rs->sr_err != LDAP_SUCCESS ) {
1275 if( rs->sr_err == SLAPD_DISCONNECT ) {
1276 rs->sr_err = LDAP_PROTOCOL_ERROR;
1277 send_ldap_disconnect( op, rs );
1278 rs->sr_err = SLAPD_DISCONNECT;
1280 send_ldap_result( op, rs );
1282 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1286 vrFilter2bv( op, op->o_vrFilter, &fstr );
1289 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1290 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1291 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1294 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1295 ? SLAP_CONTROL_CRITICAL
1296 : SLAP_CONTROL_NONCRITICAL;
1298 rs->sr_err = LDAP_SUCCESS;
1299 return LDAP_SUCCESS;
1302 static int parseSubentries (
1307 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1308 rs->sr_text = "subentries control specified multiple times";
1309 return LDAP_PROTOCOL_ERROR;
1312 /* FIXME: should use BER library */
1313 if( ( ctrl->ldctl_value.bv_len != 3 )
1314 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1315 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1317 rs->sr_text = "subentries control value encoding is bogus";
1318 return LDAP_PROTOCOL_ERROR;
1321 op->o_subentries = ctrl->ldctl_iscritical
1322 ? SLAP_CONTROL_CRITICAL
1323 : SLAP_CONTROL_NONCRITICAL;
1325 if (ctrl->ldctl_value.bv_val[2]) {
1326 set_subentries_visibility( op );
1329 return LDAP_SUCCESS;
1332 static int parsePermissiveModify (
1337 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1338 rs->sr_text = "permissiveModify control specified multiple times";
1339 return LDAP_PROTOCOL_ERROR;
1342 if ( ctrl->ldctl_value.bv_len ) {
1343 rs->sr_text = "permissiveModify control value not empty";
1344 return LDAP_PROTOCOL_ERROR;
1347 op->o_permissive_modify = ctrl->ldctl_iscritical
1348 ? SLAP_CONTROL_CRITICAL
1349 : SLAP_CONTROL_NONCRITICAL;
1351 return LDAP_SUCCESS;
1354 static int parseDomainScope (
1359 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1360 rs->sr_text = "domainScope control specified multiple times";
1361 return LDAP_PROTOCOL_ERROR;
1364 if ( ctrl->ldctl_value.bv_len ) {
1365 rs->sr_text = "domainScope control value not empty";
1366 return LDAP_PROTOCOL_ERROR;
1369 op->o_domain_scope = ctrl->ldctl_iscritical
1370 ? SLAP_CONTROL_CRITICAL
1371 : SLAP_CONTROL_NONCRITICAL;
1373 return LDAP_SUCCESS;
1376 #ifdef SLAP_CONTROL_X_TREE_DELETE
1377 static int parseTreeDelete (
1382 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1383 rs->sr_text = "treeDelete control specified multiple times";
1384 return LDAP_PROTOCOL_ERROR;
1387 if ( ctrl->ldctl_value.bv_len ) {
1388 rs->sr_text = "treeDelete control value not empty";
1389 return LDAP_PROTOCOL_ERROR;
1392 op->o_tree_delete = ctrl->ldctl_iscritical
1393 ? SLAP_CONTROL_CRITICAL
1394 : SLAP_CONTROL_NONCRITICAL;
1396 return LDAP_SUCCESS;
1400 static int parseSearchOptions (
1406 ber_int_t search_flags;
1409 if ( ctrl->ldctl_value.bv_len == 0 ) {
1410 rs->sr_text = "searchOptions control value is empty (or absent)";
1411 return LDAP_PROTOCOL_ERROR;
1414 ber = ber_init( &ctrl->ldctl_value );
1416 rs->sr_text = "internal error";
1420 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1421 rs->sr_text = "searchOptions control decoding error";
1422 return LDAP_PROTOCOL_ERROR;
1425 (void) ber_free( ber, 1 );
1427 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1428 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1429 rs->sr_text = "searchOptions control specified multiple times "
1430 "or with domainScope control";
1431 return LDAP_PROTOCOL_ERROR;
1434 op->o_domain_scope = ctrl->ldctl_iscritical
1435 ? SLAP_CONTROL_CRITICAL
1436 : SLAP_CONTROL_NONCRITICAL;
1439 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1440 /* Other search flags not recognised so far,
1442 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1444 rs->sr_text = "searchOptions contained unrecognized flag";
1445 return LDAP_UNWILLING_TO_PERFORM;
1448 return LDAP_SUCCESS;