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 op->o_proxy_authz = ctrl->ldctl_iscritical
881 ? SLAP_CONTROL_CRITICAL
882 : SLAP_CONTROL_NONCRITICAL;
884 Debug( LDAP_DEBUG_ARGS,
885 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
887 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
890 if ( ctrl->ldctl_value.bv_len == 0 ) {
891 Debug( LDAP_DEBUG_TRACE,
892 "parseProxyAuthz: conn=%lu anonymous\n",
893 op->o_connid, 0, 0 );
896 /* FIXME: do we let anonymous authz as anonymous?
897 * should we disallow authz at all for anonymous? */
898 if ( !BER_BVISNULL( &op->o_ndn ) ) {
899 op->o_ndn.bv_val[ 0 ] = '\0';
901 op->o_ndn.bv_len = 0;
903 if ( !BER_BVISNULL( &op->o_dn ) ) {
904 op->o_dn.bv_val[ 0 ] = '\0';
911 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
912 NULL, &dn, SLAP_GETDN_AUTHZID );
914 /* FIXME: empty DN in proxyAuthz control should be legal... */
915 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
917 ch_free( dn.bv_val );
919 rs->sr_text = "authzId mapping failed";
920 return LDAP_PROXY_AUTHZ_FAILURE;
923 Debug( LDAP_DEBUG_TRACE,
924 "parseProxyAuthz: conn=%lu \"%s\"\n",
926 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
928 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
931 ch_free( dn.bv_val );
932 rs->sr_text = "not authorized to assume identity";
933 return LDAP_PROXY_AUTHZ_FAILURE;
936 ch_free( op->o_ndn.bv_val );
937 ch_free( op->o_dn.bv_val );
940 * NOTE: since slap_sasl_getdn() returns a normalized dn,
941 * from now on op->o_dn is normalized
944 ber_dupbv( &op->o_dn, &dn );
947 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
948 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
953 static int parseNoOp (
958 if ( op->o_noop != SLAP_CONTROL_NONE ) {
959 rs->sr_text = "noop control specified multiple times";
960 return LDAP_PROTOCOL_ERROR;
963 if ( ctrl->ldctl_value.bv_len ) {
964 rs->sr_text = "noop control value not empty";
965 return LDAP_PROTOCOL_ERROR;
968 op->o_noop = ctrl->ldctl_iscritical
969 ? SLAP_CONTROL_CRITICAL
970 : SLAP_CONTROL_NONCRITICAL;
975 static int parsePagedResults (
980 int rc = LDAP_SUCCESS;
984 struct berval cookie = BER_BVNULL;
985 PagedResultsState *ps;
987 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
988 rs->sr_text = "paged results control specified multiple times";
989 return LDAP_PROTOCOL_ERROR;
992 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
993 rs->sr_text = "paged results control value is empty (or absent)";
994 return LDAP_PROTOCOL_ERROR;
997 /* Parse the control value
998 * realSearchControlValue ::= SEQUENCE {
999 * size INTEGER (0..maxInt),
1000 * -- requested page size from client
1001 * -- result set size estimate from server
1002 * cookie OCTET STRING
1005 ber = ber_init( &ctrl->ldctl_value );
1006 if ( ber == NULL ) {
1007 rs->sr_text = "internal error";
1011 tag = ber_scanf( ber, "{im}", &size, &cookie );
1013 if ( tag == LBER_ERROR ) {
1014 rs->sr_text = "paged results control could not be decoded";
1015 rc = LDAP_PROTOCOL_ERROR;
1020 rs->sr_text = "paged results control size invalid";
1021 rc = LDAP_PROTOCOL_ERROR;
1025 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1026 *ps = op->o_conn->c_pagedresults_state;
1028 op->o_pagedresults_state = ps;
1030 /* NOTE: according to RFC 2696 3.:
1032 If the page size is greater than or equal to the sizeLimit value, the
1033 server should ignore the control as the request can be satisfied in a
1036 * NOTE: this assumes that the op->ors_slimit be set
1037 * before the controls are parsed.
1040 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1041 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1043 } else if ( ctrl->ldctl_iscritical ) {
1044 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1047 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1051 (void)ber_free( ber, 1 );
1056 static int parseSortedResults (
1061 int rc = LDAP_SUCCESS;
1063 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1064 rs->sr_text = "sorted results control specified multiple times";
1065 return LDAP_PROTOCOL_ERROR;
1068 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1069 rs->sr_text = "sorted results control value is empty (or absent)";
1070 return LDAP_PROTOCOL_ERROR;
1073 /* blow off parsing the value */
1075 op->o_sortedresults = ctrl->ldctl_iscritical
1076 ? SLAP_CONTROL_CRITICAL
1077 : SLAP_CONTROL_NONCRITICAL;
1083 static int parseAssert (
1089 struct berval fstr = BER_BVNULL;
1091 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1092 rs->sr_text = "assert control specified multiple times";
1093 return LDAP_PROTOCOL_ERROR;
1096 if ( ctrl->ldctl_value.bv_len == 0 ) {
1097 rs->sr_text = "assert control value is empty (or absent)";
1098 return LDAP_PROTOCOL_ERROR;
1101 ber = ber_init( &(ctrl->ldctl_value) );
1103 rs->sr_text = "assert control: internal error";
1107 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1109 if( rs->sr_err != LDAP_SUCCESS ) {
1110 if( rs->sr_err == SLAPD_DISCONNECT ) {
1111 rs->sr_err = LDAP_PROTOCOL_ERROR;
1112 send_ldap_disconnect( op, rs );
1113 rs->sr_err = SLAPD_DISCONNECT;
1115 send_ldap_result( op, rs );
1117 if( op->o_assertion != NULL ) {
1118 filter_free_x( op, op->o_assertion );
1124 filter2bv_x( op, op->o_assertion, &fstr );
1126 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1127 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1128 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1131 op->o_assert = ctrl->ldctl_iscritical
1132 ? SLAP_CONTROL_CRITICAL
1133 : SLAP_CONTROL_NONCRITICAL;
1135 rs->sr_err = LDAP_SUCCESS;
1136 return LDAP_SUCCESS;
1139 static int parsePreRead (
1144 ber_len_t siz, off, i;
1145 AttributeName *an = NULL;
1148 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1149 rs->sr_text = "preread control specified multiple times";
1150 return LDAP_PROTOCOL_ERROR;
1153 if ( ctrl->ldctl_value.bv_len == 0 ) {
1154 rs->sr_text = "preread control value is empty (or absent)";
1155 return LDAP_PROTOCOL_ERROR;
1158 ber = ber_init( &(ctrl->ldctl_value) );
1160 rs->sr_text = "preread control: internal error";
1164 siz = sizeof( AttributeName );
1165 off = offsetof( AttributeName, an_name );
1166 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1167 rs->sr_text = "preread control: decoding error";
1168 return LDAP_PROTOCOL_ERROR;
1171 for( i=0; i<siz; i++ ) {
1172 int rc = LDAP_SUCCESS;
1173 const char *dummy = NULL;
1175 an[i].an_desc = NULL;
1177 an[i].an_oc_exclude = 0;
1178 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1179 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1182 : "postread control: unknown attributeType";
1187 op->o_preread = ctrl->ldctl_iscritical
1188 ? SLAP_CONTROL_CRITICAL
1189 : SLAP_CONTROL_NONCRITICAL;
1191 op->o_preread_attrs = an;
1193 rs->sr_err = LDAP_SUCCESS;
1194 return LDAP_SUCCESS;
1197 static int parsePostRead (
1202 ber_len_t siz, off, i;
1203 AttributeName *an = NULL;
1206 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1207 rs->sr_text = "postread control specified multiple times";
1208 return LDAP_PROTOCOL_ERROR;
1211 if ( ctrl->ldctl_value.bv_len == 0 ) {
1212 rs->sr_text = "postread control value is empty (or absent)";
1213 return LDAP_PROTOCOL_ERROR;
1216 ber = ber_init( &(ctrl->ldctl_value) );
1218 rs->sr_text = "postread control: internal error";
1222 siz = sizeof( AttributeName );
1223 off = offsetof( AttributeName, an_name );
1224 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1225 rs->sr_text = "postread control: decoding error";
1226 return LDAP_PROTOCOL_ERROR;
1229 for( i=0; i<siz; i++ ) {
1230 int rc = LDAP_SUCCESS;
1231 const char *dummy = NULL;
1233 an[i].an_desc = NULL;
1235 an[i].an_oc_exclude = 0;
1236 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1237 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1240 : "postread control: unknown attributeType";
1245 op->o_postread = ctrl->ldctl_iscritical
1246 ? SLAP_CONTROL_CRITICAL
1247 : SLAP_CONTROL_NONCRITICAL;
1249 op->o_postread_attrs = an;
1251 rs->sr_err = LDAP_SUCCESS;
1252 return LDAP_SUCCESS;
1255 static int parseValuesReturnFilter (
1261 struct berval fstr = BER_BVNULL;
1263 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1264 rs->sr_text = "valuesReturnFilter control specified multiple times";
1265 return LDAP_PROTOCOL_ERROR;
1268 if ( ctrl->ldctl_value.bv_len == 0 ) {
1269 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1270 return LDAP_PROTOCOL_ERROR;
1273 ber = ber_init( &(ctrl->ldctl_value) );
1275 rs->sr_text = "internal error";
1279 rs->sr_err = get_vrFilter( op, ber,
1280 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1282 if( rs->sr_err != LDAP_SUCCESS ) {
1283 if( rs->sr_err == SLAPD_DISCONNECT ) {
1284 rs->sr_err = LDAP_PROTOCOL_ERROR;
1285 send_ldap_disconnect( op, rs );
1286 rs->sr_err = SLAPD_DISCONNECT;
1288 send_ldap_result( op, rs );
1290 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1294 vrFilter2bv( op, op->o_vrFilter, &fstr );
1297 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1298 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1299 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1302 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1303 ? SLAP_CONTROL_CRITICAL
1304 : SLAP_CONTROL_NONCRITICAL;
1306 rs->sr_err = LDAP_SUCCESS;
1307 return LDAP_SUCCESS;
1310 static int parseSubentries (
1315 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1316 rs->sr_text = "subentries control specified multiple times";
1317 return LDAP_PROTOCOL_ERROR;
1320 /* FIXME: should use BER library */
1321 if( ( ctrl->ldctl_value.bv_len != 3 )
1322 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1323 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1325 rs->sr_text = "subentries control value encoding is bogus";
1326 return LDAP_PROTOCOL_ERROR;
1329 op->o_subentries = ctrl->ldctl_iscritical
1330 ? SLAP_CONTROL_CRITICAL
1331 : SLAP_CONTROL_NONCRITICAL;
1333 if (ctrl->ldctl_value.bv_val[2]) {
1334 set_subentries_visibility( op );
1337 return LDAP_SUCCESS;
1340 static int parsePermissiveModify (
1345 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1346 rs->sr_text = "permissiveModify control specified multiple times";
1347 return LDAP_PROTOCOL_ERROR;
1350 if ( ctrl->ldctl_value.bv_len ) {
1351 rs->sr_text = "permissiveModify control value not empty";
1352 return LDAP_PROTOCOL_ERROR;
1355 op->o_permissive_modify = ctrl->ldctl_iscritical
1356 ? SLAP_CONTROL_CRITICAL
1357 : SLAP_CONTROL_NONCRITICAL;
1359 return LDAP_SUCCESS;
1362 static int parseDomainScope (
1367 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1368 rs->sr_text = "domainScope control specified multiple times";
1369 return LDAP_PROTOCOL_ERROR;
1372 if ( ctrl->ldctl_value.bv_len ) {
1373 rs->sr_text = "domainScope control value not empty";
1374 return LDAP_PROTOCOL_ERROR;
1377 op->o_domain_scope = ctrl->ldctl_iscritical
1378 ? SLAP_CONTROL_CRITICAL
1379 : SLAP_CONTROL_NONCRITICAL;
1381 return LDAP_SUCCESS;
1384 #ifdef SLAP_CONTROL_X_TREE_DELETE
1385 static int parseTreeDelete (
1390 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1391 rs->sr_text = "treeDelete control specified multiple times";
1392 return LDAP_PROTOCOL_ERROR;
1395 if ( ctrl->ldctl_value.bv_len ) {
1396 rs->sr_text = "treeDelete control value not empty";
1397 return LDAP_PROTOCOL_ERROR;
1400 op->o_tree_delete = ctrl->ldctl_iscritical
1401 ? SLAP_CONTROL_CRITICAL
1402 : SLAP_CONTROL_NONCRITICAL;
1404 return LDAP_SUCCESS;
1408 static int parseSearchOptions (
1414 ber_int_t search_flags;
1417 if ( ctrl->ldctl_value.bv_len == 0 ) {
1418 rs->sr_text = "searchOptions control value is empty (or absent)";
1419 return LDAP_PROTOCOL_ERROR;
1422 ber = ber_init( &ctrl->ldctl_value );
1424 rs->sr_text = "internal error";
1428 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1429 rs->sr_text = "searchOptions control decoding error";
1430 return LDAP_PROTOCOL_ERROR;
1433 (void) ber_free( ber, 1 );
1435 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1436 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1437 rs->sr_text = "searchOptions control specified multiple times "
1438 "or with domainScope control";
1439 return LDAP_PROTOCOL_ERROR;
1442 op->o_domain_scope = ctrl->ldctl_iscritical
1443 ? SLAP_CONTROL_CRITICAL
1444 : SLAP_CONTROL_NONCRITICAL;
1447 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1448 /* Other search flags not recognised so far,
1450 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1452 rs->sr_text = "searchOptions contained unrecognized flag";
1453 return LDAP_UNWILLING_TO_PERFORM;
1456 return LDAP_SUCCESS;