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