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";
399 LDAP_LOG( OPERATION, ENTRY,
400 "get_ctrls: conn %lu\n", op->o_connid, 0, 0 );
402 Debug( LDAP_DEBUG_TRACE,
403 "=> get_ctrls\n", 0, 0, 0 );
406 if( op->o_protocol < LDAP_VERSION3 ) {
407 rs->sr_err = SLAPD_DISCONNECT;
408 rs->sr_text = "controls require LDAPv3";
412 /* one for first control, one for termination */
413 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
416 if( op->ctrls == NULL ) {
417 rs->sr_err = LDAP_NO_MEMORY;
418 rs->sr_text = "no memory";
423 op->o_ctrls[nctrls] = NULL;
425 /* step through each element */
426 for( tag = ber_first_element( ber, &len, &opaque );
428 tag = ber_next_element( ber, &len, opaque ) )
431 LDAPControl **tctrls;
433 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
434 memset(c, 0, sizeof(LDAPControl));
436 /* allocate pointer space for current controls (nctrls)
437 * + this control + extra NULL
439 tctrls = op->o_tmprealloc( op->o_ctrls,
440 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
443 if( tctrls == NULL ) {
445 ldap_controls_free(op->o_ctrls);
448 rs->sr_err = LDAP_NO_MEMORY;
449 rs->sr_text = "no memory";
453 op->o_ctrls = tctrls;
455 op->o_ctrls[nctrls++] = c;
456 op->o_ctrls[nctrls] = NULL;
458 tag = ber_scanf( ber, "{m" /*}*/, &bv );
459 c->ldctl_oid = bv.bv_val;
461 if( tag == LBER_ERROR ) {
463 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
464 op->o_connid, 0, 0 );
466 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
470 slap_free_ctrls( op, op->o_ctrls );
472 rs->sr_err = SLAPD_DISCONNECT;
473 rs->sr_text = "decoding controls error";
476 } else if( c->ldctl_oid == NULL ) {
478 LDAP_LOG( OPERATION, INFO,
479 "get_ctrls: conn %lu got emtpy OID.\n",
480 op->o_connid, 0, 0 );
482 Debug( LDAP_DEBUG_TRACE,
483 "get_ctrls: conn %lu got emtpy OID.\n",
484 op->o_connid, 0, 0 );
487 slap_free_ctrls( op, op->o_ctrls );
489 rs->sr_err = LDAP_PROTOCOL_ERROR;
490 rs->sr_text = "OID field is empty";
494 tag = ber_peek_tag( ber, &len );
496 if( tag == LBER_BOOLEAN ) {
498 tag = ber_scanf( ber, "b", &crit );
500 if( tag == LBER_ERROR ) {
502 LDAP_LOG( OPERATION, INFO,
503 "get_ctrls: conn %lu get crit failed.\n",
504 op->o_connid, 0, 0 );
506 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
509 slap_free_ctrls( op, op->o_ctrls );
511 rs->sr_err = SLAPD_DISCONNECT;
512 rs->sr_text = "decoding controls error";
516 c->ldctl_iscritical = (crit != 0);
517 tag = ber_peek_tag( ber, &len );
520 if( tag == LBER_OCTETSTRING ) {
521 tag = ber_scanf( ber, "m", &c->ldctl_value );
523 if( tag == LBER_ERROR ) {
525 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
526 "%s (%scritical): get value failed.\n",
527 op->o_connid, c->ldctl_oid,
528 c->ldctl_iscritical ? "" : "non" );
530 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
531 "%s (%scritical): get value failed.\n",
532 op->o_connid, c->ldctl_oid,
533 c->ldctl_iscritical ? "" : "non" );
535 slap_free_ctrls( op, op->o_ctrls );
537 rs->sr_err = SLAPD_DISCONNECT;
538 rs->sr_text = "decoding controls error";
544 LDAP_LOG( OPERATION, INFO,
545 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
546 op->o_connid, c->ldctl_oid, c->ldctl_iscritical ? "" : "non" );
548 Debug( LDAP_DEBUG_TRACE,
549 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
550 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
553 sc = find_ctrl( c->ldctl_oid );
555 /* recognized control */
557 switch( op->o_tag ) {
559 tagmask = SLAP_CTRL_ADD;
562 tagmask = SLAP_CTRL_BIND;
564 case LDAP_REQ_COMPARE:
565 tagmask = SLAP_CTRL_COMPARE;
567 case LDAP_REQ_DELETE:
568 tagmask = SLAP_CTRL_DELETE;
570 case LDAP_REQ_MODIFY:
571 tagmask = SLAP_CTRL_MODIFY;
573 case LDAP_REQ_RENAME:
574 tagmask = SLAP_CTRL_RENAME;
576 case LDAP_REQ_SEARCH:
577 tagmask = SLAP_CTRL_SEARCH;
579 case LDAP_REQ_UNBIND:
580 tagmask = SLAP_CTRL_UNBIND;
582 case LDAP_REQ_ABANDON:
583 tagmask = SLAP_CTRL_ABANDON;
585 case LDAP_REQ_EXTENDED:
587 assert( op->ore_reqoid.bv_val != NULL );
588 if( sc->sc_extendedops != NULL ) {
590 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
591 if( strcmp( op->ore_reqoid.bv_val,
592 sc->sc_extendedops[i] ) == 0 )
601 rs->sr_err = LDAP_OTHER;
602 rs->sr_text = "controls internal error";
606 if (( sc->sc_mask & tagmask ) == tagmask ) {
607 /* available extension */
610 if( !sc->sc_parse ) {
611 rs->sr_err = LDAP_OTHER;
612 rs->sr_text = "not yet implemented";
616 rc = sc->sc_parse( op, rs, c );
618 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
623 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
624 /* kludge to disable backend_control() check */
625 c->ldctl_iscritical = 0;
627 } else if ( tagmask == SLAP_CTRL_SEARCH &&
628 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
630 /* kludge to disable backend_control() check */
631 c->ldctl_iscritical = 0;
634 } else if( c->ldctl_iscritical ) {
635 /* unavailable CRITICAL control */
636 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
637 rs->sr_text = "critical extension is unavailable";
641 } else if( c->ldctl_iscritical ) {
642 /* unrecognized CRITICAL control */
643 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
644 rs->sr_text = "critical extension is not recognized";
652 LDAP_LOG( OPERATION, RESULTS,
653 "get_ctrls: n=%d rc=%d err=\"%s\"\n",
654 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
656 Debug( LDAP_DEBUG_TRACE,
657 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
658 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
661 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
662 if( rs->sr_err == SLAPD_DISCONNECT ) {
663 rs->sr_err = LDAP_PROTOCOL_ERROR;
664 send_ldap_disconnect( op, rs );
665 rs->sr_err = SLAPD_DISCONNECT;
667 send_ldap_result( op, rs );
674 static int parseModifyIncrement (
680 if ( op->o_modifyIncrement != SLAP_NO_CONTROL ) {
681 rs->sr_text = "modifyIncrement control specified multiple times";
682 return LDAP_PROTOCOL_ERROR;
686 if ( ctrl->ldctl_value.bv_len ) {
687 rs->sr_text = "modifyIncrement control value not empty";
688 return LDAP_PROTOCOL_ERROR;
692 op->o_modifyIncrement = ctrl->ldctl_iscritical
693 ? SLAP_CRITICAL_CONTROL
694 : SLAP_NONCRITICAL_CONTROL;
700 static int parseManageDSAit (
705 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
706 rs->sr_text = "manageDSAit control specified multiple times";
707 return LDAP_PROTOCOL_ERROR;
710 if ( ctrl->ldctl_value.bv_len ) {
711 rs->sr_text = "manageDSAit control value not empty";
712 return LDAP_PROTOCOL_ERROR;
715 op->o_managedsait = ctrl->ldctl_iscritical
716 ? SLAP_CRITICAL_CONTROL
717 : SLAP_NONCRITICAL_CONTROL;
722 static int parseProxyAuthz (
728 struct berval dn = BER_BVNULL;
730 if ( op->o_proxy_authz != SLAP_NO_CONTROL ) {
731 rs->sr_text = "proxy authorization control specified multiple times";
732 return LDAP_PROTOCOL_ERROR;
735 op->o_proxy_authz = ctrl->ldctl_iscritical
736 ? SLAP_CRITICAL_CONTROL
737 : SLAP_NONCRITICAL_CONTROL;
740 LDAP_LOG( OPERATION, ARGS,
741 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
743 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
746 Debug( LDAP_DEBUG_ARGS,
747 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
749 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
753 if( ctrl->ldctl_value.bv_len == 0 ) {
755 LDAP_LOG( OPERATION, RESULTS,
756 "parseProxyAuthz: conn=%lu anonymous\n",
757 op->o_connid, 0, 0 );
759 Debug( LDAP_DEBUG_TRACE,
760 "parseProxyAuthz: conn=%lu anonymous\n",
761 op->o_connid, 0, 0 );
765 free( op->o_dn.bv_val );
767 op->o_dn.bv_val = ch_strdup( "" );
769 free( op->o_ndn.bv_val );
770 op->o_ndn.bv_len = 0;
771 op->o_ndn.bv_val = ch_strdup( "" );
776 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
777 NULL, &dn, SLAP_GETDN_AUTHZID );
779 /* FIXME: empty DN in proxyAuthz control should be legal... */
780 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
782 ch_free( dn.bv_val );
784 rs->sr_text = "authzId mapping failed";
785 return LDAP_PROXY_AUTHZ_FAILURE;
789 LDAP_LOG( OPERATION, RESULTS,
790 "parseProxyAuthz: conn=%lu \"%s\"\n",
792 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
794 Debug( LDAP_DEBUG_TRACE,
795 "parseProxyAuthz: conn=%lu \"%s\"\n",
797 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
800 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
803 ch_free( dn.bv_val );
804 rs->sr_text = "not authorized to assume identity";
805 return LDAP_PROXY_AUTHZ_FAILURE;
808 ch_free( op->o_dn.bv_val );
809 ch_free( op->o_ndn.bv_val );
811 op->o_dn.bv_val = NULL;
814 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu PROXYAUTHZ dn=\"%s\"\n",
815 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
818 * NOTE: since slap_sasl_getdn() returns a normalized dn,
819 * from now on op->o_dn is normalized
821 ber_dupbv( &op->o_dn, &dn );
826 static int parseNoOp (
831 if ( op->o_noop != SLAP_NO_CONTROL ) {
832 rs->sr_text = "noop control specified multiple times";
833 return LDAP_PROTOCOL_ERROR;
836 if ( ctrl->ldctl_value.bv_len ) {
837 rs->sr_text = "noop control value not empty";
838 return LDAP_PROTOCOL_ERROR;
841 op->o_noop = ctrl->ldctl_iscritical
842 ? SLAP_CRITICAL_CONTROL
843 : SLAP_NONCRITICAL_CONTROL;
848 static int parsePagedResults (
853 int rc = LDAP_SUCCESS;
857 struct berval cookie = BER_BVNULL;
859 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
860 rs->sr_text = "paged results control specified multiple times";
861 return LDAP_PROTOCOL_ERROR;
864 if ( op->o_sync != SLAP_NO_CONTROL ) {
865 rs->sr_text = "paged results control specified with sync control";
866 return LDAP_PROTOCOL_ERROR;
869 if ( ctrl->ldctl_value.bv_len == 0 ) {
870 rs->sr_text = "paged results control value is empty (or absent)";
871 return LDAP_PROTOCOL_ERROR;
874 /* Parse the control value
875 * realSearchControlValue ::= SEQUENCE {
876 * size INTEGER (0..maxInt),
877 * -- requested page size from client
878 * -- result set size estimate from server
879 * cookie OCTET STRING
882 ber = ber_init( &ctrl->ldctl_value );
884 rs->sr_text = "internal error";
888 tag = ber_scanf( ber, "{im}", &size, &cookie );
890 if ( tag == LBER_ERROR ) {
891 rs->sr_text = "paged results control could not be decoded";
892 rc = LDAP_PROTOCOL_ERROR;
897 rs->sr_text = "paged results control size invalid";
898 rc = LDAP_PROTOCOL_ERROR;
903 /* defer cookie decoding/checks to backend... */
904 if ( cookie.bv_len ) {
905 PagedResultsCookie reqcookie;
906 if( cookie.bv_len != sizeof( reqcookie ) ) {
908 rs->sr_text = "paged results cookie is invalid";
909 rc = LDAP_PROTOCOL_ERROR;
913 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
915 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
917 rs->sr_text = "paged results cookie is invalid";
918 rc = LDAP_PROTOCOL_ERROR;
921 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
922 rs->sr_text = "paged results cookie is invalid or old";
923 rc = LDAP_UNWILLING_TO_PERFORM;
928 /* Initial request. Initialize state. */
930 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
931 /* There's another pagedResults control on the
932 * same connection; reject new pagedResults controls
933 * (allowed by RFC2696) */
934 rs->sr_text = "paged results cookie unavailable; try later";
935 rc = LDAP_UNWILLING_TO_PERFORM;
939 op->o_pagedresults_state.ps_cookie = 0;
940 op->o_pagedresults_state.ps_count = 0;
944 op->o_pagedresults_size = size;
946 /* NOTE: according to RFC 2696 3.:
948 If the page size is greater than or equal to the sizeLimit value, the
949 server should ignore the control as the request can be satisfied in a
952 * NOTE: this assumes that the op->ors_slimit be set
953 * before the controls are parsed.
955 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
956 op->o_pagedresults = SLAP_IGNORED_CONTROL;
958 } else if ( ctrl->ldctl_iscritical ) {
959 op->o_pagedresults = SLAP_CRITICAL_CONTROL;
962 op->o_pagedresults = SLAP_NONCRITICAL_CONTROL;
966 (void)ber_free( ber, 1 );
970 static int parseAssert (
976 struct berval fstr = BER_BVNULL;
977 const char *err_msg = "";
979 if ( op->o_assert != SLAP_NO_CONTROL ) {
980 rs->sr_text = "assert control specified multiple times";
981 return LDAP_PROTOCOL_ERROR;
984 if ( ctrl->ldctl_value.bv_len == 0 ) {
985 rs->sr_text = "assert control value is empty (or absent)";
986 return LDAP_PROTOCOL_ERROR;
989 ber = ber_init( &(ctrl->ldctl_value) );
991 rs->sr_text = "assert control: internal error";
995 rs->sr_err = get_filter( op, ber, &(op->o_assertion), &rs->sr_text);
997 if( rs->sr_err != LDAP_SUCCESS ) {
998 if( rs->sr_err == SLAPD_DISCONNECT ) {
999 rs->sr_err = LDAP_PROTOCOL_ERROR;
1000 send_ldap_disconnect( op, rs );
1001 rs->sr_err = SLAPD_DISCONNECT;
1003 send_ldap_result( op, rs );
1005 if( op->o_assertion != NULL ) {
1006 filter_free_x( op, op->o_assertion );
1012 filter2bv_x( op, op->o_assertion, &fstr );
1015 LDAP_LOG( OPERATION, ARGS,
1016 "parseAssert: conn %ld assert: %s\n",
1017 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1019 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1020 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1022 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1025 op->o_assert = ctrl->ldctl_iscritical
1026 ? SLAP_CRITICAL_CONTROL
1027 : SLAP_NONCRITICAL_CONTROL;
1029 rs->sr_err = LDAP_SUCCESS;
1030 return LDAP_SUCCESS;
1033 static int parsePreRead (
1038 ber_len_t siz, off, i;
1039 AttributeName *an = NULL;
1042 if ( op->o_preread != SLAP_NO_CONTROL ) {
1043 rs->sr_text = "preread control specified multiple times";
1044 return LDAP_PROTOCOL_ERROR;
1047 if ( ctrl->ldctl_value.bv_len == 0 ) {
1048 rs->sr_text = "preread control value is empty (or absent)";
1049 return LDAP_PROTOCOL_ERROR;
1052 ber = ber_init( &(ctrl->ldctl_value) );
1054 rs->sr_text = "preread control: internal error";
1058 siz = sizeof( AttributeName );
1059 off = offsetof( AttributeName, an_name );
1060 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1061 rs->sr_text = "preread control: decoding error";
1062 return LDAP_PROTOCOL_ERROR;
1065 for( i=0; i<siz; i++ ) {
1066 int rc = LDAP_SUCCESS;
1067 const char *dummy = NULL;
1069 an[i].an_desc = NULL;
1071 an[i].an_oc_exclude = 0;
1072 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1073 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1074 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1079 op->o_preread = ctrl->ldctl_iscritical
1080 ? SLAP_CRITICAL_CONTROL
1081 : SLAP_NONCRITICAL_CONTROL;
1083 op->o_preread_attrs = an;
1085 rs->sr_err = LDAP_SUCCESS;
1086 return LDAP_SUCCESS;
1089 static int parsePostRead (
1094 ber_len_t siz, off, i;
1095 AttributeName *an = NULL;
1098 if ( op->o_postread != SLAP_NO_CONTROL ) {
1099 rs->sr_text = "postread control specified multiple times";
1100 return LDAP_PROTOCOL_ERROR;
1103 if ( ctrl->ldctl_value.bv_len == 0 ) {
1104 rs->sr_text = "postread control value is empty (or absent)";
1105 return LDAP_PROTOCOL_ERROR;
1108 ber = ber_init( &(ctrl->ldctl_value) );
1110 rs->sr_text = "postread control: internal error";
1114 siz = sizeof( AttributeName );
1115 off = offsetof( AttributeName, an_name );
1116 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1117 rs->sr_text = "postread control: decoding error";
1118 return LDAP_PROTOCOL_ERROR;
1121 for( i=0; i<siz; i++ ) {
1122 int rc = LDAP_SUCCESS;
1123 const char *dummy = NULL;
1125 an[i].an_desc = NULL;
1127 an[i].an_oc_exclude = 0;
1128 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1129 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1130 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1135 op->o_postread = ctrl->ldctl_iscritical
1136 ? SLAP_CRITICAL_CONTROL
1137 : SLAP_NONCRITICAL_CONTROL;
1139 op->o_postread_attrs = an;
1141 rs->sr_err = LDAP_SUCCESS;
1142 return LDAP_SUCCESS;
1145 int parseValuesReturnFilter (
1151 struct berval fstr = BER_BVNULL;
1152 const char *err_msg = "";
1154 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
1155 rs->sr_text = "valuesReturnFilter control specified multiple times";
1156 return LDAP_PROTOCOL_ERROR;
1159 if ( ctrl->ldctl_value.bv_len == 0 ) {
1160 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1161 return LDAP_PROTOCOL_ERROR;
1164 ber = ber_init( &(ctrl->ldctl_value) );
1166 rs->sr_text = "internal error";
1170 rs->sr_err = get_vrFilter( op, ber, &(op->o_vrFilter), &rs->sr_text);
1172 if( rs->sr_err != LDAP_SUCCESS ) {
1173 if( rs->sr_err == SLAPD_DISCONNECT ) {
1174 rs->sr_err = LDAP_PROTOCOL_ERROR;
1175 send_ldap_disconnect( op, rs );
1176 rs->sr_err = SLAPD_DISCONNECT;
1178 send_ldap_result( op, rs );
1180 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1184 vrFilter2bv( op, op->o_vrFilter, &fstr );
1188 LDAP_LOG( OPERATION, ARGS,
1189 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
1190 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1192 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1193 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1195 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1198 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1199 ? SLAP_CRITICAL_CONTROL
1200 : SLAP_NONCRITICAL_CONTROL;
1202 rs->sr_err = LDAP_SUCCESS;
1203 return LDAP_SUCCESS;
1206 #ifdef LDAP_CONTROL_SUBENTRIES
1207 static int parseSubentries (
1212 if ( op->o_subentries != SLAP_NO_CONTROL ) {
1213 rs->sr_text = "subentries control specified multiple times";
1214 return LDAP_PROTOCOL_ERROR;
1217 /* FIXME: should use BER library */
1218 if( ( ctrl->ldctl_value.bv_len != 3 )
1219 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1220 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1222 rs->sr_text = "subentries control value encoding is bogus";
1223 return LDAP_PROTOCOL_ERROR;
1226 op->o_subentries = ctrl->ldctl_iscritical
1227 ? SLAP_CRITICAL_CONTROL
1228 : SLAP_NONCRITICAL_CONTROL;
1230 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
1232 return LDAP_SUCCESS;
1236 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1237 static int parsePermissiveModify (
1242 if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
1243 rs->sr_text = "permissiveModify control specified multiple times";
1244 return LDAP_PROTOCOL_ERROR;
1247 if ( ctrl->ldctl_value.bv_len ) {
1248 rs->sr_text = "permissiveModify control value not empty";
1249 return LDAP_PROTOCOL_ERROR;
1252 op->o_permissive_modify = ctrl->ldctl_iscritical
1253 ? SLAP_CRITICAL_CONTROL
1254 : SLAP_NONCRITICAL_CONTROL;
1256 return LDAP_SUCCESS;
1260 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1261 static int parseDomainScope (
1266 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1267 rs->sr_text = "domainScope control specified multiple times";
1268 return LDAP_PROTOCOL_ERROR;
1271 if ( ctrl->ldctl_value.bv_len ) {
1272 rs->sr_text = "domainScope control value not empty";
1273 return LDAP_PROTOCOL_ERROR;
1276 op->o_domain_scope = ctrl->ldctl_iscritical
1277 ? SLAP_CRITICAL_CONTROL
1278 : SLAP_NONCRITICAL_CONTROL;
1280 return LDAP_SUCCESS;
1284 #ifdef LDAP_CONTROL_X_TREE_DELETE
1285 static int parseTreeDelete (
1290 if ( op->o_tree_delete != SLAP_NO_CONTROL ) {
1291 rs->sr_text = "treeDelete control specified multiple times";
1292 return LDAP_PROTOCOL_ERROR;
1295 if ( ctrl->ldctl_value.bv_len ) {
1296 rs->sr_text = "treeDelete control value not empty";
1297 return LDAP_PROTOCOL_ERROR;
1300 op->o_tree_delete = ctrl->ldctl_iscritical
1301 ? SLAP_CRITICAL_CONTROL
1302 : SLAP_NONCRITICAL_CONTROL;
1304 return LDAP_SUCCESS;
1308 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1309 static int parseSearchOptions (
1315 ber_int_t search_flags;
1317 if ( ctrl->ldctl_value.bv_len == 0 ) {
1318 rs->sr_text = "searchOptions control value not empty";
1319 return LDAP_PROTOCOL_ERROR;
1322 ber = ber_init( &ctrl->ldctl_value );
1324 rs->sr_text = "internal error";
1328 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1329 rs->sr_text = "searchOptions control decoding error";
1330 return LDAP_PROTOCOL_ERROR;
1333 (void) ber_free( ber, 1 );
1335 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1336 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1337 rs->sr_text = "searchOptions control specified multiple times "
1338 "or with domainScope control";
1339 return LDAP_PROTOCOL_ERROR;
1342 op->o_domain_scope = ctrl->ldctl_iscritical
1343 ? SLAP_CRITICAL_CONTROL
1344 : SLAP_NONCRITICAL_CONTROL;
1347 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1348 /* Other search flags not recognised so far */
1349 rs->sr_text = "searchOptions contained unrecongized flag";
1350 return LDAP_UNWILLING_TO_PERFORM;
1353 return LDAP_SUCCESS;
1357 static int parseLDAPsync (
1366 struct slap_session_entry *se;
1368 if ( op->o_sync != SLAP_NO_CONTROL ) {
1369 rs->sr_text = "Sync control specified multiple times";
1370 return LDAP_PROTOCOL_ERROR;
1373 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
1374 rs->sr_text = "Sync control specified with pagedResults control";
1375 return LDAP_PROTOCOL_ERROR;
1379 if ( ctrl->ldctl_value.bv_len == 0 ) {
1380 rs->sr_text = "Sync control value is empty (or absent)";
1381 return LDAP_PROTOCOL_ERROR;
1384 /* Parse the control value
1385 * syncRequestValue ::= SEQUENCE {
1390 * refreshAndPersist (3)
1392 * cookie syncCookie OPTIONAL
1396 ber = ber_init( &ctrl->ldctl_value );
1398 rs->sr_text = "internal error";
1402 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1403 rs->sr_text = "Sync control : mode decoding error";
1404 return LDAP_PROTOCOL_ERROR;
1408 case LDAP_SYNC_REFRESH_ONLY:
1409 mode = SLAP_SYNC_REFRESH;
1411 case LDAP_SYNC_REFRESH_AND_PERSIST:
1412 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1415 rs->sr_text = "Sync control : unknown update mode";
1416 return LDAP_PROTOCOL_ERROR;
1419 tag = ber_peek_tag( ber, &len );
1421 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1422 struct berval tmp_bv;
1423 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1424 rs->sr_text = "Sync control : cookie decoding error";
1425 return LDAP_PROTOCOL_ERROR;
1427 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1428 slap_parse_sync_cookie( &op->o_sync_state );
1430 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1431 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1432 rs->sr_text = "Sync control : rhint decoding error";
1433 return LDAP_PROTOCOL_ERROR;
1436 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1437 rs->sr_text = "Sync control : decoding error";
1438 return LDAP_PROTOCOL_ERROR;
1441 (void) ber_free( ber, 1 );
1443 op->o_sync_mode = (char) mode;
1445 op->o_sync = ctrl->ldctl_iscritical
1446 ? SLAP_CRITICAL_CONTROL
1447 : SLAP_NONCRITICAL_CONTROL;
1449 return LDAP_SUCCESS;