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