]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
6cb351feacb1f955229fa8b964c6f516eb85d14c
[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         limits_destroy( bd->be_limits );
425         if ( dynamic ) {
426                 free( bd );
427         }
428 }
429
430 int backend_destroy(void)
431 {
432         BackendDB *bd;
433         BackendInfo *bi;
434
435         /* destroy each backend database */
436         while (( bd = LDAP_STAILQ_FIRST(&backendDB))) {
437                 backend_destroy_one( bd, 1 );
438         }
439
440         /* destroy each backend type */
441         LDAP_STAILQ_FOREACH( bi, &backendInfo, bi_next ) {
442                 if( bi->bi_destroy ) {
443                         bi->bi_destroy( bi );
444                 }
445         }
446
447         nBackendInfo = 0;
448         LDAP_STAILQ_INIT(&backendInfo);
449
450         /* destroy frontend database */
451         bd = frontendDB;
452         if ( bd ) {
453                 if ( bd->bd_info->bi_db_destroy ) {
454                         bd->bd_info->bi_db_destroy( bd );
455                 }
456                 ber_bvarray_free( bd->be_suffix );
457                 ber_bvarray_free( bd->be_nsuffix );
458                 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
459                         free( bd->be_rootdn.bv_val );
460                 }
461                 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
462                         free( bd->be_rootndn.bv_val );
463                 }
464                 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
465                         free( bd->be_rootpw.bv_val );
466                 }
467                 acl_destroy( bd->be_acl, frontendDB->be_acl );
468         }
469
470         return 0;
471 }
472
473 BackendInfo* backend_info(const char *type)
474 {
475         BackendInfo *bi;
476
477         /* search for the backend type */
478         LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
479                 if( strcasecmp(bi->bi_type, type) == 0 ) {
480                         return bi;
481                 }
482         }
483
484         return NULL;
485 }
486
487
488 BackendDB *
489 backend_db_init(
490     const char  *type )
491 {
492         Backend *be;
493         BackendInfo *bi = backend_info(type);
494         int     rc = 0;
495
496         if( bi == NULL ) {
497                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
498                 return NULL;
499         }
500
501         be = ch_calloc( 1, sizeof(Backend) );
502         nbackends++;
503         LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
504
505         be->bd_info = bi;
506
507         be->be_def_limit = frontendDB->be_def_limit;
508         be->be_dfltaccess = frontendDB->be_dfltaccess;
509
510         be->be_restrictops = frontendDB->be_restrictops;
511         be->be_requires = frontendDB->be_requires;
512         be->be_ssf_set = frontendDB->be_ssf_set;
513
514         be->be_pcl_mutexp = &be->be_pcl_mutex;
515         ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
516
517         /* assign a default depth limit for alias deref */
518         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
519
520         if ( bi->bi_db_init ) {
521                 rc = bi->bi_db_init( be );
522         }
523
524         if ( rc != 0 ) {
525                 fprintf( stderr, "database init failed (%s)\n", type );
526                 nbackends--;
527                 return NULL;
528         }
529
530         bi->bi_nDB++;
531         return( be );
532 }
533
534 void
535 be_db_close( void )
536 {
537         BackendDB *be;
538
539         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
540                 if ( be->bd_info->bi_db_close ) {
541                         be->bd_info->bi_db_close( be );
542                 }
543         }
544
545         if ( frontendDB->bd_info->bi_db_close ) {
546                 (*frontendDB->bd_info->bi_db_close)( frontendDB );
547         }
548
549 }
550
551 Backend *
552 select_backend(
553         struct berval * dn,
554         int manageDSAit,
555         int noSubs )
556 {
557         int             j;
558         ber_len_t       len, dnlen = dn->bv_len;
559         Backend         *be, *b2 = NULL;
560
561         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
562                 if ( be->be_nsuffix == NULL ) {
563                         continue;
564                 }
565
566                 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
567                 {
568                         if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
569                         {
570                                 continue;
571                         }
572
573                         len = be->be_nsuffix[j].bv_len;
574
575                         if ( len > dnlen ) {
576                                 /* suffix is longer than DN */
577                                 continue;
578                         }
579                         
580                         /*
581                          * input DN is normalized, so the separator check
582                          * need not look at escaping
583                          */
584                         if ( len && len < dnlen &&
585                                 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
586                         {
587                                 continue;
588                         }
589
590                         if ( strcmp( be->be_nsuffix[j].bv_val,
591                                 &dn->bv_val[dnlen-len] ) == 0 )
592                         {
593                                 if( b2 == NULL ) {
594                                         b2 = be;
595
596                                         if( manageDSAit && len == dnlen &&
597                                                 !SLAP_GLUE_SUBORDINATE( be ) ) {
598                                                 continue;
599                                         }
600                                 } else {
601                                         b2 = be;
602                                 }
603                                 return b2;
604                         }
605                 }
606         }
607
608         return b2;
609 }
610
611 int
612 be_issuffix(
613     Backend *be,
614     struct berval *bvsuffix )
615 {
616         int     i;
617
618         if ( be->be_nsuffix == NULL ) {
619                 return 0;
620         }
621
622         for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
623                 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
624                         return 1;
625                 }
626         }
627
628         return 0;
629 }
630
631 int
632 be_isroot_dn( Backend *be, struct berval *ndn )
633 {
634         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
635                 return 0;
636         }
637
638         return dn_match( &be->be_rootndn, ndn );
639 }
640
641 int
642 be_slurp_update( Operation *op )
643 {
644         return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
645                 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
646 }
647
648 int
649 be_shadow_update( Operation *op )
650 {
651         return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
652                 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
653 }
654
655 int
656 be_isupdate_dn( Backend *be, struct berval *ndn )
657 {
658         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
659                 return 0;
660         }
661
662         return dn_match( &be->be_update_ndn, ndn );
663 }
664
665 struct berval *
666 be_root_dn( Backend *be )
667 {
668         return &be->be_rootdn;
669 }
670
671 int
672 be_isroot( Operation *op )
673 {
674         return be_isroot_dn( op->o_bd, &op->o_ndn );
675 }
676
677 int
678 be_isroot_pw( Operation *op )
679 {
680         int result;
681
682         if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
683                 return 0;
684         }
685
686         if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
687                 return 0;
688         }
689
690 #ifdef SLAPD_SPASSWD
691         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
692                 op->o_conn->c_sasl_authctx, NULL );
693 #endif
694
695         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
696
697 #ifdef SLAPD_SPASSWD
698         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
699 #endif
700
701         return result == 0;
702 }
703
704 int
705 be_entry_release_rw(
706         Operation *op,
707         Entry *e,
708         int rw )
709 {
710         if ( op->o_bd->be_release ) {
711                 /* free and release entry from backend */
712                 return op->o_bd->be_release( op, e, rw );
713         } else {
714                 /* free entry */
715                 entry_free( e );
716                 return 0;
717         }
718 }
719
720 int
721 backend_unbind( Operation *op, SlapReply *rs )
722 {
723         BackendDB *be;
724
725         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
726                 if ( be->be_unbind ) {
727                         op->o_bd = be;
728                         be->be_unbind( op, rs );
729                 }
730         }
731
732         return 0;
733 }
734
735 int
736 backend_connection_init(
737         Connection   *conn )
738 {
739         BackendDB *be;
740
741         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
742                 if ( be->be_connection_init ) {
743                         be->be_connection_init( be, conn );
744                 }
745         }
746
747         return 0;
748 }
749
750 int
751 backend_connection_destroy(
752         Connection   *conn )
753 {
754         BackendDB *be;
755
756         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
757                 if ( be->be_connection_destroy ) {
758                         be->be_connection_destroy( be, conn);
759                 }
760         }
761
762         return 0;
763 }
764
765 int
766 backend_check_controls(
767         Operation *op,
768         SlapReply *rs )
769 {
770         LDAPControl **ctrls = op->o_ctrls;
771         rs->sr_err = LDAP_SUCCESS;
772
773         if( ctrls ) {
774                 for( ; *ctrls != NULL ; ctrls++ ) {
775                         int cid;
776
777                         switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
778                         case LDAP_CONTROL_NOT_FOUND:
779                                 /* unrecognized control */ 
780                                 if ( (*ctrls)->ldctl_iscritical ) {
781                                         /* should not be reachable */ 
782                                         Debug( LDAP_DEBUG_ANY,
783                                                 "backend_check_controls: unrecognized control: %s\n",
784                                                 (*ctrls)->ldctl_oid, 0, 0 );
785                                         assert( 0 );
786                                 }
787                                 break;
788
789                         case LDAP_COMPARE_FALSE:
790                                 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
791                                         /* Per RFC 2251 (and LDAPBIS discussions), if the control
792                                          * is recognized and appropriate for the operation (which
793                                          * we've already verified), then the server should make
794                                          * use of the control when performing the operation.
795                                          * 
796                                          * Here we find that operation extended by the control
797                                          * is unavailable in a particular context, and the control
798                                          * is marked Critical, hence the return of
799                                          * unwillingToPerform.
800                                          */
801                                         rs->sr_text = "critical control unavailable in context";
802                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
803                                         goto done;
804                                 }
805                                 break;
806
807                         case LDAP_COMPARE_TRUE:
808                                 break;
809
810                         default:
811                                 /* unreachable */
812                                 Debug( LDAP_DEBUG_ANY,
813                                         "backend_check_controls: unable to check control: %s\n",
814                                         (*ctrls)->ldctl_oid, 0, 0 );
815                                 assert( 0 );
816
817                                 rs->sr_text = "unable to check control";
818                                 rs->sr_err = LDAP_OTHER;
819                                 goto done;
820                         }
821                 }
822         }
823
824         /* check should be generalized */
825         if( get_manageDIT(op) && !be_isroot(op)) {
826                 rs->sr_text = "requires manager authorization";
827                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
828         }
829
830 done:;
831         return rs->sr_err;
832 }
833
834 int
835 backend_check_restrictions(
836         Operation *op,
837         SlapReply *rs,
838         struct berval *opdata )
839 {
840         slap_mask_t restrictops;
841         slap_mask_t requires;
842         slap_mask_t opflag;
843         slap_mask_t exopflag = 0;
844         slap_ssf_set_t *ssf;
845         int updateop = 0;
846         int starttls = 0;
847         int session = 0;
848
849         if ( op->o_bd ) {
850                 int     rc = SLAP_CB_CONTINUE;
851
852                 if ( op->o_bd->be_chk_controls ) {
853                         rc = ( *op->o_bd->be_chk_controls )( op, rs );
854                 }
855
856                 if ( rc == SLAP_CB_CONTINUE ) {
857                         rc = backend_check_controls( op, rs );
858                 }
859
860                 if ( rc != LDAP_SUCCESS ) {
861                         return rs->sr_err;
862                 }
863
864                 restrictops = op->o_bd->be_restrictops;
865                 requires = op->o_bd->be_requires;
866                 ssf = &op->o_bd->be_ssf_set;
867
868         } else {
869                 restrictops = frontendDB->be_restrictops;
870                 requires = frontendDB->be_requires;
871                 ssf = &frontendDB->be_ssf_set;
872         }
873
874         switch( op->o_tag ) {
875         case LDAP_REQ_ADD:
876                 opflag = SLAP_RESTRICT_OP_ADD;
877                 updateop++;
878                 break;
879         case LDAP_REQ_BIND:
880                 opflag = SLAP_RESTRICT_OP_BIND;
881                 session++;
882                 break;
883         case LDAP_REQ_COMPARE:
884                 opflag = SLAP_RESTRICT_OP_COMPARE;
885                 break;
886         case LDAP_REQ_DELETE:
887                 updateop++;
888                 opflag = SLAP_RESTRICT_OP_DELETE;
889                 break;
890         case LDAP_REQ_EXTENDED:
891                 opflag = SLAP_RESTRICT_OP_EXTENDED;
892
893                 if( !opdata ) {
894                         /* treat unspecified as a modify */
895                         opflag = SLAP_RESTRICT_OP_MODIFY;
896                         updateop++;
897                         break;
898                 }
899
900                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
901                         session++;
902                         starttls++;
903                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
904                         break;
905                 }
906
907                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
908                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
909                         break;
910                 }
911
912                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
913                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
914                         break;
915                 }
916
917                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
918                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
919                         updateop++;
920                         break;
921                 }
922
923                 /* treat everything else as a modify */
924                 opflag = SLAP_RESTRICT_OP_MODIFY;
925                 updateop++;
926                 break;
927
928         case LDAP_REQ_MODIFY:
929                 updateop++;
930                 opflag = SLAP_RESTRICT_OP_MODIFY;
931                 break;
932         case LDAP_REQ_RENAME:
933                 updateop++;
934                 opflag = SLAP_RESTRICT_OP_RENAME;
935                 break;
936         case LDAP_REQ_SEARCH:
937                 opflag = SLAP_RESTRICT_OP_SEARCH;
938                 break;
939         case LDAP_REQ_UNBIND:
940                 session++;
941                 opflag = 0;
942                 break;
943         default:
944                 rs->sr_text = "restrict operations internal error";
945                 rs->sr_err = LDAP_OTHER;
946                 return rs->sr_err;
947         }
948
949         if ( !starttls ) {
950                 /* these checks don't apply to StartTLS */
951
952                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
953                 if( op->o_transport_ssf < ssf->sss_transport ) {
954                         rs->sr_text = op->o_transport_ssf
955                                 ? "stronger transport confidentiality required"
956                                 : "transport confidentiality required";
957                         return rs->sr_err;
958                 }
959
960                 if( op->o_tls_ssf < ssf->sss_tls ) {
961                         rs->sr_text = op->o_tls_ssf
962                                 ? "stronger TLS confidentiality required"
963                                 : "TLS confidentiality required";
964                         return rs->sr_err;
965                 }
966
967
968                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
969                         /* simple bind specific check */
970                         if( op->o_ssf < ssf->sss_simple_bind ) {
971                                 rs->sr_text = op->o_ssf
972                                         ? "stronger confidentiality required"
973                                         : "confidentiality required";
974                                 return rs->sr_err;
975                         }
976                 }
977
978                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
979                         /* these checks don't apply to SASL bind */
980
981                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
982                                 rs->sr_text = op->o_sasl_ssf
983                                         ? "stronger SASL confidentiality required"
984                                         : "SASL confidentiality required";
985                                 return rs->sr_err;
986                         }
987
988                         if( op->o_ssf < ssf->sss_ssf ) {
989                                 rs->sr_text = op->o_ssf
990                                         ? "stronger confidentiality required"
991                                         : "confidentiality required";
992                                 return rs->sr_err;
993                         }
994                 }
995
996                 if( updateop ) {
997                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
998                                 rs->sr_text = op->o_transport_ssf
999                                         ? "stronger transport confidentiality required for update"
1000                                         : "transport confidentiality required for update";
1001                                 return rs->sr_err;
1002                         }
1003
1004                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
1005                                 rs->sr_text = op->o_tls_ssf
1006                                         ? "stronger TLS confidentiality required for update"
1007                                         : "TLS confidentiality required for update";
1008                                 return rs->sr_err;
1009                         }
1010
1011                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1012                                 rs->sr_text = op->o_sasl_ssf
1013                                         ? "stronger SASL confidentiality required for update"
1014                                         : "SASL confidentiality required for update";
1015                                 return rs->sr_err;
1016                         }
1017
1018                         if( op->o_ssf < ssf->sss_update_ssf ) {
1019                                 rs->sr_text = op->o_ssf
1020                                         ? "stronger confidentiality required for update"
1021                                         : "confidentiality required for update";
1022                                 return rs->sr_err;
1023                         }
1024
1025                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1026                                 BER_BVISEMPTY( &op->o_ndn ) )
1027                         {
1028                                 rs->sr_text = "modifications require authentication";
1029                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1030                                 return rs->sr_err;
1031                         }
1032
1033 #ifdef SLAP_X_LISTENER_MOD
1034                         if ( op->o_conn->c_listener &&
1035                                 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1036                                         ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1037                         {
1038                                 /* no "w" mode means readonly */
1039                                 rs->sr_text = "modifications not allowed on this listener";
1040                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1041                                 return rs->sr_err;
1042                         }
1043 #endif /* SLAP_X_LISTENER_MOD */
1044                 }
1045         }
1046
1047         if ( !session ) {
1048                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1049
1050                 if( requires & SLAP_REQUIRE_STRONG ) {
1051                         /* should check mechanism */
1052                         if( ( op->o_transport_ssf < ssf->sss_transport
1053                                 && op->o_authtype == LDAP_AUTH_SIMPLE )
1054                                 || BER_BVISEMPTY( &op->o_dn ) )
1055                         {
1056                                 rs->sr_text = "strong(er) authentication required";
1057                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1058                                 return rs->sr_err;
1059                         }
1060                 }
1061
1062                 if( requires & SLAP_REQUIRE_SASL ) {
1063                         if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1064                                 rs->sr_text = "SASL authentication required";
1065                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1066                                 return rs->sr_err;
1067                         }
1068                 }
1069                         
1070                 if( requires & SLAP_REQUIRE_AUTHC ) {
1071                         if( BER_BVISEMPTY( &op->o_dn ) ) {
1072                                 rs->sr_text = "authentication required";
1073                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1074                                 return rs->sr_err;
1075                         }
1076                 }
1077
1078                 if( requires & SLAP_REQUIRE_BIND ) {
1079                         int version;
1080                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1081                         version = op->o_conn->c_protocol;
1082                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1083
1084                         if( !version ) {
1085                                 /* no bind has occurred */
1086                                 rs->sr_text = "BIND required";
1087                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1088                                 return rs->sr_err;
1089                         }
1090                 }
1091
1092                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1093                         if( op->o_protocol < LDAP_VERSION3 ) {
1094                                 /* no bind has occurred */
1095                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1096                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1097                                 return rs->sr_err;
1098                         }
1099                 }
1100
1101 #ifdef SLAP_X_LISTENER_MOD
1102                 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1103                         if ( op->o_conn->c_listener &&
1104                                 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1105                 {
1106                                 /* no "x" mode means bind required */
1107                                 rs->sr_text = "bind required on this listener";
1108                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1109                                 return rs->sr_err;
1110                         }
1111                 }
1112
1113                 if ( !starttls && !updateop ) {
1114                         if ( op->o_conn->c_listener &&
1115                                 !( op->o_conn->c_listener->sl_perms &
1116                                         ( !BER_BVISEMPTY( &op->o_dn )
1117                                                 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1118                         {
1119                                 /* no "r" mode means no read */
1120                                 rs->sr_text = "read not allowed on this listener";
1121                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1122                                 return rs->sr_err;
1123                         }
1124                 }
1125 #endif /* SLAP_X_LISTENER_MOD */
1126
1127         }
1128
1129         if( ( restrictops & opflag )
1130                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1131                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1132                         rs->sr_text = "read operations restricted";
1133                 } else if ( restrictops & exopflag ) {
1134                         rs->sr_text = "extended operation restricted";
1135                 } else {
1136                         rs->sr_text = "operation restricted";
1137                 }
1138                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1139                 return rs->sr_err;
1140         }
1141
1142         rs->sr_err = LDAP_SUCCESS;
1143         return rs->sr_err;
1144 }
1145
1146 int backend_check_referrals( Operation *op, SlapReply *rs )
1147 {
1148         rs->sr_err = LDAP_SUCCESS;
1149
1150         if( op->o_bd->be_chk_referrals ) {
1151                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1152
1153                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1154                         send_ldap_result( op, rs );
1155                 }
1156         }
1157
1158         return rs->sr_err;
1159 }
1160
1161 int
1162 be_entry_get_rw(
1163         Operation *op,
1164         struct berval *ndn,
1165         ObjectClass *oc,
1166         AttributeDescription *at,
1167         int rw,
1168         Entry **e )
1169 {
1170         int rc;
1171
1172         *e = NULL;
1173
1174         if (op->o_bd == NULL) {
1175                 rc = LDAP_NO_SUCH_OBJECT;
1176         } else if ( op->o_bd->be_fetch ) {
1177                 rc = ( op->o_bd->be_fetch )( op, ndn,
1178                         oc, at, rw, e );
1179         } else {
1180                 rc = LDAP_UNWILLING_TO_PERFORM;
1181         }
1182         return rc;
1183 }
1184
1185 int 
1186 fe_acl_group(
1187         Operation *op,
1188         Entry   *target,
1189         struct berval *gr_ndn,
1190         struct berval *op_ndn,
1191         ObjectClass *group_oc,
1192         AttributeDescription *group_at )
1193 {
1194         Entry *e;
1195         Attribute *a;
1196         int rc;
1197         GroupAssertion *g;
1198         Backend *be = op->o_bd;
1199
1200         op->o_bd = select_backend( gr_ndn, 0, 0 );
1201
1202         for ( g = op->o_groups; g; g = g->ga_next ) {
1203                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1204                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1205                 {
1206                         continue;
1207                 }
1208                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1209                         break;
1210                 }
1211         }
1212
1213         if ( g ) {
1214                 rc = g->ga_res;
1215                 goto done;
1216         }
1217
1218         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1219                 e = target;
1220                 rc = 0;
1221         } else {
1222                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1223         }
1224         if ( e ) {
1225                 a = attr_find( e->e_attrs, group_at );
1226                 if ( a ) {
1227                         /* If the attribute is a subtype of labeledURI, treat this as
1228                          * a dynamic group ala groupOfURLs
1229                          */
1230                         if (is_at_subtype( group_at->ad_type,
1231                                 slap_schema.si_ad_labeledURI->ad_type ) )
1232                         {
1233                                 int i;
1234                                 LDAPURLDesc *ludp;
1235                                 struct berval bv, nbase;
1236                                 Filter *filter;
1237                                 Entry *user;
1238                                 Backend *b2 = op->o_bd;
1239
1240                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1241                                         user = target;
1242                                 } else {
1243                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1244                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1245                                 }
1246                                 
1247                                 if ( rc == 0 ) {
1248                                         rc = LDAP_COMPARE_FALSE;
1249                                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1250                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1251                                                         LDAP_URL_SUCCESS )
1252                                                 {
1253                                                         continue;
1254                                                 }
1255                                                 BER_BVZERO( &nbase );
1256                                                 /* host part must be empty */
1257                                                 /* attrs and extensions parts must be empty */
1258                                                 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1259                                                         ludp->lud_attrs || ludp->lud_exts )
1260                                                 {
1261                                                         goto loopit;
1262                                                 }
1263                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1264                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1265                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1266                                                 {
1267                                                         goto loopit;
1268                                                 }
1269                                                 switch ( ludp->lud_scope ) {
1270                                                 case LDAP_SCOPE_BASE:
1271                                                         if ( !dn_match( &nbase, op_ndn ) ) {
1272                                                                 goto loopit;
1273                                                         }
1274                                                         break;
1275                                                 case LDAP_SCOPE_ONELEVEL:
1276                                                         dnParent( op_ndn, &bv );
1277                                                         if ( !dn_match( &nbase, &bv ) ) {
1278                                                                 goto loopit;
1279                                                         }
1280                                                         break;
1281                                                 case LDAP_SCOPE_SUBTREE:
1282                                                         if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1283                                                                 goto loopit;
1284                                                         }
1285                                                         break;
1286 #ifdef LDAP_SCOPE_SUBORDINATE
1287                                                 case LDAP_SCOPE_SUBORDINATE:
1288                                                         if ( dn_match( &nbase, op_ndn ) ||
1289                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1290                                                         {
1291                                                                 goto loopit;
1292                                                         }
1293 #endif
1294                                                 }
1295                                                 filter = str2filter_x( op, ludp->lud_filter );
1296                                                 if ( filter ) {
1297                                                         if ( test_filter( NULL, user, filter ) ==
1298                                                                 LDAP_COMPARE_TRUE )
1299                                                         {
1300                                                                 rc = 0;
1301                                                         }
1302                                                         filter_free_x( op, filter );
1303                                                 }
1304 loopit:
1305                                                 ldap_free_urldesc( ludp );
1306                                                 if ( !BER_BVISNULL( &nbase ) ) {
1307                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1308                                                 }
1309                                                 if ( rc == 0 ) break;
1310                                         }
1311                                         if ( user != target ) {
1312                                                 be_entry_release_r( op, user );
1313                                         }
1314                                 }
1315                                 op->o_bd = b2;
1316                         } else {
1317                                 rc = value_find_ex( group_at,
1318                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1319                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1320                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1321                                 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1322                                         rc = LDAP_COMPARE_FALSE;
1323                         }
1324                 } else {
1325                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1326                 }
1327                 if (e != target ) {
1328                         be_entry_release_r( op, e );
1329                 }
1330         } else {
1331                 rc = LDAP_NO_SUCH_OBJECT;
1332         }
1333
1334         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1335                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1336                         op->o_tmpmemctx );
1337                 g->ga_be = op->o_bd;
1338                 g->ga_oc = group_oc;
1339                 g->ga_at = group_at;
1340                 g->ga_res = rc;
1341                 g->ga_len = gr_ndn->bv_len;
1342                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1343                 g->ga_next = op->o_groups;
1344                 op->o_groups = g;
1345         }
1346 done:
1347         op->o_bd = be;
1348         return rc;
1349 }
1350
1351 int 
1352 backend_group(
1353         Operation *op,
1354         Entry   *target,
1355         struct berval *gr_ndn,
1356         struct berval *op_ndn,
1357         ObjectClass *group_oc,
1358         AttributeDescription *group_at )
1359 {
1360         int                     rc;
1361         BackendDB               *be_orig;
1362
1363         if ( op->o_abandon ) {
1364                 return SLAPD_ABANDON;
1365         }
1366
1367         be_orig = op->o_bd;
1368         op->o_bd = frontendDB;
1369         rc = frontendDB->be_group( op, target, gr_ndn,
1370                 op_ndn, group_oc, group_at );
1371         op->o_bd = be_orig;
1372
1373         return rc;
1374 }
1375
1376 int 
1377 fe_acl_attribute(
1378         Operation *op,
1379         Entry   *target,
1380         struct berval   *edn,
1381         AttributeDescription *entry_at,
1382         BerVarray *vals,
1383         slap_access_t access )
1384 {
1385         Entry                   *e = NULL;
1386         Attribute               *a = NULL;
1387         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1388         AccessControlState      acl_state = ACL_STATE_INIT;
1389         Backend                 *be = op->o_bd;
1390
1391         op->o_bd = select_backend( edn, 0, 0 );
1392
1393         if ( target && dn_match( &target->e_nname, edn ) ) {
1394                 e = target;
1395
1396         } else {
1397                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1398         } 
1399
1400         if ( e ) {
1401                 a = attr_find( e->e_attrs, entry_at );
1402                 if ( a == NULL ) {
1403                         SlapReply       rs = { 0 };
1404                         AttributeName   anlist[ 2 ];
1405
1406                         anlist[ 0 ].an_name = entry_at->ad_cname;
1407                         anlist[ 0 ].an_desc = entry_at;
1408                         BER_BVZERO( &anlist[ 1 ].an_name );
1409                         rs.sr_attrs = anlist;
1410                         
1411                         /* NOTE: backend_operational() is also called
1412                          * when returning results, so it's supposed
1413                          * to do no harm to entries */
1414                         rs.sr_entry = e;
1415                         rc = backend_operational( op, &rs );
1416                         rs.sr_entry = NULL;
1417  
1418                         if ( rc == LDAP_SUCCESS ) {
1419                                 if ( rs.sr_operational_attrs ) {
1420                                         freeattr = 1;
1421                                         a = rs.sr_operational_attrs;
1422
1423                                 } else {
1424                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1425                                 }
1426                         }
1427                 }
1428
1429                 if ( a ) {
1430                         BerVarray v;
1431
1432                         if ( op->o_conn && access > ACL_NONE &&
1433                                 access_allowed( op, e, entry_at, NULL,
1434                                                 access, &acl_state ) == 0 )
1435                         {
1436                                 rc = LDAP_INSUFFICIENT_ACCESS;
1437                                 goto freeit;
1438                         }
1439
1440                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1441                                 ;
1442                         
1443                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1444                                 op->o_tmpmemctx );
1445                         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1446                         {
1447                                 if ( op->o_conn && access > ACL_NONE && 
1448                                         access_allowed( op, e, entry_at,
1449                                                         &a->a_nvals[i],
1450                                                         access,
1451                                                         &acl_state ) == 0 )
1452                                 {
1453                                         continue;
1454                                 }
1455                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1456                                                 op->o_tmpmemctx );
1457                                 if ( !BER_BVISNULL( &v[j] ) ) {
1458                                         j++;
1459                                 }
1460                         }
1461                         if ( j == 0 ) {
1462                                 op->o_tmpfree( v, op->o_tmpmemctx );
1463                                 *vals = NULL;
1464                                 rc = LDAP_INSUFFICIENT_ACCESS;
1465
1466                         } else {
1467                                 BER_BVZERO( &v[j] );
1468                                 *vals = v;
1469                                 rc = LDAP_SUCCESS;
1470                         }
1471                 }
1472 freeit:         if ( e != target ) {
1473                         be_entry_release_r( op, e );
1474                 }
1475                 if ( freeattr ) {
1476                         attr_free( a );
1477                 }
1478         }
1479
1480         op->o_bd = be;
1481         return rc;
1482 }
1483
1484 int 
1485 backend_attribute(
1486         Operation *op,
1487         Entry   *target,
1488         struct berval   *edn,
1489         AttributeDescription *entry_at,
1490         BerVarray *vals,
1491         slap_access_t access )
1492 {
1493         int                     rc;
1494         BackendDB               *be_orig;
1495
1496         be_orig = op->o_bd;
1497         op->o_bd = frontendDB;
1498         rc = frontendDB->be_attribute( op, target, edn,
1499                 entry_at, vals, access );
1500         op->o_bd = be_orig;
1501
1502         return rc;
1503 }
1504
1505 int 
1506 backend_access(
1507         Operation               *op,
1508         Entry                   *target,
1509         struct berval           *edn,
1510         AttributeDescription    *entry_at,
1511         struct berval           *nval,
1512         slap_access_t           access,
1513         slap_mask_t             *mask )
1514 {
1515         Entry           *e = NULL;
1516         int             rc = LDAP_INSUFFICIENT_ACCESS;
1517         Backend         *be = op->o_bd;
1518
1519         /* pedantic */
1520         assert( op != NULL );
1521         assert( op->o_conn != NULL );
1522         assert( edn != NULL );
1523         assert( access > ACL_NONE );
1524
1525         op->o_bd = select_backend( edn, 0, 0 );
1526
1527         if ( target && dn_match( &target->e_nname, edn ) ) {
1528                 e = target;
1529
1530         } else {
1531                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1532         } 
1533
1534         if ( e ) {
1535                 Attribute       *a = NULL;
1536                 int             freeattr = 0;
1537
1538                 if ( entry_at == NULL ) {
1539                         entry_at = slap_schema.si_ad_entry;
1540                 }
1541
1542                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1543                 {
1544                         if ( access_allowed_mask( op, e, entry_at,
1545                                         NULL, access, NULL, mask ) == 0 )
1546                         {
1547                                 rc = LDAP_INSUFFICIENT_ACCESS;
1548
1549                         } else {
1550                                 rc = LDAP_SUCCESS;
1551                         }
1552
1553                 } else {
1554                         a = attr_find( e->e_attrs, entry_at );
1555                         if ( a == NULL ) {
1556                                 SlapReply       rs = { 0 };
1557                                 AttributeName   anlist[ 2 ];
1558
1559                                 anlist[ 0 ].an_name = entry_at->ad_cname;
1560                                 anlist[ 0 ].an_desc = entry_at;
1561                                 BER_BVZERO( &anlist[ 1 ].an_name );
1562                                 rs.sr_attrs = anlist;
1563                         
1564                                 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1565
1566                                 /* NOTE: backend_operational() is also called
1567                                  * when returning results, so it's supposed
1568                                  * to do no harm to entries */
1569                                 rs.sr_entry = e;
1570                                 rc = backend_operational( op, &rs );
1571                                 rs.sr_entry = NULL;
1572
1573                                 if ( rc == LDAP_SUCCESS ) {
1574                                         if ( rs.sr_operational_attrs ) {
1575                                                 freeattr = 1;
1576                                                 a = rs.sr_operational_attrs;
1577
1578                                         } else {
1579                                                 rc = LDAP_NO_SUCH_OBJECT;
1580                                         }
1581                                 }
1582                         }
1583
1584                         if ( a ) {
1585                                 if ( access_allowed_mask( op, e, entry_at,
1586                                                 nval, access, NULL, mask ) == 0 )
1587                                 {
1588                                         rc = LDAP_INSUFFICIENT_ACCESS;
1589                                         goto freeit;
1590                                 }
1591                                 rc = LDAP_SUCCESS;
1592                         }
1593                 }
1594 freeit:         if ( e != target ) {
1595                         be_entry_release_r( op, e );
1596                 }
1597                 if ( freeattr ) {
1598                         attr_free( a );
1599                 }
1600         }
1601
1602         op->o_bd = be;
1603         return rc;
1604 }
1605
1606 int
1607 fe_aux_operational(
1608         Operation *op,
1609         SlapReply *rs )
1610 {
1611         Attribute               **ap;
1612         int                     rc = 0;
1613         BackendDB               *be_orig;
1614
1615         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1616                 /* just count them */ ;
1617
1618         /*
1619          * If operational attributes (allegedly) are required, 
1620          * and the backend supports specific operational attributes, 
1621          * add them to the attribute list
1622          */
1623         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1624                 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1625         {
1626                 *ap = slap_operational_entryDN( rs->sr_entry );
1627                 ap = &(*ap)->a_next;
1628         }
1629
1630         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1631                 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1632         {
1633                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1634                 ap = &(*ap)->a_next;
1635         }
1636
1637         if ( op->o_bd != NULL && op->o_bd != frontendDB )
1638         {
1639                 /* Let the overlays have a chance at this */
1640                 be_orig = op->o_bd;
1641                 op->o_bd = select_backend( &op->o_ndn, 0, 0 );
1642
1643                 if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1644                         op->o_bd != NULL && 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