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