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, RESULTS,
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, RESULTS,
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_ndn, &dn );
515 ch_free( dn.bv_val );
516 *text = "not authorized to assume identity";
517 return LDAP_PROXY_AUTHZ_FAILURE;
520 ch_free( op->o_dn.bv_val );
521 ch_free( op->o_ndn.bv_val );
523 op->o_dn.bv_val = NULL;
525 ber_dupbv( &op->o_dn, &dn );
530 static int parseNoOp (
536 if ( op->o_noop != SLAP_NO_CONTROL ) {
537 *text = "noop control specified multiple times";
538 return LDAP_PROTOCOL_ERROR;
541 if ( ctrl->ldctl_value.bv_len ) {
542 *text = "noop control value not empty";
543 return LDAP_PROTOCOL_ERROR;
546 op->o_noop = ctrl->ldctl_iscritical
547 ? SLAP_CRITICAL_CONTROL
548 : SLAP_NONCRITICAL_CONTROL;
553 static int parsePagedResults (
562 struct berval cookie = { 0, NULL };
564 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
565 *text = "paged results control specified multiple times";
566 return LDAP_PROTOCOL_ERROR;
569 if ( ctrl->ldctl_value.bv_len == 0 ) {
570 *text = "paged results control value is empty (or absent)";
571 return LDAP_PROTOCOL_ERROR;
574 /* Parse the control value
575 * realSearchControlValue ::= SEQUENCE {
576 * size INTEGER (0..maxInt),
577 * -- requested page size from client
578 * -- result set size estimate from server
579 * cookie OCTET STRING
581 ber = ber_init( &ctrl->ldctl_value );
583 *text = "internal error";
587 tag = ber_scanf( ber, "{im}", &size, &cookie );
588 (void) ber_free( ber, 1 );
590 if( tag == LBER_ERROR ) {
591 *text = "paged results control could not be decoded";
592 return LDAP_PROTOCOL_ERROR;
596 *text = "paged results control size invalid";
597 return LDAP_PROTOCOL_ERROR;
600 if( cookie.bv_len ) {
601 PagedResultsCookie reqcookie;
602 if( cookie.bv_len != sizeof( reqcookie ) ) {
604 *text = "paged results cookie is invalid";
605 return LDAP_PROTOCOL_ERROR;
608 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
610 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
612 *text = "paged results cookie is invalid";
613 return LDAP_PROTOCOL_ERROR;
615 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
616 *text = "paged results cookie is invalid or old";
617 return LDAP_UNWILLING_TO_PERFORM;
620 /* Initial request. Initialize state. */
621 op->o_pagedresults_state.ps_cookie = 0;
622 op->o_pagedresults_state.ps_id = NOID;
625 op->o_pagedresults_size = size;
627 op->o_pagedresults = ctrl->ldctl_iscritical
628 ? SLAP_CRITICAL_CONTROL
629 : SLAP_NONCRITICAL_CONTROL;
634 int parseValuesReturnFilter (
642 struct berval fstr = { 0, NULL };
643 const char *err_msg = "";
645 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
646 *text = "valuesReturnFilter control specified multiple times";
647 return LDAP_PROTOCOL_ERROR;
650 if ( ctrl->ldctl_value.bv_len == 0 ) {
651 *text = "valuesReturnFilter control value is empty (or absent)";
652 return LDAP_PROTOCOL_ERROR;
655 ber = ber_init( &(ctrl->ldctl_value) );
657 *text = "internal error";
661 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
663 if( rc != LDAP_SUCCESS ) {
665 if( rc == SLAPD_DISCONNECT ) {
666 send_ldap_disconnect( conn, op,
667 LDAP_PROTOCOL_ERROR, *text );
669 send_ldap_result( conn, op, rc,
670 NULL, *text, NULL, NULL );
672 if( fstr.bv_val != NULL) free( fstr.bv_val );
673 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
676 vrFilter2bv( op->vrFilter, &fstr );
680 LDAP_LOG( OPERATION, ARGS,
681 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
682 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
684 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
685 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
688 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
689 ? SLAP_CRITICAL_CONTROL
690 : SLAP_NONCRITICAL_CONTROL;
695 #ifdef LDAP_CONTROL_SUBENTRIES
696 static int parseSubentries (
702 if ( op->o_subentries != SLAP_NO_CONTROL ) {
703 *text = "subentries control specified multiple times";
704 return LDAP_PROTOCOL_ERROR;
707 /* FIXME: should use BER library */
708 if( ( ctrl->ldctl_value.bv_len != 3 )
709 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
710 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
712 *text = "subentries control value encoding is bogus";
713 return LDAP_PROTOCOL_ERROR;
716 op->o_subentries = ctrl->ldctl_iscritical
717 ? SLAP_CRITICAL_CONTROL
718 : SLAP_NONCRITICAL_CONTROL;
720 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
726 #ifdef LDAP_CLIENT_UPDATE
727 static int parseClientUpdate (
738 struct berval scheme = { 0, NULL };
739 struct berval cookie = { 0, NULL };
741 if ( op->o_clientupdate != SLAP_NO_CONTROL ) {
742 *text = "LCUP client update control specified multiple times";
743 return LDAP_PROTOCOL_ERROR;
746 if ( ctrl->ldctl_value.bv_len == 0 ) {
747 *text = "LCUP client update control value is empty (or absent)";
748 return LDAP_PROTOCOL_ERROR;
751 /* Parse the control value
752 * ClientUpdateControlValue ::= SEQUENCE {
753 * updateType ENUMERATED {
754 * synchronizeOnly {0},
755 * synchronizeAndPersist {1},
757 * sendCookieInterval INTEGER OPTIONAL,
758 * cookie LCUPCookie OPTIONAL
762 ber = ber_init( &ctrl->ldctl_value );
764 *text = "internal error";
768 if ( (tag = ber_scanf( ber, "{i" /*}*/, &type )) == LBER_ERROR ) {
769 *text = "LCUP client update control : decoding error";
770 return LDAP_PROTOCOL_ERROR;
774 case LDAP_CUP_SYNC_ONLY:
775 type = SLAP_LCUP_SYNC;
777 case LDAP_CUP_SYNC_AND_PERSIST:
778 type = SLAP_LCUP_SYNC_AND_PERSIST;
780 case LDAP_CUP_PERSIST_ONLY:
781 type = SLAP_LCUP_PERSIST;
784 *text = "LCUP client update control : unknown update type";
785 return LDAP_PROTOCOL_ERROR;
788 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
789 *text = "LCUP client update control : decoding error";
790 return LDAP_PROTOCOL_ERROR;
793 if ( tag == LDAP_TAG_INTERVAL ) {
794 if ( (tag = ber_scanf( ber, "i", &interval )) == LBER_ERROR ) {
795 *text = "LCUP client update control : decoding error";
796 return LDAP_PROTOCOL_ERROR;
799 if ( interval <= 0 ) {
800 /* server chooses interval */
801 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
805 /* server chooses interval */
806 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
809 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
810 *text = "LCUP client update control : decoding error";
811 return LDAP_PROTOCOL_ERROR;
814 if ( tag == LDAP_TAG_COOKIE ) {
815 if ( (tag = ber_scanf( ber, /*{*/ "{mm}}",
816 &scheme, &cookie )) == LBER_ERROR ) {
817 *text = "LCUP client update control : decoding error";
818 return LDAP_PROTOCOL_ERROR;
822 /* TODO : Cookie Scheme Validation */
824 if ( lcup_cookie_scheme_validate(scheme) != LDAP_SUCCESS ) {
825 *text = "Unsupported LCUP cookie scheme";
826 return LCUP_UNSUPPORTED_SCHEME;
829 if ( lcup_cookie_validate(scheme, cookie) != LDAP_SUCCESS ) {
830 *text = "Invalid LCUP cookie";
831 return LCUP_INVALID_COOKIE;
835 ber_dupbv( &op->o_clientupdate_state, &cookie );
837 (void) ber_free( ber, 1 );
839 op->o_clientupdate_type = (char) type;
840 op->o_clientupdate_interval = interval;
842 op->o_clientupdate = ctrl->ldctl_iscritical
843 ? SLAP_CRITICAL_CONTROL
844 : SLAP_NONCRITICAL_CONTROL;
848 #endif /* LDAP_CLIENT_UPDATE */