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