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