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