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