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;
40 static SLAP_CTRL_PARSE_FN parseSubentries;
42 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
44 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
45 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
47 struct slap_control_ids slap_cids;
53 /* The controlID for this control */
56 /* Operations supported by control */
59 /* Extended operations supported by control */
60 char **sc_extendedops;
62 /* Control parsing callback */
63 SLAP_CTRL_PARSE_FN *sc_parse;
65 LDAP_SLIST_ENTRY(slap_control) sc_next;
68 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
69 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
72 * all known request control OIDs should be added to this list
75 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
76 * addresses an undefined control; this allows to safely test for
77 * well known controls even if they are not registered, e.g. if
78 * they get moved to modules. An example is sc_LDAPsync, which
79 * is implemented in the syncprov overlay and thus, if configured
80 * as dynamic module, may not be registered. One side effect is that
81 * slap_known_controls[0] == NULL, so it should always be used
83 * FIXME: should we define the "undefined control" oid?
85 char *slap_known_controls[SLAP_MAX_CIDS+1];
86 static int num_known_controls = 1;
88 static char *proxy_authz_extops[] = {
89 LDAP_EXOP_MODIFY_PASSWD,
94 static struct slap_control control_defs[] = {
95 { LDAP_CONTROL_ASSERT,
96 (int)offsetof(struct slap_control_ids, sc_assert),
97 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
98 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
99 { LDAP_CONTROL_PRE_READ,
100 (int)offsetof(struct slap_control_ids, sc_preRead),
101 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
102 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
103 { LDAP_CONTROL_POST_READ,
104 (int)offsetof(struct slap_control_ids, sc_postRead),
105 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
106 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
107 { LDAP_CONTROL_VALUESRETURNFILTER,
108 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
109 SLAP_CTRL_SEARCH, NULL,
110 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
111 { LDAP_CONTROL_PAGEDRESULTS,
112 (int)offsetof(struct slap_control_ids, sc_pagedResults),
113 SLAP_CTRL_SEARCH, NULL,
114 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
115 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
116 { LDAP_CONTROL_X_DOMAIN_SCOPE,
117 (int)offsetof(struct slap_control_ids, sc_domainScope),
118 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
119 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
121 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
122 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
123 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
124 SLAP_CTRL_MODIFY, NULL,
125 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
127 #ifdef SLAP_CONTROL_X_TREE_DELETE
128 { LDAP_CONTROL_X_TREE_DELETE,
129 (int)offsetof(struct slap_control_ids, sc_treeDelete),
130 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
131 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
133 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
134 { LDAP_CONTORL_X_SEARCH_OPTIONS,
135 (int)offsetof(struct slap_control_ids, sc_searchOptions),
136 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
137 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 #ifdef LDAP_CONTROL_SUBENTRIES
140 { LDAP_CONTROL_SUBENTRIES,
141 (int)offsetof(struct slap_control_ids, sc_subentries),
142 SLAP_CTRL_SEARCH, NULL,
143 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
146 (int)offsetof(struct slap_control_ids, sc_noOp),
147 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
148 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
150 { LDAP_CONTROL_MODIFY_INCREMENT,
151 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
152 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
153 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
155 { LDAP_CONTROL_MANAGEDSAIT,
156 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
157 SLAP_CTRL_ACCESS, NULL,
158 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
159 { LDAP_CONTROL_PROXY_AUTHZ,
160 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
161 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
162 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
163 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
167 * Register a supported control.
169 * This can be called by an OpenLDAP plugin or, indirectly, by a
170 * SLAPI plugin calling slapi_register_supported_control().
173 register_supported_control(const char *controloid,
174 slap_mask_t controlmask,
176 SLAP_CTRL_PARSE_FN *controlparsefn,
179 struct slap_control *sc;
182 if ( num_known_controls >= SLAP_MAX_CIDS ) {
183 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
184 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
185 SLAP_MAX_CIDS, 0, 0 );
189 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
191 /* sanity check - should never happen */
192 for ( i = 0; slap_known_controls[ i ]; i++ ) {
193 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
194 Debug( LDAP_DEBUG_ANY,
195 "Control %s already registered.\n",
197 return LDAP_PARAM_ERROR;
201 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
202 if ( sc == NULL ) return LDAP_NO_MEMORY;
204 sc->sc_oid = ch_strdup( controloid );
205 sc->sc_mask = controlmask;
206 if ( controlexops != NULL ) {
207 sc->sc_extendedops = ldap_charray_dup( controlexops );
208 if ( sc->sc_extendedops == NULL ) {
210 return LDAP_NO_MEMORY;
213 sc->sc_extendedops = NULL;
215 sc->sc_parse = controlparsefn;
217 if ( controlcid ) *controlcid = num_known_controls;
218 sc->sc_cid = num_known_controls;
220 /* Update slap_known_controls, too. */
221 slap_known_controls[num_known_controls-1] = sc->sc_oid;
222 slap_known_controls[num_known_controls++] = NULL;
224 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
225 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
230 * One-time initialization of internal controls.
233 slap_controls_init( void )
236 struct slap_control *sc;
240 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
241 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
242 rc = register_supported_control( control_defs[i].sc_oid,
243 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
244 control_defs[i].sc_parse, cid );
245 if ( rc != LDAP_SUCCESS ) break;
252 * Free memory associated with list of supported controls.
255 controls_destroy( void )
257 struct slap_control *sc;
259 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
260 sc = LDAP_SLIST_FIRST(&controls_list);
261 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
263 ch_free( sc->sc_oid );
264 if ( sc->sc_extendedops != NULL ) {
265 ldap_charray_free( sc->sc_extendedops );
272 * Format the supportedControl attribute of the root DSE,
273 * detailing which controls are supported by the directory
277 controls_root_dse_info( Entry *e )
279 AttributeDescription *ad_supportedControl
280 = slap_schema.si_ad_supportedControl;
281 struct berval vals[2];
282 struct slap_control *sc;
284 vals[1].bv_val = NULL;
287 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
288 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
290 vals[0].bv_val = sc->sc_oid;
291 vals[0].bv_len = strlen( sc->sc_oid );
293 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
302 * Return a list of OIDs and operation masks for supported
303 * controls. Used by SLAPI.
306 get_supported_controls(char ***ctrloidsp,
307 slap_mask_t **ctrlmasks)
313 struct slap_control *sc;
317 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
327 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
328 if ( oids == NULL ) {
329 return LDAP_NO_MEMORY;
331 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
332 if ( masks == NULL ) {
334 return LDAP_NO_MEMORY;
339 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
340 oids[n] = ch_strdup( sc->sc_oid );
341 masks[n] = sc->sc_mask;
354 * Find a control given its OID.
356 static struct slap_control *
357 find_ctrl( const char *oid )
359 struct slap_control *sc;
361 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
362 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
371 slap_find_control_id(
375 struct slap_control *ctrl = find_ctrl( oid );
377 if ( cid ) *cid = ctrl->sc_cid;
380 return LDAP_CONTROL_NOT_FOUND;
383 void slap_free_ctrls(
385 LDAPControl **ctrls )
389 for (i=0; ctrls[i]; i++) {
390 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
392 op->o_tmpfree( ctrls, op->o_tmpmemctx );
404 BerElement *ber = op->o_ber;
405 struct slap_control *sc;
408 len = ber_pvt_ber_remaining(ber);
412 rs->sr_err = LDAP_SUCCESS;
416 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
417 if( tag == LBER_ERROR ) {
418 rs->sr_err = SLAPD_DISCONNECT;
419 rs->sr_text = "unexpected data in PDU";
425 Debug( LDAP_DEBUG_TRACE,
426 "=> get_ctrls\n", 0, 0, 0 );
428 if( op->o_protocol < LDAP_VERSION3 ) {
429 rs->sr_err = SLAPD_DISCONNECT;
430 rs->sr_text = "controls require LDAPv3";
434 /* one for first control, one for termination */
435 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
438 if( op->ctrls == NULL ) {
439 rs->sr_err = LDAP_NO_MEMORY;
440 rs->sr_text = "no memory";
445 op->o_ctrls[nctrls] = NULL;
447 /* step through each element */
448 for( tag = ber_first_element( ber, &len, &opaque );
450 tag = ber_next_element( ber, &len, opaque ) )
453 LDAPControl **tctrls;
455 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
456 memset(c, 0, sizeof(LDAPControl));
458 /* allocate pointer space for current controls (nctrls)
459 * + this control + extra NULL
461 tctrls = op->o_tmprealloc( op->o_ctrls,
462 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
465 if( tctrls == NULL ) {
467 ldap_controls_free(op->o_ctrls);
470 rs->sr_err = LDAP_NO_MEMORY;
471 rs->sr_text = "no memory";
475 op->o_ctrls = tctrls;
477 op->o_ctrls[nctrls++] = c;
478 op->o_ctrls[nctrls] = NULL;
480 tag = ber_scanf( ber, "{m" /*}*/, &bv );
481 c->ldctl_oid = bv.bv_val;
483 if( tag == LBER_ERROR ) {
484 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
487 slap_free_ctrls( op, op->o_ctrls );
489 rs->sr_err = SLAPD_DISCONNECT;
490 rs->sr_text = "decoding controls error";
493 } else if( c->ldctl_oid == NULL ) {
494 Debug( LDAP_DEBUG_TRACE,
495 "get_ctrls: conn %lu got emtpy OID.\n",
496 op->o_connid, 0, 0 );
498 slap_free_ctrls( op, op->o_ctrls );
500 rs->sr_err = LDAP_PROTOCOL_ERROR;
501 rs->sr_text = "OID field is empty";
505 tag = ber_peek_tag( ber, &len );
507 if( tag == LBER_BOOLEAN ) {
509 tag = ber_scanf( ber, "b", &crit );
511 if( tag == LBER_ERROR ) {
512 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
514 slap_free_ctrls( op, op->o_ctrls );
516 rs->sr_err = SLAPD_DISCONNECT;
517 rs->sr_text = "decoding controls error";
521 c->ldctl_iscritical = (crit != 0);
522 tag = ber_peek_tag( ber, &len );
525 if( tag == LBER_OCTETSTRING ) {
526 tag = ber_scanf( ber, "m", &c->ldctl_value );
528 if( tag == LBER_ERROR ) {
529 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
530 "%s (%scritical): get value failed.\n",
531 op->o_connid, c->ldctl_oid,
532 c->ldctl_iscritical ? "" : "non" );
533 slap_free_ctrls( op, op->o_ctrls );
535 rs->sr_err = SLAPD_DISCONNECT;
536 rs->sr_text = "decoding controls error";
541 Debug( LDAP_DEBUG_TRACE,
542 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
543 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
545 sc = find_ctrl( c->ldctl_oid );
547 /* recognized control */
549 switch( op->o_tag ) {
551 tagmask = SLAP_CTRL_ADD;
554 tagmask = SLAP_CTRL_BIND;
556 case LDAP_REQ_COMPARE:
557 tagmask = SLAP_CTRL_COMPARE;
559 case LDAP_REQ_DELETE:
560 tagmask = SLAP_CTRL_DELETE;
562 case LDAP_REQ_MODIFY:
563 tagmask = SLAP_CTRL_MODIFY;
565 case LDAP_REQ_RENAME:
566 tagmask = SLAP_CTRL_RENAME;
568 case LDAP_REQ_SEARCH:
569 tagmask = SLAP_CTRL_SEARCH;
571 case LDAP_REQ_UNBIND:
572 tagmask = SLAP_CTRL_UNBIND;
574 case LDAP_REQ_ABANDON:
575 tagmask = SLAP_CTRL_ABANDON;
577 case LDAP_REQ_EXTENDED:
579 assert( op->ore_reqoid.bv_val != NULL );
580 if( sc->sc_extendedops != NULL ) {
582 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
583 if( strcmp( op->ore_reqoid.bv_val,
584 sc->sc_extendedops[i] ) == 0 )
593 rs->sr_err = LDAP_OTHER;
594 rs->sr_text = "controls internal error";
598 if (( sc->sc_mask & tagmask ) == tagmask ) {
599 /* available extension */
602 if( !sc->sc_parse ) {
603 rs->sr_err = LDAP_OTHER;
604 rs->sr_text = "not yet implemented";
608 rc = sc->sc_parse( op, rs, c );
610 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
615 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
616 /* kludge to disable backend_control() check */
617 c->ldctl_iscritical = 0;
619 } else if ( tagmask == SLAP_CTRL_SEARCH &&
620 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
622 /* kludge to disable backend_control() check */
623 c->ldctl_iscritical = 0;
626 } else if( c->ldctl_iscritical ) {
627 /* unavailable CRITICAL control */
628 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
629 rs->sr_text = "critical extension is unavailable";
633 } else if( c->ldctl_iscritical ) {
634 /* unrecognized CRITICAL control */
635 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
636 rs->sr_text = "critical extension is not recognized";
643 Debug( LDAP_DEBUG_TRACE,
644 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
645 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
647 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
648 if( rs->sr_err == SLAPD_DISCONNECT ) {
649 rs->sr_err = LDAP_PROTOCOL_ERROR;
650 send_ldap_disconnect( op, rs );
651 rs->sr_err = SLAPD_DISCONNECT;
653 send_ldap_result( op, rs );
660 static int parseModifyIncrement (
666 if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
667 rs->sr_text = "modifyIncrement control specified multiple times";
668 return LDAP_PROTOCOL_ERROR;
672 if ( ctrl->ldctl_value.bv_len ) {
673 rs->sr_text = "modifyIncrement control value not empty";
674 return LDAP_PROTOCOL_ERROR;
678 op->o_modifyIncrement = ctrl->ldctl_iscritical
679 ? SLAP_CONTROL_CRITICAL
680 : SLAP_CONTROL_NONCRITICAL;
686 static int parseManageDSAit (
691 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
692 rs->sr_text = "manageDSAit control specified multiple times";
693 return LDAP_PROTOCOL_ERROR;
696 if ( ctrl->ldctl_value.bv_len ) {
697 rs->sr_text = "manageDSAit control value not empty";
698 return LDAP_PROTOCOL_ERROR;
701 op->o_managedsait = ctrl->ldctl_iscritical
702 ? SLAP_CONTROL_CRITICAL
703 : SLAP_CONTROL_NONCRITICAL;
708 static int parseProxyAuthz (
714 struct berval dn = BER_BVNULL;
716 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
717 rs->sr_text = "proxy authorization control specified multiple times";
718 return LDAP_PROTOCOL_ERROR;
721 op->o_proxy_authz = ctrl->ldctl_iscritical
722 ? SLAP_CONTROL_CRITICAL
723 : SLAP_CONTROL_NONCRITICAL;
725 Debug( LDAP_DEBUG_ARGS,
726 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
728 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
731 if( ctrl->ldctl_value.bv_len == 0 ) {
732 Debug( LDAP_DEBUG_TRACE,
733 "parseProxyAuthz: conn=%lu anonymous\n",
734 op->o_connid, 0, 0 );
737 free( op->o_dn.bv_val );
739 op->o_dn.bv_val = ch_strdup( "" );
741 free( op->o_ndn.bv_val );
742 op->o_ndn.bv_len = 0;
743 op->o_ndn.bv_val = ch_strdup( "" );
748 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
749 NULL, &dn, SLAP_GETDN_AUTHZID );
751 /* FIXME: empty DN in proxyAuthz control should be legal... */
752 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
754 ch_free( dn.bv_val );
756 rs->sr_text = "authzId mapping failed";
757 return LDAP_PROXY_AUTHZ_FAILURE;
760 Debug( LDAP_DEBUG_TRACE,
761 "parseProxyAuthz: conn=%lu \"%s\"\n",
763 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
765 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
768 ch_free( dn.bv_val );
769 rs->sr_text = "not authorized to assume identity";
770 return LDAP_PROXY_AUTHZ_FAILURE;
773 ch_free( op->o_dn.bv_val );
774 ch_free( op->o_ndn.bv_val );
776 op->o_dn.bv_val = NULL;
779 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
780 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
783 * NOTE: since slap_sasl_getdn() returns a normalized dn,
784 * from now on op->o_dn is normalized
786 ber_dupbv( &op->o_dn, &dn );
791 static int parseNoOp (
796 if ( op->o_noop != SLAP_CONTROL_NONE ) {
797 rs->sr_text = "noop control specified multiple times";
798 return LDAP_PROTOCOL_ERROR;
801 if ( ctrl->ldctl_value.bv_len ) {
802 rs->sr_text = "noop control value not empty";
803 return LDAP_PROTOCOL_ERROR;
806 op->o_noop = ctrl->ldctl_iscritical
807 ? SLAP_CONTROL_CRITICAL
808 : SLAP_CONTROL_NONCRITICAL;
813 static int parsePagedResults (
818 int rc = LDAP_SUCCESS;
822 struct berval cookie = BER_BVNULL;
823 PagedResultsState *ps;
825 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
826 rs->sr_text = "paged results control specified multiple times";
827 return LDAP_PROTOCOL_ERROR;
830 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
831 rs->sr_text = "paged results control value is empty (or absent)";
832 return LDAP_PROTOCOL_ERROR;
835 /* Parse the control value
836 * realSearchControlValue ::= SEQUENCE {
837 * size INTEGER (0..maxInt),
838 * -- requested page size from client
839 * -- result set size estimate from server
840 * cookie OCTET STRING
843 ber = ber_init( &ctrl->ldctl_value );
845 rs->sr_text = "internal error";
849 tag = ber_scanf( ber, "{im}", &size, &cookie );
851 if ( tag == LBER_ERROR ) {
852 rs->sr_text = "paged results control could not be decoded";
853 rc = LDAP_PROTOCOL_ERROR;
858 rs->sr_text = "paged results control size invalid";
859 rc = LDAP_PROTOCOL_ERROR;
864 /* defer cookie decoding/checks to backend... */
865 if ( cookie.bv_len ) {
866 PagedResultsCookie reqcookie;
867 if( cookie.bv_len != sizeof( reqcookie ) ) {
869 rs->sr_text = "paged results cookie is invalid";
870 rc = LDAP_PROTOCOL_ERROR;
874 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
876 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
878 rs->sr_text = "paged results cookie is invalid";
879 rc = LDAP_PROTOCOL_ERROR;
882 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
883 rs->sr_text = "paged results cookie is invalid or old";
884 rc = LDAP_UNWILLING_TO_PERFORM;
889 /* Initial request. Initialize state. */
891 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
892 /* There's another pagedResults control on the
893 * same connection; reject new pagedResults controls
894 * (allowed by RFC2696) */
895 rs->sr_text = "paged results cookie unavailable; try later";
896 rc = LDAP_UNWILLING_TO_PERFORM;
900 op->o_pagedresults_state.ps_cookie = 0;
901 op->o_pagedresults_state.ps_count = 0;
905 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
906 *ps = op->o_conn->c_pagedresults_state;
908 op->o_pagedresults_state = ps;
910 /* NOTE: according to RFC 2696 3.:
912 If the page size is greater than or equal to the sizeLimit value, the
913 server should ignore the control as the request can be satisfied in a
916 * NOTE: this assumes that the op->ors_slimit be set
917 * before the controls are parsed.
920 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
921 op->o_pagedresults = SLAP_CONTROL_IGNORED;
923 } else if ( ctrl->ldctl_iscritical ) {
924 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
927 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
931 (void)ber_free( ber, 1 );
935 static int parseAssert (
941 struct berval fstr = BER_BVNULL;
942 const char *err_msg = "";
944 if ( op->o_assert != SLAP_CONTROL_NONE ) {
945 rs->sr_text = "assert control specified multiple times";
946 return LDAP_PROTOCOL_ERROR;
949 if ( ctrl->ldctl_value.bv_len == 0 ) {
950 rs->sr_text = "assert control value is empty (or absent)";
951 return LDAP_PROTOCOL_ERROR;
954 ber = ber_init( &(ctrl->ldctl_value) );
956 rs->sr_text = "assert control: internal error";
960 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
962 if( rs->sr_err != LDAP_SUCCESS ) {
963 if( rs->sr_err == SLAPD_DISCONNECT ) {
964 rs->sr_err = LDAP_PROTOCOL_ERROR;
965 send_ldap_disconnect( op, rs );
966 rs->sr_err = SLAPD_DISCONNECT;
968 send_ldap_result( op, rs );
970 if( op->o_assertion != NULL ) {
971 filter_free_x( op, op->o_assertion );
977 filter2bv_x( op, op->o_assertion, &fstr );
979 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
980 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
981 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
984 op->o_assert = ctrl->ldctl_iscritical
985 ? SLAP_CONTROL_CRITICAL
986 : SLAP_CONTROL_NONCRITICAL;
988 rs->sr_err = LDAP_SUCCESS;
992 static int parsePreRead (
997 ber_len_t siz, off, i;
998 AttributeName *an = NULL;
1001 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1002 rs->sr_text = "preread control specified multiple times";
1003 return LDAP_PROTOCOL_ERROR;
1006 if ( ctrl->ldctl_value.bv_len == 0 ) {
1007 rs->sr_text = "preread control value is empty (or absent)";
1008 return LDAP_PROTOCOL_ERROR;
1011 ber = ber_init( &(ctrl->ldctl_value) );
1013 rs->sr_text = "preread control: internal error";
1017 siz = sizeof( AttributeName );
1018 off = offsetof( AttributeName, an_name );
1019 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1020 rs->sr_text = "preread control: decoding error";
1021 return LDAP_PROTOCOL_ERROR;
1024 for( i=0; i<siz; i++ ) {
1025 int rc = LDAP_SUCCESS;
1026 const char *dummy = NULL;
1028 an[i].an_desc = NULL;
1030 an[i].an_oc_exclude = 0;
1031 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1032 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1035 : "postread control: unknown attributeType";
1040 op->o_preread = ctrl->ldctl_iscritical
1041 ? SLAP_CONTROL_CRITICAL
1042 : SLAP_CONTROL_NONCRITICAL;
1044 op->o_preread_attrs = an;
1046 rs->sr_err = LDAP_SUCCESS;
1047 return LDAP_SUCCESS;
1050 static int parsePostRead (
1055 ber_len_t siz, off, i;
1056 AttributeName *an = NULL;
1059 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1060 rs->sr_text = "postread control specified multiple times";
1061 return LDAP_PROTOCOL_ERROR;
1064 if ( ctrl->ldctl_value.bv_len == 0 ) {
1065 rs->sr_text = "postread control value is empty (or absent)";
1066 return LDAP_PROTOCOL_ERROR;
1069 ber = ber_init( &(ctrl->ldctl_value) );
1071 rs->sr_text = "postread control: internal error";
1075 siz = sizeof( AttributeName );
1076 off = offsetof( AttributeName, an_name );
1077 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1078 rs->sr_text = "postread control: decoding error";
1079 return LDAP_PROTOCOL_ERROR;
1082 for( i=0; i<siz; i++ ) {
1083 int rc = LDAP_SUCCESS;
1084 const char *dummy = NULL;
1086 an[i].an_desc = NULL;
1088 an[i].an_oc_exclude = 0;
1089 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1090 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1093 : "postread control: unknown attributeType";
1098 op->o_postread = ctrl->ldctl_iscritical
1099 ? SLAP_CONTROL_CRITICAL
1100 : SLAP_CONTROL_NONCRITICAL;
1102 op->o_postread_attrs = an;
1104 rs->sr_err = LDAP_SUCCESS;
1105 return LDAP_SUCCESS;
1108 int parseValuesReturnFilter (
1114 struct berval fstr = BER_BVNULL;
1115 const char *err_msg = "";
1117 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1118 rs->sr_text = "valuesReturnFilter control specified multiple times";
1119 return LDAP_PROTOCOL_ERROR;
1122 if ( ctrl->ldctl_value.bv_len == 0 ) {
1123 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1124 return LDAP_PROTOCOL_ERROR;
1127 ber = ber_init( &(ctrl->ldctl_value) );
1129 rs->sr_text = "internal error";
1133 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1135 if( rs->sr_err != LDAP_SUCCESS ) {
1136 if( rs->sr_err == SLAPD_DISCONNECT ) {
1137 rs->sr_err = LDAP_PROTOCOL_ERROR;
1138 send_ldap_disconnect( op, rs );
1139 rs->sr_err = SLAPD_DISCONNECT;
1141 send_ldap_result( op, rs );
1143 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1147 vrFilter2bv( op, op->o_vrFilter, &fstr );
1150 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1151 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1152 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1155 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1156 ? SLAP_CONTROL_CRITICAL
1157 : SLAP_CONTROL_NONCRITICAL;
1159 rs->sr_err = LDAP_SUCCESS;
1160 return LDAP_SUCCESS;
1163 #ifdef LDAP_CONTROL_SUBENTRIES
1164 static int parseSubentries (
1169 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1170 rs->sr_text = "subentries control specified multiple times";
1171 return LDAP_PROTOCOL_ERROR;
1174 /* FIXME: should use BER library */
1175 if( ( ctrl->ldctl_value.bv_len != 3 )
1176 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1177 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1179 rs->sr_text = "subentries control value encoding is bogus";
1180 return LDAP_PROTOCOL_ERROR;
1183 op->o_subentries = ctrl->ldctl_iscritical
1184 ? SLAP_CONTROL_CRITICAL
1185 : SLAP_CONTROL_NONCRITICAL;
1187 if ( (void *)(ctrl->ldctl_value.bv_val[2] != 0x00))
1188 set_subentries_visibility( op );
1190 return LDAP_SUCCESS;
1194 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1195 static int parsePermissiveModify (
1200 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1201 rs->sr_text = "permissiveModify control specified multiple times";
1202 return LDAP_PROTOCOL_ERROR;
1205 if ( ctrl->ldctl_value.bv_len ) {
1206 rs->sr_text = "permissiveModify control value not empty";
1207 return LDAP_PROTOCOL_ERROR;
1210 op->o_permissive_modify = ctrl->ldctl_iscritical
1211 ? SLAP_CONTROL_CRITICAL
1212 : SLAP_CONTROL_NONCRITICAL;
1214 return LDAP_SUCCESS;
1218 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1219 static int parseDomainScope (
1224 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1225 rs->sr_text = "domainScope control specified multiple times";
1226 return LDAP_PROTOCOL_ERROR;
1229 if ( ctrl->ldctl_value.bv_len ) {
1230 rs->sr_text = "domainScope control value not empty";
1231 return LDAP_PROTOCOL_ERROR;
1234 op->o_domain_scope = ctrl->ldctl_iscritical
1235 ? SLAP_CONTROL_CRITICAL
1236 : SLAP_CONTROL_NONCRITICAL;
1238 return LDAP_SUCCESS;
1242 #ifdef LDAP_CONTROL_X_TREE_DELETE
1243 static int parseTreeDelete (
1248 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1249 rs->sr_text = "treeDelete control specified multiple times";
1250 return LDAP_PROTOCOL_ERROR;
1253 if ( ctrl->ldctl_value.bv_len ) {
1254 rs->sr_text = "treeDelete control value not empty";
1255 return LDAP_PROTOCOL_ERROR;
1258 op->o_tree_delete = ctrl->ldctl_iscritical
1259 ? SLAP_CONTROL_CRITICAL
1260 : SLAP_CONTROL_NONCRITICAL;
1262 return LDAP_SUCCESS;
1266 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1267 static int parseSearchOptions (
1273 ber_int_t search_flags;
1275 if ( ctrl->ldctl_value.bv_len == 0 ) {
1276 rs->sr_text = "searchOptions control value not empty";
1277 return LDAP_PROTOCOL_ERROR;
1280 ber = ber_init( &ctrl->ldctl_value );
1282 rs->sr_text = "internal error";
1286 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1287 rs->sr_text = "searchOptions control decoding error";
1288 return LDAP_PROTOCOL_ERROR;
1291 (void) ber_free( ber, 1 );
1293 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1294 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1295 rs->sr_text = "searchOptions control specified multiple times "
1296 "or with domainScope control";
1297 return LDAP_PROTOCOL_ERROR;
1300 op->o_domain_scope = ctrl->ldctl_iscritical
1301 ? SLAP_CONTROL_CRITICAL
1302 : SLAP_CONTROL_NONCRITICAL;
1305 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1306 /* Other search flags not recognised so far,
1308 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1310 rs->sr_text = "searchOptions contained unrecongized flag";
1311 return LDAP_UNWILLING_TO_PERFORM;
1314 return LDAP_SUCCESS;