]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
add 'next[/prev]' initializer
[openldap] / servers / slapd / controls.c
1 /* $OpenLDAP$ */
2 /* 
3  * Copyright 1999-2003 The OpenLDAP Foundation.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11 #include "portable.h"
12
13 #include <stdio.h>
14
15 #include <ac/string.h>
16 #include <ac/socket.h>
17
18 #include "slap.h"
19
20 #include "../../libraries/liblber/lber-int.h"
21
22 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
23 static SLAP_CTRL_PARSE_FN parseManageDSAit;
24 static SLAP_CTRL_PARSE_FN parseNoOp;
25 static SLAP_CTRL_PARSE_FN parsePagedResults;
26 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
27 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
28 static SLAP_CTRL_PARSE_FN parseDomainScope;
29
30 #ifdef LDAP_CONTROL_SUBENTRIES
31 static SLAP_CTRL_PARSE_FN parseSubentries;
32 #endif
33 #ifdef LDAP_CLIENT_UPDATE
34 static SLAP_CTRL_PARSE_FN parseClientUpdate;
35 #endif
36 #ifdef LDAP_SYNC
37 static SLAP_CTRL_PARSE_FN parseLdupSync;
38 #endif
39
40 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
41
42 static char *proxy_authz_extops[] = {
43         LDAP_EXOP_MODIFY_PASSWD,
44         LDAP_EXOP_X_WHO_AM_I,
45         NULL
46 };
47
48 struct slap_control {
49         /*
50          * Control OID
51          */
52         char *sc_oid;
53
54         /*
55          * Operations supported by control
56          */
57         slap_mask_t sc_mask;
58
59         /*
60          * Extended operations supported by control
61          */
62         char **sc_extendedops;
63
64         /*
65          * Control parsing callback
66          */
67         SLAP_CTRL_PARSE_FN *sc_parse;
68
69         LDAP_SLIST_ENTRY(slap_control) sc_next;
70 };
71
72 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
73         = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
74
75 /*
76  * all known request control OIDs should be added to this list
77  */
78 char **slap_known_controls = NULL;
79
80 static struct slap_control control_defs[] = {
81         { LDAP_CONTROL_VALUESRETURNFILTER,
82                 SLAP_CTRL_SEARCH, NULL,
83                 parseValuesReturnFilter, LDAP_SLIST_NEXT_INITIALIZER(next) },
84 #ifdef LDAP_CONTROL_PAGEDRESULTS
85         { LDAP_CONTROL_PAGEDRESULTS,
86                 SLAP_CTRL_SEARCH, NULL,
87                 parsePagedResults, LDAP_SLIST_NEXT_INITIALIZER(next) },
88 #endif
89 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
90         { LDAP_CONTROL_X_DOMAIN_SCOPE,
91                 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
92                 parseDomainScope, LDAP_SLIST_NEXT_INITIALIZER(next) },
93 #endif
94 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
95         { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
96                 SLAP_CTRL_MODIFY, NULL,
97                 parsePermissiveModify, LDAP_SLIST_NEXT_INITIALIZER(next) },
98 #endif
99 #ifdef LDAP_CONTROL_SUBENTRIES
100         { LDAP_CONTROL_SUBENTRIES,
101                 SLAP_CTRL_SEARCH, NULL,
102                 parseSubentries, LDAP_SLIST_NEXT_INITIALIZER(next) },
103 #endif
104         { LDAP_CONTROL_NOOP,
105                 SLAP_CTRL_ACCESS, NULL,
106                 parseNoOp, LDAP_SLIST_NEXT_INITIALIZER(next) },
107 #ifdef LDAP_CLIENT_UPDATE
108         { LDAP_CONTROL_CLIENT_UPDATE,
109                 SLAP_CTRL_SEARCH, NULL,
110                 parseClientUpdate, LDAP_SLIST_NEXT_INITIALIZER(next) },
111 #endif
112 #ifdef LDAP_SYNC
113         { LDAP_CONTROL_SYNC,
114                 SLAP_CTRL_SEARCH, NULL,
115                 parseLdupSync, LDAP_SLIST_NEXT_INITIALIZER(next) },
116 #endif
117         { LDAP_CONTROL_MANAGEDSAIT,
118                 SLAP_CTRL_ACCESS, NULL,
119                 parseManageDSAit, LDAP_SLIST_NEXT_INITIALIZER(next) },
120         { LDAP_CONTROL_PROXY_AUTHZ,
121                 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
122                 parseProxyAuthz, LDAP_SLIST_NEXT_INITIALIZER(next) },
123         { NULL, 0, NULL, 0, LDAP_SLIST_NEXT_INITIALIZER(next) }
124 };
125
126 /*
127  * Register a supported control.
128  *
129  * This can be called by an OpenLDAP plugin or, indirectly, by a
130  * SLAPI plugin calling slapi_register_supported_control().
131  */
132 int
133 register_supported_control(const char *controloid,
134         slap_mask_t controlmask,
135         char **controlexops,
136         SLAP_CTRL_PARSE_FN *controlparsefn)
137 {
138         struct slap_control *sc;
139         int i;
140
141         if ( controloid == NULL ) {
142                 return LDAP_PARAM_ERROR;
143         }
144
145         sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
146         if ( sc == NULL ) {
147                 return LDAP_NO_MEMORY;
148         }
149         sc->sc_oid = ch_strdup( controloid );
150         sc->sc_mask = controlmask;
151         if ( controlexops != NULL ) {
152                 sc->sc_extendedops = ldap_charray_dup( controlexops );
153                 if ( sc->sc_extendedops == NULL ) {
154                         ch_free( sc );
155                         return LDAP_NO_MEMORY;
156                 }
157         } else {
158                 sc->sc_extendedops = NULL;
159         }
160         sc->sc_parse = controlparsefn;
161
162         /* Update slap_known_controls, too. */
163         if ( slap_known_controls == NULL ) {
164                 slap_known_controls = (char **)SLAP_MALLOC( 2 * sizeof(char *) );
165                 if ( slap_known_controls == NULL ) {
166                         if ( sc->sc_extendedops != NULL ) ldap_charray_free( sc->sc_extendedops );
167                         ch_free( sc );
168                         return LDAP_NO_MEMORY;
169                 }
170                 slap_known_controls[0] = ch_strdup( sc->sc_oid );
171                 slap_known_controls[1] = NULL;
172         } else {
173                 for ( i = 0; slap_known_controls[i] != NULL; i++ )
174                         ;
175                 slap_known_controls = (char **)SLAP_REALLOC( slap_known_controls, (i + 2) * sizeof(char *) );
176                 if ( slap_known_controls == NULL ) {
177                         if ( sc->sc_extendedops != NULL ) ldap_charray_free( sc->sc_extendedops );
178                         ch_free( sc );
179                         return LDAP_NO_MEMORY;
180                 }
181                 slap_known_controls[i++] = ch_strdup( sc->sc_oid );
182                 slap_known_controls[i] = NULL;
183         }
184
185         LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
186         LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
187
188         return LDAP_SUCCESS;
189 }
190
191 /*
192  * One-time initialization of internal controls.
193  */
194 int
195 slap_controls_init( void )
196 {
197         int i, rc;
198         struct slap_control *sc;
199
200         rc = LDAP_SUCCESS;
201
202         for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
203                 rc = register_supported_control( control_defs[i].sc_oid,
204                         control_defs[i].sc_mask, control_defs[i].sc_extendedops,
205                         control_defs[i].sc_parse );
206                 if ( rc != LDAP_SUCCESS )
207                         break;
208         }
209
210         return rc;
211 }
212
213 /*
214  * Free memory associated with list of supported controls.
215  */
216 void
217 controls_destroy( void )
218 {
219         struct slap_control *sc;
220
221         while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
222                 sc = LDAP_SLIST_FIRST(&controls_list);
223                 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
224
225                 ch_free( sc->sc_oid );
226                 if ( sc->sc_extendedops != NULL ) {
227                         ldap_charray_free( sc->sc_extendedops );
228                 }
229         }
230 }
231
232 /*
233  * Format the supportedControl attribute of the root DSE,
234  * detailing which controls are supported by the directory
235  * server.
236  */
237 int
238 controls_root_dse_info( Entry *e )
239 {
240         AttributeDescription *ad_supportedControl
241                 = slap_schema.si_ad_supportedControl;
242         struct berval vals[2];
243         struct slap_control *sc;
244
245         vals[1].bv_val = NULL;
246         vals[1].bv_len = 0;
247
248         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
249                 vals[0].bv_val = sc->sc_oid;
250                 vals[0].bv_len = strlen( sc->sc_oid );
251 #ifdef SLAP_NVALUES
252                 if ( attr_merge( e, ad_supportedControl, vals, NULL ) )
253 #else
254                 if ( attr_merge( e, ad_supportedControl, vals ) )
255 #endif /* SLAP_NVALUES */
256                         return -1;
257         }
258
259         return 0;
260 }
261
262 /*
263  * Return a list of OIDs and operation masks for supported
264  * controls. Used by SLAPI.
265  */
266 int
267 get_supported_controls(char ***ctrloidsp,
268         slap_mask_t **ctrlmasks)
269 {
270         int i, n;
271         char **oids;
272         slap_mask_t *masks;
273         int rc;
274         struct slap_control *sc;
275
276         n = 0;
277
278         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
279                 n++;
280         }
281
282         if ( n == 0 ) {
283                 *ctrloidsp = NULL;
284                 *ctrlmasks = NULL;
285                 return LDAP_SUCCESS;
286         }
287
288         oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
289         if ( oids == NULL ) {
290                 return LDAP_NO_MEMORY;
291         }
292         masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
293         if  ( masks == NULL ) {
294                 ch_free( oids );
295                 return LDAP_NO_MEMORY;
296         }
297
298         n = 0;
299
300         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
301                 oids[n] = ch_strdup( sc->sc_oid );
302                 masks[n] = sc->sc_mask;
303                 n++;
304         }
305         oids[n] = NULL;
306         masks[n] = 0;
307
308         *ctrloidsp = oids;
309         *ctrlmasks = masks;
310
311         return LDAP_SUCCESS;
312 }
313
314 /*
315  * Find a control given its OID.
316  */
317 static struct slap_control *
318 find_ctrl( const char *oid )
319 {
320         struct slap_control *sc;
321
322         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
323                 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
324                         return sc;
325                 }
326         }
327
328         return NULL;
329 }
330
331 int get_ctrls(
332         Connection *conn,
333         Operation *op,
334         int sendres )
335 {
336         int nctrls = 0;
337         ber_tag_t tag;
338         ber_len_t len;
339         char *opaque;
340         BerElement *ber = op->o_ber;
341         struct slap_control *sc;
342         int rc = LDAP_SUCCESS;
343         const char *errmsg = NULL;
344
345         len = ber_pvt_ber_remaining(ber);
346
347         if( len == 0) {
348                 /* no controls */
349                 rc = LDAP_SUCCESS;
350                 return rc;
351         }
352
353         if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
354                 if( tag == LBER_ERROR ) {
355                         rc = SLAPD_DISCONNECT;
356                         errmsg = "unexpected data in PDU";
357                 }
358
359                 goto return_results;
360         }
361
362 #ifdef NEW_LOGGING
363         LDAP_LOG( OPERATION, ENTRY,
364                 "get_ctrls: conn %lu\n", conn->c_connid, 0, 0 );
365 #else
366         Debug( LDAP_DEBUG_TRACE,
367                 "=> get_ctrls\n", 0, 0, 0 );
368 #endif
369
370         if( op->o_protocol < LDAP_VERSION3 ) {
371                 rc = SLAPD_DISCONNECT;
372                 errmsg = "controls require LDAPv3";
373                 goto return_results;
374         }
375
376         /* one for first control, one for termination */
377         op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
378
379 #if 0
380         if( op->ctrls == NULL ) {
381                 rc = LDAP_NO_MEMORY;
382                 errmsg = "no memory";
383                 goto return_results;
384         }
385 #endif
386
387         op->o_ctrls[nctrls] = NULL;
388
389         /* step through each element */
390         for( tag = ber_first_element( ber, &len, &opaque );
391                 tag != LBER_ERROR;
392                 tag = ber_next_element( ber, &len, opaque ) )
393         {
394                 LDAPControl *c;
395                 LDAPControl **tctrls;
396
397                 c = ch_calloc( 1, sizeof(LDAPControl) );
398
399 #if 0
400                 if( c == NULL ) {
401                         ldap_controls_free(op->o_ctrls);
402                         op->o_ctrls = NULL;
403
404                         rc = LDAP_NO_MEMORY;
405                         errmsg = "no memory";
406                         goto return_results;
407                 }
408 #endif
409
410                 /* allocate pointer space for current controls (nctrls)
411                  * + this control + extra NULL
412                  */
413                 tctrls = ch_realloc( op->o_ctrls,
414                         (nctrls+2) * sizeof(LDAPControl *));
415
416 #if 0
417                 if( tctrls == NULL ) {
418                         ch_free( c );
419                         ldap_controls_free(op->o_ctrls);
420                         op->o_ctrls = NULL;
421
422                         rc = LDAP_NO_MEMORY;
423                         errmsg = "no memory";
424                         goto return_results;
425                 }
426 #endif
427                 op->o_ctrls = tctrls;
428
429                 op->o_ctrls[nctrls++] = c;
430                 op->o_ctrls[nctrls] = NULL;
431
432                 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
433
434                 if( tag == LBER_ERROR ) {
435 #ifdef NEW_LOGGING
436                         LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
437                                 conn->c_connid, 0, 0 );
438 #else
439                         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
440                                 0, 0, 0 );
441 #endif
442
443                         ldap_controls_free( op->o_ctrls );
444                         op->o_ctrls = NULL;
445                         rc = SLAPD_DISCONNECT;
446                         errmsg = "decoding controls error";
447                         goto return_results;
448
449                 } else if( c->ldctl_oid == NULL ) {
450 #ifdef NEW_LOGGING
451                         LDAP_LOG( OPERATION, INFO,
452                                 "get_ctrls: conn %lu got emtpy OID.\n",
453                                 conn->c_connid, 0, 0 );
454 #else
455                         Debug( LDAP_DEBUG_TRACE,
456                                 "get_ctrls: conn %lu got emtpy OID.\n",
457                                 conn->c_connid, 0, 0 );
458 #endif
459
460                         ldap_controls_free( op->o_ctrls );
461                         op->o_ctrls = NULL;
462                         rc = LDAP_PROTOCOL_ERROR;
463                         errmsg = "OID field is empty";
464                         goto return_results;
465                 }
466
467                 tag = ber_peek_tag( ber, &len );
468
469                 if( tag == LBER_BOOLEAN ) {
470                         ber_int_t crit;
471                         tag = ber_scanf( ber, "b", &crit );
472
473                         if( tag == LBER_ERROR ) {
474 #ifdef NEW_LOGGING
475                                 LDAP_LOG( OPERATION, INFO, 
476                                         "get_ctrls: conn %lu get crit failed.\n", 
477                                         conn->c_connid, 0, 0 );
478 #else
479                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
480                                         0, 0, 0 );
481 #endif
482                                 ldap_controls_free( op->o_ctrls );
483                                 op->o_ctrls = NULL;
484                                 rc = SLAPD_DISCONNECT;
485                                 errmsg = "decoding controls error";
486                                 goto return_results;
487                         }
488
489                         c->ldctl_iscritical = (crit != 0);
490                         tag = ber_peek_tag( ber, &len );
491                 }
492
493                 if( tag == LBER_OCTETSTRING ) {
494                         tag = ber_scanf( ber, "o", &c->ldctl_value );
495
496                         if( tag == LBER_ERROR ) {
497 #ifdef NEW_LOGGING
498                                 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
499                                         "%s (%scritical): get value failed.\n",
500                                         conn->c_connid, c->ldctl_oid,
501                                         c->ldctl_iscritical ? "" : "non" );
502 #else
503                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
504                                         "%s (%scritical): get value failed.\n",
505                                         conn->c_connid, c->ldctl_oid,
506                                         c->ldctl_iscritical ? "" : "non" );
507 #endif
508                                 ldap_controls_free( op->o_ctrls );
509                                 op->o_ctrls = NULL;
510                                 rc = SLAPD_DISCONNECT;
511                                 errmsg = "decoding controls error";
512                                 goto return_results;
513                         }
514                 }
515
516 #ifdef NEW_LOGGING
517                 LDAP_LOG( OPERATION, INFO, 
518                         "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
519                         conn->c_connid, c->ldctl_oid, c->ldctl_iscritical ? "" : "non" );
520 #else
521                 Debug( LDAP_DEBUG_TRACE,
522                         "=> get_ctrls: oid=\"%s\" (%scritical)\n",
523                         c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
524 #endif
525
526                 sc = find_ctrl( c->ldctl_oid );
527                 if( sc != NULL ) {
528                         /* recognized control */
529                         slap_mask_t tagmask;
530                         switch( op->o_tag ) {
531                         case LDAP_REQ_ADD:
532                                 tagmask = SLAP_CTRL_ADD;
533                                 break;
534                         case LDAP_REQ_BIND:
535                                 tagmask = SLAP_CTRL_BIND;
536                                 break;
537                         case LDAP_REQ_COMPARE:
538                                 tagmask = SLAP_CTRL_COMPARE;
539                                 break;
540                         case LDAP_REQ_DELETE:
541                                 tagmask = SLAP_CTRL_DELETE;
542                                 break;
543                         case LDAP_REQ_MODIFY:
544                                 tagmask = SLAP_CTRL_MODIFY;
545                                 break;
546                         case LDAP_REQ_RENAME:
547                                 tagmask = SLAP_CTRL_RENAME;
548                                 break;
549                         case LDAP_REQ_SEARCH:
550                                 tagmask = SLAP_CTRL_SEARCH;
551                                 break;
552                         case LDAP_REQ_UNBIND:
553                                 tagmask = SLAP_CTRL_UNBIND;
554                                 break;
555                         case LDAP_REQ_ABANDON:
556                                 tagmask = SLAP_CTRL_ABANDON;
557                                 break;
558                         case LDAP_REQ_EXTENDED:
559                                 tagmask=~0L;
560                                 assert( op->o_extendedop != NULL );
561                                 if( sc->sc_extendedops != NULL ) {
562                                         int i;
563                                         for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
564                                                 if( strcmp( op->o_extendedop, sc->sc_extendedops[i] )
565                                                         == 0 )
566                                                 {
567                                                         tagmask=0L;
568                                                         break;
569                                                 }
570                                         }
571                                 }
572                                 break;
573                         default:
574                                 rc = LDAP_OTHER;
575                                 errmsg = "controls internal error";
576                                 goto return_results;
577                         }
578
579                         if (( sc->sc_mask & tagmask ) == tagmask ) {
580                                 /* available extension */
581
582                                 if( !sc->sc_parse ) {
583                                         rc = LDAP_OTHER;
584                                         errmsg = "not yet implemented";
585                                         goto return_results;
586                                 }
587
588                                 rc = sc->sc_parse( conn, op, c, &errmsg );
589
590                                 if( rc != LDAP_SUCCESS ) goto return_results;
591
592                                 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
593                                         /* kludge to disable backend_control() check */
594                                         c->ldctl_iscritical = 0;
595
596                                 } else if ( tagmask == SLAP_CTRL_SEARCH &&
597                                         sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
598                                 {
599                                         /* kludge to disable backend_control() check */
600                                         c->ldctl_iscritical = 0;
601                                 }
602
603                         } else if( c->ldctl_iscritical ) {
604                                 /* unavailable CRITICAL control */
605                                 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
606                                 errmsg = "critical extension is unavailable";
607                                 goto return_results;
608                         }
609
610                 } else if( c->ldctl_iscritical ) {
611                         /* unrecognized CRITICAL control */
612                         rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
613                         errmsg = "critical extension is not recognized";
614                         goto return_results;
615                 }
616         }
617
618 return_results:
619 #ifdef NEW_LOGGING
620         LDAP_LOG( OPERATION, RESULTS, 
621                 "get_ctrls: n=%d rc=%d err=\"%s\"\n",
622                 nctrls, rc, errmsg ? errmsg : "" );
623 #else
624         Debug( LDAP_DEBUG_TRACE,
625                 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
626                 nctrls, rc, errmsg ? errmsg : "");
627 #endif
628
629         if( sendres && rc != LDAP_SUCCESS ) {
630                 if( rc == SLAPD_DISCONNECT ) {
631                         send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
632                 } else {
633                         send_ldap_result( conn, op, rc,
634                                 NULL, errmsg, NULL, NULL );
635                 }
636         }
637
638         return rc;
639 }
640
641 static int parseManageDSAit (
642         Connection *conn,
643         Operation *op,
644         LDAPControl *ctrl,
645         const char **text )
646 {
647         if ( op->o_managedsait != SLAP_NO_CONTROL ) {
648                 *text = "manageDSAit control specified multiple times";
649                 return LDAP_PROTOCOL_ERROR;
650         }
651
652         if ( ctrl->ldctl_value.bv_len ) {
653                 *text = "manageDSAit control value not empty";
654                 return LDAP_PROTOCOL_ERROR;
655         }
656
657         op->o_managedsait = ctrl->ldctl_iscritical
658                 ? SLAP_CRITICAL_CONTROL
659                 : SLAP_NONCRITICAL_CONTROL;
660
661         return LDAP_SUCCESS;
662 }
663
664 static int parseProxyAuthz (
665         Connection *conn,
666         Operation *op,
667         LDAPControl *ctrl,
668         const char **text )
669 {
670         int rc;
671         struct berval dn = { 0, NULL };
672
673         if ( op->o_proxy_authz != SLAP_NO_CONTROL ) {
674                 *text = "proxy authorization control specified multiple times";
675                 return LDAP_PROTOCOL_ERROR;
676         }
677
678         op->o_proxy_authz = ctrl->ldctl_iscritical
679                 ? SLAP_CRITICAL_CONTROL
680                 : SLAP_NONCRITICAL_CONTROL;
681
682 #ifdef NEW_LOGGING
683         LDAP_LOG( OPERATION, ARGS, 
684                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
685                 conn->c_connid,
686                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
687                 0 );
688 #else
689         Debug( LDAP_DEBUG_ARGS,
690                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
691                 conn->c_connid,
692                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
693                 0 );
694 #endif
695
696         if( ctrl->ldctl_value.bv_len == 0 ) {
697 #ifdef NEW_LOGGING
698                 LDAP_LOG( OPERATION, RESULTS, 
699                         "parseProxyAuthz: conn=%lu anonymous\n", 
700                         conn->c_connid, 0, 0 );
701 #else
702                 Debug( LDAP_DEBUG_TRACE,
703                         "parseProxyAuthz: conn=%lu anonymous\n", 
704                         conn->c_connid, 0, 0 );
705 #endif
706
707                 /* anonymous */
708                 free( op->o_dn.bv_val );
709                 op->o_dn.bv_len = 0;
710                 op->o_dn.bv_val = ch_strdup( "" );
711
712                 free( op->o_ndn.bv_val );
713                 op->o_ndn.bv_len = 0;
714                 op->o_ndn.bv_val = ch_strdup( "" );
715
716                 return LDAP_SUCCESS;
717         }
718
719         rc = slap_sasl_getdn( conn,
720                 ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len,
721                 NULL, &dn, SLAP_GETDN_AUTHZID );
722
723         if( rc != LDAP_SUCCESS || !dn.bv_len ) {
724                 if ( dn.bv_val ) {
725                         ch_free( dn.bv_val );
726                 }
727                 *text = "authzId mapping failed";
728                 return LDAP_PROXY_AUTHZ_FAILURE;
729         }
730
731 #ifdef NEW_LOGGING
732         LDAP_LOG( OPERATION, RESULTS, 
733                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
734                 conn->c_connid,
735                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
736 #else
737         Debug( LDAP_DEBUG_TRACE,
738                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
739                 conn->c_connid,
740                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
741 #endif
742
743         rc = slap_sasl_authorized( conn, &op->o_ndn, &dn );
744
745         if( rc ) {
746                 ch_free( dn.bv_val );
747                 *text = "not authorized to assume identity";
748                 return LDAP_PROXY_AUTHZ_FAILURE;
749         }
750
751         ch_free( op->o_dn.bv_val );
752         ch_free( op->o_ndn.bv_val );
753
754         op->o_dn.bv_val = NULL;
755         op->o_ndn = dn;
756
757         /*
758          * NOTE: since slap_sasl_getdn() returns a normalized dn,
759          * from now on op->o_dn is normalized
760          */
761         ber_dupbv( &op->o_dn, &dn );
762
763         return LDAP_SUCCESS;
764 }
765
766 static int parseNoOp (
767         Connection *conn,
768         Operation *op,
769         LDAPControl *ctrl,
770         const char **text )
771 {
772         if ( op->o_noop != SLAP_NO_CONTROL ) {
773                 *text = "noop control specified multiple times";
774                 return LDAP_PROTOCOL_ERROR;
775         }
776
777         if ( ctrl->ldctl_value.bv_len ) {
778                 *text = "noop control value not empty";
779                 return LDAP_PROTOCOL_ERROR;
780         }
781
782         op->o_noop = ctrl->ldctl_iscritical
783                 ? SLAP_CRITICAL_CONTROL
784                 : SLAP_NONCRITICAL_CONTROL;
785
786         return LDAP_SUCCESS;
787 }
788
789 #ifdef LDAP_CONTROL_PAGEDRESULTS
790 static int parsePagedResults (
791         Connection *conn,
792         Operation *op,
793         LDAPControl *ctrl,
794         const char **text )
795 {
796         ber_tag_t tag;
797         ber_int_t size;
798         BerElement *ber;
799         struct berval cookie = { 0, NULL };
800
801         if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
802                 *text = "paged results control specified multiple times";
803                 return LDAP_PROTOCOL_ERROR;
804         }
805
806         if ( ctrl->ldctl_value.bv_len == 0 ) {
807                 *text = "paged results control value is empty (or absent)";
808                 return LDAP_PROTOCOL_ERROR;
809         }
810
811         /* Parse the control value
812          *      realSearchControlValue ::= SEQUENCE {
813          *              size    INTEGER (0..maxInt),
814          *                              -- requested page size from client
815          *                              -- result set size estimate from server
816          *              cookie  OCTET STRING
817          * }
818          */
819         ber = ber_init( &ctrl->ldctl_value );
820         if( ber == NULL ) {
821                 *text = "internal error";
822                 return LDAP_OTHER;
823         }
824
825         tag = ber_scanf( ber, "{im}", &size, &cookie );
826         (void) ber_free( ber, 1 );
827
828         if( tag == LBER_ERROR ) {
829                 *text = "paged results control could not be decoded";
830                 return LDAP_PROTOCOL_ERROR;
831         }
832
833         if( size < 0 ) {
834                 *text = "paged results control size invalid";
835                 return LDAP_PROTOCOL_ERROR;
836         }
837
838         if( cookie.bv_len ) {
839                 PagedResultsCookie reqcookie;
840                 if( cookie.bv_len != sizeof( reqcookie ) ) {
841                         /* bad cookie */
842                         *text = "paged results cookie is invalid";
843                         return LDAP_PROTOCOL_ERROR;
844                 }
845
846                 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
847
848                 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
849                         /* bad cookie */
850                         *text = "paged results cookie is invalid";
851                         return LDAP_PROTOCOL_ERROR;
852
853                 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
854                         *text = "paged results cookie is invalid or old";
855                         return LDAP_UNWILLING_TO_PERFORM;
856                 }
857         } else {
858                 /* Initial request.  Initialize state. */
859                 op->o_pagedresults_state.ps_cookie = 0;
860                 op->o_pagedresults_state.ps_id = NOID;
861         }
862
863         op->o_pagedresults_size = size;
864
865         op->o_pagedresults = ctrl->ldctl_iscritical
866                 ? SLAP_CRITICAL_CONTROL
867                 : SLAP_NONCRITICAL_CONTROL;
868
869         return LDAP_SUCCESS;
870 }
871 #endif
872
873 int parseValuesReturnFilter (
874         Connection *conn,
875         Operation *op,
876         LDAPControl *ctrl,
877         const char **text )
878 {
879         int             rc;
880         BerElement      *ber;
881         struct berval   fstr = { 0, NULL };
882         const char *err_msg = "";
883
884         if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
885                 *text = "valuesReturnFilter control specified multiple times";
886                 return LDAP_PROTOCOL_ERROR;
887         }
888
889         if ( ctrl->ldctl_value.bv_len == 0 ) {
890                 *text = "valuesReturnFilter control value is empty (or absent)";
891                 return LDAP_PROTOCOL_ERROR;
892         }
893
894         ber = ber_init( &(ctrl->ldctl_value) );
895         if (ber == NULL) {
896                 *text = "internal error";
897                 return LDAP_OTHER;
898         }
899         
900         rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
901
902         if( rc != LDAP_SUCCESS ) {
903                 text = &err_msg;
904                 if( rc == SLAPD_DISCONNECT ) {
905                         send_ldap_disconnect( conn, op,
906                                 LDAP_PROTOCOL_ERROR, *text );
907                 } else {
908                         send_ldap_result( conn, op, rc,
909                                 NULL, *text, NULL, NULL );
910                 }
911                 if( fstr.bv_val != NULL) free( fstr.bv_val );
912                 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter ); 
913
914         } else {
915                 vrFilter2bv( op->vrFilter, &fstr );
916         }
917
918 #ifdef NEW_LOGGING
919         LDAP_LOG( OPERATION, ARGS, 
920                 "parseValuesReturnFilter: conn %d       vrFilter: %s\n", 
921                 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
922 #else
923         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
924                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
925 #endif
926
927         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
928                 ? SLAP_CRITICAL_CONTROL
929                 : SLAP_NONCRITICAL_CONTROL;
930
931         return LDAP_SUCCESS;
932 }
933
934 #ifdef LDAP_CONTROL_SUBENTRIES
935 static int parseSubentries (
936         Connection *conn,
937         Operation *op,
938         LDAPControl *ctrl,
939         const char **text )
940 {
941         if ( op->o_subentries != SLAP_NO_CONTROL ) {
942                 *text = "subentries control specified multiple times";
943                 return LDAP_PROTOCOL_ERROR;
944         }
945
946         /* FIXME: should use BER library */
947         if( ( ctrl->ldctl_value.bv_len != 3 )
948                 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
949                 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
950         {
951                 *text = "subentries control value encoding is bogus";
952                 return LDAP_PROTOCOL_ERROR;
953         }
954
955         op->o_subentries = ctrl->ldctl_iscritical
956                 ? SLAP_CRITICAL_CONTROL
957                 : SLAP_NONCRITICAL_CONTROL;
958
959         op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
960
961         return LDAP_SUCCESS;
962 }
963 #endif
964
965 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
966 static int parsePermissiveModify (
967         Connection *conn,
968         Operation *op,
969         LDAPControl *ctrl,
970         const char **text )
971 {
972         if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
973                 *text = "permissiveModify control specified multiple times";
974                 return LDAP_PROTOCOL_ERROR;
975         }
976
977         if ( ctrl->ldctl_value.bv_len ) {
978                 *text = "permissiveModify control value not empty";
979                 return LDAP_PROTOCOL_ERROR;
980         }
981
982         op->o_permissive_modify = ctrl->ldctl_iscritical
983                 ? SLAP_CRITICAL_CONTROL
984                 : SLAP_NONCRITICAL_CONTROL;
985
986         return LDAP_SUCCESS;
987 }
988 #endif
989
990 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
991 static int parseDomainScope (
992         Connection *conn,
993         Operation *op,
994         LDAPControl *ctrl,
995         const char **text )
996 {
997         if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
998                 *text = "domainScope control specified multiple times";
999                 return LDAP_PROTOCOL_ERROR;
1000         }
1001
1002         if ( ctrl->ldctl_value.bv_len ) {
1003                 *text = "domainScope control value not empty";
1004                 return LDAP_PROTOCOL_ERROR;
1005         }
1006
1007         op->o_domain_scope = ctrl->ldctl_iscritical
1008                 ? SLAP_CRITICAL_CONTROL
1009                 : SLAP_NONCRITICAL_CONTROL;
1010
1011         return LDAP_SUCCESS;
1012 }
1013 #endif
1014
1015 #ifdef LDAP_CLIENT_UPDATE
1016 static int parseClientUpdate (
1017         Connection *conn,
1018         Operation *op,
1019         LDAPControl *ctrl,
1020         const char **text )
1021 {
1022         ber_tag_t tag;
1023         BerElement *ber;
1024         ber_int_t type;
1025         ber_int_t interval;
1026         ber_len_t len;
1027         struct berval scheme = { 0, NULL };
1028         struct berval cookie = { 0, NULL };
1029
1030         if ( op->o_clientupdate != SLAP_NO_CONTROL ) {
1031                 *text = "LCUP client update control specified multiple times";
1032                 return LDAP_PROTOCOL_ERROR;
1033         }
1034
1035 #ifdef LDAP_SYNC
1036         if ( op->o_sync != SLAP_NO_CONTROL ) {
1037                 *text = "LDAP Client Update and Sync controls used together";
1038                 return LDAP_PROTOCOL_ERROR;
1039         }
1040 #endif
1041
1042         if ( ctrl->ldctl_value.bv_len == 0 ) {
1043                 *text = "LCUP client update control value is empty (or absent)";
1044                 return LDAP_PROTOCOL_ERROR;
1045         }
1046
1047         /* Parse the control value
1048          *      ClientUpdateControlValue ::= SEQUENCE {
1049          *              updateType      ENUMERATED {
1050          *                                      synchronizeOnly {0},
1051          *                                      synchronizeAndPersist {1},
1052          *                                      persistOnly {2} },
1053          *              sendCookieInterval INTEGER OPTIONAL,
1054          *              cookie          LCUPCookie OPTIONAL
1055          *      }
1056          */
1057
1058         ber = ber_init( &ctrl->ldctl_value );
1059         if( ber == NULL ) {
1060                 *text = "internal error";
1061                 return LDAP_OTHER;
1062         }
1063
1064         if ( (tag = ber_scanf( ber, "{i" /*}*/, &type )) == LBER_ERROR ) {
1065                 *text = "LCUP client update control : decoding error";
1066                 return LDAP_PROTOCOL_ERROR;
1067         }
1068
1069         switch( type ) {
1070         case LDAP_CUP_SYNC_ONLY:
1071                 type = SLAP_LCUP_SYNC;
1072                 break;
1073         case LDAP_CUP_SYNC_AND_PERSIST:
1074                 type = SLAP_LCUP_SYNC_AND_PERSIST;
1075                 break;
1076         case LDAP_CUP_PERSIST_ONLY:
1077                 type = SLAP_LCUP_PERSIST;
1078                 break;
1079         default:
1080                 *text = "LCUP client update control : unknown update type";
1081                 return LDAP_PROTOCOL_ERROR;
1082         }
1083
1084         if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
1085                 *text = "LCUP client update control : decoding error";
1086                 return LDAP_PROTOCOL_ERROR;
1087         }
1088
1089         if ( tag == LDAP_CUP_TAG_INTERVAL ) {
1090                 if ( (tag = ber_scanf( ber, "i", &interval )) == LBER_ERROR ) {
1091                         *text = "LCUP client update control : decoding error";
1092                         return LDAP_PROTOCOL_ERROR;
1093                 }
1094                 
1095                 if ( interval <= 0 ) {
1096                         /* server chooses interval */
1097                         interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
1098                 }
1099
1100         } else {
1101                 /* server chooses interval */
1102                 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
1103         }
1104
1105         if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
1106                 *text = "LCUP client update control : decoding error";
1107                 return LDAP_PROTOCOL_ERROR;
1108         }
1109
1110         if ( tag == LDAP_CUP_TAG_COOKIE ) {
1111                 if ( (tag = ber_scanf( ber, /*{*/ "{mm}}",
1112                         &scheme, &cookie )) == LBER_ERROR )
1113                 {
1114                         *text = "LCUP client update control : decoding error";
1115                         return LDAP_PROTOCOL_ERROR;
1116                 }
1117         }
1118
1119         /* TODO : Cookie Scheme Validation */
1120 #if 0
1121         if ( lcup_cookie_scheme_validate(scheme) != LDAP_SUCCESS ) {
1122                 *text = "Unsupported LCUP cookie scheme";
1123                 return LCUP_UNSUPPORTED_SCHEME;
1124         }
1125
1126         if ( lcup_cookie_validate(scheme, cookie) != LDAP_SUCCESS ) {
1127                 *text = "Invalid LCUP cookie";
1128                 return LCUP_INVALID_COOKIE;
1129         }
1130 #endif
1131
1132         ber_dupbv( &op->o_clientupdate_state, &cookie );
1133
1134         (void) ber_free( ber, 1 );
1135
1136         op->o_clientupdate_type = (char) type;
1137         op->o_clientupdate_interval = interval;
1138
1139         op->o_clientupdate = ctrl->ldctl_iscritical
1140                 ? SLAP_CRITICAL_CONTROL
1141                 : SLAP_NONCRITICAL_CONTROL;
1142
1143         return LDAP_SUCCESS;
1144 }
1145 #endif
1146
1147 #ifdef LDAP_SYNC
1148 static int parseLdupSync (
1149         Connection *conn,
1150         Operation *op,
1151         LDAPControl *ctrl,
1152         const char **text )
1153 {
1154         ber_tag_t tag;
1155         BerElement *ber;
1156         ber_int_t mode;
1157         ber_len_t len;
1158         struct berval cookie = { 0, NULL };
1159
1160         if ( op->o_sync != SLAP_NO_CONTROL ) {
1161                 *text = "LDAP Sync control specified multiple times";
1162                 return LDAP_PROTOCOL_ERROR;
1163         }
1164
1165 #ifdef LDAP_CLIENT_UPDATE
1166         if ( op->o_clientupdate != SLAP_NO_CONTROL ) {
1167                 *text = "LDAP Sync and LDAP Client Update controls used together";
1168                 return LDAP_PROTOCOL_ERROR;
1169         }
1170 #endif
1171
1172         if ( ctrl->ldctl_value.bv_len == 0 ) {
1173                 *text = "LDAP Sync control value is empty (or absent)";
1174                 return LDAP_PROTOCOL_ERROR;
1175         }
1176
1177         /* Parse the control value
1178          *      syncRequestValue ::= SEQUENCE {
1179          *              mode   ENUMERATED {
1180          *                      -- 0 unused
1181          *                      refreshOnly             (1),
1182          *                      -- 2 reserved
1183          *                      refreshAndPersist       (3)
1184          *              },
1185          *              cookie  syncCookie OPTIONAL
1186          *      }
1187          */
1188
1189         ber = ber_init( &ctrl->ldctl_value );
1190         if( ber == NULL ) {
1191                 *text = "internal error";
1192                 return LDAP_OTHER;
1193         }
1194
1195         if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1196                 *text = "LDAP Sync control : mode decoding error";
1197                 return LDAP_PROTOCOL_ERROR;
1198         }
1199
1200         switch( mode ) {
1201         case LDAP_SYNC_REFRESH_ONLY:
1202                 mode = SLAP_SYNC_REFRESH;
1203                 break;
1204         case LDAP_SYNC_REFRESH_AND_PERSIST:
1205                 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1206                 break;
1207         default:
1208                 *text = "LDAP Sync control : unknown update mode";
1209                 return LDAP_PROTOCOL_ERROR;
1210         }
1211
1212         tag = ber_peek_tag( ber, &len );
1213
1214         if ( tag == LDAP_SYNC_TAG_COOKIE ) {
1215                 if (( ber_scanf( ber, /*{*/ "m}",
1216                                         &cookie )) == LBER_ERROR ) {
1217                         *text = "LDAP Sync control : cookie decoding error";
1218                         return LDAP_PROTOCOL_ERROR;
1219                 }
1220         } else {
1221                 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1222                         *text = "LDAP Sync control : decoding error";
1223                         return LDAP_PROTOCOL_ERROR;
1224                 }
1225                 cookie.bv_len = 0;
1226                 cookie.bv_val = NULL;
1227         }
1228
1229         /* TODO : Cookie Scheme Validation */
1230 #if 0
1231         if ( lcup_cookie_scheme_validate(scheme) != LDAP_SUCCESS ) {
1232                 *text = "Unsupported LCUP cookie scheme";
1233                 return LCUP_UNSUPPORTED_SCHEME;
1234         }
1235
1236         if ( lcup_cookie_validate(scheme, cookie) != LDAP_SUCCESS ) {
1237                 *text = "Invalid LCUP cookie";
1238                 return LCUP_INVALID_COOKIE;
1239         }
1240 #endif
1241
1242         ber_dupbv( &op->o_sync_state, &cookie );
1243
1244         (void) ber_free( ber, 1 );
1245
1246         op->o_sync_mode = (char) mode;
1247
1248         op->o_sync = ctrl->ldctl_iscritical
1249                 ? SLAP_CRITICAL_CONTROL
1250                 : SLAP_NONCRITICAL_CONTROL;
1251
1252         return LDAP_SUCCESS;
1253 }
1254 #endif