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, 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_extendedops );
290 sc->sc_extendedops = NULL;
294 sc->sc_extendedopsbv = extendedopsbv;
295 sc->sc_mask = controlmask;
296 sc->sc_parse = controlparsefn;
298 *controlcid = sc->sc_cid;
305 * One-time initialization of internal controls.
308 slap_controls_init( void )
314 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
315 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
316 rc = register_supported_control( control_defs[i].sc_oid,
317 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
318 control_defs[i].sc_parse, cid );
319 if ( rc != LDAP_SUCCESS ) break;
326 * Free memory associated with list of supported controls.
329 controls_destroy( void )
331 struct slap_control *sc;
333 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
334 sc = LDAP_SLIST_FIRST(&controls_list);
335 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
337 ch_free( sc->sc_oid );
338 if ( sc->sc_extendedopsbv != NULL ) {
339 ber_bvarray_free( sc->sc_extendedopsbv );
346 * Format the supportedControl attribute of the root DSE,
347 * detailing which controls are supported by the directory
351 controls_root_dse_info( Entry *e )
353 AttributeDescription *ad_supportedControl
354 = slap_schema.si_ad_supportedControl;
355 struct berval vals[2];
356 struct slap_control *sc;
358 vals[1].bv_val = NULL;
361 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
362 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
364 vals[0].bv_val = sc->sc_oid;
365 vals[0].bv_len = strlen( sc->sc_oid );
367 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
376 * Return a list of OIDs and operation masks for supported
377 * controls. Used by SLAPI.
380 get_supported_controls(char ***ctrloidsp,
381 slap_mask_t **ctrlmasks)
386 struct slap_control *sc;
390 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
400 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
401 if ( oids == NULL ) {
402 return LDAP_NO_MEMORY;
404 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
405 if ( masks == NULL ) {
407 return LDAP_NO_MEMORY;
412 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
413 oids[n] = ch_strdup( sc->sc_oid );
414 masks[n] = sc->sc_mask;
427 * Find a control given its OID.
429 static struct slap_control *
430 find_ctrl( const char *oid )
432 struct slap_control *sc;
434 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
435 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
444 slap_find_control_id(
448 struct slap_control *ctrl = find_ctrl( oid );
450 if ( cid ) *cid = ctrl->sc_cid;
453 return LDAP_CONTROL_NOT_FOUND;
457 slap_global_control( Operation *op, const char *oid, int *cid )
459 struct slap_control *ctrl = find_ctrl( oid );
461 if ( ctrl == NULL ) {
462 /* should not be reachable */
463 Debug( LDAP_DEBUG_ANY,
464 "slap_global_control: unrecognized control: %s\n",
466 return LDAP_CONTROL_NOT_FOUND;
469 if ( cid ) *cid = ctrl->sc_cid;
471 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
472 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
473 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
475 return LDAP_COMPARE_TRUE;
478 Debug( LDAP_DEBUG_TRACE,
479 "slap_global_control: unavailable control: %s\n",
482 return LDAP_COMPARE_FALSE;
485 void slap_free_ctrls(
487 LDAPControl **ctrls )
491 for (i=0; ctrls[i]; i++) {
492 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
494 op->o_tmpfree( ctrls, op->o_tmpmemctx );
500 LDAPControl *control,
503 struct slap_control *sc;
505 sc = find_ctrl( control->ldctl_oid );
507 /* recognized control */
509 switch( op->o_tag ) {
511 tagmask = SLAP_CTRL_ADD;
514 tagmask = SLAP_CTRL_BIND;
516 case LDAP_REQ_COMPARE:
517 tagmask = SLAP_CTRL_COMPARE;
519 case LDAP_REQ_DELETE:
520 tagmask = SLAP_CTRL_DELETE;
522 case LDAP_REQ_MODIFY:
523 tagmask = SLAP_CTRL_MODIFY;
525 case LDAP_REQ_RENAME:
526 tagmask = SLAP_CTRL_RENAME;
528 case LDAP_REQ_SEARCH:
529 tagmask = SLAP_CTRL_SEARCH;
531 case LDAP_REQ_UNBIND:
532 tagmask = SLAP_CTRL_UNBIND;
534 case LDAP_REQ_ABANDON:
535 tagmask = SLAP_CTRL_ABANDON;
537 case LDAP_REQ_EXTENDED:
539 assert( op->ore_reqoid.bv_val != NULL );
540 if( sc->sc_extendedopsbv != NULL ) {
542 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
543 if( bvmatch( &op->ore_reqoid,
544 &sc->sc_extendedopsbv[i] ) )
553 *text = "controls internal error";
557 if (( sc->sc_mask & tagmask ) == tagmask ) {
558 /* available extension */
561 if( !sc->sc_parse ) {
562 *text = "not yet implemented";
566 rc = sc->sc_parse( op, rs, control );
568 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
572 } else if( control->ldctl_iscritical ) {
573 /* unavailable CRITICAL control */
574 *text = "critical extension is unavailable";
575 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
577 } else if( control->ldctl_iscritical ) {
578 /* unrecognized CRITICAL control */
579 *text = "critical extension is not recognized";
580 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
595 BerElement *ber = op->o_ber;
598 len = ber_pvt_ber_remaining(ber);
602 rs->sr_err = LDAP_SUCCESS;
606 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
607 if( tag == LBER_ERROR ) {
608 rs->sr_err = SLAPD_DISCONNECT;
609 rs->sr_text = "unexpected data in PDU";
615 Debug( LDAP_DEBUG_TRACE,
616 "=> get_ctrls\n", 0, 0, 0 );
618 if( op->o_protocol < LDAP_VERSION3 ) {
619 rs->sr_err = SLAPD_DISCONNECT;
620 rs->sr_text = "controls require LDAPv3";
624 /* one for first control, one for termination */
625 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
628 if( op->ctrls == NULL ) {
629 rs->sr_err = LDAP_NO_MEMORY;
630 rs->sr_text = "no memory";
635 op->o_ctrls[nctrls] = NULL;
637 /* step through each element */
638 for( tag = ber_first_element( ber, &len, &opaque );
640 tag = ber_next_element( ber, &len, opaque ) )
643 LDAPControl **tctrls;
645 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
646 memset(c, 0, sizeof(LDAPControl));
648 /* allocate pointer space for current controls (nctrls)
649 * + this control + extra NULL
651 tctrls = op->o_tmprealloc( op->o_ctrls,
652 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
655 if( tctrls == NULL ) {
657 ldap_controls_free(op->o_ctrls);
660 rs->sr_err = LDAP_NO_MEMORY;
661 rs->sr_text = "no memory";
665 op->o_ctrls = tctrls;
667 op->o_ctrls[nctrls++] = c;
668 op->o_ctrls[nctrls] = NULL;
670 tag = ber_scanf( ber, "{m" /*}*/, &bv );
671 c->ldctl_oid = bv.bv_val;
673 if( tag == LBER_ERROR ) {
674 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
677 slap_free_ctrls( op, op->o_ctrls );
679 rs->sr_err = SLAPD_DISCONNECT;
680 rs->sr_text = "decoding controls error";
683 } else if( c->ldctl_oid == NULL ) {
684 Debug( LDAP_DEBUG_TRACE,
685 "get_ctrls: conn %lu got emtpy OID.\n",
686 op->o_connid, 0, 0 );
688 slap_free_ctrls( op, op->o_ctrls );
690 rs->sr_err = LDAP_PROTOCOL_ERROR;
691 rs->sr_text = "OID field is empty";
695 tag = ber_peek_tag( ber, &len );
697 if( tag == LBER_BOOLEAN ) {
699 tag = ber_scanf( ber, "b", &crit );
701 if( tag == LBER_ERROR ) {
702 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
704 slap_free_ctrls( op, op->o_ctrls );
706 rs->sr_err = SLAPD_DISCONNECT;
707 rs->sr_text = "decoding controls error";
711 c->ldctl_iscritical = (crit != 0);
712 tag = ber_peek_tag( ber, &len );
715 if( tag == LBER_OCTETSTRING ) {
716 tag = ber_scanf( ber, "m", &c->ldctl_value );
718 if( tag == LBER_ERROR ) {
719 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
720 "%s (%scritical): get value failed.\n",
721 op->o_connid, c->ldctl_oid,
722 c->ldctl_iscritical ? "" : "non" );
723 slap_free_ctrls( op, op->o_ctrls );
725 rs->sr_err = SLAPD_DISCONNECT;
726 rs->sr_text = "decoding controls error";
731 Debug( LDAP_DEBUG_TRACE,
732 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
733 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
735 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
736 if ( rs->sr_err != LDAP_SUCCESS ) {
742 Debug( LDAP_DEBUG_TRACE,
743 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
744 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
746 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
747 if( rs->sr_err == SLAPD_DISCONNECT ) {
748 rs->sr_err = LDAP_PROTOCOL_ERROR;
749 send_ldap_disconnect( op, rs );
750 rs->sr_err = SLAPD_DISCONNECT;
752 send_ldap_result( op, rs );
764 BI_chk_controls fnc )
768 switch ( op->o_ctrlflag[ ctrl ] ) {
769 case SLAP_CONTROL_NONCRITICAL:
770 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
771 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
772 slap_known_controls[ ctrl - 1 ] ) == 0 )
779 rs->sr_err = LDAP_OTHER;
787 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
790 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
791 ( i - j ) * sizeof( LDAPControl * ) );
794 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
798 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
800 Debug( LDAP_DEBUG_ANY, "%s: "
801 "non-critical control \"%s\" not supported; stripped.\n",
802 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
805 case SLAP_CONTROL_IGNORED:
806 case SLAP_CONTROL_NONE:
807 rs->sr_err = SLAP_CB_CONTINUE;
810 case SLAP_CONTROL_CRITICAL:
811 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
815 Debug( LDAP_DEBUG_ANY, "%s: "
816 "critical control \"%s\" not supported.\n",
817 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
821 /* handle all cases! */
829 static int parseDontUseCopy (
834 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
835 rs->sr_text = "dontUseCopy control specified multiple times";
836 return LDAP_PROTOCOL_ERROR;
839 if ( ctrl->ldctl_value.bv_len ) {
840 rs->sr_text = "dontUseCopy control value not empty";
841 return LDAP_PROTOCOL_ERROR;
844 if ( ctrl->ldctl_iscritical != SLAP_CONTROL_CRITICAL ) {
845 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
846 return LDAP_PROTOCOL_ERROR;
849 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
853 static int parseManageDIT (
858 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
859 rs->sr_text = "manageDIT control specified multiple times";
860 return LDAP_PROTOCOL_ERROR;
863 if ( ctrl->ldctl_value.bv_len ) {
864 rs->sr_text = "manageDIT control value not empty";
865 return LDAP_PROTOCOL_ERROR;
868 op->o_managedit = ctrl->ldctl_iscritical
869 ? SLAP_CONTROL_CRITICAL
870 : SLAP_CONTROL_NONCRITICAL;
876 static int parseManageDSAit (
881 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
882 rs->sr_text = "manageDSAit control specified multiple times";
883 return LDAP_PROTOCOL_ERROR;
886 if ( ctrl->ldctl_value.bv_len ) {
887 rs->sr_text = "manageDSAit control value not empty";
888 return LDAP_PROTOCOL_ERROR;
891 op->o_managedsait = ctrl->ldctl_iscritical
892 ? SLAP_CONTROL_CRITICAL
893 : SLAP_CONTROL_NONCRITICAL;
898 static int parseProxyAuthz (
904 struct berval dn = BER_BVNULL;
906 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
907 rs->sr_text = "proxy authorization control specified multiple times";
908 return LDAP_PROTOCOL_ERROR;
911 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
912 && BER_BVISEMPTY( &op->o_ndn ) )
914 rs->sr_text = "anonymous proxyAuthz not allowed";
915 return LDAP_PROXY_AUTHZ_FAILURE;
918 op->o_proxy_authz = ctrl->ldctl_iscritical
919 ? SLAP_CONTROL_CRITICAL
920 : SLAP_CONTROL_NONCRITICAL;
922 Debug( LDAP_DEBUG_ARGS,
923 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
925 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
928 if ( ctrl->ldctl_value.bv_len == 0 ) {
929 Debug( LDAP_DEBUG_TRACE,
930 "parseProxyAuthz: conn=%lu anonymous\n",
931 op->o_connid, 0, 0 );
934 if ( !BER_BVISNULL( &op->o_ndn ) ) {
935 op->o_ndn.bv_val[ 0 ] = '\0';
937 op->o_ndn.bv_len = 0;
939 if ( !BER_BVISNULL( &op->o_dn ) ) {
940 op->o_dn.bv_val[ 0 ] = '\0';
947 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
948 NULL, &dn, SLAP_GETDN_AUTHZID );
950 /* FIXME: empty DN in proxyAuthz control should be legal... */
951 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
953 ch_free( dn.bv_val );
955 rs->sr_text = "authzId mapping failed";
956 return LDAP_PROXY_AUTHZ_FAILURE;
959 Debug( LDAP_DEBUG_TRACE,
960 "parseProxyAuthz: conn=%lu \"%s\"\n",
962 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
964 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
967 ch_free( dn.bv_val );
968 rs->sr_text = "not authorized to assume identity";
969 return LDAP_PROXY_AUTHZ_FAILURE;
972 ch_free( op->o_ndn.bv_val );
973 ch_free( op->o_dn.bv_val );
976 * NOTE: since slap_sasl_getdn() returns a normalized dn,
977 * from now on op->o_dn is normalized
980 ber_dupbv( &op->o_dn, &dn );
983 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
984 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
989 static int parseNoOp (
994 if ( op->o_noop != SLAP_CONTROL_NONE ) {
995 rs->sr_text = "noop control specified multiple times";
996 return LDAP_PROTOCOL_ERROR;
999 if ( ctrl->ldctl_value.bv_len ) {
1000 rs->sr_text = "noop control value not empty";
1001 return LDAP_PROTOCOL_ERROR;
1004 op->o_noop = ctrl->ldctl_iscritical
1005 ? SLAP_CONTROL_CRITICAL
1006 : SLAP_CONTROL_NONCRITICAL;
1008 return LDAP_SUCCESS;
1011 static int parsePagedResults (
1016 int rc = LDAP_SUCCESS;
1020 struct berval cookie = BER_BVNULL;
1021 PagedResultsState *ps;
1023 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1024 rs->sr_text = "paged results control specified multiple times";
1025 return LDAP_PROTOCOL_ERROR;
1028 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1029 rs->sr_text = "paged results control value is empty (or absent)";
1030 return LDAP_PROTOCOL_ERROR;
1033 /* Parse the control value
1034 * realSearchControlValue ::= SEQUENCE {
1035 * size INTEGER (0..maxInt),
1036 * -- requested page size from client
1037 * -- result set size estimate from server
1038 * cookie OCTET STRING
1041 ber = ber_init( &ctrl->ldctl_value );
1042 if ( ber == NULL ) {
1043 rs->sr_text = "internal error";
1047 tag = ber_scanf( ber, "{im}", &size, &cookie );
1049 if ( tag == LBER_ERROR ) {
1050 rs->sr_text = "paged results control could not be decoded";
1051 rc = LDAP_PROTOCOL_ERROR;
1056 rs->sr_text = "paged results control size invalid";
1057 rc = LDAP_PROTOCOL_ERROR;
1061 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1062 *ps = op->o_conn->c_pagedresults_state;
1064 op->o_pagedresults_state = ps;
1066 /* NOTE: according to RFC 2696 3.:
1068 If the page size is greater than or equal to the sizeLimit value, the
1069 server should ignore the control as the request can be satisfied in a
1072 * NOTE: this assumes that the op->ors_slimit be set
1073 * before the controls are parsed.
1076 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1077 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1079 } else if ( ctrl->ldctl_iscritical ) {
1080 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1083 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1087 (void)ber_free( ber, 1 );
1092 static int parseSortedResults (
1097 int rc = LDAP_SUCCESS;
1099 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1100 rs->sr_text = "sorted results control specified multiple times";
1101 return LDAP_PROTOCOL_ERROR;
1104 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1105 rs->sr_text = "sorted results control value is empty (or absent)";
1106 return LDAP_PROTOCOL_ERROR;
1109 /* blow off parsing the value */
1111 op->o_sortedresults = ctrl->ldctl_iscritical
1112 ? SLAP_CONTROL_CRITICAL
1113 : SLAP_CONTROL_NONCRITICAL;
1119 static int parseAssert (
1125 struct berval fstr = BER_BVNULL;
1127 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1128 rs->sr_text = "assert control specified multiple times";
1129 return LDAP_PROTOCOL_ERROR;
1132 if ( ctrl->ldctl_value.bv_len == 0 ) {
1133 rs->sr_text = "assert control value is empty (or absent)";
1134 return LDAP_PROTOCOL_ERROR;
1137 ber = ber_init( &(ctrl->ldctl_value) );
1139 rs->sr_text = "assert control: internal error";
1143 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1145 if( rs->sr_err != LDAP_SUCCESS ) {
1146 if( rs->sr_err == SLAPD_DISCONNECT ) {
1147 rs->sr_err = LDAP_PROTOCOL_ERROR;
1148 send_ldap_disconnect( op, rs );
1149 rs->sr_err = SLAPD_DISCONNECT;
1151 send_ldap_result( op, rs );
1153 if( op->o_assertion != NULL ) {
1154 filter_free_x( op, op->o_assertion );
1160 filter2bv_x( op, op->o_assertion, &fstr );
1162 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1163 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1164 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1167 op->o_assert = ctrl->ldctl_iscritical
1168 ? SLAP_CONTROL_CRITICAL
1169 : SLAP_CONTROL_NONCRITICAL;
1171 rs->sr_err = LDAP_SUCCESS;
1172 return LDAP_SUCCESS;
1175 static int parsePreRead (
1180 ber_len_t siz, off, i;
1181 AttributeName *an = NULL;
1184 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1185 rs->sr_text = "preread control specified multiple times";
1186 return LDAP_PROTOCOL_ERROR;
1189 if ( ctrl->ldctl_value.bv_len == 0 ) {
1190 rs->sr_text = "preread control value is empty (or absent)";
1191 return LDAP_PROTOCOL_ERROR;
1194 ber = ber_init( &(ctrl->ldctl_value) );
1196 rs->sr_text = "preread control: internal error";
1200 siz = sizeof( AttributeName );
1201 off = offsetof( AttributeName, an_name );
1202 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1203 rs->sr_text = "preread control: decoding error";
1204 return LDAP_PROTOCOL_ERROR;
1207 for( i=0; i<siz; i++ ) {
1208 int rc = LDAP_SUCCESS;
1209 const char *dummy = NULL;
1211 an[i].an_desc = NULL;
1213 an[i].an_oc_exclude = 0;
1214 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1215 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1218 : "postread control: unknown attributeType";
1223 op->o_preread = ctrl->ldctl_iscritical
1224 ? SLAP_CONTROL_CRITICAL
1225 : SLAP_CONTROL_NONCRITICAL;
1227 op->o_preread_attrs = an;
1229 rs->sr_err = LDAP_SUCCESS;
1230 return LDAP_SUCCESS;
1233 static int parsePostRead (
1238 ber_len_t siz, off, i;
1239 AttributeName *an = NULL;
1242 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1243 rs->sr_text = "postread control specified multiple times";
1244 return LDAP_PROTOCOL_ERROR;
1247 if ( ctrl->ldctl_value.bv_len == 0 ) {
1248 rs->sr_text = "postread control value is empty (or absent)";
1249 return LDAP_PROTOCOL_ERROR;
1252 ber = ber_init( &(ctrl->ldctl_value) );
1254 rs->sr_text = "postread control: internal error";
1258 siz = sizeof( AttributeName );
1259 off = offsetof( AttributeName, an_name );
1260 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1261 rs->sr_text = "postread control: decoding error";
1262 return LDAP_PROTOCOL_ERROR;
1265 for( i=0; i<siz; i++ ) {
1266 int rc = LDAP_SUCCESS;
1267 const char *dummy = NULL;
1269 an[i].an_desc = NULL;
1271 an[i].an_oc_exclude = 0;
1272 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1273 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1276 : "postread control: unknown attributeType";
1281 op->o_postread = ctrl->ldctl_iscritical
1282 ? SLAP_CONTROL_CRITICAL
1283 : SLAP_CONTROL_NONCRITICAL;
1285 op->o_postread_attrs = an;
1287 rs->sr_err = LDAP_SUCCESS;
1288 return LDAP_SUCCESS;
1291 static int parseValuesReturnFilter (
1297 struct berval fstr = BER_BVNULL;
1299 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1300 rs->sr_text = "valuesReturnFilter control specified multiple times";
1301 return LDAP_PROTOCOL_ERROR;
1304 if ( ctrl->ldctl_value.bv_len == 0 ) {
1305 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1306 return LDAP_PROTOCOL_ERROR;
1309 ber = ber_init( &(ctrl->ldctl_value) );
1311 rs->sr_text = "internal error";
1315 rs->sr_err = get_vrFilter( op, ber,
1316 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1318 if( rs->sr_err != LDAP_SUCCESS ) {
1319 if( rs->sr_err == SLAPD_DISCONNECT ) {
1320 rs->sr_err = LDAP_PROTOCOL_ERROR;
1321 send_ldap_disconnect( op, rs );
1322 rs->sr_err = SLAPD_DISCONNECT;
1324 send_ldap_result( op, rs );
1326 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1330 vrFilter2bv( op, op->o_vrFilter, &fstr );
1333 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1334 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1335 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1338 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1339 ? SLAP_CONTROL_CRITICAL
1340 : SLAP_CONTROL_NONCRITICAL;
1342 rs->sr_err = LDAP_SUCCESS;
1343 return LDAP_SUCCESS;
1346 static int parseSubentries (
1351 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1352 rs->sr_text = "subentries control specified multiple times";
1353 return LDAP_PROTOCOL_ERROR;
1356 /* FIXME: should use BER library */
1357 if( ( ctrl->ldctl_value.bv_len != 3 )
1358 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1359 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1361 rs->sr_text = "subentries control value encoding is bogus";
1362 return LDAP_PROTOCOL_ERROR;
1365 op->o_subentries = ctrl->ldctl_iscritical
1366 ? SLAP_CONTROL_CRITICAL
1367 : SLAP_CONTROL_NONCRITICAL;
1369 if (ctrl->ldctl_value.bv_val[2]) {
1370 set_subentries_visibility( op );
1373 return LDAP_SUCCESS;
1376 static int parsePermissiveModify (
1381 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1382 rs->sr_text = "permissiveModify control specified multiple times";
1383 return LDAP_PROTOCOL_ERROR;
1386 if ( ctrl->ldctl_value.bv_len ) {
1387 rs->sr_text = "permissiveModify control value not empty";
1388 return LDAP_PROTOCOL_ERROR;
1391 op->o_permissive_modify = ctrl->ldctl_iscritical
1392 ? SLAP_CONTROL_CRITICAL
1393 : SLAP_CONTROL_NONCRITICAL;
1395 return LDAP_SUCCESS;
1398 static int parseDomainScope (
1403 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1404 rs->sr_text = "domainScope control specified multiple times";
1405 return LDAP_PROTOCOL_ERROR;
1408 if ( ctrl->ldctl_value.bv_len ) {
1409 rs->sr_text = "domainScope control value not empty";
1410 return LDAP_PROTOCOL_ERROR;
1413 op->o_domain_scope = ctrl->ldctl_iscritical
1414 ? SLAP_CONTROL_CRITICAL
1415 : SLAP_CONTROL_NONCRITICAL;
1417 return LDAP_SUCCESS;
1420 #ifdef SLAP_CONTROL_X_TREE_DELETE
1421 static int parseTreeDelete (
1426 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1427 rs->sr_text = "treeDelete control specified multiple times";
1428 return LDAP_PROTOCOL_ERROR;
1431 if ( ctrl->ldctl_value.bv_len ) {
1432 rs->sr_text = "treeDelete control value not empty";
1433 return LDAP_PROTOCOL_ERROR;
1436 op->o_tree_delete = ctrl->ldctl_iscritical
1437 ? SLAP_CONTROL_CRITICAL
1438 : SLAP_CONTROL_NONCRITICAL;
1440 return LDAP_SUCCESS;
1444 static int parseSearchOptions (
1450 ber_int_t search_flags;
1453 if ( ctrl->ldctl_value.bv_len == 0 ) {
1454 rs->sr_text = "searchOptions control value is empty (or absent)";
1455 return LDAP_PROTOCOL_ERROR;
1458 ber = ber_init( &ctrl->ldctl_value );
1460 rs->sr_text = "internal error";
1464 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1465 rs->sr_text = "searchOptions control decoding error";
1466 return LDAP_PROTOCOL_ERROR;
1469 (void) ber_free( ber, 1 );
1471 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1472 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1473 rs->sr_text = "searchOptions control specified multiple times "
1474 "or with domainScope control";
1475 return LDAP_PROTOCOL_ERROR;
1478 op->o_domain_scope = ctrl->ldctl_iscritical
1479 ? SLAP_CONTROL_CRITICAL
1480 : SLAP_CONTROL_NONCRITICAL;
1483 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1484 /* Other search flags not recognised so far,
1486 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1488 rs->sr_text = "searchOptions contained unrecognized flag";
1489 return LDAP_UNWILLING_TO_PERFORM;
1492 return LDAP_SUCCESS;