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
24 #define SLAP_CTRL_OPFLAGS 0x0000FFFFU
25 #define SLAP_CTRL_ABANDON 0x00000001U
26 #define SLAP_CTRL_ADD 0x00002002U
27 #define SLAP_CTRL_BIND 0x00000004U
28 #define SLAP_CTRL_COMPARE 0x00001008U
29 #define SLAP_CTRL_DELETE 0x00002010U
30 #define SLAP_CTRL_MODIFY 0x00002020U
31 #define SLAP_CTRL_RENAME 0x00002040U
32 #define SLAP_CTRL_SEARCH 0x00001080U
33 #define SLAP_CTRL_UNBIND 0x00000100U
35 #define SLAP_CTRL_INTROGATE (SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH)
36 #define SLAP_CTRL_UPDATE \
37 (SLAP_CTRL_ADD|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME)
38 #define SLAP_CTRL_ACCESS (SLAP_CTRL_INTROGATE|SLAP_CTRL_UPDATE)
40 typedef int (SLAP_CTRL_PARSE_FN) LDAP_P((
46 static SLAP_CTRL_PARSE_FN parseManageDSAit;
47 static SLAP_CTRL_PARSE_FN parseSubentries;
48 static SLAP_CTRL_PARSE_FN parseNoOp;
49 static SLAP_CTRL_PARSE_FN parsePagedResults;
51 static struct slap_control {
54 char **sc_extendedops;
55 SLAP_CTRL_PARSE_FN *sc_parse;
57 } supportedControls[] = {
58 { LDAP_CONTROL_MANAGEDSAIT,
59 SLAP_CTRL_ACCESS, NULL,
61 #ifdef LDAP_CONTROL_SUBENTRIES
62 { LDAP_CONTROL_SUBENTRIES,
63 SLAP_CTRL_SEARCH, NULL,
66 #ifdef LDAP_CONTROL_NOOP
68 SLAP_CTRL_UPDATE, NULL,
71 #ifdef LDAP_CONTROL_PAGEDRESULTS_REQUEST
72 { LDAP_CONTROL_PAGEDRESULTS_REQUEST,
73 SLAP_CTRL_SEARCH, NULL,
80 get_supported_ctrl(int index)
82 return supportedControls[index].sc_oid;
85 static struct slap_control *
86 find_ctrl( const char *oid )
89 for( i=0; supportedControls[i].sc_oid; i++ ) {
90 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
91 return &supportedControls[i];
106 BerElement *ber = op->o_ber;
107 struct slap_control *sc;
108 int rc = LDAP_SUCCESS;
109 const char *errmsg = NULL;
111 len = ber_pvt_ber_remaining(ber);
119 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
120 if( tag == LBER_ERROR ) {
121 rc = SLAPD_DISCONNECT;
122 errmsg = "unexpected data in PDU";
129 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
130 "get_ctrls: conn %lu\n", conn->c_connid ));
132 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
134 if( op->o_protocol < LDAP_VERSION3 ) {
135 rc = SLAPD_DISCONNECT;
136 errmsg = "controls require LDAPv3";
140 /* one for first control, one for termination */
141 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
144 if( op->ctrls == NULL ) {
146 errmsg = "no memory";
151 op->o_ctrls[nctrls] = NULL;
153 /* step through each element */
154 for( tag = ber_first_element( ber, &len, &opaque );
156 tag = ber_next_element( ber, &len, opaque ) )
159 LDAPControl **tctrls;
161 c = ch_calloc( 1, sizeof(LDAPControl) );
165 ldap_controls_free(op->o_ctrls);
169 errmsg = "no memory";
174 /* allocate pointer space for current controls (nctrls)
175 * + this control + extra NULL
177 tctrls = ch_realloc( op->o_ctrls,
178 (nctrls+2) * sizeof(LDAPControl *));
181 if( tctrls == NULL ) {
183 ldap_controls_free(op->o_ctrls);
187 errmsg = "no memory";
191 op->o_ctrls = tctrls;
193 op->o_ctrls[nctrls++] = c;
194 op->o_ctrls[nctrls] = NULL;
196 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
198 if( tag == LBER_ERROR ) {
200 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
201 "get_ctrls: conn %lu get OID failed.\n",
204 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
207 ldap_controls_free( op->o_ctrls );
209 rc = SLAPD_DISCONNECT;
210 errmsg = "decoding controls error";
214 tag = ber_peek_tag( ber, &len );
216 if( tag == LBER_BOOLEAN ) {
218 tag = ber_scanf( ber, "b", &crit );
220 if( tag == LBER_ERROR ) {
222 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
223 "get_ctrls: conn %lu get crit failed.\n",
226 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
229 ldap_controls_free( op->o_ctrls );
231 rc = SLAPD_DISCONNECT;
232 errmsg = "decoding controls error";
236 c->ldctl_iscritical = (crit != 0);
237 tag = ber_peek_tag( ber, &len );
240 if( tag == LBER_OCTETSTRING ) {
241 tag = ber_scanf( ber, "o", &c->ldctl_value );
243 if( tag == LBER_ERROR ) {
245 LDAP_LOG(( "operation", LDAP_LEVEL_INFO, "get_ctrls: conn %lu: "
246 "%s (%scritical): get value failed.\n",
248 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
249 c->ldctl_iscritical ? "" : "non" ));
251 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
252 "%s (%scritical): get value failed.\n",
254 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
255 c->ldctl_iscritical ? "" : "non" );
257 ldap_controls_free( op->o_ctrls );
259 rc = SLAPD_DISCONNECT;
260 errmsg = "decoding controls error";
266 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
267 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
269 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
270 c->ldctl_iscritical ? "" : "non" ));
272 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
273 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
274 c->ldctl_iscritical ? "" : "non",
278 sc = find_ctrl( c->ldctl_oid );
280 /* recognized control */
282 switch( op->o_tag ) {
284 tagmask = SLAP_CTRL_ADD;
287 tagmask = SLAP_CTRL_BIND;
289 case LDAP_REQ_COMPARE:
290 tagmask = SLAP_CTRL_COMPARE;
292 case LDAP_REQ_DELETE:
293 tagmask = SLAP_CTRL_DELETE;
295 case LDAP_REQ_MODIFY:
296 tagmask = SLAP_CTRL_MODIFY;
298 case LDAP_REQ_RENAME:
299 tagmask = SLAP_CTRL_RENAME;
301 case LDAP_REQ_SEARCH:
302 tagmask = SLAP_CTRL_SEARCH;
304 case LDAP_REQ_UNBIND:
305 tagmask = SLAP_CTRL_UNBIND;
307 case LDAP_REQ_EXTENDED:
308 /* FIXME: check list of extended operations */
313 errmsg = "controls internal error";
317 if (( sc->sc_mask & tagmask ) == tagmask ) {
318 /* available extension */
320 if( !sc->sc_parse ) {
322 errmsg = "not yet implemented";
326 rc = sc->sc_parse( conn, op, c, &errmsg );
328 if( rc != LDAP_SUCCESS ) goto return_results;
330 if( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
331 /* kludge to disable backend_control() check */
332 c->ldctl_iscritical = 0;
335 } else if( c->ldctl_iscritical ) {
336 /* unavailable CRITICAL control */
337 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
338 errmsg = "critical extension is unavailable";
342 } else if( c->ldctl_iscritical ) {
343 /* unrecognized CRITICAL control */
344 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
345 errmsg = "critical extension is not recognized";
352 LDAP_LOG(( "operation", LDAP_LEVEL_RESULTS,
353 "get_ctrls: conn=%lu n=%d rc=%d err=%s\n",
354 conn->c_connid, nctrls, rc, errmsg ? errmsg : "" ));
356 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: n=%d rc=%d err=%s\n",
357 nctrls, rc, errmsg ? errmsg : "");
360 if( sendres && rc != LDAP_SUCCESS ) {
361 if( rc == SLAPD_DISCONNECT ) {
362 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
364 send_ldap_result( conn, op, rc,
365 NULL, errmsg, NULL, NULL );
372 static int parseManageDSAit (
378 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
379 *text = "manageDSAit control specified multiple times";
380 return LDAP_PROTOCOL_ERROR;
383 if ( ctrl->ldctl_value.bv_len ) {
384 *text = "manageDSAit control value not empty";
385 return LDAP_PROTOCOL_ERROR;
388 op->o_managedsait = ctrl->ldctl_iscritical
389 ? SLAP_CRITICAL_CONTROL
390 : SLAP_NONCRITICAL_CONTROL;
395 #ifdef LDAP_CONTROL_SUBENTRIES
396 static int parseSubentries (
402 if ( op->o_subentries != SLAP_NO_CONTROL ) {
403 *text = "subentries control specified multiple times";
404 return LDAP_PROTOCOL_ERROR;
407 /* FIXME: should use BER library */
408 if( ( ctrl->ldctl_value.bv_len != 3 )
409 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
410 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
412 *text = "subentries control value encoding is bogus";
413 return LDAP_PROTOCOL_ERROR;
416 op->o_subentries = ctrl->ldctl_iscritical
417 ? SLAP_CRITICAL_CONTROL
418 : SLAP_NONCRITICAL_CONTROL;
420 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
426 #ifdef LDAP_CONTROL_NOOP
427 static int parseNoOp (
433 if ( op->o_noop != SLAP_NO_CONTROL ) {
434 *text = "noop control specified multiple times";
435 return LDAP_PROTOCOL_ERROR;
438 if ( ctrl->ldctl_value.bv_len ) {
439 *text = "noop control value not empty";
440 return LDAP_PROTOCOL_ERROR;
443 op->o_noop = ctrl->ldctl_iscritical
444 ? SLAP_CRITICAL_CONTROL
445 : SLAP_NONCRITICAL_CONTROL;
451 #ifdef LDAP_CONTROL_PAGEDRESULTS_REQUEST
452 static int parsePagedResults (
461 struct berval cookie = { 0, NULL };
463 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
464 *text = "paged results control specified multiple times";
465 return LDAP_PROTOCOL_ERROR;
468 if ( ctrl->ldctl_value.bv_len == 0 ) {
469 *text = "paged results control value is empty";
470 return LDAP_PROTOCOL_ERROR;
473 /* Parse the control value
474 * realSearchControlValue ::= SEQUENCE {
475 * size INTEGER (0..maxInt),
476 * -- requested page size from client
477 * -- result set size estimate from server
478 * cookie OCTET STRING
480 ber = ber_init( &ctrl->ldctl_value );
482 *text = "internal error";
486 tag = ber_scanf( ber, "{im}", &size, &cookie );
487 (void) ber_free( ber, 1 );
489 if( tag == LBER_ERROR ) {
490 *text = "paged results control could not be decoded";
491 return LDAP_PROTOCOL_ERROR;
495 *text = "paged results control size invalid";
496 return LDAP_PROTOCOL_ERROR;
499 if( cookie.bv_len ) {
500 PagedResultsCookie reqcookie;
501 if( cookie.bv_len != sizeof( reqcookie ) ) {
503 *text = "paged results cookie is invalid";
504 return LDAP_PROTOCOL_ERROR;
507 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
509 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
511 *text = "paged results cookie is invalid";
512 return LDAP_PROTOCOL_ERROR;
514 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
515 *text = "paged results cookie is invalid or old";
516 return LDAP_UNWILLING_TO_PERFORM;
520 op->o_pagedresults_state.ps_cookie = op->o_opid;
521 op->o_pagedresults_size = size;
523 op->o_pagedresults = ctrl->ldctl_iscritical
524 ? SLAP_CRITICAL_CONTROL
525 : SLAP_NONCRITICAL_CONTROL;