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