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) }
201 * Register a supported control.
203 * This can be called by an OpenLDAP plugin or, indirectly, by a
204 * SLAPI plugin calling slapi_register_supported_control().
207 register_supported_control(const char *controloid,
208 slap_mask_t controlmask,
210 SLAP_CTRL_PARSE_FN *controlparsefn,
213 struct slap_control *sc;
216 if ( num_known_controls >= SLAP_MAX_CIDS ) {
217 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
218 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
219 SLAP_MAX_CIDS, 0, 0 );
223 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
225 /* sanity check - should never happen */
226 for ( i = 0; slap_known_controls[ i ]; i++ ) {
227 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
228 Debug( LDAP_DEBUG_ANY,
229 "Control %s already registered.\n",
231 return LDAP_PARAM_ERROR;
235 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
236 if ( sc == NULL ) return LDAP_NO_MEMORY;
238 sc->sc_oid = ch_strdup( controloid );
239 sc->sc_mask = controlmask;
240 if ( controlexops != NULL ) {
243 for ( i = 0; controlexops[ i ]; i++ );
245 sc->sc_extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
246 if ( sc->sc_extendedopsbv == NULL ) {
248 return LDAP_NO_MEMORY;
251 for ( i = 0; controlexops[ i ]; i++ ) {
252 ber_str2bv( controlexops[ i ], 0, 1, &sc->sc_extendedopsbv[ i ] );
256 sc->sc_extendedopsbv = NULL;
258 sc->sc_extendedops = NULL;
259 sc->sc_parse = controlparsefn;
261 if ( controlcid ) *controlcid = num_known_controls;
262 sc->sc_cid = num_known_controls;
264 /* Update slap_known_controls, too. */
265 slap_known_controls[num_known_controls-1] = sc->sc_oid;
266 slap_known_controls[num_known_controls++] = NULL;
268 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
269 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
274 * One-time initialization of internal controls.
277 slap_controls_init( void )
283 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
284 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
285 rc = register_supported_control( control_defs[i].sc_oid,
286 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
287 control_defs[i].sc_parse, cid );
288 if ( rc != LDAP_SUCCESS ) break;
295 * Free memory associated with list of supported controls.
298 controls_destroy( void )
300 struct slap_control *sc;
302 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
303 sc = LDAP_SLIST_FIRST(&controls_list);
304 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
306 ch_free( sc->sc_oid );
307 if ( sc->sc_extendedopsbv != NULL ) {
308 ber_bvarray_free( sc->sc_extendedopsbv );
315 * Format the supportedControl attribute of the root DSE,
316 * detailing which controls are supported by the directory
320 controls_root_dse_info( Entry *e )
322 AttributeDescription *ad_supportedControl
323 = slap_schema.si_ad_supportedControl;
324 struct berval vals[2];
325 struct slap_control *sc;
327 vals[1].bv_val = NULL;
330 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
331 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
333 vals[0].bv_val = sc->sc_oid;
334 vals[0].bv_len = strlen( sc->sc_oid );
336 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
345 * Return a list of OIDs and operation masks for supported
346 * controls. Used by SLAPI.
349 get_supported_controls(char ***ctrloidsp,
350 slap_mask_t **ctrlmasks)
355 struct slap_control *sc;
359 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
369 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
370 if ( oids == NULL ) {
371 return LDAP_NO_MEMORY;
373 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
374 if ( masks == NULL ) {
376 return LDAP_NO_MEMORY;
381 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
382 oids[n] = ch_strdup( sc->sc_oid );
383 masks[n] = sc->sc_mask;
396 * Find a control given its OID.
398 static struct slap_control *
399 find_ctrl( const char *oid )
401 struct slap_control *sc;
403 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
404 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
413 slap_find_control_id(
417 struct slap_control *ctrl = find_ctrl( oid );
419 if ( cid ) *cid = ctrl->sc_cid;
422 return LDAP_CONTROL_NOT_FOUND;
426 slap_global_control( Operation *op, const char *oid, int *cid )
428 struct slap_control *ctrl = find_ctrl( oid );
430 if ( ctrl == NULL ) {
431 /* should not be reachable */
432 Debug( LDAP_DEBUG_ANY,
433 "slap_global_control: unrecognized control: %s\n",
435 return LDAP_CONTROL_NOT_FOUND;
438 if ( cid ) *cid = ctrl->sc_cid;
440 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
441 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
442 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
444 return LDAP_COMPARE_TRUE;
447 Debug( LDAP_DEBUG_TRACE,
448 "slap_global_control: unavailable control: %s\n",
451 return LDAP_COMPARE_FALSE;
454 void slap_free_ctrls(
456 LDAPControl **ctrls )
460 for (i=0; ctrls[i]; i++) {
461 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
463 op->o_tmpfree( ctrls, op->o_tmpmemctx );
469 LDAPControl *control,
472 struct slap_control *sc;
474 sc = find_ctrl( control->ldctl_oid );
476 /* recognized control */
478 switch( op->o_tag ) {
480 tagmask = SLAP_CTRL_ADD;
483 tagmask = SLAP_CTRL_BIND;
485 case LDAP_REQ_COMPARE:
486 tagmask = SLAP_CTRL_COMPARE;
488 case LDAP_REQ_DELETE:
489 tagmask = SLAP_CTRL_DELETE;
491 case LDAP_REQ_MODIFY:
492 tagmask = SLAP_CTRL_MODIFY;
494 case LDAP_REQ_RENAME:
495 tagmask = SLAP_CTRL_RENAME;
497 case LDAP_REQ_SEARCH:
498 tagmask = SLAP_CTRL_SEARCH;
500 case LDAP_REQ_UNBIND:
501 tagmask = SLAP_CTRL_UNBIND;
503 case LDAP_REQ_ABANDON:
504 tagmask = SLAP_CTRL_ABANDON;
506 case LDAP_REQ_EXTENDED:
508 assert( op->ore_reqoid.bv_val != NULL );
509 if( sc->sc_extendedopsbv != NULL ) {
511 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
512 if( bvmatch( &op->ore_reqoid,
513 &sc->sc_extendedopsbv[i] ) )
522 *text = "controls internal error";
526 if (( sc->sc_mask & tagmask ) == tagmask ) {
527 /* available extension */
530 if( !sc->sc_parse ) {
531 *text = "not yet implemented";
535 rc = sc->sc_parse( op, rs, control );
537 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
541 } else if( control->ldctl_iscritical ) {
542 /* unavailable CRITICAL control */
543 *text = "critical extension is unavailable";
544 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
546 } else if( control->ldctl_iscritical ) {
547 /* unrecognized CRITICAL control */
548 *text = "critical extension is not recognized";
549 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
564 BerElement *ber = op->o_ber;
567 len = ber_pvt_ber_remaining(ber);
571 rs->sr_err = LDAP_SUCCESS;
575 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
576 if( tag == LBER_ERROR ) {
577 rs->sr_err = SLAPD_DISCONNECT;
578 rs->sr_text = "unexpected data in PDU";
584 Debug( LDAP_DEBUG_TRACE,
585 "=> get_ctrls\n", 0, 0, 0 );
587 if( op->o_protocol < LDAP_VERSION3 ) {
588 rs->sr_err = SLAPD_DISCONNECT;
589 rs->sr_text = "controls require LDAPv3";
593 /* one for first control, one for termination */
594 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
597 if( op->ctrls == NULL ) {
598 rs->sr_err = LDAP_NO_MEMORY;
599 rs->sr_text = "no memory";
604 op->o_ctrls[nctrls] = NULL;
606 /* step through each element */
607 for( tag = ber_first_element( ber, &len, &opaque );
609 tag = ber_next_element( ber, &len, opaque ) )
612 LDAPControl **tctrls;
614 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
615 memset(c, 0, sizeof(LDAPControl));
617 /* allocate pointer space for current controls (nctrls)
618 * + this control + extra NULL
620 tctrls = op->o_tmprealloc( op->o_ctrls,
621 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
624 if( tctrls == NULL ) {
626 ldap_controls_free(op->o_ctrls);
629 rs->sr_err = LDAP_NO_MEMORY;
630 rs->sr_text = "no memory";
634 op->o_ctrls = tctrls;
636 op->o_ctrls[nctrls++] = c;
637 op->o_ctrls[nctrls] = NULL;
639 tag = ber_scanf( ber, "{m" /*}*/, &bv );
640 c->ldctl_oid = bv.bv_val;
642 if( tag == LBER_ERROR ) {
643 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
646 slap_free_ctrls( op, op->o_ctrls );
648 rs->sr_err = SLAPD_DISCONNECT;
649 rs->sr_text = "decoding controls error";
652 } else if( c->ldctl_oid == NULL ) {
653 Debug( LDAP_DEBUG_TRACE,
654 "get_ctrls: conn %lu got emtpy OID.\n",
655 op->o_connid, 0, 0 );
657 slap_free_ctrls( op, op->o_ctrls );
659 rs->sr_err = LDAP_PROTOCOL_ERROR;
660 rs->sr_text = "OID field is empty";
664 tag = ber_peek_tag( ber, &len );
666 if( tag == LBER_BOOLEAN ) {
668 tag = ber_scanf( ber, "b", &crit );
670 if( tag == LBER_ERROR ) {
671 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
673 slap_free_ctrls( op, op->o_ctrls );
675 rs->sr_err = SLAPD_DISCONNECT;
676 rs->sr_text = "decoding controls error";
680 c->ldctl_iscritical = (crit != 0);
681 tag = ber_peek_tag( ber, &len );
684 if( tag == LBER_OCTETSTRING ) {
685 tag = ber_scanf( ber, "m", &c->ldctl_value );
687 if( tag == LBER_ERROR ) {
688 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
689 "%s (%scritical): get value failed.\n",
690 op->o_connid, c->ldctl_oid,
691 c->ldctl_iscritical ? "" : "non" );
692 slap_free_ctrls( op, op->o_ctrls );
694 rs->sr_err = SLAPD_DISCONNECT;
695 rs->sr_text = "decoding controls error";
700 Debug( LDAP_DEBUG_TRACE,
701 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
702 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
704 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
705 if ( rs->sr_err != LDAP_SUCCESS ) {
711 Debug( LDAP_DEBUG_TRACE,
712 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
713 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
715 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
716 if( rs->sr_err == SLAPD_DISCONNECT ) {
717 rs->sr_err = LDAP_PROTOCOL_ERROR;
718 send_ldap_disconnect( op, rs );
719 rs->sr_err = SLAPD_DISCONNECT;
721 send_ldap_result( op, rs );
733 BI_chk_controls fnc )
737 switch ( op->o_ctrlflag[ ctrl ] ) {
738 case SLAP_CONTROL_NONCRITICAL:
739 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
740 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
741 slap_known_controls[ ctrl - 1 ] ) == 0 )
748 rs->sr_err = LDAP_OTHER;
756 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
759 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
760 ( i - j ) * sizeof( LDAPControl * ) );
763 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
767 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
769 Debug( LDAP_DEBUG_ANY, "%s: "
770 "non-critical control \"%s\" not supported; stripped.\n",
771 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
774 case SLAP_CONTROL_IGNORED:
775 case SLAP_CONTROL_NONE:
776 rs->sr_err = SLAP_CB_CONTINUE;
779 case SLAP_CONTROL_CRITICAL:
780 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
784 Debug( LDAP_DEBUG_ANY, "%s: "
785 "critical control \"%s\" not supported.\n",
786 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
790 /* handle all cases! */
798 static int parseDontUseCopy (
803 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
804 rs->sr_text = "dontUseCopy control specified multiple times";
805 return LDAP_PROTOCOL_ERROR;
808 if ( ctrl->ldctl_value.bv_len ) {
809 rs->sr_text = "dontUseCopy control value not empty";
810 return LDAP_PROTOCOL_ERROR;
813 if ( ctrl->ldctl_iscritical != SLAP_CONTROL_CRITICAL ) {
814 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
815 return LDAP_PROTOCOL_ERROR;
818 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
822 static int parseManageDIT (
827 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
828 rs->sr_text = "manageDIT control specified multiple times";
829 return LDAP_PROTOCOL_ERROR;
832 if ( ctrl->ldctl_value.bv_len ) {
833 rs->sr_text = "manageDIT control value not empty";
834 return LDAP_PROTOCOL_ERROR;
837 op->o_managedit = ctrl->ldctl_iscritical
838 ? SLAP_CONTROL_CRITICAL
839 : SLAP_CONTROL_NONCRITICAL;
845 static int parseManageDSAit (
850 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
851 rs->sr_text = "manageDSAit control specified multiple times";
852 return LDAP_PROTOCOL_ERROR;
855 if ( ctrl->ldctl_value.bv_len ) {
856 rs->sr_text = "manageDSAit control value not empty";
857 return LDAP_PROTOCOL_ERROR;
860 op->o_managedsait = ctrl->ldctl_iscritical
861 ? SLAP_CONTROL_CRITICAL
862 : SLAP_CONTROL_NONCRITICAL;
867 static int parseProxyAuthz (
873 struct berval dn = BER_BVNULL;
875 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
876 rs->sr_text = "proxy authorization control specified multiple times";
877 return LDAP_PROTOCOL_ERROR;
880 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
881 && BER_BVISEMPTY( &op->o_ndn ) )
883 rs->sr_text = "anonymous proxyAuthz not allowed";
884 return LDAP_PROXY_AUTHZ_FAILURE;
887 op->o_proxy_authz = ctrl->ldctl_iscritical
888 ? SLAP_CONTROL_CRITICAL
889 : SLAP_CONTROL_NONCRITICAL;
891 Debug( LDAP_DEBUG_ARGS,
892 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
894 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
897 if ( ctrl->ldctl_value.bv_len == 0 ) {
898 Debug( LDAP_DEBUG_TRACE,
899 "parseProxyAuthz: conn=%lu anonymous\n",
900 op->o_connid, 0, 0 );
903 if ( !BER_BVISNULL( &op->o_ndn ) ) {
904 op->o_ndn.bv_val[ 0 ] = '\0';
906 op->o_ndn.bv_len = 0;
908 if ( !BER_BVISNULL( &op->o_dn ) ) {
909 op->o_dn.bv_val[ 0 ] = '\0';
916 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
917 NULL, &dn, SLAP_GETDN_AUTHZID );
919 /* FIXME: empty DN in proxyAuthz control should be legal... */
920 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
922 ch_free( dn.bv_val );
924 rs->sr_text = "authzId mapping failed";
925 return LDAP_PROXY_AUTHZ_FAILURE;
928 Debug( LDAP_DEBUG_TRACE,
929 "parseProxyAuthz: conn=%lu \"%s\"\n",
931 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
933 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
936 ch_free( dn.bv_val );
937 rs->sr_text = "not authorized to assume identity";
938 return LDAP_PROXY_AUTHZ_FAILURE;
941 ch_free( op->o_ndn.bv_val );
942 ch_free( op->o_dn.bv_val );
945 * NOTE: since slap_sasl_getdn() returns a normalized dn,
946 * from now on op->o_dn is normalized
949 ber_dupbv( &op->o_dn, &dn );
952 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
953 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
958 static int parseNoOp (
963 if ( op->o_noop != SLAP_CONTROL_NONE ) {
964 rs->sr_text = "noop control specified multiple times";
965 return LDAP_PROTOCOL_ERROR;
968 if ( ctrl->ldctl_value.bv_len ) {
969 rs->sr_text = "noop control value not empty";
970 return LDAP_PROTOCOL_ERROR;
973 op->o_noop = ctrl->ldctl_iscritical
974 ? SLAP_CONTROL_CRITICAL
975 : SLAP_CONTROL_NONCRITICAL;
980 static int parsePagedResults (
985 int rc = LDAP_SUCCESS;
989 struct berval cookie = BER_BVNULL;
990 PagedResultsState *ps;
992 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
993 rs->sr_text = "paged results control specified multiple times";
994 return LDAP_PROTOCOL_ERROR;
997 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
998 rs->sr_text = "paged results control value is empty (or absent)";
999 return LDAP_PROTOCOL_ERROR;
1002 /* Parse the control value
1003 * realSearchControlValue ::= SEQUENCE {
1004 * size INTEGER (0..maxInt),
1005 * -- requested page size from client
1006 * -- result set size estimate from server
1007 * cookie OCTET STRING
1010 ber = ber_init( &ctrl->ldctl_value );
1011 if ( ber == NULL ) {
1012 rs->sr_text = "internal error";
1016 tag = ber_scanf( ber, "{im}", &size, &cookie );
1018 if ( tag == LBER_ERROR ) {
1019 rs->sr_text = "paged results control could not be decoded";
1020 rc = LDAP_PROTOCOL_ERROR;
1025 rs->sr_text = "paged results control size invalid";
1026 rc = LDAP_PROTOCOL_ERROR;
1030 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1031 *ps = op->o_conn->c_pagedresults_state;
1033 op->o_pagedresults_state = ps;
1035 /* NOTE: according to RFC 2696 3.:
1037 If the page size is greater than or equal to the sizeLimit value, the
1038 server should ignore the control as the request can be satisfied in a
1041 * NOTE: this assumes that the op->ors_slimit be set
1042 * before the controls are parsed.
1045 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1046 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1048 } else if ( ctrl->ldctl_iscritical ) {
1049 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1052 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1056 (void)ber_free( ber, 1 );
1061 static int parseSortedResults (
1066 int rc = LDAP_SUCCESS;
1068 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1069 rs->sr_text = "sorted results control specified multiple times";
1070 return LDAP_PROTOCOL_ERROR;
1073 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1074 rs->sr_text = "sorted results control value is empty (or absent)";
1075 return LDAP_PROTOCOL_ERROR;
1078 /* blow off parsing the value */
1080 op->o_sortedresults = ctrl->ldctl_iscritical
1081 ? SLAP_CONTROL_CRITICAL
1082 : SLAP_CONTROL_NONCRITICAL;
1088 static int parseAssert (
1094 struct berval fstr = BER_BVNULL;
1096 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1097 rs->sr_text = "assert control specified multiple times";
1098 return LDAP_PROTOCOL_ERROR;
1101 if ( ctrl->ldctl_value.bv_len == 0 ) {
1102 rs->sr_text = "assert control value is empty (or absent)";
1103 return LDAP_PROTOCOL_ERROR;
1106 ber = ber_init( &(ctrl->ldctl_value) );
1108 rs->sr_text = "assert control: internal error";
1112 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1114 if( rs->sr_err != LDAP_SUCCESS ) {
1115 if( rs->sr_err == SLAPD_DISCONNECT ) {
1116 rs->sr_err = LDAP_PROTOCOL_ERROR;
1117 send_ldap_disconnect( op, rs );
1118 rs->sr_err = SLAPD_DISCONNECT;
1120 send_ldap_result( op, rs );
1122 if( op->o_assertion != NULL ) {
1123 filter_free_x( op, op->o_assertion );
1129 filter2bv_x( op, op->o_assertion, &fstr );
1131 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1132 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1133 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1136 op->o_assert = ctrl->ldctl_iscritical
1137 ? SLAP_CONTROL_CRITICAL
1138 : SLAP_CONTROL_NONCRITICAL;
1140 rs->sr_err = LDAP_SUCCESS;
1141 return LDAP_SUCCESS;
1144 static int parsePreRead (
1149 ber_len_t siz, off, i;
1150 AttributeName *an = NULL;
1153 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1154 rs->sr_text = "preread control specified multiple times";
1155 return LDAP_PROTOCOL_ERROR;
1158 if ( ctrl->ldctl_value.bv_len == 0 ) {
1159 rs->sr_text = "preread control value is empty (or absent)";
1160 return LDAP_PROTOCOL_ERROR;
1163 ber = ber_init( &(ctrl->ldctl_value) );
1165 rs->sr_text = "preread control: internal error";
1169 siz = sizeof( AttributeName );
1170 off = offsetof( AttributeName, an_name );
1171 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1172 rs->sr_text = "preread control: decoding error";
1173 return LDAP_PROTOCOL_ERROR;
1176 for( i=0; i<siz; i++ ) {
1177 int rc = LDAP_SUCCESS;
1178 const char *dummy = NULL;
1180 an[i].an_desc = NULL;
1182 an[i].an_oc_exclude = 0;
1183 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1184 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1187 : "postread control: unknown attributeType";
1192 op->o_preread = ctrl->ldctl_iscritical
1193 ? SLAP_CONTROL_CRITICAL
1194 : SLAP_CONTROL_NONCRITICAL;
1196 op->o_preread_attrs = an;
1198 rs->sr_err = LDAP_SUCCESS;
1199 return LDAP_SUCCESS;
1202 static int parsePostRead (
1207 ber_len_t siz, off, i;
1208 AttributeName *an = NULL;
1211 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1212 rs->sr_text = "postread control specified multiple times";
1213 return LDAP_PROTOCOL_ERROR;
1216 if ( ctrl->ldctl_value.bv_len == 0 ) {
1217 rs->sr_text = "postread control value is empty (or absent)";
1218 return LDAP_PROTOCOL_ERROR;
1221 ber = ber_init( &(ctrl->ldctl_value) );
1223 rs->sr_text = "postread control: internal error";
1227 siz = sizeof( AttributeName );
1228 off = offsetof( AttributeName, an_name );
1229 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1230 rs->sr_text = "postread control: decoding error";
1231 return LDAP_PROTOCOL_ERROR;
1234 for( i=0; i<siz; i++ ) {
1235 int rc = LDAP_SUCCESS;
1236 const char *dummy = NULL;
1238 an[i].an_desc = NULL;
1240 an[i].an_oc_exclude = 0;
1241 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1242 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1245 : "postread control: unknown attributeType";
1250 op->o_postread = ctrl->ldctl_iscritical
1251 ? SLAP_CONTROL_CRITICAL
1252 : SLAP_CONTROL_NONCRITICAL;
1254 op->o_postread_attrs = an;
1256 rs->sr_err = LDAP_SUCCESS;
1257 return LDAP_SUCCESS;
1260 static int parseValuesReturnFilter (
1266 struct berval fstr = BER_BVNULL;
1268 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1269 rs->sr_text = "valuesReturnFilter control specified multiple times";
1270 return LDAP_PROTOCOL_ERROR;
1273 if ( ctrl->ldctl_value.bv_len == 0 ) {
1274 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1275 return LDAP_PROTOCOL_ERROR;
1278 ber = ber_init( &(ctrl->ldctl_value) );
1280 rs->sr_text = "internal error";
1284 rs->sr_err = get_vrFilter( op, ber,
1285 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1287 if( rs->sr_err != LDAP_SUCCESS ) {
1288 if( rs->sr_err == SLAPD_DISCONNECT ) {
1289 rs->sr_err = LDAP_PROTOCOL_ERROR;
1290 send_ldap_disconnect( op, rs );
1291 rs->sr_err = SLAPD_DISCONNECT;
1293 send_ldap_result( op, rs );
1295 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1299 vrFilter2bv( op, op->o_vrFilter, &fstr );
1302 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1303 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1304 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1307 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1308 ? SLAP_CONTROL_CRITICAL
1309 : SLAP_CONTROL_NONCRITICAL;
1311 rs->sr_err = LDAP_SUCCESS;
1312 return LDAP_SUCCESS;
1315 static int parseSubentries (
1320 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1321 rs->sr_text = "subentries control specified multiple times";
1322 return LDAP_PROTOCOL_ERROR;
1325 /* FIXME: should use BER library */
1326 if( ( ctrl->ldctl_value.bv_len != 3 )
1327 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1328 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1330 rs->sr_text = "subentries control value encoding is bogus";
1331 return LDAP_PROTOCOL_ERROR;
1334 op->o_subentries = ctrl->ldctl_iscritical
1335 ? SLAP_CONTROL_CRITICAL
1336 : SLAP_CONTROL_NONCRITICAL;
1338 if (ctrl->ldctl_value.bv_val[2]) {
1339 set_subentries_visibility( op );
1342 return LDAP_SUCCESS;
1345 static int parsePermissiveModify (
1350 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1351 rs->sr_text = "permissiveModify control specified multiple times";
1352 return LDAP_PROTOCOL_ERROR;
1355 if ( ctrl->ldctl_value.bv_len ) {
1356 rs->sr_text = "permissiveModify control value not empty";
1357 return LDAP_PROTOCOL_ERROR;
1360 op->o_permissive_modify = ctrl->ldctl_iscritical
1361 ? SLAP_CONTROL_CRITICAL
1362 : SLAP_CONTROL_NONCRITICAL;
1364 return LDAP_SUCCESS;
1367 static int parseDomainScope (
1372 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1373 rs->sr_text = "domainScope control specified multiple times";
1374 return LDAP_PROTOCOL_ERROR;
1377 if ( ctrl->ldctl_value.bv_len ) {
1378 rs->sr_text = "domainScope control value not empty";
1379 return LDAP_PROTOCOL_ERROR;
1382 op->o_domain_scope = ctrl->ldctl_iscritical
1383 ? SLAP_CONTROL_CRITICAL
1384 : SLAP_CONTROL_NONCRITICAL;
1386 return LDAP_SUCCESS;
1389 #ifdef SLAP_CONTROL_X_TREE_DELETE
1390 static int parseTreeDelete (
1395 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1396 rs->sr_text = "treeDelete control specified multiple times";
1397 return LDAP_PROTOCOL_ERROR;
1400 if ( ctrl->ldctl_value.bv_len ) {
1401 rs->sr_text = "treeDelete control value not empty";
1402 return LDAP_PROTOCOL_ERROR;
1405 op->o_tree_delete = ctrl->ldctl_iscritical
1406 ? SLAP_CONTROL_CRITICAL
1407 : SLAP_CONTROL_NONCRITICAL;
1409 return LDAP_SUCCESS;
1413 static int parseSearchOptions (
1419 ber_int_t search_flags;
1422 if ( ctrl->ldctl_value.bv_len == 0 ) {
1423 rs->sr_text = "searchOptions control value is empty (or absent)";
1424 return LDAP_PROTOCOL_ERROR;
1427 ber = ber_init( &ctrl->ldctl_value );
1429 rs->sr_text = "internal error";
1433 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1434 rs->sr_text = "searchOptions control decoding error";
1435 return LDAP_PROTOCOL_ERROR;
1438 (void) ber_free( ber, 1 );
1440 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1441 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1442 rs->sr_text = "searchOptions control specified multiple times "
1443 "or with domainScope control";
1444 return LDAP_PROTOCOL_ERROR;
1447 op->o_domain_scope = ctrl->ldctl_iscritical
1448 ? SLAP_CONTROL_CRITICAL
1449 : SLAP_CONTROL_NONCRITICAL;
1452 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1453 /* Other search flags not recognised so far,
1455 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1457 rs->sr_text = "searchOptions contained unrecognized flag";
1458 return LDAP_UNWILLING_TO_PERFORM;
1461 return LDAP_SUCCESS;