]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Move backend_operational() implementation into frontend so global
[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 backend_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         if ( op->o_abandon ) return SLAPD_ABANDON;
1200
1201         op->o_bd = select_backend( gr_ndn, 0, 0 );
1202
1203         for ( g = op->o_groups; g; g = g->ga_next ) {
1204                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1205                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1206                 {
1207                         continue;
1208                 }
1209                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1210                         break;
1211                 }
1212         }
1213
1214         if ( g ) {
1215                 rc = g->ga_res;
1216                 goto done;
1217         }
1218
1219         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1220                 e = target;
1221                 rc = 0;
1222         } else {
1223                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1224         }
1225         if ( e ) {
1226                 a = attr_find( e->e_attrs, group_at );
1227                 if ( a ) {
1228                         /* If the attribute is a subtype of labeledURI, treat this as
1229                          * a dynamic group ala groupOfURLs
1230                          */
1231                         if (is_at_subtype( group_at->ad_type,
1232                                 slap_schema.si_ad_labeledURI->ad_type ) )
1233                         {
1234                                 int i;
1235                                 LDAPURLDesc *ludp;
1236                                 struct berval bv, nbase;
1237                                 Filter *filter;
1238                                 Entry *user;
1239                                 Backend *b2 = op->o_bd;
1240
1241                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1242                                         user = target;
1243                                 } else {
1244                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1245                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1246                                 }
1247                                 
1248                                 if ( rc == 0 ) {
1249                                         rc = LDAP_COMPARE_FALSE;
1250                                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1251                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1252                                                         LDAP_URL_SUCCESS )
1253                                                 {
1254                                                         continue;
1255                                                 }
1256                                                 BER_BVZERO( &nbase );
1257                                                 /* host part must be empty */
1258                                                 /* attrs and extensions parts must be empty */
1259                                                 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1260                                                         ludp->lud_attrs || ludp->lud_exts )
1261                                                 {
1262                                                         goto loopit;
1263                                                 }
1264                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1265                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1266                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1267                                                 {
1268                                                         goto loopit;
1269                                                 }
1270                                                 switch ( ludp->lud_scope ) {
1271                                                 case LDAP_SCOPE_BASE:
1272                                                         if ( !dn_match( &nbase, op_ndn ) ) {
1273                                                                 goto loopit;
1274                                                         }
1275                                                         break;
1276                                                 case LDAP_SCOPE_ONELEVEL:
1277                                                         dnParent( op_ndn, &bv );
1278                                                         if ( !dn_match( &nbase, &bv ) ) {
1279                                                                 goto loopit;
1280                                                         }
1281                                                         break;
1282                                                 case LDAP_SCOPE_SUBTREE:
1283                                                         if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1284                                                                 goto loopit;
1285                                                         }
1286                                                         break;
1287 #ifdef LDAP_SCOPE_SUBORDINATE
1288                                                 case LDAP_SCOPE_SUBORDINATE:
1289                                                         if ( dn_match( &nbase, op_ndn ) ||
1290                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1291                                                         {
1292                                                                 goto loopit;
1293                                                         }
1294 #endif
1295                                                 }
1296                                                 filter = str2filter_x( op, ludp->lud_filter );
1297                                                 if ( filter ) {
1298                                                         if ( test_filter( NULL, user, filter ) ==
1299                                                                 LDAP_COMPARE_TRUE )
1300                                                         {
1301                                                                 rc = 0;
1302                                                         }
1303                                                         filter_free_x( op, filter );
1304                                                 }
1305 loopit:
1306                                                 ldap_free_urldesc( ludp );
1307                                                 if ( !BER_BVISNULL( &nbase ) ) {
1308                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1309                                                 }
1310                                                 if ( rc == 0 ) break;
1311                                         }
1312                                         if ( user != target ) {
1313                                                 be_entry_release_r( op, user );
1314                                         }
1315                                 }
1316                                 op->o_bd = b2;
1317                         } else {
1318                                 rc = value_find_ex( group_at,
1319                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1320                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1321                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1322                                 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1323                                         rc = LDAP_COMPARE_FALSE;
1324                         }
1325                 } else {
1326                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1327                 }
1328                 if (e != target ) {
1329                         be_entry_release_r( op, e );
1330                 }
1331         } else {
1332                 rc = LDAP_NO_SUCH_OBJECT;
1333         }
1334
1335         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1336                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1337                         op->o_tmpmemctx );
1338                 g->ga_be = op->o_bd;
1339                 g->ga_oc = group_oc;
1340                 g->ga_at = group_at;
1341                 g->ga_res = rc;
1342                 g->ga_len = gr_ndn->bv_len;
1343                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1344                 g->ga_next = op->o_groups;
1345                 op->o_groups = g;
1346         }
1347 done:
1348         op->o_bd = be;
1349         return rc;
1350 }
1351
1352 int 
1353 backend_attribute(
1354         Operation *op,
1355         Entry   *target,
1356         struct berval   *edn,
1357         AttributeDescription *entry_at,
1358         BerVarray *vals,
1359         slap_access_t access )
1360 {
1361         Entry                   *e = NULL;
1362         Attribute               *a = NULL;
1363         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1364         AccessControlState      acl_state = ACL_STATE_INIT;
1365         Backend                 *be = op->o_bd;
1366
1367         op->o_bd = select_backend( edn, 0, 0 );
1368
1369         if ( target && dn_match( &target->e_nname, edn ) ) {
1370                 e = target;
1371
1372         } else {
1373                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1374         } 
1375
1376         if ( e ) {
1377                 a = attr_find( e->e_attrs, entry_at );
1378                 if ( a == NULL ) {
1379                         SlapReply       rs = { 0 };
1380                         AttributeName   anlist[ 2 ];
1381
1382                         anlist[ 0 ].an_name = entry_at->ad_cname;
1383                         anlist[ 0 ].an_desc = entry_at;
1384                         BER_BVZERO( &anlist[ 1 ].an_name );
1385                         rs.sr_attrs = anlist;
1386                         
1387                         /* NOTE: backend_operational() is also called
1388                          * when returning results, so it's supposed
1389                          * to do no harm to entries */
1390                         rs.sr_entry = e;
1391                         rc = backend_operational( op, &rs );
1392                         rs.sr_entry = NULL;
1393  
1394                         if ( rc == LDAP_SUCCESS ) {
1395                                 if ( rs.sr_operational_attrs ) {
1396                                         freeattr = 1;
1397                                         a = rs.sr_operational_attrs;
1398
1399                                 } else {
1400                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1401                                 }
1402                         }
1403                 }
1404
1405                 if ( a ) {
1406                         BerVarray v;
1407
1408                         if ( op->o_conn && access > ACL_NONE &&
1409                                 access_allowed( op, e, entry_at, NULL,
1410                                                 access, &acl_state ) == 0 )
1411                         {
1412                                 rc = LDAP_INSUFFICIENT_ACCESS;
1413                                 goto freeit;
1414                         }
1415
1416                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1417                                 ;
1418                         
1419                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1420                                 op->o_tmpmemctx );
1421                         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1422                         {
1423                                 if ( op->o_conn && access > ACL_NONE && 
1424                                         access_allowed( op, e, entry_at,
1425                                                         &a->a_nvals[i],
1426                                                         access,
1427                                                         &acl_state ) == 0 )
1428                                 {
1429                                         continue;
1430                                 }
1431                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1432                                                 op->o_tmpmemctx );
1433                                 if ( !BER_BVISNULL( &v[j] ) ) {
1434                                         j++;
1435                                 }
1436                         }
1437                         if ( j == 0 ) {
1438                                 op->o_tmpfree( v, op->o_tmpmemctx );
1439                                 *vals = NULL;
1440                                 rc = LDAP_INSUFFICIENT_ACCESS;
1441
1442                         } else {
1443                                 BER_BVZERO( &v[j] );
1444                                 *vals = v;
1445                                 rc = LDAP_SUCCESS;
1446                         }
1447                 }
1448 freeit:         if ( e != target ) {
1449                         be_entry_release_r( op, e );
1450                 }
1451                 if ( freeattr ) {
1452                         attr_free( a );
1453                 }
1454         }
1455
1456         op->o_bd = be;
1457         return rc;
1458 }
1459
1460 int 
1461 backend_access(
1462         Operation               *op,
1463         Entry                   *target,
1464         struct berval           *edn,
1465         AttributeDescription    *entry_at,
1466         struct berval           *nval,
1467         slap_access_t           access,
1468         slap_mask_t             *mask )
1469 {
1470         Entry           *e = NULL;
1471         int             rc = LDAP_INSUFFICIENT_ACCESS;
1472         Backend         *be = op->o_bd;
1473
1474         /* pedantic */
1475         assert( op != NULL );
1476         assert( op->o_conn != NULL );
1477         assert( edn != NULL );
1478         assert( access > ACL_NONE );
1479
1480         op->o_bd = select_backend( edn, 0, 0 );
1481
1482         if ( target && dn_match( &target->e_nname, edn ) ) {
1483                 e = target;
1484
1485         } else {
1486                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1487         } 
1488
1489         if ( e ) {
1490                 Attribute       *a = NULL;
1491                 int             freeattr = 0;
1492
1493                 if ( entry_at == NULL ) {
1494                         entry_at = slap_schema.si_ad_entry;
1495                 }
1496
1497                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1498                 {
1499                         if ( access_allowed_mask( op, e, entry_at,
1500                                         NULL, access, NULL, mask ) == 0 )
1501                         {
1502                                 rc = LDAP_INSUFFICIENT_ACCESS;
1503
1504                         } else {
1505                                 rc = LDAP_SUCCESS;
1506                         }
1507
1508                 } else {
1509                         a = attr_find( e->e_attrs, entry_at );
1510                         if ( a == NULL ) {
1511                                 SlapReply       rs = { 0 };
1512                                 AttributeName   anlist[ 2 ];
1513
1514                                 anlist[ 0 ].an_name = entry_at->ad_cname;
1515                                 anlist[ 0 ].an_desc = entry_at;
1516                                 BER_BVZERO( &anlist[ 1 ].an_name );
1517                                 rs.sr_attrs = anlist;
1518                         
1519                                 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1520
1521                                 /* NOTE: backend_operational() is also called
1522                                  * when returning results, so it's supposed
1523                                  * to do no harm to entries */
1524                                 rs.sr_entry = e;
1525                                 rc = backend_operational( op, &rs );
1526                                 rs.sr_entry = NULL;
1527
1528                                 if ( rc == LDAP_SUCCESS ) {
1529                                         if ( rs.sr_operational_attrs ) {
1530                                                 freeattr = 1;
1531                                                 a = rs.sr_operational_attrs;
1532
1533                                         } else {
1534                                                 rc = LDAP_NO_SUCH_OBJECT;
1535                                         }
1536                                 }
1537                         }
1538
1539                         if ( a ) {
1540                                 if ( access_allowed_mask( op, e, entry_at,
1541                                                 nval, access, NULL, mask ) == 0 )
1542                                 {
1543                                         rc = LDAP_INSUFFICIENT_ACCESS;
1544                                         goto freeit;
1545                                 }
1546                                 rc = LDAP_SUCCESS;
1547                         }
1548                 }
1549 freeit:         if ( e != target ) {
1550                         be_entry_release_r( op, e );
1551                 }
1552                 if ( freeattr ) {
1553                         attr_free( a );
1554                 }
1555         }
1556
1557         op->o_bd = be;
1558         return rc;
1559 }
1560
1561 int backend_operational( Operation *op, SlapReply *rs )
1562 {
1563         int rc;
1564         BackendDB *be_orig;
1565
1566         /* Moved this into the frontend so global overlays are called */
1567
1568         be_orig = op->o_bd;
1569         op->o_bd = frontendDB;
1570         rc = frontendDB->be_operational( op, rs );
1571         op->o_bd = be_orig;
1572
1573         return rc;
1574 }
1575