]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
072777c69bf109e7ed5ca603a760eaa41926516e
[openldap] / servers / slapd / controls.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 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 #include "lutil.h"
25
26 #include "../../libraries/liblber/lber-int.h"
27
28 static SLAP_CTRL_PARSE_FN parseAssert;
29 static SLAP_CTRL_PARSE_FN parseDomainScope;
30 static SLAP_CTRL_PARSE_FN parseDontUseCopy;
31 static SLAP_CTRL_PARSE_FN parseManageDSAit;
32 static SLAP_CTRL_PARSE_FN parseNoOp;
33 static SLAP_CTRL_PARSE_FN parsePagedResults;
34 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
35 static SLAP_CTRL_PARSE_FN parsePreRead, parsePostRead;
36 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
37 static SLAP_CTRL_PARSE_FN parseRelax;
38 static SLAP_CTRL_PARSE_FN parseSearchOptions;
39 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
40 static SLAP_CTRL_PARSE_FN parseSortedResults;
41 #endif
42 static SLAP_CTRL_PARSE_FN parseSubentries;
43 #ifdef SLAP_CONTROL_X_TREE_DELETE
44 static SLAP_CTRL_PARSE_FN parseTreeDelete;
45 #endif
46 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
47 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
48 static SLAP_CTRL_PARSE_FN parseSessionTracking;
49 #endif
50
51 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
52
53 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
54 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
55
56 struct slap_control_ids slap_cids;
57
58 struct slap_control {
59         /* Control OID */
60         char *sc_oid;
61
62         /* The controlID for this control */
63         int sc_cid;
64
65         /* Operations supported by control */
66         slap_mask_t sc_mask;
67
68         /* Extended operations supported by control */
69         char **sc_extendedops;          /* input */
70         BerVarray sc_extendedopsbv;     /* run-time use */
71
72         /* Control parsing callback */
73         SLAP_CTRL_PARSE_FN *sc_parse;
74
75         LDAP_SLIST_ENTRY(slap_control) sc_next;
76 };
77
78 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
79         = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
80
81 /*
82  * all known request control OIDs should be added to this list
83  */
84 /*
85  * NOTE: initialize num_known_controls to 1 so that cid = 0 always
86  * addresses an undefined control; this allows to safely test for 
87  * well known controls even if they are not registered, e.g. if 
88  * they get moved to modules.  An example is sc_LDAPsync, which 
89  * is implemented in the syncprov overlay and thus, if configured 
90  * as dynamic module, may not be registered.  One side effect is that 
91  * slap_known_controls[0] == NULL, so it should always be used 
92  * starting from 1.
93  * FIXME: should we define the "undefined control" oid?
94  */
95 char *slap_known_controls[SLAP_MAX_CIDS+1];
96 static int num_known_controls = 1;
97
98 static char *proxy_authz_extops[] = {
99         LDAP_EXOP_MODIFY_PASSWD,
100         LDAP_EXOP_WHO_AM_I,
101         LDAP_EXOP_REFRESH,
102         NULL
103 };
104
105 static char *manageDSAit_extops[] = {
106         LDAP_EXOP_REFRESH,
107         NULL
108 };
109
110 static struct slap_control control_defs[] = {
111         {  LDAP_CONTROL_ASSERT,
112                 (int)offsetof(struct slap_control_ids, sc_assert),
113                 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME|
114                         SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
115                 NULL, NULL,
116                 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
117         { LDAP_CONTROL_PRE_READ,
118                 (int)offsetof(struct slap_control_ids, sc_preRead),
119                 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
120                 NULL, NULL,
121                 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
122         { LDAP_CONTROL_POST_READ,
123                 (int)offsetof(struct slap_control_ids, sc_postRead),
124                 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
125                 NULL, NULL,
126                 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
127         { LDAP_CONTROL_VALUESRETURNFILTER,
128                 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
129                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
130                 NULL, NULL,
131                 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
132         { LDAP_CONTROL_PAGEDRESULTS,
133                 (int)offsetof(struct slap_control_ids, sc_pagedResults),
134                 SLAP_CTRL_SEARCH,
135                 NULL, NULL,
136                 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
137 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
138         { LDAP_CONTROL_SORTREQUEST,
139                 (int)offsetof(struct slap_control_ids, sc_sortedResults),
140                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
141                 NULL, NULL,
142                 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
143 #endif
144         { LDAP_CONTROL_X_DOMAIN_SCOPE,
145                 (int)offsetof(struct slap_control_ids, sc_domainScope),
146                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
147                 NULL, NULL,
148                 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149         { LDAP_CONTROL_DONTUSECOPY,
150                 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
151                 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
152                 NULL, NULL,
153                 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
154         { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
155                 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
156                 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
157                 NULL, NULL,
158                 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
159 #ifdef SLAP_CONTROL_X_TREE_DELETE
160         { LDAP_CONTROL_X_TREE_DELETE,
161                 (int)offsetof(struct slap_control_ids, sc_treeDelete),
162                 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
163                 NULL, NULL,
164                 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
165 #endif
166         { LDAP_CONTROL_X_SEARCH_OPTIONS,
167                 (int)offsetof(struct slap_control_ids, sc_searchOptions),
168                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
169                 NULL, NULL,
170                 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
171         { LDAP_CONTROL_SUBENTRIES,
172                 (int)offsetof(struct slap_control_ids, sc_subentries),
173                 SLAP_CTRL_SEARCH,
174                 NULL, NULL,
175                 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
176         { LDAP_CONTROL_NOOP,
177                 (int)offsetof(struct slap_control_ids, sc_noOp),
178                 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
179                 NULL, NULL,
180                 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
181         { LDAP_CONTROL_RELAX,
182                 (int)offsetof(struct slap_control_ids, sc_relax),
183                 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
184                 NULL, NULL,
185                 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
186 #ifdef LDAP_X_TXN
187         { LDAP_CONTROL_X_TXN_SPEC,
188                 (int)offsetof(struct slap_control_ids, sc_txnSpec),
189                 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
190                 NULL, NULL,
191                 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
192 #endif
193         { LDAP_CONTROL_MANAGEDSAIT,
194                 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
195                 SLAP_CTRL_ACCESS,
196                 manageDSAit_extops, NULL,
197                 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
198         { LDAP_CONTROL_PROXY_AUTHZ,
199                 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
200                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
201                 proxy_authz_extops, NULL,
202                 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
203 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
204         { LDAP_CONTROL_X_SESSION_TRACKING,
205                 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
206                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
207                 NULL, NULL,
208                 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
209 #endif
210         { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
211 };
212
213 static struct slap_control *
214 find_ctrl( const char *oid );
215
216 /*
217  * Register a supported control.
218  *
219  * This can be called by an OpenLDAP plugin or, indirectly, by a
220  * SLAPI plugin calling slapi_register_supported_control().
221  *
222  * NOTE: if flags == 1 the control is replaced if already registered;
223  * otherwise registering an already registered control is not allowed.
224  */
225 int
226 register_supported_control2(const char *controloid,
227         slap_mask_t controlmask,
228         char **controlexops,
229         SLAP_CTRL_PARSE_FN *controlparsefn,
230         unsigned flags,
231         int *controlcid)
232 {
233         struct slap_control *sc = NULL;
234         int i;
235         BerVarray extendedopsbv = NULL;
236
237         if ( num_known_controls >= SLAP_MAX_CIDS ) {
238                 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
239                         " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
240                 SLAP_MAX_CIDS, 0, 0 );
241                 return LDAP_OTHER;
242         }
243
244         if ( controloid == NULL ) {
245                 return LDAP_PARAM_ERROR;
246         }
247
248         /* check if already registered */
249         for ( i = 0; slap_known_controls[ i ]; i++ ) {
250                 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
251                         if ( flags == 1 ) {
252                                 Debug( LDAP_DEBUG_TRACE,
253                                         "Control %s already registered; replacing.\n",
254                                         controloid, 0, 0 );
255                                 /* (find and) replace existing handler */
256                                 sc = find_ctrl( controloid );
257                                 assert( sc != NULL );
258                                 break;
259                         }
260
261                         Debug( LDAP_DEBUG_ANY,
262                                 "Control %s already registered.\n",
263                                 controloid, 0, 0 );
264                         return LDAP_PARAM_ERROR;
265                 }
266         }
267
268         /* turn compatible extended operations into bervals */
269         if ( controlexops != NULL ) {
270                 int i;
271
272                 for ( i = 0; controlexops[ i ]; i++ );
273
274                 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
275                 if ( extendedopsbv == NULL ) {
276                         return LDAP_NO_MEMORY;
277                 }
278
279                 for ( i = 0; controlexops[ i ]; i++ ) {
280                         ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
281                 }
282         }
283
284         if ( sc == NULL ) {
285                 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
286                 if ( sc == NULL ) {
287                         return LDAP_NO_MEMORY;
288                 }
289
290                 sc->sc_oid = ch_strdup( controloid );
291                 sc->sc_cid = num_known_controls;
292
293                 /* Update slap_known_controls, too. */
294                 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
295                 slap_known_controls[num_known_controls++] = NULL;
296
297                 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
298                 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
299
300         } else {
301                 if ( sc->sc_extendedopsbv ) {
302                         /* FIXME: in principle, we should rather merge
303                          * existing extops with those supported by the
304                          * new control handling implementation.
305                          * In fact, whether a control is compatible with
306                          * an extop should not be a matter of implementation.
307                          * We likely also need a means for a newly
308                          * registered extop to declare that it is
309                          * comptible with an already registered control.
310                          */
311                         ber_bvarray_free( sc->sc_extendedopsbv );
312                         sc->sc_extendedopsbv = NULL;
313                         sc->sc_extendedops = NULL;
314                 }
315         }
316
317         sc->sc_extendedopsbv = extendedopsbv;
318         sc->sc_mask = controlmask;
319         sc->sc_parse = controlparsefn;
320         if ( controlcid ) {
321                 *controlcid = sc->sc_cid;
322         }
323
324         return LDAP_SUCCESS;
325 }
326
327 /*
328  * One-time initialization of internal controls.
329  */
330 int
331 slap_controls_init( void )
332 {
333         int i, rc;
334
335         rc = LDAP_SUCCESS;
336
337         for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
338                 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
339                 rc = register_supported_control( control_defs[i].sc_oid,
340                         control_defs[i].sc_mask, control_defs[i].sc_extendedops,
341                         control_defs[i].sc_parse, cid );
342                 if ( rc != LDAP_SUCCESS ) break;
343         }
344
345         return rc;
346 }
347
348 /*
349  * Free memory associated with list of supported controls.
350  */
351 void
352 controls_destroy( void )
353 {
354         struct slap_control *sc;
355
356         while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
357                 sc = LDAP_SLIST_FIRST(&controls_list);
358                 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
359
360                 ch_free( sc->sc_oid );
361                 if ( sc->sc_extendedopsbv != NULL ) {
362                         ber_bvarray_free( sc->sc_extendedopsbv );
363                 }
364                 ch_free( sc );
365         }
366 }
367
368 /*
369  * Format the supportedControl attribute of the root DSE,
370  * detailing which controls are supported by the directory
371  * server.
372  */
373 int
374 controls_root_dse_info( Entry *e )
375 {
376         AttributeDescription *ad_supportedControl
377                 = slap_schema.si_ad_supportedControl;
378         struct berval vals[2];
379         struct slap_control *sc;
380
381         vals[1].bv_val = NULL;
382         vals[1].bv_len = 0;
383
384         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
385                 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
386
387                 vals[0].bv_val = sc->sc_oid;
388                 vals[0].bv_len = strlen( sc->sc_oid );
389
390                 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
391                         return -1;
392                 }
393         }
394
395         return 0;
396 }
397
398 /*
399  * Return a list of OIDs and operation masks for supported
400  * controls. Used by SLAPI.
401  */
402 int
403 get_supported_controls(char ***ctrloidsp,
404         slap_mask_t **ctrlmasks)
405 {
406         int n;
407         char **oids;
408         slap_mask_t *masks;
409         struct slap_control *sc;
410
411         n = 0;
412
413         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
414                 n++;
415         }
416
417         if ( n == 0 ) {
418                 *ctrloidsp = NULL;
419                 *ctrlmasks = NULL;
420                 return LDAP_SUCCESS;
421         }
422
423         oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
424         if ( oids == NULL ) {
425                 return LDAP_NO_MEMORY;
426         }
427         masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
428         if  ( masks == NULL ) {
429                 SLAP_FREE( oids );
430                 return LDAP_NO_MEMORY;
431         }
432
433         n = 0;
434
435         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
436                 oids[n] = ch_strdup( sc->sc_oid );
437                 masks[n] = sc->sc_mask;
438                 n++;
439         }
440         oids[n] = NULL;
441         masks[n] = 0;
442
443         *ctrloidsp = oids;
444         *ctrlmasks = masks;
445
446         return LDAP_SUCCESS;
447 }
448
449 /*
450  * Find a control given its OID.
451  */
452 static struct slap_control *
453 find_ctrl( const char *oid )
454 {
455         struct slap_control *sc;
456
457         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
458                 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
459                         return sc;
460                 }
461         }
462
463         return NULL;
464 }
465
466 int
467 slap_find_control_id(
468         const char *oid,
469         int *cid )
470 {
471         struct slap_control *ctrl = find_ctrl( oid );
472         if ( ctrl ) {
473                 if ( cid ) *cid = ctrl->sc_cid;
474                 return LDAP_SUCCESS;
475         }
476         return LDAP_CONTROL_NOT_FOUND;
477 }
478
479 int
480 slap_global_control( Operation *op, const char *oid, int *cid )
481 {
482         struct slap_control *ctrl = find_ctrl( oid );
483
484         if ( ctrl == NULL ) {
485                 /* should not be reachable */
486                 Debug( LDAP_DEBUG_ANY,
487                         "slap_global_control: unrecognized control: %s\n",      
488                         oid, 0, 0 );
489                 return LDAP_CONTROL_NOT_FOUND;
490         }
491
492         if ( cid ) *cid = ctrl->sc_cid;
493
494         if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
495                 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
496                 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
497         {
498                 return LDAP_COMPARE_TRUE;
499         }
500
501 #if 0
502         Debug( LDAP_DEBUG_TRACE,
503                 "slap_global_control: unavailable control: %s\n",      
504                 oid, 0, 0 );
505 #endif
506
507         return LDAP_COMPARE_FALSE;
508 }
509
510 void slap_free_ctrls(
511         Operation *op,
512         LDAPControl **ctrls )
513 {
514         int i;
515
516         for (i=0; ctrls[i]; i++) {
517                 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
518         }
519         op->o_tmpfree( ctrls, op->o_tmpmemctx );
520 }
521
522 int slap_parse_ctrl(
523         Operation *op,
524         SlapReply *rs,
525         LDAPControl *control,
526         const char **text )
527 {
528         struct slap_control *sc;
529
530         sc = find_ctrl( control->ldctl_oid );
531         if( sc != NULL ) {
532                 /* recognized control */
533                 slap_mask_t tagmask;
534                 switch( op->o_tag ) {
535                 case LDAP_REQ_ADD:
536                         tagmask = SLAP_CTRL_ADD;
537                         break;
538                 case LDAP_REQ_BIND:
539                         tagmask = SLAP_CTRL_BIND;
540                         break;
541                 case LDAP_REQ_COMPARE:
542                         tagmask = SLAP_CTRL_COMPARE;
543                         break;
544                 case LDAP_REQ_DELETE:
545                         tagmask = SLAP_CTRL_DELETE;
546                         break;
547                 case LDAP_REQ_MODIFY:
548                         tagmask = SLAP_CTRL_MODIFY;
549                         break;
550                 case LDAP_REQ_RENAME:
551                         tagmask = SLAP_CTRL_RENAME;
552                         break;
553                 case LDAP_REQ_SEARCH:
554                         tagmask = SLAP_CTRL_SEARCH;
555                         break;
556                 case LDAP_REQ_UNBIND:
557                         tagmask = SLAP_CTRL_UNBIND;
558                         break;
559                 case LDAP_REQ_ABANDON:
560                         tagmask = SLAP_CTRL_ABANDON;
561                         break;
562                 case LDAP_REQ_EXTENDED:
563                         tagmask=~0L;
564                         assert( op->ore_reqoid.bv_val != NULL );
565                         if( sc->sc_extendedopsbv != NULL ) {
566                                 int i;
567                                 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
568                                         if( bvmatch( &op->ore_reqoid,
569                                                 &sc->sc_extendedopsbv[i] ) )
570                                         {
571                                                 tagmask=0L;
572                                                 break;
573                                         }
574                                 }
575                         }
576                         break;
577                 default:
578                         *text = "controls internal error";
579                         return LDAP_OTHER;
580                 }
581
582                 if (( sc->sc_mask & tagmask ) == tagmask ) {
583                         /* available extension */
584                         int     rc;
585
586                         if( !sc->sc_parse ) {
587                                 *text = "not yet implemented";
588                                 return LDAP_OTHER;
589                         }
590
591                         rc = sc->sc_parse( op, rs, control );
592                         if ( rc ) {
593                                 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
594                                 return rc;
595                         }
596
597                 } else if( control->ldctl_iscritical ) {
598                         /* unavailable CRITICAL control */
599                         *text = "critical extension is unavailable";
600                         return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
601                 }
602         } else if( control->ldctl_iscritical ) {
603                 /* unrecognized CRITICAL control */
604                 *text = "critical extension is not recognized";
605                 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
606         }
607
608         return LDAP_SUCCESS;
609 }
610
611 int get_ctrls(
612         Operation *op,
613         SlapReply *rs,
614         int sendres )
615 {
616         int nctrls = 0;
617         ber_tag_t tag;
618         ber_len_t len;
619         char *opaque;
620         BerElement *ber = op->o_ber;
621         struct berval bv;
622
623         len = ber_pvt_ber_remaining(ber);
624
625         if( len == 0) {
626                 /* no controls */
627                 rs->sr_err = LDAP_SUCCESS;
628                 return rs->sr_err;
629         }
630
631         if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
632                 if( tag == LBER_ERROR ) {
633                         rs->sr_err = SLAPD_DISCONNECT;
634                         rs->sr_text = "unexpected data in PDU";
635                 }
636
637                 goto return_results;
638         }
639
640         Debug( LDAP_DEBUG_TRACE,
641                 "=> get_ctrls\n", 0, 0, 0 );
642
643         if( op->o_protocol < LDAP_VERSION3 ) {
644                 rs->sr_err = SLAPD_DISCONNECT;
645                 rs->sr_text = "controls require LDAPv3";
646                 goto return_results;
647         }
648
649         /* one for first control, one for termination */
650         op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
651
652 #if 0
653         if( op->ctrls == NULL ) {
654                 rs->sr_err = LDAP_NO_MEMORY;
655                 rs->sr_text = "no memory";
656                 goto return_results;
657         }
658 #endif
659
660         op->o_ctrls[nctrls] = NULL;
661
662         /* step through each element */
663         for( tag = ber_first_element( ber, &len, &opaque );
664                 tag != LBER_ERROR;
665                 tag = ber_next_element( ber, &len, opaque ) )
666         {
667                 LDAPControl *c;
668                 LDAPControl **tctrls;
669
670                 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
671                 memset(c, 0, sizeof(LDAPControl));
672
673                 /* allocate pointer space for current controls (nctrls)
674                  * + this control + extra NULL
675                  */
676                 tctrls = op->o_tmprealloc( op->o_ctrls,
677                         (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
678
679 #if 0
680                 if( tctrls == NULL ) {
681                         ch_free( c );
682                         ldap_controls_free(op->o_ctrls);
683                         op->o_ctrls = NULL;
684
685                         rs->sr_err = LDAP_NO_MEMORY;
686                         rs->sr_text = "no memory";
687                         goto return_results;
688                 }
689 #endif
690                 op->o_ctrls = tctrls;
691
692                 op->o_ctrls[nctrls++] = c;
693                 op->o_ctrls[nctrls] = NULL;
694
695                 tag = ber_scanf( ber, "{m" /*}*/, &bv );
696                 c->ldctl_oid = bv.bv_val;
697
698                 if( tag == LBER_ERROR ) {
699                         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
700                                 0, 0, 0 );
701
702                         slap_free_ctrls( op, op->o_ctrls );
703                         op->o_ctrls = NULL;
704                         rs->sr_err = SLAPD_DISCONNECT;
705                         rs->sr_text = "decoding controls error";
706                         goto return_results;
707
708                 } else if( c->ldctl_oid == NULL ) {
709                         Debug( LDAP_DEBUG_TRACE,
710                                 "get_ctrls: conn %lu got emtpy OID.\n",
711                                 op->o_connid, 0, 0 );
712
713                         slap_free_ctrls( op, op->o_ctrls );
714                         op->o_ctrls = NULL;
715                         rs->sr_err = LDAP_PROTOCOL_ERROR;
716                         rs->sr_text = "OID field is empty";
717                         goto return_results;
718                 }
719
720                 tag = ber_peek_tag( ber, &len );
721
722                 if( tag == LBER_BOOLEAN ) {
723                         ber_int_t crit;
724                         tag = ber_scanf( ber, "b", &crit );
725
726                         if( tag == LBER_ERROR ) {
727                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
728                                         0, 0, 0 );
729                                 slap_free_ctrls( op, op->o_ctrls );
730                                 op->o_ctrls = NULL;
731                                 rs->sr_err = SLAPD_DISCONNECT;
732                                 rs->sr_text = "decoding controls error";
733                                 goto return_results;
734                         }
735
736                         c->ldctl_iscritical = (crit != 0);
737                         tag = ber_peek_tag( ber, &len );
738                 }
739
740                 if( tag == LBER_OCTETSTRING ) {
741                         tag = ber_scanf( ber, "m", &c->ldctl_value );
742
743                         if( tag == LBER_ERROR ) {
744                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
745                                         "%s (%scritical): get value failed.\n",
746                                         op->o_connid, c->ldctl_oid,
747                                         c->ldctl_iscritical ? "" : "non" );
748                                 slap_free_ctrls( op, op->o_ctrls );
749                                 op->o_ctrls = NULL;
750                                 rs->sr_err = SLAPD_DISCONNECT;
751                                 rs->sr_text = "decoding controls error";
752                                 goto return_results;
753                         }
754                 }
755
756                 Debug( LDAP_DEBUG_TRACE,
757                         "=> get_ctrls: oid=\"%s\" (%scritical)\n",
758                         c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
759
760                 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
761                 if ( rs->sr_err != LDAP_SUCCESS ) {
762                         goto return_results;
763                 }
764         }
765
766 return_results:
767         Debug( LDAP_DEBUG_TRACE,
768                 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
769                 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
770
771         if( sendres && rs->sr_err != LDAP_SUCCESS ) {
772                 if( rs->sr_err == SLAPD_DISCONNECT ) {
773                         rs->sr_err = LDAP_PROTOCOL_ERROR;
774                         send_ldap_disconnect( op, rs );
775                         rs->sr_err = SLAPD_DISCONNECT;
776                 } else {
777                         send_ldap_result( op, rs );
778                 }
779         }
780
781         return rs->sr_err;
782 }
783
784 int
785 slap_remove_control(
786         Operation       *op,
787         SlapReply       *rs,
788         int             ctrl,
789         BI_chk_controls fnc )
790 {
791         int             i, j;
792
793         switch ( op->o_ctrlflag[ ctrl ] ) {
794         case SLAP_CONTROL_NONCRITICAL:
795                 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
796                         if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
797                                 slap_known_controls[ ctrl - 1 ] ) == 0 )
798                         {
799                                 j = i;
800                         }
801                 }
802
803                 if ( j == -1 ) {
804                         rs->sr_err = LDAP_OTHER;
805                         break;
806                 }
807
808                 if ( fnc ) {
809                         (void)fnc( op, rs );
810                 }
811
812                 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
813
814                 if ( i > 1 ) {
815                         AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
816                                 ( i - j ) * sizeof( LDAPControl * ) );
817
818                 } else {
819                         op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
820                         op->o_ctrls = NULL;
821                 }
822
823                 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
824
825                 Debug( LDAP_DEBUG_ANY, "%s: "
826                         "non-critical control \"%s\" not supported; stripped.\n",
827                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
828                 /* fall thru */
829
830         case SLAP_CONTROL_IGNORED:
831         case SLAP_CONTROL_NONE:
832                 rs->sr_err = SLAP_CB_CONTINUE;
833                 break;
834
835         case SLAP_CONTROL_CRITICAL:
836                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
837                 if ( fnc ) {
838                         (void)fnc( op, rs );
839                 }
840                 Debug( LDAP_DEBUG_ANY, "%s: "
841                         "critical control \"%s\" not supported.\n",
842                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
843                 break;
844
845         default:
846                 /* handle all cases! */
847                 assert( 0 );
848         }
849
850         return rs->sr_err;
851 }
852
853 static int parseDontUseCopy (
854         Operation *op,
855         SlapReply *rs,
856         LDAPControl *ctrl )
857 {
858         if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
859                 rs->sr_text = "dontUseCopy control specified multiple times";
860                 return LDAP_PROTOCOL_ERROR;
861         }
862
863         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
864                 rs->sr_text = "dontUseCopy control value not absent";
865                 return LDAP_PROTOCOL_ERROR;
866         }
867
868         if ( !ctrl->ldctl_iscritical ) {
869                 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
870                 return LDAP_PROTOCOL_ERROR;
871         }
872
873         op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
874         return LDAP_SUCCESS;
875 }
876
877 static int parseRelax (
878         Operation *op,
879         SlapReply *rs,
880         LDAPControl *ctrl )
881 {
882         if ( op->o_relax != SLAP_CONTROL_NONE ) {
883                 rs->sr_text = "relax control specified multiple times";
884                 return LDAP_PROTOCOL_ERROR;
885         }
886
887         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
888                 rs->sr_text = "relax control value not absent";
889                 return LDAP_PROTOCOL_ERROR;
890         }
891
892         op->o_relax = ctrl->ldctl_iscritical
893                 ? SLAP_CONTROL_CRITICAL
894                 : SLAP_CONTROL_NONCRITICAL;
895
896         return LDAP_SUCCESS;
897 }
898
899 static int parseManageDSAit (
900         Operation *op,
901         SlapReply *rs,
902         LDAPControl *ctrl )
903 {
904         if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
905                 rs->sr_text = "manageDSAit control specified multiple times";
906                 return LDAP_PROTOCOL_ERROR;
907         }
908
909         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
910                 rs->sr_text = "manageDSAit control value not absent";
911                 return LDAP_PROTOCOL_ERROR;
912         }
913
914         op->o_managedsait = ctrl->ldctl_iscritical
915                 ? SLAP_CONTROL_CRITICAL
916                 : SLAP_CONTROL_NONCRITICAL;
917
918         return LDAP_SUCCESS;
919 }
920
921 static int parseProxyAuthz (
922         Operation *op,
923         SlapReply *rs,
924         LDAPControl *ctrl )
925 {
926         int             rc;
927         struct berval   dn = BER_BVNULL;
928
929         if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
930                 rs->sr_text = "proxy authorization control specified multiple times";
931                 return LDAP_PROTOCOL_ERROR;
932         }
933
934         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
935                 rs->sr_text = "proxy authorization control value absent";
936                 return LDAP_PROTOCOL_ERROR;
937         }
938
939         if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
940                 && BER_BVISEMPTY( &op->o_ndn ) )
941         {
942                 rs->sr_text = "anonymous proxied authorization not allowed";
943                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
944         }
945
946         op->o_proxy_authz = ctrl->ldctl_iscritical
947                 ? SLAP_CONTROL_CRITICAL
948                 : SLAP_CONTROL_NONCRITICAL;
949
950         Debug( LDAP_DEBUG_ARGS,
951                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
952                 op->o_connid,
953                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
954                 0 );
955
956         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
957                 Debug( LDAP_DEBUG_TRACE,
958                         "parseProxyAuthz: conn=%lu anonymous\n", 
959                         op->o_connid, 0, 0 );
960
961                 /* anonymous */
962                 if ( !BER_BVISNULL( &op->o_ndn ) ) {
963                         op->o_ndn.bv_val[ 0 ] = '\0';
964                 }
965                 op->o_ndn.bv_len = 0;
966
967                 if ( !BER_BVISNULL( &op->o_dn ) ) {
968                         op->o_dn.bv_val[ 0 ] = '\0';
969                 }
970                 op->o_dn.bv_len = 0;
971
972                 return LDAP_SUCCESS;
973         }
974
975         rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
976                         NULL, &dn, SLAP_GETDN_AUTHZID );
977
978         /* FIXME: empty DN in proxyAuthz control should be legal... */
979         if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
980                 if ( dn.bv_val ) {
981                         ch_free( dn.bv_val );
982                 }
983                 rs->sr_text = "authzId mapping failed";
984                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
985         }
986
987         Debug( LDAP_DEBUG_TRACE,
988                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
989                 op->o_connid,
990                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
991
992         rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
993
994         if ( rc ) {
995                 ch_free( dn.bv_val );
996                 rs->sr_text = "not authorized to assume identity";
997                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
998         }
999
1000         ch_free( op->o_ndn.bv_val );
1001         ch_free( op->o_dn.bv_val );
1002
1003         /*
1004          * NOTE: since slap_sasl_getdn() returns a normalized dn,
1005          * from now on op->o_dn is normalized
1006          */
1007         op->o_ndn = dn;
1008         ber_dupbv( &op->o_dn, &dn );
1009
1010         Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1011             op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1012
1013         return LDAP_SUCCESS;
1014 }
1015
1016 static int parseNoOp (
1017         Operation *op,
1018         SlapReply *rs,
1019         LDAPControl *ctrl )
1020 {
1021         if ( op->o_noop != SLAP_CONTROL_NONE ) {
1022                 rs->sr_text = "noop control specified multiple times";
1023                 return LDAP_PROTOCOL_ERROR;
1024         }
1025
1026         if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1027                 rs->sr_text = "noop control value not empty";
1028                 return LDAP_PROTOCOL_ERROR;
1029         }
1030
1031         op->o_noop = ctrl->ldctl_iscritical
1032                 ? SLAP_CONTROL_CRITICAL
1033                 : SLAP_CONTROL_NONCRITICAL;
1034
1035         return LDAP_SUCCESS;
1036 }
1037
1038 static int parsePagedResults (
1039         Operation *op,
1040         SlapReply *rs,
1041         LDAPControl *ctrl )
1042 {
1043         int             rc = LDAP_SUCCESS;
1044         ber_tag_t       tag;
1045         ber_int_t       size;
1046         BerElement      *ber;
1047         struct berval   cookie = BER_BVNULL;
1048         PagedResultsState       *ps;
1049
1050         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1051                 rs->sr_text = "paged results control specified multiple times";
1052                 return LDAP_PROTOCOL_ERROR;
1053         }
1054
1055         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1056                 rs->sr_text = "paged results control value is absent";
1057                 return LDAP_PROTOCOL_ERROR;
1058         }
1059
1060         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1061                 rs->sr_text = "paged results control value is empty";
1062                 return LDAP_PROTOCOL_ERROR;
1063         }
1064
1065         /* Parse the control value
1066          *      realSearchControlValue ::= SEQUENCE {
1067          *              size    INTEGER (0..maxInt),
1068          *                              -- requested page size from client
1069          *                              -- result set size estimate from server
1070          *              cookie  OCTET STRING
1071          * }
1072          */
1073         ber = ber_init( &ctrl->ldctl_value );
1074         if ( ber == NULL ) {
1075                 rs->sr_text = "internal error";
1076                 return LDAP_OTHER;
1077         }
1078
1079         tag = ber_scanf( ber, "{im}", &size, &cookie );
1080
1081         if ( tag == LBER_ERROR ) {
1082                 rs->sr_text = "paged results control could not be decoded";
1083                 rc = LDAP_PROTOCOL_ERROR;
1084                 goto done;
1085         }
1086
1087         if ( size < 0 ) {
1088                 rs->sr_text = "paged results control size invalid";
1089                 rc = LDAP_PROTOCOL_ERROR;
1090                 goto done;
1091         }
1092
1093         ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1094         *ps = op->o_conn->c_pagedresults_state;
1095         ps->ps_size = size;
1096         op->o_pagedresults_state = ps;
1097
1098         /* NOTE: according to RFC 2696 3.:
1099
1100     If the page size is greater than or equal to the sizeLimit value, the
1101     server should ignore the control as the request can be satisfied in a
1102     single page.
1103          
1104          * NOTE: this assumes that the op->ors_slimit be set
1105          * before the controls are parsed.     
1106          */
1107                 
1108         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1109                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1110
1111         } else if ( ctrl->ldctl_iscritical ) {
1112                 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1113
1114         } else {
1115                 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1116         }
1117
1118 done:;
1119         (void)ber_free( ber, 1 );
1120         return rc;
1121 }
1122
1123 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1124 static int parseSortedResults (
1125         Operation *op,
1126         SlapReply *rs,
1127         LDAPControl *ctrl )
1128 {
1129         int             rc = LDAP_SUCCESS;
1130
1131         if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1132                 rs->sr_text = "sorted results control specified multiple times";
1133                 return LDAP_PROTOCOL_ERROR;
1134         }
1135
1136         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1137                 rs->sr_text = "sorted results control value is absent";
1138                 return LDAP_PROTOCOL_ERROR;
1139         }
1140
1141         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1142                 rs->sr_text = "sorted results control value is empty";
1143                 return LDAP_PROTOCOL_ERROR;
1144         }
1145
1146         /* blow off parsing the value */
1147
1148         op->o_sortedresults = ctrl->ldctl_iscritical
1149                 ? SLAP_CONTROL_CRITICAL
1150                 : SLAP_CONTROL_NONCRITICAL;
1151
1152         return rc;
1153 }
1154 #endif
1155
1156 static int parseAssert (
1157         Operation *op,
1158         SlapReply *rs,
1159         LDAPControl *ctrl )
1160 {
1161         BerElement      *ber;
1162         struct berval   fstr = BER_BVNULL;
1163
1164         if ( op->o_assert != SLAP_CONTROL_NONE ) {
1165                 rs->sr_text = "assert control specified multiple times";
1166                 return LDAP_PROTOCOL_ERROR;
1167         }
1168
1169         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1170                 rs->sr_text = "assert control value is absent";
1171                 return LDAP_PROTOCOL_ERROR;
1172         }
1173
1174         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1175                 rs->sr_text = "assert control value is empty";
1176                 return LDAP_PROTOCOL_ERROR;
1177         }
1178
1179         ber = ber_init( &(ctrl->ldctl_value) );
1180         if (ber == NULL) {
1181                 rs->sr_text = "assert control: internal error";
1182                 return LDAP_OTHER;
1183         }
1184         
1185         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1186                 &rs->sr_text);
1187         (void) ber_free( ber, 1 );
1188         if( rs->sr_err != LDAP_SUCCESS ) {
1189                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1190                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1191                         send_ldap_disconnect( op, rs );
1192                         rs->sr_err = SLAPD_DISCONNECT;
1193                 } else {
1194                         send_ldap_result( op, rs );
1195                 }
1196                 if( op->o_assertion != NULL ) {
1197                         filter_free_x( op, op->o_assertion );
1198                 }
1199                 return rs->sr_err;
1200         }
1201
1202 #ifdef LDAP_DEBUG
1203         filter2bv_x( op, op->o_assertion, &fstr );
1204
1205         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1206                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1207         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1208 #endif
1209
1210         op->o_assert = ctrl->ldctl_iscritical
1211                 ? SLAP_CONTROL_CRITICAL
1212                 : SLAP_CONTROL_NONCRITICAL;
1213
1214         rs->sr_err = LDAP_SUCCESS;
1215         return LDAP_SUCCESS;
1216 }
1217
1218 static int parsePreRead (
1219         Operation *op,
1220         SlapReply *rs,
1221         LDAPControl *ctrl )
1222 {
1223         ber_len_t siz, off, i;
1224         AttributeName *an = NULL;
1225         BerElement      *ber;
1226
1227         if ( op->o_preread != SLAP_CONTROL_NONE ) {
1228                 rs->sr_text = "preread control specified multiple times";
1229                 return LDAP_PROTOCOL_ERROR;
1230         }
1231
1232         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1233                 rs->sr_text = "preread control value is absent";
1234                 return LDAP_PROTOCOL_ERROR;
1235         }
1236
1237         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1238                 rs->sr_text = "preread control value is empty";
1239                 return LDAP_PROTOCOL_ERROR;
1240         }
1241
1242 #ifdef LDAP_X_TXN
1243         if ( op->o_txnSpec ) { /* temporary limitation */
1244                 rs->sr_text = "cannot perform pre-read in transaction";
1245                 return LDAP_UNWILLING_TO_PERFORM;
1246         }
1247 #endif
1248
1249         ber = ber_init( &(ctrl->ldctl_value) );
1250         if (ber == NULL) {
1251                 rs->sr_text = "preread control: internal error";
1252                 return LDAP_OTHER;
1253         }
1254
1255         rs->sr_err = LDAP_SUCCESS;
1256
1257         siz = sizeof( AttributeName );
1258         off = offsetof( AttributeName, an_name );
1259         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1260                 rs->sr_text = "preread control: decoding error";
1261                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1262                 goto done;
1263         }
1264
1265         for( i=0; i<siz; i++ ) {
1266                 const char      *dummy = NULL;
1267
1268                 an[i].an_desc = NULL;
1269                 an[i].an_oc = NULL;
1270                 an[i].an_oc_exclude = 0;
1271                 rs->sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1272                 if ( rs->sr_err != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1273                         rs->sr_text = dummy
1274                                 ? dummy
1275                                 : "postread control: unknown attributeType";
1276                         goto done;
1277                 }
1278         }
1279
1280         op->o_preread = ctrl->ldctl_iscritical
1281                 ? SLAP_CONTROL_CRITICAL
1282                 : SLAP_CONTROL_NONCRITICAL;
1283
1284         op->o_preread_attrs = an;
1285
1286 done:
1287         (void) ber_free( ber, 1 );
1288         return rs->sr_err;
1289 }
1290
1291 static int parsePostRead (
1292         Operation *op,
1293         SlapReply *rs,
1294         LDAPControl *ctrl )
1295 {
1296         ber_len_t siz, off, i;
1297         AttributeName *an = NULL;
1298         BerElement      *ber;
1299
1300         if ( op->o_postread != SLAP_CONTROL_NONE ) {
1301                 rs->sr_text = "postread control specified multiple times";
1302                 return LDAP_PROTOCOL_ERROR;
1303         }
1304
1305         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1306                 rs->sr_text = "postread control value is absent";
1307                 return LDAP_PROTOCOL_ERROR;
1308         }
1309
1310         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1311                 rs->sr_text = "postread control value is empty";
1312                 return LDAP_PROTOCOL_ERROR;
1313         }
1314
1315 #ifdef LDAP_X_TXN
1316         if ( op->o_txnSpec ) { /* temporary limitation */
1317                 rs->sr_text = "cannot perform post-read in transaction";
1318                 return LDAP_UNWILLING_TO_PERFORM;
1319         }
1320 #endif
1321
1322         ber = ber_init( &(ctrl->ldctl_value) );
1323         if (ber == NULL) {
1324                 rs->sr_text = "postread control: internal error";
1325                 return LDAP_OTHER;
1326         }
1327
1328         rs->sr_err = LDAP_SUCCESS;
1329         siz = sizeof( AttributeName );
1330         off = offsetof( AttributeName, an_name );
1331         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1332                 rs->sr_text = "postread control: decoding error";
1333                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1334                 goto done;
1335         }
1336
1337         for ( i = 0; i < siz; i++ ) {
1338                 const char      *dummy = NULL;
1339                 int             rc;
1340
1341                 an[i].an_desc = NULL;
1342                 an[i].an_oc = NULL;
1343                 an[i].an_oc_exclude = 0;
1344                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1345                 if ( rc != LDAP_SUCCESS ) {
1346                         int                     i;
1347                         static struct berval    special_attrs[] = {
1348                                 BER_BVC( LDAP_NO_ATTRS ),
1349                                 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1350                                 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1351                                 BER_BVNULL
1352                         };
1353
1354                         /* deal with special attribute types */
1355                         for ( i = 0; !BER_BVISNULL( &special_attrs[ i ] ); i++ ) {
1356                                 if ( bvmatch( &an[i].an_name, &special_attrs[ i ] ) ) {
1357                                         break;
1358                                 }
1359                         }
1360
1361                         if ( BER_BVISNULL( &special_attrs[ i ] ) && ctrl->ldctl_iscritical ) {
1362                                 rs->sr_err = rc;
1363                                 rs->sr_text = dummy
1364                                         ? dummy
1365                                         : "postread control: unknown attributeType";
1366                                 goto done;
1367                         }
1368                 }
1369         }
1370
1371         op->o_postread = ctrl->ldctl_iscritical
1372                 ? SLAP_CONTROL_CRITICAL
1373                 : SLAP_CONTROL_NONCRITICAL;
1374
1375         op->o_postread_attrs = an;
1376
1377 done:
1378         (void) ber_free( ber, 1 );
1379         return rs->sr_err;
1380 }
1381
1382 static int parseValuesReturnFilter (
1383         Operation *op,
1384         SlapReply *rs,
1385         LDAPControl *ctrl )
1386 {
1387         BerElement      *ber;
1388         struct berval   fstr = BER_BVNULL;
1389
1390         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1391                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1392                 return LDAP_PROTOCOL_ERROR;
1393         }
1394
1395         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1396                 rs->sr_text = "valuesReturnFilter control value is absent";
1397                 return LDAP_PROTOCOL_ERROR;
1398         }
1399
1400         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1401                 rs->sr_text = "valuesReturnFilter control value is empty";
1402                 return LDAP_PROTOCOL_ERROR;
1403         }
1404
1405         ber = ber_init( &(ctrl->ldctl_value) );
1406         if (ber == NULL) {
1407                 rs->sr_text = "internal error";
1408                 return LDAP_OTHER;
1409         }
1410         
1411         rs->sr_err = get_vrFilter( op, ber,
1412                 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1413
1414         (void) ber_free( ber, 1 );
1415
1416         if( rs->sr_err != LDAP_SUCCESS ) {
1417                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1418                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1419                         send_ldap_disconnect( op, rs );
1420                         rs->sr_err = SLAPD_DISCONNECT;
1421                 } else {
1422                         send_ldap_result( op, rs );
1423                 }
1424                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1425         }
1426 #ifdef LDAP_DEBUG
1427         else {
1428                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1429         }
1430
1431         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1432                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1433         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1434 #endif
1435
1436         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1437                 ? SLAP_CONTROL_CRITICAL
1438                 : SLAP_CONTROL_NONCRITICAL;
1439
1440         rs->sr_err = LDAP_SUCCESS;
1441         return LDAP_SUCCESS;
1442 }
1443
1444 static int parseSubentries (
1445         Operation *op,
1446         SlapReply *rs,
1447         LDAPControl *ctrl )
1448 {
1449         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1450                 rs->sr_text = "subentries control specified multiple times";
1451                 return LDAP_PROTOCOL_ERROR;
1452         }
1453
1454         /* FIXME: should use BER library */
1455         if( ( ctrl->ldctl_value.bv_len != 3 )
1456                 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1457                 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1458         {
1459                 rs->sr_text = "subentries control value encoding is bogus";
1460                 return LDAP_PROTOCOL_ERROR;
1461         }
1462
1463         op->o_subentries = ctrl->ldctl_iscritical
1464                 ? SLAP_CONTROL_CRITICAL
1465                 : SLAP_CONTROL_NONCRITICAL;
1466
1467         if (ctrl->ldctl_value.bv_val[2]) {
1468                 set_subentries_visibility( op );
1469         }
1470
1471         return LDAP_SUCCESS;
1472 }
1473
1474 static int parsePermissiveModify (
1475         Operation *op,
1476         SlapReply *rs,
1477         LDAPControl *ctrl )
1478 {
1479         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1480                 rs->sr_text = "permissiveModify control specified multiple times";
1481                 return LDAP_PROTOCOL_ERROR;
1482         }
1483
1484         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1485                 rs->sr_text = "permissiveModify control value not absent";
1486                 return LDAP_PROTOCOL_ERROR;
1487         }
1488
1489         op->o_permissive_modify = ctrl->ldctl_iscritical
1490                 ? SLAP_CONTROL_CRITICAL
1491                 : SLAP_CONTROL_NONCRITICAL;
1492
1493         return LDAP_SUCCESS;
1494 }
1495
1496 static int parseDomainScope (
1497         Operation *op,
1498         SlapReply *rs,
1499         LDAPControl *ctrl )
1500 {
1501         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1502                 rs->sr_text = "domainScope control specified multiple times";
1503                 return LDAP_PROTOCOL_ERROR;
1504         }
1505
1506         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1507                 rs->sr_text = "domainScope control value not empty";
1508                 return LDAP_PROTOCOL_ERROR;
1509         }
1510
1511         op->o_domain_scope = ctrl->ldctl_iscritical
1512                 ? SLAP_CONTROL_CRITICAL
1513                 : SLAP_CONTROL_NONCRITICAL;
1514
1515         return LDAP_SUCCESS;
1516 }
1517
1518 #ifdef SLAP_CONTROL_X_TREE_DELETE
1519 static int parseTreeDelete (
1520         Operation *op,
1521         SlapReply *rs,
1522         LDAPControl *ctrl )
1523 {
1524         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1525                 rs->sr_text = "treeDelete control specified multiple times";
1526                 return LDAP_PROTOCOL_ERROR;
1527         }
1528
1529         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1530                 rs->sr_text = "treeDelete control value not absent";
1531                 return LDAP_PROTOCOL_ERROR;
1532         }
1533
1534         op->o_tree_delete = ctrl->ldctl_iscritical
1535                 ? SLAP_CONTROL_CRITICAL
1536                 : SLAP_CONTROL_NONCRITICAL;
1537
1538         return LDAP_SUCCESS;
1539 }
1540 #endif
1541
1542 static int parseSearchOptions (
1543         Operation *op,
1544         SlapReply *rs,
1545         LDAPControl *ctrl )
1546 {
1547         BerElement *ber;
1548         ber_int_t search_flags;
1549         ber_tag_t tag;
1550
1551         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1552                 rs->sr_text = "searchOptions control value is absent";
1553                 return LDAP_PROTOCOL_ERROR;
1554         }
1555
1556         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1557                 rs->sr_text = "searchOptions control value is empty";
1558                 return LDAP_PROTOCOL_ERROR;
1559         }
1560
1561         ber = ber_init( &ctrl->ldctl_value );
1562         if( ber == NULL ) {
1563                 rs->sr_text = "internal error";
1564                 return LDAP_OTHER;
1565         }
1566
1567         tag = ber_scanf( ber, "{i}", &search_flags );
1568         (void) ber_free( ber, 1 );
1569
1570         if ( tag == LBER_ERROR ) {
1571                 rs->sr_text = "searchOptions control decoding error";
1572                 return LDAP_PROTOCOL_ERROR;
1573         }
1574
1575         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1576                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1577                         rs->sr_text = "searchOptions control specified multiple times "
1578                                 "or with domainScope control";
1579                         return LDAP_PROTOCOL_ERROR;
1580                 }
1581
1582                 op->o_domain_scope = ctrl->ldctl_iscritical
1583                         ? SLAP_CONTROL_CRITICAL
1584                         : SLAP_CONTROL_NONCRITICAL;
1585         }
1586
1587         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1588                 /* Other search flags not recognised so far,
1589                  * including:
1590                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOM
1591                  */
1592                 rs->sr_text = "searchOptions contained unrecognized flag";
1593                 return LDAP_UNWILLING_TO_PERFORM;
1594         }
1595
1596         return LDAP_SUCCESS;
1597 }
1598
1599 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1600 static int parseSessionTracking(
1601         Operation *op,
1602         SlapReply *rs,
1603         LDAPControl *ctrl )
1604 {
1605         BerElement              *ber;
1606         ber_tag_t               tag;
1607         ber_len_t               len;
1608         int                     rc;
1609
1610         struct berval           sessionSourceIp = BER_BVNULL,
1611                                 sessionSourceName = BER_BVNULL,
1612                                 formatOID = BER_BVNULL,
1613                                 sessionTrackingIdentifier = BER_BVNULL;
1614
1615         size_t                  st_len, st_pos;
1616
1617         if ( ctrl->ldctl_iscritical ) {
1618                 rs->sr_text = "sessionTracking criticality is TRUE";
1619                 return LDAP_PROTOCOL_ERROR;
1620         }
1621
1622         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1623                 rs->sr_text = "sessionTracking control value is absent";
1624                 return LDAP_PROTOCOL_ERROR;
1625         }
1626
1627         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1628                 rs->sr_text = "sessionTracking control value is empty";
1629                 return LDAP_PROTOCOL_ERROR;
1630         }
1631
1632         ber = ber_init( &ctrl->ldctl_value );
1633         if ( ber == NULL ) {
1634                 rs->sr_text = "internal error";
1635                 return LDAP_OTHER;
1636         }
1637
1638         tag = ber_skip_tag( ber, &len );
1639         if ( tag != LBER_SEQUENCE ) {
1640                 tag = LBER_ERROR;
1641                 goto error;
1642         }
1643
1644         /* sessionSourceIp */
1645         tag = ber_peek_tag( ber, &len );
1646         if ( tag == LBER_DEFAULT ) {
1647                 tag = LBER_ERROR;
1648                 goto error;
1649         }
1650
1651         if ( len == 0 ) {
1652                 tag = ber_skip_tag( ber, &len );
1653
1654         } else if ( len > 128 ) {
1655                 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1656                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1657                 goto error;
1658
1659         } else {
1660                 tag = ber_scanf( ber, "m", &sessionSourceIp );
1661         }
1662
1663         if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1664                 BER_BVZERO( &sessionSourceIp );
1665         }
1666
1667         /* sessionSourceName */
1668         tag = ber_peek_tag( ber, &len );
1669         if ( tag == LBER_DEFAULT ) {
1670                 tag = LBER_ERROR;
1671                 goto error;
1672         }
1673
1674         if ( len == 0 ) {
1675                 tag = ber_skip_tag( ber, &len );
1676
1677         } else if ( len > 65536 ) {
1678                 rs->sr_text = "sessionTracking.sessionSourceName too long";
1679                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1680                 goto error;
1681
1682         } else {
1683                 tag = ber_scanf( ber, "m", &sessionSourceName );
1684         }
1685
1686         if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1687                 BER_BVZERO( &sessionSourceName );
1688         }
1689
1690         /* formatOID */
1691         tag = ber_peek_tag( ber, &len );
1692         if ( tag == LBER_DEFAULT ) {
1693                 tag = LBER_ERROR;
1694                 goto error;
1695         }
1696
1697         if ( len == 0 ) {
1698                 rs->sr_text = "sessionTracking.formatOID empty";
1699                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1700                 goto error;
1701
1702         } else if ( len > 1024 ) {
1703                 rs->sr_text = "sessionTracking.formatOID too long";
1704                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1705                 goto error;
1706
1707         } else {
1708                 tag = ber_scanf( ber, "m", &formatOID );
1709         }
1710
1711         rc = numericoidValidate( NULL, &formatOID );
1712         if ( rc != LDAP_SUCCESS ) {
1713                 rs->sr_text = "sessionTracking.formatOID invalid";
1714                 goto error;
1715         }
1716
1717         /* sessionTrackingIdentifier */
1718         tag = ber_peek_tag( ber, &len );
1719         if ( tag == LBER_DEFAULT ) {
1720                 tag = LBER_ERROR;
1721                 goto error;
1722         }
1723
1724         if ( len == 0 ) {
1725                 tag = ber_skip_tag( ber, &len );
1726
1727         } else {
1728                 /* note: should not be more than 65536... */
1729                 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1730         }
1731
1732         if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1733                 BER_BVZERO( &sessionTrackingIdentifier );
1734         }
1735
1736         /* closure */
1737         tag = ber_skip_tag( ber, &len );
1738         if ( tag == LBER_DEFAULT && len == 0 ) {
1739                 tag = 0;
1740         }
1741
1742         st_len = 0;
1743         if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1744                 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1745         }
1746         if ( !BER_BVISNULL( &sessionSourceName ) ) {
1747                 if ( st_len ) st_len++;
1748                 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1749         }
1750         if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1751                 if ( st_len ) st_len++;
1752                 st_len += STRLENOF( "ID=" ) + sessionTrackingIdentifier.bv_len;
1753         }
1754
1755         if ( st_len == 0 ) {
1756                 goto error;
1757         }
1758
1759         st_len += STRLENOF( " []" );
1760         st_pos = strlen( op->o_log_prefix );
1761
1762         if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1763                 char    *ptr = &op->o_log_prefix[ st_pos ];
1764
1765                 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1766
1767                 st_len = 0;
1768                 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1769                         ptr = lutil_strcopy( ptr, "IP=" );
1770                         ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1771                         st_len++;
1772                 }
1773
1774                 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1775                         if ( st_len ) *ptr++ = ' ';
1776                         ptr = lutil_strcopy( ptr, "NAME=" );
1777                         ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1778                         st_len++;
1779                 }
1780
1781                 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1782                         if ( st_len ) *ptr++ = ' ';
1783                         ptr = lutil_strcopy( ptr, "ID=" );
1784                         ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1785                 }
1786
1787                 *ptr++ = /*[*/ ']';
1788                 *ptr = '\0';
1789         }
1790
1791 error:;
1792         (void)ber_free( ber, 1 );
1793
1794         if ( tag == LBER_ERROR ) {
1795                 rs->sr_text = "sessionTracking control decoding error";
1796                 return LDAP_PROTOCOL_ERROR;
1797         }
1798
1799
1800         return rs->sr_err;
1801 }
1802 #endif