]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
050f0ac7e14aa0ebda16ae63e23ae58d1f8ed143
[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 #ifdef LDAP_CONTROL_X_VALUESRETURNFILTER
132         { LDAP_CONTROL_X_VALUESRETURNFILTER /* bad OID */,
133                 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
134                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
135                 NULL, NULL,
136                 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
137 #endif
138         { LDAP_CONTROL_PAGEDRESULTS,
139                 (int)offsetof(struct slap_control_ids, sc_pagedResults),
140                 SLAP_CTRL_SEARCH,
141                 NULL, NULL,
142                 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
143 #ifdef LDAP_DEVEL
144         { LDAP_CONTROL_SORTREQUEST,
145                 (int)offsetof(struct slap_control_ids, sc_sortedResults),
146                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
147                 NULL, NULL,
148                 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149 #endif
150         { LDAP_CONTROL_X_DOMAIN_SCOPE,
151                 (int)offsetof(struct slap_control_ids, sc_domainScope),
152                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
153                 NULL, NULL,
154                 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
155         { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
156                 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
157                 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
158                 NULL, NULL,
159                 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
160 #ifdef SLAP_CONTROL_X_TREE_DELETE
161         { LDAP_CONTROL_X_TREE_DELETE,
162                 (int)offsetof(struct slap_control_ids, sc_treeDelete),
163                 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
164                 NULL, NULL,
165                 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
166 #endif
167         { LDAP_CONTROL_X_SEARCH_OPTIONS,
168                 (int)offsetof(struct slap_control_ids, sc_searchOptions),
169                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
170                 NULL, NULL,
171                 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
172         { LDAP_CONTROL_SUBENTRIES,
173                 (int)offsetof(struct slap_control_ids, sc_subentries),
174                 SLAP_CTRL_SEARCH,
175                 NULL, NULL,
176                 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
177         { LDAP_CONTROL_NOOP,
178                 (int)offsetof(struct slap_control_ids, sc_noOp),
179                 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
180                 NULL, NULL,
181                 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
182 #ifdef LDAP_DEVEL
183         { LDAP_CONTROL_DONTUSECOPY,
184                 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
185                 SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
186                 NULL, NULL,
187                 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
188         { LDAP_CONTROL_MANAGEDIT,
189                 (int)offsetof(struct slap_control_ids, sc_manageDIT),
190                 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
191                 NULL, NULL,
192                 parseManageDIT, 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         Debug( LDAP_DEBUG_TRACE,
496                 "slap_global_control: unavailable control: %s\n",      
497                 oid, 0, 0 );
498
499         return LDAP_COMPARE_FALSE;
500 }
501
502 void slap_free_ctrls(
503         Operation *op,
504         LDAPControl **ctrls )
505 {
506         int i;
507
508         for (i=0; ctrls[i]; i++) {
509                 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
510         }
511         op->o_tmpfree( ctrls, op->o_tmpmemctx );
512 }
513
514 int slap_parse_ctrl(
515         Operation *op,
516         SlapReply *rs,
517         LDAPControl *control,
518         const char **text )
519 {
520         struct slap_control *sc;
521
522         sc = find_ctrl( control->ldctl_oid );
523         if( sc != NULL ) {
524                 /* recognized control */
525                 slap_mask_t tagmask;
526                 switch( op->o_tag ) {
527                 case LDAP_REQ_ADD:
528                         tagmask = SLAP_CTRL_ADD;
529                         break;
530                 case LDAP_REQ_BIND:
531                         tagmask = SLAP_CTRL_BIND;
532                         break;
533                 case LDAP_REQ_COMPARE:
534                         tagmask = SLAP_CTRL_COMPARE;
535                         break;
536                 case LDAP_REQ_DELETE:
537                         tagmask = SLAP_CTRL_DELETE;
538                         break;
539                 case LDAP_REQ_MODIFY:
540                         tagmask = SLAP_CTRL_MODIFY;
541                         break;
542                 case LDAP_REQ_RENAME:
543                         tagmask = SLAP_CTRL_RENAME;
544                         break;
545                 case LDAP_REQ_SEARCH:
546                         tagmask = SLAP_CTRL_SEARCH;
547                         break;
548                 case LDAP_REQ_UNBIND:
549                         tagmask = SLAP_CTRL_UNBIND;
550                         break;
551                 case LDAP_REQ_ABANDON:
552                         tagmask = SLAP_CTRL_ABANDON;
553                         break;
554                 case LDAP_REQ_EXTENDED:
555                         tagmask=~0L;
556                         assert( op->ore_reqoid.bv_val != NULL );
557                         if( sc->sc_extendedopsbv != NULL ) {
558                                 int i;
559                                 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
560                                         if( bvmatch( &op->ore_reqoid,
561                                                 &sc->sc_extendedopsbv[i] ) )
562                                         {
563                                                 tagmask=0L;
564                                                 break;
565                                         }
566                                 }
567                         }
568                         break;
569                 default:
570                         *text = "controls internal error";
571                         return LDAP_OTHER;
572                 }
573
574                 if (( sc->sc_mask & tagmask ) == tagmask ) {
575                         /* available extension */
576                         int     rc;
577
578                         if( !sc->sc_parse ) {
579                                 *text = "not yet implemented";
580                                 return LDAP_OTHER;
581                         }
582
583                         rc = sc->sc_parse( op, rs, control );
584                         if ( rc ) {
585                                 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
586                                 return rc;
587                         }
588
589                 } else if( control->ldctl_iscritical ) {
590                         /* unavailable CRITICAL control */
591                         *text = "critical extension is unavailable";
592                         return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
593                 }
594         } else if( control->ldctl_iscritical ) {
595                 /* unrecognized CRITICAL control */
596                 *text = "critical extension is not recognized";
597                 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
598         }
599
600         return LDAP_SUCCESS;
601 }
602
603 int get_ctrls(
604         Operation *op,
605         SlapReply *rs,
606         int sendres )
607 {
608         int nctrls = 0;
609         ber_tag_t tag;
610         ber_len_t len;
611         char *opaque;
612         BerElement *ber = op->o_ber;
613         struct berval bv;
614
615         len = ber_pvt_ber_remaining(ber);
616
617         if( len == 0) {
618                 /* no controls */
619                 rs->sr_err = LDAP_SUCCESS;
620                 return rs->sr_err;
621         }
622
623         if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
624                 if( tag == LBER_ERROR ) {
625                         rs->sr_err = SLAPD_DISCONNECT;
626                         rs->sr_text = "unexpected data in PDU";
627                 }
628
629                 goto return_results;
630         }
631
632         Debug( LDAP_DEBUG_TRACE,
633                 "=> get_ctrls\n", 0, 0, 0 );
634
635         if( op->o_protocol < LDAP_VERSION3 ) {
636                 rs->sr_err = SLAPD_DISCONNECT;
637                 rs->sr_text = "controls require LDAPv3";
638                 goto return_results;
639         }
640
641         /* one for first control, one for termination */
642         op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
643
644 #if 0
645         if( op->ctrls == NULL ) {
646                 rs->sr_err = LDAP_NO_MEMORY;
647                 rs->sr_text = "no memory";
648                 goto return_results;
649         }
650 #endif
651
652         op->o_ctrls[nctrls] = NULL;
653
654         /* step through each element */
655         for( tag = ber_first_element( ber, &len, &opaque );
656                 tag != LBER_ERROR;
657                 tag = ber_next_element( ber, &len, opaque ) )
658         {
659                 LDAPControl *c;
660                 LDAPControl **tctrls;
661
662                 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
663                 memset(c, 0, sizeof(LDAPControl));
664
665                 /* allocate pointer space for current controls (nctrls)
666                  * + this control + extra NULL
667                  */
668                 tctrls = op->o_tmprealloc( op->o_ctrls,
669                         (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
670
671 #if 0
672                 if( tctrls == NULL ) {
673                         ch_free( c );
674                         ldap_controls_free(op->o_ctrls);
675                         op->o_ctrls = NULL;
676
677                         rs->sr_err = LDAP_NO_MEMORY;
678                         rs->sr_text = "no memory";
679                         goto return_results;
680                 }
681 #endif
682                 op->o_ctrls = tctrls;
683
684                 op->o_ctrls[nctrls++] = c;
685                 op->o_ctrls[nctrls] = NULL;
686
687                 tag = ber_scanf( ber, "{m" /*}*/, &bv );
688                 c->ldctl_oid = bv.bv_val;
689
690                 if( tag == LBER_ERROR ) {
691                         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
692                                 0, 0, 0 );
693
694                         slap_free_ctrls( op, op->o_ctrls );
695                         op->o_ctrls = NULL;
696                         rs->sr_err = SLAPD_DISCONNECT;
697                         rs->sr_text = "decoding controls error";
698                         goto return_results;
699
700                 } else if( c->ldctl_oid == NULL ) {
701                         Debug( LDAP_DEBUG_TRACE,
702                                 "get_ctrls: conn %lu got emtpy OID.\n",
703                                 op->o_connid, 0, 0 );
704
705                         slap_free_ctrls( op, op->o_ctrls );
706                         op->o_ctrls = NULL;
707                         rs->sr_err = LDAP_PROTOCOL_ERROR;
708                         rs->sr_text = "OID field is empty";
709                         goto return_results;
710                 }
711
712                 tag = ber_peek_tag( ber, &len );
713
714                 if( tag == LBER_BOOLEAN ) {
715                         ber_int_t crit;
716                         tag = ber_scanf( ber, "b", &crit );
717
718                         if( tag == LBER_ERROR ) {
719                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
720                                         0, 0, 0 );
721                                 slap_free_ctrls( op, op->o_ctrls );
722                                 op->o_ctrls = NULL;
723                                 rs->sr_err = SLAPD_DISCONNECT;
724                                 rs->sr_text = "decoding controls error";
725                                 goto return_results;
726                         }
727
728                         c->ldctl_iscritical = (crit != 0);
729                         tag = ber_peek_tag( ber, &len );
730                 }
731
732                 if( tag == LBER_OCTETSTRING ) {
733                         tag = ber_scanf( ber, "m", &c->ldctl_value );
734
735                         if( tag == LBER_ERROR ) {
736                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
737                                         "%s (%scritical): get value failed.\n",
738                                         op->o_connid, c->ldctl_oid,
739                                         c->ldctl_iscritical ? "" : "non" );
740                                 slap_free_ctrls( op, op->o_ctrls );
741                                 op->o_ctrls = NULL;
742                                 rs->sr_err = SLAPD_DISCONNECT;
743                                 rs->sr_text = "decoding controls error";
744                                 goto return_results;
745                         }
746                 }
747
748                 Debug( LDAP_DEBUG_TRACE,
749                         "=> get_ctrls: oid=\"%s\" (%scritical)\n",
750                         c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
751
752                 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
753                 if ( rs->sr_err != LDAP_SUCCESS ) {
754                         goto return_results;
755                 }
756         }
757
758 return_results:
759         Debug( LDAP_DEBUG_TRACE,
760                 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
761                 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
762
763         if( sendres && rs->sr_err != LDAP_SUCCESS ) {
764                 if( rs->sr_err == SLAPD_DISCONNECT ) {
765                         rs->sr_err = LDAP_PROTOCOL_ERROR;
766                         send_ldap_disconnect( op, rs );
767                         rs->sr_err = SLAPD_DISCONNECT;
768                 } else {
769                         send_ldap_result( op, rs );
770                 }
771         }
772
773         return rs->sr_err;
774 }
775
776 int
777 slap_remove_control(
778         Operation       *op,
779         SlapReply       *rs,
780         int             ctrl,
781         BI_chk_controls fnc )
782 {
783         int             i, j;
784
785         switch ( op->o_ctrlflag[ ctrl ] ) {
786         case SLAP_CONTROL_NONCRITICAL:
787                 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
788                         if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
789                                 slap_known_controls[ ctrl - 1 ] ) == 0 )
790                         {
791                                 j = i;
792                         }
793                 }
794
795                 if ( j == -1 ) {
796                         rs->sr_err = LDAP_OTHER;
797                         break;
798                 }
799
800                 if ( fnc ) {
801                         (void)fnc( op, rs );
802                 }
803
804                 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
805
806                 if ( i > 1 ) {
807                         AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
808                                 ( i - j ) * sizeof( LDAPControl * ) );
809
810                 } else {
811                         op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
812                         op->o_ctrls = NULL;
813                 }
814
815                 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
816
817                 Debug( LDAP_DEBUG_ANY, "%s: "
818                         "non-critical control \"%s\" not supported; stripped.\n",
819                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
820                 /* fall thru */
821
822         case SLAP_CONTROL_IGNORED:
823         case SLAP_CONTROL_NONE:
824                 rs->sr_err = SLAP_CB_CONTINUE;
825                 break;
826
827         case SLAP_CONTROL_CRITICAL:
828                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
829                 if ( fnc ) {
830                         (void)fnc( op, rs );
831                 }
832                 Debug( LDAP_DEBUG_ANY, "%s: "
833                         "critical control \"%s\" not supported.\n",
834                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
835                 break;
836
837         default:
838                 /* handle all cases! */
839                 assert( 0 );
840         }
841
842         return rs->sr_err;
843 }
844
845 #ifdef LDAP_DEVEL
846 static int parseDontUseCopy (
847         Operation *op,
848         SlapReply *rs,
849         LDAPControl *ctrl )
850 {
851         if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
852                 rs->sr_text = "dontUseCopy control specified multiple times";
853                 return LDAP_PROTOCOL_ERROR;
854         }
855
856         if ( ctrl->ldctl_value.bv_len ) {
857                 rs->sr_text = "dontUseCopy control value not empty";
858                 return LDAP_PROTOCOL_ERROR;
859         }
860
861         if ( ctrl->ldctl_iscritical != SLAP_CONTROL_CRITICAL ) {
862                 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
863                 return LDAP_PROTOCOL_ERROR;
864         }
865
866         op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
867         return LDAP_SUCCESS;
868 }
869
870 static int parseManageDIT (
871         Operation *op,
872         SlapReply *rs,
873         LDAPControl *ctrl )
874 {
875         if ( op->o_managedit != SLAP_CONTROL_NONE ) {
876                 rs->sr_text = "manageDIT control specified multiple times";
877                 return LDAP_PROTOCOL_ERROR;
878         }
879
880         if ( ctrl->ldctl_value.bv_len ) {
881                 rs->sr_text = "manageDIT control value not empty";
882                 return LDAP_PROTOCOL_ERROR;
883         }
884
885         op->o_managedit = ctrl->ldctl_iscritical
886                 ? SLAP_CONTROL_CRITICAL
887                 : SLAP_CONTROL_NONCRITICAL;
888
889         return LDAP_SUCCESS;
890 }
891 #endif
892
893 static int parseManageDSAit (
894         Operation *op,
895         SlapReply *rs,
896         LDAPControl *ctrl )
897 {
898         if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
899                 rs->sr_text = "manageDSAit control specified multiple times";
900                 return LDAP_PROTOCOL_ERROR;
901         }
902
903         if ( ctrl->ldctl_value.bv_len ) {
904                 rs->sr_text = "manageDSAit control value not empty";
905                 return LDAP_PROTOCOL_ERROR;
906         }
907
908         op->o_managedsait = ctrl->ldctl_iscritical
909                 ? SLAP_CONTROL_CRITICAL
910                 : SLAP_CONTROL_NONCRITICAL;
911
912         return LDAP_SUCCESS;
913 }
914
915 static int parseProxyAuthz (
916         Operation *op,
917         SlapReply *rs,
918         LDAPControl *ctrl )
919 {
920         int             rc;
921         struct berval   dn = BER_BVNULL;
922
923         if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
924                 rs->sr_text = "proxy authorization control specified multiple times";
925                 return LDAP_PROTOCOL_ERROR;
926         }
927
928         if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
929                 && BER_BVISEMPTY( &op->o_ndn ) )
930         {
931                 rs->sr_text = "anonymous proxied authorization not allowed";
932                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
933         }
934
935         op->o_proxy_authz = ctrl->ldctl_iscritical
936                 ? SLAP_CONTROL_CRITICAL
937                 : SLAP_CONTROL_NONCRITICAL;
938
939         Debug( LDAP_DEBUG_ARGS,
940                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
941                 op->o_connid,
942                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
943                 0 );
944
945         if ( ctrl->ldctl_value.bv_len == 0 ) {
946                 Debug( LDAP_DEBUG_TRACE,
947                         "parseProxyAuthz: conn=%lu anonymous\n", 
948                         op->o_connid, 0, 0 );
949
950                 /* anonymous */
951                 if ( !BER_BVISNULL( &op->o_ndn ) ) {
952                         op->o_ndn.bv_val[ 0 ] = '\0';
953                 }
954                 op->o_ndn.bv_len = 0;
955
956                 if ( !BER_BVISNULL( &op->o_dn ) ) {
957                         op->o_dn.bv_val[ 0 ] = '\0';
958                 }
959                 op->o_dn.bv_len = 0;
960
961                 return LDAP_SUCCESS;
962         }
963
964         rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
965                         NULL, &dn, SLAP_GETDN_AUTHZID );
966
967         /* FIXME: empty DN in proxyAuthz control should be legal... */
968         if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
969                 if ( dn.bv_val ) {
970                         ch_free( dn.bv_val );
971                 }
972                 rs->sr_text = "authzId mapping failed";
973                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
974         }
975
976         Debug( LDAP_DEBUG_TRACE,
977                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
978                 op->o_connid,
979                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
980
981         rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
982
983         if ( rc ) {
984                 ch_free( dn.bv_val );
985                 rs->sr_text = "not authorized to assume identity";
986                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
987         }
988
989         ch_free( op->o_ndn.bv_val );
990         ch_free( op->o_dn.bv_val );
991
992         /*
993          * NOTE: since slap_sasl_getdn() returns a normalized dn,
994          * from now on op->o_dn is normalized
995          */
996         op->o_ndn = dn;
997         ber_dupbv( &op->o_dn, &dn );
998
999
1000         Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1001             op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1002
1003         return LDAP_SUCCESS;
1004 }
1005
1006 static int parseNoOp (
1007         Operation *op,
1008         SlapReply *rs,
1009         LDAPControl *ctrl )
1010 {
1011         if ( op->o_noop != SLAP_CONTROL_NONE ) {
1012                 rs->sr_text = "noop control specified multiple times";
1013                 return LDAP_PROTOCOL_ERROR;
1014         }
1015
1016         if ( ctrl->ldctl_value.bv_len ) {
1017                 rs->sr_text = "noop control value not empty";
1018                 return LDAP_PROTOCOL_ERROR;
1019         }
1020
1021         op->o_noop = ctrl->ldctl_iscritical
1022                 ? SLAP_CONTROL_CRITICAL
1023                 : SLAP_CONTROL_NONCRITICAL;
1024
1025         return LDAP_SUCCESS;
1026 }
1027
1028 static int parsePagedResults (
1029         Operation *op,
1030         SlapReply *rs,
1031         LDAPControl *ctrl )
1032 {
1033         int             rc = LDAP_SUCCESS;
1034         ber_tag_t       tag;
1035         ber_int_t       size;
1036         BerElement      *ber;
1037         struct berval   cookie = BER_BVNULL;
1038         PagedResultsState       *ps;
1039
1040         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1041                 rs->sr_text = "paged results control specified multiple times";
1042                 return LDAP_PROTOCOL_ERROR;
1043         }
1044
1045         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1046                 rs->sr_text = "paged results control value is empty (or absent)";
1047                 return LDAP_PROTOCOL_ERROR;
1048         }
1049
1050         /* Parse the control value
1051          *      realSearchControlValue ::= SEQUENCE {
1052          *              size    INTEGER (0..maxInt),
1053          *                              -- requested page size from client
1054          *                              -- result set size estimate from server
1055          *              cookie  OCTET STRING
1056          * }
1057          */
1058         ber = ber_init( &ctrl->ldctl_value );
1059         if ( ber == NULL ) {
1060                 rs->sr_text = "internal error";
1061                 return LDAP_OTHER;
1062         }
1063
1064         tag = ber_scanf( ber, "{im}", &size, &cookie );
1065
1066         if ( tag == LBER_ERROR ) {
1067                 rs->sr_text = "paged results control could not be decoded";
1068                 rc = LDAP_PROTOCOL_ERROR;
1069                 goto done;
1070         }
1071
1072         if ( size < 0 ) {
1073                 rs->sr_text = "paged results control size invalid";
1074                 rc = LDAP_PROTOCOL_ERROR;
1075                 goto done;
1076         }
1077
1078         ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1079         *ps = op->o_conn->c_pagedresults_state;
1080         ps->ps_size = size;
1081         op->o_pagedresults_state = ps;
1082
1083         /* NOTE: according to RFC 2696 3.:
1084
1085     If the page size is greater than or equal to the sizeLimit value, the
1086     server should ignore the control as the request can be satisfied in a
1087     single page.
1088          
1089          * NOTE: this assumes that the op->ors_slimit be set
1090          * before the controls are parsed.     
1091          */
1092                 
1093         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1094                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1095
1096         } else if ( ctrl->ldctl_iscritical ) {
1097                 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1098
1099         } else {
1100                 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1101         }
1102
1103 done:;
1104         (void)ber_free( ber, 1 );
1105         return rc;
1106 }
1107
1108 #ifdef LDAP_DEVEL
1109 static int parseSortedResults (
1110         Operation *op,
1111         SlapReply *rs,
1112         LDAPControl *ctrl )
1113 {
1114         int             rc = LDAP_SUCCESS;
1115
1116         if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1117                 rs->sr_text = "sorted results control specified multiple times";
1118                 return LDAP_PROTOCOL_ERROR;
1119         }
1120
1121         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1122                 rs->sr_text = "sorted results control value is empty (or absent)";
1123                 return LDAP_PROTOCOL_ERROR;
1124         }
1125
1126         /* blow off parsing the value */
1127
1128         op->o_sortedresults = ctrl->ldctl_iscritical
1129                 ? SLAP_CONTROL_CRITICAL
1130                 : SLAP_CONTROL_NONCRITICAL;
1131
1132         return rc;
1133 }
1134 #endif
1135
1136 static int parseAssert (
1137         Operation *op,
1138         SlapReply *rs,
1139         LDAPControl *ctrl )
1140 {
1141         BerElement      *ber;
1142         struct berval   fstr = BER_BVNULL;
1143
1144         if ( op->o_assert != SLAP_CONTROL_NONE ) {
1145                 rs->sr_text = "assert control specified multiple times";
1146                 return LDAP_PROTOCOL_ERROR;
1147         }
1148
1149         if ( ctrl->ldctl_value.bv_len == 0 ) {
1150                 rs->sr_text = "assert control value is empty (or absent)";
1151                 return LDAP_PROTOCOL_ERROR;
1152         }
1153
1154         ber = ber_init( &(ctrl->ldctl_value) );
1155         if (ber == NULL) {
1156                 rs->sr_text = "assert control: internal error";
1157                 return LDAP_OTHER;
1158         }
1159         
1160         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1161                 &rs->sr_text);
1162         if( rs->sr_err != LDAP_SUCCESS ) {
1163                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1164                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1165                         send_ldap_disconnect( op, rs );
1166                         rs->sr_err = SLAPD_DISCONNECT;
1167                 } else {
1168                         send_ldap_result( op, rs );
1169                 }
1170                 if( op->o_assertion != NULL ) {
1171                         filter_free_x( op, op->o_assertion );
1172                 }
1173                 return rs->sr_err;
1174         }
1175
1176 #ifdef LDAP_DEBUG
1177         filter2bv_x( op, op->o_assertion, &fstr );
1178
1179         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1180                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1181         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1182 #endif
1183
1184         op->o_assert = ctrl->ldctl_iscritical
1185                 ? SLAP_CONTROL_CRITICAL
1186                 : SLAP_CONTROL_NONCRITICAL;
1187
1188         rs->sr_err = LDAP_SUCCESS;
1189         return LDAP_SUCCESS;
1190 }
1191
1192 static int parsePreRead (
1193         Operation *op,
1194         SlapReply *rs,
1195         LDAPControl *ctrl )
1196 {
1197         ber_len_t siz, off, i;
1198         AttributeName *an = NULL;
1199         BerElement      *ber;
1200
1201         if ( op->o_preread != SLAP_CONTROL_NONE ) {
1202                 rs->sr_text = "preread control specified multiple times";
1203                 return LDAP_PROTOCOL_ERROR;
1204         }
1205
1206         if ( ctrl->ldctl_value.bv_len == 0 ) {
1207                 rs->sr_text = "preread control value is empty (or absent)";
1208                 return LDAP_PROTOCOL_ERROR;
1209         }
1210
1211         ber = ber_init( &(ctrl->ldctl_value) );
1212         if (ber == NULL) {
1213                 rs->sr_text = "preread control: internal error";
1214                 return LDAP_OTHER;
1215         }
1216
1217         siz = sizeof( AttributeName );
1218         off = offsetof( AttributeName, an_name );
1219         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1220                 rs->sr_text = "preread control: decoding error";
1221                 return LDAP_PROTOCOL_ERROR;
1222         }
1223
1224         for( i=0; i<siz; i++ ) {
1225                 int             rc = LDAP_SUCCESS;
1226                 const char      *dummy = NULL;
1227
1228                 an[i].an_desc = NULL;
1229                 an[i].an_oc = NULL;
1230                 an[i].an_oc_exclude = 0;
1231                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1232                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1233                         rs->sr_text = dummy
1234                                 ? dummy
1235                                 : "postread control: unknown attributeType";
1236                         return rc;
1237                 }
1238         }
1239
1240         op->o_preread = ctrl->ldctl_iscritical
1241                 ? SLAP_CONTROL_CRITICAL
1242                 : SLAP_CONTROL_NONCRITICAL;
1243
1244         op->o_preread_attrs = an;
1245
1246         rs->sr_err = LDAP_SUCCESS;
1247         return LDAP_SUCCESS;
1248 }
1249
1250 static int parsePostRead (
1251         Operation *op,
1252         SlapReply *rs,
1253         LDAPControl *ctrl )
1254 {
1255         ber_len_t siz, off, i;
1256         AttributeName *an = NULL;
1257         BerElement      *ber;
1258
1259         if ( op->o_postread != SLAP_CONTROL_NONE ) {
1260                 rs->sr_text = "postread control specified multiple times";
1261                 return LDAP_PROTOCOL_ERROR;
1262         }
1263
1264         if ( ctrl->ldctl_value.bv_len == 0 ) {
1265                 rs->sr_text = "postread control value is empty (or absent)";
1266                 return LDAP_PROTOCOL_ERROR;
1267         }
1268
1269         ber = ber_init( &(ctrl->ldctl_value) );
1270         if (ber == NULL) {
1271                 rs->sr_text = "postread control: internal error";
1272                 return LDAP_OTHER;
1273         }
1274
1275         siz = sizeof( AttributeName );
1276         off = offsetof( AttributeName, an_name );
1277         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1278                 rs->sr_text = "postread control: decoding error";
1279                 return LDAP_PROTOCOL_ERROR;
1280         }
1281
1282         for( i=0; i<siz; i++ ) {
1283                 int             rc = LDAP_SUCCESS;
1284                 const char      *dummy = NULL;
1285
1286                 an[i].an_desc = NULL;
1287                 an[i].an_oc = NULL;
1288                 an[i].an_oc_exclude = 0;
1289                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1290                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1291                         rs->sr_text = dummy
1292                                 ? dummy
1293                                 : "postread control: unknown attributeType";
1294                         return rc;
1295                 }
1296         }
1297
1298         op->o_postread = ctrl->ldctl_iscritical
1299                 ? SLAP_CONTROL_CRITICAL
1300                 : SLAP_CONTROL_NONCRITICAL;
1301
1302         op->o_postread_attrs = an;
1303
1304         rs->sr_err = LDAP_SUCCESS;
1305         return LDAP_SUCCESS;
1306 }
1307
1308 static int parseValuesReturnFilter (
1309         Operation *op,
1310         SlapReply *rs,
1311         LDAPControl *ctrl )
1312 {
1313         BerElement      *ber;
1314         struct berval   fstr = BER_BVNULL;
1315
1316         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1317                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1318                 return LDAP_PROTOCOL_ERROR;
1319         }
1320
1321         if ( ctrl->ldctl_value.bv_len == 0 ) {
1322                 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1323                 return LDAP_PROTOCOL_ERROR;
1324         }
1325
1326         ber = ber_init( &(ctrl->ldctl_value) );
1327         if (ber == NULL) {
1328                 rs->sr_text = "internal error";
1329                 return LDAP_OTHER;
1330         }
1331         
1332         rs->sr_err = get_vrFilter( op, ber,
1333                 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1334
1335         if( rs->sr_err != LDAP_SUCCESS ) {
1336                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1337                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1338                         send_ldap_disconnect( op, rs );
1339                         rs->sr_err = SLAPD_DISCONNECT;
1340                 } else {
1341                         send_ldap_result( op, rs );
1342                 }
1343                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1344         }
1345 #ifdef LDAP_DEBUG
1346         else {
1347                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1348         }
1349
1350         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1351                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1352         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1353 #endif
1354
1355         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1356                 ? SLAP_CONTROL_CRITICAL
1357                 : SLAP_CONTROL_NONCRITICAL;
1358
1359         rs->sr_err = LDAP_SUCCESS;
1360         return LDAP_SUCCESS;
1361 }
1362
1363 static int parseSubentries (
1364         Operation *op,
1365         SlapReply *rs,
1366         LDAPControl *ctrl )
1367 {
1368         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1369                 rs->sr_text = "subentries control specified multiple times";
1370                 return LDAP_PROTOCOL_ERROR;
1371         }
1372
1373         /* FIXME: should use BER library */
1374         if( ( ctrl->ldctl_value.bv_len != 3 )
1375                 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1376                 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1377         {
1378                 rs->sr_text = "subentries control value encoding is bogus";
1379                 return LDAP_PROTOCOL_ERROR;
1380         }
1381
1382         op->o_subentries = ctrl->ldctl_iscritical
1383                 ? SLAP_CONTROL_CRITICAL
1384                 : SLAP_CONTROL_NONCRITICAL;
1385
1386         if (ctrl->ldctl_value.bv_val[2]) {
1387                 set_subentries_visibility( op );
1388         }
1389
1390         return LDAP_SUCCESS;
1391 }
1392
1393 static int parsePermissiveModify (
1394         Operation *op,
1395         SlapReply *rs,
1396         LDAPControl *ctrl )
1397 {
1398         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1399                 rs->sr_text = "permissiveModify control specified multiple times";
1400                 return LDAP_PROTOCOL_ERROR;
1401         }
1402
1403         if ( ctrl->ldctl_value.bv_len ) {
1404                 rs->sr_text = "permissiveModify control value not empty";
1405                 return LDAP_PROTOCOL_ERROR;
1406         }
1407
1408         op->o_permissive_modify = ctrl->ldctl_iscritical
1409                 ? SLAP_CONTROL_CRITICAL
1410                 : SLAP_CONTROL_NONCRITICAL;
1411
1412         return LDAP_SUCCESS;
1413 }
1414
1415 static int parseDomainScope (
1416         Operation *op,
1417         SlapReply *rs,
1418         LDAPControl *ctrl )
1419 {
1420         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1421                 rs->sr_text = "domainScope control specified multiple times";
1422                 return LDAP_PROTOCOL_ERROR;
1423         }
1424
1425         if ( ctrl->ldctl_value.bv_len ) {
1426                 rs->sr_text = "domainScope control value not empty";
1427                 return LDAP_PROTOCOL_ERROR;
1428         }
1429
1430         op->o_domain_scope = ctrl->ldctl_iscritical
1431                 ? SLAP_CONTROL_CRITICAL
1432                 : SLAP_CONTROL_NONCRITICAL;
1433
1434         return LDAP_SUCCESS;
1435 }
1436
1437 #ifdef SLAP_CONTROL_X_TREE_DELETE
1438 static int parseTreeDelete (
1439         Operation *op,
1440         SlapReply *rs,
1441         LDAPControl *ctrl )
1442 {
1443         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1444                 rs->sr_text = "treeDelete control specified multiple times";
1445                 return LDAP_PROTOCOL_ERROR;
1446         }
1447
1448         if ( ctrl->ldctl_value.bv_len ) {
1449                 rs->sr_text = "treeDelete control value not empty";
1450                 return LDAP_PROTOCOL_ERROR;
1451         }
1452
1453         op->o_tree_delete = ctrl->ldctl_iscritical
1454                 ? SLAP_CONTROL_CRITICAL
1455                 : SLAP_CONTROL_NONCRITICAL;
1456
1457         return LDAP_SUCCESS;
1458 }
1459 #endif
1460
1461 static int parseSearchOptions (
1462         Operation *op,
1463         SlapReply *rs,
1464         LDAPControl *ctrl )
1465 {
1466         BerElement *ber;
1467         ber_int_t search_flags;
1468         ber_tag_t tag;
1469
1470         if ( ctrl->ldctl_value.bv_len == 0 ) {
1471                 rs->sr_text = "searchOptions control value is empty (or absent)";
1472                 return LDAP_PROTOCOL_ERROR;
1473         }
1474
1475         ber = ber_init( &ctrl->ldctl_value );
1476         if( ber == NULL ) {
1477                 rs->sr_text = "internal error";
1478                 return LDAP_OTHER;
1479         }
1480
1481         if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1482                 rs->sr_text = "searchOptions control decoding error";
1483                 return LDAP_PROTOCOL_ERROR;
1484         }
1485
1486         (void) ber_free( ber, 1 );
1487
1488         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1489                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1490                         rs->sr_text = "searchOptions control specified multiple times "
1491                                 "or with domainScope control";
1492                         return LDAP_PROTOCOL_ERROR;
1493                 }
1494
1495                 op->o_domain_scope = ctrl->ldctl_iscritical
1496                         ? SLAP_CONTROL_CRITICAL
1497                         : SLAP_CONTROL_NONCRITICAL;
1498         }
1499
1500         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1501                 /* Other search flags not recognised so far,
1502                  * including:
1503                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOM
1504                  */
1505                 rs->sr_text = "searchOptions contained unrecognized flag";
1506                 return LDAP_UNWILLING_TO_PERFORM;
1507         }
1508
1509         return LDAP_SUCCESS;
1510 }
1511