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);
55 /* Operations supported by control */
58 /* Extended operations supported by control */
59 char **sc_extendedops;
61 /* Control parsing callback */
62 SLAP_CTRL_PARSE_FN *sc_parse;
64 LDAP_SLIST_ENTRY(slap_control) sc_next;
67 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
68 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
71 * all known request control OIDs should be added to this list
73 char **slap_known_controls = NULL;
75 static char *proxy_authz_extops[] = {
76 LDAP_EXOP_MODIFY_PASSWD,
81 static struct slap_control control_defs[] = {
82 { LDAP_CONTROL_ASSERT,
83 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
84 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
85 { LDAP_CONTROL_PRE_READ,
86 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
87 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
88 { LDAP_CONTROL_POST_READ,
89 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
90 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
91 { LDAP_CONTROL_VALUESRETURNFILTER,
92 SLAP_CTRL_SEARCH, NULL,
93 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
94 { LDAP_CONTROL_PAGEDRESULTS,
95 SLAP_CTRL_SEARCH, NULL,
96 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
97 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
98 { LDAP_CONTROL_X_DOMAIN_SCOPE,
99 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
100 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
102 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
103 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
104 SLAP_CTRL_MODIFY, NULL,
105 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
107 #ifdef LDAP_CONTROL_X_TREE_DELETE
108 { LDAP_CONTROL_X_TREE_DELETE,
109 SLAP_CTRL_DELETE, NULL,
110 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
112 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
113 { LDAP_CONTORL_X_SEARCH_OPTIONS,
114 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
115 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
117 #ifdef LDAP_CONTROL_SUBENTRIES
118 { LDAP_CONTROL_SUBENTRIES,
119 SLAP_CTRL_SEARCH, NULL,
120 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
123 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
124 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
126 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
127 parseLDAPsync, LDAP_SLIST_ENTRY_INITIALIZER(next) },
128 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
129 { LDAP_CONTROL_MODIFY_INCREMENT,
130 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
131 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
133 { LDAP_CONTROL_MANAGEDSAIT,
134 SLAP_CTRL_ACCESS, NULL,
135 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
136 { LDAP_CONTROL_PROXY_AUTHZ,
137 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
138 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 { NULL, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
143 * Register a supported control.
145 * This can be called by an OpenLDAP plugin or, indirectly, by a
146 * SLAPI plugin calling slapi_register_supported_control().
149 register_supported_control(const char *controloid,
150 slap_mask_t controlmask,
152 SLAP_CTRL_PARSE_FN *controlparsefn)
154 struct slap_control *sc;
157 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
159 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
160 if ( sc == NULL ) return LDAP_NO_MEMORY;
162 sc->sc_oid = ch_strdup( controloid );
163 sc->sc_mask = controlmask;
164 if ( controlexops != NULL ) {
165 sc->sc_extendedops = ldap_charray_dup( controlexops );
166 if ( sc->sc_extendedops == NULL ) {
168 return LDAP_NO_MEMORY;
171 sc->sc_extendedops = NULL;
173 sc->sc_parse = controlparsefn;
175 /* Update slap_known_controls, too. */
176 if ( slap_known_controls == NULL ) {
177 slap_known_controls = (char **)SLAP_MALLOC( 2 * sizeof(char *) );
178 if ( slap_known_controls == NULL ) {
179 if ( sc->sc_extendedops != NULL ) {
180 ldap_charray_free( sc->sc_extendedops );
183 return LDAP_NO_MEMORY;
185 slap_known_controls[0] = ch_strdup( sc->sc_oid );
186 slap_known_controls[1] = NULL;
189 char **new_known_controls;
191 for ( i = 0; slap_known_controls[i] != NULL; i++ ) {
195 new_known_controls = (char **)SLAP_REALLOC(
196 slap_known_controls, (i + 2) * sizeof(char *) );
198 if ( new_known_controls == NULL ) {
199 if ( sc->sc_extendedops != NULL ) {
200 ldap_charray_free( sc->sc_extendedops );
203 return LDAP_NO_MEMORY;
205 slap_known_controls = new_known_controls;
206 slap_known_controls[i++] = ch_strdup( sc->sc_oid );
207 slap_known_controls[i] = NULL;
210 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
211 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
216 * One-time initialization of internal controls.
219 slap_controls_init( void )
222 struct slap_control *sc;
226 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
227 rc = register_supported_control( control_defs[i].sc_oid,
228 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
229 control_defs[i].sc_parse );
230 if ( rc != LDAP_SUCCESS ) break;
237 * Free memory associated with list of supported controls.
240 controls_destroy( void )
242 struct slap_control *sc;
244 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
245 sc = LDAP_SLIST_FIRST(&controls_list);
246 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
248 ch_free( sc->sc_oid );
249 if ( sc->sc_extendedops != NULL ) {
250 ldap_charray_free( sc->sc_extendedops );
254 ldap_charray_free( slap_known_controls );
258 * Format the supportedControl attribute of the root DSE,
259 * detailing which controls are supported by the directory
263 controls_root_dse_info( Entry *e )
265 AttributeDescription *ad_supportedControl
266 = slap_schema.si_ad_supportedControl;
267 struct berval vals[2];
268 struct slap_control *sc;
270 vals[1].bv_val = NULL;
273 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
274 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
276 vals[0].bv_val = sc->sc_oid;
277 vals[0].bv_len = strlen( sc->sc_oid );
279 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
288 * Return a list of OIDs and operation masks for supported
289 * controls. Used by SLAPI.
292 get_supported_controls(char ***ctrloidsp,
293 slap_mask_t **ctrlmasks)
299 struct slap_control *sc;
303 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
313 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
314 if ( oids == NULL ) {
315 return LDAP_NO_MEMORY;
317 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
318 if ( masks == NULL ) {
320 return LDAP_NO_MEMORY;
325 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
326 oids[n] = ch_strdup( sc->sc_oid );
327 masks[n] = sc->sc_mask;
340 * Find a control given its OID.
342 static struct slap_control *
343 find_ctrl( const char *oid )
345 struct slap_control *sc;
347 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
348 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
356 void slap_free_ctrls(
358 LDAPControl **ctrls )
362 for (i=0; ctrls[i]; i++) {
363 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
365 op->o_tmpfree( ctrls, op->o_tmpmemctx );
377 BerElement *ber = op->o_ber;
378 struct slap_control *sc;
381 len = ber_pvt_ber_remaining(ber);
385 rs->sr_err = LDAP_SUCCESS;
389 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
390 if( tag == LBER_ERROR ) {
391 rs->sr_err = SLAPD_DISCONNECT;
392 rs->sr_text = "unexpected data in PDU";
398 Debug( LDAP_DEBUG_TRACE,
399 "=> get_ctrls\n", 0, 0, 0 );
401 if( op->o_protocol < LDAP_VERSION3 ) {
402 rs->sr_err = SLAPD_DISCONNECT;
403 rs->sr_text = "controls require LDAPv3";
407 /* one for first control, one for termination */
408 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
411 if( op->ctrls == NULL ) {
412 rs->sr_err = LDAP_NO_MEMORY;
413 rs->sr_text = "no memory";
418 op->o_ctrls[nctrls] = NULL;
420 /* step through each element */
421 for( tag = ber_first_element( ber, &len, &opaque );
423 tag = ber_next_element( ber, &len, opaque ) )
426 LDAPControl **tctrls;
428 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
429 memset(c, 0, sizeof(LDAPControl));
431 /* allocate pointer space for current controls (nctrls)
432 * + this control + extra NULL
434 tctrls = op->o_tmprealloc( op->o_ctrls,
435 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
438 if( tctrls == NULL ) {
440 ldap_controls_free(op->o_ctrls);
443 rs->sr_err = LDAP_NO_MEMORY;
444 rs->sr_text = "no memory";
448 op->o_ctrls = tctrls;
450 op->o_ctrls[nctrls++] = c;
451 op->o_ctrls[nctrls] = NULL;
453 tag = ber_scanf( ber, "{m" /*}*/, &bv );
454 c->ldctl_oid = bv.bv_val;
456 if( tag == LBER_ERROR ) {
457 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
460 slap_free_ctrls( op, op->o_ctrls );
462 rs->sr_err = SLAPD_DISCONNECT;
463 rs->sr_text = "decoding controls error";
466 } else if( c->ldctl_oid == NULL ) {
467 Debug( LDAP_DEBUG_TRACE,
468 "get_ctrls: conn %lu got emtpy OID.\n",
469 op->o_connid, 0, 0 );
471 slap_free_ctrls( op, op->o_ctrls );
473 rs->sr_err = LDAP_PROTOCOL_ERROR;
474 rs->sr_text = "OID field is empty";
478 tag = ber_peek_tag( ber, &len );
480 if( tag == LBER_BOOLEAN ) {
482 tag = ber_scanf( ber, "b", &crit );
484 if( tag == LBER_ERROR ) {
485 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
487 slap_free_ctrls( op, op->o_ctrls );
489 rs->sr_err = SLAPD_DISCONNECT;
490 rs->sr_text = "decoding controls error";
494 c->ldctl_iscritical = (crit != 0);
495 tag = ber_peek_tag( ber, &len );
498 if( tag == LBER_OCTETSTRING ) {
499 tag = ber_scanf( ber, "m", &c->ldctl_value );
501 if( tag == LBER_ERROR ) {
502 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
503 "%s (%scritical): get value failed.\n",
504 op->o_connid, c->ldctl_oid,
505 c->ldctl_iscritical ? "" : "non" );
506 slap_free_ctrls( op, op->o_ctrls );
508 rs->sr_err = SLAPD_DISCONNECT;
509 rs->sr_text = "decoding controls error";
514 Debug( LDAP_DEBUG_TRACE,
515 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
516 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
518 sc = find_ctrl( c->ldctl_oid );
520 /* recognized control */
522 switch( op->o_tag ) {
524 tagmask = SLAP_CTRL_ADD;
527 tagmask = SLAP_CTRL_BIND;
529 case LDAP_REQ_COMPARE:
530 tagmask = SLAP_CTRL_COMPARE;
532 case LDAP_REQ_DELETE:
533 tagmask = SLAP_CTRL_DELETE;
535 case LDAP_REQ_MODIFY:
536 tagmask = SLAP_CTRL_MODIFY;
538 case LDAP_REQ_RENAME:
539 tagmask = SLAP_CTRL_RENAME;
541 case LDAP_REQ_SEARCH:
542 tagmask = SLAP_CTRL_SEARCH;
544 case LDAP_REQ_UNBIND:
545 tagmask = SLAP_CTRL_UNBIND;
547 case LDAP_REQ_ABANDON:
548 tagmask = SLAP_CTRL_ABANDON;
550 case LDAP_REQ_EXTENDED:
552 assert( op->ore_reqoid.bv_val != NULL );
553 if( sc->sc_extendedops != NULL ) {
555 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
556 if( strcmp( op->ore_reqoid.bv_val,
557 sc->sc_extendedops[i] ) == 0 )
566 rs->sr_err = LDAP_OTHER;
567 rs->sr_text = "controls internal error";
571 if (( sc->sc_mask & tagmask ) == tagmask ) {
572 /* available extension */
575 if( !sc->sc_parse ) {
576 rs->sr_err = LDAP_OTHER;
577 rs->sr_text = "not yet implemented";
581 rc = sc->sc_parse( op, rs, c );
583 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
588 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
589 /* kludge to disable backend_control() check */
590 c->ldctl_iscritical = 0;
592 } else if ( tagmask == SLAP_CTRL_SEARCH &&
593 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
595 /* kludge to disable backend_control() check */
596 c->ldctl_iscritical = 0;
599 } else if( c->ldctl_iscritical ) {
600 /* unavailable CRITICAL control */
601 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
602 rs->sr_text = "critical extension is unavailable";
606 } else if( c->ldctl_iscritical ) {
607 /* unrecognized CRITICAL control */
608 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
609 rs->sr_text = "critical extension is not recognized";
616 Debug( LDAP_DEBUG_TRACE,
617 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
618 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
620 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
621 if( rs->sr_err == SLAPD_DISCONNECT ) {
622 rs->sr_err = LDAP_PROTOCOL_ERROR;
623 send_ldap_disconnect( op, rs );
624 rs->sr_err = SLAPD_DISCONNECT;
626 send_ldap_result( op, rs );
633 static int parseModifyIncrement (
639 if ( op->o_modifyIncrement != SLAP_NO_CONTROL ) {
640 rs->sr_text = "modifyIncrement control specified multiple times";
641 return LDAP_PROTOCOL_ERROR;
645 if ( ctrl->ldctl_value.bv_len ) {
646 rs->sr_text = "modifyIncrement control value not empty";
647 return LDAP_PROTOCOL_ERROR;
651 op->o_modifyIncrement = ctrl->ldctl_iscritical
652 ? SLAP_CRITICAL_CONTROL
653 : SLAP_NONCRITICAL_CONTROL;
659 static int parseManageDSAit (
664 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
665 rs->sr_text = "manageDSAit control specified multiple times";
666 return LDAP_PROTOCOL_ERROR;
669 if ( ctrl->ldctl_value.bv_len ) {
670 rs->sr_text = "manageDSAit control value not empty";
671 return LDAP_PROTOCOL_ERROR;
674 op->o_managedsait = ctrl->ldctl_iscritical
675 ? SLAP_CRITICAL_CONTROL
676 : SLAP_NONCRITICAL_CONTROL;
681 static int parseProxyAuthz (
687 struct berval dn = BER_BVNULL;
689 if ( op->o_proxy_authz != SLAP_NO_CONTROL ) {
690 rs->sr_text = "proxy authorization control specified multiple times";
691 return LDAP_PROTOCOL_ERROR;
694 op->o_proxy_authz = ctrl->ldctl_iscritical
695 ? SLAP_CRITICAL_CONTROL
696 : SLAP_NONCRITICAL_CONTROL;
698 Debug( LDAP_DEBUG_ARGS,
699 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
701 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
704 if( ctrl->ldctl_value.bv_len == 0 ) {
705 Debug( LDAP_DEBUG_TRACE,
706 "parseProxyAuthz: conn=%lu anonymous\n",
707 op->o_connid, 0, 0 );
710 free( op->o_dn.bv_val );
712 op->o_dn.bv_val = ch_strdup( "" );
714 free( op->o_ndn.bv_val );
715 op->o_ndn.bv_len = 0;
716 op->o_ndn.bv_val = ch_strdup( "" );
721 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
722 NULL, &dn, SLAP_GETDN_AUTHZID );
724 /* FIXME: empty DN in proxyAuthz control should be legal... */
725 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
727 ch_free( dn.bv_val );
729 rs->sr_text = "authzId mapping failed";
730 return LDAP_PROXY_AUTHZ_FAILURE;
733 Debug( LDAP_DEBUG_TRACE,
734 "parseProxyAuthz: conn=%lu \"%s\"\n",
736 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
738 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
741 ch_free( dn.bv_val );
742 rs->sr_text = "not authorized to assume identity";
743 return LDAP_PROXY_AUTHZ_FAILURE;
746 ch_free( op->o_dn.bv_val );
747 ch_free( op->o_ndn.bv_val );
749 op->o_dn.bv_val = NULL;
752 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
753 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
756 * NOTE: since slap_sasl_getdn() returns a normalized dn,
757 * from now on op->o_dn is normalized
759 ber_dupbv( &op->o_dn, &dn );
764 static int parseNoOp (
769 if ( op->o_noop != SLAP_NO_CONTROL ) {
770 rs->sr_text = "noop control specified multiple times";
771 return LDAP_PROTOCOL_ERROR;
774 if ( ctrl->ldctl_value.bv_len ) {
775 rs->sr_text = "noop control value not empty";
776 return LDAP_PROTOCOL_ERROR;
779 op->o_noop = ctrl->ldctl_iscritical
780 ? SLAP_CRITICAL_CONTROL
781 : SLAP_NONCRITICAL_CONTROL;
786 static int parsePagedResults (
791 int rc = LDAP_SUCCESS;
795 struct berval cookie = BER_BVNULL;
797 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
798 rs->sr_text = "paged results control specified multiple times";
799 return LDAP_PROTOCOL_ERROR;
802 if ( op->o_sync != SLAP_NO_CONTROL ) {
803 rs->sr_text = "paged results control specified with sync control";
804 return LDAP_PROTOCOL_ERROR;
807 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
808 rs->sr_text = "paged results control value is empty (or absent)";
809 return LDAP_PROTOCOL_ERROR;
812 /* Parse the control value
813 * realSearchControlValue ::= SEQUENCE {
814 * size INTEGER (0..maxInt),
815 * -- requested page size from client
816 * -- result set size estimate from server
817 * cookie OCTET STRING
820 ber = ber_init( &ctrl->ldctl_value );
822 rs->sr_text = "internal error";
826 tag = ber_scanf( ber, "{im}", &size, &cookie );
828 if ( tag == LBER_ERROR ) {
829 rs->sr_text = "paged results control could not be decoded";
830 rc = LDAP_PROTOCOL_ERROR;
835 rs->sr_text = "paged results control size invalid";
836 rc = LDAP_PROTOCOL_ERROR;
841 /* defer cookie decoding/checks to backend... */
842 if ( cookie.bv_len ) {
843 PagedResultsCookie reqcookie;
844 if( cookie.bv_len != sizeof( reqcookie ) ) {
846 rs->sr_text = "paged results cookie is invalid";
847 rc = LDAP_PROTOCOL_ERROR;
851 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
853 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
855 rs->sr_text = "paged results cookie is invalid";
856 rc = LDAP_PROTOCOL_ERROR;
859 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
860 rs->sr_text = "paged results cookie is invalid or old";
861 rc = LDAP_UNWILLING_TO_PERFORM;
866 /* Initial request. Initialize state. */
868 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
869 /* There's another pagedResults control on the
870 * same connection; reject new pagedResults controls
871 * (allowed by RFC2696) */
872 rs->sr_text = "paged results cookie unavailable; try later";
873 rc = LDAP_UNWILLING_TO_PERFORM;
877 op->o_pagedresults_state.ps_cookie = 0;
878 op->o_pagedresults_state.ps_count = 0;
882 op->o_pagedresults_size = size;
884 /* NOTE: according to RFC 2696 3.:
886 If the page size is greater than or equal to the sizeLimit value, the
887 server should ignore the control as the request can be satisfied in a
890 * NOTE: this assumes that the op->ors_slimit be set
891 * before the controls are parsed.
894 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
895 op->o_pagedresults = SLAP_IGNORED_CONTROL;
897 } else if ( ctrl->ldctl_iscritical ) {
898 op->o_pagedresults = SLAP_CRITICAL_CONTROL;
901 op->o_pagedresults = SLAP_NONCRITICAL_CONTROL;
905 (void)ber_free( ber, 1 );
909 static int parseAssert (
915 struct berval fstr = BER_BVNULL;
916 const char *err_msg = "";
918 if ( op->o_assert != SLAP_NO_CONTROL ) {
919 rs->sr_text = "assert control specified multiple times";
920 return LDAP_PROTOCOL_ERROR;
923 if ( ctrl->ldctl_value.bv_len == 0 ) {
924 rs->sr_text = "assert control value is empty (or absent)";
925 return LDAP_PROTOCOL_ERROR;
928 ber = ber_init( &(ctrl->ldctl_value) );
930 rs->sr_text = "assert control: internal error";
934 rs->sr_err = get_filter( op, ber, &(op->o_assertion), &rs->sr_text);
936 if( rs->sr_err != LDAP_SUCCESS ) {
937 if( rs->sr_err == SLAPD_DISCONNECT ) {
938 rs->sr_err = LDAP_PROTOCOL_ERROR;
939 send_ldap_disconnect( op, rs );
940 rs->sr_err = SLAPD_DISCONNECT;
942 send_ldap_result( op, rs );
944 if( op->o_assertion != NULL ) {
945 filter_free_x( op, op->o_assertion );
951 filter2bv_x( op, op->o_assertion, &fstr );
953 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
954 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
955 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
958 op->o_assert = ctrl->ldctl_iscritical
959 ? SLAP_CRITICAL_CONTROL
960 : SLAP_NONCRITICAL_CONTROL;
962 rs->sr_err = LDAP_SUCCESS;
966 static int parsePreRead (
971 ber_len_t siz, off, i;
972 AttributeName *an = NULL;
975 if ( op->o_preread != SLAP_NO_CONTROL ) {
976 rs->sr_text = "preread control specified multiple times";
977 return LDAP_PROTOCOL_ERROR;
980 if ( ctrl->ldctl_value.bv_len == 0 ) {
981 rs->sr_text = "preread control value is empty (or absent)";
982 return LDAP_PROTOCOL_ERROR;
985 ber = ber_init( &(ctrl->ldctl_value) );
987 rs->sr_text = "preread control: internal error";
991 siz = sizeof( AttributeName );
992 off = offsetof( AttributeName, an_name );
993 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
994 rs->sr_text = "preread control: decoding error";
995 return LDAP_PROTOCOL_ERROR;
998 for( i=0; i<siz; i++ ) {
999 int rc = LDAP_SUCCESS;
1000 const char *dummy = NULL;
1002 an[i].an_desc = NULL;
1004 an[i].an_oc_exclude = 0;
1005 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1006 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1007 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1012 op->o_preread = ctrl->ldctl_iscritical
1013 ? SLAP_CRITICAL_CONTROL
1014 : SLAP_NONCRITICAL_CONTROL;
1016 op->o_preread_attrs = an;
1018 rs->sr_err = LDAP_SUCCESS;
1019 return LDAP_SUCCESS;
1022 static int parsePostRead (
1027 ber_len_t siz, off, i;
1028 AttributeName *an = NULL;
1031 if ( op->o_postread != SLAP_NO_CONTROL ) {
1032 rs->sr_text = "postread control specified multiple times";
1033 return LDAP_PROTOCOL_ERROR;
1036 if ( ctrl->ldctl_value.bv_len == 0 ) {
1037 rs->sr_text = "postread control value is empty (or absent)";
1038 return LDAP_PROTOCOL_ERROR;
1041 ber = ber_init( &(ctrl->ldctl_value) );
1043 rs->sr_text = "postread control: internal error";
1047 siz = sizeof( AttributeName );
1048 off = offsetof( AttributeName, an_name );
1049 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1050 rs->sr_text = "postread control: decoding error";
1051 return LDAP_PROTOCOL_ERROR;
1054 for( i=0; i<siz; i++ ) {
1055 int rc = LDAP_SUCCESS;
1056 const char *dummy = NULL;
1058 an[i].an_desc = NULL;
1060 an[i].an_oc_exclude = 0;
1061 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1062 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1063 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1068 op->o_postread = ctrl->ldctl_iscritical
1069 ? SLAP_CRITICAL_CONTROL
1070 : SLAP_NONCRITICAL_CONTROL;
1072 op->o_postread_attrs = an;
1074 rs->sr_err = LDAP_SUCCESS;
1075 return LDAP_SUCCESS;
1078 int parseValuesReturnFilter (
1084 struct berval fstr = BER_BVNULL;
1085 const char *err_msg = "";
1087 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
1088 rs->sr_text = "valuesReturnFilter control specified multiple times";
1089 return LDAP_PROTOCOL_ERROR;
1092 if ( ctrl->ldctl_value.bv_len == 0 ) {
1093 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1094 return LDAP_PROTOCOL_ERROR;
1097 ber = ber_init( &(ctrl->ldctl_value) );
1099 rs->sr_text = "internal error";
1103 rs->sr_err = get_vrFilter( op, ber, &(op->o_vrFilter), &rs->sr_text);
1105 if( rs->sr_err != LDAP_SUCCESS ) {
1106 if( rs->sr_err == SLAPD_DISCONNECT ) {
1107 rs->sr_err = LDAP_PROTOCOL_ERROR;
1108 send_ldap_disconnect( op, rs );
1109 rs->sr_err = SLAPD_DISCONNECT;
1111 send_ldap_result( op, rs );
1113 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1117 vrFilter2bv( op, op->o_vrFilter, &fstr );
1120 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1121 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1122 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1125 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1126 ? SLAP_CRITICAL_CONTROL
1127 : SLAP_NONCRITICAL_CONTROL;
1129 rs->sr_err = LDAP_SUCCESS;
1130 return LDAP_SUCCESS;
1133 #ifdef LDAP_CONTROL_SUBENTRIES
1134 static int parseSubentries (
1139 if ( op->o_subentries != SLAP_NO_CONTROL ) {
1140 rs->sr_text = "subentries control specified multiple times";
1141 return LDAP_PROTOCOL_ERROR;
1144 /* FIXME: should use BER library */
1145 if( ( ctrl->ldctl_value.bv_len != 3 )
1146 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1147 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1149 rs->sr_text = "subentries control value encoding is bogus";
1150 return LDAP_PROTOCOL_ERROR;
1153 op->o_subentries = ctrl->ldctl_iscritical
1154 ? SLAP_CRITICAL_CONTROL
1155 : SLAP_NONCRITICAL_CONTROL;
1157 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
1159 return LDAP_SUCCESS;
1163 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1164 static int parsePermissiveModify (
1169 if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
1170 rs->sr_text = "permissiveModify control specified multiple times";
1171 return LDAP_PROTOCOL_ERROR;
1174 if ( ctrl->ldctl_value.bv_len ) {
1175 rs->sr_text = "permissiveModify control value not empty";
1176 return LDAP_PROTOCOL_ERROR;
1179 op->o_permissive_modify = ctrl->ldctl_iscritical
1180 ? SLAP_CRITICAL_CONTROL
1181 : SLAP_NONCRITICAL_CONTROL;
1183 return LDAP_SUCCESS;
1187 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1188 static int parseDomainScope (
1193 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1194 rs->sr_text = "domainScope control specified multiple times";
1195 return LDAP_PROTOCOL_ERROR;
1198 if ( ctrl->ldctl_value.bv_len ) {
1199 rs->sr_text = "domainScope control value not empty";
1200 return LDAP_PROTOCOL_ERROR;
1203 op->o_domain_scope = ctrl->ldctl_iscritical
1204 ? SLAP_CRITICAL_CONTROL
1205 : SLAP_NONCRITICAL_CONTROL;
1207 return LDAP_SUCCESS;
1211 #ifdef LDAP_CONTROL_X_TREE_DELETE
1212 static int parseTreeDelete (
1217 if ( op->o_tree_delete != SLAP_NO_CONTROL ) {
1218 rs->sr_text = "treeDelete control specified multiple times";
1219 return LDAP_PROTOCOL_ERROR;
1222 if ( ctrl->ldctl_value.bv_len ) {
1223 rs->sr_text = "treeDelete control value not empty";
1224 return LDAP_PROTOCOL_ERROR;
1227 op->o_tree_delete = ctrl->ldctl_iscritical
1228 ? SLAP_CRITICAL_CONTROL
1229 : SLAP_NONCRITICAL_CONTROL;
1231 return LDAP_SUCCESS;
1235 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1236 static int parseSearchOptions (
1242 ber_int_t search_flags;
1244 if ( ctrl->ldctl_value.bv_len == 0 ) {
1245 rs->sr_text = "searchOptions control value not empty";
1246 return LDAP_PROTOCOL_ERROR;
1249 ber = ber_init( &ctrl->ldctl_value );
1251 rs->sr_text = "internal error";
1255 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1256 rs->sr_text = "searchOptions control decoding error";
1257 return LDAP_PROTOCOL_ERROR;
1260 (void) ber_free( ber, 1 );
1262 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1263 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1264 rs->sr_text = "searchOptions control specified multiple times "
1265 "or with domainScope control";
1266 return LDAP_PROTOCOL_ERROR;
1269 op->o_domain_scope = ctrl->ldctl_iscritical
1270 ? SLAP_CRITICAL_CONTROL
1271 : SLAP_NONCRITICAL_CONTROL;
1274 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1275 /* Other search flags not recognised so far */
1276 rs->sr_text = "searchOptions contained unrecongized flag";
1277 return LDAP_UNWILLING_TO_PERFORM;
1280 return LDAP_SUCCESS;
1284 static int parseLDAPsync (
1293 struct slap_session_entry *se;
1295 if ( op->o_sync != SLAP_NO_CONTROL ) {
1296 rs->sr_text = "Sync control specified multiple times";
1297 return LDAP_PROTOCOL_ERROR;
1300 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
1301 rs->sr_text = "Sync control specified with pagedResults control";
1302 return LDAP_PROTOCOL_ERROR;
1306 if ( ctrl->ldctl_value.bv_len == 0 ) {
1307 rs->sr_text = "Sync control value is empty (or absent)";
1308 return LDAP_PROTOCOL_ERROR;
1311 /* Parse the control value
1312 * syncRequestValue ::= SEQUENCE {
1317 * refreshAndPersist (3)
1319 * cookie syncCookie OPTIONAL
1323 ber = ber_init( &ctrl->ldctl_value );
1325 rs->sr_text = "internal error";
1329 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1330 rs->sr_text = "Sync control : mode decoding error";
1331 return LDAP_PROTOCOL_ERROR;
1335 case LDAP_SYNC_REFRESH_ONLY:
1336 mode = SLAP_SYNC_REFRESH;
1338 case LDAP_SYNC_REFRESH_AND_PERSIST:
1339 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1342 rs->sr_text = "Sync control : unknown update mode";
1343 return LDAP_PROTOCOL_ERROR;
1346 tag = ber_peek_tag( ber, &len );
1348 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1349 struct berval tmp_bv;
1350 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1351 rs->sr_text = "Sync control : cookie decoding error";
1352 return LDAP_PROTOCOL_ERROR;
1354 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1355 slap_parse_sync_cookie( &op->o_sync_state );
1357 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1358 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1359 rs->sr_text = "Sync control : rhint decoding error";
1360 return LDAP_PROTOCOL_ERROR;
1363 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1364 rs->sr_text = "Sync control : decoding error";
1365 return LDAP_PROTOCOL_ERROR;
1368 (void) ber_free( ber, 1 );
1370 op->o_sync_mode = (char) mode;
1372 op->o_sync = ctrl->ldctl_iscritical
1373 ? SLAP_CRITICAL_CONTROL
1374 : SLAP_NONCRITICAL_CONTROL;
1376 return LDAP_SUCCESS;