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