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