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 /* FIXME: in principle, we should rather merge
290 * existing extops with those supported by the
291 * new control handling implementation.
292 * In fact, whether a control is compatible with
293 * an extop should not be a matter of implementation.
294 * We likely also need a means for a newly
295 * registered extop to declare that it is
296 * comptible with an already registered control.
298 ber_bvarray_free( sc->sc_extendedopsbv );
299 sc->sc_extendedopsbv = NULL;
300 sc->sc_extendedops = NULL;
304 sc->sc_extendedopsbv = extendedopsbv;
305 sc->sc_mask = controlmask;
306 sc->sc_parse = controlparsefn;
308 *controlcid = sc->sc_cid;
315 * One-time initialization of internal controls.
318 slap_controls_init( void )
324 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
325 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
326 rc = register_supported_control( control_defs[i].sc_oid,
327 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
328 control_defs[i].sc_parse, cid );
329 if ( rc != LDAP_SUCCESS ) break;
336 * Free memory associated with list of supported controls.
339 controls_destroy( void )
341 struct slap_control *sc;
343 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
344 sc = LDAP_SLIST_FIRST(&controls_list);
345 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
347 ch_free( sc->sc_oid );
348 if ( sc->sc_extendedopsbv != NULL ) {
349 ber_bvarray_free( sc->sc_extendedopsbv );
356 * Format the supportedControl attribute of the root DSE,
357 * detailing which controls are supported by the directory
361 controls_root_dse_info( Entry *e )
363 AttributeDescription *ad_supportedControl
364 = slap_schema.si_ad_supportedControl;
365 struct berval vals[2];
366 struct slap_control *sc;
368 vals[1].bv_val = NULL;
371 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
372 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
374 vals[0].bv_val = sc->sc_oid;
375 vals[0].bv_len = strlen( sc->sc_oid );
377 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
386 * Return a list of OIDs and operation masks for supported
387 * controls. Used by SLAPI.
390 get_supported_controls(char ***ctrloidsp,
391 slap_mask_t **ctrlmasks)
396 struct slap_control *sc;
400 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
410 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
411 if ( oids == NULL ) {
412 return LDAP_NO_MEMORY;
414 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
415 if ( masks == NULL ) {
417 return LDAP_NO_MEMORY;
422 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
423 oids[n] = ch_strdup( sc->sc_oid );
424 masks[n] = sc->sc_mask;
437 * Find a control given its OID.
439 static struct slap_control *
440 find_ctrl( const char *oid )
442 struct slap_control *sc;
444 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
445 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
454 slap_find_control_id(
458 struct slap_control *ctrl = find_ctrl( oid );
460 if ( cid ) *cid = ctrl->sc_cid;
463 return LDAP_CONTROL_NOT_FOUND;
467 slap_global_control( Operation *op, const char *oid, int *cid )
469 struct slap_control *ctrl = find_ctrl( oid );
471 if ( ctrl == NULL ) {
472 /* should not be reachable */
473 Debug( LDAP_DEBUG_ANY,
474 "slap_global_control: unrecognized control: %s\n",
476 return LDAP_CONTROL_NOT_FOUND;
479 if ( cid ) *cid = ctrl->sc_cid;
481 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
482 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
483 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
485 return LDAP_COMPARE_TRUE;
488 Debug( LDAP_DEBUG_TRACE,
489 "slap_global_control: unavailable control: %s\n",
492 return LDAP_COMPARE_FALSE;
495 void slap_free_ctrls(
497 LDAPControl **ctrls )
501 for (i=0; ctrls[i]; i++) {
502 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
504 op->o_tmpfree( ctrls, op->o_tmpmemctx );
510 LDAPControl *control,
513 struct slap_control *sc;
515 sc = find_ctrl( control->ldctl_oid );
517 /* recognized control */
519 switch( op->o_tag ) {
521 tagmask = SLAP_CTRL_ADD;
524 tagmask = SLAP_CTRL_BIND;
526 case LDAP_REQ_COMPARE:
527 tagmask = SLAP_CTRL_COMPARE;
529 case LDAP_REQ_DELETE:
530 tagmask = SLAP_CTRL_DELETE;
532 case LDAP_REQ_MODIFY:
533 tagmask = SLAP_CTRL_MODIFY;
535 case LDAP_REQ_RENAME:
536 tagmask = SLAP_CTRL_RENAME;
538 case LDAP_REQ_SEARCH:
539 tagmask = SLAP_CTRL_SEARCH;
541 case LDAP_REQ_UNBIND:
542 tagmask = SLAP_CTRL_UNBIND;
544 case LDAP_REQ_ABANDON:
545 tagmask = SLAP_CTRL_ABANDON;
547 case LDAP_REQ_EXTENDED:
549 assert( op->ore_reqoid.bv_val != NULL );
550 if( sc->sc_extendedopsbv != NULL ) {
552 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
553 if( bvmatch( &op->ore_reqoid,
554 &sc->sc_extendedopsbv[i] ) )
563 *text = "controls internal error";
567 if (( sc->sc_mask & tagmask ) == tagmask ) {
568 /* available extension */
571 if( !sc->sc_parse ) {
572 *text = "not yet implemented";
576 rc = sc->sc_parse( op, rs, control );
578 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
582 } else if( control->ldctl_iscritical ) {
583 /* unavailable CRITICAL control */
584 *text = "critical extension is unavailable";
585 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
587 } else if( control->ldctl_iscritical ) {
588 /* unrecognized CRITICAL control */
589 *text = "critical extension is not recognized";
590 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
605 BerElement *ber = op->o_ber;
608 len = ber_pvt_ber_remaining(ber);
612 rs->sr_err = LDAP_SUCCESS;
616 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
617 if( tag == LBER_ERROR ) {
618 rs->sr_err = SLAPD_DISCONNECT;
619 rs->sr_text = "unexpected data in PDU";
625 Debug( LDAP_DEBUG_TRACE,
626 "=> get_ctrls\n", 0, 0, 0 );
628 if( op->o_protocol < LDAP_VERSION3 ) {
629 rs->sr_err = SLAPD_DISCONNECT;
630 rs->sr_text = "controls require LDAPv3";
634 /* one for first control, one for termination */
635 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
638 if( op->ctrls == NULL ) {
639 rs->sr_err = LDAP_NO_MEMORY;
640 rs->sr_text = "no memory";
645 op->o_ctrls[nctrls] = NULL;
647 /* step through each element */
648 for( tag = ber_first_element( ber, &len, &opaque );
650 tag = ber_next_element( ber, &len, opaque ) )
653 LDAPControl **tctrls;
655 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
656 memset(c, 0, sizeof(LDAPControl));
658 /* allocate pointer space for current controls (nctrls)
659 * + this control + extra NULL
661 tctrls = op->o_tmprealloc( op->o_ctrls,
662 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
665 if( tctrls == NULL ) {
667 ldap_controls_free(op->o_ctrls);
670 rs->sr_err = LDAP_NO_MEMORY;
671 rs->sr_text = "no memory";
675 op->o_ctrls = tctrls;
677 op->o_ctrls[nctrls++] = c;
678 op->o_ctrls[nctrls] = NULL;
680 tag = ber_scanf( ber, "{m" /*}*/, &bv );
681 c->ldctl_oid = bv.bv_val;
683 if( tag == LBER_ERROR ) {
684 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
687 slap_free_ctrls( op, op->o_ctrls );
689 rs->sr_err = SLAPD_DISCONNECT;
690 rs->sr_text = "decoding controls error";
693 } else if( c->ldctl_oid == NULL ) {
694 Debug( LDAP_DEBUG_TRACE,
695 "get_ctrls: conn %lu got emtpy OID.\n",
696 op->o_connid, 0, 0 );
698 slap_free_ctrls( op, op->o_ctrls );
700 rs->sr_err = LDAP_PROTOCOL_ERROR;
701 rs->sr_text = "OID field is empty";
705 tag = ber_peek_tag( ber, &len );
707 if( tag == LBER_BOOLEAN ) {
709 tag = ber_scanf( ber, "b", &crit );
711 if( tag == LBER_ERROR ) {
712 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
714 slap_free_ctrls( op, op->o_ctrls );
716 rs->sr_err = SLAPD_DISCONNECT;
717 rs->sr_text = "decoding controls error";
721 c->ldctl_iscritical = (crit != 0);
722 tag = ber_peek_tag( ber, &len );
725 if( tag == LBER_OCTETSTRING ) {
726 tag = ber_scanf( ber, "m", &c->ldctl_value );
728 if( tag == LBER_ERROR ) {
729 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
730 "%s (%scritical): get value failed.\n",
731 op->o_connid, c->ldctl_oid,
732 c->ldctl_iscritical ? "" : "non" );
733 slap_free_ctrls( op, op->o_ctrls );
735 rs->sr_err = SLAPD_DISCONNECT;
736 rs->sr_text = "decoding controls error";
741 Debug( LDAP_DEBUG_TRACE,
742 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
743 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
745 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
746 if ( rs->sr_err != LDAP_SUCCESS ) {
752 Debug( LDAP_DEBUG_TRACE,
753 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
754 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
756 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
757 if( rs->sr_err == SLAPD_DISCONNECT ) {
758 rs->sr_err = LDAP_PROTOCOL_ERROR;
759 send_ldap_disconnect( op, rs );
760 rs->sr_err = SLAPD_DISCONNECT;
762 send_ldap_result( op, rs );
774 BI_chk_controls fnc )
778 switch ( op->o_ctrlflag[ ctrl ] ) {
779 case SLAP_CONTROL_NONCRITICAL:
780 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
781 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
782 slap_known_controls[ ctrl - 1 ] ) == 0 )
789 rs->sr_err = LDAP_OTHER;
797 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
800 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
801 ( i - j ) * sizeof( LDAPControl * ) );
804 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
808 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
810 Debug( LDAP_DEBUG_ANY, "%s: "
811 "non-critical control \"%s\" not supported; stripped.\n",
812 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
815 case SLAP_CONTROL_IGNORED:
816 case SLAP_CONTROL_NONE:
817 rs->sr_err = SLAP_CB_CONTINUE;
820 case SLAP_CONTROL_CRITICAL:
821 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
825 Debug( LDAP_DEBUG_ANY, "%s: "
826 "critical control \"%s\" not supported.\n",
827 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
831 /* handle all cases! */
839 static int parseDontUseCopy (
844 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
845 rs->sr_text = "dontUseCopy control specified multiple times";
846 return LDAP_PROTOCOL_ERROR;
849 if ( ctrl->ldctl_value.bv_len ) {
850 rs->sr_text = "dontUseCopy control value not empty";
851 return LDAP_PROTOCOL_ERROR;
854 if ( ctrl->ldctl_iscritical != SLAP_CONTROL_CRITICAL ) {
855 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
856 return LDAP_PROTOCOL_ERROR;
859 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
863 static int parseManageDIT (
868 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
869 rs->sr_text = "manageDIT control specified multiple times";
870 return LDAP_PROTOCOL_ERROR;
873 if ( ctrl->ldctl_value.bv_len ) {
874 rs->sr_text = "manageDIT control value not empty";
875 return LDAP_PROTOCOL_ERROR;
878 op->o_managedit = ctrl->ldctl_iscritical
879 ? SLAP_CONTROL_CRITICAL
880 : SLAP_CONTROL_NONCRITICAL;
886 static int parseManageDSAit (
891 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
892 rs->sr_text = "manageDSAit control specified multiple times";
893 return LDAP_PROTOCOL_ERROR;
896 if ( ctrl->ldctl_value.bv_len ) {
897 rs->sr_text = "manageDSAit control value not empty";
898 return LDAP_PROTOCOL_ERROR;
901 op->o_managedsait = ctrl->ldctl_iscritical
902 ? SLAP_CONTROL_CRITICAL
903 : SLAP_CONTROL_NONCRITICAL;
908 static int parseProxyAuthz (
914 struct berval dn = BER_BVNULL;
916 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
917 rs->sr_text = "proxy authorization control specified multiple times";
918 return LDAP_PROTOCOL_ERROR;
921 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
922 && BER_BVISEMPTY( &op->o_ndn ) )
924 rs->sr_text = "anonymous proxied authorization not allowed";
925 return LDAP_PROXIED_AUTHORIZATION_DENIED;
928 op->o_proxy_authz = ctrl->ldctl_iscritical
929 ? SLAP_CONTROL_CRITICAL
930 : SLAP_CONTROL_NONCRITICAL;
932 Debug( LDAP_DEBUG_ARGS,
933 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
935 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
938 if ( ctrl->ldctl_value.bv_len == 0 ) {
939 Debug( LDAP_DEBUG_TRACE,
940 "parseProxyAuthz: conn=%lu anonymous\n",
941 op->o_connid, 0, 0 );
944 if ( !BER_BVISNULL( &op->o_ndn ) ) {
945 op->o_ndn.bv_val[ 0 ] = '\0';
947 op->o_ndn.bv_len = 0;
949 if ( !BER_BVISNULL( &op->o_dn ) ) {
950 op->o_dn.bv_val[ 0 ] = '\0';
957 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
958 NULL, &dn, SLAP_GETDN_AUTHZID );
960 /* FIXME: empty DN in proxyAuthz control should be legal... */
961 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
963 ch_free( dn.bv_val );
965 rs->sr_text = "authzId mapping failed";
966 return LDAP_PROXIED_AUTHORIZATION_DENIED;
969 Debug( LDAP_DEBUG_TRACE,
970 "parseProxyAuthz: conn=%lu \"%s\"\n",
972 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
974 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
977 ch_free( dn.bv_val );
978 rs->sr_text = "not authorized to assume identity";
979 return LDAP_PROXIED_AUTHORIZATION_DENIED;
982 ch_free( op->o_ndn.bv_val );
983 ch_free( op->o_dn.bv_val );
986 * NOTE: since slap_sasl_getdn() returns a normalized dn,
987 * from now on op->o_dn is normalized
990 ber_dupbv( &op->o_dn, &dn );
993 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
994 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
999 static int parseNoOp (
1004 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1005 rs->sr_text = "noop control specified multiple times";
1006 return LDAP_PROTOCOL_ERROR;
1009 if ( ctrl->ldctl_value.bv_len ) {
1010 rs->sr_text = "noop control value not empty";
1011 return LDAP_PROTOCOL_ERROR;
1014 op->o_noop = ctrl->ldctl_iscritical
1015 ? SLAP_CONTROL_CRITICAL
1016 : SLAP_CONTROL_NONCRITICAL;
1018 return LDAP_SUCCESS;
1021 static int parsePagedResults (
1026 int rc = LDAP_SUCCESS;
1030 struct berval cookie = BER_BVNULL;
1031 PagedResultsState *ps;
1033 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1034 rs->sr_text = "paged results control specified multiple times";
1035 return LDAP_PROTOCOL_ERROR;
1038 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1039 rs->sr_text = "paged results control value is empty (or absent)";
1040 return LDAP_PROTOCOL_ERROR;
1043 /* Parse the control value
1044 * realSearchControlValue ::= SEQUENCE {
1045 * size INTEGER (0..maxInt),
1046 * -- requested page size from client
1047 * -- result set size estimate from server
1048 * cookie OCTET STRING
1051 ber = ber_init( &ctrl->ldctl_value );
1052 if ( ber == NULL ) {
1053 rs->sr_text = "internal error";
1057 tag = ber_scanf( ber, "{im}", &size, &cookie );
1059 if ( tag == LBER_ERROR ) {
1060 rs->sr_text = "paged results control could not be decoded";
1061 rc = LDAP_PROTOCOL_ERROR;
1066 rs->sr_text = "paged results control size invalid";
1067 rc = LDAP_PROTOCOL_ERROR;
1071 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1072 *ps = op->o_conn->c_pagedresults_state;
1074 op->o_pagedresults_state = ps;
1076 /* NOTE: according to RFC 2696 3.:
1078 If the page size is greater than or equal to the sizeLimit value, the
1079 server should ignore the control as the request can be satisfied in a
1082 * NOTE: this assumes that the op->ors_slimit be set
1083 * before the controls are parsed.
1086 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1087 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1089 } else if ( ctrl->ldctl_iscritical ) {
1090 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1093 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1097 (void)ber_free( ber, 1 );
1102 static int parseSortedResults (
1107 int rc = LDAP_SUCCESS;
1109 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1110 rs->sr_text = "sorted results control specified multiple times";
1111 return LDAP_PROTOCOL_ERROR;
1114 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1115 rs->sr_text = "sorted results control value is empty (or absent)";
1116 return LDAP_PROTOCOL_ERROR;
1119 /* blow off parsing the value */
1121 op->o_sortedresults = ctrl->ldctl_iscritical
1122 ? SLAP_CONTROL_CRITICAL
1123 : SLAP_CONTROL_NONCRITICAL;
1129 static int parseAssert (
1135 struct berval fstr = BER_BVNULL;
1137 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1138 rs->sr_text = "assert control specified multiple times";
1139 return LDAP_PROTOCOL_ERROR;
1142 if ( ctrl->ldctl_value.bv_len == 0 ) {
1143 rs->sr_text = "assert control value is empty (or absent)";
1144 return LDAP_PROTOCOL_ERROR;
1147 ber = ber_init( &(ctrl->ldctl_value) );
1149 rs->sr_text = "assert control: internal error";
1153 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1155 if( rs->sr_err != LDAP_SUCCESS ) {
1156 if( rs->sr_err == SLAPD_DISCONNECT ) {
1157 rs->sr_err = LDAP_PROTOCOL_ERROR;
1158 send_ldap_disconnect( op, rs );
1159 rs->sr_err = SLAPD_DISCONNECT;
1161 send_ldap_result( op, rs );
1163 if( op->o_assertion != NULL ) {
1164 filter_free_x( op, op->o_assertion );
1170 filter2bv_x( op, op->o_assertion, &fstr );
1172 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1173 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1174 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1177 op->o_assert = ctrl->ldctl_iscritical
1178 ? SLAP_CONTROL_CRITICAL
1179 : SLAP_CONTROL_NONCRITICAL;
1181 rs->sr_err = LDAP_SUCCESS;
1182 return LDAP_SUCCESS;
1185 static int parsePreRead (
1190 ber_len_t siz, off, i;
1191 AttributeName *an = NULL;
1194 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1195 rs->sr_text = "preread control specified multiple times";
1196 return LDAP_PROTOCOL_ERROR;
1199 if ( ctrl->ldctl_value.bv_len == 0 ) {
1200 rs->sr_text = "preread control value is empty (or absent)";
1201 return LDAP_PROTOCOL_ERROR;
1204 ber = ber_init( &(ctrl->ldctl_value) );
1206 rs->sr_text = "preread control: internal error";
1210 siz = sizeof( AttributeName );
1211 off = offsetof( AttributeName, an_name );
1212 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1213 rs->sr_text = "preread control: decoding error";
1214 return LDAP_PROTOCOL_ERROR;
1217 for( i=0; i<siz; i++ ) {
1218 int rc = LDAP_SUCCESS;
1219 const char *dummy = NULL;
1221 an[i].an_desc = NULL;
1223 an[i].an_oc_exclude = 0;
1224 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1225 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1228 : "postread control: unknown attributeType";
1233 op->o_preread = ctrl->ldctl_iscritical
1234 ? SLAP_CONTROL_CRITICAL
1235 : SLAP_CONTROL_NONCRITICAL;
1237 op->o_preread_attrs = an;
1239 rs->sr_err = LDAP_SUCCESS;
1240 return LDAP_SUCCESS;
1243 static int parsePostRead (
1248 ber_len_t siz, off, i;
1249 AttributeName *an = NULL;
1252 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1253 rs->sr_text = "postread control specified multiple times";
1254 return LDAP_PROTOCOL_ERROR;
1257 if ( ctrl->ldctl_value.bv_len == 0 ) {
1258 rs->sr_text = "postread control value is empty (or absent)";
1259 return LDAP_PROTOCOL_ERROR;
1262 ber = ber_init( &(ctrl->ldctl_value) );
1264 rs->sr_text = "postread control: internal error";
1268 siz = sizeof( AttributeName );
1269 off = offsetof( AttributeName, an_name );
1270 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1271 rs->sr_text = "postread control: decoding error";
1272 return LDAP_PROTOCOL_ERROR;
1275 for( i=0; i<siz; i++ ) {
1276 int rc = LDAP_SUCCESS;
1277 const char *dummy = NULL;
1279 an[i].an_desc = NULL;
1281 an[i].an_oc_exclude = 0;
1282 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1283 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1286 : "postread control: unknown attributeType";
1291 op->o_postread = ctrl->ldctl_iscritical
1292 ? SLAP_CONTROL_CRITICAL
1293 : SLAP_CONTROL_NONCRITICAL;
1295 op->o_postread_attrs = an;
1297 rs->sr_err = LDAP_SUCCESS;
1298 return LDAP_SUCCESS;
1301 static int parseValuesReturnFilter (
1307 struct berval fstr = BER_BVNULL;
1309 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1310 rs->sr_text = "valuesReturnFilter control specified multiple times";
1311 return LDAP_PROTOCOL_ERROR;
1314 if ( ctrl->ldctl_value.bv_len == 0 ) {
1315 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1316 return LDAP_PROTOCOL_ERROR;
1319 ber = ber_init( &(ctrl->ldctl_value) );
1321 rs->sr_text = "internal error";
1325 rs->sr_err = get_vrFilter( op, ber,
1326 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1328 if( rs->sr_err != LDAP_SUCCESS ) {
1329 if( rs->sr_err == SLAPD_DISCONNECT ) {
1330 rs->sr_err = LDAP_PROTOCOL_ERROR;
1331 send_ldap_disconnect( op, rs );
1332 rs->sr_err = SLAPD_DISCONNECT;
1334 send_ldap_result( op, rs );
1336 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1340 vrFilter2bv( op, op->o_vrFilter, &fstr );
1343 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1344 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1345 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1348 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1349 ? SLAP_CONTROL_CRITICAL
1350 : SLAP_CONTROL_NONCRITICAL;
1352 rs->sr_err = LDAP_SUCCESS;
1353 return LDAP_SUCCESS;
1356 static int parseSubentries (
1361 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1362 rs->sr_text = "subentries control specified multiple times";
1363 return LDAP_PROTOCOL_ERROR;
1366 /* FIXME: should use BER library */
1367 if( ( ctrl->ldctl_value.bv_len != 3 )
1368 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1369 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1371 rs->sr_text = "subentries control value encoding is bogus";
1372 return LDAP_PROTOCOL_ERROR;
1375 op->o_subentries = ctrl->ldctl_iscritical
1376 ? SLAP_CONTROL_CRITICAL
1377 : SLAP_CONTROL_NONCRITICAL;
1379 if (ctrl->ldctl_value.bv_val[2]) {
1380 set_subentries_visibility( op );
1383 return LDAP_SUCCESS;
1386 static int parsePermissiveModify (
1391 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1392 rs->sr_text = "permissiveModify control specified multiple times";
1393 return LDAP_PROTOCOL_ERROR;
1396 if ( ctrl->ldctl_value.bv_len ) {
1397 rs->sr_text = "permissiveModify control value not empty";
1398 return LDAP_PROTOCOL_ERROR;
1401 op->o_permissive_modify = ctrl->ldctl_iscritical
1402 ? SLAP_CONTROL_CRITICAL
1403 : SLAP_CONTROL_NONCRITICAL;
1405 return LDAP_SUCCESS;
1408 static int parseDomainScope (
1413 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1414 rs->sr_text = "domainScope control specified multiple times";
1415 return LDAP_PROTOCOL_ERROR;
1418 if ( ctrl->ldctl_value.bv_len ) {
1419 rs->sr_text = "domainScope control value not empty";
1420 return LDAP_PROTOCOL_ERROR;
1423 op->o_domain_scope = ctrl->ldctl_iscritical
1424 ? SLAP_CONTROL_CRITICAL
1425 : SLAP_CONTROL_NONCRITICAL;
1427 return LDAP_SUCCESS;
1430 #ifdef SLAP_CONTROL_X_TREE_DELETE
1431 static int parseTreeDelete (
1436 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1437 rs->sr_text = "treeDelete control specified multiple times";
1438 return LDAP_PROTOCOL_ERROR;
1441 if ( ctrl->ldctl_value.bv_len ) {
1442 rs->sr_text = "treeDelete control value not empty";
1443 return LDAP_PROTOCOL_ERROR;
1446 op->o_tree_delete = ctrl->ldctl_iscritical
1447 ? SLAP_CONTROL_CRITICAL
1448 : SLAP_CONTROL_NONCRITICAL;
1450 return LDAP_SUCCESS;
1454 static int parseSearchOptions (
1460 ber_int_t search_flags;
1463 if ( ctrl->ldctl_value.bv_len == 0 ) {
1464 rs->sr_text = "searchOptions control value is empty (or absent)";
1465 return LDAP_PROTOCOL_ERROR;
1468 ber = ber_init( &ctrl->ldctl_value );
1470 rs->sr_text = "internal error";
1474 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1475 rs->sr_text = "searchOptions control decoding error";
1476 return LDAP_PROTOCOL_ERROR;
1479 (void) ber_free( ber, 1 );
1481 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1482 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1483 rs->sr_text = "searchOptions control specified multiple times "
1484 "or with domainScope control";
1485 return LDAP_PROTOCOL_ERROR;
1488 op->o_domain_scope = ctrl->ldctl_iscritical
1489 ? SLAP_CONTROL_CRITICAL
1490 : SLAP_CONTROL_NONCRITICAL;
1493 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1494 /* Other search flags not recognised so far,
1496 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1498 rs->sr_text = "searchOptions contained unrecognized flag";
1499 return LDAP_UNWILLING_TO_PERFORM;
1502 return LDAP_SUCCESS;