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