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