]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
wake listener when scheduling qtask so it can run immediately
[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,
822                                                 "backend_check_controls: unrecognized control: %s\n",
823                                                 (*ctrls)->ldctl_oid, 0, 0 );
824                                         assert( 0 );
825                                 }
826                                 break;
827
828                         case LDAP_COMPARE_FALSE:
829                                 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
830                                         /* Per RFC 2251 (and LDAPBIS discussions), if the control
831                                          * is recognized and appropriate for the operation (which
832                                          * we've already verified), then the server should make
833                                          * use of the control when performing the operation.
834                                          * 
835                                          * Here we find that operation extended by the control
836                                          * is unavailable in a particular context, and the control
837                                          * is marked Critical, hence the return of
838                                          * unwillingToPerform.
839                                          */
840                                         rs->sr_text = "critical control unavailable in context";
841                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
842                                         goto done;
843                                 }
844                                 break;
845
846                         case LDAP_COMPARE_TRUE:
847                                 break;
848
849                         default:
850                                 /* unreachable */
851                                 Debug( LDAP_DEBUG_ANY,
852                                         "backend_check_controls: unable to check control: %s\n",
853                                         (*ctrls)->ldctl_oid, 0, 0 );
854                                 assert( 0 );
855
856                                 rs->sr_text = "unable to check control";
857                                 rs->sr_err = LDAP_OTHER;
858                                 goto done;
859                         }
860                 }
861         }
862
863         /* temporarily removed */
864 #if 0
865         /* check should be generalized */
866         if( get_manageDIT(op) && !be_isroot(op)) {
867                 rs->sr_text = "requires manager authorization";
868                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
869         }
870 #endif
871
872 done:;
873         return rs->sr_err;
874 }
875
876 int
877 backend_check_restrictions(
878         Operation *op,
879         SlapReply *rs,
880         struct berval *opdata )
881 {
882         slap_mask_t restrictops;
883         slap_mask_t requires;
884         slap_mask_t opflag;
885         slap_mask_t exopflag = 0;
886         slap_ssf_set_t *ssf;
887         int updateop = 0;
888         int starttls = 0;
889         int session = 0;
890
891         if ( op->o_bd ) {
892                 int     rc = SLAP_CB_CONTINUE;
893
894                 if ( op->o_bd->be_chk_controls ) {
895                         rc = ( *op->o_bd->be_chk_controls )( op, rs );
896                 }
897
898                 if ( rc == SLAP_CB_CONTINUE ) {
899                         rc = backend_check_controls( op, rs );
900                 }
901
902                 if ( rc != LDAP_SUCCESS ) {
903                         return rs->sr_err;
904                 }
905
906                 restrictops = op->o_bd->be_restrictops;
907                 requires = op->o_bd->be_requires;
908                 ssf = &op->o_bd->be_ssf_set;
909
910         } else {
911                 restrictops = frontendDB->be_restrictops;
912                 requires = frontendDB->be_requires;
913                 ssf = &frontendDB->be_ssf_set;
914         }
915
916         switch( op->o_tag ) {
917         case LDAP_REQ_ADD:
918                 opflag = SLAP_RESTRICT_OP_ADD;
919                 updateop++;
920                 break;
921         case LDAP_REQ_BIND:
922                 opflag = SLAP_RESTRICT_OP_BIND;
923                 session++;
924                 break;
925         case LDAP_REQ_COMPARE:
926                 opflag = SLAP_RESTRICT_OP_COMPARE;
927                 break;
928         case LDAP_REQ_DELETE:
929                 updateop++;
930                 opflag = SLAP_RESTRICT_OP_DELETE;
931                 break;
932         case LDAP_REQ_EXTENDED:
933                 opflag = SLAP_RESTRICT_OP_EXTENDED;
934
935                 if( !opdata ) {
936                         /* treat unspecified as a modify */
937                         opflag = SLAP_RESTRICT_OP_MODIFY;
938                         updateop++;
939                         break;
940                 }
941
942                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
943                         session++;
944                         starttls++;
945                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
946                         break;
947                 }
948
949                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
950                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
951                         break;
952                 }
953
954                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
955                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
956                         break;
957                 }
958
959                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
960                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
961                         updateop++;
962                         break;
963                 }
964
965                 /* treat everything else as a modify */
966                 opflag = SLAP_RESTRICT_OP_MODIFY;
967                 updateop++;
968                 break;
969
970         case LDAP_REQ_MODIFY:
971                 updateop++;
972                 opflag = SLAP_RESTRICT_OP_MODIFY;
973                 break;
974         case LDAP_REQ_RENAME:
975                 updateop++;
976                 opflag = SLAP_RESTRICT_OP_RENAME;
977                 break;
978         case LDAP_REQ_SEARCH:
979                 opflag = SLAP_RESTRICT_OP_SEARCH;
980                 break;
981         case LDAP_REQ_UNBIND:
982                 session++;
983                 opflag = 0;
984                 break;
985         default:
986                 rs->sr_text = "restrict operations internal error";
987                 rs->sr_err = LDAP_OTHER;
988                 return rs->sr_err;
989         }
990
991         if ( !starttls ) {
992                 /* these checks don't apply to StartTLS */
993
994                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
995                 if( op->o_transport_ssf < ssf->sss_transport ) {
996                         rs->sr_text = op->o_transport_ssf
997                                 ? "stronger transport confidentiality required"
998                                 : "transport confidentiality required";
999                         return rs->sr_err;
1000                 }
1001
1002                 if( op->o_tls_ssf < ssf->sss_tls ) {
1003                         rs->sr_text = op->o_tls_ssf
1004                                 ? "stronger TLS confidentiality required"
1005                                 : "TLS confidentiality required";
1006                         return rs->sr_err;
1007                 }
1008
1009
1010                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1011                         /* simple bind specific check */
1012                         if( op->o_ssf < ssf->sss_simple_bind ) {
1013                                 rs->sr_text = op->o_ssf
1014                                         ? "stronger confidentiality required"
1015                                         : "confidentiality required";
1016                                 return rs->sr_err;
1017                         }
1018                 }
1019
1020                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1021                         /* these checks don't apply to SASL bind */
1022
1023                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
1024                                 rs->sr_text = op->o_sasl_ssf
1025                                         ? "stronger SASL confidentiality required"
1026                                         : "SASL confidentiality required";
1027                                 return rs->sr_err;
1028                         }
1029
1030                         if( op->o_ssf < ssf->sss_ssf ) {
1031                                 rs->sr_text = op->o_ssf
1032                                         ? "stronger confidentiality required"
1033                                         : "confidentiality required";
1034                                 return rs->sr_err;
1035                         }
1036                 }
1037
1038                 if( updateop ) {
1039                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
1040                                 rs->sr_text = op->o_transport_ssf
1041                                         ? "stronger transport confidentiality required for update"
1042                                         : "transport confidentiality required for update";
1043                                 return rs->sr_err;
1044                         }
1045
1046                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
1047                                 rs->sr_text = op->o_tls_ssf
1048                                         ? "stronger TLS confidentiality required for update"
1049                                         : "TLS confidentiality required for update";
1050                                 return rs->sr_err;
1051                         }
1052
1053                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1054                                 rs->sr_text = op->o_sasl_ssf
1055                                         ? "stronger SASL confidentiality required for update"
1056                                         : "SASL confidentiality required for update";
1057                                 return rs->sr_err;
1058                         }
1059
1060                         if( op->o_ssf < ssf->sss_update_ssf ) {
1061                                 rs->sr_text = op->o_ssf
1062                                         ? "stronger confidentiality required for update"
1063                                         : "confidentiality required for update";
1064                                 return rs->sr_err;
1065                         }
1066
1067                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1068                                 BER_BVISEMPTY( &op->o_ndn ) )
1069                         {
1070                                 rs->sr_text = "modifications require authentication";
1071                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1072                                 return rs->sr_err;
1073                         }
1074
1075 #ifdef SLAP_X_LISTENER_MOD
1076                         if ( op->o_conn->c_listener &&
1077                                 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1078                                         ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1079                         {
1080                                 /* no "w" mode means readonly */
1081                                 rs->sr_text = "modifications not allowed on this listener";
1082                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1083                                 return rs->sr_err;
1084                         }
1085 #endif /* SLAP_X_LISTENER_MOD */
1086                 }
1087         }
1088
1089         if ( !session ) {
1090                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1091
1092                 if( requires & SLAP_REQUIRE_STRONG ) {
1093                         /* should check mechanism */
1094                         if( ( op->o_transport_ssf < ssf->sss_transport
1095                                 && op->o_authtype == LDAP_AUTH_SIMPLE )
1096                                 || BER_BVISEMPTY( &op->o_dn ) )
1097                         {
1098                                 rs->sr_text = "strong(er) authentication required";
1099                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1100                                 return rs->sr_err;
1101                         }
1102                 }
1103
1104                 if( requires & SLAP_REQUIRE_SASL ) {
1105                         if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1106                                 rs->sr_text = "SASL authentication required";
1107                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1108                                 return rs->sr_err;
1109                         }
1110                 }
1111                         
1112                 if( requires & SLAP_REQUIRE_AUTHC ) {
1113                         if( BER_BVISEMPTY( &op->o_dn ) ) {
1114                                 rs->sr_text = "authentication required";
1115                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1116                                 return rs->sr_err;
1117                         }
1118                 }
1119
1120                 if( requires & SLAP_REQUIRE_BIND ) {
1121                         int version;
1122                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1123                         version = op->o_conn->c_protocol;
1124                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1125
1126                         if( !version ) {
1127                                 /* no bind has occurred */
1128                                 rs->sr_text = "BIND required";
1129                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1130                                 return rs->sr_err;
1131                         }
1132                 }
1133
1134                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1135                         if( op->o_protocol < LDAP_VERSION3 ) {
1136                                 /* no bind has occurred */
1137                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1138                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1139                                 return rs->sr_err;
1140                         }
1141                 }
1142
1143 #ifdef SLAP_X_LISTENER_MOD
1144                 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1145                         if ( op->o_conn->c_listener &&
1146                                 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1147                 {
1148                                 /* no "x" mode means bind required */
1149                                 rs->sr_text = "bind required on this listener";
1150                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1151                                 return rs->sr_err;
1152                         }
1153                 }
1154
1155                 if ( !starttls && !updateop ) {
1156                         if ( op->o_conn->c_listener &&
1157                                 !( op->o_conn->c_listener->sl_perms &
1158                                         ( !BER_BVISEMPTY( &op->o_dn )
1159                                                 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1160                         {
1161                                 /* no "r" mode means no read */
1162                                 rs->sr_text = "read not allowed on this listener";
1163                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1164                                 return rs->sr_err;
1165                         }
1166                 }
1167 #endif /* SLAP_X_LISTENER_MOD */
1168
1169         }
1170
1171         if( ( restrictops & opflag )
1172                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1173                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1174                         rs->sr_text = "read operations restricted";
1175                 } else if ( restrictops & exopflag ) {
1176                         rs->sr_text = "extended operation restricted";
1177                 } else {
1178                         rs->sr_text = "operation restricted";
1179                 }
1180                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1181                 return rs->sr_err;
1182         }
1183
1184         rs->sr_err = LDAP_SUCCESS;
1185         return rs->sr_err;
1186 }
1187
1188 int backend_check_referrals( Operation *op, SlapReply *rs )
1189 {
1190         rs->sr_err = LDAP_SUCCESS;
1191
1192         if( op->o_bd->be_chk_referrals ) {
1193                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1194
1195                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1196                         send_ldap_result( op, rs );
1197                 }
1198         }
1199
1200         return rs->sr_err;
1201 }
1202
1203 int
1204 be_entry_get_rw(
1205         Operation *op,
1206         struct berval *ndn,
1207         ObjectClass *oc,
1208         AttributeDescription *at,
1209         int rw,
1210         Entry **e )
1211 {
1212         *e = NULL;
1213
1214         if ( op->o_bd == NULL ) {
1215                 return LDAP_NO_SUCH_OBJECT;
1216         }
1217
1218         if ( op->o_bd->be_fetch ) {
1219                 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1220         }
1221
1222         return LDAP_UNWILLING_TO_PERFORM;
1223 }
1224
1225 int 
1226 fe_acl_group(
1227         Operation *op,
1228         Entry   *target,
1229         struct berval *gr_ndn,
1230         struct berval *op_ndn,
1231         ObjectClass *group_oc,
1232         AttributeDescription *group_at )
1233 {
1234         Entry *e;
1235         void *o_priv = op->o_private, *e_priv = NULL;
1236         Attribute *a;
1237         int rc;
1238         GroupAssertion *g;
1239         Backend *be = op->o_bd;
1240
1241         op->o_bd = select_backend( gr_ndn, 0, 0 );
1242
1243         for ( g = op->o_groups; g; g = g->ga_next ) {
1244                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1245                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1246                 {
1247                         continue;
1248                 }
1249                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1250                         break;
1251                 }
1252         }
1253
1254         if ( g ) {
1255                 rc = g->ga_res;
1256                 goto done;
1257         }
1258
1259         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1260                 e = target;
1261                 rc = 0;
1262         } else {
1263                 op->o_private = NULL;
1264                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1265                 e_priv = op->o_private;
1266                 op->o_private = o_priv;
1267         }
1268         if ( e ) {
1269                 a = attr_find( e->e_attrs, group_at );
1270                 if ( a ) {
1271                         /* If the attribute is a subtype of labeledURI, treat this as
1272                          * a dynamic group ala groupOfURLs
1273                          */
1274                         if (is_at_subtype( group_at->ad_type,
1275                                 slap_schema.si_ad_labeledURI->ad_type ) )
1276                         {
1277                                 int i;
1278                                 LDAPURLDesc *ludp;
1279                                 struct berval bv, nbase;
1280                                 Filter *filter;
1281                                 Entry *user;
1282                                 void *user_priv = NULL;
1283                                 Backend *b2 = op->o_bd;
1284
1285                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1286                                         user = target;
1287                                 } else {
1288                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1289                                         op->o_private = NULL;
1290                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1291                                         user_priv = op->o_private;
1292                                         op->o_private = o_priv;
1293                                 }
1294                                 
1295                                 if ( rc == 0 ) {
1296                                         rc = LDAP_COMPARE_FALSE;
1297                                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1298                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1299                                                         LDAP_URL_SUCCESS )
1300                                                 {
1301                                                         continue;
1302                                                 }
1303                                                 BER_BVZERO( &nbase );
1304                                                 /* host part must be empty */
1305                                                 /* attrs and extensions parts must be empty */
1306                                                 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1307                                                         ludp->lud_attrs || ludp->lud_exts )
1308                                                 {
1309                                                         goto loopit;
1310                                                 }
1311                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1312                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1313                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1314                                                 {
1315                                                         goto loopit;
1316                                                 }
1317                                                 switch ( ludp->lud_scope ) {
1318                                                 case LDAP_SCOPE_BASE:
1319                                                         if ( !dn_match( &nbase, op_ndn ) ) {
1320                                                                 goto loopit;
1321                                                         }
1322                                                         break;
1323                                                 case LDAP_SCOPE_ONELEVEL:
1324                                                         dnParent( op_ndn, &bv );
1325                                                         if ( !dn_match( &nbase, &bv ) ) {
1326                                                                 goto loopit;
1327                                                         }
1328                                                         break;
1329                                                 case LDAP_SCOPE_SUBTREE:
1330                                                         if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1331                                                                 goto loopit;
1332                                                         }
1333                                                         break;
1334                                                 case LDAP_SCOPE_SUBORDINATE:
1335                                                         if ( dn_match( &nbase, op_ndn ) ||
1336                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1337                                                         {
1338                                                                 goto loopit;
1339                                                         }
1340                                                 }
1341                                                 filter = str2filter_x( op, ludp->lud_filter );
1342                                                 if ( filter ) {
1343                                                         if ( test_filter( NULL, user, filter ) ==
1344                                                                 LDAP_COMPARE_TRUE )
1345                                                         {
1346                                                                 rc = 0;
1347                                                         }
1348                                                         filter_free_x( op, filter );
1349                                                 }
1350 loopit:
1351                                                 ldap_free_urldesc( ludp );
1352                                                 if ( !BER_BVISNULL( &nbase ) ) {
1353                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1354                                                 }
1355                                                 if ( rc == 0 ) break;
1356                                         }
1357                                         if ( user != target ) {
1358                                                 op->o_private = user_priv;
1359                                                 be_entry_release_r( op, user );
1360                                                 op->o_private = o_priv;
1361                                         }
1362                                 }
1363                                 op->o_bd = b2;
1364                         } else {
1365                                 rc = value_find_ex( group_at,
1366                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1367                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1368                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1369                                 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1370                                         rc = LDAP_COMPARE_FALSE;
1371                         }
1372                 } else {
1373                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1374                 }
1375                 if ( e != target ) {
1376                         op->o_private = e_priv;
1377                         be_entry_release_r( op, e );
1378                         op->o_private = o_priv;
1379                 }
1380         } else {
1381                 rc = LDAP_NO_SUCH_OBJECT;
1382         }
1383
1384         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1385                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1386                         op->o_tmpmemctx );
1387                 g->ga_be = op->o_bd;
1388                 g->ga_oc = group_oc;
1389                 g->ga_at = group_at;
1390                 g->ga_res = rc;
1391                 g->ga_len = gr_ndn->bv_len;
1392                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1393                 g->ga_next = op->o_groups;
1394                 op->o_groups = g;
1395         }
1396 done:
1397         op->o_bd = be;
1398         return rc;
1399 }
1400
1401 int 
1402 backend_group(
1403         Operation *op,
1404         Entry   *target,
1405         struct berval *gr_ndn,
1406         struct berval *op_ndn,
1407         ObjectClass *group_oc,
1408         AttributeDescription *group_at )
1409 {
1410         int                     rc;
1411         BackendDB               *be_orig;
1412
1413         if ( op->o_abandon ) {
1414                 return SLAPD_ABANDON;
1415         }
1416
1417         be_orig = op->o_bd;
1418         op->o_bd = frontendDB;
1419 #ifdef SLAP_OVERLAY_ACCESS
1420         rc = frontendDB->be_group( op, target, gr_ndn,
1421                 op_ndn, group_oc, group_at );
1422 #else /* ! SLAP_OVERLAY_ACCESS */
1423         rc = fe_acl_group( op, target, gr_ndn,
1424                 op_ndn, group_oc, group_at );
1425 #endif /* ! SLAP_OVERLAY_ACCESS */
1426         op->o_bd = be_orig;
1427
1428         return rc;
1429 }
1430
1431 int 
1432 fe_acl_attribute(
1433         Operation *op,
1434         Entry   *target,
1435         struct berval   *edn,
1436         AttributeDescription *entry_at,
1437         BerVarray *vals,
1438         slap_access_t access )
1439 {
1440         Entry                   *e = NULL;
1441         void                    *o_priv = op->o_private, *e_priv = NULL;
1442         Attribute               *a = NULL;
1443         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1444         AccessControlState      acl_state = ACL_STATE_INIT;
1445         Backend                 *be = op->o_bd;
1446
1447         op->o_bd = select_backend( edn, 0, 0 );
1448
1449         if ( target && dn_match( &target->e_nname, edn ) ) {
1450                 e = target;
1451
1452         } else {
1453                 op->o_private = NULL;
1454                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1455                 e_priv = op->o_private;
1456                 op->o_private = o_priv;
1457         } 
1458
1459         if ( e ) {
1460                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1461                         assert( vals == NULL );
1462
1463                         rc = LDAP_SUCCESS;
1464                         if ( op->o_conn && access > ACL_NONE &&
1465                                 access_allowed( op, e, entry_at, NULL,
1466                                                 access, &acl_state ) == 0 )
1467                         {
1468                                 rc = LDAP_INSUFFICIENT_ACCESS;
1469                         }
1470                         goto freeit;
1471                 }
1472
1473                 a = attr_find( e->e_attrs, entry_at );
1474                 if ( a == NULL ) {
1475                         SlapReply       rs = { 0 };
1476                         AttributeName   anlist[ 2 ];
1477
1478                         anlist[ 0 ].an_name = entry_at->ad_cname;
1479                         anlist[ 0 ].an_desc = entry_at;
1480                         BER_BVZERO( &anlist[ 1 ].an_name );
1481                         rs.sr_attrs = anlist;
1482                         
1483                         /* NOTE: backend_operational() is also called
1484                          * when returning results, so it's supposed
1485                          * to do no harm to entries */
1486                         rs.sr_entry = e;
1487                         rc = backend_operational( op, &rs );
1488                         rs.sr_entry = NULL;
1489  
1490                         if ( rc == LDAP_SUCCESS ) {
1491                                 if ( rs.sr_operational_attrs ) {
1492                                         freeattr = 1;
1493                                         a = rs.sr_operational_attrs;
1494
1495                                 } else {
1496                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1497                                 }
1498                         }
1499                 }
1500
1501                 if ( a ) {
1502                         BerVarray v;
1503
1504                         if ( op->o_conn && access > ACL_NONE &&
1505                                 access_allowed( op, e, entry_at, NULL,
1506                                                 access, &acl_state ) == 0 )
1507                         {
1508                                 rc = LDAP_INSUFFICIENT_ACCESS;
1509                                 goto freeit;
1510                         }
1511
1512                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1513                                 ;
1514                         
1515                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1516                                 op->o_tmpmemctx );
1517                         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1518                         {
1519                                 if ( op->o_conn && access > ACL_NONE && 
1520                                         access_allowed( op, e, entry_at,
1521                                                         &a->a_nvals[i],
1522                                                         access,
1523                                                         &acl_state ) == 0 )
1524                                 {
1525                                         continue;
1526                                 }
1527                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1528                                                 op->o_tmpmemctx );
1529                                 if ( !BER_BVISNULL( &v[j] ) ) {
1530                                         j++;
1531                                 }
1532                         }
1533                         if ( j == 0 ) {
1534                                 op->o_tmpfree( v, op->o_tmpmemctx );
1535                                 *vals = NULL;
1536                                 rc = LDAP_INSUFFICIENT_ACCESS;
1537
1538                         } else {
1539                                 BER_BVZERO( &v[j] );
1540                                 *vals = v;
1541                                 rc = LDAP_SUCCESS;
1542                         }
1543                 }
1544 freeit:         if ( e != target ) {
1545                         op->o_private = e_priv;
1546                         be_entry_release_r( op, e );
1547                         op->o_private = o_priv;
1548                 }
1549                 if ( freeattr ) {
1550                         attr_free( a );
1551                 }
1552         }
1553
1554         op->o_bd = be;
1555         return rc;
1556 }
1557
1558 int 
1559 backend_attribute(
1560         Operation *op,
1561         Entry   *target,
1562         struct berval   *edn,
1563         AttributeDescription *entry_at,
1564         BerVarray *vals,
1565         slap_access_t access )
1566 {
1567         int                     rc;
1568         BackendDB               *be_orig;
1569
1570         be_orig = op->o_bd;
1571         op->o_bd = frontendDB;
1572 #ifdef SLAP_OVERLAY_ACCESS
1573         rc = frontendDB->be_attribute( op, target, edn,
1574                 entry_at, vals, access );
1575 #else /* !SLAP_OVERLAY_ACCESS */
1576         rc = fe_acl_attribute( op, target, edn,
1577                 entry_at, vals, access );
1578 #endif /* !SLAP_OVERLAY_ACCESS */
1579         op->o_bd = be_orig;
1580
1581         return rc;
1582 }
1583
1584 int 
1585 backend_access(
1586         Operation               *op,
1587         Entry                   *target,
1588         struct berval           *edn,
1589         AttributeDescription    *entry_at,
1590         struct berval           *nval,
1591         slap_access_t           access,
1592         slap_mask_t             *mask )
1593 {
1594         Entry           *e = NULL;
1595         void            *o_priv = op->o_private, *e_priv = NULL;
1596         int             rc = LDAP_INSUFFICIENT_ACCESS;
1597         Backend         *be = op->o_bd;
1598
1599         /* pedantic */
1600         assert( op != NULL );
1601         assert( op->o_conn != NULL );
1602         assert( edn != NULL );
1603         assert( access > ACL_NONE );
1604
1605         op->o_bd = select_backend( edn, 0, 0 );
1606
1607         if ( target && dn_match( &target->e_nname, edn ) ) {
1608                 e = target;
1609
1610         } else {
1611                 op->o_private = NULL;
1612                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1613                 e_priv = op->o_private;
1614                 op->o_private = o_priv;
1615         } 
1616
1617         if ( e ) {
1618                 Attribute       *a = NULL;
1619                 int             freeattr = 0;
1620
1621                 if ( entry_at == NULL ) {
1622                         entry_at = slap_schema.si_ad_entry;
1623                 }
1624
1625                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1626                 {
1627                         if ( access_allowed_mask( op, e, entry_at,
1628                                         NULL, access, NULL, mask ) == 0 )
1629                         {
1630                                 rc = LDAP_INSUFFICIENT_ACCESS;
1631
1632                         } else {
1633                                 rc = LDAP_SUCCESS;
1634                         }
1635
1636                 } else {
1637                         a = attr_find( e->e_attrs, entry_at );
1638                         if ( a == NULL ) {
1639                                 SlapReply       rs = { 0 };
1640                                 AttributeName   anlist[ 2 ];
1641
1642                                 anlist[ 0 ].an_name = entry_at->ad_cname;
1643                                 anlist[ 0 ].an_desc = entry_at;
1644                                 BER_BVZERO( &anlist[ 1 ].an_name );
1645                                 rs.sr_attrs = anlist;
1646                         
1647                                 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1648
1649                                 /* NOTE: backend_operational() is also called
1650                                  * when returning results, so it's supposed
1651                                  * to do no harm to entries */
1652                                 rs.sr_entry = e;
1653                                 rc = backend_operational( op, &rs );
1654                                 rs.sr_entry = NULL;
1655
1656                                 if ( rc == LDAP_SUCCESS ) {
1657                                         if ( rs.sr_operational_attrs ) {
1658                                                 freeattr = 1;
1659                                                 a = rs.sr_operational_attrs;
1660
1661                                         } else {
1662                                                 rc = LDAP_NO_SUCH_OBJECT;
1663                                         }
1664                                 }
1665                         }
1666
1667                         if ( a ) {
1668                                 if ( access_allowed_mask( op, e, entry_at,
1669                                                 nval, access, NULL, mask ) == 0 )
1670                                 {
1671                                         rc = LDAP_INSUFFICIENT_ACCESS;
1672                                         goto freeit;
1673                                 }
1674                                 rc = LDAP_SUCCESS;
1675                         }
1676                 }
1677 freeit:         if ( e != target ) {
1678                         op->o_private = e_priv;
1679                         be_entry_release_r( op, e );
1680                         op->o_private = o_priv;
1681                 }
1682                 if ( freeattr ) {
1683                         attr_free( a );
1684                 }
1685         }
1686
1687         op->o_bd = be;
1688         return rc;
1689 }
1690
1691 int
1692 fe_aux_operational(
1693         Operation *op,
1694         SlapReply *rs )
1695 {
1696         Attribute               **ap;
1697         int                     rc = 0;
1698         BackendDB               *be_orig;
1699
1700         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1701                 /* just count them */ ;
1702
1703         /*
1704          * If operational attributes (allegedly) are required, 
1705          * and the backend supports specific operational attributes, 
1706          * add them to the attribute list
1707          */
1708         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1709                 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1710         {
1711                 *ap = slap_operational_entryDN( rs->sr_entry );
1712                 ap = &(*ap)->a_next;
1713         }
1714
1715         if ( 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