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