]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
Plug mutex/rwlock leaks (destroy them)
[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
1141         /*
1142          * NOTE: since slap_sasl_getdn() returns a normalized dn,
1143          * from now on op->o_dn is normalized
1144          */
1145         op->o_ndn = dn;
1146         ber_bvreplace( &op->o_dn, &dn );
1147
1148         Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1149             op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1150
1151         return LDAP_SUCCESS;
1152 }
1153
1154 static int parseNoOp (
1155         Operation *op,
1156         SlapReply *rs,
1157         LDAPControl *ctrl )
1158 {
1159         if ( op->o_noop != SLAP_CONTROL_NONE ) {
1160                 rs->sr_text = "noop control specified multiple times";
1161                 return LDAP_PROTOCOL_ERROR;
1162         }
1163
1164         if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1165                 rs->sr_text = "noop control value not empty";
1166                 return LDAP_PROTOCOL_ERROR;
1167         }
1168
1169         op->o_noop = ctrl->ldctl_iscritical
1170                 ? SLAP_CONTROL_CRITICAL
1171                 : SLAP_CONTROL_NONCRITICAL;
1172
1173         return LDAP_SUCCESS;
1174 }
1175
1176 static int parsePagedResults (
1177         Operation *op,
1178         SlapReply *rs,
1179         LDAPControl *ctrl )
1180 {
1181         BerElementBuffer berbuf;
1182         BerElement      *ber = (BerElement *)&berbuf;
1183         struct berval   cookie;
1184         PagedResultsState       *ps;
1185         int             rc = LDAP_SUCCESS;
1186         ber_tag_t       tag;
1187         ber_int_t       size;
1188
1189         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1190                 rs->sr_text = "paged results control specified multiple times";
1191                 return LDAP_PROTOCOL_ERROR;
1192         }
1193
1194         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1195                 rs->sr_text = "paged results control value is absent";
1196                 return LDAP_PROTOCOL_ERROR;
1197         }
1198
1199         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1200                 rs->sr_text = "paged results control value is empty";
1201                 return LDAP_PROTOCOL_ERROR;
1202         }
1203
1204         /* Parse the control value
1205          *      realSearchControlValue ::= SEQUENCE {
1206          *              size    INTEGER (0..maxInt),
1207          *                              -- requested page size from client
1208          *                              -- result set size estimate from server
1209          *              cookie  OCTET STRING
1210          * }
1211          */
1212         ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1213
1214         tag = ber_scanf( ber, "{im}", &size, &cookie );
1215
1216         if ( tag == LBER_ERROR ) {
1217                 rs->sr_text = "paged results control could not be decoded";
1218                 rc = LDAP_PROTOCOL_ERROR;
1219                 goto done;
1220         }
1221
1222         if ( size < 0 ) {
1223                 rs->sr_text = "paged results control size invalid";
1224                 rc = LDAP_PROTOCOL_ERROR;
1225                 goto done;
1226         }
1227
1228         ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1229         *ps = op->o_conn->c_pagedresults_state;
1230         ps->ps_size = size;
1231         ps->ps_cookieval = cookie;
1232         op->o_pagedresults_state = ps;
1233         if ( !cookie.bv_len ) {
1234                 ps->ps_count = 0;
1235                 ps->ps_cookie = 0;
1236                 /* taint ps_cookie, to detect whether it's set */
1237                 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1238         }
1239
1240         /* NOTE: according to RFC 2696 3.:
1241
1242     If the page size is greater than or equal to the sizeLimit value, the
1243     server should ignore the control as the request can be satisfied in a
1244     single page.
1245
1246          * NOTE: this assumes that the op->ors_slimit be set
1247          * before the controls are parsed.     
1248          */
1249
1250         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1251                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1252
1253         } else if ( ctrl->ldctl_iscritical ) {
1254                 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1255
1256         } else {
1257                 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1258         }
1259
1260 done:;
1261         return rc;
1262 }
1263
1264 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1265 static int parseSortedResults (
1266         Operation *op,
1267         SlapReply *rs,
1268         LDAPControl *ctrl )
1269 {
1270         int             rc = LDAP_SUCCESS;
1271
1272         if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1273                 rs->sr_text = "sorted results control specified multiple times";
1274                 return LDAP_PROTOCOL_ERROR;
1275         }
1276
1277         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1278                 rs->sr_text = "sorted results control value is absent";
1279                 return LDAP_PROTOCOL_ERROR;
1280         }
1281
1282         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1283                 rs->sr_text = "sorted results control value is empty";
1284                 return LDAP_PROTOCOL_ERROR;
1285         }
1286
1287         /* blow off parsing the value */
1288
1289         op->o_sortedresults = ctrl->ldctl_iscritical
1290                 ? SLAP_CONTROL_CRITICAL
1291                 : SLAP_CONTROL_NONCRITICAL;
1292
1293         return rc;
1294 }
1295 #endif
1296
1297 static int parseAssert (
1298         Operation *op,
1299         SlapReply *rs,
1300         LDAPControl *ctrl )
1301 {
1302         BerElement      *ber;
1303         struct berval   fstr = BER_BVNULL;
1304
1305         if ( op->o_assert != SLAP_CONTROL_NONE ) {
1306                 rs->sr_text = "assert control specified multiple times";
1307                 return LDAP_PROTOCOL_ERROR;
1308         }
1309
1310         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1311                 rs->sr_text = "assert control value is absent";
1312                 return LDAP_PROTOCOL_ERROR;
1313         }
1314
1315         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1316                 rs->sr_text = "assert control value is empty";
1317                 return LDAP_PROTOCOL_ERROR;
1318         }
1319
1320         ber = ber_init( &(ctrl->ldctl_value) );
1321         if (ber == NULL) {
1322                 rs->sr_text = "assert control: internal error";
1323                 return LDAP_OTHER;
1324         }
1325
1326         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1327                 &rs->sr_text);
1328         (void) ber_free( ber, 1 );
1329         if( rs->sr_err != LDAP_SUCCESS ) {
1330                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1331                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1332                         send_ldap_disconnect( op, rs );
1333                         rs->sr_err = SLAPD_DISCONNECT;
1334                 } else {
1335                         send_ldap_result( op, rs );
1336                 }
1337                 if( op->o_assertion != NULL ) {
1338                         filter_free_x( op, op->o_assertion, 1 );
1339                 }
1340                 return rs->sr_err;
1341         }
1342
1343 #ifdef LDAP_DEBUG
1344         filter2bv_x( op, op->o_assertion, &fstr );
1345
1346         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1347                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1348         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1349 #endif
1350
1351         op->o_assert = ctrl->ldctl_iscritical
1352                 ? SLAP_CONTROL_CRITICAL
1353                 : SLAP_CONTROL_NONCRITICAL;
1354
1355         rs->sr_err = LDAP_SUCCESS;
1356         return LDAP_SUCCESS;
1357 }
1358
1359 #define READMSG(post, msg) \
1360         ( post ? "postread control: " msg : "preread control: " msg )
1361
1362 static int
1363 parseReadAttrs(
1364         Operation *op,
1365         SlapReply *rs,
1366         LDAPControl *ctrl,
1367         int post )
1368 {
1369         ber_len_t       siz, off, i;
1370         BerElement      *ber;
1371         AttributeName   *an = NULL;
1372
1373         if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1374                 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1375         {
1376                 rs->sr_text = READMSG( post, "specified multiple times" );
1377                 return LDAP_PROTOCOL_ERROR;
1378         }
1379
1380         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1381                 rs->sr_text = READMSG( post, "value is absent" );
1382                 return LDAP_PROTOCOL_ERROR;
1383         }
1384
1385         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1386                 rs->sr_text = READMSG( post, "value is empty" );
1387                 return LDAP_PROTOCOL_ERROR;
1388         }
1389
1390 #ifdef LDAP_X_TXN
1391         if ( op->o_txnSpec ) { /* temporary limitation */
1392                 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1393                 return LDAP_UNWILLING_TO_PERFORM;
1394         }
1395 #endif
1396
1397         ber = ber_init( &ctrl->ldctl_value );
1398         if ( ber == NULL ) {
1399                 rs->sr_text = READMSG( post, "internal error" );
1400                 return LDAP_OTHER;
1401         }
1402
1403         rs->sr_err = LDAP_SUCCESS;
1404         siz = sizeof( AttributeName );
1405         off = offsetof( AttributeName, an_name );
1406         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1407                 rs->sr_text = READMSG( post, "decoding error" );
1408                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1409                 goto done;
1410         }
1411
1412         for ( i = 0; i < siz; i++ ) {
1413                 const char      *dummy = NULL;
1414                 int             rc;
1415
1416                 an[i].an_desc = NULL;
1417                 an[i].an_oc = NULL;
1418                 an[i].an_flags = 0;
1419                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1420                 if ( rc == LDAP_SUCCESS ) {
1421                         an[i].an_name = an[i].an_desc->ad_cname;
1422
1423                 } else {
1424                         int                     j;
1425                         static struct berval    special_attrs[] = {
1426                                 BER_BVC( LDAP_NO_ATTRS ),
1427                                 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1428                                 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1429                                 BER_BVNULL
1430                         };
1431
1432                         /* deal with special attribute types */
1433                         for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1434                                 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1435                                         an[i].an_name = special_attrs[ j ];
1436                                         break;
1437                                 }
1438                         }
1439
1440                         if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1441                                 rs->sr_err = rc;
1442                                 rs->sr_text = dummy ? dummy
1443                                         : READMSG( post, "unknown attributeType" );
1444                                 goto done;
1445                         }
1446                 }
1447         }
1448
1449         if ( post ) {
1450                 op->o_postread_attrs = an;
1451                 op->o_postread = ctrl->ldctl_iscritical
1452                         ? SLAP_CONTROL_CRITICAL
1453                         : SLAP_CONTROL_NONCRITICAL;
1454         } else {
1455                 op->o_preread_attrs = an;
1456                 op->o_preread = ctrl->ldctl_iscritical
1457                         ? SLAP_CONTROL_CRITICAL
1458                         : SLAP_CONTROL_NONCRITICAL;
1459         }
1460
1461 done:
1462         (void) ber_free( ber, 1 );
1463         return rs->sr_err;
1464 }
1465
1466 static int parsePreRead (
1467         Operation *op,
1468         SlapReply *rs,
1469         LDAPControl *ctrl )
1470 {
1471         return parseReadAttrs( op, rs, ctrl, 0 );
1472 }
1473
1474 static int parsePostRead (
1475         Operation *op,
1476         SlapReply *rs,
1477         LDAPControl *ctrl )
1478 {
1479         return parseReadAttrs( op, rs, ctrl, 1 );
1480 }
1481
1482 static int parseValuesReturnFilter (
1483         Operation *op,
1484         SlapReply *rs,
1485         LDAPControl *ctrl )
1486 {
1487         BerElement      *ber;
1488         struct berval   fstr = BER_BVNULL;
1489
1490         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1491                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1492                 return LDAP_PROTOCOL_ERROR;
1493         }
1494
1495         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1496                 rs->sr_text = "valuesReturnFilter control value is absent";
1497                 return LDAP_PROTOCOL_ERROR;
1498         }
1499
1500         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1501                 rs->sr_text = "valuesReturnFilter control value is empty";
1502                 return LDAP_PROTOCOL_ERROR;
1503         }
1504
1505         ber = ber_init( &(ctrl->ldctl_value) );
1506         if (ber == NULL) {
1507                 rs->sr_text = "internal error";
1508                 return LDAP_OTHER;
1509         }
1510
1511         rs->sr_err = get_vrFilter( op, ber,
1512                 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1513
1514         (void) ber_free( ber, 1 );
1515
1516         if( rs->sr_err != LDAP_SUCCESS ) {
1517                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1518                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1519                         send_ldap_disconnect( op, rs );
1520                         rs->sr_err = SLAPD_DISCONNECT;
1521                 } else {
1522                         send_ldap_result( op, rs );
1523                 }
1524                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1525         }
1526 #ifdef LDAP_DEBUG
1527         else {
1528                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1529         }
1530
1531         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1532                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1533         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1534 #endif
1535
1536         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1537                 ? SLAP_CONTROL_CRITICAL
1538                 : SLAP_CONTROL_NONCRITICAL;
1539
1540         rs->sr_err = LDAP_SUCCESS;
1541         return LDAP_SUCCESS;
1542 }
1543
1544 static int parseSubentries (
1545         Operation *op,
1546         SlapReply *rs,
1547         LDAPControl *ctrl )
1548 {
1549         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1550                 rs->sr_text = "subentries control specified multiple times";
1551                 return LDAP_PROTOCOL_ERROR;
1552         }
1553
1554         /* FIXME: should use BER library */
1555         if( ( ctrl->ldctl_value.bv_len != 3 )
1556                 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1557                 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1558         {
1559                 rs->sr_text = "subentries control value encoding is bogus";
1560                 return LDAP_PROTOCOL_ERROR;
1561         }
1562
1563         op->o_subentries = ctrl->ldctl_iscritical
1564                 ? SLAP_CONTROL_CRITICAL
1565                 : SLAP_CONTROL_NONCRITICAL;
1566
1567         if (ctrl->ldctl_value.bv_val[2]) {
1568                 set_subentries_visibility( op );
1569         }
1570
1571         return LDAP_SUCCESS;
1572 }
1573
1574 static int parsePermissiveModify (
1575         Operation *op,
1576         SlapReply *rs,
1577         LDAPControl *ctrl )
1578 {
1579         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1580                 rs->sr_text = "permissiveModify control specified multiple times";
1581                 return LDAP_PROTOCOL_ERROR;
1582         }
1583
1584         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1585                 rs->sr_text = "permissiveModify control value not absent";
1586                 return LDAP_PROTOCOL_ERROR;
1587         }
1588
1589         op->o_permissive_modify = ctrl->ldctl_iscritical
1590                 ? SLAP_CONTROL_CRITICAL
1591                 : SLAP_CONTROL_NONCRITICAL;
1592
1593         return LDAP_SUCCESS;
1594 }
1595
1596 static int parseDomainScope (
1597         Operation *op,
1598         SlapReply *rs,
1599         LDAPControl *ctrl )
1600 {
1601         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1602                 rs->sr_text = "domainScope control specified multiple times";
1603                 return LDAP_PROTOCOL_ERROR;
1604         }
1605
1606         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1607                 rs->sr_text = "domainScope control value not empty";
1608                 return LDAP_PROTOCOL_ERROR;
1609         }
1610
1611         op->o_domain_scope = ctrl->ldctl_iscritical
1612                 ? SLAP_CONTROL_CRITICAL
1613                 : SLAP_CONTROL_NONCRITICAL;
1614
1615         return LDAP_SUCCESS;
1616 }
1617
1618 #ifdef SLAP_CONTROL_X_TREE_DELETE
1619 static int parseTreeDelete (
1620         Operation *op,
1621         SlapReply *rs,
1622         LDAPControl *ctrl )
1623 {
1624         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1625                 rs->sr_text = "treeDelete control specified multiple times";
1626                 return LDAP_PROTOCOL_ERROR;
1627         }
1628
1629         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1630                 rs->sr_text = "treeDelete control value not absent";
1631                 return LDAP_PROTOCOL_ERROR;
1632         }
1633
1634         op->o_tree_delete = ctrl->ldctl_iscritical
1635                 ? SLAP_CONTROL_CRITICAL
1636                 : SLAP_CONTROL_NONCRITICAL;
1637
1638         return LDAP_SUCCESS;
1639 }
1640 #endif
1641
1642 static int parseSearchOptions (
1643         Operation *op,
1644         SlapReply *rs,
1645         LDAPControl *ctrl )
1646 {
1647         BerElement *ber;
1648         ber_int_t search_flags;
1649         ber_tag_t tag;
1650
1651         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1652                 rs->sr_text = "searchOptions control value is absent";
1653                 return LDAP_PROTOCOL_ERROR;
1654         }
1655
1656         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1657                 rs->sr_text = "searchOptions control value is empty";
1658                 return LDAP_PROTOCOL_ERROR;
1659         }
1660
1661         ber = ber_init( &ctrl->ldctl_value );
1662         if( ber == NULL ) {
1663                 rs->sr_text = "internal error";
1664                 return LDAP_OTHER;
1665         }
1666
1667         tag = ber_scanf( ber, "{i}", &search_flags );
1668         (void) ber_free( ber, 1 );
1669
1670         if ( tag == LBER_ERROR ) {
1671                 rs->sr_text = "searchOptions control decoding error";
1672                 return LDAP_PROTOCOL_ERROR;
1673         }
1674
1675         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1676                 /* Search flags not recognised so far,
1677                  * including:
1678                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOT
1679                  */
1680                 if ( ctrl->ldctl_iscritical ) {
1681                         rs->sr_text = "searchOptions contained unrecognized flag";
1682                         return LDAP_UNWILLING_TO_PERFORM;
1683                 }
1684
1685                 /* Ignore */
1686                 Debug( LDAP_DEBUG_TRACE,
1687                         "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n", 
1688                         op->o_connid, (unsigned)search_flags, 0 );
1689
1690                 return LDAP_SUCCESS;
1691         }
1692
1693         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1694                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1695                         rs->sr_text = "searchOptions control specified multiple times "
1696                                 "or with domainScope control";
1697                         return LDAP_PROTOCOL_ERROR;
1698                 }
1699
1700                 op->o_domain_scope = ctrl->ldctl_iscritical
1701                         ? SLAP_CONTROL_CRITICAL
1702                         : SLAP_CONTROL_NONCRITICAL;
1703         }
1704
1705         return LDAP_SUCCESS;
1706 }
1707
1708 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1709 struct berval session_tracking_formats[] = {
1710         BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1711                 BER_BVC( "RADIUS-Acct-Session-Id" ),
1712         BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1713                 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1714         BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1715                 BER_BVC( "USERNAME" ),
1716
1717         BER_BVNULL
1718 };
1719
1720 static int parseSessionTracking(
1721         Operation *op,
1722         SlapReply *rs,
1723         LDAPControl *ctrl )
1724 {
1725         BerElement              *ber;
1726         ber_tag_t               tag;
1727         ber_len_t               len;
1728         int                     i, rc;
1729
1730         struct berval           sessionSourceIp = BER_BVNULL,
1731                                 sessionSourceName = BER_BVNULL,
1732                                 formatOID = BER_BVNULL,
1733                                 sessionTrackingIdentifier = BER_BVNULL;
1734
1735         size_t                  st_len, st_pos;
1736
1737         if ( ctrl->ldctl_iscritical ) {
1738                 rs->sr_text = "sessionTracking criticality is TRUE";
1739                 return LDAP_PROTOCOL_ERROR;
1740         }
1741
1742         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1743                 rs->sr_text = "sessionTracking control value is absent";
1744                 return LDAP_PROTOCOL_ERROR;
1745         }
1746
1747         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1748                 rs->sr_text = "sessionTracking control value is empty";
1749                 return LDAP_PROTOCOL_ERROR;
1750         }
1751
1752         /* TODO: add the capability to determine if a client is allowed
1753          * to use this control, based on identity, ip and so */
1754
1755         ber = ber_init( &ctrl->ldctl_value );
1756         if ( ber == NULL ) {
1757                 rs->sr_text = "internal error";
1758                 return LDAP_OTHER;
1759         }
1760
1761         tag = ber_skip_tag( ber, &len );
1762         if ( tag != LBER_SEQUENCE ) {
1763                 tag = LBER_ERROR;
1764                 goto error;
1765         }
1766
1767         /* sessionSourceIp */
1768         tag = ber_peek_tag( ber, &len );
1769         if ( tag == LBER_DEFAULT ) {
1770                 tag = LBER_ERROR;
1771                 goto error;
1772         }
1773
1774         if ( len == 0 ) {
1775                 tag = ber_skip_tag( ber, &len );
1776
1777         } else if ( len > 128 ) {
1778                 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1779                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1780                 goto error;
1781
1782         } else {
1783                 tag = ber_scanf( ber, "m", &sessionSourceIp );
1784         }
1785
1786         if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1787                 BER_BVZERO( &sessionSourceIp );
1788         }
1789
1790         /* sessionSourceName */
1791         tag = ber_peek_tag( ber, &len );
1792         if ( tag == LBER_DEFAULT ) {
1793                 tag = LBER_ERROR;
1794                 goto error;
1795         }
1796
1797         if ( len == 0 ) {
1798                 tag = ber_skip_tag( ber, &len );
1799
1800         } else if ( len > 65536 ) {
1801                 rs->sr_text = "sessionTracking.sessionSourceName too long";
1802                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1803                 goto error;
1804
1805         } else {
1806                 tag = ber_scanf( ber, "m", &sessionSourceName );
1807         }
1808
1809         if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1810                 BER_BVZERO( &sessionSourceName );
1811         }
1812
1813         /* formatOID */
1814         tag = ber_peek_tag( ber, &len );
1815         if ( tag == LBER_DEFAULT ) {
1816                 tag = LBER_ERROR;
1817                 goto error;
1818         }
1819
1820         if ( len == 0 ) {
1821                 rs->sr_text = "sessionTracking.formatOID empty";
1822                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1823                 goto error;
1824
1825         } else if ( len > 1024 ) {
1826                 rs->sr_text = "sessionTracking.formatOID too long";
1827                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1828                 goto error;
1829
1830         } else {
1831                 tag = ber_scanf( ber, "m", &formatOID );
1832         }
1833
1834         rc = numericoidValidate( NULL, &formatOID );
1835         if ( rc != LDAP_SUCCESS ) {
1836                 rs->sr_text = "sessionTracking.formatOID invalid";
1837                 goto error;
1838         }
1839
1840         for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1841         {
1842                 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1843                         formatOID = session_tracking_formats[ i + 1 ];
1844                         break;
1845                 }
1846         }
1847
1848         /* sessionTrackingIdentifier */
1849         tag = ber_peek_tag( ber, &len );
1850         if ( tag == LBER_DEFAULT ) {
1851                 tag = LBER_ERROR;
1852                 goto error;
1853         }
1854
1855         if ( len == 0 ) {
1856                 tag = ber_skip_tag( ber, &len );
1857
1858         } else {
1859                 /* note: should not be more than 65536... */
1860                 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1861                 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1862                         /* we want the OID printed, at least */
1863                         BER_BVSTR( &sessionTrackingIdentifier, "" );
1864                 }
1865         }
1866
1867         /* closure */
1868         tag = ber_skip_tag( ber, &len );
1869         if ( tag != LBER_DEFAULT || len != 0 ) {
1870                 tag = LBER_ERROR;
1871                 goto error;
1872         }
1873         tag = 0;
1874
1875         st_len = 0;
1876         if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1877                 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1878         }
1879         if ( !BER_BVISNULL( &sessionSourceName ) ) {
1880                 if ( st_len ) st_len++;
1881                 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1882         }
1883         if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1884                 if ( st_len ) st_len++;
1885                 st_len += formatOID.bv_len + STRLENOF( "=" )
1886                         + sessionTrackingIdentifier.bv_len;
1887         }
1888
1889         if ( st_len == 0 ) {
1890                 goto error;
1891         }
1892
1893         st_len += STRLENOF( " []" );
1894         st_pos = strlen( op->o_log_prefix );
1895
1896         if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1897                 char    *ptr = &op->o_log_prefix[ st_pos ];
1898
1899                 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1900
1901                 st_len = 0;
1902                 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1903                         ptr = lutil_strcopy( ptr, "IP=" );
1904                         ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1905                         st_len++;
1906                 }
1907
1908                 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1909                         if ( st_len ) *ptr++ = ' ';
1910                         ptr = lutil_strcopy( ptr, "NAME=" );
1911                         ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1912                         st_len++;
1913                 }
1914
1915                 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1916                         if ( st_len ) *ptr++ = ' ';
1917                         ptr = lutil_strcopy( ptr, formatOID.bv_val );
1918                         *ptr++ = '=';
1919                         ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1920                 }
1921
1922                 *ptr++ = /*[*/ ']';
1923                 *ptr = '\0';
1924         }
1925
1926 error:;
1927         (void)ber_free( ber, 1 );
1928
1929         if ( tag == LBER_ERROR ) {
1930                 rs->sr_text = "sessionTracking control decoding error";
1931                 return LDAP_PROTOCOL_ERROR;
1932         }
1933
1934
1935         return rs->sr_err;
1936 }
1937
1938 int
1939 slap_ctrl_session_tracking_add(
1940         Operation *op,
1941         SlapReply *rs,
1942         struct berval *ip,
1943         struct berval *name,
1944         struct berval *id,
1945         LDAPControl *ctrl )
1946 {
1947         BerElementBuffer berbuf;
1948         BerElement      *ber = (BerElement *)&berbuf;
1949
1950         static struct berval    oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
1951
1952         assert( ctrl != NULL );
1953
1954         ber_init2( ber, NULL, LBER_USE_DER );
1955
1956         ber_printf( ber, "{OOOO}", ip, name, &oid, id ); 
1957
1958         if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
1959                 rs->sr_err = LDAP_OTHER;
1960                 goto done;
1961         }
1962
1963         ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1964         ctrl->ldctl_iscritical = 0;
1965
1966         rs->sr_err = LDAP_SUCCESS;
1967
1968 done:;
1969         return rs->sr_err;
1970 }
1971
1972 int
1973 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
1974 {
1975         static struct berval    bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
1976         struct berval           ip = BER_BVNULL,
1977                                 name = BER_BVNULL,
1978                                 id = BER_BVNULL;
1979
1980         if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
1981                 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1982         {
1983                 char    *ptr;
1984
1985                 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
1986                 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
1987
1988                 ptr = ber_bvchr( &ip, ':' );
1989                 if ( ptr ) {
1990                         ip.bv_len = ptr - ip.bv_val;
1991                 }
1992         }
1993
1994         if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
1995                 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
1996         {
1997                 name = op->o_conn->c_peer_domain;
1998         }
1999
2000         if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2001                 id = op->o_dn;
2002         }
2003
2004         return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2005 }
2006 #endif
2007
2008 #ifdef SLAP_CONTROL_X_WHATFAILED
2009 static int parseWhatFailed(
2010         Operation *op,
2011         SlapReply *rs,
2012         LDAPControl *ctrl )
2013 {
2014         if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2015                 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2016                 return LDAP_PROTOCOL_ERROR;
2017         }
2018
2019         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2020                 rs->sr_text = "\"What Failed?\" control value not absent";
2021                 return LDAP_PROTOCOL_ERROR;
2022         }
2023
2024         op->o_whatFailed = ctrl->ldctl_iscritical
2025                 ? SLAP_CONTROL_CRITICAL
2026                 : SLAP_CONTROL_NONCRITICAL;
2027
2028         return LDAP_SUCCESS;
2029 }
2030
2031 int
2032 slap_ctrl_whatFailed_add(
2033         Operation *op,
2034         SlapReply *rs,
2035         char **oids )
2036 {
2037         BerElementBuffer berbuf;
2038         BerElement *ber = (BerElement *) &berbuf;
2039         LDAPControl **ctrls = NULL;
2040         struct berval ctrlval;
2041         int i, rc = LDAP_SUCCESS;
2042
2043         ber_init2( ber, NULL, LBER_USE_DER );
2044         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2045         ber_printf( ber, "[" /*]*/ );
2046         for ( i = 0; oids[ i ] != NULL; i++ ) {
2047                 ber_printf( ber, "s", oids[ i ] );
2048         }
2049         ber_printf( ber, /*[*/ "]" );
2050
2051         if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2052                 rc = LDAP_OTHER;
2053                 goto done;
2054         }
2055
2056         i = 0;
2057         if ( rs->sr_ctrls != NULL ) {
2058                 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2059                         if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2060                                 /* TODO: add */
2061                                 assert( 0 );
2062                         }
2063                 }
2064         }
2065
2066         ctrls = op->o_tmprealloc( rs->sr_ctrls,
2067                         sizeof(LDAPControl *)*( i + 2 )
2068                         + sizeof(LDAPControl)
2069                         + ctrlval.bv_len + 1,
2070                         op->o_tmpmemctx );
2071         if ( ctrls == NULL ) {
2072                 rc = LDAP_OTHER;
2073                 goto done;
2074         }
2075         ctrls[ i + 1 ] = NULL;
2076         ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2077         ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2078         ctrls[ i ]->ldctl_iscritical = 0;
2079         ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2080         AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2081         ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2082
2083         ber_free_buf( ber );
2084
2085         rs->sr_ctrls = ctrls;
2086
2087 done:;
2088         return rc;
2089 }
2090 #endif