2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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;
31 static SLAP_CTRL_PARSE_FN parseManageDSAit;
32 static SLAP_CTRL_PARSE_FN parseModifyIncrement;
33 static SLAP_CTRL_PARSE_FN parseNoOp;
34 static SLAP_CTRL_PARSE_FN parsePagedResults;
35 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
36 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
37 static SLAP_CTRL_PARSE_FN parseDomainScope;
38 static SLAP_CTRL_PARSE_FN parseTreeDelete;
39 static SLAP_CTRL_PARSE_FN parseSearchOptions;
41 #ifdef LDAP_CONTROL_SUBENTRIES
42 static SLAP_CTRL_PARSE_FN parseSubentries;
45 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
47 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
48 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
50 struct slap_control_ids slap_cids;
56 /* The controlID for this control */
59 /* Operations supported by control */
62 /* Extended operations supported by control */
63 char **sc_extendedops;
65 /* Control parsing callback */
66 SLAP_CTRL_PARSE_FN *sc_parse;
68 LDAP_SLIST_ENTRY(slap_control) sc_next;
71 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
72 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
75 * all known request control OIDs should be added to this list
78 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
79 * addresses an undefined control; this allows to safely test for
80 * well known controls even if they are not registered, e.g. if
81 * they get moved to modules. An example is sc_LDAPsync, which
82 * is implemented in the syncprov overlay and thus, if configured
83 * as dynamic module, may not be registered. One side effect is that
84 * slap_known_controls[0] == NULL, so it should always be used
86 * FIXME: should we define the "undefined control" oid?
88 char *slap_known_controls[SLAP_MAX_CIDS+1];
89 static int num_known_controls = 1;
91 static char *proxy_authz_extops[] = {
92 LDAP_EXOP_MODIFY_PASSWD,
97 static struct slap_control control_defs[] = {
98 { LDAP_CONTROL_ASSERT,
99 (int)offsetof(struct slap_control_ids, sc_assert),
100 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
101 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
102 { LDAP_CONTROL_PRE_READ,
103 (int)offsetof(struct slap_control_ids, sc_preRead),
104 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
105 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
106 { LDAP_CONTROL_POST_READ,
107 (int)offsetof(struct slap_control_ids, sc_postRead),
108 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
109 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
110 { LDAP_CONTROL_VALUESRETURNFILTER,
111 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
112 SLAP_CTRL_SEARCH, NULL,
113 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
114 { LDAP_CONTROL_PAGEDRESULTS,
115 (int)offsetof(struct slap_control_ids, sc_pagedResults),
116 SLAP_CTRL_SEARCH, NULL,
117 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
118 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
119 { LDAP_CONTROL_X_DOMAIN_SCOPE,
120 (int)offsetof(struct slap_control_ids, sc_domainScope),
121 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
122 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
124 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
125 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
126 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
127 SLAP_CTRL_MODIFY, NULL,
128 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
130 #ifdef LDAP_CONTROL_X_TREE_DELETE
131 { LDAP_CONTROL_X_TREE_DELETE,
132 (int)offsetof(struct slap_control_ids, sc_treeDelete),
133 SLAP_CTRL_DELETE, NULL,
134 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
136 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
137 { LDAP_CONTORL_X_SEARCH_OPTIONS,
138 (int)offsetof(struct slap_control_ids, sc_searchOptions),
139 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
140 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
142 #ifdef LDAP_CONTROL_SUBENTRIES
143 { LDAP_CONTROL_SUBENTRIES,
144 (int)offsetof(struct slap_control_ids, sc_subentries),
145 SLAP_CTRL_SEARCH, NULL,
146 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149 (int)offsetof(struct slap_control_ids, sc_noOp),
150 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
151 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
152 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
153 { LDAP_CONTROL_MODIFY_INCREMENT,
154 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
155 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
156 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
158 { LDAP_CONTROL_MANAGEDSAIT,
159 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
160 SLAP_CTRL_ACCESS, NULL,
161 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
162 { LDAP_CONTROL_PROXY_AUTHZ,
163 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
164 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
165 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
166 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
170 * Register a supported control.
172 * This can be called by an OpenLDAP plugin or, indirectly, by a
173 * SLAPI plugin calling slapi_register_supported_control().
176 register_supported_control(const char *controloid,
177 slap_mask_t controlmask,
179 SLAP_CTRL_PARSE_FN *controlparsefn,
182 struct slap_control *sc;
185 if ( num_known_controls >= SLAP_MAX_CIDS ) {
186 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
187 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
188 SLAP_MAX_CIDS, 0, 0 );
192 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
194 /* sanity check - should never happen */
195 for ( i = 0; slap_known_controls[ i ]; i++ ) {
196 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
197 Debug( LDAP_DEBUG_ANY,
198 "Control %s already registered.\n",
200 return LDAP_PARAM_ERROR;
204 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
205 if ( sc == NULL ) return LDAP_NO_MEMORY;
207 sc->sc_oid = ch_strdup( controloid );
208 sc->sc_mask = controlmask;
209 if ( controlexops != NULL ) {
210 sc->sc_extendedops = ldap_charray_dup( controlexops );
211 if ( sc->sc_extendedops == NULL ) {
213 return LDAP_NO_MEMORY;
216 sc->sc_extendedops = NULL;
218 sc->sc_parse = controlparsefn;
220 if ( controlcid ) *controlcid = num_known_controls;
221 sc->sc_cid = num_known_controls;
223 /* Update slap_known_controls, too. */
224 slap_known_controls[num_known_controls-1] = sc->sc_oid;
225 slap_known_controls[num_known_controls++] = NULL;
227 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
228 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
233 * One-time initialization of internal controls.
236 slap_controls_init( void )
239 struct slap_control *sc;
243 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
244 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
245 rc = register_supported_control( control_defs[i].sc_oid,
246 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
247 control_defs[i].sc_parse, cid );
248 if ( rc != LDAP_SUCCESS ) break;
255 * Free memory associated with list of supported controls.
258 controls_destroy( void )
260 struct slap_control *sc;
262 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
263 sc = LDAP_SLIST_FIRST(&controls_list);
264 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
266 ch_free( sc->sc_oid );
267 if ( sc->sc_extendedops != NULL ) {
268 ldap_charray_free( sc->sc_extendedops );
275 * Format the supportedControl attribute of the root DSE,
276 * detailing which controls are supported by the directory
280 controls_root_dse_info( Entry *e )
282 AttributeDescription *ad_supportedControl
283 = slap_schema.si_ad_supportedControl;
284 struct berval vals[2];
285 struct slap_control *sc;
287 vals[1].bv_val = NULL;
290 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
291 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
293 vals[0].bv_val = sc->sc_oid;
294 vals[0].bv_len = strlen( sc->sc_oid );
296 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
305 * Return a list of OIDs and operation masks for supported
306 * controls. Used by SLAPI.
309 get_supported_controls(char ***ctrloidsp,
310 slap_mask_t **ctrlmasks)
316 struct slap_control *sc;
320 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
330 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
331 if ( oids == NULL ) {
332 return LDAP_NO_MEMORY;
334 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
335 if ( masks == NULL ) {
337 return LDAP_NO_MEMORY;
342 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
343 oids[n] = ch_strdup( sc->sc_oid );
344 masks[n] = sc->sc_mask;
357 * Find a control given its OID.
359 static struct slap_control *
360 find_ctrl( const char *oid )
362 struct slap_control *sc;
364 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
365 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
374 slap_find_control_id(
378 struct slap_control *ctrl = find_ctrl( oid );
380 if ( cid ) *cid = ctrl->sc_cid;
383 return LDAP_CONTROL_NOT_FOUND;
386 void slap_free_ctrls(
388 LDAPControl **ctrls )
392 for (i=0; ctrls[i]; i++) {
393 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
395 op->o_tmpfree( ctrls, op->o_tmpmemctx );
407 BerElement *ber = op->o_ber;
408 struct slap_control *sc;
411 len = ber_pvt_ber_remaining(ber);
415 rs->sr_err = LDAP_SUCCESS;
419 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
420 if( tag == LBER_ERROR ) {
421 rs->sr_err = SLAPD_DISCONNECT;
422 rs->sr_text = "unexpected data in PDU";
428 Debug( LDAP_DEBUG_TRACE,
429 "=> get_ctrls\n", 0, 0, 0 );
431 if( op->o_protocol < LDAP_VERSION3 ) {
432 rs->sr_err = SLAPD_DISCONNECT;
433 rs->sr_text = "controls require LDAPv3";
437 /* one for first control, one for termination */
438 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
441 if( op->ctrls == NULL ) {
442 rs->sr_err = LDAP_NO_MEMORY;
443 rs->sr_text = "no memory";
448 op->o_ctrls[nctrls] = NULL;
450 /* step through each element */
451 for( tag = ber_first_element( ber, &len, &opaque );
453 tag = ber_next_element( ber, &len, opaque ) )
456 LDAPControl **tctrls;
458 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
459 memset(c, 0, sizeof(LDAPControl));
461 /* allocate pointer space for current controls (nctrls)
462 * + this control + extra NULL
464 tctrls = op->o_tmprealloc( op->o_ctrls,
465 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
468 if( tctrls == NULL ) {
470 ldap_controls_free(op->o_ctrls);
473 rs->sr_err = LDAP_NO_MEMORY;
474 rs->sr_text = "no memory";
478 op->o_ctrls = tctrls;
480 op->o_ctrls[nctrls++] = c;
481 op->o_ctrls[nctrls] = NULL;
483 tag = ber_scanf( ber, "{m" /*}*/, &bv );
484 c->ldctl_oid = bv.bv_val;
486 if( tag == LBER_ERROR ) {
487 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
490 slap_free_ctrls( op, op->o_ctrls );
492 rs->sr_err = SLAPD_DISCONNECT;
493 rs->sr_text = "decoding controls error";
496 } else if( c->ldctl_oid == NULL ) {
497 Debug( LDAP_DEBUG_TRACE,
498 "get_ctrls: conn %lu got emtpy OID.\n",
499 op->o_connid, 0, 0 );
501 slap_free_ctrls( op, op->o_ctrls );
503 rs->sr_err = LDAP_PROTOCOL_ERROR;
504 rs->sr_text = "OID field is empty";
508 tag = ber_peek_tag( ber, &len );
510 if( tag == LBER_BOOLEAN ) {
512 tag = ber_scanf( ber, "b", &crit );
514 if( tag == LBER_ERROR ) {
515 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
517 slap_free_ctrls( op, op->o_ctrls );
519 rs->sr_err = SLAPD_DISCONNECT;
520 rs->sr_text = "decoding controls error";
524 c->ldctl_iscritical = (crit != 0);
525 tag = ber_peek_tag( ber, &len );
528 if( tag == LBER_OCTETSTRING ) {
529 tag = ber_scanf( ber, "m", &c->ldctl_value );
531 if( tag == LBER_ERROR ) {
532 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
533 "%s (%scritical): get value failed.\n",
534 op->o_connid, c->ldctl_oid,
535 c->ldctl_iscritical ? "" : "non" );
536 slap_free_ctrls( op, op->o_ctrls );
538 rs->sr_err = SLAPD_DISCONNECT;
539 rs->sr_text = "decoding controls error";
544 Debug( LDAP_DEBUG_TRACE,
545 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
546 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
548 sc = find_ctrl( c->ldctl_oid );
550 /* recognized control */
552 switch( op->o_tag ) {
554 tagmask = SLAP_CTRL_ADD;
557 tagmask = SLAP_CTRL_BIND;
559 case LDAP_REQ_COMPARE:
560 tagmask = SLAP_CTRL_COMPARE;
562 case LDAP_REQ_DELETE:
563 tagmask = SLAP_CTRL_DELETE;
565 case LDAP_REQ_MODIFY:
566 tagmask = SLAP_CTRL_MODIFY;
568 case LDAP_REQ_RENAME:
569 tagmask = SLAP_CTRL_RENAME;
571 case LDAP_REQ_SEARCH:
572 tagmask = SLAP_CTRL_SEARCH;
574 case LDAP_REQ_UNBIND:
575 tagmask = SLAP_CTRL_UNBIND;
577 case LDAP_REQ_ABANDON:
578 tagmask = SLAP_CTRL_ABANDON;
580 case LDAP_REQ_EXTENDED:
582 assert( op->ore_reqoid.bv_val != NULL );
583 if( sc->sc_extendedops != NULL ) {
585 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
586 if( strcmp( op->ore_reqoid.bv_val,
587 sc->sc_extendedops[i] ) == 0 )
596 rs->sr_err = LDAP_OTHER;
597 rs->sr_text = "controls internal error";
601 if (( sc->sc_mask & tagmask ) == tagmask ) {
602 /* available extension */
605 if( !sc->sc_parse ) {
606 rs->sr_err = LDAP_OTHER;
607 rs->sr_text = "not yet implemented";
611 rc = sc->sc_parse( op, rs, c );
613 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
618 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
619 /* kludge to disable backend_control() check */
620 c->ldctl_iscritical = 0;
622 } else if ( tagmask == SLAP_CTRL_SEARCH &&
623 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
625 /* kludge to disable backend_control() check */
626 c->ldctl_iscritical = 0;
629 } else if( c->ldctl_iscritical ) {
630 /* unavailable CRITICAL control */
631 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
632 rs->sr_text = "critical extension is unavailable";
636 } else if( c->ldctl_iscritical ) {
637 /* unrecognized CRITICAL control */
638 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
639 rs->sr_text = "critical extension is not recognized";
646 Debug( LDAP_DEBUG_TRACE,
647 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
648 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
650 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
651 if( rs->sr_err == SLAPD_DISCONNECT ) {
652 rs->sr_err = LDAP_PROTOCOL_ERROR;
653 send_ldap_disconnect( op, rs );
654 rs->sr_err = SLAPD_DISCONNECT;
656 send_ldap_result( op, rs );
663 static int parseModifyIncrement (
669 if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
670 rs->sr_text = "modifyIncrement control specified multiple times";
671 return LDAP_PROTOCOL_ERROR;
675 if ( ctrl->ldctl_value.bv_len ) {
676 rs->sr_text = "modifyIncrement control value not empty";
677 return LDAP_PROTOCOL_ERROR;
681 op->o_modifyIncrement = ctrl->ldctl_iscritical
682 ? SLAP_CONTROL_CRITICAL
683 : SLAP_CONTROL_NONCRITICAL;
689 static int parseManageDSAit (
694 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
695 rs->sr_text = "manageDSAit control specified multiple times";
696 return LDAP_PROTOCOL_ERROR;
699 if ( ctrl->ldctl_value.bv_len ) {
700 rs->sr_text = "manageDSAit control value not empty";
701 return LDAP_PROTOCOL_ERROR;
704 op->o_managedsait = ctrl->ldctl_iscritical
705 ? SLAP_CONTROL_CRITICAL
706 : SLAP_CONTROL_NONCRITICAL;
711 static int parseProxyAuthz (
717 struct berval dn = BER_BVNULL;
719 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
720 rs->sr_text = "proxy authorization control specified multiple times";
721 return LDAP_PROTOCOL_ERROR;
724 op->o_proxy_authz = ctrl->ldctl_iscritical
725 ? SLAP_CONTROL_CRITICAL
726 : SLAP_CONTROL_NONCRITICAL;
728 Debug( LDAP_DEBUG_ARGS,
729 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
731 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
734 if( ctrl->ldctl_value.bv_len == 0 ) {
735 Debug( LDAP_DEBUG_TRACE,
736 "parseProxyAuthz: conn=%lu anonymous\n",
737 op->o_connid, 0, 0 );
740 free( op->o_dn.bv_val );
742 op->o_dn.bv_val = ch_strdup( "" );
744 free( op->o_ndn.bv_val );
745 op->o_ndn.bv_len = 0;
746 op->o_ndn.bv_val = ch_strdup( "" );
751 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
752 NULL, &dn, SLAP_GETDN_AUTHZID );
754 /* FIXME: empty DN in proxyAuthz control should be legal... */
755 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
757 ch_free( dn.bv_val );
759 rs->sr_text = "authzId mapping failed";
760 return LDAP_PROXY_AUTHZ_FAILURE;
763 Debug( LDAP_DEBUG_TRACE,
764 "parseProxyAuthz: conn=%lu \"%s\"\n",
766 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
768 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
771 ch_free( dn.bv_val );
772 rs->sr_text = "not authorized to assume identity";
773 return LDAP_PROXY_AUTHZ_FAILURE;
776 ch_free( op->o_dn.bv_val );
777 ch_free( op->o_ndn.bv_val );
779 op->o_dn.bv_val = NULL;
782 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
783 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
786 * NOTE: since slap_sasl_getdn() returns a normalized dn,
787 * from now on op->o_dn is normalized
789 ber_dupbv( &op->o_dn, &dn );
794 static int parseNoOp (
799 if ( op->o_noop != SLAP_CONTROL_NONE ) {
800 rs->sr_text = "noop control specified multiple times";
801 return LDAP_PROTOCOL_ERROR;
804 if ( ctrl->ldctl_value.bv_len ) {
805 rs->sr_text = "noop control value not empty";
806 return LDAP_PROTOCOL_ERROR;
809 op->o_noop = ctrl->ldctl_iscritical
810 ? SLAP_CONTROL_CRITICAL
811 : SLAP_CONTROL_NONCRITICAL;
816 static int parsePagedResults (
821 int rc = LDAP_SUCCESS;
825 struct berval cookie = BER_BVNULL;
826 PagedResultsState *ps;
828 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
829 rs->sr_text = "paged results control specified multiple times";
830 return LDAP_PROTOCOL_ERROR;
833 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
834 rs->sr_text = "paged results control value is empty (or absent)";
835 return LDAP_PROTOCOL_ERROR;
838 /* Parse the control value
839 * realSearchControlValue ::= SEQUENCE {
840 * size INTEGER (0..maxInt),
841 * -- requested page size from client
842 * -- result set size estimate from server
843 * cookie OCTET STRING
846 ber = ber_init( &ctrl->ldctl_value );
848 rs->sr_text = "internal error";
852 tag = ber_scanf( ber, "{im}", &size, &cookie );
854 if ( tag == LBER_ERROR ) {
855 rs->sr_text = "paged results control could not be decoded";
856 rc = LDAP_PROTOCOL_ERROR;
861 rs->sr_text = "paged results control size invalid";
862 rc = LDAP_PROTOCOL_ERROR;
867 /* defer cookie decoding/checks to backend... */
868 if ( cookie.bv_len ) {
869 PagedResultsCookie reqcookie;
870 if( cookie.bv_len != sizeof( reqcookie ) ) {
872 rs->sr_text = "paged results cookie is invalid";
873 rc = LDAP_PROTOCOL_ERROR;
877 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
879 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
881 rs->sr_text = "paged results cookie is invalid";
882 rc = LDAP_PROTOCOL_ERROR;
885 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
886 rs->sr_text = "paged results cookie is invalid or old";
887 rc = LDAP_UNWILLING_TO_PERFORM;
892 /* Initial request. Initialize state. */
894 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
895 /* There's another pagedResults control on the
896 * same connection; reject new pagedResults controls
897 * (allowed by RFC2696) */
898 rs->sr_text = "paged results cookie unavailable; try later";
899 rc = LDAP_UNWILLING_TO_PERFORM;
903 op->o_pagedresults_state.ps_cookie = 0;
904 op->o_pagedresults_state.ps_count = 0;
908 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
909 *ps = op->o_conn->c_pagedresults_state;
911 op->o_pagedresults_state = ps;
913 /* NOTE: according to RFC 2696 3.:
915 If the page size is greater than or equal to the sizeLimit value, the
916 server should ignore the control as the request can be satisfied in a
919 * NOTE: this assumes that the op->ors_slimit be set
920 * before the controls are parsed.
923 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
924 op->o_pagedresults = SLAP_CONTROL_IGNORED;
926 } else if ( ctrl->ldctl_iscritical ) {
927 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
930 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
934 (void)ber_free( ber, 1 );
938 static int parseAssert (
944 struct berval fstr = BER_BVNULL;
945 const char *err_msg = "";
947 if ( op->o_assert != SLAP_CONTROL_NONE ) {
948 rs->sr_text = "assert control specified multiple times";
949 return LDAP_PROTOCOL_ERROR;
952 if ( ctrl->ldctl_value.bv_len == 0 ) {
953 rs->sr_text = "assert control value is empty (or absent)";
954 return LDAP_PROTOCOL_ERROR;
957 ber = ber_init( &(ctrl->ldctl_value) );
959 rs->sr_text = "assert control: internal error";
963 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
965 if( rs->sr_err != LDAP_SUCCESS ) {
966 if( rs->sr_err == SLAPD_DISCONNECT ) {
967 rs->sr_err = LDAP_PROTOCOL_ERROR;
968 send_ldap_disconnect( op, rs );
969 rs->sr_err = SLAPD_DISCONNECT;
971 send_ldap_result( op, rs );
973 if( op->o_assertion != NULL ) {
974 filter_free_x( op, op->o_assertion );
980 filter2bv_x( op, op->o_assertion, &fstr );
982 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
983 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
984 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
987 op->o_assert = ctrl->ldctl_iscritical
988 ? SLAP_CONTROL_CRITICAL
989 : SLAP_CONTROL_NONCRITICAL;
991 rs->sr_err = LDAP_SUCCESS;
995 static int parsePreRead (
1000 ber_len_t siz, off, i;
1001 AttributeName *an = NULL;
1004 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1005 rs->sr_text = "preread control specified multiple times";
1006 return LDAP_PROTOCOL_ERROR;
1009 if ( ctrl->ldctl_value.bv_len == 0 ) {
1010 rs->sr_text = "preread control value is empty (or absent)";
1011 return LDAP_PROTOCOL_ERROR;
1014 ber = ber_init( &(ctrl->ldctl_value) );
1016 rs->sr_text = "preread control: internal error";
1020 siz = sizeof( AttributeName );
1021 off = offsetof( AttributeName, an_name );
1022 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1023 rs->sr_text = "preread control: decoding error";
1024 return LDAP_PROTOCOL_ERROR;
1027 for( i=0; i<siz; i++ ) {
1028 int rc = LDAP_SUCCESS;
1029 const char *dummy = NULL;
1031 an[i].an_desc = NULL;
1033 an[i].an_oc_exclude = 0;
1034 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1035 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1038 : "postread control: unknown attributeType";
1043 op->o_preread = ctrl->ldctl_iscritical
1044 ? SLAP_CONTROL_CRITICAL
1045 : SLAP_CONTROL_NONCRITICAL;
1047 op->o_preread_attrs = an;
1049 rs->sr_err = LDAP_SUCCESS;
1050 return LDAP_SUCCESS;
1053 static int parsePostRead (
1058 ber_len_t siz, off, i;
1059 AttributeName *an = NULL;
1062 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1063 rs->sr_text = "postread control specified multiple times";
1064 return LDAP_PROTOCOL_ERROR;
1067 if ( ctrl->ldctl_value.bv_len == 0 ) {
1068 rs->sr_text = "postread control value is empty (or absent)";
1069 return LDAP_PROTOCOL_ERROR;
1072 ber = ber_init( &(ctrl->ldctl_value) );
1074 rs->sr_text = "postread control: internal error";
1078 siz = sizeof( AttributeName );
1079 off = offsetof( AttributeName, an_name );
1080 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1081 rs->sr_text = "postread control: decoding error";
1082 return LDAP_PROTOCOL_ERROR;
1085 for( i=0; i<siz; i++ ) {
1086 int rc = LDAP_SUCCESS;
1087 const char *dummy = NULL;
1089 an[i].an_desc = NULL;
1091 an[i].an_oc_exclude = 0;
1092 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1093 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1096 : "postread control: unknown attributeType";
1101 op->o_postread = ctrl->ldctl_iscritical
1102 ? SLAP_CONTROL_CRITICAL
1103 : SLAP_CONTROL_NONCRITICAL;
1105 op->o_postread_attrs = an;
1107 rs->sr_err = LDAP_SUCCESS;
1108 return LDAP_SUCCESS;
1111 int parseValuesReturnFilter (
1117 struct berval fstr = BER_BVNULL;
1118 const char *err_msg = "";
1120 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1121 rs->sr_text = "valuesReturnFilter control specified multiple times";
1122 return LDAP_PROTOCOL_ERROR;
1125 if ( ctrl->ldctl_value.bv_len == 0 ) {
1126 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1127 return LDAP_PROTOCOL_ERROR;
1130 ber = ber_init( &(ctrl->ldctl_value) );
1132 rs->sr_text = "internal error";
1136 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1138 if( rs->sr_err != LDAP_SUCCESS ) {
1139 if( rs->sr_err == SLAPD_DISCONNECT ) {
1140 rs->sr_err = LDAP_PROTOCOL_ERROR;
1141 send_ldap_disconnect( op, rs );
1142 rs->sr_err = SLAPD_DISCONNECT;
1144 send_ldap_result( op, rs );
1146 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1150 vrFilter2bv( op, op->o_vrFilter, &fstr );
1153 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1154 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1155 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1158 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1159 ? SLAP_CONTROL_CRITICAL
1160 : SLAP_CONTROL_NONCRITICAL;
1162 rs->sr_err = LDAP_SUCCESS;
1163 return LDAP_SUCCESS;
1166 #ifdef LDAP_CONTROL_SUBENTRIES
1167 static int parseSubentries (
1172 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1173 rs->sr_text = "subentries control specified multiple times";
1174 return LDAP_PROTOCOL_ERROR;
1177 /* FIXME: should use BER library */
1178 if( ( ctrl->ldctl_value.bv_len != 3 )
1179 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1180 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1182 rs->sr_text = "subentries control value encoding is bogus";
1183 return LDAP_PROTOCOL_ERROR;
1186 op->o_subentries = ctrl->ldctl_iscritical
1187 ? SLAP_CONTROL_CRITICAL
1188 : SLAP_CONTROL_NONCRITICAL;
1190 if ( (void *)(ctrl->ldctl_value.bv_val[2] != 0x00))
1191 set_subentries_visibility( op );
1193 return LDAP_SUCCESS;
1197 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1198 static int parsePermissiveModify (
1203 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1204 rs->sr_text = "permissiveModify control specified multiple times";
1205 return LDAP_PROTOCOL_ERROR;
1208 if ( ctrl->ldctl_value.bv_len ) {
1209 rs->sr_text = "permissiveModify control value not empty";
1210 return LDAP_PROTOCOL_ERROR;
1213 op->o_permissive_modify = ctrl->ldctl_iscritical
1214 ? SLAP_CONTROL_CRITICAL
1215 : SLAP_CONTROL_NONCRITICAL;
1217 return LDAP_SUCCESS;
1221 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1222 static int parseDomainScope (
1227 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1228 rs->sr_text = "domainScope control specified multiple times";
1229 return LDAP_PROTOCOL_ERROR;
1232 if ( ctrl->ldctl_value.bv_len ) {
1233 rs->sr_text = "domainScope control value not empty";
1234 return LDAP_PROTOCOL_ERROR;
1237 op->o_domain_scope = ctrl->ldctl_iscritical
1238 ? SLAP_CONTROL_CRITICAL
1239 : SLAP_CONTROL_NONCRITICAL;
1241 return LDAP_SUCCESS;
1245 #ifdef LDAP_CONTROL_X_TREE_DELETE
1246 static int parseTreeDelete (
1251 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1252 rs->sr_text = "treeDelete control specified multiple times";
1253 return LDAP_PROTOCOL_ERROR;
1256 if ( ctrl->ldctl_value.bv_len ) {
1257 rs->sr_text = "treeDelete control value not empty";
1258 return LDAP_PROTOCOL_ERROR;
1261 op->o_tree_delete = ctrl->ldctl_iscritical
1262 ? SLAP_CONTROL_CRITICAL
1263 : SLAP_CONTROL_NONCRITICAL;
1265 return LDAP_SUCCESS;
1269 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1270 static int parseSearchOptions (
1276 ber_int_t search_flags;
1278 if ( ctrl->ldctl_value.bv_len == 0 ) {
1279 rs->sr_text = "searchOptions control value not empty";
1280 return LDAP_PROTOCOL_ERROR;
1283 ber = ber_init( &ctrl->ldctl_value );
1285 rs->sr_text = "internal error";
1289 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1290 rs->sr_text = "searchOptions control decoding error";
1291 return LDAP_PROTOCOL_ERROR;
1294 (void) ber_free( ber, 1 );
1296 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1297 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1298 rs->sr_text = "searchOptions control specified multiple times "
1299 "or with domainScope control";
1300 return LDAP_PROTOCOL_ERROR;
1303 op->o_domain_scope = ctrl->ldctl_iscritical
1304 ? SLAP_CONTROL_CRITICAL
1305 : SLAP_CONTROL_NONCRITICAL;
1308 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1309 /* Other search flags not recognised so far */
1310 rs->sr_text = "searchOptions contained unrecongized flag";
1311 return LDAP_UNWILLING_TO_PERFORM;
1314 return LDAP_SUCCESS;