3 * Copyright 1999-2002 The OpenLDAP Foundation.
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
15 #include <ac/string.h>
16 #include <ac/socket.h>
20 #include "../../libraries/liblber/lber-int.h"
22 #define SLAP_CTRL_FRONTEND 0x80000000U
23 #define SLAP_CTRL_FRONTEND_SEARCH 0x01000000U /* for NOOP */
25 #define SLAP_CTRL_OPFLAGS 0x0000FFFFU
26 #define SLAP_CTRL_ABANDON 0x00000001U
27 #define SLAP_CTRL_ADD 0x00002002U
28 #define SLAP_CTRL_BIND 0x00000004U
29 #define SLAP_CTRL_COMPARE 0x00001008U
30 #define SLAP_CTRL_DELETE 0x00002010U
31 #define SLAP_CTRL_MODIFY 0x00002020U
32 #define SLAP_CTRL_RENAME 0x00002040U
33 #define SLAP_CTRL_SEARCH 0x00001080U
34 #define SLAP_CTRL_UNBIND 0x00000100U
36 #define SLAP_CTRL_INTROGATE (SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH)
37 #define SLAP_CTRL_UPDATE \
38 (SLAP_CTRL_ADD|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME)
39 #define SLAP_CTRL_ACCESS (SLAP_CTRL_INTROGATE|SLAP_CTRL_UPDATE)
41 typedef int (SLAP_CTRL_PARSE_FN) LDAP_P((
47 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
48 static SLAP_CTRL_PARSE_FN parseManageDSAit;
49 static SLAP_CTRL_PARSE_FN parseNoOp;
50 static SLAP_CTRL_PARSE_FN parsePagedResults;
51 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
53 #ifdef LDAP_CONTROL_SUBENTRIES
54 static SLAP_CTRL_PARSE_FN parseSubentries;
56 #ifdef LDAP_CLIENT_UPDATE
57 static SLAP_CTRL_PARSE_FN parseClientUpdate;
60 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
62 static char *proxy_authz_extops[] = {
63 LDAP_EXOP_MODIFY_PASSWD,
68 static struct slap_control {
71 char **sc_extendedops;
72 SLAP_CTRL_PARSE_FN *sc_parse;
74 } supportedControls[] = {
75 { LDAP_CONTROL_PROXY_AUTHZ,
76 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
78 { LDAP_CONTROL_MANAGEDSAIT,
79 SLAP_CTRL_ACCESS, NULL,
82 SLAP_CTRL_ACCESS, NULL,
84 { LDAP_CONTROL_PAGEDRESULTS,
85 SLAP_CTRL_SEARCH, NULL,
87 { LDAP_CONTROL_VALUESRETURNFILTER,
88 SLAP_CTRL_SEARCH, NULL,
89 parseValuesReturnFilter },
90 #ifdef LDAP_CONTROL_SUBENTRIES
91 { LDAP_CONTROL_SUBENTRIES,
92 SLAP_CTRL_SEARCH, NULL,
95 #ifdef LDAP_CLIENT_UPDATE
96 { LDAP_CONTROL_CLIENT_UPDATE,
97 SLAP_CTRL_SEARCH, NULL,
99 #endif /* LDAP_CLIENT_UPDATE */
104 get_supported_ctrl(int index)
106 return supportedControls[index].sc_oid;
110 get_supported_ctrl_mask(int index)
112 return supportedControls[index].sc_mask;
115 static struct slap_control *
116 find_ctrl( const char *oid )
119 for( i=0; supportedControls[i].sc_oid; i++ ) {
120 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
121 return &supportedControls[i];
136 BerElement *ber = op->o_ber;
137 struct slap_control *sc;
138 int rc = LDAP_SUCCESS;
139 const char *errmsg = NULL;
141 len = ber_pvt_ber_remaining(ber);
149 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
150 if( tag == LBER_ERROR ) {
151 rc = SLAPD_DISCONNECT;
152 errmsg = "unexpected data in PDU";
159 LDAP_LOG( OPERATION, ENTRY, "get_ctrls: conn %lu\n", conn->c_connid, 0, 0 );
161 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
163 if( op->o_protocol < LDAP_VERSION3 ) {
164 rc = SLAPD_DISCONNECT;
165 errmsg = "controls require LDAPv3";
169 /* one for first control, one for termination */
170 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
173 if( op->ctrls == NULL ) {
175 errmsg = "no memory";
180 op->o_ctrls[nctrls] = NULL;
182 /* step through each element */
183 for( tag = ber_first_element( ber, &len, &opaque );
185 tag = ber_next_element( ber, &len, opaque ) )
188 LDAPControl **tctrls;
190 c = ch_calloc( 1, sizeof(LDAPControl) );
194 ldap_controls_free(op->o_ctrls);
198 errmsg = "no memory";
203 /* allocate pointer space for current controls (nctrls)
204 * + this control + extra NULL
206 tctrls = ch_realloc( op->o_ctrls,
207 (nctrls+2) * sizeof(LDAPControl *));
210 if( tctrls == NULL ) {
212 ldap_controls_free(op->o_ctrls);
216 errmsg = "no memory";
220 op->o_ctrls = tctrls;
222 op->o_ctrls[nctrls++] = c;
223 op->o_ctrls[nctrls] = NULL;
225 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
227 if( tag == LBER_ERROR ) {
229 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
230 conn->c_connid, 0, 0 );
232 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
235 ldap_controls_free( op->o_ctrls );
237 rc = SLAPD_DISCONNECT;
238 errmsg = "decoding controls error";
242 tag = ber_peek_tag( ber, &len );
244 if( tag == LBER_BOOLEAN ) {
246 tag = ber_scanf( ber, "b", &crit );
248 if( tag == LBER_ERROR ) {
250 LDAP_LOG( OPERATION, INFO,
251 "get_ctrls: conn %lu get crit failed.\n",
252 conn->c_connid, 0, 0 );
254 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
257 ldap_controls_free( op->o_ctrls );
259 rc = SLAPD_DISCONNECT;
260 errmsg = "decoding controls error";
264 c->ldctl_iscritical = (crit != 0);
265 tag = ber_peek_tag( ber, &len );
268 if( tag == LBER_OCTETSTRING ) {
269 tag = ber_scanf( ber, "o", &c->ldctl_value );
271 if( tag == LBER_ERROR ) {
273 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
274 "%s (%scritical): get value failed.\n",
275 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
276 c->ldctl_iscritical ? "" : "non" );
278 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
279 "%s (%scritical): get value failed.\n",
281 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
282 c->ldctl_iscritical ? "" : "non" );
284 ldap_controls_free( op->o_ctrls );
286 rc = SLAPD_DISCONNECT;
287 errmsg = "decoding controls error";
293 LDAP_LOG( OPERATION, INFO,
294 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
295 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
296 c->ldctl_iscritical ? "" : "non" );
298 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
299 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
300 c->ldctl_iscritical ? "" : "non",
304 sc = find_ctrl( c->ldctl_oid );
306 /* recognized control */
308 switch( op->o_tag ) {
310 tagmask = SLAP_CTRL_ADD;
313 tagmask = SLAP_CTRL_BIND;
315 case LDAP_REQ_COMPARE:
316 tagmask = SLAP_CTRL_COMPARE;
318 case LDAP_REQ_DELETE:
319 tagmask = SLAP_CTRL_DELETE;
321 case LDAP_REQ_MODIFY:
322 tagmask = SLAP_CTRL_MODIFY;
324 case LDAP_REQ_RENAME:
325 tagmask = SLAP_CTRL_RENAME;
327 case LDAP_REQ_SEARCH:
328 tagmask = SLAP_CTRL_SEARCH;
330 case LDAP_REQ_UNBIND:
331 tagmask = SLAP_CTRL_UNBIND;
333 case LDAP_REQ_ABANDON:
334 tagmask = SLAP_CTRL_ABANDON;
336 case LDAP_REQ_EXTENDED:
338 assert( op->o_extendedop != NULL );
339 if( sc->sc_extendedops != NULL ) {
341 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
342 if( strcmp( op->o_extendedop, sc->sc_extendedops[i] )
353 errmsg = "controls internal error";
357 if (( sc->sc_mask & tagmask ) == tagmask ) {
358 /* available extension */
360 if( !sc->sc_parse ) {
362 errmsg = "not yet implemented";
366 rc = sc->sc_parse( conn, op, c, &errmsg );
368 if( rc != LDAP_SUCCESS ) goto return_results;
370 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
371 /* kludge to disable backend_control() check */
372 c->ldctl_iscritical = 0;
374 } else if ( tagmask == SLAP_CTRL_SEARCH &&
375 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
377 /* kludge to disable backend_control() check */
378 c->ldctl_iscritical = 0;
381 } else if( c->ldctl_iscritical ) {
382 /* unavailable CRITICAL control */
383 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
384 errmsg = "critical extension is unavailable";
388 } else if( c->ldctl_iscritical ) {
389 /* unrecognized CRITICAL control */
390 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
391 errmsg = "critical extension is not recognized";
398 LDAP_LOG( OPERATION, RESULTS,
399 "get_ctrls: n=%d rc=%d err=\"%s\"\n",
400 nctrls, rc, errmsg ? errmsg : "" );
402 Debug( LDAP_DEBUG_TRACE,
403 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
404 nctrls, rc, errmsg ? errmsg : "");
407 if( sendres && rc != LDAP_SUCCESS ) {
408 if( rc == SLAPD_DISCONNECT ) {
409 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
411 send_ldap_result( conn, op, rc,
412 NULL, errmsg, NULL, NULL );
419 static int parseManageDSAit (
425 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
426 *text = "manageDSAit control specified multiple times";
427 return LDAP_PROTOCOL_ERROR;
430 if ( ctrl->ldctl_value.bv_len ) {
431 *text = "manageDSAit control value not empty";
432 return LDAP_PROTOCOL_ERROR;
435 op->o_managedsait = ctrl->ldctl_iscritical
436 ? SLAP_CRITICAL_CONTROL
437 : SLAP_NONCRITICAL_CONTROL;
442 static int parseProxyAuthz (
451 if ( op->o_proxy_authz != SLAP_NO_CONTROL ) {
452 *text = "proxy authorization control specified multiple times";
453 return LDAP_PROTOCOL_ERROR;
456 op->o_proxy_authz = ctrl->ldctl_iscritical
457 ? SLAP_CRITICAL_CONTROL
458 : SLAP_NONCRITICAL_CONTROL;
461 LDAP_LOG( OPERATION, ARGS,
462 "parseProxyAuthz: conn %d authzid=\"%s\"\n",
464 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
467 Debug( LDAP_DEBUG_ARGS,
468 "parseProxyAuthz: conn %d authzid=\"%s\"\n",
470 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
474 if( ctrl->ldctl_value.bv_len == 0 ) {
476 LDAP_LOG( OPERATION, RESULTS,
477 "parseProxyAuthz: conn=%d anonymous\n",
478 conn->c_connid, 0, 0 );
480 Debug( LDAP_DEBUG_TRACE,
481 "parseProxyAuthz: conn=%d anonymous\n",
482 conn->c_connid, 0, 0 );
486 free( op->o_dn.bv_val );
488 op->o_dn.bv_val = ch_strdup( "" );
490 free( op->o_ndn.bv_val );
491 op->o_ndn.bv_len = 0;
492 op->o_ndn.bv_val = ch_strdup( "" );
497 rc = slap_sasl_getdn( conn,
498 ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len,
499 NULL, &dn, SLAP_GETDN_AUTHZID );
501 if( rc != LDAP_SUCCESS || !dn.bv_len ) {
502 *text = "authzId mapping failed";
503 return LDAP_PROXY_AUTHZ_FAILURE;
507 LDAP_LOG( OPERATION, RESULTS,
508 "parseProxyAuthz: conn=%d \"%s\"\n",
510 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
512 Debug( LDAP_DEBUG_TRACE,
513 "parseProxyAuthz: conn=%d \"%s\"\n",
515 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
518 rc = slap_sasl_authorized( conn, &op->o_ndn, &dn );
521 ch_free( dn.bv_val );
522 *text = "not authorized to assume identity";
523 return LDAP_PROXY_AUTHZ_FAILURE;
526 ch_free( op->o_dn.bv_val );
527 ch_free( op->o_ndn.bv_val );
529 op->o_dn.bv_val = NULL;
531 ber_dupbv( &op->o_dn, &dn );
536 static int parseNoOp (
542 if ( op->o_noop != SLAP_NO_CONTROL ) {
543 *text = "noop control specified multiple times";
544 return LDAP_PROTOCOL_ERROR;
547 if ( ctrl->ldctl_value.bv_len ) {
548 *text = "noop control value not empty";
549 return LDAP_PROTOCOL_ERROR;
552 op->o_noop = ctrl->ldctl_iscritical
553 ? SLAP_CRITICAL_CONTROL
554 : SLAP_NONCRITICAL_CONTROL;
559 static int parsePagedResults (
568 struct berval cookie = { 0, NULL };
570 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
571 *text = "paged results control specified multiple times";
572 return LDAP_PROTOCOL_ERROR;
575 if ( ctrl->ldctl_value.bv_len == 0 ) {
576 *text = "paged results control value is empty (or absent)";
577 return LDAP_PROTOCOL_ERROR;
580 /* Parse the control value
581 * realSearchControlValue ::= SEQUENCE {
582 * size INTEGER (0..maxInt),
583 * -- requested page size from client
584 * -- result set size estimate from server
585 * cookie OCTET STRING
587 ber = ber_init( &ctrl->ldctl_value );
589 *text = "internal error";
593 tag = ber_scanf( ber, "{im}", &size, &cookie );
594 (void) ber_free( ber, 1 );
596 if( tag == LBER_ERROR ) {
597 *text = "paged results control could not be decoded";
598 return LDAP_PROTOCOL_ERROR;
602 *text = "paged results control size invalid";
603 return LDAP_PROTOCOL_ERROR;
606 if( cookie.bv_len ) {
607 PagedResultsCookie reqcookie;
608 if( cookie.bv_len != sizeof( reqcookie ) ) {
610 *text = "paged results cookie is invalid";
611 return LDAP_PROTOCOL_ERROR;
614 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
616 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
618 *text = "paged results cookie is invalid";
619 return LDAP_PROTOCOL_ERROR;
621 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
622 *text = "paged results cookie is invalid or old";
623 return LDAP_UNWILLING_TO_PERFORM;
626 /* Initial request. Initialize state. */
627 op->o_pagedresults_state.ps_cookie = 0;
628 op->o_pagedresults_state.ps_id = NOID;
631 op->o_pagedresults_size = size;
633 op->o_pagedresults = ctrl->ldctl_iscritical
634 ? SLAP_CRITICAL_CONTROL
635 : SLAP_NONCRITICAL_CONTROL;
640 int parseValuesReturnFilter (
648 struct berval fstr = { 0, NULL };
649 const char *err_msg = "";
651 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
652 *text = "valuesReturnFilter control specified multiple times";
653 return LDAP_PROTOCOL_ERROR;
656 if ( ctrl->ldctl_value.bv_len == 0 ) {
657 *text = "valuesReturnFilter control value is empty (or absent)";
658 return LDAP_PROTOCOL_ERROR;
661 ber = ber_init( &(ctrl->ldctl_value) );
663 *text = "internal error";
667 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
669 if( rc != LDAP_SUCCESS ) {
671 if( rc == SLAPD_DISCONNECT ) {
672 send_ldap_disconnect( conn, op,
673 LDAP_PROTOCOL_ERROR, *text );
675 send_ldap_result( conn, op, rc,
676 NULL, *text, NULL, NULL );
678 if( fstr.bv_val != NULL) free( fstr.bv_val );
679 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
682 vrFilter2bv( op->vrFilter, &fstr );
686 LDAP_LOG( OPERATION, ARGS,
687 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
688 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
690 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
691 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
694 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
695 ? SLAP_CRITICAL_CONTROL
696 : SLAP_NONCRITICAL_CONTROL;
701 #ifdef LDAP_CONTROL_SUBENTRIES
702 static int parseSubentries (
708 if ( op->o_subentries != SLAP_NO_CONTROL ) {
709 *text = "subentries control specified multiple times";
710 return LDAP_PROTOCOL_ERROR;
713 /* FIXME: should use BER library */
714 if( ( ctrl->ldctl_value.bv_len != 3 )
715 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
716 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
718 *text = "subentries control value encoding is bogus";
719 return LDAP_PROTOCOL_ERROR;
722 op->o_subentries = ctrl->ldctl_iscritical
723 ? SLAP_CRITICAL_CONTROL
724 : SLAP_NONCRITICAL_CONTROL;
726 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
732 #ifdef LDAP_CLIENT_UPDATE
733 static int parseClientUpdate (
744 struct berval scheme = { 0, NULL };
745 struct berval cookie = { 0, NULL };
747 if ( op->o_clientupdate != SLAP_NO_CONTROL ) {
748 *text = "LCUP client update control specified multiple times";
749 return LDAP_PROTOCOL_ERROR;
752 if ( ctrl->ldctl_value.bv_len == 0 ) {
753 *text = "LCUP client update control value is empty (or absent)";
754 return LDAP_PROTOCOL_ERROR;
757 /* Parse the control value
758 * ClientUpdateControlValue ::= SEQUENCE {
759 * updateType ENUMERATED {
760 * synchronizeOnly {0},
761 * synchronizeAndPersist {1},
763 * sendCookieInterval INTEGER OPTIONAL,
764 * cookie LCUPCookie OPTIONAL
768 ber = ber_init( &ctrl->ldctl_value );
770 *text = "internal error";
774 if ( (tag = ber_scanf( ber, "{i" /*}*/, &type )) == LBER_ERROR ) {
775 *text = "LCUP client update control : decoding error";
776 return LDAP_PROTOCOL_ERROR;
780 case LDAP_CUP_SYNC_ONLY:
781 type = SLAP_LCUP_SYNC;
783 case LDAP_CUP_SYNC_AND_PERSIST:
784 type = SLAP_LCUP_SYNC_AND_PERSIST;
786 case LDAP_CUP_PERSIST_ONLY:
787 type = SLAP_LCUP_PERSIST;
790 *text = "LCUP client update control : unknown update type";
791 return LDAP_PROTOCOL_ERROR;
794 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
795 *text = "LCUP client update control : decoding error";
796 return LDAP_PROTOCOL_ERROR;
799 if ( tag == LDAP_TAG_INTERVAL ) {
800 if ( (tag = ber_scanf( ber, "i", &interval )) == LBER_ERROR ) {
801 *text = "LCUP client update control : decoding error";
802 return LDAP_PROTOCOL_ERROR;
805 if ( interval <= 0 ) {
806 /* server chooses interval */
807 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
811 /* server chooses interval */
812 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
815 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
816 *text = "LCUP client update control : decoding error";
817 return LDAP_PROTOCOL_ERROR;
820 if ( tag == LDAP_TAG_COOKIE ) {
821 if ( (tag = ber_scanf( ber, /*{*/ "{mm}}",
822 &scheme, &cookie )) == LBER_ERROR ) {
823 *text = "LCUP client update control : decoding error";
824 return LDAP_PROTOCOL_ERROR;
828 /* TODO : Cookie Scheme Validation */
830 if ( lcup_cookie_scheme_validate(scheme) != LDAP_SUCCESS ) {
831 *text = "Unsupported LCUP cookie scheme";
832 return LCUP_UNSUPPORTED_SCHEME;
835 if ( lcup_cookie_validate(scheme, cookie) != LDAP_SUCCESS ) {
836 *text = "Invalid LCUP cookie";
837 return LCUP_INVALID_COOKIE;
841 ber_dupbv( &op->o_clientupdate_state, &cookie );
843 (void) ber_free( ber, 1 );
845 op->o_clientupdate_type = (char) type;
846 op->o_clientupdate_interval = interval;
848 op->o_clientupdate = ctrl->ldctl_iscritical
849 ? SLAP_CRITICAL_CONTROL
850 : SLAP_NONCRITICAL_CONTROL;
854 #endif /* LDAP_CLIENT_UPDATE */