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