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