2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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;
44 static SLAP_CTRL_PARSE_FN parseLDAPsync;
46 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
48 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
49 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
51 struct slap_control_ids slap_cids;
57 /* The controlID for this control */
60 /* Operations supported by control */
63 /* Extended operations supported by control */
64 char **sc_extendedops;
66 /* Control parsing callback */
67 SLAP_CTRL_PARSE_FN *sc_parse;
69 LDAP_SLIST_ENTRY(slap_control) sc_next;
72 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
73 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
76 * all known request control OIDs should be added to this list
78 char **slap_known_controls = NULL;
79 static int num_known_controls;
81 static char *proxy_authz_extops[] = {
82 LDAP_EXOP_MODIFY_PASSWD,
87 static struct slap_control control_defs[] = {
88 { LDAP_CONTROL_ASSERT,
89 (int)offsetof(struct slap_control_ids, sc_assert),
90 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
91 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
92 { LDAP_CONTROL_PRE_READ,
93 (int)offsetof(struct slap_control_ids, sc_preRead),
94 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
95 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
96 { LDAP_CONTROL_POST_READ,
97 (int)offsetof(struct slap_control_ids, sc_postRead),
98 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
99 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
100 { LDAP_CONTROL_VALUESRETURNFILTER,
101 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
102 SLAP_CTRL_SEARCH, NULL,
103 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
104 { LDAP_CONTROL_PAGEDRESULTS,
105 (int)offsetof(struct slap_control_ids, sc_pagedResults),
106 SLAP_CTRL_SEARCH, NULL,
107 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
108 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
109 { LDAP_CONTROL_X_DOMAIN_SCOPE,
110 (int)offsetof(struct slap_control_ids, sc_domainScope),
111 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
112 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
114 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
115 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
116 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
117 SLAP_CTRL_MODIFY, NULL,
118 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
120 #ifdef LDAP_CONTROL_X_TREE_DELETE
121 { LDAP_CONTROL_X_TREE_DELETE,
122 (int)offsetof(struct slap_control_ids, sc_treeDelete),
123 SLAP_CTRL_DELETE, NULL,
124 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
126 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
127 { LDAP_CONTORL_X_SEARCH_OPTIONS,
128 (int)offsetof(struct slap_control_ids, sc_searchOptions),
129 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
130 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
132 #ifdef LDAP_CONTROL_SUBENTRIES
133 { LDAP_CONTROL_SUBENTRIES,
134 (int)offsetof(struct slap_control_ids, sc_subentries),
135 SLAP_CTRL_SEARCH, NULL,
136 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 (int)offsetof(struct slap_control_ids, sc_noOp),
140 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
141 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
143 (int)offsetof(struct slap_control_ids, sc_LDAPsync),
144 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
145 parseLDAPsync, LDAP_SLIST_ENTRY_INITIALIZER(next) },
146 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
147 { LDAP_CONTROL_MODIFY_INCREMENT,
148 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
149 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
150 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
152 { LDAP_CONTROL_MANAGEDSAIT,
153 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
154 SLAP_CTRL_ACCESS, NULL,
155 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
156 { LDAP_CONTROL_PROXY_AUTHZ,
157 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
158 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
159 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
160 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
164 * Register a supported control.
166 * This can be called by an OpenLDAP plugin or, indirectly, by a
167 * SLAPI plugin calling slapi_register_supported_control().
170 register_supported_control(const char *controloid,
171 slap_mask_t controlmask,
173 SLAP_CTRL_PARSE_FN *controlparsefn,
176 struct slap_control *sc;
178 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
180 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
181 if ( sc == NULL ) return LDAP_NO_MEMORY;
183 sc->sc_oid = ch_strdup( controloid );
184 sc->sc_mask = controlmask;
185 if ( controlexops != NULL ) {
186 sc->sc_extendedops = ldap_charray_dup( controlexops );
187 if ( sc->sc_extendedops == NULL ) {
189 return LDAP_NO_MEMORY;
192 sc->sc_extendedops = NULL;
194 sc->sc_parse = controlparsefn;
196 /* Update slap_known_controls, too. */
197 if ( slap_known_controls == NULL ) {
198 slap_known_controls = (char **)SLAP_MALLOC( 2 * sizeof(char *) );
199 if ( slap_known_controls == NULL ) {
200 if ( sc->sc_extendedops != NULL ) {
201 ldap_charray_free( sc->sc_extendedops );
204 return LDAP_NO_MEMORY;
208 char **new_known_controls;
210 new_known_controls = (char **)SLAP_REALLOC(
211 slap_known_controls, (num_known_controls + 2) * sizeof(char *) );
213 if ( new_known_controls == NULL ) {
214 if ( sc->sc_extendedops != NULL ) {
215 ldap_charray_free( sc->sc_extendedops );
218 return LDAP_NO_MEMORY;
220 slap_known_controls = new_known_controls;
222 if ( controlcid ) *controlcid = num_known_controls;
223 sc->sc_cid = num_known_controls;
224 slap_known_controls[num_known_controls++] = ch_strdup( 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 );
272 ldap_charray_free( slap_known_controls );
276 * Format the supportedControl attribute of the root DSE,
277 * detailing which controls are supported by the directory
281 controls_root_dse_info( Entry *e )
283 AttributeDescription *ad_supportedControl
284 = slap_schema.si_ad_supportedControl;
285 struct berval vals[2];
286 struct slap_control *sc;
288 vals[1].bv_val = NULL;
291 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
292 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
294 vals[0].bv_val = sc->sc_oid;
295 vals[0].bv_len = strlen( sc->sc_oid );
297 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
306 * Return a list of OIDs and operation masks for supported
307 * controls. Used by SLAPI.
310 get_supported_controls(char ***ctrloidsp,
311 slap_mask_t **ctrlmasks)
317 struct slap_control *sc;
321 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
331 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
332 if ( oids == NULL ) {
333 return LDAP_NO_MEMORY;
335 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
336 if ( masks == NULL ) {
338 return LDAP_NO_MEMORY;
343 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
344 oids[n] = ch_strdup( sc->sc_oid );
345 masks[n] = sc->sc_mask;
358 * Find a control given its OID.
360 static struct slap_control *
361 find_ctrl( const char *oid )
363 struct slap_control *sc;
365 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
366 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
374 void slap_free_ctrls(
376 LDAPControl **ctrls )
380 for (i=0; ctrls[i]; i++) {
381 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
383 op->o_tmpfree( ctrls, op->o_tmpmemctx );
395 BerElement *ber = op->o_ber;
396 struct slap_control *sc;
399 len = ber_pvt_ber_remaining(ber);
403 rs->sr_err = LDAP_SUCCESS;
407 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
408 if( tag == LBER_ERROR ) {
409 rs->sr_err = SLAPD_DISCONNECT;
410 rs->sr_text = "unexpected data in PDU";
416 Debug( LDAP_DEBUG_TRACE,
417 "=> get_ctrls\n", 0, 0, 0 );
419 if( op->o_protocol < LDAP_VERSION3 ) {
420 rs->sr_err = SLAPD_DISCONNECT;
421 rs->sr_text = "controls require LDAPv3";
425 /* one for first control, one for termination */
426 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
429 if( op->ctrls == NULL ) {
430 rs->sr_err = LDAP_NO_MEMORY;
431 rs->sr_text = "no memory";
436 op->o_ctrls[nctrls] = NULL;
438 /* step through each element */
439 for( tag = ber_first_element( ber, &len, &opaque );
441 tag = ber_next_element( ber, &len, opaque ) )
444 LDAPControl **tctrls;
446 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
447 memset(c, 0, sizeof(LDAPControl));
449 /* allocate pointer space for current controls (nctrls)
450 * + this control + extra NULL
452 tctrls = op->o_tmprealloc( op->o_ctrls,
453 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
456 if( tctrls == NULL ) {
458 ldap_controls_free(op->o_ctrls);
461 rs->sr_err = LDAP_NO_MEMORY;
462 rs->sr_text = "no memory";
466 op->o_ctrls = tctrls;
468 op->o_ctrls[nctrls++] = c;
469 op->o_ctrls[nctrls] = NULL;
471 tag = ber_scanf( ber, "{m" /*}*/, &bv );
472 c->ldctl_oid = bv.bv_val;
474 if( tag == LBER_ERROR ) {
475 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
478 slap_free_ctrls( op, op->o_ctrls );
480 rs->sr_err = SLAPD_DISCONNECT;
481 rs->sr_text = "decoding controls error";
484 } else if( c->ldctl_oid == NULL ) {
485 Debug( LDAP_DEBUG_TRACE,
486 "get_ctrls: conn %lu got emtpy OID.\n",
487 op->o_connid, 0, 0 );
489 slap_free_ctrls( op, op->o_ctrls );
491 rs->sr_err = LDAP_PROTOCOL_ERROR;
492 rs->sr_text = "OID field is empty";
496 tag = ber_peek_tag( ber, &len );
498 if( tag == LBER_BOOLEAN ) {
500 tag = ber_scanf( ber, "b", &crit );
502 if( tag == LBER_ERROR ) {
503 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
505 slap_free_ctrls( op, op->o_ctrls );
507 rs->sr_err = SLAPD_DISCONNECT;
508 rs->sr_text = "decoding controls error";
512 c->ldctl_iscritical = (crit != 0);
513 tag = ber_peek_tag( ber, &len );
516 if( tag == LBER_OCTETSTRING ) {
517 tag = ber_scanf( ber, "m", &c->ldctl_value );
519 if( tag == LBER_ERROR ) {
520 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
521 "%s (%scritical): get value failed.\n",
522 op->o_connid, c->ldctl_oid,
523 c->ldctl_iscritical ? "" : "non" );
524 slap_free_ctrls( op, op->o_ctrls );
526 rs->sr_err = SLAPD_DISCONNECT;
527 rs->sr_text = "decoding controls error";
532 Debug( LDAP_DEBUG_TRACE,
533 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
534 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
536 sc = find_ctrl( c->ldctl_oid );
538 /* recognized control */
540 switch( op->o_tag ) {
542 tagmask = SLAP_CTRL_ADD;
545 tagmask = SLAP_CTRL_BIND;
547 case LDAP_REQ_COMPARE:
548 tagmask = SLAP_CTRL_COMPARE;
550 case LDAP_REQ_DELETE:
551 tagmask = SLAP_CTRL_DELETE;
553 case LDAP_REQ_MODIFY:
554 tagmask = SLAP_CTRL_MODIFY;
556 case LDAP_REQ_RENAME:
557 tagmask = SLAP_CTRL_RENAME;
559 case LDAP_REQ_SEARCH:
560 tagmask = SLAP_CTRL_SEARCH;
562 case LDAP_REQ_UNBIND:
563 tagmask = SLAP_CTRL_UNBIND;
565 case LDAP_REQ_ABANDON:
566 tagmask = SLAP_CTRL_ABANDON;
568 case LDAP_REQ_EXTENDED:
570 assert( op->ore_reqoid.bv_val != NULL );
571 if( sc->sc_extendedops != NULL ) {
573 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
574 if( strcmp( op->ore_reqoid.bv_val,
575 sc->sc_extendedops[i] ) == 0 )
584 rs->sr_err = LDAP_OTHER;
585 rs->sr_text = "controls internal error";
589 if (( sc->sc_mask & tagmask ) == tagmask ) {
590 /* available extension */
593 if( !sc->sc_parse ) {
594 rs->sr_err = LDAP_OTHER;
595 rs->sr_text = "not yet implemented";
599 rc = sc->sc_parse( op, rs, c );
601 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
606 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
607 /* kludge to disable backend_control() check */
608 c->ldctl_iscritical = 0;
610 } else if ( tagmask == SLAP_CTRL_SEARCH &&
611 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
613 /* kludge to disable backend_control() check */
614 c->ldctl_iscritical = 0;
617 } else if( c->ldctl_iscritical ) {
618 /* unavailable CRITICAL control */
619 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
620 rs->sr_text = "critical extension is unavailable";
624 } else if( c->ldctl_iscritical ) {
625 /* unrecognized CRITICAL control */
626 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
627 rs->sr_text = "critical extension is not recognized";
634 Debug( LDAP_DEBUG_TRACE,
635 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
636 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
638 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
639 if( rs->sr_err == SLAPD_DISCONNECT ) {
640 rs->sr_err = LDAP_PROTOCOL_ERROR;
641 send_ldap_disconnect( op, rs );
642 rs->sr_err = SLAPD_DISCONNECT;
644 send_ldap_result( op, rs );
651 static int parseModifyIncrement (
657 if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
658 rs->sr_text = "modifyIncrement control specified multiple times";
659 return LDAP_PROTOCOL_ERROR;
663 if ( ctrl->ldctl_value.bv_len ) {
664 rs->sr_text = "modifyIncrement control value not empty";
665 return LDAP_PROTOCOL_ERROR;
669 op->o_modifyIncrement = ctrl->ldctl_iscritical
670 ? SLAP_CONTROL_CRITICAL
671 : SLAP_CONTROL_NONCRITICAL;
677 static int parseManageDSAit (
682 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
683 rs->sr_text = "manageDSAit control specified multiple times";
684 return LDAP_PROTOCOL_ERROR;
687 if ( ctrl->ldctl_value.bv_len ) {
688 rs->sr_text = "manageDSAit control value not empty";
689 return LDAP_PROTOCOL_ERROR;
692 op->o_managedsait = ctrl->ldctl_iscritical
693 ? SLAP_CONTROL_CRITICAL
694 : SLAP_CONTROL_NONCRITICAL;
699 static int parseProxyAuthz (
705 struct berval dn = BER_BVNULL;
707 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
708 rs->sr_text = "proxy authorization control specified multiple times";
709 return LDAP_PROTOCOL_ERROR;
712 op->o_proxy_authz = ctrl->ldctl_iscritical
713 ? SLAP_CONTROL_CRITICAL
714 : SLAP_CONTROL_NONCRITICAL;
716 Debug( LDAP_DEBUG_ARGS,
717 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
719 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
722 if( ctrl->ldctl_value.bv_len == 0 ) {
723 Debug( LDAP_DEBUG_TRACE,
724 "parseProxyAuthz: conn=%lu anonymous\n",
725 op->o_connid, 0, 0 );
728 free( op->o_dn.bv_val );
730 op->o_dn.bv_val = ch_strdup( "" );
732 free( op->o_ndn.bv_val );
733 op->o_ndn.bv_len = 0;
734 op->o_ndn.bv_val = ch_strdup( "" );
739 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
740 NULL, &dn, SLAP_GETDN_AUTHZID );
742 /* FIXME: empty DN in proxyAuthz control should be legal... */
743 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
745 ch_free( dn.bv_val );
747 rs->sr_text = "authzId mapping failed";
748 return LDAP_PROXY_AUTHZ_FAILURE;
751 Debug( LDAP_DEBUG_TRACE,
752 "parseProxyAuthz: conn=%lu \"%s\"\n",
754 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
756 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
759 ch_free( dn.bv_val );
760 rs->sr_text = "not authorized to assume identity";
761 return LDAP_PROXY_AUTHZ_FAILURE;
764 ch_free( op->o_dn.bv_val );
765 ch_free( op->o_ndn.bv_val );
767 op->o_dn.bv_val = NULL;
770 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
771 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
774 * NOTE: since slap_sasl_getdn() returns a normalized dn,
775 * from now on op->o_dn is normalized
777 ber_dupbv( &op->o_dn, &dn );
782 static int parseNoOp (
787 if ( op->o_noop != SLAP_CONTROL_NONE ) {
788 rs->sr_text = "noop control specified multiple times";
789 return LDAP_PROTOCOL_ERROR;
792 if ( ctrl->ldctl_value.bv_len ) {
793 rs->sr_text = "noop control value not empty";
794 return LDAP_PROTOCOL_ERROR;
797 op->o_noop = ctrl->ldctl_iscritical
798 ? SLAP_CONTROL_CRITICAL
799 : SLAP_CONTROL_NONCRITICAL;
804 static int parsePagedResults (
809 int rc = LDAP_SUCCESS;
813 struct berval cookie = BER_BVNULL;
814 PagedResultsState *ps;
816 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
817 rs->sr_text = "paged results control specified multiple times";
818 return LDAP_PROTOCOL_ERROR;
821 if ( op->o_sync != SLAP_CONTROL_NONE ) {
822 rs->sr_text = "paged results control specified with sync control";
823 return LDAP_PROTOCOL_ERROR;
826 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
827 rs->sr_text = "paged results control value is empty (or absent)";
828 return LDAP_PROTOCOL_ERROR;
831 /* Parse the control value
832 * realSearchControlValue ::= SEQUENCE {
833 * size INTEGER (0..maxInt),
834 * -- requested page size from client
835 * -- result set size estimate from server
836 * cookie OCTET STRING
839 ber = ber_init( &ctrl->ldctl_value );
841 rs->sr_text = "internal error";
845 tag = ber_scanf( ber, "{im}", &size, &cookie );
847 if ( tag == LBER_ERROR ) {
848 rs->sr_text = "paged results control could not be decoded";
849 rc = LDAP_PROTOCOL_ERROR;
854 rs->sr_text = "paged results control size invalid";
855 rc = LDAP_PROTOCOL_ERROR;
860 /* defer cookie decoding/checks to backend... */
861 if ( cookie.bv_len ) {
862 PagedResultsCookie reqcookie;
863 if( cookie.bv_len != sizeof( reqcookie ) ) {
865 rs->sr_text = "paged results cookie is invalid";
866 rc = LDAP_PROTOCOL_ERROR;
870 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
872 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
874 rs->sr_text = "paged results cookie is invalid";
875 rc = LDAP_PROTOCOL_ERROR;
878 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
879 rs->sr_text = "paged results cookie is invalid or old";
880 rc = LDAP_UNWILLING_TO_PERFORM;
885 /* Initial request. Initialize state. */
887 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
888 /* There's another pagedResults control on the
889 * same connection; reject new pagedResults controls
890 * (allowed by RFC2696) */
891 rs->sr_text = "paged results cookie unavailable; try later";
892 rc = LDAP_UNWILLING_TO_PERFORM;
896 op->o_pagedresults_state.ps_cookie = 0;
897 op->o_pagedresults_state.ps_count = 0;
901 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
902 *ps = op->o_conn->c_pagedresults_state;
904 op->o_pagedresults_state = ps;
906 /* NOTE: according to RFC 2696 3.:
908 If the page size is greater than or equal to the sizeLimit value, the
909 server should ignore the control as the request can be satisfied in a
912 * NOTE: this assumes that the op->ors_slimit be set
913 * before the controls are parsed.
916 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
917 op->o_pagedresults = SLAP_CONTROL_IGNORED;
919 } else if ( ctrl->ldctl_iscritical ) {
920 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
923 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
927 (void)ber_free( ber, 1 );
931 static int parseAssert (
937 struct berval fstr = BER_BVNULL;
938 const char *err_msg = "";
940 if ( op->o_assert != SLAP_CONTROL_NONE ) {
941 rs->sr_text = "assert control specified multiple times";
942 return LDAP_PROTOCOL_ERROR;
945 if ( ctrl->ldctl_value.bv_len == 0 ) {
946 rs->sr_text = "assert control value is empty (or absent)";
947 return LDAP_PROTOCOL_ERROR;
950 ber = ber_init( &(ctrl->ldctl_value) );
952 rs->sr_text = "assert control: internal error";
956 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
958 if( rs->sr_err != LDAP_SUCCESS ) {
959 if( rs->sr_err == SLAPD_DISCONNECT ) {
960 rs->sr_err = LDAP_PROTOCOL_ERROR;
961 send_ldap_disconnect( op, rs );
962 rs->sr_err = SLAPD_DISCONNECT;
964 send_ldap_result( op, rs );
966 if( op->o_assertion != NULL ) {
967 filter_free_x( op, op->o_assertion );
973 filter2bv_x( op, op->o_assertion, &fstr );
975 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
976 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
977 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
980 op->o_assert = ctrl->ldctl_iscritical
981 ? SLAP_CONTROL_CRITICAL
982 : SLAP_CONTROL_NONCRITICAL;
984 rs->sr_err = LDAP_SUCCESS;
988 static int parsePreRead (
993 ber_len_t siz, off, i;
994 AttributeName *an = NULL;
997 if ( op->o_preread != SLAP_CONTROL_NONE ) {
998 rs->sr_text = "preread control specified multiple times";
999 return LDAP_PROTOCOL_ERROR;
1002 if ( ctrl->ldctl_value.bv_len == 0 ) {
1003 rs->sr_text = "preread control value is empty (or absent)";
1004 return LDAP_PROTOCOL_ERROR;
1007 ber = ber_init( &(ctrl->ldctl_value) );
1009 rs->sr_text = "preread control: internal error";
1013 siz = sizeof( AttributeName );
1014 off = offsetof( AttributeName, an_name );
1015 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1016 rs->sr_text = "preread control: decoding error";
1017 return LDAP_PROTOCOL_ERROR;
1020 for( i=0; i<siz; i++ ) {
1021 int rc = LDAP_SUCCESS;
1022 const char *dummy = NULL;
1024 an[i].an_desc = NULL;
1026 an[i].an_oc_exclude = 0;
1027 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1028 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1029 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1034 op->o_preread = ctrl->ldctl_iscritical
1035 ? SLAP_CONTROL_CRITICAL
1036 : SLAP_CONTROL_NONCRITICAL;
1038 op->o_preread_attrs = an;
1040 rs->sr_err = LDAP_SUCCESS;
1041 return LDAP_SUCCESS;
1044 static int parsePostRead (
1049 ber_len_t siz, off, i;
1050 AttributeName *an = NULL;
1053 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1054 rs->sr_text = "postread control specified multiple times";
1055 return LDAP_PROTOCOL_ERROR;
1058 if ( ctrl->ldctl_value.bv_len == 0 ) {
1059 rs->sr_text = "postread control value is empty (or absent)";
1060 return LDAP_PROTOCOL_ERROR;
1063 ber = ber_init( &(ctrl->ldctl_value) );
1065 rs->sr_text = "postread control: internal error";
1069 siz = sizeof( AttributeName );
1070 off = offsetof( AttributeName, an_name );
1071 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1072 rs->sr_text = "postread control: decoding error";
1073 return LDAP_PROTOCOL_ERROR;
1076 for( i=0; i<siz; i++ ) {
1077 int rc = LDAP_SUCCESS;
1078 const char *dummy = NULL;
1080 an[i].an_desc = NULL;
1082 an[i].an_oc_exclude = 0;
1083 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1084 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1085 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1090 op->o_postread = ctrl->ldctl_iscritical
1091 ? SLAP_CONTROL_CRITICAL
1092 : SLAP_CONTROL_NONCRITICAL;
1094 op->o_postread_attrs = an;
1096 rs->sr_err = LDAP_SUCCESS;
1097 return LDAP_SUCCESS;
1100 int parseValuesReturnFilter (
1106 struct berval fstr = BER_BVNULL;
1107 const char *err_msg = "";
1109 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1110 rs->sr_text = "valuesReturnFilter control specified multiple times";
1111 return LDAP_PROTOCOL_ERROR;
1114 if ( ctrl->ldctl_value.bv_len == 0 ) {
1115 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1116 return LDAP_PROTOCOL_ERROR;
1119 ber = ber_init( &(ctrl->ldctl_value) );
1121 rs->sr_text = "internal error";
1125 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1127 if( rs->sr_err != LDAP_SUCCESS ) {
1128 if( rs->sr_err == SLAPD_DISCONNECT ) {
1129 rs->sr_err = LDAP_PROTOCOL_ERROR;
1130 send_ldap_disconnect( op, rs );
1131 rs->sr_err = SLAPD_DISCONNECT;
1133 send_ldap_result( op, rs );
1135 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1139 vrFilter2bv( op, op->o_vrFilter, &fstr );
1142 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1143 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1144 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1147 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1148 ? SLAP_CONTROL_CRITICAL
1149 : SLAP_CONTROL_NONCRITICAL;
1151 rs->sr_err = LDAP_SUCCESS;
1152 return LDAP_SUCCESS;
1155 #ifdef LDAP_CONTROL_SUBENTRIES
1156 static int parseSubentries (
1161 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1162 rs->sr_text = "subentries control specified multiple times";
1163 return LDAP_PROTOCOL_ERROR;
1166 /* FIXME: should use BER library */
1167 if( ( ctrl->ldctl_value.bv_len != 3 )
1168 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1169 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1171 rs->sr_text = "subentries control value encoding is bogus";
1172 return LDAP_PROTOCOL_ERROR;
1175 op->o_subentries = ctrl->ldctl_iscritical
1176 ? SLAP_CONTROL_CRITICAL
1177 : SLAP_CONTROL_NONCRITICAL;
1179 if ( (void *)(ctrl->ldctl_value.bv_val[2] != 0x00))
1180 set_subentries_visibility( op );
1182 return LDAP_SUCCESS;
1186 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1187 static int parsePermissiveModify (
1192 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1193 rs->sr_text = "permissiveModify control specified multiple times";
1194 return LDAP_PROTOCOL_ERROR;
1197 if ( ctrl->ldctl_value.bv_len ) {
1198 rs->sr_text = "permissiveModify control value not empty";
1199 return LDAP_PROTOCOL_ERROR;
1202 op->o_permissive_modify = ctrl->ldctl_iscritical
1203 ? SLAP_CONTROL_CRITICAL
1204 : SLAP_CONTROL_NONCRITICAL;
1206 return LDAP_SUCCESS;
1210 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1211 static int parseDomainScope (
1216 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1217 rs->sr_text = "domainScope control specified multiple times";
1218 return LDAP_PROTOCOL_ERROR;
1221 if ( ctrl->ldctl_value.bv_len ) {
1222 rs->sr_text = "domainScope control value not empty";
1223 return LDAP_PROTOCOL_ERROR;
1226 op->o_domain_scope = ctrl->ldctl_iscritical
1227 ? SLAP_CONTROL_CRITICAL
1228 : SLAP_CONTROL_NONCRITICAL;
1230 return LDAP_SUCCESS;
1234 #ifdef LDAP_CONTROL_X_TREE_DELETE
1235 static int parseTreeDelete (
1240 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1241 rs->sr_text = "treeDelete control specified multiple times";
1242 return LDAP_PROTOCOL_ERROR;
1245 if ( ctrl->ldctl_value.bv_len ) {
1246 rs->sr_text = "treeDelete control value not empty";
1247 return LDAP_PROTOCOL_ERROR;
1250 op->o_tree_delete = ctrl->ldctl_iscritical
1251 ? SLAP_CONTROL_CRITICAL
1252 : SLAP_CONTROL_NONCRITICAL;
1254 return LDAP_SUCCESS;
1258 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1259 static int parseSearchOptions (
1265 ber_int_t search_flags;
1267 if ( ctrl->ldctl_value.bv_len == 0 ) {
1268 rs->sr_text = "searchOptions control value not empty";
1269 return LDAP_PROTOCOL_ERROR;
1272 ber = ber_init( &ctrl->ldctl_value );
1274 rs->sr_text = "internal error";
1278 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1279 rs->sr_text = "searchOptions control decoding error";
1280 return LDAP_PROTOCOL_ERROR;
1283 (void) ber_free( ber, 1 );
1285 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1286 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1287 rs->sr_text = "searchOptions control specified multiple times "
1288 "or with domainScope control";
1289 return LDAP_PROTOCOL_ERROR;
1292 op->o_domain_scope = ctrl->ldctl_iscritical
1293 ? SLAP_CONTROL_CRITICAL
1294 : SLAP_CONTROL_NONCRITICAL;
1297 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1298 /* Other search flags not recognised so far */
1299 rs->sr_text = "searchOptions contained unrecongized flag";
1300 return LDAP_UNWILLING_TO_PERFORM;
1303 return LDAP_SUCCESS;
1307 static int parseLDAPsync (
1316 struct slap_session_entry *se;
1318 if ( op->o_sync != SLAP_CONTROL_NONE ) {
1319 rs->sr_text = "Sync control specified multiple times";
1320 return LDAP_PROTOCOL_ERROR;
1323 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1324 rs->sr_text = "Sync control specified with pagedResults control";
1325 return LDAP_PROTOCOL_ERROR;
1329 if ( ctrl->ldctl_value.bv_len == 0 ) {
1330 rs->sr_text = "Sync control value is empty (or absent)";
1331 return LDAP_PROTOCOL_ERROR;
1334 /* Parse the control value
1335 * syncRequestValue ::= SEQUENCE {
1340 * refreshAndPersist (3)
1342 * cookie syncCookie OPTIONAL
1346 ber = ber_init( &ctrl->ldctl_value );
1348 rs->sr_text = "internal error";
1352 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1353 rs->sr_text = "Sync control : mode decoding error";
1354 return LDAP_PROTOCOL_ERROR;
1358 case LDAP_SYNC_REFRESH_ONLY:
1359 mode = SLAP_SYNC_REFRESH;
1361 case LDAP_SYNC_REFRESH_AND_PERSIST:
1362 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1365 rs->sr_text = "Sync control : unknown update mode";
1366 return LDAP_PROTOCOL_ERROR;
1369 tag = ber_peek_tag( ber, &len );
1371 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1372 struct berval tmp_bv;
1373 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1374 rs->sr_text = "Sync control : cookie decoding error";
1375 return LDAP_PROTOCOL_ERROR;
1377 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1378 slap_parse_sync_cookie( &op->o_sync_state );
1380 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1381 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1382 rs->sr_text = "Sync control : rhint decoding error";
1383 return LDAP_PROTOCOL_ERROR;
1386 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1387 rs->sr_text = "Sync control : decoding error";
1388 return LDAP_PROTOCOL_ERROR;
1391 (void) ber_free( ber, 1 );
1393 op->o_sync_mode = (char) mode;
1395 op->o_sync = ctrl->ldctl_iscritical
1396 ? SLAP_CONTROL_CRITICAL
1397 : SLAP_CONTROL_NONCRITICAL;
1399 return LDAP_SUCCESS;