]> 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|SLAP_CTRL_HIDE,
164                 NULL, NULL,
165                 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
166         { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
167                 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
168                 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
169                 NULL, NULL,
170                 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
171 #ifdef SLAP_CONTROL_X_TREE_DELETE
172         { LDAP_CONTROL_X_TREE_DELETE,
173                 (int)offsetof(struct slap_control_ids, sc_treeDelete),
174                 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
175                 NULL, NULL,
176                 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
177 #endif
178         { LDAP_CONTROL_X_SEARCH_OPTIONS,
179                 (int)offsetof(struct slap_control_ids, sc_searchOptions),
180                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
181                 NULL, NULL,
182                 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
183         { LDAP_CONTROL_SUBENTRIES,
184                 (int)offsetof(struct slap_control_ids, sc_subentries),
185                 SLAP_CTRL_SEARCH,
186                 NULL, NULL,
187                 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
188         { LDAP_CONTROL_NOOP,
189                 (int)offsetof(struct slap_control_ids, sc_noOp),
190                 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
191                 NULL, NULL,
192                 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
193         { LDAP_CONTROL_RELAX,
194                 (int)offsetof(struct slap_control_ids, sc_relax),
195                 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
196                 NULL, NULL,
197                 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
198 #ifdef LDAP_X_TXN
199         { LDAP_CONTROL_X_TXN_SPEC,
200                 (int)offsetof(struct slap_control_ids, sc_txnSpec),
201                 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
202                 NULL, NULL,
203                 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
204 #endif
205         { LDAP_CONTROL_MANAGEDSAIT,
206                 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
207                 SLAP_CTRL_ACCESS,
208                 manageDSAit_extops, NULL,
209                 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
210         { LDAP_CONTROL_PROXY_AUTHZ,
211                 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
212                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
213                 proxy_authz_extops, NULL,
214                 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
215 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
216         { LDAP_CONTROL_X_SESSION_TRACKING,
217                 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
218                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
219                 session_tracking_extops, NULL,
220                 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
221 #endif
222 #ifdef SLAP_CONTROL_X_WHATFAILED
223         { LDAP_CONTROL_X_WHATFAILED,
224                 (int)offsetof(struct slap_control_ids, sc_whatFailed),
225                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
226                 NULL, NULL,
227                 parseWhatFailed, LDAP_SLIST_ENTRY_INITIALIZER(next) },
228 #endif
229
230         { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
231 };
232
233 static struct slap_control *
234 find_ctrl( const char *oid );
235
236 /*
237  * Register a supported control.
238  *
239  * This can be called by an OpenLDAP plugin or, indirectly, by a
240  * SLAPI plugin calling slapi_register_supported_control().
241  *
242  * NOTE: if flags == 1 the control is replaced if already registered;
243  * otherwise registering an already registered control is not allowed.
244  */
245 int
246 register_supported_control2(const char *controloid,
247         slap_mask_t controlmask,
248         char **controlexops,
249         SLAP_CTRL_PARSE_FN *controlparsefn,
250         unsigned flags,
251         int *controlcid)
252 {
253         struct slap_control *sc = NULL;
254         int i;
255         BerVarray extendedopsbv = NULL;
256
257         if ( num_known_controls >= SLAP_MAX_CIDS ) {
258                 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
259                         " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
260                 SLAP_MAX_CIDS, 0, 0 );
261                 return LDAP_OTHER;
262         }
263
264         if ( controloid == NULL ) {
265                 return LDAP_PARAM_ERROR;
266         }
267
268         /* check if already registered */
269         for ( i = 0; slap_known_controls[ i ]; i++ ) {
270                 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
271                         if ( flags == 1 ) {
272                                 Debug( LDAP_DEBUG_TRACE,
273                                         "Control %s already registered; replacing.\n",
274                                         controloid, 0, 0 );
275                                 /* (find and) replace existing handler */
276                                 sc = find_ctrl( controloid );
277                                 assert( sc != NULL );
278                                 break;
279                         }
280
281                         Debug( LDAP_DEBUG_ANY,
282                                 "Control %s already registered.\n",
283                                 controloid, 0, 0 );
284                         return LDAP_PARAM_ERROR;
285                 }
286         }
287
288         /* turn compatible extended operations into bervals */
289         if ( controlexops != NULL ) {
290                 int i;
291
292                 for ( i = 0; controlexops[ i ]; i++ );
293
294                 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
295                 if ( extendedopsbv == NULL ) {
296                         return LDAP_NO_MEMORY;
297                 }
298
299                 for ( i = 0; controlexops[ i ]; i++ ) {
300                         ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
301                 }
302         }
303
304         if ( sc == NULL ) {
305                 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
306                 if ( sc == NULL ) {
307                         return LDAP_NO_MEMORY;
308                 }
309
310                 sc->sc_oid = ch_strdup( controloid );
311                 sc->sc_cid = num_known_controls;
312
313                 /* Update slap_known_controls, too. */
314                 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
315                 slap_known_controls[num_known_controls++] = NULL;
316
317                 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
318                 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
319
320         } else {
321                 if ( sc->sc_extendedopsbv ) {
322                         /* FIXME: in principle, we should rather merge
323                          * existing extops with those supported by the
324                          * new control handling implementation.
325                          * In fact, whether a control is compatible with
326                          * an extop should not be a matter of implementation.
327                          * We likely also need a means for a newly
328                          * registered extop to declare that it is
329                          * comptible with an already registered control.
330                          */
331                         ber_bvarray_free( sc->sc_extendedopsbv );
332                         sc->sc_extendedopsbv = NULL;
333                         sc->sc_extendedops = NULL;
334                 }
335         }
336
337         sc->sc_extendedopsbv = extendedopsbv;
338         sc->sc_mask = controlmask;
339         sc->sc_parse = controlparsefn;
340         if ( controlcid ) {
341                 *controlcid = sc->sc_cid;
342         }
343
344         return LDAP_SUCCESS;
345 }
346
347 #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 get_ctrls(
715         Operation *op,
716         SlapReply *rs,
717         int sendres )
718 {
719         int nctrls = 0;
720         ber_tag_t tag;
721         ber_len_t len;
722         char *opaque;
723         BerElement *ber = op->o_ber;
724         struct berval bv;
725 #ifdef SLAP_CONTROL_X_WHATFAILED
726         /* NOTE: right now, slapd checks the validity of each control
727          * while parsing.  As a consequence, it can only detect one
728          * cause of failure at a time.  This results in returning
729          * exactly one OID with the whatFailed control, or no control
730          * at all.
731          */
732         char *failed_oid = NULL;
733 #endif
734
735         len = ber_pvt_ber_remaining(ber);
736
737         if( len == 0) {
738                 /* no controls */
739                 rs->sr_err = LDAP_SUCCESS;
740                 return rs->sr_err;
741         }
742
743         if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
744                 if( tag == LBER_ERROR ) {
745                         rs->sr_err = SLAPD_DISCONNECT;
746                         rs->sr_text = "unexpected data in PDU";
747                 }
748
749                 goto return_results;
750         }
751
752         Debug( LDAP_DEBUG_TRACE,
753                 "=> get_ctrls\n", 0, 0, 0 );
754
755         if( op->o_protocol < LDAP_VERSION3 ) {
756                 rs->sr_err = SLAPD_DISCONNECT;
757                 rs->sr_text = "controls require LDAPv3";
758                 goto return_results;
759         }
760
761         /* one for first control, one for termination */
762         op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
763
764 #if 0
765         if( op->ctrls == NULL ) {
766                 rs->sr_err = LDAP_NO_MEMORY;
767                 rs->sr_text = "no memory";
768                 goto return_results;
769         }
770 #endif
771
772         op->o_ctrls[nctrls] = NULL;
773
774         /* step through each element */
775         for( tag = ber_first_element( ber, &len, &opaque );
776                 tag != LBER_ERROR;
777                 tag = ber_next_element( ber, &len, opaque ) )
778         {
779                 LDAPControl *c;
780                 LDAPControl **tctrls;
781
782                 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
783                 memset(c, 0, sizeof(LDAPControl));
784
785                 /* allocate pointer space for current controls (nctrls)
786                  * + this control + extra NULL
787                  */
788                 tctrls = op->o_tmprealloc( op->o_ctrls,
789                         (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
790
791 #if 0
792                 if( tctrls == NULL ) {
793                         ch_free( c );
794                         ldap_controls_free(op->o_ctrls);
795                         op->o_ctrls = NULL;
796
797                         rs->sr_err = LDAP_NO_MEMORY;
798                         rs->sr_text = "no memory";
799                         goto return_results;
800                 }
801 #endif
802                 op->o_ctrls = tctrls;
803
804                 op->o_ctrls[nctrls++] = c;
805                 op->o_ctrls[nctrls] = NULL;
806
807                 tag = ber_scanf( ber, "{m" /*}*/, &bv );
808                 c->ldctl_oid = bv.bv_val;
809
810                 if( tag == LBER_ERROR ) {
811                         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
812                                 0, 0, 0 );
813
814                         slap_free_ctrls( op, op->o_ctrls );
815                         op->o_ctrls = NULL;
816                         rs->sr_err = SLAPD_DISCONNECT;
817                         rs->sr_text = "decoding controls error";
818                         goto return_results;
819
820                 } else if( c->ldctl_oid == NULL ) {
821                         Debug( LDAP_DEBUG_TRACE,
822                                 "get_ctrls: conn %lu got emtpy OID.\n",
823                                 op->o_connid, 0, 0 );
824
825                         slap_free_ctrls( op, op->o_ctrls );
826                         op->o_ctrls = NULL;
827                         rs->sr_err = LDAP_PROTOCOL_ERROR;
828                         rs->sr_text = "OID field is empty";
829                         goto return_results;
830                 }
831
832                 tag = ber_peek_tag( ber, &len );
833
834                 if( tag == LBER_BOOLEAN ) {
835                         ber_int_t crit;
836                         tag = ber_scanf( ber, "b", &crit );
837
838                         if( tag == LBER_ERROR ) {
839                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
840                                         0, 0, 0 );
841                                 slap_free_ctrls( op, op->o_ctrls );
842                                 op->o_ctrls = NULL;
843                                 rs->sr_err = SLAPD_DISCONNECT;
844                                 rs->sr_text = "decoding controls error";
845                                 goto return_results;
846                         }
847
848                         c->ldctl_iscritical = (crit != 0);
849                         tag = ber_peek_tag( ber, &len );
850                 }
851
852                 if( tag == LBER_OCTETSTRING ) {
853                         tag = ber_scanf( ber, "m", &c->ldctl_value );
854
855                         if( tag == LBER_ERROR ) {
856                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
857                                         "%s (%scritical): get value failed.\n",
858                                         op->o_connid, c->ldctl_oid,
859                                         c->ldctl_iscritical ? "" : "non" );
860                                 slap_free_ctrls( op, op->o_ctrls );
861                                 op->o_ctrls = NULL;
862                                 rs->sr_err = SLAPD_DISCONNECT;
863                                 rs->sr_text = "decoding controls error";
864                                 goto return_results;
865                         }
866                 }
867
868                 Debug( LDAP_DEBUG_TRACE,
869                         "=> get_ctrls: oid=\"%s\" (%scritical)\n",
870                         c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
871
872                 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
873                 if ( rs->sr_err != LDAP_SUCCESS ) {
874 #ifdef SLAP_CONTROL_X_WHATFAILED
875                         failed_oid = c->ldctl_oid;
876 #endif
877                         goto return_results;
878                 }
879         }
880
881 return_results:
882         Debug( LDAP_DEBUG_TRACE,
883                 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
884                 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
885
886         if( sendres && rs->sr_err != LDAP_SUCCESS ) {
887                 if( rs->sr_err == SLAPD_DISCONNECT ) {
888                         rs->sr_err = LDAP_PROTOCOL_ERROR;
889                         send_ldap_disconnect( op, rs );
890                         rs->sr_err = SLAPD_DISCONNECT;
891                 } else {
892 #ifdef SLAP_CONTROL_X_WHATFAILED
893                         /* might have not been parsed yet? */
894                         if ( failed_oid != NULL ) {
895                                 if ( !get_whatFailed( op ) ) {
896                                         /* look it up */
897
898                                         /* step through each remaining element */
899                                         for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
900                                         {
901                                                 LDAPControl c = { 0 };
902
903                                                 tag = ber_scanf( ber, "{m" /*}*/, &bv );
904                                                 c.ldctl_oid = bv.bv_val;
905
906                                                 if ( tag == LBER_ERROR ) {
907                                                         slap_free_ctrls( op, op->o_ctrls );
908                                                         op->o_ctrls = NULL;
909                                                         break;
910
911                                                 } else if ( c.ldctl_oid == NULL ) {
912                                                         slap_free_ctrls( op, op->o_ctrls );
913                                                         op->o_ctrls = NULL;
914                                                         break;
915                                                 }
916
917                                                 tag = ber_peek_tag( ber, &len );
918                                                 if ( tag == LBER_BOOLEAN ) {
919                                                         ber_int_t crit;
920                                                         tag = ber_scanf( ber, "b", &crit );
921                                                         if( tag == LBER_ERROR ) {
922                                                                 slap_free_ctrls( op, op->o_ctrls );
923                                                                 op->o_ctrls = NULL;
924                                                                 break;
925                                                         }
926
927                                                         tag = ber_peek_tag( ber, &len );
928                                                 }
929
930                                                 if ( tag == LBER_OCTETSTRING ) {
931                                                         tag = ber_scanf( ber, "m", &c.ldctl_value );
932
933                                                         if( tag == LBER_ERROR ) {
934                                                                 slap_free_ctrls( op, op->o_ctrls );
935                                                                 op->o_ctrls = NULL;
936                                                                 break;
937                                                         }
938                                                 }
939
940                                                 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
941                                                         const char *text;
942                                                         slap_parse_ctrl( op, rs, &c, &text );
943                                                         break;
944                                                 }
945                                         }
946                                 }
947
948                                 if ( get_whatFailed( op ) ) {
949                                         char *oids[ 2 ];
950                                         oids[ 0 ] = failed_oid;
951                                         oids[ 1 ] = NULL;
952                                         slap_ctrl_whatFailed_add( op, rs, oids );
953                                 }
954                         }
955 #endif
956
957                         send_ldap_result( op, rs );
958                 }
959         }
960
961         return rs->sr_err;
962 }
963
964 int
965 slap_remove_control(
966         Operation       *op,
967         SlapReply       *rs,
968         int             ctrl,
969         BI_chk_controls fnc )
970 {
971         int             i, j;
972
973         switch ( op->o_ctrlflag[ ctrl ] ) {
974         case SLAP_CONTROL_NONCRITICAL:
975                 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
976                         if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
977                                 slap_known_controls[ ctrl - 1 ] ) == 0 )
978                         {
979                                 j = i;
980                         }
981                 }
982
983                 if ( j == -1 ) {
984                         rs->sr_err = LDAP_OTHER;
985                         break;
986                 }
987
988                 if ( fnc ) {
989                         (void)fnc( op, rs );
990                 }
991
992                 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
993
994                 if ( i > 1 ) {
995                         AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
996                                 ( i - j ) * sizeof( LDAPControl * ) );
997
998                 } else {
999                         op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
1000                         op->o_ctrls = NULL;
1001                 }
1002
1003                 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
1004
1005                 Debug( LDAP_DEBUG_ANY, "%s: "
1006                         "non-critical control \"%s\" not supported; stripped.\n",
1007                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1008                 /* fall thru */
1009
1010         case SLAP_CONTROL_IGNORED:
1011         case SLAP_CONTROL_NONE:
1012                 rs->sr_err = SLAP_CB_CONTINUE;
1013                 break;
1014
1015         case SLAP_CONTROL_CRITICAL:
1016                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
1017                 if ( fnc ) {
1018                         (void)fnc( op, rs );
1019                 }
1020                 Debug( LDAP_DEBUG_ANY, "%s: "
1021                         "critical control \"%s\" not supported.\n",
1022                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1023                 break;
1024
1025         default:
1026                 /* handle all cases! */
1027                 assert( 0 );
1028         }
1029
1030         return rs->sr_err;
1031 }
1032
1033 static int parseDontUseCopy (
1034         Operation *op,
1035         SlapReply *rs,
1036         LDAPControl *ctrl )
1037 {
1038         if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
1039                 rs->sr_text = "dontUseCopy control specified multiple times";
1040                 return LDAP_PROTOCOL_ERROR;
1041         }
1042
1043         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1044                 rs->sr_text = "dontUseCopy control value not absent";
1045                 return LDAP_PROTOCOL_ERROR;
1046         }
1047
1048         if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
1049                 && !ctrl->ldctl_iscritical )
1050         {
1051                 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
1052                 return LDAP_PROTOCOL_ERROR;
1053         }
1054
1055         op->o_dontUseCopy = ctrl->ldctl_iscritical
1056                 ? SLAP_CONTROL_CRITICAL
1057                 : SLAP_CONTROL_NONCRITICAL;
1058
1059         return LDAP_SUCCESS;
1060 }
1061
1062 static int parseRelax (
1063         Operation *op,
1064         SlapReply *rs,
1065         LDAPControl *ctrl )
1066 {
1067         if ( op->o_relax != SLAP_CONTROL_NONE ) {
1068                 rs->sr_text = "relax control specified multiple times";
1069                 return LDAP_PROTOCOL_ERROR;
1070         }
1071
1072         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1073                 rs->sr_text = "relax control value not absent";
1074                 return LDAP_PROTOCOL_ERROR;
1075         }
1076
1077         op->o_relax = ctrl->ldctl_iscritical
1078                 ? SLAP_CONTROL_CRITICAL
1079                 : SLAP_CONTROL_NONCRITICAL;
1080
1081         return LDAP_SUCCESS;
1082 }
1083
1084 static int parseManageDSAit (
1085         Operation *op,
1086         SlapReply *rs,
1087         LDAPControl *ctrl )
1088 {
1089         if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1090                 rs->sr_text = "manageDSAit control specified multiple times";
1091                 return LDAP_PROTOCOL_ERROR;
1092         }
1093
1094         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1095                 rs->sr_text = "manageDSAit control value not absent";
1096                 return LDAP_PROTOCOL_ERROR;
1097         }
1098
1099         op->o_managedsait = ctrl->ldctl_iscritical
1100                 ? SLAP_CONTROL_CRITICAL
1101                 : SLAP_CONTROL_NONCRITICAL;
1102
1103         return LDAP_SUCCESS;
1104 }
1105
1106 static int parseProxyAuthz (
1107         Operation *op,
1108         SlapReply *rs,
1109         LDAPControl *ctrl )
1110 {
1111         int             rc;
1112         struct berval   dn = BER_BVNULL;
1113
1114         if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1115                 rs->sr_text = "proxy authorization control specified multiple times";
1116                 return LDAP_PROTOCOL_ERROR;
1117         }
1118
1119         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1120                 rs->sr_text = "proxy authorization control value absent";
1121                 return LDAP_PROTOCOL_ERROR;
1122         }
1123
1124         if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1125                 && !ctrl->ldctl_iscritical )
1126         {
1127                 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1128                 return LDAP_PROTOCOL_ERROR;
1129         }
1130
1131         if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1132                 && BER_BVISEMPTY( &op->o_ndn ) )
1133         {
1134                 rs->sr_text = "anonymous proxied authorization not allowed";
1135                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1136         }
1137
1138         op->o_proxy_authz = ctrl->ldctl_iscritical
1139                 ? SLAP_CONTROL_CRITICAL
1140                 : SLAP_CONTROL_NONCRITICAL;
1141
1142         Debug( LDAP_DEBUG_ARGS,
1143                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
1144                 op->o_connid,
1145                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
1146                 0 );
1147
1148         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1149                 Debug( LDAP_DEBUG_TRACE,
1150                         "parseProxyAuthz: conn=%lu anonymous\n", 
1151                         op->o_connid, 0, 0 );
1152
1153                 /* anonymous */
1154                 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1155                         op->o_ndn.bv_val[ 0 ] = '\0';
1156                 }
1157                 op->o_ndn.bv_len = 0;
1158
1159                 if ( !BER_BVISNULL( &op->o_dn ) ) {
1160                         op->o_dn.bv_val[ 0 ] = '\0';
1161                 }
1162                 op->o_dn.bv_len = 0;
1163
1164                 return LDAP_SUCCESS;
1165         }
1166
1167         rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1168                         NULL, &dn, SLAP_GETDN_AUTHZID );
1169
1170         /* FIXME: empty DN in proxyAuthz control should be legal... */
1171         if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1172                 if ( dn.bv_val ) {
1173                         ch_free( dn.bv_val );
1174                 }
1175                 rs->sr_text = "authzId mapping failed";
1176                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1177         }
1178
1179         Debug( LDAP_DEBUG_TRACE,
1180                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
1181                 op->o_connid,
1182                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1183
1184         rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1185
1186         if ( rc ) {
1187                 ch_free( dn.bv_val );
1188                 rs->sr_text = "not authorized to assume identity";
1189                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1190         }
1191
1192         ch_free( op->o_ndn.bv_val );
1193
1194         /*
1195          * NOTE: since slap_sasl_getdn() returns a normalized dn,
1196          * from now on op->o_dn is normalized
1197          */
1198         op->o_ndn = dn;
1199         ber_bvreplace( &op->o_dn, &dn );
1200
1201         Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1202             op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1203
1204         return LDAP_SUCCESS;
1205 }
1206
1207 static int parseNoOp (
1208         Operation *op,
1209         SlapReply *rs,
1210         LDAPControl *ctrl )
1211 {
1212         if ( op->o_noop != SLAP_CONTROL_NONE ) {
1213                 rs->sr_text = "noop control specified multiple times";
1214                 return LDAP_PROTOCOL_ERROR;
1215         }
1216
1217         if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1218                 rs->sr_text = "noop control value not empty";
1219                 return LDAP_PROTOCOL_ERROR;
1220         }
1221
1222         op->o_noop = ctrl->ldctl_iscritical
1223                 ? SLAP_CONTROL_CRITICAL
1224                 : SLAP_CONTROL_NONCRITICAL;
1225
1226         return LDAP_SUCCESS;
1227 }
1228
1229 static int parsePagedResults (
1230         Operation *op,
1231         SlapReply *rs,
1232         LDAPControl *ctrl )
1233 {
1234         BerElementBuffer berbuf;
1235         BerElement      *ber = (BerElement *)&berbuf;
1236         struct berval   cookie;
1237         PagedResultsState       *ps;
1238         int             rc = LDAP_SUCCESS;
1239         ber_tag_t       tag;
1240         ber_int_t       size;
1241
1242         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1243                 rs->sr_text = "paged results control specified multiple times";
1244                 return LDAP_PROTOCOL_ERROR;
1245         }
1246
1247         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1248                 rs->sr_text = "paged results control value is absent";
1249                 return LDAP_PROTOCOL_ERROR;
1250         }
1251
1252         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1253                 rs->sr_text = "paged results control value is empty";
1254                 return LDAP_PROTOCOL_ERROR;
1255         }
1256
1257         /* Parse the control value
1258          *      realSearchControlValue ::= SEQUENCE {
1259          *              size    INTEGER (0..maxInt),
1260          *                              -- requested page size from client
1261          *                              -- result set size estimate from server
1262          *              cookie  OCTET STRING
1263          * }
1264          */
1265         ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1266
1267         tag = ber_scanf( ber, "{im}", &size, &cookie );
1268
1269         if ( tag == LBER_ERROR ) {
1270                 rs->sr_text = "paged results control could not be decoded";
1271                 rc = LDAP_PROTOCOL_ERROR;
1272                 goto done;
1273         }
1274
1275         if ( size < 0 ) {
1276                 rs->sr_text = "paged results control size invalid";
1277                 rc = LDAP_PROTOCOL_ERROR;
1278                 goto done;
1279         }
1280
1281         ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1282         *ps = op->o_conn->c_pagedresults_state;
1283         ps->ps_size = size;
1284         ps->ps_cookieval = cookie;
1285         op->o_pagedresults_state = ps;
1286         if ( !cookie.bv_len ) {
1287                 ps->ps_count = 0;
1288                 ps->ps_cookie = 0;
1289                 /* taint ps_cookie, to detect whether it's set */
1290                 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1291         }
1292
1293         /* NOTE: according to RFC 2696 3.:
1294
1295     If the page size is greater than or equal to the sizeLimit value, the
1296     server should ignore the control as the request can be satisfied in a
1297     single page.
1298
1299          * NOTE: this assumes that the op->ors_slimit be set
1300          * before the controls are parsed.     
1301          */
1302
1303         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1304                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1305
1306         } else if ( ctrl->ldctl_iscritical ) {
1307                 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1308
1309         } else {
1310                 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1311         }
1312
1313 done:;
1314         return rc;
1315 }
1316
1317 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1318 static int parseSortedResults (
1319         Operation *op,
1320         SlapReply *rs,
1321         LDAPControl *ctrl )
1322 {
1323         int             rc = LDAP_SUCCESS;
1324
1325         if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1326                 rs->sr_text = "sorted results control specified multiple times";
1327                 return LDAP_PROTOCOL_ERROR;
1328         }
1329
1330         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1331                 rs->sr_text = "sorted results control value is absent";
1332                 return LDAP_PROTOCOL_ERROR;
1333         }
1334
1335         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1336                 rs->sr_text = "sorted results control value is empty";
1337                 return LDAP_PROTOCOL_ERROR;
1338         }
1339
1340         /* blow off parsing the value */
1341
1342         op->o_sortedresults = ctrl->ldctl_iscritical
1343                 ? SLAP_CONTROL_CRITICAL
1344                 : SLAP_CONTROL_NONCRITICAL;
1345
1346         return rc;
1347 }
1348 #endif
1349
1350 static int parseAssert (
1351         Operation *op,
1352         SlapReply *rs,
1353         LDAPControl *ctrl )
1354 {
1355         BerElement      *ber;
1356         struct berval   fstr = BER_BVNULL;
1357
1358         if ( op->o_assert != SLAP_CONTROL_NONE ) {
1359                 rs->sr_text = "assert control specified multiple times";
1360                 return LDAP_PROTOCOL_ERROR;
1361         }
1362
1363         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1364                 rs->sr_text = "assert control value is absent";
1365                 return LDAP_PROTOCOL_ERROR;
1366         }
1367
1368         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1369                 rs->sr_text = "assert control value is empty";
1370                 return LDAP_PROTOCOL_ERROR;
1371         }
1372
1373         ber = ber_init( &(ctrl->ldctl_value) );
1374         if (ber == NULL) {
1375                 rs->sr_text = "assert control: internal error";
1376                 return LDAP_OTHER;
1377         }
1378
1379         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1380                 &rs->sr_text);
1381         (void) ber_free( ber, 1 );
1382         if( rs->sr_err != LDAP_SUCCESS ) {
1383                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1384                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1385                         send_ldap_disconnect( op, rs );
1386                         rs->sr_err = SLAPD_DISCONNECT;
1387                 } else {
1388                         send_ldap_result( op, rs );
1389                 }
1390                 if( op->o_assertion != NULL ) {
1391                         filter_free_x( op, op->o_assertion, 1 );
1392                 }
1393                 return rs->sr_err;
1394         }
1395
1396 #ifdef LDAP_DEBUG
1397         filter2bv_x( op, op->o_assertion, &fstr );
1398
1399         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1400                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1401         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1402 #endif
1403
1404         op->o_assert = ctrl->ldctl_iscritical
1405                 ? SLAP_CONTROL_CRITICAL
1406                 : SLAP_CONTROL_NONCRITICAL;
1407
1408         rs->sr_err = LDAP_SUCCESS;
1409         return LDAP_SUCCESS;
1410 }
1411
1412 #define READMSG(post, msg) \
1413         ( post ? "postread control: " msg : "preread control: " msg )
1414
1415 static int
1416 parseReadAttrs(
1417         Operation *op,
1418         SlapReply *rs,
1419         LDAPControl *ctrl,
1420         int post )
1421 {
1422         ber_len_t       siz, off, i;
1423         BerElement      *ber;
1424         AttributeName   *an = NULL;
1425
1426         if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1427                 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1428         {
1429                 rs->sr_text = READMSG( post, "specified multiple times" );
1430                 return LDAP_PROTOCOL_ERROR;
1431         }
1432
1433         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1434                 rs->sr_text = READMSG( post, "value is absent" );
1435                 return LDAP_PROTOCOL_ERROR;
1436         }
1437
1438         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1439                 rs->sr_text = READMSG( post, "value is empty" );
1440                 return LDAP_PROTOCOL_ERROR;
1441         }
1442
1443 #ifdef LDAP_X_TXN
1444         if ( op->o_txnSpec ) { /* temporary limitation */
1445                 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1446                 return LDAP_UNWILLING_TO_PERFORM;
1447         }
1448 #endif
1449
1450         ber = ber_init( &ctrl->ldctl_value );
1451         if ( ber == NULL ) {
1452                 rs->sr_text = READMSG( post, "internal error" );
1453                 return LDAP_OTHER;
1454         }
1455
1456         rs->sr_err = LDAP_SUCCESS;
1457         siz = sizeof( AttributeName );
1458         off = offsetof( AttributeName, an_name );
1459         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1460                 rs->sr_text = READMSG( post, "decoding error" );
1461                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1462                 goto done;
1463         }
1464
1465         for ( i = 0; i < siz; i++ ) {
1466                 const char      *dummy = NULL;
1467                 int             rc;
1468
1469                 an[i].an_desc = NULL;
1470                 an[i].an_oc = NULL;
1471                 an[i].an_flags = 0;
1472                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1473                 if ( rc == LDAP_SUCCESS ) {
1474                         an[i].an_name = an[i].an_desc->ad_cname;
1475
1476                 } else {
1477                         int                     j;
1478                         static struct berval    special_attrs[] = {
1479                                 BER_BVC( LDAP_NO_ATTRS ),
1480                                 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1481                                 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1482                                 BER_BVNULL
1483                         };
1484
1485                         /* deal with special attribute types */
1486                         for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1487                                 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1488                                         an[i].an_name = special_attrs[ j ];
1489                                         break;
1490                                 }
1491                         }
1492
1493                         if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1494                                 rs->sr_err = rc;
1495                                 rs->sr_text = dummy ? dummy
1496                                         : READMSG( post, "unknown attributeType" );
1497                                 goto done;
1498                         }
1499                 }
1500         }
1501
1502         if ( post ) {
1503                 op->o_postread_attrs = an;
1504                 op->o_postread = ctrl->ldctl_iscritical
1505                         ? SLAP_CONTROL_CRITICAL
1506                         : SLAP_CONTROL_NONCRITICAL;
1507         } else {
1508                 op->o_preread_attrs = an;
1509                 op->o_preread = ctrl->ldctl_iscritical
1510                         ? SLAP_CONTROL_CRITICAL
1511                         : SLAP_CONTROL_NONCRITICAL;
1512         }
1513
1514 done:
1515         (void) ber_free( ber, 1 );
1516         return rs->sr_err;
1517 }
1518
1519 static int parsePreRead (
1520         Operation *op,
1521         SlapReply *rs,
1522         LDAPControl *ctrl )
1523 {
1524         return parseReadAttrs( op, rs, ctrl, 0 );
1525 }
1526
1527 static int parsePostRead (
1528         Operation *op,
1529         SlapReply *rs,
1530         LDAPControl *ctrl )
1531 {
1532         return parseReadAttrs( op, rs, ctrl, 1 );
1533 }
1534
1535 static int parseValuesReturnFilter (
1536         Operation *op,
1537         SlapReply *rs,
1538         LDAPControl *ctrl )
1539 {
1540         BerElement      *ber;
1541         struct berval   fstr = BER_BVNULL;
1542
1543         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1544                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1545                 return LDAP_PROTOCOL_ERROR;
1546         }
1547
1548         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1549                 rs->sr_text = "valuesReturnFilter control value is absent";
1550                 return LDAP_PROTOCOL_ERROR;
1551         }
1552
1553         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1554                 rs->sr_text = "valuesReturnFilter control value is empty";
1555                 return LDAP_PROTOCOL_ERROR;
1556         }
1557
1558         ber = ber_init( &(ctrl->ldctl_value) );
1559         if (ber == NULL) {
1560                 rs->sr_text = "internal error";
1561                 return LDAP_OTHER;
1562         }
1563
1564         rs->sr_err = get_vrFilter( op, ber,
1565                 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1566
1567         (void) ber_free( ber, 1 );
1568
1569         if( rs->sr_err != LDAP_SUCCESS ) {
1570                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1571                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1572                         send_ldap_disconnect( op, rs );
1573                         rs->sr_err = SLAPD_DISCONNECT;
1574                 } else {
1575                         send_ldap_result( op, rs );
1576                 }
1577                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1578         }
1579 #ifdef LDAP_DEBUG
1580         else {
1581                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1582         }
1583
1584         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1585                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1586         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1587 #endif
1588
1589         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1590                 ? SLAP_CONTROL_CRITICAL
1591                 : SLAP_CONTROL_NONCRITICAL;
1592
1593         rs->sr_err = LDAP_SUCCESS;
1594         return LDAP_SUCCESS;
1595 }
1596
1597 static int parseSubentries (
1598         Operation *op,
1599         SlapReply *rs,
1600         LDAPControl *ctrl )
1601 {
1602         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1603                 rs->sr_text = "subentries control specified multiple times";
1604                 return LDAP_PROTOCOL_ERROR;
1605         }
1606
1607         /* FIXME: should use BER library */
1608         if( ( ctrl->ldctl_value.bv_len != 3 )
1609                 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1610                 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1611         {
1612                 rs->sr_text = "subentries control value encoding is bogus";
1613                 return LDAP_PROTOCOL_ERROR;
1614         }
1615
1616         op->o_subentries = ctrl->ldctl_iscritical
1617                 ? SLAP_CONTROL_CRITICAL
1618                 : SLAP_CONTROL_NONCRITICAL;
1619
1620         if (ctrl->ldctl_value.bv_val[2]) {
1621                 set_subentries_visibility( op );
1622         }
1623
1624         return LDAP_SUCCESS;
1625 }
1626
1627 static int parsePermissiveModify (
1628         Operation *op,
1629         SlapReply *rs,
1630         LDAPControl *ctrl )
1631 {
1632         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1633                 rs->sr_text = "permissiveModify control specified multiple times";
1634                 return LDAP_PROTOCOL_ERROR;
1635         }
1636
1637         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1638                 rs->sr_text = "permissiveModify control value not absent";
1639                 return LDAP_PROTOCOL_ERROR;
1640         }
1641
1642         op->o_permissive_modify = ctrl->ldctl_iscritical
1643                 ? SLAP_CONTROL_CRITICAL
1644                 : SLAP_CONTROL_NONCRITICAL;
1645
1646         return LDAP_SUCCESS;
1647 }
1648
1649 static int parseDomainScope (
1650         Operation *op,
1651         SlapReply *rs,
1652         LDAPControl *ctrl )
1653 {
1654         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1655                 rs->sr_text = "domainScope control specified multiple times";
1656                 return LDAP_PROTOCOL_ERROR;
1657         }
1658
1659         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1660                 rs->sr_text = "domainScope control value not empty";
1661                 return LDAP_PROTOCOL_ERROR;
1662         }
1663
1664         op->o_domain_scope = ctrl->ldctl_iscritical
1665                 ? SLAP_CONTROL_CRITICAL
1666                 : SLAP_CONTROL_NONCRITICAL;
1667
1668         return LDAP_SUCCESS;
1669 }
1670
1671 #ifdef SLAP_CONTROL_X_TREE_DELETE
1672 static int parseTreeDelete (
1673         Operation *op,
1674         SlapReply *rs,
1675         LDAPControl *ctrl )
1676 {
1677         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1678                 rs->sr_text = "treeDelete control specified multiple times";
1679                 return LDAP_PROTOCOL_ERROR;
1680         }
1681
1682         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1683                 rs->sr_text = "treeDelete control value not absent";
1684                 return LDAP_PROTOCOL_ERROR;
1685         }
1686
1687         op->o_tree_delete = ctrl->ldctl_iscritical
1688                 ? SLAP_CONTROL_CRITICAL
1689                 : SLAP_CONTROL_NONCRITICAL;
1690
1691         return LDAP_SUCCESS;
1692 }
1693 #endif
1694
1695 static int parseSearchOptions (
1696         Operation *op,
1697         SlapReply *rs,
1698         LDAPControl *ctrl )
1699 {
1700         BerElement *ber;
1701         ber_int_t search_flags;
1702         ber_tag_t tag;
1703
1704         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1705                 rs->sr_text = "searchOptions control value is absent";
1706                 return LDAP_PROTOCOL_ERROR;
1707         }
1708
1709         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1710                 rs->sr_text = "searchOptions control value is empty";
1711                 return LDAP_PROTOCOL_ERROR;
1712         }
1713
1714         ber = ber_init( &ctrl->ldctl_value );
1715         if( ber == NULL ) {
1716                 rs->sr_text = "internal error";
1717                 return LDAP_OTHER;
1718         }
1719
1720         tag = ber_scanf( ber, "{i}", &search_flags );
1721         (void) ber_free( ber, 1 );
1722
1723         if ( tag == LBER_ERROR ) {
1724                 rs->sr_text = "searchOptions control decoding error";
1725                 return LDAP_PROTOCOL_ERROR;
1726         }
1727
1728         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1729                 /* Search flags not recognised so far,
1730                  * including:
1731                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOT
1732                  */
1733                 if ( ctrl->ldctl_iscritical ) {
1734                         rs->sr_text = "searchOptions contained unrecognized flag";
1735                         return LDAP_UNWILLING_TO_PERFORM;
1736                 }
1737
1738                 /* Ignore */
1739                 Debug( LDAP_DEBUG_TRACE,
1740                         "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n", 
1741                         op->o_connid, (unsigned)search_flags, 0 );
1742
1743                 return LDAP_SUCCESS;
1744         }
1745
1746         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1747                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1748                         rs->sr_text = "searchOptions control specified multiple times "
1749                                 "or with domainScope control";
1750                         return LDAP_PROTOCOL_ERROR;
1751                 }
1752
1753                 op->o_domain_scope = ctrl->ldctl_iscritical
1754                         ? SLAP_CONTROL_CRITICAL
1755                         : SLAP_CONTROL_NONCRITICAL;
1756         }
1757
1758         return LDAP_SUCCESS;
1759 }
1760
1761 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1762 struct berval session_tracking_formats[] = {
1763         BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1764                 BER_BVC( "RADIUS-Acct-Session-Id" ),
1765         BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1766                 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1767         BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1768                 BER_BVC( "USERNAME" ),
1769
1770         BER_BVNULL
1771 };
1772
1773 static int parseSessionTracking(
1774         Operation *op,
1775         SlapReply *rs,
1776         LDAPControl *ctrl )
1777 {
1778         BerElement              *ber;
1779         ber_tag_t               tag;
1780         ber_len_t               len;
1781         int                     i, rc;
1782
1783         struct berval           sessionSourceIp = BER_BVNULL,
1784                                 sessionSourceName = BER_BVNULL,
1785                                 formatOID = BER_BVNULL,
1786                                 sessionTrackingIdentifier = BER_BVNULL;
1787
1788         size_t                  st_len, st_pos;
1789
1790         if ( ctrl->ldctl_iscritical ) {
1791                 rs->sr_text = "sessionTracking criticality is TRUE";
1792                 return LDAP_PROTOCOL_ERROR;
1793         }
1794
1795         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1796                 rs->sr_text = "sessionTracking control value is absent";
1797                 return LDAP_PROTOCOL_ERROR;
1798         }
1799
1800         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1801                 rs->sr_text = "sessionTracking control value is empty";
1802                 return LDAP_PROTOCOL_ERROR;
1803         }
1804
1805         /* TODO: add the capability to determine if a client is allowed
1806          * to use this control, based on identity, ip and so */
1807
1808         ber = ber_init( &ctrl->ldctl_value );
1809         if ( ber == NULL ) {
1810                 rs->sr_text = "internal error";
1811                 return LDAP_OTHER;
1812         }
1813
1814         tag = ber_skip_tag( ber, &len );
1815         if ( tag != LBER_SEQUENCE ) {
1816                 tag = LBER_ERROR;
1817                 goto error;
1818         }
1819
1820         /* sessionSourceIp */
1821         tag = ber_peek_tag( ber, &len );
1822         if ( tag == LBER_DEFAULT ) {
1823                 tag = LBER_ERROR;
1824                 goto error;
1825         }
1826
1827         if ( len == 0 ) {
1828                 tag = ber_skip_tag( ber, &len );
1829
1830         } else if ( len > 128 ) {
1831                 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1832                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1833                 goto error;
1834
1835         } else {
1836                 tag = ber_scanf( ber, "m", &sessionSourceIp );
1837         }
1838
1839         if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1840                 BER_BVZERO( &sessionSourceIp );
1841         }
1842
1843         /* sessionSourceName */
1844         tag = ber_peek_tag( ber, &len );
1845         if ( tag == LBER_DEFAULT ) {
1846                 tag = LBER_ERROR;
1847                 goto error;
1848         }
1849
1850         if ( len == 0 ) {
1851                 tag = ber_skip_tag( ber, &len );
1852
1853         } else if ( len > 65536 ) {
1854                 rs->sr_text = "sessionTracking.sessionSourceName too long";
1855                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1856                 goto error;
1857
1858         } else {
1859                 tag = ber_scanf( ber, "m", &sessionSourceName );
1860         }
1861
1862         if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1863                 BER_BVZERO( &sessionSourceName );
1864         }
1865
1866         /* formatOID */
1867         tag = ber_peek_tag( ber, &len );
1868         if ( tag == LBER_DEFAULT ) {
1869                 tag = LBER_ERROR;
1870                 goto error;
1871         }
1872
1873         if ( len == 0 ) {
1874                 rs->sr_text = "sessionTracking.formatOID empty";
1875                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1876                 goto error;
1877
1878         } else if ( len > 1024 ) {
1879                 rs->sr_text = "sessionTracking.formatOID too long";
1880                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1881                 goto error;
1882
1883         } else {
1884                 tag = ber_scanf( ber, "m", &formatOID );
1885         }
1886
1887         rc = numericoidValidate( NULL, &formatOID );
1888         if ( rc != LDAP_SUCCESS ) {
1889                 rs->sr_text = "sessionTracking.formatOID invalid";
1890                 goto error;
1891         }
1892
1893         for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1894         {
1895                 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1896                         formatOID = session_tracking_formats[ i + 1 ];
1897                         break;
1898                 }
1899         }
1900
1901         /* sessionTrackingIdentifier */
1902         tag = ber_peek_tag( ber, &len );
1903         if ( tag == LBER_DEFAULT ) {
1904                 tag = LBER_ERROR;
1905                 goto error;
1906         }
1907
1908         if ( len == 0 ) {
1909                 tag = ber_skip_tag( ber, &len );
1910
1911         } else {
1912                 /* note: should not be more than 65536... */
1913                 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1914                 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1915                         /* we want the OID printed, at least */
1916                         BER_BVSTR( &sessionTrackingIdentifier, "" );
1917                 }
1918         }
1919
1920         /* closure */
1921         tag = ber_skip_tag( ber, &len );
1922         if ( tag != LBER_DEFAULT || len != 0 ) {
1923                 tag = LBER_ERROR;
1924                 goto error;
1925         }
1926         tag = 0;
1927
1928         st_len = 0;
1929         if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1930                 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1931         }
1932         if ( !BER_BVISNULL( &sessionSourceName ) ) {
1933                 if ( st_len ) st_len++;
1934                 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1935         }
1936         if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1937                 if ( st_len ) st_len++;
1938                 st_len += formatOID.bv_len + STRLENOF( "=" )
1939                         + sessionTrackingIdentifier.bv_len;
1940         }
1941
1942         if ( st_len == 0 ) {
1943                 goto error;
1944         }
1945
1946         st_len += STRLENOF( " []" );
1947         st_pos = strlen( op->o_log_prefix );
1948
1949         if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1950                 char    *ptr = &op->o_log_prefix[ st_pos ];
1951
1952                 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1953
1954                 st_len = 0;
1955                 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1956                         ptr = lutil_strcopy( ptr, "IP=" );
1957                         ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1958                         st_len++;
1959                 }
1960
1961                 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1962                         if ( st_len ) *ptr++ = ' ';
1963                         ptr = lutil_strcopy( ptr, "NAME=" );
1964                         ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1965                         st_len++;
1966                 }
1967
1968                 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1969                         if ( st_len ) *ptr++ = ' ';
1970                         ptr = lutil_strcopy( ptr, formatOID.bv_val );
1971                         *ptr++ = '=';
1972                         ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1973                 }
1974
1975                 *ptr++ = /*[*/ ']';
1976                 *ptr = '\0';
1977         }
1978
1979 error:;
1980         (void)ber_free( ber, 1 );
1981
1982         if ( tag == LBER_ERROR ) {
1983                 rs->sr_text = "sessionTracking control decoding error";
1984                 return LDAP_PROTOCOL_ERROR;
1985         }
1986
1987
1988         return rs->sr_err;
1989 }
1990
1991 int
1992 slap_ctrl_session_tracking_add(
1993         Operation *op,
1994         SlapReply *rs,
1995         struct berval *ip,
1996         struct berval *name,
1997         struct berval *id,
1998         LDAPControl *ctrl )
1999 {
2000         BerElementBuffer berbuf;
2001         BerElement      *ber = (BerElement *)&berbuf;
2002
2003         static struct berval    oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
2004
2005         assert( ctrl != NULL );
2006
2007         ber_init2( ber, NULL, LBER_USE_DER );
2008
2009         ber_printf( ber, "{OOOO}", ip, name, &oid, id ); 
2010
2011         if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
2012                 rs->sr_err = LDAP_OTHER;
2013                 goto done;
2014         }
2015
2016         ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
2017         ctrl->ldctl_iscritical = 0;
2018
2019         rs->sr_err = LDAP_SUCCESS;
2020
2021 done:;
2022         return rs->sr_err;
2023 }
2024
2025 int
2026 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
2027 {
2028         static struct berval    bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
2029         struct berval           ip = BER_BVNULL,
2030                                 name = BER_BVNULL,
2031                                 id = BER_BVNULL;
2032
2033         if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
2034                 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
2035         {
2036                 char    *ptr;
2037
2038                 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
2039                 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
2040
2041                 ptr = ber_bvchr( &ip, ':' );
2042                 if ( ptr ) {
2043                         ip.bv_len = ptr - ip.bv_val;
2044                 }
2045         }
2046
2047         if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
2048                 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
2049         {
2050                 name = op->o_conn->c_peer_domain;
2051         }
2052
2053         if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2054                 id = op->o_dn;
2055         }
2056
2057         return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2058 }
2059 #endif
2060
2061 #ifdef SLAP_CONTROL_X_WHATFAILED
2062 static int parseWhatFailed(
2063         Operation *op,
2064         SlapReply *rs,
2065         LDAPControl *ctrl )
2066 {
2067         if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2068                 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2069                 return LDAP_PROTOCOL_ERROR;
2070         }
2071
2072         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2073                 rs->sr_text = "\"What Failed?\" control value not absent";
2074                 return LDAP_PROTOCOL_ERROR;
2075         }
2076
2077         op->o_whatFailed = ctrl->ldctl_iscritical
2078                 ? SLAP_CONTROL_CRITICAL
2079                 : SLAP_CONTROL_NONCRITICAL;
2080
2081         return LDAP_SUCCESS;
2082 }
2083
2084 int
2085 slap_ctrl_whatFailed_add(
2086         Operation *op,
2087         SlapReply *rs,
2088         char **oids )
2089 {
2090         BerElementBuffer berbuf;
2091         BerElement *ber = (BerElement *) &berbuf;
2092         LDAPControl **ctrls = NULL;
2093         struct berval ctrlval;
2094         int i, rc = LDAP_SUCCESS;
2095
2096         ber_init2( ber, NULL, LBER_USE_DER );
2097         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2098         ber_printf( ber, "[" /*]*/ );
2099         for ( i = 0; oids[ i ] != NULL; i++ ) {
2100                 ber_printf( ber, "s", oids[ i ] );
2101         }
2102         ber_printf( ber, /*[*/ "]" );
2103
2104         if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2105                 rc = LDAP_OTHER;
2106                 goto done;
2107         }
2108
2109         i = 0;
2110         if ( rs->sr_ctrls != NULL ) {
2111                 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2112                         if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2113                                 /* TODO: add */
2114                                 assert( 0 );
2115                         }
2116                 }
2117         }
2118
2119         ctrls = op->o_tmprealloc( rs->sr_ctrls,
2120                         sizeof(LDAPControl *)*( i + 2 )
2121                         + sizeof(LDAPControl)
2122                         + ctrlval.bv_len + 1,
2123                         op->o_tmpmemctx );
2124         if ( ctrls == NULL ) {
2125                 rc = LDAP_OTHER;
2126                 goto done;
2127         }
2128         ctrls[ i + 1 ] = NULL;
2129         ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2130         ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2131         ctrls[ i ]->ldctl_iscritical = 0;
2132         ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2133         AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2134         ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2135
2136         ber_free_buf( ber );
2137
2138         rs->sr_ctrls = ctrls;
2139
2140 done:;
2141         return rc;
2142 }
2143 #endif