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