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