]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Support for global ACL overlays, corresponding SLAPI support
[openldap] / servers / slapd / backend.c
1 /* backend.c - routines for dealing with back-end databases */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27
28 #include "portable.h"
29
30 #include <stdio.h>
31
32 #include <ac/string.h>
33 #include <ac/socket.h>
34 #include <sys/stat.h>
35
36 #include "slap.h"
37 #include "lutil.h"
38 #include "lber_pvt.h"
39
40 /*
41  * If a module is configured as dynamic, its header should not
42  * get included into slapd. While this is a general rule and does
43  * not have much of an effect in UNIX, this rule should be adhered
44  * to for Windows, where dynamic object code should not be implicitly
45  * imported into slapd without appropriate __declspec(dllimport) directives.
46  */
47
48 int                     nBackendInfo = 0;
49 slap_bi_head backendInfo = LDAP_STAILQ_HEAD_INITIALIZER(backendInfo);
50
51 int                     nBackendDB = 0; 
52 slap_be_head backendDB = LDAP_STAILQ_HEAD_INITIALIZER(backendDB);
53
54 static int
55 backend_init_controls( BackendInfo *bi )
56 {
57         if ( bi->bi_controls ) {
58                 int     i;
59
60                 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
61                         int     cid;
62
63                         if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
64                                         == LDAP_CONTROL_NOT_FOUND )
65                         {
66                                 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
67                                         assert( 0 );
68                                 }
69
70                                 return -1;
71                         }
72
73                         bi->bi_ctrls[ cid ] = 1;
74                 }
75         }
76
77         return 0;
78 }
79
80 int backend_init(void)
81 {
82         int rc = -1;
83         BackendInfo *bi;
84
85         if((nBackendInfo != 0) || !LDAP_STAILQ_EMPTY(&backendInfo)) {
86                 /* already initialized */
87                 Debug( LDAP_DEBUG_ANY,
88                         "backend_init: already initialized\n", 0, 0, 0 );
89                 return -1;
90         }
91
92         for( bi=slap_binfo; bi->bi_type != NULL; bi++,nBackendInfo++ ) {
93                 assert( bi->bi_init != 0 );
94
95                 rc = bi->bi_init( bi );
96
97                 if(rc != 0) {
98                         Debug( LDAP_DEBUG_ANY,
99                                 "backend_init: initialized for type \"%s\"\n",
100                                 bi->bi_type, 0, 0 );
101                         /* destroy those we've already inited */
102                         for( nBackendInfo--;
103                                 nBackendInfo >= 0 ;
104                                 nBackendInfo-- )
105                         { 
106                                 if ( slap_binfo[nBackendInfo].bi_destroy ) {
107                                         slap_binfo[nBackendInfo].bi_destroy(
108                                                 &slap_binfo[nBackendInfo] );
109                                 }
110                         }
111                         return rc;
112                 }
113
114                 LDAP_STAILQ_INSERT_TAIL(&backendInfo, bi, bi_next);
115         }
116
117         if ( nBackendInfo > 0) {
118                 return 0;
119         }
120
121 #ifdef SLAPD_MODULES    
122         return 0;
123 #else
124
125         Debug( LDAP_DEBUG_ANY,
126                 "backend_init: failed\n",
127                 0, 0, 0 );
128
129         return rc;
130 #endif /* SLAPD_MODULES */
131 }
132
133 int backend_add(BackendInfo *aBackendInfo)
134 {
135         int rc = 0;
136
137         if ( aBackendInfo->bi_init == NULL ) {
138                 Debug( LDAP_DEBUG_ANY, "backend_add: "
139                         "backend type \"%s\" does not have the (mandatory)init function\n",
140                         aBackendInfo->bi_type, 0, 0 );
141                 return -1;
142         }
143
144         rc = aBackendInfo->bi_init(aBackendInfo);
145         if ( rc != 0) {
146                 Debug( LDAP_DEBUG_ANY,
147                         "backend_add:  initialization for type \"%s\" failed\n",
148                         aBackendInfo->bi_type, 0, 0 );
149                 return rc;
150         }
151
152         (void)backend_init_controls( aBackendInfo );
153
154         /* now add the backend type to the Backend Info List */
155         LDAP_STAILQ_INSERT_TAIL( &backendInfo, aBackendInfo, bi_next );
156         nBackendInfo++;
157         return 0;
158 }
159
160 static int
161 backend_set_controls( BackendDB *be )
162 {
163         BackendInfo     *bi = be->bd_info;
164
165         /* back-relay takes care of itself; so may do other */
166         if ( overlay_is_over( be ) ) {
167                 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
168         }
169
170         if ( bi->bi_controls ) {
171                 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
172                         AC_MEMCPY( be->be_ctrls, bi->bi_ctrls,
173                                         sizeof( be->be_ctrls ) );
174                         be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
175                         
176                 } else {
177                         int     i;
178                         
179                         for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
180                                 if ( bi->bi_ctrls[ i ] ) {
181                                         be->be_ctrls[ i ] = bi->bi_ctrls[ i ];
182                                 }
183                         }
184                 }
185
186         }
187
188         return 0;
189 }
190
191 /* startup a specific backend database */
192 int backend_startup_one(Backend *be)
193 {
194         int             rc = 0;
195
196         assert( be != NULL );
197
198         be->be_pending_csn_list = (struct be_pcl *)
199                 ch_calloc( 1, sizeof( struct be_pcl ));
200
201         LDAP_TAILQ_INIT( be->be_pending_csn_list );
202
203         Debug( LDAP_DEBUG_TRACE,
204                 "backend_startup_one: starting \"%s\"\n",
205                 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
206                 0, 0 );
207
208         /* set database controls */
209         (void)backend_set_controls( be );
210
211         if ( be->bd_info->bi_db_open ) {
212                 rc = be->bd_info->bi_db_open( be );
213                 if ( rc == 0 ) {
214                         (void)backend_set_controls( be );
215
216                 } else {
217                         Debug( LDAP_DEBUG_ANY,
218                                 "backend_startup_one: bi_db_open failed! (%d)\n",
219                                 rc, 0, 0 );
220                 }
221         }
222
223         return rc;
224 }
225
226 int backend_startup(Backend *be)
227 {
228         int i;
229         int rc = 0;
230         BackendInfo *bi;
231
232         if( ! ( nBackendDB > 0 ) ) {
233                 /* no databases */
234                 Debug( LDAP_DEBUG_ANY,
235                         "backend_startup: %d databases to startup.\n",
236                         nBackendDB, 0, 0 );
237                 return 1;
238         }
239
240         if(be != NULL) {
241                 if ( be->bd_info->bi_open ) {
242                         rc = be->bd_info->bi_open( be->bd_info );
243                         if ( rc != 0 ) {
244                                 Debug( LDAP_DEBUG_ANY,
245                                         "backend_startup: bi_open failed!\n",
246                                         0, 0, 0 );
247
248                                 return rc;
249                         }
250                 }
251
252                 return backend_startup_one( be );
253         }
254
255         /* open frontend, if required */
256         if ( frontendDB->bd_info->bi_db_open ) {
257                 rc = frontendDB->bd_info->bi_db_open( frontendDB );
258                 if ( rc != 0 ) {
259                         Debug( LDAP_DEBUG_ANY,
260                                 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
261                                 rc, 0, 0 );
262                         return rc;
263                 }
264         }
265
266         /* open each backend type */
267         i = -1;
268         LDAP_STAILQ_FOREACH(bi, &backendInfo, bi_next) {
269                 i++;
270                 if( bi->bi_nDB == 0) {
271                         /* no database of this type, don't open */
272                         continue;
273                 }
274
275                 if( bi->bi_open ) {
276                         rc = bi->bi_open( bi );
277                         if ( rc != 0 ) {
278                                 Debug( LDAP_DEBUG_ANY,
279                                         "backend_startup: bi_open %d (%s) failed!\n",
280                                         i, bi->bi_type, 0 );
281                                 return rc;
282                         }
283                 }
284
285                 (void)backend_init_controls( bi );
286         }
287
288         /* open each backend database */
289         i = -1;
290         LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
291                 i++;
292                 if ( be->be_suffix == NULL ) {
293                         Debug( LDAP_DEBUG_ANY,
294                                 "backend_startup: warning, database %d (%s) "
295                                 "has no suffix\n",
296                                 i, be->bd_info->bi_type, 0 );
297                 }
298                 /* append global access controls */
299                 acl_append( &be->be_acl, frontendDB->be_acl, -1 );
300
301                 rc = backend_startup_one( be );
302
303                 if ( rc ) return rc;
304         }
305
306         return rc;
307 }
308
309 int backend_num( Backend *be )
310 {
311         int i = 0;
312         BackendDB *b2;
313
314         if( be == NULL ) return -1;
315
316         LDAP_STAILQ_FOREACH( b2, &backendDB, be_next ) {
317                 if( be == b2 ) return i;
318                 i++;
319         }
320         return -1;
321 }
322
323 int backend_shutdown( Backend *be )
324 {
325         int rc = 0;
326         BackendInfo *bi;
327
328         if( be != NULL ) {
329                 /* shutdown a specific backend database */
330
331                 if ( be->bd_info->bi_nDB == 0 ) {
332                         /* no database of this type, we never opened it */
333                         return 0;
334                 }
335
336                 if ( be->bd_info->bi_db_close ) {
337                         be->bd_info->bi_db_close( be );
338                 }
339
340                 if( be->bd_info->bi_close ) {
341                         be->bd_info->bi_close( be->bd_info );
342                 }
343
344                 return 0;
345         }
346
347         /* close each backend database */
348         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
349                 if ( be->bd_info->bi_db_close ) {
350                         be->bd_info->bi_db_close( be );
351                 }
352
353                 if(rc != 0) {
354                         Debug( LDAP_DEBUG_ANY,
355                                 "backend_close: bi_db_close %s failed!\n",
356                                 be->be_type, 0, 0 );
357                 }
358         }
359
360         /* close each backend type */
361         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
362                 if( bi->bi_nDB == 0 ) {
363                         /* no database of this type */
364                         continue;
365                 }
366
367                 if( bi->bi_close ) {
368                         bi->bi_close( bi );
369                 }
370         }
371
372         /* close frontend, if required */
373         if ( frontendDB->bd_info->bi_db_close ) {
374                 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
375                 if ( rc != 0 ) {
376                         Debug( LDAP_DEBUG_ANY,
377                                 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
378                                 rc, 0, 0 );
379                 }
380         }
381
382         return 0;
383 }
384
385 void backend_destroy_one( BackendDB *bd, int dynamic )
386 {
387         if ( dynamic ) {
388                 LDAP_STAILQ_REMOVE(&backendDB, bd, slap_backend_db, be_next );
389         }
390
391         if ( bd->be_syncinfo ) {
392                 syncinfo_free( bd->be_syncinfo );
393         }
394
395         if ( bd->be_pending_csn_list ) {
396                 struct slap_csn_entry *csne;
397                 csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
398                 while ( csne ) {
399                         struct slap_csn_entry *tmp_csne = csne;
400
401                         LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
402                         ch_free( csne->ce_csn.bv_val );
403                         csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
404                         ch_free( tmp_csne );
405                 }
406                 ch_free( bd->be_pending_csn_list );
407         }
408
409         if ( bd->bd_info->bi_db_destroy ) {
410                 bd->bd_info->bi_db_destroy( bd );
411         }
412         ber_bvarray_free( bd->be_suffix );
413         ber_bvarray_free( bd->be_nsuffix );
414         if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
415                 free( bd->be_rootdn.bv_val );
416         }
417         if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
418                 free( bd->be_rootndn.bv_val );
419         }
420         if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
421                 free( bd->be_rootpw.bv_val );
422         }
423         acl_destroy( bd->be_acl, frontendDB->be_acl );
424         if ( dynamic ) {
425                 free( bd );
426         }
427 }
428
429 int backend_destroy(void)
430 {
431         BackendDB *bd;
432         BackendInfo *bi;
433
434         /* destroy each backend database */
435         while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
436                 backend_destroy_one( bd, 1 );
437         }
438
439         /* destroy each backend type */
440         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
441                 if( bi->bi_destroy ) {
442                         bi->bi_destroy( bi );
443                 }
444         }
445
446         nBackendInfo = 0;
447         LDAP_STAILQ_INIT(&backendInfo);
448
449         /* destroy frontend database */
450         bd = frontendDB;
451         if ( bd ) {
452                 if ( bd->bd_info->bi_db_destroy ) {
453                         bd->bd_info->bi_db_destroy( bd );
454                 }
455                 ber_bvarray_free( bd->be_suffix );
456                 ber_bvarray_free( bd->be_nsuffix );
457                 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
458                         free( bd->be_rootdn.bv_val );
459                 }
460                 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
461                         free( bd->be_rootndn.bv_val );
462                 }
463                 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
464                         free( bd->be_rootpw.bv_val );
465                 }
466                 acl_destroy( bd->be_acl, frontendDB->be_acl );
467         }
468
469         return 0;
470 }
471
472 BackendInfo* backend_info(const char *type)
473 {
474         BackendInfo *bi;
475
476         /* search for the backend type */
477         LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
478                 if( strcasecmp(bi->bi_type, type) == 0 ) {
479                         return bi;
480                 }
481         }
482
483         return NULL;
484 }
485
486
487 BackendDB *
488 backend_db_init(
489     const char  *type )
490 {
491         Backend *be;
492         BackendInfo *bi = backend_info(type);
493         int     rc = 0;
494
495         if( bi == NULL ) {
496                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
497                 return NULL;
498         }
499
500         be = ch_calloc( 1, sizeof(Backend) );
501         nbackends++;
502         LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
503
504         be->bd_info = bi;
505
506         be->be_def_limit = frontendDB->be_def_limit;
507         be->be_dfltaccess = frontendDB->be_dfltaccess;
508
509         be->be_restrictops = frontendDB->be_restrictops;
510         be->be_requires = frontendDB->be_requires;
511         be->be_ssf_set = frontendDB->be_ssf_set;
512
513         be->be_pcl_mutexp = &be->be_pcl_mutex;
514         ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
515
516         /* assign a default depth limit for alias deref */
517         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
518
519         if ( bi->bi_db_init ) {
520                 rc = bi->bi_db_init( be );
521         }
522
523         if ( rc != 0 ) {
524                 fprintf( stderr, "database init failed (%s)\n", type );
525                 nbackends--;
526                 return NULL;
527         }
528
529         bi->bi_nDB++;
530         return( be );
531 }
532
533 void
534 be_db_close( void )
535 {
536         BackendDB *be;
537
538         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
539                 if ( be->bd_info->bi_db_close ) {
540                         be->bd_info->bi_db_close( be );
541                 }
542         }
543
544         if ( frontendDB->bd_info->bi_db_close ) {
545                 (*frontendDB->bd_info->bi_db_close)( frontendDB );
546         }
547
548 }
549
550 Backend *
551 select_backend(
552         struct berval * dn,
553         int manageDSAit,
554         int noSubs )
555 {
556         int             j;
557         ber_len_t       len, dnlen = dn->bv_len;
558         Backend         *be, *b2 = NULL;
559
560         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
561                 if ( be->be_nsuffix == NULL ) {
562                         continue;
563                 }
564
565                 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
566                 {
567                         if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
568                         {
569                                 continue;
570                         }
571
572                         len = be->be_nsuffix[j].bv_len;
573
574                         if ( len > dnlen ) {
575                                 /* suffix is longer than DN */
576                                 continue;
577                         }
578                         
579                         /*
580                          * input DN is normalized, so the separator check
581                          * need not look at escaping
582                          */
583                         if ( len && len < dnlen &&
584                                 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
585                         {
586                                 continue;
587                         }
588
589                         if ( strcmp( be->be_nsuffix[j].bv_val,
590                                 &dn->bv_val[dnlen-len] ) == 0 )
591                         {
592                                 if( b2 == NULL ) {
593                                         b2 = be;
594
595                                         if( manageDSAit && len == dnlen &&
596                                                 !SLAP_GLUE_SUBORDINATE( be ) ) {
597                                                 continue;
598                                         }
599                                 } else {
600                                         b2 = be;
601                                 }
602                                 return b2;
603                         }
604                 }
605         }
606
607         return b2;
608 }
609
610 int
611 be_issuffix(
612     Backend *be,
613     struct berval *bvsuffix )
614 {
615         int     i;
616
617         if ( be->be_nsuffix == NULL ) {
618                 return 0;
619         }
620
621         for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
622                 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
623                         return 1;
624                 }
625         }
626
627         return 0;
628 }
629
630 int
631 be_isroot_dn( Backend *be, struct berval *ndn )
632 {
633         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
634                 return 0;
635         }
636
637         return dn_match( &be->be_rootndn, ndn );
638 }
639
640 int
641 be_slurp_update( Operation *op )
642 {
643         return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
644                 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
645 }
646
647 int
648 be_shadow_update( Operation *op )
649 {
650         return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
651                 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
652 }
653
654 int
655 be_isupdate_dn( Backend *be, struct berval *ndn )
656 {
657         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
658                 return 0;
659         }
660
661         return dn_match( &be->be_update_ndn, ndn );
662 }
663
664 struct berval *
665 be_root_dn( Backend *be )
666 {
667         return &be->be_rootdn;
668 }
669
670 int
671 be_isroot( Operation *op )
672 {
673         return be_isroot_dn( op->o_bd, &op->o_ndn );
674 }
675
676 int
677 be_isroot_pw( Operation *op )
678 {
679         int result;
680
681         if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
682                 return 0;
683         }
684
685         if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
686                 return 0;
687         }
688
689 #ifdef SLAPD_SPASSWD
690         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
691                 op->o_conn->c_sasl_authctx, NULL );
692 #endif
693
694         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
695
696 #ifdef SLAPD_SPASSWD
697         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
698 #endif
699
700         return result == 0;
701 }
702
703 int
704 be_entry_release_rw(
705         Operation *op,
706         Entry *e,
707         int rw )
708 {
709         if ( op->o_bd->be_release ) {
710                 /* free and release entry from backend */
711                 return op->o_bd->be_release( op, e, rw );
712         } else {
713                 /* free entry */
714                 entry_free( e );
715                 return 0;
716         }
717 }
718
719 int
720 backend_unbind( Operation *op, SlapReply *rs )
721 {
722         BackendDB *be;
723
724         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
725                 if ( be->be_unbind ) {
726                         op->o_bd = be;
727                         be->be_unbind( op, rs );
728                 }
729         }
730
731         return 0;
732 }
733
734 int
735 backend_connection_init(
736         Connection   *conn )
737 {
738         BackendDB *be;
739
740         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
741                 if ( be->be_connection_init ) {
742                         be->be_connection_init( be, conn );
743                 }
744         }
745
746         return 0;
747 }
748
749 int
750 backend_connection_destroy(
751         Connection   *conn )
752 {
753         BackendDB *be;
754
755         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
756                 if ( be->be_connection_destroy ) {
757                         be->be_connection_destroy( be, conn);
758                 }
759         }
760
761         return 0;
762 }
763
764 int
765 backend_check_controls(
766         Operation *op,
767         SlapReply *rs )
768 {
769         LDAPControl **ctrls = op->o_ctrls;
770         rs->sr_err = LDAP_SUCCESS;
771
772         if( ctrls ) {
773                 for( ; *ctrls != NULL ; ctrls++ ) {
774                         int cid;
775
776                         switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
777                         case LDAP_CONTROL_NOT_FOUND:
778                                 /* unrecognized control */ 
779                                 if ( (*ctrls)->ldctl_iscritical ) {
780                                         /* should not be reachable */ 
781                                         Debug( LDAP_DEBUG_ANY,
782                                                 "backend_check_controls: unrecognized control: %s\n",
783                                                 (*ctrls)->ldctl_oid, 0, 0 );
784                                         assert( 0 );
785                                 }
786                                 break;
787
788                         case LDAP_COMPARE_FALSE:
789                                 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
790                                         /* Per RFC 2251 (and LDAPBIS discussions), if the control
791                                          * is recognized and appropriate for the operation (which
792                                          * we've already verified), then the server should make
793                                          * use of the control when performing the operation.
794                                          * 
795                                          * Here we find that operation extended by the control
796                                          * is unavailable in a particular context, and the control
797                                          * is marked Critical, hence the return of
798                                          * unwillingToPerform.
799                                          */
800                                         rs->sr_text = "critical control unavailable in context";
801                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
802                                         goto done;
803                                 }
804                                 break;
805
806                         case LDAP_COMPARE_TRUE:
807                                 break;
808
809                         default:
810                                 /* unreachable */
811                                 Debug( LDAP_DEBUG_ANY,
812                                         "backend_check_controls: unable to check control: %s\n",
813                                         (*ctrls)->ldctl_oid, 0, 0 );
814                                 assert( 0 );
815
816                                 rs->sr_text = "unable to check control";
817                                 rs->sr_err = LDAP_OTHER;
818                                 goto done;
819                         }
820                 }
821         }
822
823         /* check should be generalized */
824         if( get_manageDIT(op) && !be_isroot(op)) {
825                 rs->sr_text = "requires manager authorization";
826                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
827         }
828
829 done:;
830         return rs->sr_err;
831 }
832
833 int
834 backend_check_restrictions(
835         Operation *op,
836         SlapReply *rs,
837         struct berval *opdata )
838 {
839         slap_mask_t restrictops;
840         slap_mask_t requires;
841         slap_mask_t opflag;
842         slap_mask_t exopflag = 0;
843         slap_ssf_set_t *ssf;
844         int updateop = 0;
845         int starttls = 0;
846         int session = 0;
847
848         if ( op->o_bd ) {
849                 int     rc = SLAP_CB_CONTINUE;
850
851                 if ( op->o_bd->be_chk_controls ) {
852                         rc = ( *op->o_bd->be_chk_controls )( op, rs );
853                 }
854
855                 if ( rc == SLAP_CB_CONTINUE ) {
856                         rc = backend_check_controls( op, rs );
857                 }
858
859                 if ( rc != LDAP_SUCCESS ) {
860                         return rs->sr_err;
861                 }
862
863                 restrictops = op->o_bd->be_restrictops;
864                 requires = op->o_bd->be_requires;
865                 ssf = &op->o_bd->be_ssf_set;
866
867         } else {
868                 restrictops = frontendDB->be_restrictops;
869                 requires = frontendDB->be_requires;
870                 ssf = &frontendDB->be_ssf_set;
871         }
872
873         switch( op->o_tag ) {
874         case LDAP_REQ_ADD:
875                 opflag = SLAP_RESTRICT_OP_ADD;
876                 updateop++;
877                 break;
878         case LDAP_REQ_BIND:
879                 opflag = SLAP_RESTRICT_OP_BIND;
880                 session++;
881                 break;
882         case LDAP_REQ_COMPARE:
883                 opflag = SLAP_RESTRICT_OP_COMPARE;
884                 break;
885         case LDAP_REQ_DELETE:
886                 updateop++;
887                 opflag = SLAP_RESTRICT_OP_DELETE;
888                 break;
889         case LDAP_REQ_EXTENDED:
890                 opflag = SLAP_RESTRICT_OP_EXTENDED;
891
892                 if( !opdata ) {
893                         /* treat unspecified as a modify */
894                         opflag = SLAP_RESTRICT_OP_MODIFY;
895                         updateop++;
896                         break;
897                 }
898
899                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
900                         session++;
901                         starttls++;
902                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
903                         break;
904                 }
905
906                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
907                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
908                         break;
909                 }
910
911                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
912                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
913                         break;
914                 }
915
916                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
917                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
918                         updateop++;
919                         break;
920                 }
921
922                 /* treat everything else as a modify */
923                 opflag = SLAP_RESTRICT_OP_MODIFY;
924                 updateop++;
925                 break;
926
927         case LDAP_REQ_MODIFY:
928                 updateop++;
929                 opflag = SLAP_RESTRICT_OP_MODIFY;
930                 break;
931         case LDAP_REQ_RENAME:
932                 updateop++;
933                 opflag = SLAP_RESTRICT_OP_RENAME;
934                 break;
935         case LDAP_REQ_SEARCH:
936                 opflag = SLAP_RESTRICT_OP_SEARCH;
937                 break;
938         case LDAP_REQ_UNBIND:
939                 session++;
940                 opflag = 0;
941                 break;
942         default:
943                 rs->sr_text = "restrict operations internal error";
944                 rs->sr_err = LDAP_OTHER;
945                 return rs->sr_err;
946         }
947
948         if ( !starttls ) {
949                 /* these checks don't apply to StartTLS */
950
951                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
952                 if( op->o_transport_ssf < ssf->sss_transport ) {
953                         rs->sr_text = op->o_transport_ssf
954                                 ? "stronger transport confidentiality required"
955                                 : "transport confidentiality required";
956                         return rs->sr_err;
957                 }
958
959                 if( op->o_tls_ssf < ssf->sss_tls ) {
960                         rs->sr_text = op->o_tls_ssf
961                                 ? "stronger TLS confidentiality required"
962                                 : "TLS confidentiality required";
963                         return rs->sr_err;
964                 }
965
966
967                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
968                         /* simple bind specific check */
969                         if( op->o_ssf < ssf->sss_simple_bind ) {
970                                 rs->sr_text = op->o_ssf
971                                         ? "stronger confidentiality required"
972                                         : "confidentiality required";
973                                 return rs->sr_err;
974                         }
975                 }
976
977                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
978                         /* these checks don't apply to SASL bind */
979
980                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
981                                 rs->sr_text = op->o_sasl_ssf
982                                         ? "stronger SASL confidentiality required"
983                                         : "SASL confidentiality required";
984                                 return rs->sr_err;
985                         }
986
987                         if( op->o_ssf < ssf->sss_ssf ) {
988                                 rs->sr_text = op->o_ssf
989                                         ? "stronger confidentiality required"
990                                         : "confidentiality required";
991                                 return rs->sr_err;
992                         }
993                 }
994
995                 if( updateop ) {
996                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
997                                 rs->sr_text = op->o_transport_ssf
998                                         ? "stronger transport confidentiality required for update"
999                                         : "transport confidentiality required for update";
1000                                 return rs->sr_err;
1001                         }
1002
1003                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
1004                                 rs->sr_text = op->o_tls_ssf
1005                                         ? "stronger TLS confidentiality required for update"
1006                                         : "TLS confidentiality required for update";
1007                                 return rs->sr_err;
1008                         }
1009
1010                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1011                                 rs->sr_text = op->o_sasl_ssf
1012                                         ? "stronger SASL confidentiality required for update"
1013                                         : "SASL confidentiality required for update";
1014                                 return rs->sr_err;
1015                         }
1016
1017                         if( op->o_ssf < ssf->sss_update_ssf ) {
1018                                 rs->sr_text = op->o_ssf
1019                                         ? "stronger confidentiality required for update"
1020                                         : "confidentiality required for update";
1021                                 return rs->sr_err;
1022                         }
1023
1024                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1025                                 BER_BVISEMPTY( &op->o_ndn ) )
1026                         {
1027                                 rs->sr_text = "modifications require authentication";
1028                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1029                                 return rs->sr_err;
1030                         }
1031
1032 #ifdef SLAP_X_LISTENER_MOD
1033                         if ( op->o_conn->c_listener &&
1034                                 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1035                                         ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1036                         {
1037                                 /* no "w" mode means readonly */
1038                                 rs->sr_text = "modifications not allowed on this listener";
1039                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1040                                 return rs->sr_err;
1041                         }
1042 #endif /* SLAP_X_LISTENER_MOD */
1043                 }
1044         }
1045
1046         if ( !session ) {
1047                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1048
1049                 if( requires & SLAP_REQUIRE_STRONG ) {
1050                         /* should check mechanism */
1051                         if( ( op->o_transport_ssf < ssf->sss_transport
1052                                 && op->o_authtype == LDAP_AUTH_SIMPLE )
1053                                 || BER_BVISEMPTY( &op->o_dn ) )
1054                         {
1055                                 rs->sr_text = "strong(er) authentication required";
1056                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1057                                 return rs->sr_err;
1058                         }
1059                 }
1060
1061                 if( requires & SLAP_REQUIRE_SASL ) {
1062                         if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1063                                 rs->sr_text = "SASL authentication required";
1064                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1065                                 return rs->sr_err;
1066                         }
1067                 }
1068                         
1069                 if( requires & SLAP_REQUIRE_AUTHC ) {
1070                         if( BER_BVISEMPTY( &op->o_dn ) ) {
1071                                 rs->sr_text = "authentication required";
1072                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1073                                 return rs->sr_err;
1074                         }
1075                 }
1076
1077                 if( requires & SLAP_REQUIRE_BIND ) {
1078                         int version;
1079                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1080                         version = op->o_conn->c_protocol;
1081                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1082
1083                         if( !version ) {
1084                                 /* no bind has occurred */
1085                                 rs->sr_text = "BIND required";
1086                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1087                                 return rs->sr_err;
1088                         }
1089                 }
1090
1091                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1092                         if( op->o_protocol < LDAP_VERSION3 ) {
1093                                 /* no bind has occurred */
1094                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1095                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1096                                 return rs->sr_err;
1097                         }
1098                 }
1099
1100 #ifdef SLAP_X_LISTENER_MOD
1101                 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1102                         if ( op->o_conn->c_listener &&
1103                                 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1104                 {
1105                                 /* no "x" mode means bind required */
1106                                 rs->sr_text = "bind required on this listener";
1107                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1108                                 return rs->sr_err;
1109                         }
1110                 }
1111
1112                 if ( !starttls && !updateop ) {
1113                         if ( op->o_conn->c_listener &&
1114                                 !( op->o_conn->c_listener->sl_perms &
1115                                         ( !BER_BVISEMPTY( &op->o_dn )
1116                                                 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1117                         {
1118                                 /* no "r" mode means no read */
1119                                 rs->sr_text = "read not allowed on this listener";
1120                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1121                                 return rs->sr_err;
1122                         }
1123                 }
1124 #endif /* SLAP_X_LISTENER_MOD */
1125
1126         }
1127
1128         if( ( restrictops & opflag )
1129                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1130                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1131                         rs->sr_text = "read operations restricted";
1132                 } else if ( restrictops & exopflag ) {
1133                         rs->sr_text = "extended operation restricted";
1134                 } else {
1135                         rs->sr_text = "operation restricted";
1136                 }
1137                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1138                 return rs->sr_err;
1139         }
1140
1141         rs->sr_err = LDAP_SUCCESS;
1142         return rs->sr_err;
1143 }
1144
1145 int backend_check_referrals( Operation *op, SlapReply *rs )
1146 {
1147         rs->sr_err = LDAP_SUCCESS;
1148
1149         if( op->o_bd->be_chk_referrals ) {
1150                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1151
1152                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1153                         send_ldap_result( op, rs );
1154                 }
1155         }
1156
1157         return rs->sr_err;
1158 }
1159
1160 int
1161 be_entry_get_rw(
1162         Operation *op,
1163         struct berval *ndn,
1164         ObjectClass *oc,
1165         AttributeDescription *at,
1166         int rw,
1167         Entry **e )
1168 {
1169         int rc;
1170
1171         *e = NULL;
1172
1173         if (op->o_bd == NULL) {
1174                 rc = LDAP_NO_SUCH_OBJECT;
1175         } else if ( op->o_bd->be_fetch ) {
1176                 rc = ( op->o_bd->be_fetch )( op, ndn,
1177                         oc, at, rw, e );
1178         } else {
1179                 rc = LDAP_UNWILLING_TO_PERFORM;
1180         }
1181         return rc;
1182 }
1183
1184 int 
1185 fe_acl_group(
1186         Operation *op,
1187         Entry   *target,
1188         struct berval *gr_ndn,
1189         struct berval *op_ndn,
1190         ObjectClass *group_oc,
1191         AttributeDescription *group_at )
1192 {
1193         Entry *e;
1194         Attribute *a;
1195         int rc;
1196         GroupAssertion *g;
1197         Backend *be = op->o_bd;
1198
1199         op->o_bd = select_backend( gr_ndn, 0, 0 );
1200
1201         for ( g = op->o_groups; g; g = g->ga_next ) {
1202                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1203                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1204                 {
1205                         continue;
1206                 }
1207                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1208                         break;
1209                 }
1210         }
1211
1212         if ( g ) {
1213                 rc = g->ga_res;
1214                 goto done;
1215         }
1216
1217         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1218                 e = target;
1219                 rc = 0;
1220         } else {
1221                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1222         }
1223         if ( e ) {
1224                 a = attr_find( e->e_attrs, group_at );
1225                 if ( a ) {
1226                         /* If the attribute is a subtype of labeledURI, treat this as
1227                          * a dynamic group ala groupOfURLs
1228                          */
1229                         if (is_at_subtype( group_at->ad_type,
1230                                 slap_schema.si_ad_labeledURI->ad_type ) )
1231                         {
1232                                 int i;
1233                                 LDAPURLDesc *ludp;
1234                                 struct berval bv, nbase;
1235                                 Filter *filter;
1236                                 Entry *user;
1237                                 Backend *b2 = op->o_bd;
1238
1239                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1240                                         user = target;
1241                                 } else {
1242                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1243                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1244                                 }
1245                                 
1246                                 if ( rc == 0 ) {
1247                                         rc = LDAP_COMPARE_FALSE;
1248                                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1249                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1250                                                         LDAP_URL_SUCCESS )
1251                                                 {
1252                                                         continue;
1253                                                 }
1254                                                 BER_BVZERO( &nbase );
1255                                                 /* host part must be empty */
1256                                                 /* attrs and extensions parts must be empty */
1257                                                 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1258                                                         ludp->lud_attrs || ludp->lud_exts )
1259                                                 {
1260                                                         goto loopit;
1261                                                 }
1262                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1263                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1264                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1265                                                 {
1266                                                         goto loopit;
1267                                                 }
1268                                                 switch ( ludp->lud_scope ) {
1269                                                 case LDAP_SCOPE_BASE:
1270                                                         if ( !dn_match( &nbase, op_ndn ) ) {
1271                                                                 goto loopit;
1272                                                         }
1273                                                         break;
1274                                                 case LDAP_SCOPE_ONELEVEL:
1275                                                         dnParent( op_ndn, &bv );
1276                                                         if ( !dn_match( &nbase, &bv ) ) {
1277                                                                 goto loopit;
1278                                                         }
1279                                                         break;
1280                                                 case LDAP_SCOPE_SUBTREE:
1281                                                         if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1282                                                                 goto loopit;
1283                                                         }
1284                                                         break;
1285 #ifdef LDAP_SCOPE_SUBORDINATE
1286                                                 case LDAP_SCOPE_SUBORDINATE:
1287                                                         if ( dn_match( &nbase, op_ndn ) ||
1288                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1289                                                         {
1290                                                                 goto loopit;
1291                                                         }
1292 #endif
1293                                                 }
1294                                                 filter = str2filter_x( op, ludp->lud_filter );
1295                                                 if ( filter ) {
1296                                                         if ( test_filter( NULL, user, filter ) ==
1297                                                                 LDAP_COMPARE_TRUE )
1298                                                         {
1299                                                                 rc = 0;
1300                                                         }
1301                                                         filter_free_x( op, filter );
1302                                                 }
1303 loopit:
1304                                                 ldap_free_urldesc( ludp );
1305                                                 if ( !BER_BVISNULL( &nbase ) ) {
1306                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1307                                                 }
1308                                                 if ( rc == 0 ) break;
1309                                         }
1310                                         if ( user != target ) {
1311                                                 be_entry_release_r( op, user );
1312                                         }
1313                                 }
1314                                 op->o_bd = b2;
1315                         } else {
1316                                 rc = value_find_ex( group_at,
1317                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1318                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1319                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1320                                 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1321                                         rc = LDAP_COMPARE_FALSE;
1322                         }
1323                 } else {
1324                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1325                 }
1326                 if (e != target ) {
1327                         be_entry_release_r( op, e );
1328                 }
1329         } else {
1330                 rc = LDAP_NO_SUCH_OBJECT;
1331         }
1332
1333         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1334                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1335                         op->o_tmpmemctx );
1336                 g->ga_be = op->o_bd;
1337                 g->ga_oc = group_oc;
1338                 g->ga_at = group_at;
1339                 g->ga_res = rc;
1340                 g->ga_len = gr_ndn->bv_len;
1341                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1342                 g->ga_next = op->o_groups;
1343                 op->o_groups = g;
1344         }
1345 done:
1346         op->o_bd = be;
1347         return rc;
1348 }
1349
1350 int 
1351 backend_group(
1352         Operation *op,
1353         Entry   *target,
1354         struct berval *gr_ndn,
1355         struct berval *op_ndn,
1356         ObjectClass *group_oc,
1357         AttributeDescription *group_at )
1358 {
1359         int                     rc;
1360         BackendDB               *be_orig;
1361
1362         if ( op->o_abandon ) {
1363                 return SLAPD_ABANDON;
1364         }
1365
1366         be_orig = op->o_bd;
1367         op->o_bd = frontendDB;
1368         rc = frontendDB->be_group( op, target, gr_ndn,
1369                 op_ndn, group_oc, group_at );
1370         op->o_bd = be_orig;
1371
1372         return rc;
1373 }
1374
1375 int 
1376 fe_acl_attribute(
1377         Operation *op,
1378         Entry   *target,
1379         struct berval   *edn,
1380         AttributeDescription *entry_at,
1381         BerVarray *vals,
1382         slap_access_t access )
1383 {
1384         Entry                   *e = NULL;
1385         Attribute               *a = NULL;
1386         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1387         AccessControlState      acl_state = ACL_STATE_INIT;
1388         Backend                 *be = op->o_bd;
1389
1390         op->o_bd = select_backend( edn, 0, 0 );
1391
1392         if ( target && dn_match( &target->e_nname, edn ) ) {
1393                 e = target;
1394
1395         } else {
1396                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1397         } 
1398
1399         if ( e ) {
1400                 a = attr_find( e->e_attrs, entry_at );
1401                 if ( a == NULL ) {
1402                         SlapReply       rs = { 0 };
1403                         AttributeName   anlist[ 2 ];
1404
1405                         anlist[ 0 ].an_name = entry_at->ad_cname;
1406                         anlist[ 0 ].an_desc = entry_at;
1407                         BER_BVZERO( &anlist[ 1 ].an_name );
1408                         rs.sr_attrs = anlist;
1409                         
1410                         /* NOTE: backend_operational() is also called
1411                          * when returning results, so it's supposed
1412                          * to do no harm to entries */
1413                         rs.sr_entry = e;
1414                         rc = backend_operational( op, &rs );
1415                         rs.sr_entry = NULL;
1416  
1417                         if ( rc == LDAP_SUCCESS ) {
1418                                 if ( rs.sr_operational_attrs ) {
1419                                         freeattr = 1;
1420                                         a = rs.sr_operational_attrs;
1421
1422                                 } else {
1423                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1424                                 }
1425                         }
1426                 }
1427
1428                 if ( a ) {
1429                         BerVarray v;
1430
1431                         if ( op->o_conn && access > ACL_NONE &&
1432                                 access_allowed( op, e, entry_at, NULL,
1433                                                 access, &acl_state ) == 0 )
1434                         {
1435                                 rc = LDAP_INSUFFICIENT_ACCESS;
1436                                 goto freeit;
1437                         }
1438
1439                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1440                                 ;
1441                         
1442                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1443                                 op->o_tmpmemctx );
1444                         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1445                         {
1446                                 if ( op->o_conn && access > ACL_NONE && 
1447                                         access_allowed( op, e, entry_at,
1448                                                         &a->a_nvals[i],
1449                                                         access,
1450                                                         &acl_state ) == 0 )
1451                                 {
1452                                         continue;
1453                                 }
1454                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1455                                                 op->o_tmpmemctx );
1456                                 if ( !BER_BVISNULL( &v[j] ) ) {
1457                                         j++;
1458                                 }
1459                         }
1460                         if ( j == 0 ) {
1461                                 op->o_tmpfree( v, op->o_tmpmemctx );
1462                                 *vals = NULL;
1463                                 rc = LDAP_INSUFFICIENT_ACCESS;
1464
1465                         } else {
1466                                 BER_BVZERO( &v[j] );
1467                                 *vals = v;
1468                                 rc = LDAP_SUCCESS;
1469                         }
1470                 }
1471 freeit:         if ( e != target ) {
1472                         be_entry_release_r( op, e );
1473                 }
1474                 if ( freeattr ) {
1475                         attr_free( a );
1476                 }
1477         }
1478
1479         op->o_bd = be;
1480         return rc;
1481 }
1482
1483 int 
1484 backend_attribute(
1485         Operation *op,
1486         Entry   *target,
1487         struct berval   *edn,
1488         AttributeDescription *entry_at,
1489         BerVarray *vals,
1490         slap_access_t access )
1491 {
1492         int                     rc;
1493         BackendDB               *be_orig;
1494
1495         be_orig = op->o_bd;
1496         op->o_bd = frontendDB;
1497         rc = frontendDB->be_attribute( op, target, edn,
1498                 entry_at, vals, access );
1499         op->o_bd = be_orig;
1500
1501         return rc;
1502 }
1503
1504 int 
1505 backend_access(
1506         Operation               *op,
1507         Entry                   *target,
1508         struct berval           *edn,
1509         AttributeDescription    *entry_at,
1510         struct berval           *nval,
1511         slap_access_t           access,
1512         slap_mask_t             *mask )
1513 {
1514         Entry           *e = NULL;
1515         int             rc = LDAP_INSUFFICIENT_ACCESS;
1516         Backend         *be = op->o_bd;
1517
1518         /* pedantic */
1519         assert( op != NULL );
1520         assert( op->o_conn != NULL );
1521         assert( edn != NULL );
1522         assert( access > ACL_NONE );
1523
1524         op->o_bd = select_backend( edn, 0, 0 );
1525
1526         if ( target && dn_match( &target->e_nname, edn ) ) {
1527                 e = target;
1528
1529         } else {
1530                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1531         } 
1532
1533         if ( e ) {
1534                 Attribute       *a = NULL;
1535                 int             freeattr = 0;
1536
1537                 if ( entry_at == NULL ) {
1538                         entry_at = slap_schema.si_ad_entry;
1539                 }
1540
1541                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1542                 {
1543                         if ( access_allowed_mask( op, e, entry_at,
1544                                         NULL, access, NULL, mask ) == 0 )
1545                         {
1546                                 rc = LDAP_INSUFFICIENT_ACCESS;
1547
1548                         } else {
1549                                 rc = LDAP_SUCCESS;
1550                         }
1551
1552                 } else {
1553                         a = attr_find( e->e_attrs, entry_at );
1554                         if ( a == NULL ) {
1555                                 SlapReply       rs = { 0 };
1556                                 AttributeName   anlist[ 2 ];
1557
1558                                 anlist[ 0 ].an_name = entry_at->ad_cname;
1559                                 anlist[ 0 ].an_desc = entry_at;
1560                                 BER_BVZERO( &anlist[ 1 ].an_name );
1561                                 rs.sr_attrs = anlist;
1562                         
1563                                 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1564
1565                                 /* NOTE: backend_operational() is also called
1566                                  * when returning results, so it's supposed
1567                                  * to do no harm to entries */
1568                                 rs.sr_entry = e;
1569                                 rc = backend_operational( op, &rs );
1570                                 rs.sr_entry = NULL;
1571
1572                                 if ( rc == LDAP_SUCCESS ) {
1573                                         if ( rs.sr_operational_attrs ) {
1574                                                 freeattr = 1;
1575                                                 a = rs.sr_operational_attrs;
1576
1577                                         } else {
1578                                                 rc = LDAP_NO_SUCH_OBJECT;
1579                                         }
1580                                 }
1581                         }
1582
1583                         if ( a ) {
1584                                 if ( access_allowed_mask( op, e, entry_at,
1585                                                 nval, access, NULL, mask ) == 0 )
1586                                 {
1587                                         rc = LDAP_INSUFFICIENT_ACCESS;
1588                                         goto freeit;
1589                                 }
1590                                 rc = LDAP_SUCCESS;
1591                         }
1592                 }
1593 freeit:         if ( e != target ) {
1594                         be_entry_release_r( op, e );
1595                 }
1596                 if ( freeattr ) {
1597                         attr_free( a );
1598                 }
1599         }
1600
1601         op->o_bd = be;
1602         return rc;
1603 }
1604
1605 int
1606 fe_aux_operational(
1607         Operation *op,
1608         SlapReply *rs )
1609 {
1610         Attribute               **ap;
1611         int                     rc = 0;
1612         BackendDB               *be_orig;
1613
1614         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1615                 /* just count them */ ;
1616
1617         /*
1618          * If operational attributes (allegedly) are required, 
1619          * and the backend supports specific operational attributes, 
1620          * add them to the attribute list
1621          */
1622         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1623                 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1624         {
1625                 *ap = slap_operational_entryDN( rs->sr_entry );
1626                 ap = &(*ap)->a_next;
1627         }
1628
1629         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1630                 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1631         {
1632                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1633                 ap = &(*ap)->a_next;
1634         }
1635
1636         if ( op->o_bd != NULL )
1637         {
1638                 /* Let the overlays have a chance at this */
1639                 be_orig = op->o_bd;
1640                 if ( SLAP_ISOVERLAY( be_orig ) )
1641                         op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1642
1643                 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1644                         op->o_bd && op->o_bd->be_operational != NULL )
1645                 {
1646                         rc = op->o_bd->be_operational( op, rs );
1647                 }
1648                 op->o_bd = be_orig;
1649         }
1650
1651         return rc;
1652 }
1653
1654 int backend_operational( Operation *op, SlapReply *rs )
1655 {
1656         int rc;
1657         BackendDB *be_orig;
1658
1659         /* Moved this into the frontend so global overlays are called */
1660
1661         be_orig = op->o_bd;
1662         op->o_bd = frontendDB;
1663         rc = frontendDB->be_operational( op, rs );
1664         op->o_bd = be_orig;
1665
1666         return rc;
1667 }
1668