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