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;
109 static struct slap_control *
110 find_ctrl( const char *oid )
113 for( i=0; supportedControls[i].sc_oid; i++ ) {
114 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
115 return &supportedControls[i];
130 BerElement *ber = op->o_ber;
131 struct slap_control *sc;
132 int rc = LDAP_SUCCESS;
133 const char *errmsg = NULL;
135 len = ber_pvt_ber_remaining(ber);
143 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
144 if( tag == LBER_ERROR ) {
145 rc = SLAPD_DISCONNECT;
146 errmsg = "unexpected data in PDU";
153 LDAP_LOG( OPERATION, ENTRY, "get_ctrls: conn %lu\n", conn->c_connid, 0, 0 );
155 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
157 if( op->o_protocol < LDAP_VERSION3 ) {
158 rc = SLAPD_DISCONNECT;
159 errmsg = "controls require LDAPv3";
163 /* one for first control, one for termination */
164 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
167 if( op->ctrls == NULL ) {
169 errmsg = "no memory";
174 op->o_ctrls[nctrls] = NULL;
176 /* step through each element */
177 for( tag = ber_first_element( ber, &len, &opaque );
179 tag = ber_next_element( ber, &len, opaque ) )
182 LDAPControl **tctrls;
184 c = ch_calloc( 1, sizeof(LDAPControl) );
188 ldap_controls_free(op->o_ctrls);
192 errmsg = "no memory";
197 /* allocate pointer space for current controls (nctrls)
198 * + this control + extra NULL
200 tctrls = ch_realloc( op->o_ctrls,
201 (nctrls+2) * sizeof(LDAPControl *));
204 if( tctrls == NULL ) {
206 ldap_controls_free(op->o_ctrls);
210 errmsg = "no memory";
214 op->o_ctrls = tctrls;
216 op->o_ctrls[nctrls++] = c;
217 op->o_ctrls[nctrls] = NULL;
219 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
221 if( tag == LBER_ERROR ) {
223 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
224 conn->c_connid, 0, 0 );
226 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
229 ldap_controls_free( op->o_ctrls );
231 rc = SLAPD_DISCONNECT;
232 errmsg = "decoding controls error";
236 tag = ber_peek_tag( ber, &len );
238 if( tag == LBER_BOOLEAN ) {
240 tag = ber_scanf( ber, "b", &crit );
242 if( tag == LBER_ERROR ) {
244 LDAP_LOG( OPERATION, INFO,
245 "get_ctrls: conn %lu get crit failed.\n",
246 conn->c_connid, 0, 0 );
248 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
251 ldap_controls_free( op->o_ctrls );
253 rc = SLAPD_DISCONNECT;
254 errmsg = "decoding controls error";
258 c->ldctl_iscritical = (crit != 0);
259 tag = ber_peek_tag( ber, &len );
262 if( tag == LBER_OCTETSTRING ) {
263 tag = ber_scanf( ber, "o", &c->ldctl_value );
265 if( tag == LBER_ERROR ) {
267 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
268 "%s (%scritical): get value failed.\n",
269 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
270 c->ldctl_iscritical ? "" : "non" );
272 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
273 "%s (%scritical): get value failed.\n",
275 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
276 c->ldctl_iscritical ? "" : "non" );
278 ldap_controls_free( op->o_ctrls );
280 rc = SLAPD_DISCONNECT;
281 errmsg = "decoding controls error";
287 LDAP_LOG( OPERATION, INFO,
288 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
289 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
290 c->ldctl_iscritical ? "" : "non" );
292 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
293 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
294 c->ldctl_iscritical ? "" : "non",
298 sc = find_ctrl( c->ldctl_oid );
300 /* recognized control */
302 switch( op->o_tag ) {
304 tagmask = SLAP_CTRL_ADD;
307 tagmask = SLAP_CTRL_BIND;
309 case LDAP_REQ_COMPARE:
310 tagmask = SLAP_CTRL_COMPARE;
312 case LDAP_REQ_DELETE:
313 tagmask = SLAP_CTRL_DELETE;
315 case LDAP_REQ_MODIFY:
316 tagmask = SLAP_CTRL_MODIFY;
318 case LDAP_REQ_RENAME:
319 tagmask = SLAP_CTRL_RENAME;
321 case LDAP_REQ_SEARCH:
322 tagmask = SLAP_CTRL_SEARCH;
324 case LDAP_REQ_UNBIND:
325 tagmask = SLAP_CTRL_UNBIND;
327 case LDAP_REQ_ABANDON:
328 tagmask = SLAP_CTRL_ABANDON;
330 case LDAP_REQ_EXTENDED:
332 assert( op->o_extendedop != NULL );
333 if( sc->sc_extendedops != NULL ) {
335 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
336 if( strcmp( op->o_extendedop, sc->sc_extendedops[i] )
347 errmsg = "controls internal error";
351 if (( sc->sc_mask & tagmask ) == tagmask ) {
352 /* available extension */
354 if( !sc->sc_parse ) {
356 errmsg = "not yet implemented";
360 rc = sc->sc_parse( conn, op, c, &errmsg );
362 if( rc != LDAP_SUCCESS ) goto return_results;
364 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
365 /* kludge to disable backend_control() check */
366 c->ldctl_iscritical = 0;
368 } else if ( tagmask == SLAP_CTRL_SEARCH &&
369 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
371 /* kludge to disable backend_control() check */
372 c->ldctl_iscritical = 0;
375 } else if( c->ldctl_iscritical ) {
376 /* unavailable CRITICAL control */
377 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
378 errmsg = "critical extension is unavailable";
382 } else if( c->ldctl_iscritical ) {
383 /* unrecognized CRITICAL control */
384 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
385 errmsg = "critical extension is not recognized";
392 LDAP_LOG( OPERATION, RESULTS,
393 "get_ctrls: n=%d rc=%d err=\"%s\"\n",
394 nctrls, rc, errmsg ? errmsg : "" );
396 Debug( LDAP_DEBUG_TRACE,
397 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
398 nctrls, rc, errmsg ? errmsg : "");
401 if( sendres && rc != LDAP_SUCCESS ) {
402 if( rc == SLAPD_DISCONNECT ) {
403 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
405 send_ldap_result( conn, op, rc,
406 NULL, errmsg, NULL, NULL );
413 static int parseManageDSAit (
419 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
420 *text = "manageDSAit control specified multiple times";
421 return LDAP_PROTOCOL_ERROR;
424 if ( ctrl->ldctl_value.bv_len ) {
425 *text = "manageDSAit control value not empty";
426 return LDAP_PROTOCOL_ERROR;
429 op->o_managedsait = ctrl->ldctl_iscritical
430 ? SLAP_CRITICAL_CONTROL
431 : SLAP_NONCRITICAL_CONTROL;
436 static int parseProxyAuthz (
445 if ( op->o_proxy_authz != SLAP_NO_CONTROL ) {
446 *text = "proxy authorization control specified multiple times";
447 return LDAP_PROTOCOL_ERROR;
450 op->o_proxy_authz = ctrl->ldctl_iscritical
451 ? SLAP_CRITICAL_CONTROL
452 : SLAP_NONCRITICAL_CONTROL;
455 LDAP_LOG( OPERATION, ARGS,
456 "parseProxyAuthz: conn %d authzid=\"%s\"\n",
458 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
461 Debug( LDAP_DEBUG_ARGS,
462 "parseProxyAuthz: conn %d authzid=\"%s\"\n",
464 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
468 if( ctrl->ldctl_value.bv_len == 0 ) {
470 LDAP_LOG( OPERATION, TRACE,
471 "parseProxyAuthz: conn=%d anonymous\n",
472 conn->c_connid, 0, 0 );
474 Debug( LDAP_DEBUG_TRACE,
475 "parseProxyAuthz: conn=%d anonymous\n",
476 conn->c_connid, 0, 0 );
480 free( op->o_dn.bv_val );
482 op->o_dn.bv_val = ch_strdup( "" );
484 free( op->o_ndn.bv_val );
485 op->o_ndn.bv_len = 0;
486 op->o_ndn.bv_val = ch_strdup( "" );
491 rc = slap_sasl_getdn( conn,
492 ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len,
493 NULL, &dn, SLAP_GETDN_AUTHZID );
495 if( rc != LDAP_SUCCESS || !dn.bv_len ) {
496 *text = "authzId mapping failed";
497 return LDAP_PROXY_AUTHZ_FAILURE;
501 LDAP_LOG( OPERATION, TRACE,
502 "parseProxyAuthz: conn=%d \"%s\"\n",
504 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
506 Debug( LDAP_DEBUG_TRACE,
507 "parseProxyAuthz: conn=%d \"%s\"\n",
509 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
512 rc = slap_sasl_authorized( conn, &op->o_dn, &dn );
515 ch_free( dn.bv_val );
516 *text = "not authorized to assume identity";
517 return LDAP_PROXY_AUTHZ_FAILURE;
522 ch_free( op->o_ndn );
527 *text = "not (yet) implemented";
531 static int parseNoOp (
537 if ( op->o_noop != SLAP_NO_CONTROL ) {
538 *text = "noop control specified multiple times";
539 return LDAP_PROTOCOL_ERROR;
542 if ( ctrl->ldctl_value.bv_len ) {
543 *text = "noop control value not empty";
544 return LDAP_PROTOCOL_ERROR;
547 op->o_noop = ctrl->ldctl_iscritical
548 ? SLAP_CRITICAL_CONTROL
549 : SLAP_NONCRITICAL_CONTROL;
554 static int parsePagedResults (
563 struct berval cookie = { 0, NULL };
565 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
566 *text = "paged results control specified multiple times";
567 return LDAP_PROTOCOL_ERROR;
570 if ( ctrl->ldctl_value.bv_len == 0 ) {
571 *text = "paged results control value is empty (or absent)";
572 return LDAP_PROTOCOL_ERROR;
575 /* Parse the control value
576 * realSearchControlValue ::= SEQUENCE {
577 * size INTEGER (0..maxInt),
578 * -- requested page size from client
579 * -- result set size estimate from server
580 * cookie OCTET STRING
582 ber = ber_init( &ctrl->ldctl_value );
584 *text = "internal error";
588 tag = ber_scanf( ber, "{im}", &size, &cookie );
589 (void) ber_free( ber, 1 );
591 if( tag == LBER_ERROR ) {
592 *text = "paged results control could not be decoded";
593 return LDAP_PROTOCOL_ERROR;
597 *text = "paged results control size invalid";
598 return LDAP_PROTOCOL_ERROR;
601 if( cookie.bv_len ) {
602 PagedResultsCookie reqcookie;
603 if( cookie.bv_len != sizeof( reqcookie ) ) {
605 *text = "paged results cookie is invalid";
606 return LDAP_PROTOCOL_ERROR;
609 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
611 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
613 *text = "paged results cookie is invalid";
614 return LDAP_PROTOCOL_ERROR;
616 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
617 *text = "paged results cookie is invalid or old";
618 return LDAP_UNWILLING_TO_PERFORM;
621 /* Initial request. Initialize state. */
622 op->o_pagedresults_state.ps_cookie = 0;
623 op->o_pagedresults_state.ps_id = NOID;
626 op->o_pagedresults_size = size;
628 op->o_pagedresults = ctrl->ldctl_iscritical
629 ? SLAP_CRITICAL_CONTROL
630 : SLAP_NONCRITICAL_CONTROL;
635 int parseValuesReturnFilter (
643 struct berval fstr = { 0, NULL };
644 const char *err_msg = "";
646 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
647 *text = "valuesReturnFilter control specified multiple times";
648 return LDAP_PROTOCOL_ERROR;
651 if ( ctrl->ldctl_value.bv_len == 0 ) {
652 *text = "valuesReturnFilter control value is empty (or absent)";
653 return LDAP_PROTOCOL_ERROR;
656 ber = ber_init( &(ctrl->ldctl_value) );
658 *text = "internal error";
662 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
664 if( rc != LDAP_SUCCESS ) {
666 if( rc == SLAPD_DISCONNECT ) {
667 send_ldap_disconnect( conn, op,
668 LDAP_PROTOCOL_ERROR, *text );
670 send_ldap_result( conn, op, rc,
671 NULL, *text, NULL, NULL );
673 if( fstr.bv_val != NULL) free( fstr.bv_val );
674 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
677 vrFilter2bv( op->vrFilter, &fstr );
681 LDAP_LOG( OPERATION, ARGS,
682 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
683 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
685 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
686 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
689 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
690 ? SLAP_CRITICAL_CONTROL
691 : SLAP_NONCRITICAL_CONTROL;
696 #ifdef LDAP_CONTROL_SUBENTRIES
697 static int parseSubentries (
703 if ( op->o_subentries != SLAP_NO_CONTROL ) {
704 *text = "subentries control specified multiple times";
705 return LDAP_PROTOCOL_ERROR;
708 /* FIXME: should use BER library */
709 if( ( ctrl->ldctl_value.bv_len != 3 )
710 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
711 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
713 *text = "subentries control value encoding is bogus";
714 return LDAP_PROTOCOL_ERROR;
717 op->o_subentries = ctrl->ldctl_iscritical
718 ? SLAP_CRITICAL_CONTROL
719 : SLAP_NONCRITICAL_CONTROL;
721 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
727 #ifdef LDAP_CLIENT_UPDATE
728 static int parseClientUpdate (
739 struct berval scheme = { 0, NULL };
740 struct berval cookie = { 0, NULL };
742 if ( op->o_clientupdate != SLAP_NO_CONTROL ) {
743 *text = "LCUP client update control specified multiple times";
744 return LDAP_PROTOCOL_ERROR;
747 if ( ctrl->ldctl_value.bv_len == 0 ) {
748 *text = "LCUP client update control value is empty (or absent)";
749 return LDAP_PROTOCOL_ERROR;
752 /* Parse the control value
753 * ClientUpdateControlValue ::= SEQUENCE {
754 * updateType ENUMERATED {
755 * synchronizeOnly {0},
756 * synchronizeAndPersist {1},
758 * sendCookieInterval INTEGER OPTIONAL,
759 * cookie LCUPCookie OPTIONAL
763 ber = ber_init( &ctrl->ldctl_value );
765 *text = "internal error";
769 if ( (tag = ber_scanf( ber, "{i" /*}*/, &type )) == LBER_ERROR ) {
770 *text = "LCUP client update control : decoding error";
771 return LDAP_PROTOCOL_ERROR;
775 case LDAP_CUP_SYNC_ONLY:
776 type = SLAP_LCUP_SYNC;
778 case LDAP_CUP_SYNC_AND_PERSIST:
779 type = SLAP_LCUP_SYNC_AND_PERSIST;
781 case LDAP_CUP_PERSIST_ONLY:
782 type = SLAP_LCUP_PERSIST;
785 *text = "LCUP client update control : unknown update type";
786 return LDAP_PROTOCOL_ERROR;
789 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
790 *text = "LCUP client update control : decoding error";
791 return LDAP_PROTOCOL_ERROR;
794 if ( tag == LDAP_TAG_INTERVAL ) {
795 if ( (tag = ber_scanf( ber, "i", &interval )) == LBER_ERROR ) {
796 *text = "LCUP client update control : decoding error";
797 return LDAP_PROTOCOL_ERROR;
800 if ( interval <= 0 ) {
801 /* server chooses interval */
802 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
806 /* server chooses interval */
807 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
810 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
811 *text = "LCUP client update control : decoding error";
812 return LDAP_PROTOCOL_ERROR;
815 if ( tag == LDAP_TAG_COOKIE ) {
816 if ( (tag = ber_scanf( ber, /*{*/ "{mm}}",
817 &scheme, &cookie )) == LBER_ERROR ) {
818 *text = "LCUP client update control : decoding error";
819 return LDAP_PROTOCOL_ERROR;
823 /* TODO : Cookie Scheme Validation */
825 if ( lcup_cookie_scheme_validate(scheme) != LDAP_SUCCESS ) {
826 *text = "Unsupported LCUP cookie scheme";
827 return LCUP_UNSUPPORTED_SCHEME;
830 if ( lcup_cookie_validate(scheme, cookie) != LDAP_SUCCESS ) {
831 *text = "Invalid LCUP cookie";
832 return LCUP_INVALID_COOKIE;
836 ber_dupbv( &op->o_clientupdate_state, &cookie );
838 (void) ber_free( ber, 1 );
840 op->o_clientupdate_type = (char) type;
841 op->o_clientupdate_interval = interval;
843 op->o_clientupdate = ctrl->ldctl_iscritical
844 ? SLAP_CRITICAL_CONTROL
845 : SLAP_NONCRITICAL_CONTROL;
849 #endif /* LDAP_CLIENT_UPDATE */