]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
Sync with HEAD
[openldap] / servers / slapd / controls.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/string.h>
21 #include <ac/socket.h>
22
23 #include "slap.h"
24
25 #include "../../libraries/liblber/lber-int.h"
26
27 static SLAP_CTRL_PARSE_FN parseAssert;
28 static SLAP_CTRL_PARSE_FN parsePreRead;
29 static SLAP_CTRL_PARSE_FN parsePostRead;
30 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
31 #ifdef LDAP_DEVEL
32 static SLAP_CTRL_PARSE_FN parseManageDIT;
33 #endif
34 static SLAP_CTRL_PARSE_FN parseManageDSAit;
35 static SLAP_CTRL_PARSE_FN parseNoOp;
36 static SLAP_CTRL_PARSE_FN parsePagedResults;
37 #ifdef LDAP_DEVEL
38 static SLAP_CTRL_PARSE_FN parseSortedResults;
39 #endif
40 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
41 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
42 static SLAP_CTRL_PARSE_FN parseDomainScope;
43 #ifdef SLAP_CONTROL_X_TREE_DELETE
44 static SLAP_CTRL_PARSE_FN parseTreeDelete;
45 #endif
46 static SLAP_CTRL_PARSE_FN parseSearchOptions;
47 static SLAP_CTRL_PARSE_FN parseSubentries;
48
49 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
50
51 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
52 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
53
54 struct slap_control_ids slap_cids;
55
56 struct slap_control {
57         /* Control OID */
58         char *sc_oid;
59
60         /* The controlID for this control */
61         int sc_cid;
62
63         /* Operations supported by control */
64         slap_mask_t sc_mask;
65
66         /* Extended operations supported by control */
67         char **sc_extendedops;
68
69         /* Control parsing callback */
70         SLAP_CTRL_PARSE_FN *sc_parse;
71
72         LDAP_SLIST_ENTRY(slap_control) sc_next;
73 };
74
75 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
76         = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
77
78 /*
79  * all known request control OIDs should be added to this list
80  */
81 /*
82  * NOTE: initialize num_known_controls to 1 so that cid = 0 always
83  * addresses an undefined control; this allows to safely test for 
84  * well known controls even if they are not registered, e.g. if 
85  * they get moved to modules.  An example is sc_LDAPsync, which 
86  * is implemented in the syncprov overlay and thus, if configured 
87  * as dynamic module, may not be registered.  One side effect is that 
88  * slap_known_controls[0] == NULL, so it should always be used 
89  * starting from 1.
90  * FIXME: should we define the "undefined control" oid?
91  */
92 char *slap_known_controls[SLAP_MAX_CIDS+1];
93 static int num_known_controls = 1;
94
95 static char *proxy_authz_extops[] = {
96         LDAP_EXOP_MODIFY_PASSWD,
97         LDAP_EXOP_X_WHO_AM_I,
98         NULL
99 };
100
101 static struct slap_control control_defs[] = {
102         {  LDAP_CONTROL_ASSERT,
103                 (int)offsetof(struct slap_control_ids, sc_assert),
104                 SLAP_CTRL_ACCESS, NULL,
105                 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
106         { LDAP_CONTROL_PRE_READ,
107                 (int)offsetof(struct slap_control_ids, sc_preRead),
108                 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
109                 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
110         { LDAP_CONTROL_POST_READ,
111                 (int)offsetof(struct slap_control_ids, sc_postRead),
112                 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
113                 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
114         { LDAP_CONTROL_VALUESRETURNFILTER,
115                 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
116                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
117                 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
118         { LDAP_CONTROL_PAGEDRESULTS,
119                 (int)offsetof(struct slap_control_ids, sc_pagedResults),
120                 SLAP_CTRL_SEARCH, NULL,
121                 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
122 #ifdef LDAP_DEVEL
123         { LDAP_CONTROL_SORTREQUEST,
124                 (int)offsetof(struct slap_control_ids, sc_sortedResults),
125                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE, NULL,
126                 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
127 #endif
128 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
129         { LDAP_CONTROL_X_DOMAIN_SCOPE,
130                 (int)offsetof(struct slap_control_ids, sc_domainScope),
131                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
132                 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
133 #endif
134 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
135         { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
136                 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
137                 SLAP_CTRL_MODIFY, NULL,
138                 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 #endif
140 #ifdef SLAP_CONTROL_X_TREE_DELETE
141         { LDAP_CONTROL_X_TREE_DELETE,
142                 (int)offsetof(struct slap_control_ids, sc_treeDelete),
143                 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
144                 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
145 #endif
146 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
147         { LDAP_CONTROL_X_SEARCH_OPTIONS,
148                 (int)offsetof(struct slap_control_ids, sc_searchOptions),
149                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
150                 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
151 #endif
152 #ifdef LDAP_CONTROL_SUBENTRIES
153         { LDAP_CONTROL_SUBENTRIES,
154                 (int)offsetof(struct slap_control_ids, sc_subentries),
155                 SLAP_CTRL_SEARCH, NULL,
156                 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
157 #endif
158         { LDAP_CONTROL_NOOP,
159                 (int)offsetof(struct slap_control_ids, sc_noOp),
160                 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
161                 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
162 #ifdef LDAP_DEVEL
163         { LDAP_CONTROL_MANAGEDIT,
164                 (int)offsetof(struct slap_control_ids, sc_manageDIT),
165                 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE, NULL,
166                 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
167 #endif
168         { LDAP_CONTROL_MANAGEDSAIT,
169                 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
170                 SLAP_CTRL_ACCESS, NULL,
171                 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
172         { LDAP_CONTROL_PROXY_AUTHZ,
173                 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
174                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS, proxy_authz_extops,
175                 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
176         { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
177 };
178
179 /*
180  * Register a supported control.
181  *
182  * This can be called by an OpenLDAP plugin or, indirectly, by a
183  * SLAPI plugin calling slapi_register_supported_control().
184  */
185 int
186 register_supported_control(const char *controloid,
187         slap_mask_t controlmask,
188         char **controlexops,
189         SLAP_CTRL_PARSE_FN *controlparsefn,
190         int *controlcid)
191 {
192         struct slap_control *sc;
193         int i;
194
195         if ( num_known_controls >= SLAP_MAX_CIDS ) {
196                 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
197                         " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
198                 SLAP_MAX_CIDS, 0, 0 );
199                 return LDAP_OTHER;
200         }
201
202         if ( controloid == NULL ) return LDAP_PARAM_ERROR;
203
204         /* sanity check - should never happen */
205         for ( i = 0; slap_known_controls[ i ]; i++ ) {
206                 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
207                         Debug( LDAP_DEBUG_ANY,
208                                 "Control %s already registered.\n",
209                                 controloid, 0, 0 );
210                         return LDAP_PARAM_ERROR;
211                 }
212         }
213
214         sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
215         if ( sc == NULL ) return LDAP_NO_MEMORY;
216
217         sc->sc_oid = ch_strdup( controloid );
218         sc->sc_mask = controlmask;
219         if ( controlexops != NULL ) {
220                 sc->sc_extendedops = ldap_charray_dup( controlexops );
221                 if ( sc->sc_extendedops == NULL ) {
222                         ch_free( sc );
223                         return LDAP_NO_MEMORY;
224                 }
225         } else {
226                 sc->sc_extendedops = NULL;
227         }
228         sc->sc_parse = controlparsefn;
229
230         if ( controlcid ) *controlcid = num_known_controls;
231         sc->sc_cid = num_known_controls;
232
233         /* Update slap_known_controls, too. */
234         slap_known_controls[num_known_controls-1] = sc->sc_oid;
235         slap_known_controls[num_known_controls++] = NULL;
236
237         LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
238         LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
239         return LDAP_SUCCESS;
240 }
241
242 /*
243  * One-time initialization of internal controls.
244  */
245 int
246 slap_controls_init( void )
247 {
248         int i, rc;
249
250         rc = LDAP_SUCCESS;
251
252         for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
253                 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
254                 rc = register_supported_control( control_defs[i].sc_oid,
255                         control_defs[i].sc_mask, control_defs[i].sc_extendedops,
256                         control_defs[i].sc_parse, cid );
257                 if ( rc != LDAP_SUCCESS ) break;
258         }
259
260         return rc;
261 }
262
263 /*
264  * Free memory associated with list of supported controls.
265  */
266 void
267 controls_destroy( void )
268 {
269         struct slap_control *sc;
270
271         while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
272                 sc = LDAP_SLIST_FIRST(&controls_list);
273                 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
274
275                 ch_free( sc->sc_oid );
276                 if ( sc->sc_extendedops != NULL ) {
277                         ldap_charray_free( sc->sc_extendedops );
278                 }
279                 ch_free( sc );
280         }
281 }
282
283 /*
284  * Format the supportedControl attribute of the root DSE,
285  * detailing which controls are supported by the directory
286  * server.
287  */
288 int
289 controls_root_dse_info( Entry *e )
290 {
291         AttributeDescription *ad_supportedControl
292                 = slap_schema.si_ad_supportedControl;
293         struct berval vals[2];
294         struct slap_control *sc;
295
296         vals[1].bv_val = NULL;
297         vals[1].bv_len = 0;
298
299         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
300                 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
301
302                 vals[0].bv_val = sc->sc_oid;
303                 vals[0].bv_len = strlen( sc->sc_oid );
304
305                 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
306                         return -1;
307                 }
308         }
309
310         return 0;
311 }
312
313 /*
314  * Return a list of OIDs and operation masks for supported
315  * controls. Used by SLAPI.
316  */
317 int
318 get_supported_controls(char ***ctrloidsp,
319         slap_mask_t **ctrlmasks)
320 {
321         int n;
322         char **oids;
323         slap_mask_t *masks;
324         struct slap_control *sc;
325
326         n = 0;
327
328         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
329                 n++;
330         }
331
332         if ( n == 0 ) {
333                 *ctrloidsp = NULL;
334                 *ctrlmasks = NULL;
335                 return LDAP_SUCCESS;
336         }
337
338         oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
339         if ( oids == NULL ) {
340                 return LDAP_NO_MEMORY;
341         }
342         masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
343         if  ( masks == NULL ) {
344                 ch_free( oids );
345                 return LDAP_NO_MEMORY;
346         }
347
348         n = 0;
349
350         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
351                 oids[n] = ch_strdup( sc->sc_oid );
352                 masks[n] = sc->sc_mask;
353                 n++;
354         }
355         oids[n] = NULL;
356         masks[n] = 0;
357
358         *ctrloidsp = oids;
359         *ctrlmasks = masks;
360
361         return LDAP_SUCCESS;
362 }
363
364 /*
365  * Find a control given its OID.
366  */
367 static struct slap_control *
368 find_ctrl( const char *oid )
369 {
370         struct slap_control *sc;
371
372         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
373                 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
374                         return sc;
375                 }
376         }
377
378         return NULL;
379 }
380
381 int
382 slap_find_control_id(
383         const char *oid,
384         int *cid )
385 {
386         struct slap_control *ctrl = find_ctrl( oid );
387         if ( ctrl ) {
388                 if ( cid ) *cid = ctrl->sc_cid;
389                 return LDAP_SUCCESS;
390         }
391         return LDAP_CONTROL_NOT_FOUND;
392 }
393
394 int
395 slap_global_control( Operation *op, const char *oid, int *cid )
396 {
397         struct slap_control *ctrl = find_ctrl( oid );
398
399         if ( ctrl == NULL ) {
400                 /* should not be reachable */
401                 Debug( LDAP_DEBUG_ANY,
402                         "slap_global_control: unrecognized control: %s\n",      
403                         oid, 0, 0 );
404                 return LDAP_CONTROL_NOT_FOUND;
405         }
406
407         if ( cid ) *cid = ctrl->sc_cid;
408
409         if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
410                 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
411                 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
412         {
413                 return LDAP_COMPARE_TRUE;
414         }
415
416         Debug( LDAP_DEBUG_TRACE,
417                 "slap_global_control: unavailable control: %s\n",      
418                 oid, 0, 0 );
419
420         return LDAP_COMPARE_FALSE;
421 }
422
423 void slap_free_ctrls(
424         Operation *op,
425         LDAPControl **ctrls )
426 {
427         int i;
428
429         for (i=0; ctrls[i]; i++) {
430                 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
431         }
432         op->o_tmpfree( ctrls, op->o_tmpmemctx );
433 }
434
435 int slap_parse_ctrl(
436         Operation *op,
437         SlapReply *rs,
438         LDAPControl *control,
439         const char **text )
440 {
441         struct slap_control *sc;
442
443         sc = find_ctrl( control->ldctl_oid );
444         if( sc != NULL ) {
445                 /* recognized control */
446                 slap_mask_t tagmask;
447                 switch( op->o_tag ) {
448                 case LDAP_REQ_ADD:
449                         tagmask = SLAP_CTRL_ADD;
450                         break;
451                 case LDAP_REQ_BIND:
452                         tagmask = SLAP_CTRL_BIND;
453                         break;
454                 case LDAP_REQ_COMPARE:
455                         tagmask = SLAP_CTRL_COMPARE;
456                         break;
457                 case LDAP_REQ_DELETE:
458                         tagmask = SLAP_CTRL_DELETE;
459                         break;
460                 case LDAP_REQ_MODIFY:
461                         tagmask = SLAP_CTRL_MODIFY;
462                         break;
463                 case LDAP_REQ_RENAME:
464                         tagmask = SLAP_CTRL_RENAME;
465                         break;
466                 case LDAP_REQ_SEARCH:
467                         tagmask = SLAP_CTRL_SEARCH;
468                         break;
469                 case LDAP_REQ_UNBIND:
470                         tagmask = SLAP_CTRL_UNBIND;
471                         break;
472                 case LDAP_REQ_ABANDON:
473                         tagmask = SLAP_CTRL_ABANDON;
474                         break;
475                 case LDAP_REQ_EXTENDED:
476                         tagmask=~0L;
477                         assert( op->ore_reqoid.bv_val != NULL );
478                         if( sc->sc_extendedops != NULL ) {
479                                 int i;
480                                 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
481                                         if( strcmp( op->ore_reqoid.bv_val,
482                                                 sc->sc_extendedops[i] ) == 0 )
483                                         {
484                                                 tagmask=0L;
485                                                 break;
486                                         }
487                                 }
488                         }
489                         break;
490                 default:
491                         *text = "controls internal error";
492                         return LDAP_OTHER;
493                 }
494
495                 if (( sc->sc_mask & tagmask ) == tagmask ) {
496                         /* available extension */
497                         int     rc;
498
499                         if( !sc->sc_parse ) {
500                                 *text = "not yet implemented";
501                                 return LDAP_OTHER;
502                         }
503
504                         rc = sc->sc_parse( op, rs, control );
505                         if ( rc ) {
506                                 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
507                                 return rc;
508                         }
509
510                 } else if( control->ldctl_iscritical ) {
511                         /* unavailable CRITICAL control */
512                         *text = "critical extension is unavailable";
513                         return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
514                 }
515         } else if( control->ldctl_iscritical ) {
516                 /* unrecognized CRITICAL control */
517                 *text = "critical extension is not recognized";
518                 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
519         }
520
521         return LDAP_SUCCESS;
522 }
523
524 int get_ctrls(
525         Operation *op,
526         SlapReply *rs,
527         int sendres )
528 {
529         int nctrls = 0;
530         ber_tag_t tag;
531         ber_len_t len;
532         char *opaque;
533         BerElement *ber = op->o_ber;
534         struct berval bv;
535
536         len = ber_pvt_ber_remaining(ber);
537
538         if( len == 0) {
539                 /* no controls */
540                 rs->sr_err = LDAP_SUCCESS;
541                 return rs->sr_err;
542         }
543
544         if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
545                 if( tag == LBER_ERROR ) {
546                         rs->sr_err = SLAPD_DISCONNECT;
547                         rs->sr_text = "unexpected data in PDU";
548                 }
549
550                 goto return_results;
551         }
552
553         Debug( LDAP_DEBUG_TRACE,
554                 "=> get_ctrls\n", 0, 0, 0 );
555
556         if( op->o_protocol < LDAP_VERSION3 ) {
557                 rs->sr_err = SLAPD_DISCONNECT;
558                 rs->sr_text = "controls require LDAPv3";
559                 goto return_results;
560         }
561
562         /* one for first control, one for termination */
563         op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
564
565 #if 0
566         if( op->ctrls == NULL ) {
567                 rs->sr_err = LDAP_NO_MEMORY;
568                 rs->sr_text = "no memory";
569                 goto return_results;
570         }
571 #endif
572
573         op->o_ctrls[nctrls] = NULL;
574
575         /* step through each element */
576         for( tag = ber_first_element( ber, &len, &opaque );
577                 tag != LBER_ERROR;
578                 tag = ber_next_element( ber, &len, opaque ) )
579         {
580                 LDAPControl *c;
581                 LDAPControl **tctrls;
582
583                 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
584                 memset(c, 0, sizeof(LDAPControl));
585
586                 /* allocate pointer space for current controls (nctrls)
587                  * + this control + extra NULL
588                  */
589                 tctrls = op->o_tmprealloc( op->o_ctrls,
590                         (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
591
592 #if 0
593                 if( tctrls == NULL ) {
594                         ch_free( c );
595                         ldap_controls_free(op->o_ctrls);
596                         op->o_ctrls = NULL;
597
598                         rs->sr_err = LDAP_NO_MEMORY;
599                         rs->sr_text = "no memory";
600                         goto return_results;
601                 }
602 #endif
603                 op->o_ctrls = tctrls;
604
605                 op->o_ctrls[nctrls++] = c;
606                 op->o_ctrls[nctrls] = NULL;
607
608                 tag = ber_scanf( ber, "{m" /*}*/, &bv );
609                 c->ldctl_oid = bv.bv_val;
610
611                 if( tag == LBER_ERROR ) {
612                         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
613                                 0, 0, 0 );
614
615                         slap_free_ctrls( op, op->o_ctrls );
616                         op->o_ctrls = NULL;
617                         rs->sr_err = SLAPD_DISCONNECT;
618                         rs->sr_text = "decoding controls error";
619                         goto return_results;
620
621                 } else if( c->ldctl_oid == NULL ) {
622                         Debug( LDAP_DEBUG_TRACE,
623                                 "get_ctrls: conn %lu got emtpy OID.\n",
624                                 op->o_connid, 0, 0 );
625
626                         slap_free_ctrls( op, op->o_ctrls );
627                         op->o_ctrls = NULL;
628                         rs->sr_err = LDAP_PROTOCOL_ERROR;
629                         rs->sr_text = "OID field is empty";
630                         goto return_results;
631                 }
632
633                 tag = ber_peek_tag( ber, &len );
634
635                 if( tag == LBER_BOOLEAN ) {
636                         ber_int_t crit;
637                         tag = ber_scanf( ber, "b", &crit );
638
639                         if( tag == LBER_ERROR ) {
640                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
641                                         0, 0, 0 );
642                                 slap_free_ctrls( op, op->o_ctrls );
643                                 op->o_ctrls = NULL;
644                                 rs->sr_err = SLAPD_DISCONNECT;
645                                 rs->sr_text = "decoding controls error";
646                                 goto return_results;
647                         }
648
649                         c->ldctl_iscritical = (crit != 0);
650                         tag = ber_peek_tag( ber, &len );
651                 }
652
653                 if( tag == LBER_OCTETSTRING ) {
654                         tag = ber_scanf( ber, "m", &c->ldctl_value );
655
656                         if( tag == LBER_ERROR ) {
657                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
658                                         "%s (%scritical): get value failed.\n",
659                                         op->o_connid, c->ldctl_oid,
660                                         c->ldctl_iscritical ? "" : "non" );
661                                 slap_free_ctrls( op, op->o_ctrls );
662                                 op->o_ctrls = NULL;
663                                 rs->sr_err = SLAPD_DISCONNECT;
664                                 rs->sr_text = "decoding controls error";
665                                 goto return_results;
666                         }
667                 }
668
669                 Debug( LDAP_DEBUG_TRACE,
670                         "=> get_ctrls: oid=\"%s\" (%scritical)\n",
671                         c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
672
673                 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
674                 if ( rs->sr_err != LDAP_SUCCESS ) {
675                         goto return_results;
676                 }
677         }
678
679 return_results:
680         Debug( LDAP_DEBUG_TRACE,
681                 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
682                 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
683
684         if( sendres && rs->sr_err != LDAP_SUCCESS ) {
685                 if( rs->sr_err == SLAPD_DISCONNECT ) {
686                         rs->sr_err = LDAP_PROTOCOL_ERROR;
687                         send_ldap_disconnect( op, rs );
688                         rs->sr_err = SLAPD_DISCONNECT;
689                 } else {
690                         send_ldap_result( op, rs );
691                 }
692         }
693
694         return rs->sr_err;
695 }
696
697 int
698 slap_remove_control(
699         Operation       *op,
700         SlapReply       *rs,
701         int             ctrl,
702         BI_chk_controls fnc )
703 {
704         int             i, j;
705
706         switch ( op->o_ctrlflag[ ctrl ] ) {
707         case SLAP_CONTROL_NONCRITICAL:
708                 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
709                         if ( strcmp( op->o_ctrls[ i ]->ldctl_oid, slap_known_controls[ ctrl - 1 ] ) == 0 )
710                         {
711                                 j = i;
712                         }
713                 }
714
715                 if ( j == -1 ) {
716                         rs->sr_err = LDAP_OTHER;
717                         break;
718                 }
719
720                 if ( fnc ) {
721                         (void)fnc( op, rs );
722                 }
723
724                 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
725
726                 if ( i > 1 ) {
727                         AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
728                                 ( i - j ) * sizeof( LDAPControl * ) );
729
730                 } else {
731                         op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
732                         op->o_ctrls = NULL;
733                 }
734
735                 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
736
737                 Debug( LDAP_DEBUG_ANY, "%s: "
738                         "non-critical control \"%s\" not supported; stripped.\n",
739                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
740                 /* fall thru */
741
742         case SLAP_CONTROL_IGNORED:
743         case SLAP_CONTROL_NONE:
744                 rs->sr_err = SLAP_CB_CONTINUE;
745                 break;
746
747         case SLAP_CONTROL_CRITICAL:
748                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
749                 if ( fnc ) {
750                         (void)fnc( op, rs );
751                 }
752                 Debug( LDAP_DEBUG_ANY, "%s: "
753                         "critical control \"%s\" not supported.\n",
754                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
755                 break;
756
757         default:
758                 /* handle all cases! */
759                 assert( 0 );
760         }
761
762         return rs->sr_err;
763 }
764
765 #ifdef LDAP_DEVEL
766 static int parseManageDIT (
767         Operation *op,
768         SlapReply *rs,
769         LDAPControl *ctrl )
770 {
771         if ( op->o_managedit != SLAP_CONTROL_NONE ) {
772                 rs->sr_text = "manageDIT control specified multiple times";
773                 return LDAP_PROTOCOL_ERROR;
774         }
775
776         if ( ctrl->ldctl_value.bv_len ) {
777                 rs->sr_text = "manageDIT control value not empty";
778                 return LDAP_PROTOCOL_ERROR;
779         }
780
781         op->o_managedit = ctrl->ldctl_iscritical
782                 ? SLAP_CONTROL_CRITICAL
783                 : SLAP_CONTROL_NONCRITICAL;
784
785         return LDAP_SUCCESS;
786 }
787 #endif
788
789 static int parseManageDSAit (
790         Operation *op,
791         SlapReply *rs,
792         LDAPControl *ctrl )
793 {
794         if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
795                 rs->sr_text = "manageDSAit control specified multiple times";
796                 return LDAP_PROTOCOL_ERROR;
797         }
798
799         if ( ctrl->ldctl_value.bv_len ) {
800                 rs->sr_text = "manageDSAit control value not empty";
801                 return LDAP_PROTOCOL_ERROR;
802         }
803
804         op->o_managedsait = ctrl->ldctl_iscritical
805                 ? SLAP_CONTROL_CRITICAL
806                 : SLAP_CONTROL_NONCRITICAL;
807
808         return LDAP_SUCCESS;
809 }
810
811 static int parseProxyAuthz (
812         Operation *op,
813         SlapReply *rs,
814         LDAPControl *ctrl )
815 {
816         int             rc;
817         struct berval   dn = BER_BVNULL;
818
819         if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
820                 rs->sr_text = "proxy authorization control specified multiple times";
821                 return LDAP_PROTOCOL_ERROR;
822         }
823
824         op->o_proxy_authz = ctrl->ldctl_iscritical
825                 ? SLAP_CONTROL_CRITICAL
826                 : SLAP_CONTROL_NONCRITICAL;
827
828         Debug( LDAP_DEBUG_ARGS,
829                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
830                 op->o_connid,
831                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
832                 0 );
833
834         if ( ctrl->ldctl_value.bv_len == 0 ) {
835                 Debug( LDAP_DEBUG_TRACE,
836                         "parseProxyAuthz: conn=%lu anonymous\n", 
837                         op->o_connid, 0, 0 );
838
839                 /* anonymous */
840                 op->o_ndn.bv_val[ 0 ] = '\0';
841                 op->o_ndn.bv_len = 0;
842
843                 op->o_dn.bv_val[ 0 ] = '\0';
844                 op->o_dn.bv_len = 0;
845
846                 return LDAP_SUCCESS;
847         }
848
849         rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
850                         NULL, &dn, SLAP_GETDN_AUTHZID );
851
852         /* FIXME: empty DN in proxyAuthz control should be legal... */
853         if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
854                 if ( dn.bv_val ) {
855                         ch_free( dn.bv_val );
856                 }
857                 rs->sr_text = "authzId mapping failed";
858                 return LDAP_PROXY_AUTHZ_FAILURE;
859         }
860
861         Debug( LDAP_DEBUG_TRACE,
862                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
863                 op->o_connid,
864                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
865
866         rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
867
868         if ( rc ) {
869                 ch_free( dn.bv_val );
870                 rs->sr_text = "not authorized to assume identity";
871                 return LDAP_PROXY_AUTHZ_FAILURE;
872         }
873
874         ch_free( op->o_ndn.bv_val );
875         ch_free( op->o_dn.bv_val );
876
877         /*
878          * NOTE: since slap_sasl_getdn() returns a normalized dn,
879          * from now on op->o_dn is normalized
880          */
881         op->o_ndn = dn;
882         ber_dupbv( &op->o_dn, &dn );
883
884
885         Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
886             op->o_log_prefix, dn.bv_val, 0, 0, 0 );
887
888         return LDAP_SUCCESS;
889 }
890
891 static int parseNoOp (
892         Operation *op,
893         SlapReply *rs,
894         LDAPControl *ctrl )
895 {
896         if ( op->o_noop != SLAP_CONTROL_NONE ) {
897                 rs->sr_text = "noop control specified multiple times";
898                 return LDAP_PROTOCOL_ERROR;
899         }
900
901         if ( ctrl->ldctl_value.bv_len ) {
902                 rs->sr_text = "noop control value not empty";
903                 return LDAP_PROTOCOL_ERROR;
904         }
905
906         op->o_noop = ctrl->ldctl_iscritical
907                 ? SLAP_CONTROL_CRITICAL
908                 : SLAP_CONTROL_NONCRITICAL;
909
910         return LDAP_SUCCESS;
911 }
912
913 static int parsePagedResults (
914         Operation *op,
915         SlapReply *rs,
916         LDAPControl *ctrl )
917 {
918         int             rc = LDAP_SUCCESS;
919         ber_tag_t       tag;
920         ber_int_t       size;
921         BerElement      *ber;
922         struct berval   cookie = BER_BVNULL;
923         PagedResultsState       *ps;
924
925         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
926                 rs->sr_text = "paged results control specified multiple times";
927                 return LDAP_PROTOCOL_ERROR;
928         }
929
930         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
931                 rs->sr_text = "paged results control value is empty (or absent)";
932                 return LDAP_PROTOCOL_ERROR;
933         }
934
935         /* Parse the control value
936          *      realSearchControlValue ::= SEQUENCE {
937          *              size    INTEGER (0..maxInt),
938          *                              -- requested page size from client
939          *                              -- result set size estimate from server
940          *              cookie  OCTET STRING
941          * }
942          */
943         ber = ber_init( &ctrl->ldctl_value );
944         if ( ber == NULL ) {
945                 rs->sr_text = "internal error";
946                 return LDAP_OTHER;
947         }
948
949         tag = ber_scanf( ber, "{im}", &size, &cookie );
950
951         if ( tag == LBER_ERROR ) {
952                 rs->sr_text = "paged results control could not be decoded";
953                 rc = LDAP_PROTOCOL_ERROR;
954                 goto done;
955         }
956
957         if ( size < 0 ) {
958                 rs->sr_text = "paged results control size invalid";
959                 rc = LDAP_PROTOCOL_ERROR;
960                 goto done;
961         }
962
963         ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
964         *ps = op->o_conn->c_pagedresults_state;
965         ps->ps_size = size;
966         op->o_pagedresults_state = ps;
967
968         /* NOTE: according to RFC 2696 3.:
969
970     If the page size is greater than or equal to the sizeLimit value, the
971     server should ignore the control as the request can be satisfied in a
972     single page.
973          
974          * NOTE: this assumes that the op->ors_slimit be set
975          * before the controls are parsed.     
976          */
977                 
978         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
979                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
980
981         } else if ( ctrl->ldctl_iscritical ) {
982                 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
983
984         } else {
985                 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
986         }
987
988 done:;
989         (void)ber_free( ber, 1 );
990         return rc;
991 }
992
993 #ifdef LDAP_DEVEL
994 static int parseSortedResults (
995         Operation *op,
996         SlapReply *rs,
997         LDAPControl *ctrl )
998 {
999         int             rc = LDAP_SUCCESS;
1000
1001         if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1002                 rs->sr_text = "sorted results control specified multiple times";
1003                 return LDAP_PROTOCOL_ERROR;
1004         }
1005
1006         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1007                 rs->sr_text = "sorted results control value is empty (or absent)";
1008                 return LDAP_PROTOCOL_ERROR;
1009         }
1010
1011         /* blow off parsing the value */
1012
1013         op->o_sortedresults = ctrl->ldctl_iscritical
1014                 ? SLAP_CONTROL_CRITICAL
1015                 : SLAP_CONTROL_NONCRITICAL;
1016
1017         return rc;
1018 }
1019 #endif
1020
1021 static int parseAssert (
1022         Operation *op,
1023         SlapReply *rs,
1024         LDAPControl *ctrl )
1025 {
1026         BerElement      *ber;
1027         struct berval   fstr = BER_BVNULL;
1028
1029         if ( op->o_assert != SLAP_CONTROL_NONE ) {
1030                 rs->sr_text = "assert control specified multiple times";
1031                 return LDAP_PROTOCOL_ERROR;
1032         }
1033
1034         if ( ctrl->ldctl_value.bv_len == 0 ) {
1035                 rs->sr_text = "assert control value is empty (or absent)";
1036                 return LDAP_PROTOCOL_ERROR;
1037         }
1038
1039         ber = ber_init( &(ctrl->ldctl_value) );
1040         if (ber == NULL) {
1041                 rs->sr_text = "assert control: internal error";
1042                 return LDAP_OTHER;
1043         }
1044         
1045         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
1046
1047         if( rs->sr_err != LDAP_SUCCESS ) {
1048                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1049                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1050                         send_ldap_disconnect( op, rs );
1051                         rs->sr_err = SLAPD_DISCONNECT;
1052                 } else {
1053                         send_ldap_result( op, rs );
1054                 }
1055                 if( op->o_assertion != NULL ) {
1056                         filter_free_x( op, op->o_assertion );
1057                 }
1058                 return rs->sr_err;
1059         }
1060
1061 #ifdef LDAP_DEBUG
1062         filter2bv_x( op, op->o_assertion, &fstr );
1063
1064         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1065                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1066         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1067 #endif
1068
1069         op->o_assert = ctrl->ldctl_iscritical
1070                 ? SLAP_CONTROL_CRITICAL
1071                 : SLAP_CONTROL_NONCRITICAL;
1072
1073         rs->sr_err = LDAP_SUCCESS;
1074         return LDAP_SUCCESS;
1075 }
1076
1077 static int parsePreRead (
1078         Operation *op,
1079         SlapReply *rs,
1080         LDAPControl *ctrl )
1081 {
1082         ber_len_t siz, off, i;
1083         AttributeName *an = NULL;
1084         BerElement      *ber;
1085
1086         if ( op->o_preread != SLAP_CONTROL_NONE ) {
1087                 rs->sr_text = "preread control specified multiple times";
1088                 return LDAP_PROTOCOL_ERROR;
1089         }
1090
1091         if ( ctrl->ldctl_value.bv_len == 0 ) {
1092                 rs->sr_text = "preread control value is empty (or absent)";
1093                 return LDAP_PROTOCOL_ERROR;
1094         }
1095
1096         ber = ber_init( &(ctrl->ldctl_value) );
1097         if (ber == NULL) {
1098                 rs->sr_text = "preread control: internal error";
1099                 return LDAP_OTHER;
1100         }
1101
1102         siz = sizeof( AttributeName );
1103         off = offsetof( AttributeName, an_name );
1104         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1105                 rs->sr_text = "preread control: decoding error";
1106                 return LDAP_PROTOCOL_ERROR;
1107         }
1108
1109         for( i=0; i<siz; i++ ) {
1110                 int             rc = LDAP_SUCCESS;
1111                 const char      *dummy = NULL;
1112
1113                 an[i].an_desc = NULL;
1114                 an[i].an_oc = NULL;
1115                 an[i].an_oc_exclude = 0;
1116                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1117                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1118                         rs->sr_text = dummy
1119                                 ? dummy
1120                                 : "postread control: unknown attributeType";
1121                         return rc;
1122                 }
1123         }
1124
1125         op->o_preread = ctrl->ldctl_iscritical
1126                 ? SLAP_CONTROL_CRITICAL
1127                 : SLAP_CONTROL_NONCRITICAL;
1128
1129         op->o_preread_attrs = an;
1130
1131         rs->sr_err = LDAP_SUCCESS;
1132         return LDAP_SUCCESS;
1133 }
1134
1135 static int parsePostRead (
1136         Operation *op,
1137         SlapReply *rs,
1138         LDAPControl *ctrl )
1139 {
1140         ber_len_t siz, off, i;
1141         AttributeName *an = NULL;
1142         BerElement      *ber;
1143
1144         if ( op->o_postread != SLAP_CONTROL_NONE ) {
1145                 rs->sr_text = "postread control specified multiple times";
1146                 return LDAP_PROTOCOL_ERROR;
1147         }
1148
1149         if ( ctrl->ldctl_value.bv_len == 0 ) {
1150                 rs->sr_text = "postread control value is empty (or absent)";
1151                 return LDAP_PROTOCOL_ERROR;
1152         }
1153
1154         ber = ber_init( &(ctrl->ldctl_value) );
1155         if (ber == NULL) {
1156                 rs->sr_text = "postread control: internal error";
1157                 return LDAP_OTHER;
1158         }
1159
1160         siz = sizeof( AttributeName );
1161         off = offsetof( AttributeName, an_name );
1162         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1163                 rs->sr_text = "postread control: decoding error";
1164                 return LDAP_PROTOCOL_ERROR;
1165         }
1166
1167         for( i=0; i<siz; i++ ) {
1168                 int             rc = LDAP_SUCCESS;
1169                 const char      *dummy = NULL;
1170
1171                 an[i].an_desc = NULL;
1172                 an[i].an_oc = NULL;
1173                 an[i].an_oc_exclude = 0;
1174                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1175                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1176                         rs->sr_text = dummy
1177                                 ? dummy
1178                                 : "postread control: unknown attributeType";
1179                         return rc;
1180                 }
1181         }
1182
1183         op->o_postread = ctrl->ldctl_iscritical
1184                 ? SLAP_CONTROL_CRITICAL
1185                 : SLAP_CONTROL_NONCRITICAL;
1186
1187         op->o_postread_attrs = an;
1188
1189         rs->sr_err = LDAP_SUCCESS;
1190         return LDAP_SUCCESS;
1191 }
1192
1193 static int parseValuesReturnFilter (
1194         Operation *op,
1195         SlapReply *rs,
1196         LDAPControl *ctrl )
1197 {
1198         BerElement      *ber;
1199         struct berval   fstr = BER_BVNULL;
1200
1201         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1202                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1203                 return LDAP_PROTOCOL_ERROR;
1204         }
1205
1206         if ( ctrl->ldctl_value.bv_len == 0 ) {
1207                 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1208                 return LDAP_PROTOCOL_ERROR;
1209         }
1210
1211         ber = ber_init( &(ctrl->ldctl_value) );
1212         if (ber == NULL) {
1213                 rs->sr_text = "internal error";
1214                 return LDAP_OTHER;
1215         }
1216         
1217         rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1218
1219         if( rs->sr_err != LDAP_SUCCESS ) {
1220                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1221                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1222                         send_ldap_disconnect( op, rs );
1223                         rs->sr_err = SLAPD_DISCONNECT;
1224                 } else {
1225                         send_ldap_result( op, rs );
1226                 }
1227                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1228         }
1229 #ifdef LDAP_DEBUG
1230         else {
1231                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1232         }
1233
1234         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1235                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1236         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1237 #endif
1238
1239         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1240                 ? SLAP_CONTROL_CRITICAL
1241                 : SLAP_CONTROL_NONCRITICAL;
1242
1243         rs->sr_err = LDAP_SUCCESS;
1244         return LDAP_SUCCESS;
1245 }
1246
1247 #ifdef LDAP_CONTROL_SUBENTRIES
1248 static int parseSubentries (
1249         Operation *op,
1250         SlapReply *rs,
1251         LDAPControl *ctrl )
1252 {
1253         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1254                 rs->sr_text = "subentries control specified multiple times";
1255                 return LDAP_PROTOCOL_ERROR;
1256         }
1257
1258         /* FIXME: should use BER library */
1259         if( ( ctrl->ldctl_value.bv_len != 3 )
1260                 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1261                 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1262         {
1263                 rs->sr_text = "subentries control value encoding is bogus";
1264                 return LDAP_PROTOCOL_ERROR;
1265         }
1266
1267         op->o_subentries = ctrl->ldctl_iscritical
1268                 ? SLAP_CONTROL_CRITICAL
1269                 : SLAP_CONTROL_NONCRITICAL;
1270
1271         if (ctrl->ldctl_value.bv_val[2]) {
1272                 set_subentries_visibility( op );
1273         }
1274
1275         return LDAP_SUCCESS;
1276 }
1277 #endif
1278
1279 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1280 static int parsePermissiveModify (
1281         Operation *op,
1282         SlapReply *rs,
1283         LDAPControl *ctrl )
1284 {
1285         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1286                 rs->sr_text = "permissiveModify control specified multiple times";
1287                 return LDAP_PROTOCOL_ERROR;
1288         }
1289
1290         if ( ctrl->ldctl_value.bv_len ) {
1291                 rs->sr_text = "permissiveModify control value not empty";
1292                 return LDAP_PROTOCOL_ERROR;
1293         }
1294
1295         op->o_permissive_modify = ctrl->ldctl_iscritical
1296                 ? SLAP_CONTROL_CRITICAL
1297                 : SLAP_CONTROL_NONCRITICAL;
1298
1299         return LDAP_SUCCESS;
1300 }
1301 #endif
1302
1303 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1304 static int parseDomainScope (
1305         Operation *op,
1306         SlapReply *rs,
1307         LDAPControl *ctrl )
1308 {
1309         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1310                 rs->sr_text = "domainScope control specified multiple times";
1311                 return LDAP_PROTOCOL_ERROR;
1312         }
1313
1314         if ( ctrl->ldctl_value.bv_len ) {
1315                 rs->sr_text = "domainScope control value not empty";
1316                 return LDAP_PROTOCOL_ERROR;
1317         }
1318
1319         op->o_domain_scope = ctrl->ldctl_iscritical
1320                 ? SLAP_CONTROL_CRITICAL
1321                 : SLAP_CONTROL_NONCRITICAL;
1322
1323         return LDAP_SUCCESS;
1324 }
1325 #endif
1326
1327 #ifdef SLAP_CONTROL_X_TREE_DELETE
1328 static int parseTreeDelete (
1329         Operation *op,
1330         SlapReply *rs,
1331         LDAPControl *ctrl )
1332 {
1333         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1334                 rs->sr_text = "treeDelete control specified multiple times";
1335                 return LDAP_PROTOCOL_ERROR;
1336         }
1337
1338         if ( ctrl->ldctl_value.bv_len ) {
1339                 rs->sr_text = "treeDelete control value not empty";
1340                 return LDAP_PROTOCOL_ERROR;
1341         }
1342
1343         op->o_tree_delete = ctrl->ldctl_iscritical
1344                 ? SLAP_CONTROL_CRITICAL
1345                 : SLAP_CONTROL_NONCRITICAL;
1346
1347         return LDAP_SUCCESS;
1348 }
1349 #endif
1350
1351 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
1352 static int parseSearchOptions (
1353         Operation *op,
1354         SlapReply *rs,
1355         LDAPControl *ctrl )
1356 {
1357         BerElement *ber;
1358         ber_int_t search_flags;
1359         ber_tag_t tag;
1360
1361         if ( ctrl->ldctl_value.bv_len == 0 ) {
1362                 rs->sr_text = "searchOptions control value is empty (or absent)";
1363                 return LDAP_PROTOCOL_ERROR;
1364         }
1365
1366         ber = ber_init( &ctrl->ldctl_value );
1367         if( ber == NULL ) {
1368                 rs->sr_text = "internal error";
1369                 return LDAP_OTHER;
1370         }
1371
1372         if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1373                 rs->sr_text = "searchOptions control decoding error";
1374                 return LDAP_PROTOCOL_ERROR;
1375         }
1376
1377         (void) ber_free( ber, 1 );
1378
1379         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1380                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1381                         rs->sr_text = "searchOptions control specified multiple times "
1382                                 "or with domainScope control";
1383                         return LDAP_PROTOCOL_ERROR;
1384                 }
1385
1386                 op->o_domain_scope = ctrl->ldctl_iscritical
1387                         ? SLAP_CONTROL_CRITICAL
1388                         : SLAP_CONTROL_NONCRITICAL;
1389         }
1390
1391         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1392                 /* Other search flags not recognised so far,
1393                  * including:
1394                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOM
1395                  */
1396                 rs->sr_text = "searchOptions contained unrecognized flag";
1397                 return LDAP_UNWILLING_TO_PERFORM;
1398         }
1399
1400         return LDAP_SUCCESS;
1401 }
1402 #endif
1403