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 ) {
158 return LDAP_PARAM_ERROR;
161 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
163 return LDAP_NO_MEMORY;
165 sc->sc_oid = ch_strdup( controloid );
166 sc->sc_mask = controlmask;
167 if ( controlexops != NULL ) {
168 sc->sc_extendedops = ldap_charray_dup( controlexops );
169 if ( sc->sc_extendedops == NULL ) {
171 return LDAP_NO_MEMORY;
174 sc->sc_extendedops = NULL;
176 sc->sc_parse = controlparsefn;
178 /* Update slap_known_controls, too. */
179 if ( slap_known_controls == NULL ) {
180 slap_known_controls = (char **)SLAP_MALLOC( 2 * sizeof(char *) );
181 if ( slap_known_controls == NULL ) {
182 if ( sc->sc_extendedops != NULL ) ldap_charray_free( sc->sc_extendedops );
184 return LDAP_NO_MEMORY;
186 slap_known_controls[0] = ch_strdup( sc->sc_oid );
187 slap_known_controls[1] = NULL;
189 for ( i = 0; slap_known_controls[i] != NULL; i++ )
191 slap_known_controls = (char **)SLAP_REALLOC( slap_known_controls, (i + 2) * sizeof(char *) );
192 if ( slap_known_controls == NULL ) {
193 if ( sc->sc_extendedops != NULL ) ldap_charray_free( sc->sc_extendedops );
195 return LDAP_NO_MEMORY;
197 slap_known_controls[i++] = ch_strdup( sc->sc_oid );
198 slap_known_controls[i] = NULL;
201 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
202 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
208 * One-time initialization of internal controls.
211 slap_controls_init( void )
214 struct slap_control *sc;
218 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
219 rc = register_supported_control( control_defs[i].sc_oid,
220 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
221 control_defs[i].sc_parse );
222 if ( rc != LDAP_SUCCESS )
230 * Free memory associated with list of supported controls.
233 controls_destroy( void )
235 struct slap_control *sc;
237 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
238 sc = LDAP_SLIST_FIRST(&controls_list);
239 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
241 ch_free( sc->sc_oid );
242 if ( sc->sc_extendedops != NULL ) {
243 ldap_charray_free( sc->sc_extendedops );
247 ldap_charray_free( slap_known_controls );
251 * Format the supportedControl attribute of the root DSE,
252 * detailing which controls are supported by the directory
256 controls_root_dse_info( Entry *e )
258 AttributeDescription *ad_supportedControl
259 = slap_schema.si_ad_supportedControl;
260 struct berval vals[2];
261 struct slap_control *sc;
263 vals[1].bv_val = NULL;
266 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
267 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
269 vals[0].bv_val = sc->sc_oid;
270 vals[0].bv_len = strlen( sc->sc_oid );
272 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
281 * Return a list of OIDs and operation masks for supported
282 * controls. Used by SLAPI.
285 get_supported_controls(char ***ctrloidsp,
286 slap_mask_t **ctrlmasks)
292 struct slap_control *sc;
296 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
306 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
307 if ( oids == NULL ) {
308 return LDAP_NO_MEMORY;
310 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
311 if ( masks == NULL ) {
313 return LDAP_NO_MEMORY;
318 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
319 oids[n] = ch_strdup( sc->sc_oid );
320 masks[n] = sc->sc_mask;
333 * Find a control given its OID.
335 static struct slap_control *
336 find_ctrl( const char *oid )
338 struct slap_control *sc;
340 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
341 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
349 void slap_free_ctrls(
351 LDAPControl **ctrls )
355 for (i=0; ctrls[i]; i++) {
356 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
358 op->o_tmpfree( ctrls, op->o_tmpmemctx );
370 BerElement *ber = op->o_ber;
371 struct slap_control *sc;
374 len = ber_pvt_ber_remaining(ber);
378 rs->sr_err = LDAP_SUCCESS;
382 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
383 if( tag == LBER_ERROR ) {
384 rs->sr_err = SLAPD_DISCONNECT;
385 rs->sr_text = "unexpected data in PDU";
392 LDAP_LOG( OPERATION, ENTRY,
393 "get_ctrls: conn %lu\n", op->o_connid, 0, 0 );
395 Debug( LDAP_DEBUG_TRACE,
396 "=> get_ctrls\n", 0, 0, 0 );
399 if( op->o_protocol < LDAP_VERSION3 ) {
400 rs->sr_err = SLAPD_DISCONNECT;
401 rs->sr_text = "controls require LDAPv3";
405 /* one for first control, one for termination */
406 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
409 if( op->ctrls == NULL ) {
410 rs->sr_err = LDAP_NO_MEMORY;
411 rs->sr_text = "no memory";
416 op->o_ctrls[nctrls] = NULL;
418 /* step through each element */
419 for( tag = ber_first_element( ber, &len, &opaque );
421 tag = ber_next_element( ber, &len, opaque ) )
424 LDAPControl **tctrls;
426 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
427 memset(c, 0, sizeof(LDAPControl));
429 /* allocate pointer space for current controls (nctrls)
430 * + this control + extra NULL
432 tctrls = op->o_tmprealloc( op->o_ctrls,
433 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
436 if( tctrls == NULL ) {
438 ldap_controls_free(op->o_ctrls);
441 rs->sr_err = LDAP_NO_MEMORY;
442 rs->sr_text = "no memory";
446 op->o_ctrls = tctrls;
448 op->o_ctrls[nctrls++] = c;
449 op->o_ctrls[nctrls] = NULL;
451 tag = ber_scanf( ber, "{m" /*}*/, &bv );
452 c->ldctl_oid = bv.bv_val;
454 if( tag == LBER_ERROR ) {
456 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
457 op->o_connid, 0, 0 );
459 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
463 slap_free_ctrls( op, op->o_ctrls );
465 rs->sr_err = SLAPD_DISCONNECT;
466 rs->sr_text = "decoding controls error";
469 } else if( c->ldctl_oid == NULL ) {
471 LDAP_LOG( OPERATION, INFO,
472 "get_ctrls: conn %lu got emtpy OID.\n",
473 op->o_connid, 0, 0 );
475 Debug( LDAP_DEBUG_TRACE,
476 "get_ctrls: conn %lu got emtpy OID.\n",
477 op->o_connid, 0, 0 );
480 slap_free_ctrls( op, op->o_ctrls );
482 rs->sr_err = LDAP_PROTOCOL_ERROR;
483 rs->sr_text = "OID field is empty";
487 tag = ber_peek_tag( ber, &len );
489 if( tag == LBER_BOOLEAN ) {
491 tag = ber_scanf( ber, "b", &crit );
493 if( tag == LBER_ERROR ) {
495 LDAP_LOG( OPERATION, INFO,
496 "get_ctrls: conn %lu get crit failed.\n",
497 op->o_connid, 0, 0 );
499 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
502 slap_free_ctrls( op, op->o_ctrls );
504 rs->sr_err = SLAPD_DISCONNECT;
505 rs->sr_text = "decoding controls error";
509 c->ldctl_iscritical = (crit != 0);
510 tag = ber_peek_tag( ber, &len );
513 if( tag == LBER_OCTETSTRING ) {
514 tag = ber_scanf( ber, "m", &c->ldctl_value );
516 if( tag == LBER_ERROR ) {
518 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
519 "%s (%scritical): get value failed.\n",
520 op->o_connid, c->ldctl_oid,
521 c->ldctl_iscritical ? "" : "non" );
523 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
524 "%s (%scritical): get value failed.\n",
525 op->o_connid, c->ldctl_oid,
526 c->ldctl_iscritical ? "" : "non" );
528 slap_free_ctrls( op, op->o_ctrls );
530 rs->sr_err = SLAPD_DISCONNECT;
531 rs->sr_text = "decoding controls error";
537 LDAP_LOG( OPERATION, INFO,
538 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
539 op->o_connid, c->ldctl_oid, c->ldctl_iscritical ? "" : "non" );
541 Debug( LDAP_DEBUG_TRACE,
542 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
543 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
546 sc = find_ctrl( c->ldctl_oid );
548 /* recognized control */
550 switch( op->o_tag ) {
552 tagmask = SLAP_CTRL_ADD;
555 tagmask = SLAP_CTRL_BIND;
557 case LDAP_REQ_COMPARE:
558 tagmask = SLAP_CTRL_COMPARE;
560 case LDAP_REQ_DELETE:
561 tagmask = SLAP_CTRL_DELETE;
563 case LDAP_REQ_MODIFY:
564 tagmask = SLAP_CTRL_MODIFY;
566 case LDAP_REQ_RENAME:
567 tagmask = SLAP_CTRL_RENAME;
569 case LDAP_REQ_SEARCH:
570 tagmask = SLAP_CTRL_SEARCH;
572 case LDAP_REQ_UNBIND:
573 tagmask = SLAP_CTRL_UNBIND;
575 case LDAP_REQ_ABANDON:
576 tagmask = SLAP_CTRL_ABANDON;
578 case LDAP_REQ_EXTENDED:
580 assert( op->ore_reqoid.bv_val != NULL );
581 if( sc->sc_extendedops != NULL ) {
583 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
584 if( strcmp( op->ore_reqoid.bv_val,
585 sc->sc_extendedops[i] ) == 0 )
594 rs->sr_err = LDAP_OTHER;
595 rs->sr_text = "controls internal error";
599 if (( sc->sc_mask & tagmask ) == tagmask ) {
600 /* available extension */
603 if( !sc->sc_parse ) {
604 rs->sr_err = LDAP_OTHER;
605 rs->sr_text = "not yet implemented";
609 rc = sc->sc_parse( op, rs, c );
610 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
612 /* for some reason, the control should be ignored */
613 case SLAP_CTRL_IGNORE:
614 op->o_tmpfree( c, op->o_tmpmemctx );
615 op->o_ctrls[--nctrls] = NULL;
618 /* the control was successfully parsed */
622 /* something happened... */
628 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
629 /* kludge to disable backend_control() check */
630 c->ldctl_iscritical = 0;
632 } else if ( tagmask == SLAP_CTRL_SEARCH &&
633 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
635 /* kludge to disable backend_control() check */
636 c->ldctl_iscritical = 0;
639 } else if( c->ldctl_iscritical ) {
640 /* unavailable CRITICAL control */
641 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
642 rs->sr_text = "critical extension is unavailable";
646 } else if( c->ldctl_iscritical ) {
647 /* unrecognized CRITICAL control */
648 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
649 rs->sr_text = "critical extension is not recognized";
657 LDAP_LOG( OPERATION, RESULTS,
658 "get_ctrls: n=%d rc=%d err=\"%s\"\n",
659 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
661 Debug( LDAP_DEBUG_TRACE,
662 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
663 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
666 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
667 if( rs->sr_err == SLAPD_DISCONNECT ) {
668 rs->sr_err = LDAP_PROTOCOL_ERROR;
669 send_ldap_disconnect( op, rs );
670 rs->sr_err = SLAPD_DISCONNECT;
672 send_ldap_result( op, rs );
679 static int parseModifyIncrement (
685 if ( op->o_modifyIncrement != SLAP_NO_CONTROL ) {
686 rs->sr_text = "modifyIncrement control specified multiple times";
687 return LDAP_PROTOCOL_ERROR;
691 if ( ctrl->ldctl_value.bv_len ) {
692 rs->sr_text = "modifyIncrement control value not empty";
693 return LDAP_PROTOCOL_ERROR;
697 op->o_modifyIncrement = ctrl->ldctl_iscritical
698 ? SLAP_CRITICAL_CONTROL
699 : SLAP_NONCRITICAL_CONTROL;
705 static int parseManageDSAit (
710 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
711 rs->sr_text = "manageDSAit control specified multiple times";
712 return LDAP_PROTOCOL_ERROR;
715 if ( ctrl->ldctl_value.bv_len ) {
716 rs->sr_text = "manageDSAit control value not empty";
717 return LDAP_PROTOCOL_ERROR;
720 op->o_managedsait = ctrl->ldctl_iscritical
721 ? SLAP_CRITICAL_CONTROL
722 : SLAP_NONCRITICAL_CONTROL;
727 static int parseProxyAuthz (
733 struct berval dn = BER_BVNULL;
735 if ( op->o_proxy_authz != SLAP_NO_CONTROL ) {
736 rs->sr_text = "proxy authorization control specified multiple times";
737 return LDAP_PROTOCOL_ERROR;
740 op->o_proxy_authz = ctrl->ldctl_iscritical
741 ? SLAP_CRITICAL_CONTROL
742 : SLAP_NONCRITICAL_CONTROL;
745 LDAP_LOG( OPERATION, ARGS,
746 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
748 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
751 Debug( LDAP_DEBUG_ARGS,
752 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
754 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
758 if( ctrl->ldctl_value.bv_len == 0 ) {
760 LDAP_LOG( OPERATION, RESULTS,
761 "parseProxyAuthz: conn=%lu anonymous\n",
762 op->o_connid, 0, 0 );
764 Debug( LDAP_DEBUG_TRACE,
765 "parseProxyAuthz: conn=%lu anonymous\n",
766 op->o_connid, 0, 0 );
770 free( op->o_dn.bv_val );
772 op->o_dn.bv_val = ch_strdup( "" );
774 free( op->o_ndn.bv_val );
775 op->o_ndn.bv_len = 0;
776 op->o_ndn.bv_val = ch_strdup( "" );
781 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
782 NULL, &dn, SLAP_GETDN_AUTHZID );
784 /* FIXME: empty DN in proxyAuthz control should be legal... */
785 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
787 ch_free( dn.bv_val );
789 rs->sr_text = "authzId mapping failed";
790 return LDAP_PROXY_AUTHZ_FAILURE;
794 LDAP_LOG( OPERATION, RESULTS,
795 "parseProxyAuthz: conn=%lu \"%s\"\n",
797 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
799 Debug( LDAP_DEBUG_TRACE,
800 "parseProxyAuthz: conn=%lu \"%s\"\n",
802 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
805 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
808 ch_free( dn.bv_val );
809 rs->sr_text = "not authorized to assume identity";
810 return LDAP_PROXY_AUTHZ_FAILURE;
813 ch_free( op->o_dn.bv_val );
814 ch_free( op->o_ndn.bv_val );
816 op->o_dn.bv_val = NULL;
819 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu PROXYAUTHZ dn=\"%s\"\n",
820 op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
823 * NOTE: since slap_sasl_getdn() returns a normalized dn,
824 * from now on op->o_dn is normalized
826 ber_dupbv( &op->o_dn, &dn );
831 static int parseNoOp (
836 if ( op->o_noop != SLAP_NO_CONTROL ) {
837 rs->sr_text = "noop control specified multiple times";
838 return LDAP_PROTOCOL_ERROR;
841 if ( ctrl->ldctl_value.bv_len ) {
842 rs->sr_text = "noop control value not empty";
843 return LDAP_PROTOCOL_ERROR;
846 op->o_noop = ctrl->ldctl_iscritical
847 ? SLAP_CRITICAL_CONTROL
848 : SLAP_NONCRITICAL_CONTROL;
853 static int parsePagedResults (
861 struct berval cookie = BER_BVNULL;
863 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
864 rs->sr_text = "paged results control specified multiple times";
865 return LDAP_PROTOCOL_ERROR;
868 if ( op->o_sync != SLAP_NO_CONTROL ) {
869 rs->sr_text = "paged results control specified with sync control";
870 return LDAP_PROTOCOL_ERROR;
873 if ( ctrl->ldctl_value.bv_len == 0 ) {
874 rs->sr_text = "paged results control value is empty (or absent)";
875 return LDAP_PROTOCOL_ERROR;
878 /* Parse the control value
879 * realSearchControlValue ::= SEQUENCE {
880 * size INTEGER (0..maxInt),
881 * -- requested page size from client
882 * -- result set size estimate from server
883 * cookie OCTET STRING
886 ber = ber_init( &ctrl->ldctl_value );
888 rs->sr_text = "internal error";
892 tag = ber_scanf( ber, "{im}", &size, &cookie );
893 (void) ber_free( ber, 1 );
895 if( tag == LBER_ERROR ) {
896 rs->sr_text = "paged results control could not be decoded";
897 return LDAP_PROTOCOL_ERROR;
901 rs->sr_text = "paged results control size invalid";
902 return LDAP_PROTOCOL_ERROR;
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 before the controls
915 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
916 return SLAP_CTRL_IGNORE;
919 if( cookie.bv_len ) {
920 PagedResultsCookie reqcookie;
921 if( cookie.bv_len != sizeof( reqcookie ) ) {
923 rs->sr_text = "paged results cookie is invalid";
924 return LDAP_PROTOCOL_ERROR;
927 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
929 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
931 rs->sr_text = "paged results cookie is invalid";
932 return LDAP_PROTOCOL_ERROR;
934 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
935 rs->sr_text = "paged results cookie is invalid or old";
936 return LDAP_UNWILLING_TO_PERFORM;
940 /* Initial request. Initialize state. */
941 op->o_pagedresults_state.ps_cookie = 0;
942 op->o_pagedresults_state.ps_count = 0;
945 op->o_pagedresults_size = size;
947 op->o_pagedresults = ctrl->ldctl_iscritical
948 ? SLAP_CRITICAL_CONTROL
949 : SLAP_NONCRITICAL_CONTROL;
954 static int parseAssert (
960 struct berval fstr = BER_BVNULL;
961 const char *err_msg = "";
963 if ( op->o_assert != SLAP_NO_CONTROL ) {
964 rs->sr_text = "assert control specified multiple times";
965 return LDAP_PROTOCOL_ERROR;
968 if ( ctrl->ldctl_value.bv_len == 0 ) {
969 rs->sr_text = "assert control value is empty (or absent)";
970 return LDAP_PROTOCOL_ERROR;
973 ber = ber_init( &(ctrl->ldctl_value) );
975 rs->sr_text = "assert control: internal error";
979 rs->sr_err = get_filter( op, ber, &(op->o_assertion), &rs->sr_text);
981 if( rs->sr_err != LDAP_SUCCESS ) {
982 if( rs->sr_err == SLAPD_DISCONNECT ) {
983 rs->sr_err = LDAP_PROTOCOL_ERROR;
984 send_ldap_disconnect( op, rs );
985 rs->sr_err = SLAPD_DISCONNECT;
987 send_ldap_result( op, rs );
989 if( op->o_assertion != NULL ) {
990 filter_free_x( op, op->o_assertion );
996 filter2bv_x( op, op->o_assertion, &fstr );
999 LDAP_LOG( OPERATION, ARGS,
1000 "parseAssert: conn %ld assert: %s\n",
1001 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1003 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1004 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1006 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1009 op->o_assert = ctrl->ldctl_iscritical
1010 ? SLAP_CRITICAL_CONTROL
1011 : SLAP_NONCRITICAL_CONTROL;
1013 rs->sr_err = LDAP_SUCCESS;
1014 return LDAP_SUCCESS;
1017 static int parsePreRead (
1022 ber_len_t siz, off, i;
1023 AttributeName *an = NULL;
1026 if ( op->o_preread != SLAP_NO_CONTROL ) {
1027 rs->sr_text = "preread control specified multiple times";
1028 return LDAP_PROTOCOL_ERROR;
1031 if ( ctrl->ldctl_value.bv_len == 0 ) {
1032 rs->sr_text = "preread control value is empty (or absent)";
1033 return LDAP_PROTOCOL_ERROR;
1036 ber = ber_init( &(ctrl->ldctl_value) );
1038 rs->sr_text = "preread control: internal error";
1042 siz = sizeof( AttributeName );
1043 off = offsetof( AttributeName, an_name );
1044 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1045 rs->sr_text = "preread control: decoding error";
1046 return LDAP_PROTOCOL_ERROR;
1049 for( i=0; i<siz; i++ ) {
1050 int rc = LDAP_SUCCESS;
1051 const char *dummy = NULL;
1053 an[i].an_desc = NULL;
1055 an[i].an_oc_exclude = 0;
1056 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1057 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1058 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1063 op->o_preread = ctrl->ldctl_iscritical
1064 ? SLAP_CRITICAL_CONTROL
1065 : SLAP_NONCRITICAL_CONTROL;
1067 op->o_preread_attrs = an;
1069 rs->sr_err = LDAP_SUCCESS;
1070 return LDAP_SUCCESS;
1073 static int parsePostRead (
1078 ber_len_t siz, off, i;
1079 AttributeName *an = NULL;
1082 if ( op->o_postread != SLAP_NO_CONTROL ) {
1083 rs->sr_text = "postread control specified multiple times";
1084 return LDAP_PROTOCOL_ERROR;
1087 if ( ctrl->ldctl_value.bv_len == 0 ) {
1088 rs->sr_text = "postread control value is empty (or absent)";
1089 return LDAP_PROTOCOL_ERROR;
1092 ber = ber_init( &(ctrl->ldctl_value) );
1094 rs->sr_text = "postread control: internal error";
1098 siz = sizeof( AttributeName );
1099 off = offsetof( AttributeName, an_name );
1100 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1101 rs->sr_text = "postread control: decoding error";
1102 return LDAP_PROTOCOL_ERROR;
1105 for( i=0; i<siz; i++ ) {
1106 int rc = LDAP_SUCCESS;
1107 const char *dummy = NULL;
1109 an[i].an_desc = NULL;
1111 an[i].an_oc_exclude = 0;
1112 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1113 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1114 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1119 op->o_postread = ctrl->ldctl_iscritical
1120 ? SLAP_CRITICAL_CONTROL
1121 : SLAP_NONCRITICAL_CONTROL;
1123 op->o_postread_attrs = an;
1125 rs->sr_err = LDAP_SUCCESS;
1126 return LDAP_SUCCESS;
1129 int parseValuesReturnFilter (
1135 struct berval fstr = BER_BVNULL;
1136 const char *err_msg = "";
1138 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
1139 rs->sr_text = "valuesReturnFilter control specified multiple times";
1140 return LDAP_PROTOCOL_ERROR;
1143 if ( ctrl->ldctl_value.bv_len == 0 ) {
1144 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1145 return LDAP_PROTOCOL_ERROR;
1148 ber = ber_init( &(ctrl->ldctl_value) );
1150 rs->sr_text = "internal error";
1154 rs->sr_err = get_vrFilter( op, ber, &(op->o_vrFilter), &rs->sr_text);
1156 if( rs->sr_err != LDAP_SUCCESS ) {
1157 if( rs->sr_err == SLAPD_DISCONNECT ) {
1158 rs->sr_err = LDAP_PROTOCOL_ERROR;
1159 send_ldap_disconnect( op, rs );
1160 rs->sr_err = SLAPD_DISCONNECT;
1162 send_ldap_result( op, rs );
1164 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1168 vrFilter2bv( op, op->o_vrFilter, &fstr );
1172 LDAP_LOG( OPERATION, ARGS,
1173 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
1174 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1176 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1177 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1179 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1182 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1183 ? SLAP_CRITICAL_CONTROL
1184 : SLAP_NONCRITICAL_CONTROL;
1186 rs->sr_err = LDAP_SUCCESS;
1187 return LDAP_SUCCESS;
1190 #ifdef LDAP_CONTROL_SUBENTRIES
1191 static int parseSubentries (
1196 if ( op->o_subentries != SLAP_NO_CONTROL ) {
1197 rs->sr_text = "subentries control specified multiple times";
1198 return LDAP_PROTOCOL_ERROR;
1201 /* FIXME: should use BER library */
1202 if( ( ctrl->ldctl_value.bv_len != 3 )
1203 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1204 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1206 rs->sr_text = "subentries control value encoding is bogus";
1207 return LDAP_PROTOCOL_ERROR;
1210 op->o_subentries = ctrl->ldctl_iscritical
1211 ? SLAP_CRITICAL_CONTROL
1212 : SLAP_NONCRITICAL_CONTROL;
1214 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
1216 return LDAP_SUCCESS;
1220 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1221 static int parsePermissiveModify (
1226 if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
1227 rs->sr_text = "permissiveModify control specified multiple times";
1228 return LDAP_PROTOCOL_ERROR;
1231 if ( ctrl->ldctl_value.bv_len ) {
1232 rs->sr_text = "permissiveModify control value not empty";
1233 return LDAP_PROTOCOL_ERROR;
1236 op->o_permissive_modify = ctrl->ldctl_iscritical
1237 ? SLAP_CRITICAL_CONTROL
1238 : SLAP_NONCRITICAL_CONTROL;
1240 return LDAP_SUCCESS;
1244 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1245 static int parseDomainScope (
1250 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1251 rs->sr_text = "domainScope control specified multiple times";
1252 return LDAP_PROTOCOL_ERROR;
1255 if ( ctrl->ldctl_value.bv_len ) {
1256 rs->sr_text = "domainScope control value not empty";
1257 return LDAP_PROTOCOL_ERROR;
1260 op->o_domain_scope = ctrl->ldctl_iscritical
1261 ? SLAP_CRITICAL_CONTROL
1262 : SLAP_NONCRITICAL_CONTROL;
1264 return LDAP_SUCCESS;
1268 #ifdef LDAP_CONTROL_X_TREE_DELETE
1269 static int parseTreeDelete (
1274 if ( op->o_tree_delete != SLAP_NO_CONTROL ) {
1275 rs->sr_text = "treeDelete control specified multiple times";
1276 return LDAP_PROTOCOL_ERROR;
1279 if ( ctrl->ldctl_value.bv_len ) {
1280 rs->sr_text = "treeDelete control value not empty";
1281 return LDAP_PROTOCOL_ERROR;
1284 op->o_tree_delete = ctrl->ldctl_iscritical
1285 ? SLAP_CRITICAL_CONTROL
1286 : SLAP_NONCRITICAL_CONTROL;
1288 return LDAP_SUCCESS;
1292 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1293 static int parseSearchOptions (
1299 ber_int_t search_flags;
1301 if ( ctrl->ldctl_value.bv_len == 0 ) {
1302 rs->sr_text = "searchOptions control value not empty";
1303 return LDAP_PROTOCOL_ERROR;
1306 ber = ber_init( &ctrl->ldctl_value );
1308 rs->sr_text = "internal error";
1312 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1313 rs->sr_text = "searchOptions control decoding error";
1314 return LDAP_PROTOCOL_ERROR;
1317 (void) ber_free( ber, 1 );
1319 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1320 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1321 rs->sr_text = "searchOptions control specified multiple times "
1322 "or with domainScope control";
1323 return LDAP_PROTOCOL_ERROR;
1326 op->o_domain_scope = ctrl->ldctl_iscritical
1327 ? SLAP_CRITICAL_CONTROL
1328 : SLAP_NONCRITICAL_CONTROL;
1331 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1332 /* Other search flags not recognised so far */
1333 rs->sr_text = "searchOptions contained unrecongized flag";
1334 return LDAP_UNWILLING_TO_PERFORM;
1337 return LDAP_SUCCESS;
1341 static int parseLDAPsync (
1350 struct slap_session_entry *se;
1352 if ( op->o_sync != SLAP_NO_CONTROL ) {
1353 rs->sr_text = "Sync control specified multiple times";
1354 return LDAP_PROTOCOL_ERROR;
1357 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
1358 rs->sr_text = "Sync control specified with pagedResults control";
1359 return LDAP_PROTOCOL_ERROR;
1363 if ( ctrl->ldctl_value.bv_len == 0 ) {
1364 rs->sr_text = "Sync control value is empty (or absent)";
1365 return LDAP_PROTOCOL_ERROR;
1368 /* Parse the control value
1369 * syncRequestValue ::= SEQUENCE {
1374 * refreshAndPersist (3)
1376 * cookie syncCookie OPTIONAL
1380 ber = ber_init( &ctrl->ldctl_value );
1382 rs->sr_text = "internal error";
1386 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1387 rs->sr_text = "Sync control : mode decoding error";
1388 return LDAP_PROTOCOL_ERROR;
1392 case LDAP_SYNC_REFRESH_ONLY:
1393 mode = SLAP_SYNC_REFRESH;
1395 case LDAP_SYNC_REFRESH_AND_PERSIST:
1396 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1399 rs->sr_text = "Sync control : unknown update mode";
1400 return LDAP_PROTOCOL_ERROR;
1403 tag = ber_peek_tag( ber, &len );
1405 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1406 struct berval tmp_bv;
1407 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1408 rs->sr_text = "Sync control : cookie decoding error";
1409 return LDAP_PROTOCOL_ERROR;
1411 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1412 slap_parse_sync_cookie( &op->o_sync_state );
1414 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1415 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1416 rs->sr_text = "Sync control : rhint decoding error";
1417 return LDAP_PROTOCOL_ERROR;
1420 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1421 rs->sr_text = "Sync control : decoding error";
1422 return LDAP_PROTOCOL_ERROR;
1425 (void) ber_free( ber, 1 );
1427 op->o_sync_mode = (char) mode;
1429 op->o_sync = ctrl->ldctl_iscritical
1430 ? SLAP_CRITICAL_CONTROL
1431 : SLAP_NONCRITICAL_CONTROL;
1433 return LDAP_SUCCESS;