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