]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
cleanup objectclass inheritance; don't proceed if initial lookup resulted in a hit...
[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, 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 #ifdef LDAP_DEVEL
698 static int parseManageDIT (
699         Operation *op,
700         SlapReply *rs,
701         LDAPControl *ctrl )
702 {
703         if ( op->o_managedit != SLAP_CONTROL_NONE ) {
704                 rs->sr_text = "manageDIT control specified multiple times";
705                 return LDAP_PROTOCOL_ERROR;
706         }
707
708         if ( ctrl->ldctl_value.bv_len ) {
709                 rs->sr_text = "manageDIT control value not empty";
710                 return LDAP_PROTOCOL_ERROR;
711         }
712
713         op->o_managedit = ctrl->ldctl_iscritical
714                 ? SLAP_CONTROL_CRITICAL
715                 : SLAP_CONTROL_NONCRITICAL;
716
717         return LDAP_SUCCESS;
718 }
719 #endif
720
721 static int parseManageDSAit (
722         Operation *op,
723         SlapReply *rs,
724         LDAPControl *ctrl )
725 {
726         if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
727                 rs->sr_text = "manageDSAit control specified multiple times";
728                 return LDAP_PROTOCOL_ERROR;
729         }
730
731         if ( ctrl->ldctl_value.bv_len ) {
732                 rs->sr_text = "manageDSAit control value not empty";
733                 return LDAP_PROTOCOL_ERROR;
734         }
735
736         op->o_managedsait = ctrl->ldctl_iscritical
737                 ? SLAP_CONTROL_CRITICAL
738                 : SLAP_CONTROL_NONCRITICAL;
739
740         return LDAP_SUCCESS;
741 }
742
743 static int parseProxyAuthz (
744         Operation *op,
745         SlapReply *rs,
746         LDAPControl *ctrl )
747 {
748         int             rc;
749         struct berval   dn = BER_BVNULL;
750
751         if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
752                 rs->sr_text = "proxy authorization control specified multiple times";
753                 return LDAP_PROTOCOL_ERROR;
754         }
755
756         op->o_proxy_authz = ctrl->ldctl_iscritical
757                 ? SLAP_CONTROL_CRITICAL
758                 : SLAP_CONTROL_NONCRITICAL;
759
760         Debug( LDAP_DEBUG_ARGS,
761                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
762                 op->o_connid,
763                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
764                 0 );
765
766         if ( ctrl->ldctl_value.bv_len == 0 ) {
767                 Debug( LDAP_DEBUG_TRACE,
768                         "parseProxyAuthz: conn=%lu anonymous\n", 
769                         op->o_connid, 0, 0 );
770
771                 /* anonymous */
772                 op->o_ndn.bv_val[ 0 ] = '\0';
773                 op->o_ndn.bv_len = 0;
774
775                 op->o_dn.bv_val[ 0 ] = '\0';
776                 op->o_dn.bv_len = 0;
777
778                 return LDAP_SUCCESS;
779         }
780
781         rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
782                         NULL, &dn, SLAP_GETDN_AUTHZID );
783
784         /* FIXME: empty DN in proxyAuthz control should be legal... */
785         if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
786                 if ( dn.bv_val ) {
787                         ch_free( dn.bv_val );
788                 }
789                 rs->sr_text = "authzId mapping failed";
790                 return LDAP_PROXY_AUTHZ_FAILURE;
791         }
792
793         Debug( LDAP_DEBUG_TRACE,
794                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
795                 op->o_connid,
796                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
797
798         rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
799
800         if ( rc ) {
801                 ch_free( dn.bv_val );
802                 rs->sr_text = "not authorized to assume identity";
803                 return LDAP_PROXY_AUTHZ_FAILURE;
804         }
805
806         ch_free( op->o_ndn.bv_val );
807         ch_free( op->o_dn.bv_val );
808
809         /*
810          * NOTE: since slap_sasl_getdn() returns a normalized dn,
811          * from now on op->o_dn is normalized
812          */
813         op->o_ndn = dn;
814         ber_dupbv( &op->o_dn, &dn );
815
816
817         Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
818             op->o_log_prefix, dn.bv_val, 0, 0, 0 );
819
820         return LDAP_SUCCESS;
821 }
822
823 static int parseNoOp (
824         Operation *op,
825         SlapReply *rs,
826         LDAPControl *ctrl )
827 {
828         if ( op->o_noop != SLAP_CONTROL_NONE ) {
829                 rs->sr_text = "noop control specified multiple times";
830                 return LDAP_PROTOCOL_ERROR;
831         }
832
833         if ( ctrl->ldctl_value.bv_len ) {
834                 rs->sr_text = "noop control value not empty";
835                 return LDAP_PROTOCOL_ERROR;
836         }
837
838         op->o_noop = ctrl->ldctl_iscritical
839                 ? SLAP_CONTROL_CRITICAL
840                 : SLAP_CONTROL_NONCRITICAL;
841
842         return LDAP_SUCCESS;
843 }
844
845 static int parsePagedResults (
846         Operation *op,
847         SlapReply *rs,
848         LDAPControl *ctrl )
849 {
850         int             rc = LDAP_SUCCESS;
851         ber_tag_t       tag;
852         ber_int_t       size;
853         BerElement      *ber;
854         struct berval   cookie = BER_BVNULL;
855         PagedResultsState       *ps;
856
857         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
858                 rs->sr_text = "paged results control specified multiple times";
859                 return LDAP_PROTOCOL_ERROR;
860         }
861
862         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
863                 rs->sr_text = "paged results control value is empty (or absent)";
864                 return LDAP_PROTOCOL_ERROR;
865         }
866
867         /* Parse the control value
868          *      realSearchControlValue ::= SEQUENCE {
869          *              size    INTEGER (0..maxInt),
870          *                              -- requested page size from client
871          *                              -- result set size estimate from server
872          *              cookie  OCTET STRING
873          * }
874          */
875         ber = ber_init( &ctrl->ldctl_value );
876         if ( ber == NULL ) {
877                 rs->sr_text = "internal error";
878                 return LDAP_OTHER;
879         }
880
881         tag = ber_scanf( ber, "{im}", &size, &cookie );
882
883         if ( tag == LBER_ERROR ) {
884                 rs->sr_text = "paged results control could not be decoded";
885                 rc = LDAP_PROTOCOL_ERROR;
886                 goto done;
887         }
888
889         if ( size < 0 ) {
890                 rs->sr_text = "paged results control size invalid";
891                 rc = LDAP_PROTOCOL_ERROR;
892                 goto done;
893         }
894
895 #if 0
896         /* defer cookie decoding/checks to backend... */
897         if ( cookie.bv_len ) {
898                 PagedResultsCookie reqcookie;
899                 if( cookie.bv_len != sizeof( reqcookie ) ) {
900                         /* bad cookie */
901                         rs->sr_text = "paged results cookie is invalid";
902                         rc = LDAP_PROTOCOL_ERROR;
903                         goto done;
904                 }
905
906                 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
907
908                 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
909                         /* bad cookie */
910                         rs->sr_text = "paged results cookie is invalid";
911                         rc = LDAP_PROTOCOL_ERROR;
912                         goto done;
913
914                 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
915                         rs->sr_text = "paged results cookie is invalid or old";
916                         rc = LDAP_UNWILLING_TO_PERFORM;
917                         goto done;
918                 }
919
920         } else {
921                 /* Initial request.  Initialize state. */
922 #if 0
923                 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
924                         /* There's another pagedResults control on the
925                          * same connection; reject new pagedResults controls 
926                          * (allowed by RFC2696) */
927                         rs->sr_text = "paged results cookie unavailable; try later";
928                         rc = LDAP_UNWILLING_TO_PERFORM;
929                         goto done;
930                 }
931 #endif
932                 op->o_pagedresults_state.ps_cookie = 0;
933                 op->o_pagedresults_state.ps_count = 0;
934         }
935 #endif
936
937         ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
938         *ps = op->o_conn->c_pagedresults_state;
939         ps->ps_size = size;
940         op->o_pagedresults_state = ps;
941
942         /* NOTE: according to RFC 2696 3.:
943
944     If the page size is greater than or equal to the sizeLimit value, the
945     server should ignore the control as the request can be satisfied in a
946     single page.
947          
948          * NOTE: this assumes that the op->ors_slimit be set
949          * before the controls are parsed.     
950          */
951                 
952         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
953                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
954
955         } else if ( ctrl->ldctl_iscritical ) {
956                 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
957
958         } else {
959                 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
960         }
961
962 done:;
963         (void)ber_free( ber, 1 );
964         return rc;
965 }
966
967 #ifdef LDAP_DEVEL
968 static int parseSortedResults (
969         Operation *op,
970         SlapReply *rs,
971         LDAPControl *ctrl )
972 {
973         int             rc = LDAP_SUCCESS;
974
975         if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
976                 rs->sr_text = "sorted results control specified multiple times";
977                 return LDAP_PROTOCOL_ERROR;
978         }
979
980         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
981                 rs->sr_text = "sorted results control value is empty (or absent)";
982                 return LDAP_PROTOCOL_ERROR;
983         }
984
985         /* blow off parsing the value */
986
987         op->o_sortedresults = ctrl->ldctl_iscritical
988                 ? SLAP_CONTROL_CRITICAL
989                 : SLAP_CONTROL_NONCRITICAL;
990
991         return rc;
992 }
993 #endif
994
995 static int parseAssert (
996         Operation *op,
997         SlapReply *rs,
998         LDAPControl *ctrl )
999 {
1000         BerElement      *ber;
1001         struct berval   fstr = BER_BVNULL;
1002
1003         if ( op->o_assert != SLAP_CONTROL_NONE ) {
1004                 rs->sr_text = "assert control specified multiple times";
1005                 return LDAP_PROTOCOL_ERROR;
1006         }
1007
1008         if ( ctrl->ldctl_value.bv_len == 0 ) {
1009                 rs->sr_text = "assert control value is empty (or absent)";
1010                 return LDAP_PROTOCOL_ERROR;
1011         }
1012
1013         ber = ber_init( &(ctrl->ldctl_value) );
1014         if (ber == NULL) {
1015                 rs->sr_text = "assert control: internal error";
1016                 return LDAP_OTHER;
1017         }
1018         
1019         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
1020
1021         if( rs->sr_err != LDAP_SUCCESS ) {
1022                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1023                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1024                         send_ldap_disconnect( op, rs );
1025                         rs->sr_err = SLAPD_DISCONNECT;
1026                 } else {
1027                         send_ldap_result( op, rs );
1028                 }
1029                 if( op->o_assertion != NULL ) {
1030                         filter_free_x( op, op->o_assertion );
1031                 }
1032                 return rs->sr_err;
1033         }
1034
1035 #ifdef LDAP_DEBUG
1036         filter2bv_x( op, op->o_assertion, &fstr );
1037
1038         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1039                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1040         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1041 #endif
1042
1043         op->o_assert = ctrl->ldctl_iscritical
1044                 ? SLAP_CONTROL_CRITICAL
1045                 : SLAP_CONTROL_NONCRITICAL;
1046
1047         rs->sr_err = LDAP_SUCCESS;
1048         return LDAP_SUCCESS;
1049 }
1050
1051 static int parsePreRead (
1052         Operation *op,
1053         SlapReply *rs,
1054         LDAPControl *ctrl )
1055 {
1056         ber_len_t siz, off, i;
1057         AttributeName *an = NULL;
1058         BerElement      *ber;
1059
1060         if ( op->o_preread != SLAP_CONTROL_NONE ) {
1061                 rs->sr_text = "preread control specified multiple times";
1062                 return LDAP_PROTOCOL_ERROR;
1063         }
1064
1065         if ( ctrl->ldctl_value.bv_len == 0 ) {
1066                 rs->sr_text = "preread control value is empty (or absent)";
1067                 return LDAP_PROTOCOL_ERROR;
1068         }
1069
1070         ber = ber_init( &(ctrl->ldctl_value) );
1071         if (ber == NULL) {
1072                 rs->sr_text = "preread control: internal error";
1073                 return LDAP_OTHER;
1074         }
1075
1076         siz = sizeof( AttributeName );
1077         off = offsetof( AttributeName, an_name );
1078         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1079                 rs->sr_text = "preread control: decoding error";
1080                 return LDAP_PROTOCOL_ERROR;
1081         }
1082
1083         for( i=0; i<siz; i++ ) {
1084                 int             rc = LDAP_SUCCESS;
1085                 const char      *dummy = NULL;
1086
1087                 an[i].an_desc = NULL;
1088                 an[i].an_oc = NULL;
1089                 an[i].an_oc_exclude = 0;
1090                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1091                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1092                         rs->sr_text = dummy
1093                                 ? dummy
1094                                 : "postread control: unknown attributeType";
1095                         return rc;
1096                 }
1097         }
1098
1099         op->o_preread = ctrl->ldctl_iscritical
1100                 ? SLAP_CONTROL_CRITICAL
1101                 : SLAP_CONTROL_NONCRITICAL;
1102
1103         op->o_preread_attrs = an;
1104
1105         rs->sr_err = LDAP_SUCCESS;
1106         return LDAP_SUCCESS;
1107 }
1108
1109 static int parsePostRead (
1110         Operation *op,
1111         SlapReply *rs,
1112         LDAPControl *ctrl )
1113 {
1114         ber_len_t siz, off, i;
1115         AttributeName *an = NULL;
1116         BerElement      *ber;
1117
1118         if ( op->o_postread != SLAP_CONTROL_NONE ) {
1119                 rs->sr_text = "postread control specified multiple times";
1120                 return LDAP_PROTOCOL_ERROR;
1121         }
1122
1123         if ( ctrl->ldctl_value.bv_len == 0 ) {
1124                 rs->sr_text = "postread control value is empty (or absent)";
1125                 return LDAP_PROTOCOL_ERROR;
1126         }
1127
1128         ber = ber_init( &(ctrl->ldctl_value) );
1129         if (ber == NULL) {
1130                 rs->sr_text = "postread control: internal error";
1131                 return LDAP_OTHER;
1132         }
1133
1134         siz = sizeof( AttributeName );
1135         off = offsetof( AttributeName, an_name );
1136         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1137                 rs->sr_text = "postread control: decoding error";
1138                 return LDAP_PROTOCOL_ERROR;
1139         }
1140
1141         for( i=0; i<siz; i++ ) {
1142                 int             rc = LDAP_SUCCESS;
1143                 const char      *dummy = NULL;
1144
1145                 an[i].an_desc = NULL;
1146                 an[i].an_oc = NULL;
1147                 an[i].an_oc_exclude = 0;
1148                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1149                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1150                         rs->sr_text = dummy
1151                                 ? dummy
1152                                 : "postread control: unknown attributeType";
1153                         return rc;
1154                 }
1155         }
1156
1157         op->o_postread = ctrl->ldctl_iscritical
1158                 ? SLAP_CONTROL_CRITICAL
1159                 : SLAP_CONTROL_NONCRITICAL;
1160
1161         op->o_postread_attrs = an;
1162
1163         rs->sr_err = LDAP_SUCCESS;
1164         return LDAP_SUCCESS;
1165 }
1166
1167 static int parseValuesReturnFilter (
1168         Operation *op,
1169         SlapReply *rs,
1170         LDAPControl *ctrl )
1171 {
1172         BerElement      *ber;
1173         struct berval   fstr = BER_BVNULL;
1174
1175         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1176                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1177                 return LDAP_PROTOCOL_ERROR;
1178         }
1179
1180         if ( ctrl->ldctl_value.bv_len == 0 ) {
1181                 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1182                 return LDAP_PROTOCOL_ERROR;
1183         }
1184
1185         ber = ber_init( &(ctrl->ldctl_value) );
1186         if (ber == NULL) {
1187                 rs->sr_text = "internal error";
1188                 return LDAP_OTHER;
1189         }
1190         
1191         rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1192
1193         if( rs->sr_err != LDAP_SUCCESS ) {
1194                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1195                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1196                         send_ldap_disconnect( op, rs );
1197                         rs->sr_err = SLAPD_DISCONNECT;
1198                 } else {
1199                         send_ldap_result( op, rs );
1200                 }
1201                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1202         }
1203 #ifdef LDAP_DEBUG
1204         else {
1205                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1206         }
1207
1208         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1209                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1210         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1211 #endif
1212
1213         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1214                 ? SLAP_CONTROL_CRITICAL
1215                 : SLAP_CONTROL_NONCRITICAL;
1216
1217         rs->sr_err = LDAP_SUCCESS;
1218         return LDAP_SUCCESS;
1219 }
1220
1221 #ifdef LDAP_CONTROL_SUBENTRIES
1222 static int parseSubentries (
1223         Operation *op,
1224         SlapReply *rs,
1225         LDAPControl *ctrl )
1226 {
1227         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1228                 rs->sr_text = "subentries control specified multiple times";
1229                 return LDAP_PROTOCOL_ERROR;
1230         }
1231
1232         /* FIXME: should use BER library */
1233         if( ( ctrl->ldctl_value.bv_len != 3 )
1234                 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1235                 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1236         {
1237                 rs->sr_text = "subentries control value encoding is bogus";
1238                 return LDAP_PROTOCOL_ERROR;
1239         }
1240
1241         op->o_subentries = ctrl->ldctl_iscritical
1242                 ? SLAP_CONTROL_CRITICAL
1243                 : SLAP_CONTROL_NONCRITICAL;
1244
1245         if (ctrl->ldctl_value.bv_val[2]) {
1246                 set_subentries_visibility( op );
1247         }
1248
1249         return LDAP_SUCCESS;
1250 }
1251 #endif
1252
1253 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1254 static int parsePermissiveModify (
1255         Operation *op,
1256         SlapReply *rs,
1257         LDAPControl *ctrl )
1258 {
1259         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1260                 rs->sr_text = "permissiveModify control specified multiple times";
1261                 return LDAP_PROTOCOL_ERROR;
1262         }
1263
1264         if ( ctrl->ldctl_value.bv_len ) {
1265                 rs->sr_text = "permissiveModify control value not empty";
1266                 return LDAP_PROTOCOL_ERROR;
1267         }
1268
1269         op->o_permissive_modify = ctrl->ldctl_iscritical
1270                 ? SLAP_CONTROL_CRITICAL
1271                 : SLAP_CONTROL_NONCRITICAL;
1272
1273         return LDAP_SUCCESS;
1274 }
1275 #endif
1276
1277 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1278 static int parseDomainScope (
1279         Operation *op,
1280         SlapReply *rs,
1281         LDAPControl *ctrl )
1282 {
1283         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1284                 rs->sr_text = "domainScope control specified multiple times";
1285                 return LDAP_PROTOCOL_ERROR;
1286         }
1287
1288         if ( ctrl->ldctl_value.bv_len ) {
1289                 rs->sr_text = "domainScope control value not empty";
1290                 return LDAP_PROTOCOL_ERROR;
1291         }
1292
1293         op->o_domain_scope = ctrl->ldctl_iscritical
1294                 ? SLAP_CONTROL_CRITICAL
1295                 : SLAP_CONTROL_NONCRITICAL;
1296
1297         return LDAP_SUCCESS;
1298 }
1299 #endif
1300
1301 #ifdef SLAP_CONTROL_X_TREE_DELETE
1302 static int parseTreeDelete (
1303         Operation *op,
1304         SlapReply *rs,
1305         LDAPControl *ctrl )
1306 {
1307         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1308                 rs->sr_text = "treeDelete control specified multiple times";
1309                 return LDAP_PROTOCOL_ERROR;
1310         }
1311
1312         if ( ctrl->ldctl_value.bv_len ) {
1313                 rs->sr_text = "treeDelete control value not empty";
1314                 return LDAP_PROTOCOL_ERROR;
1315         }
1316
1317         op->o_tree_delete = ctrl->ldctl_iscritical
1318                 ? SLAP_CONTROL_CRITICAL
1319                 : SLAP_CONTROL_NONCRITICAL;
1320
1321         return LDAP_SUCCESS;
1322 }
1323 #endif
1324
1325 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
1326 static int parseSearchOptions (
1327         Operation *op,
1328         SlapReply *rs,
1329         LDAPControl *ctrl )
1330 {
1331         BerElement *ber;
1332         ber_int_t search_flags;
1333         ber_tag_t tag;
1334
1335         if ( ctrl->ldctl_value.bv_len == 0 ) {
1336                 rs->sr_text = "searchOptions control value not empty";
1337                 return LDAP_PROTOCOL_ERROR;
1338         }
1339
1340         ber = ber_init( &ctrl->ldctl_value );
1341         if( ber == NULL ) {
1342                 rs->sr_text = "internal error";
1343                 return LDAP_OTHER;
1344         }
1345
1346         if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1347                 rs->sr_text = "searchOptions control decoding error";
1348                 return LDAP_PROTOCOL_ERROR;
1349         }
1350
1351         (void) ber_free( ber, 1 );
1352
1353         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1354                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1355                         rs->sr_text = "searchOptions control specified multiple times "
1356                                 "or with domainScope control";
1357                         return LDAP_PROTOCOL_ERROR;
1358                 }
1359
1360                 op->o_domain_scope = ctrl->ldctl_iscritical
1361                         ? SLAP_CONTROL_CRITICAL
1362                         : SLAP_CONTROL_NONCRITICAL;
1363         }
1364
1365         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1366                 /* Other search flags not recognised so far,
1367                  * including:
1368                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOM
1369                  */
1370                 rs->sr_text = "searchOptions contained unrecongized flag";
1371                 return LDAP_UNWILLING_TO_PERFORM;
1372         }
1373
1374         return LDAP_SUCCESS;
1375 }
1376 #endif
1377