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;
50 static struct slap_control {
53 char **sc_extendedops;
54 SLAP_CTRL_PARSE_FN *sc_parse;
56 } supportedControls[] = {
57 { LDAP_CONTROL_MANAGEDSAIT,
58 SLAP_CTRL_ACCESS, NULL,
60 { LDAP_CONTROL_SUBENTRIES,
61 SLAP_CTRL_SEARCH, NULL,
63 #ifdef LDAP_CONTROL_NOOP
65 SLAP_CTRL_UPDATE, NULL,
72 get_supported_ctrl(int index)
74 return supportedControls[index].sc_oid;
77 static struct slap_control *
78 find_ctrl( const char *oid )
81 for( i=0; supportedControls[i].sc_oid; i++ ) {
82 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
83 return &supportedControls[i];
98 BerElement *ber = op->o_ber;
99 struct slap_control *sc;
100 int rc = LDAP_SUCCESS;
101 const char *errmsg = NULL;
103 len = ber_pvt_ber_remaining(ber);
111 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
112 if( tag == LBER_ERROR ) {
113 rc = SLAPD_DISCONNECT;
114 errmsg = "unexpected data in PDU";
121 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
122 "get_ctrls: conn %d\n", conn->c_connid ));
124 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
126 if( op->o_protocol < LDAP_VERSION3 ) {
127 rc = SLAPD_DISCONNECT;
128 errmsg = "controls require LDAPv3";
132 /* one for first control, one for termination */
133 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
136 if( op->ctrls == NULL ) {
138 errmsg = "no memory";
143 op->o_ctrls[nctrls=0] = NULL;
145 /* step through each element */
146 for( tag = ber_first_element( ber, &len, &opaque );
148 tag = ber_next_element( ber, &len, opaque ) )
151 LDAPControl **tctrls;
153 c = ch_calloc( 1, sizeof(LDAPControl) );
157 ldap_controls_free(op->o_ctrls);
161 errmsg = "no memory";
166 /* allocate pointer space for current controls (nctrls)
167 * + this control + extra NULL
169 tctrls = ch_realloc( op->o_ctrls,
170 (nctrls+2) * sizeof(LDAPControl *));
173 if( tctrls == NULL ) {
175 ldap_controls_free(op->o_ctrls);
179 errmsg = "no memory";
183 op->o_ctrls = tctrls;
185 op->o_ctrls[nctrls++] = c;
186 op->o_ctrls[nctrls] = NULL;
188 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
190 if( tag == LBER_ERROR ) {
192 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
193 "get_ctrls: conn %d get OID failed.\n",
196 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
199 ldap_controls_free( op->o_ctrls );
201 rc = SLAPD_DISCONNECT;
202 errmsg = "decoding controls error";
206 tag = ber_peek_tag( ber, &len );
208 if( tag == LBER_BOOLEAN ) {
210 tag = ber_scanf( ber, "b", &crit );
212 if( tag == LBER_ERROR ) {
214 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
215 "get_ctrls: conn %d get crit failed.\n",
218 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
221 ldap_controls_free( op->o_ctrls );
223 rc = SLAPD_DISCONNECT;
224 errmsg = "decoding controls error";
228 c->ldctl_iscritical = (crit != 0);
229 tag = ber_peek_tag( ber, &len );
232 if( tag == LBER_OCTETSTRING ) {
233 tag = ber_scanf( ber, "o", &c->ldctl_value );
235 if( tag == LBER_ERROR ) {
237 LDAP_LOG(( "operation", LDAP_LEVEL_INFO, "get_ctrls: conn %d: "
238 "%s (%scritical): get value failed.\n",
240 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
241 c->ldctl_iscritical ? "" : "non" ));
243 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %d: "
244 "%s (%scritical): get value failed.\n",
246 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
247 c->ldctl_iscritical ? "" : "non" );
249 ldap_controls_free( op->o_ctrls );
251 rc = SLAPD_DISCONNECT;
252 errmsg = "decoding controls error";
258 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
259 "get_ctrls: conn %d oid=\"%s\" (%scritical)\n",
261 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
262 c->ldctl_iscritical ? "" : "non" ));
264 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
265 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
266 c->ldctl_iscritical ? "" : "non",
270 sc = find_ctrl( c->ldctl_oid );
272 /* recognized control */
274 switch( op->o_tag ) {
276 tagmask = SLAP_CTRL_ADD;
279 tagmask = SLAP_CTRL_BIND;
281 case LDAP_REQ_COMPARE:
282 tagmask = SLAP_CTRL_COMPARE;
284 case LDAP_REQ_DELETE:
285 tagmask = SLAP_CTRL_DELETE;
287 case LDAP_REQ_MODIFY:
288 tagmask = SLAP_CTRL_MODIFY;
290 case LDAP_REQ_RENAME:
291 tagmask = SLAP_CTRL_RENAME;
293 case LDAP_REQ_SEARCH:
294 tagmask = SLAP_CTRL_SEARCH;
296 case LDAP_REQ_UNBIND:
297 tagmask = SLAP_CTRL_UNBIND;
299 case LDAP_REQ_EXTENDED:
300 /* FIXME: check list of extended operations */
305 errmsg = "controls internal error";
309 if (( sc->sc_mask & tagmask ) == tagmask ) {
310 /* available extension */
312 if( !sc->sc_parse ) {
314 errmsg = "not yet implemented";
318 rc = sc->sc_parse( conn, op, c, &errmsg );
320 if( rc != LDAP_SUCCESS ) goto return_results;
322 if( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
323 /* kludge to disable backend_control() check */
324 c->ldctl_iscritical = 0;
327 } else if( c->ldctl_iscritical ) {
328 /* unavailable CRITICAL control */
329 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
330 errmsg = "critical extension is unavailable";
334 } else if( c->ldctl_iscritical ) {
335 /* unrecognized CRITICAL control */
336 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
337 errmsg = "critical extension is not recognized";
344 LDAP_LOG(( "operation", LDAP_LEVEL_RESULTS,
345 "get_ctrls: conn %d %d %d %s\n",
346 conn->c_connid, nctrls, rc, errmsg ? errmsg : "" ));
348 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: %d %d %s\n",
349 nctrls, rc, errmsg ? errmsg : "");
352 if( sendres && rc != LDAP_SUCCESS ) {
353 if( rc == SLAPD_DISCONNECT ) {
354 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
356 send_ldap_result( conn, op, rc,
357 NULL, errmsg, NULL, NULL );
364 static int parseManageDSAit (
370 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
371 *text = "manageDSAit control specified multiple times";
372 return LDAP_PROTOCOL_ERROR;
375 if ( ctrl->ldctl_value.bv_len ) {
376 *text = "manageDSAit control value not empty";
377 return LDAP_PROTOCOL_ERROR;
380 op->o_managedsait = ctrl->ldctl_iscritical
381 ? SLAP_CRITICAL_CONTROL
382 : SLAP_NONCRITICAL_CONTROL;
387 static int parseSubentries (
393 if ( op->o_subentries != SLAP_NO_CONTROL ) {
394 *text = "subentries control specified multiple times";
395 return LDAP_PROTOCOL_ERROR;
398 /* FIXME: should use BER library */
399 if( ( ctrl->ldctl_value.bv_len != 3 )
400 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
401 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
403 *text = "subentries control value encoding is bogus";
404 return LDAP_PROTOCOL_ERROR;
407 op->o_subentries = ctrl->ldctl_iscritical
408 ? SLAP_CRITICAL_CONTROL
409 : SLAP_NONCRITICAL_CONTROL;
411 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
416 static int parseNoOp (
422 if ( op->o_noop != SLAP_NO_CONTROL ) {
423 *text = "noop control specified multiple times";
424 return LDAP_PROTOCOL_ERROR;
427 if ( ctrl->ldctl_value.bv_len ) {
428 *text = "noop control value not empty";
429 return LDAP_PROTOCOL_ERROR;
432 op->o_noop = ctrl->ldctl_iscritical
433 ? SLAP_CRITICAL_CONTROL
434 : SLAP_NONCRITICAL_CONTROL;