]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Fix ITS#3288: use be_pcl_mutexp instead of be_pcl_mutex
[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-2004 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 #include "ldap_rq.h"
41
42 #ifdef LDAP_SLAPI
43 #include "slapi/slapi.h"
44 #endif
45
46 /*
47  * If a module is configured as dynamic, its header should not
48  * get included into slapd. While this is a general rule and does
49  * not have much of an effect in UNIX, this rule should be adhered
50  * to for Windows, where dynamic object code should not be implicitly
51  * imported into slapd without appropriate __declspec(dllimport) directives.
52  */
53
54 #if SLAPD_BDB == SLAPD_MOD_STATIC
55 #include "back-bdb/external.h"
56 #endif
57 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
58 #include "back-dnssrv/external.h"
59 #endif
60 #if SLAPD_HDB == SLAPD_MOD_STATIC
61 #include "back-hdb/external.h"
62 #endif
63 #if SLAPD_LDAP == SLAPD_MOD_STATIC
64 #include "back-ldap/external.h"
65 #endif
66 #if SLAPD_LDBM == SLAPD_MOD_STATIC
67 #include "back-ldbm/external.h"
68 #endif
69 #if SLAPD_META == SLAPD_MOD_STATIC
70 #include "back-meta/external.h"
71 #endif
72 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
73 #include "back-monitor/external.h"
74 #endif
75 #if SLAPD_NULL == SLAPD_MOD_STATIC
76 #include "back-null/external.h"
77 #endif
78 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
79 #include "back-passwd/external.h"
80 #endif
81 #if SLAPD_PERL == SLAPD_MOD_STATIC
82 #include "back-perl/external.h"
83 #endif
84 #if SLAPD_RELAY == SLAPD_MOD_STATIC
85 #include "back-relay/external.h"
86 #endif
87 #if SLAPD_SHELL == SLAPD_MOD_STATIC
88 #include "back-shell/external.h"
89 #endif
90 #if SLAPD_TCL == SLAPD_MOD_STATIC
91 #include "back-tcl/external.h"
92 #endif
93 #if SLAPD_SQL == SLAPD_MOD_STATIC
94 #include "back-sql/external.h"
95 #endif
96 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
97 #include "private/external.h"
98 #endif
99
100 static BackendInfo binfo[] = {
101 #if SLAPD_BDB == SLAPD_MOD_STATIC
102         {"bdb", bdb_initialize},
103 #endif
104 #if SLAPD_DNSSRV == SLAPD_MOD_STATIC
105         {"dnssrv",      dnssrv_back_initialize},
106 #endif
107 #if SLAPD_HDB == SLAPD_MOD_STATIC
108         {"hdb", hdb_initialize},
109 #endif
110 #if SLAPD_LDAP == SLAPD_MOD_STATIC
111         {"ldap",        ldap_back_initialize},
112 #endif
113 #if SLAPD_LDBM == SLAPD_MOD_STATIC
114         {"ldbm",        ldbm_back_initialize},
115 #endif
116 #if SLAPD_META == SLAPD_MOD_STATIC
117         {"meta",        meta_back_initialize},
118 #endif
119 #if SLAPD_MONITOR == SLAPD_MOD_STATIC
120         {"monitor",     monitor_back_initialize},
121 #endif
122 #if SLAPD_NULL == SLAPD_MOD_STATIC
123         {"null",        null_back_initialize},
124 #endif
125 #if SLAPD_PASSWD == SLAPD_MOD_STATIC
126         {"passwd",      passwd_back_initialize},
127 #endif
128 #if SLAPD_PERL == SLAPD_MOD_STATIC
129         {"perl",        perl_back_initialize},
130 #endif
131 #if SLAPD_RELAY == SLAPD_MOD_STATIC
132         {"relay",       relay_back_initialize},
133 #endif
134 #if SLAPD_SHELL == SLAPD_MOD_STATIC
135         {"shell",       shell_back_initialize},
136 #endif
137 #if SLAPD_TCL == SLAPD_MOD_STATIC
138         {"tcl",         tcl_back_initialize},
139 #endif
140 #if SLAPD_SQL == SLAPD_MOD_STATIC
141         {"sql",         sql_back_initialize},
142 #endif
143         /* for any private backend */
144 #if SLAPD_PRIVATE == SLAPD_MOD_STATIC
145         {"private",     private_back_initialize},
146 #endif
147         {NULL}
148 };
149
150 int                     nBackendInfo = 0;
151 BackendInfo     *backendInfo = NULL;
152
153 int                     nBackendDB = 0; 
154 BackendDB       *backendDB = NULL;
155
156 ldap_pvt_thread_pool_t  syncrepl_pool;
157 int                     syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
158
159 int backend_init(void)
160 {
161         int rc = -1;
162
163         ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
164
165         if((nBackendInfo != 0) || (backendInfo != NULL)) {
166                 /* already initialized */
167 #ifdef NEW_LOGGING
168                 LDAP_LOG( BACKEND, ERR, 
169                         "backend_init:  backend already initialized\n", 0, 0, 0 );
170 #else
171                 Debug( LDAP_DEBUG_ANY,
172                         "backend_init: already initialized.\n", 0, 0, 0 );
173 #endif
174                 return -1;
175         }
176
177         for( ;
178                 binfo[nBackendInfo].bi_type != NULL;
179                 nBackendInfo++ )
180         {
181                 assert( binfo[nBackendInfo].bi_init );
182
183                 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
184
185                 if(rc != 0) {
186 #ifdef NEW_LOGGING
187                         LDAP_LOG( BACKEND, INFO, 
188                                 "backend_init:  initialized for type \"%s\"\n",
189                                 binfo[nBackendInfo].bi_type, 0, 0 );
190 #else
191                         Debug( LDAP_DEBUG_ANY,
192                                 "backend_init: initialized for type \"%s\"\n",
193                                 binfo[nBackendInfo].bi_type, 0, 0 );
194 #endif
195                         /* destroy those we've already inited */
196                         for( nBackendInfo--;
197                                 nBackendInfo >= 0 ;
198                                 nBackendInfo-- )
199                         { 
200                                 if ( binfo[nBackendInfo].bi_destroy ) {
201                                         binfo[nBackendInfo].bi_destroy(
202                                                 &binfo[nBackendInfo] );
203                                 }
204                         }
205                         return rc;
206                 }
207         }
208
209         if ( nBackendInfo > 0) {
210                 backendInfo = binfo;
211                 return 0;
212         }
213
214 #ifdef SLAPD_MODULES    
215         return 0;
216 #else
217
218 #ifdef NEW_LOGGING
219         LDAP_LOG( BACKEND, ERR, "backend_init: failed\n", 0, 0, 0 );
220 #else
221         Debug( LDAP_DEBUG_ANY,
222                 "backend_init: failed\n",
223                 0, 0, 0 );
224 #endif
225
226         return rc;
227 #endif /* SLAPD_MODULES */
228 }
229
230 int backend_add(BackendInfo *aBackendInfo)
231 {
232         int rc = 0;
233
234         if ( aBackendInfo->bi_init == NULL ) {
235 #ifdef NEW_LOGGING
236                 LDAP_LOG( BACKEND, ERR, "backend_add: "
237                         "backend type \"%s\" does not have the (mandatory)init function\n",
238                         aBackendInfo->bi_type, 0, 0 );
239 #else
240                 Debug( LDAP_DEBUG_ANY, "backend_add: "
241                         "backend type \"%s\" does not have the (mandatory)init function\n",
242                         aBackendInfo->bi_type, 0, 0 );
243 #endif
244                 return -1;
245         }
246
247    if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
248 #ifdef NEW_LOGGING
249                 LDAP_LOG( BACKEND, ERR, 
250                         "backend_add:  initialization for type \"%s\" failed\n",
251                         aBackendInfo->bi_type, 0, 0 );
252 #else
253                 Debug( LDAP_DEBUG_ANY,
254                         "backend_add:  initialization for type \"%s\" failed\n",
255                         aBackendInfo->bi_type, 0, 0 );
256 #endif
257                 return rc;
258    }
259
260         /* now add the backend type to the Backend Info List */
261         {
262                 BackendInfo *newBackendInfo = 0;
263
264                 /* if backendInfo == binfo no deallocation of old backendInfo */
265                 if (backendInfo == binfo) {
266                         newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
267                         AC_MEMCPY(newBackendInfo, backendInfo,
268                                 sizeof(BackendInfo) * nBackendInfo);
269                 } else {
270                         newBackendInfo = ch_realloc(backendInfo,
271                                 sizeof(BackendInfo) * (nBackendInfo + 1));
272                 }
273
274                 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
275                         sizeof(BackendInfo));
276                 backendInfo = newBackendInfo;
277                 nBackendInfo++;
278                 return 0;
279         }
280 }
281
282 /* startup a specific backend database */
283 int backend_startup_one(Backend *be)
284 {
285         int rc = 0;
286
287         assert(be);
288
289         be->be_pending_csn_list = (struct be_pcl *)
290                 ch_calloc( 1, sizeof( struct be_pcl ));
291         build_new_dn( &be->be_context_csn, be->be_nsuffix,
292                 (struct berval *)&slap_ldapsync_cn_bv, NULL );
293
294         LDAP_TAILQ_INIT( be->be_pending_csn_list );
295
296 #ifdef NEW_LOGGING
297         LDAP_LOG( BACKEND, DETAIL1, "backend_startup:  starting \"%s\"\n",
298                 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
299                 0, 0 );
300 #else
301         Debug( LDAP_DEBUG_TRACE,
302                 "backend_startup: starting \"%s\"\n",
303                 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
304                 0, 0 );
305 #endif
306         if ( be->bd_info->bi_db_open ) {
307                 rc = be->bd_info->bi_db_open( be );
308                 if ( rc != 0 ) {
309 #ifdef NEW_LOGGING
310                         LDAP_LOG( BACKEND, CRIT, 
311                                 "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
312 #else
313                         Debug( LDAP_DEBUG_ANY,
314                                 "backend_startup: bi_db_open failed! (%d)\n",
315                                 rc, 0, 0 );
316 #endif
317                 }
318         }
319         return rc;
320 }
321
322 int backend_startup(Backend *be)
323 {
324         int i;
325         int rc = 0;
326
327         if( ! ( nBackendDB > 0 ) ) {
328                 /* no databases */
329 #ifdef NEW_LOGGING
330                 LDAP_LOG( BACKEND, INFO, 
331                         "backend_startup: %d databases to startup. \n", nBackendDB, 0, 0 );
332 #else
333                 Debug( LDAP_DEBUG_ANY,
334                         "backend_startup: %d databases to startup.\n",
335                         nBackendDB, 0, 0 );
336 #endif
337                 return 1;
338         }
339
340         if(be != NULL) {
341                 if ( be->bd_info->bi_open ) {
342                         rc = be->bd_info->bi_open( be->bd_info );
343                         if ( rc != 0 ) {
344 #ifdef NEW_LOGGING
345                                 LDAP_LOG( BACKEND, CRIT,
346                                         "backend_startup: bi_open failed!\n", 0, 0, 0 );
347 #else
348                                 Debug( LDAP_DEBUG_ANY,
349                                         "backend_startup: bi_open failed!\n",
350                                         0, 0, 0 );
351 #endif
352
353                                 return rc;
354                         }
355                 }
356
357                 return backend_startup_one( be );
358         }
359
360         /* open frontend, if required */
361         if ( frontendDB->bd_info->bi_db_open ) {
362                 rc = frontendDB->bd_info->bi_db_open( frontendDB );
363                 if ( rc != 0 ) {
364 #ifdef NEW_LOGGING
365                         LDAP_LOG( BACKEND, CRIT, 
366                                 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
367                                 rc, 0, 0 );
368 #else
369                         Debug( LDAP_DEBUG_ANY,
370                                 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
371                                 rc, 0, 0 );
372 #endif
373                         return rc;
374                 }
375         }
376
377         /* open each backend type */
378         for( i = 0; i < nBackendInfo; i++ ) {
379                 if( backendInfo[i].bi_nDB == 0) {
380                         /* no database of this type, don't open */
381                         continue;
382                 }
383
384                 if( backendInfo[i].bi_open ) {
385                         rc = backendInfo[i].bi_open(
386                                 &backendInfo[i] );
387                         if ( rc != 0 ) {
388 #ifdef NEW_LOGGING
389                                 LDAP_LOG( BACKEND, CRIT, 
390                                         "backend_startup: bi_open %d failed!\n", i, 0, 0 );
391 #else
392                                 Debug( LDAP_DEBUG_ANY,
393                                         "backend_startup: bi_open %d failed!\n",
394                                         i, 0, 0 );
395 #endif
396                                 return rc;
397                         }
398                 }
399         }
400
401         ldap_pvt_thread_mutex_init( &syncrepl_rq.rq_mutex );
402         LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
403         LDAP_STAILQ_INIT( &syncrepl_rq.run_list );
404
405         /* open each backend database */
406         for( i = 0; i < nBackendDB; i++ ) {
407                 if ( backendDB[i].be_suffix == NULL ) {
408 #ifdef NEW_LOGGING
409                         LDAP_LOG( BACKEND, CRIT, 
410                                 "backend_startup: warning, database %d (%s) "
411                                 "has no suffix\n",
412                                 i, backendDB[i].bd_info->bi_type, 0 );
413 #else
414                         Debug( LDAP_DEBUG_ANY,
415                                 "backend_startup: warning, database %d (%s) "
416                                 "has no suffix\n",
417                                 i, backendDB[i].bd_info->bi_type, 0 );
418 #endif
419                 }
420                 /* append global access controls */
421                 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
422
423                 rc = backend_startup_one( &backendDB[i] );
424
425                 if ( rc ) return rc;
426
427
428                 if ( !LDAP_STAILQ_EMPTY( &backendDB[i].be_syncinfo )) {
429                         syncinfo_t *si;
430
431                         if ( !( backendDB[i].be_search && backendDB[i].be_add &&
432                                 backendDB[i].be_modify && backendDB[i].be_delete )) {
433 #ifdef NEW_LOGGING
434                                 LDAP_LOG( BACKEND, CRIT, 
435                                         "backend_startup: database(%d) does not support "
436                                         "operations required for syncrepl", i, 0, 0 );
437 #else
438                                 Debug( LDAP_DEBUG_ANY,
439                                         "backend_startup: database(%d) does not support "
440                                         "operations required for syncrepl", i, 0, 0 );
441 #endif
442                                 continue;
443                         }
444
445                         LDAP_STAILQ_FOREACH( si, &backendDB[i].be_syncinfo, si_next ) {
446                                 si->si_be = &backendDB[i];
447                                 init_syncrepl( si );
448                                 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
449                                 ldap_pvt_runqueue_insert( &syncrepl_rq,
450                                                 si->si_interval, do_syncrepl, (void *) si );
451                                 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
452                         }
453                 }
454         }
455
456         return rc;
457 }
458
459 int backend_num( Backend *be )
460 {
461         int i;
462
463         if( be == NULL ) return -1;
464
465         for( i = 0; i < nBackendDB; i++ ) {
466                 if( be == &backendDB[i] ) return i;
467         }
468         return -1;
469 }
470
471 int backend_shutdown( Backend *be )
472 {
473         int i;
474         int rc = 0;
475
476         if( be != NULL ) {
477                 /* shutdown a specific backend database */
478
479                 if ( be->bd_info->bi_nDB == 0 ) {
480                         /* no database of this type, we never opened it */
481                         return 0;
482                 }
483
484                 if ( be->bd_info->bi_db_close ) {
485                         be->bd_info->bi_db_close( be );
486                 }
487
488                 if( be->bd_info->bi_close ) {
489                         be->bd_info->bi_close( be->bd_info );
490                 }
491
492                 return 0;
493         }
494
495         /* close each backend database */
496         for( i = 0; i < nBackendDB; i++ ) {
497                 if ( backendDB[i].bd_info->bi_db_close ) {
498                         backendDB[i].bd_info->bi_db_close(
499                                 &backendDB[i] );
500                 }
501
502                 if(rc != 0) {
503 #ifdef NEW_LOGGING
504                         LDAP_LOG( BACKEND, NOTICE, 
505                                 "backend_shutdown: bi_db_close %s failed!\n",
506                                 backendDB[i].be_type, 0, 0 );
507 #else
508                         Debug( LDAP_DEBUG_ANY,
509                                 "backend_close: bi_db_close %s failed!\n",
510                                 backendDB[i].be_type, 0, 0 );
511 #endif
512                 }
513         }
514
515         /* close each backend type */
516         for( i = 0; i < nBackendInfo; i++ ) {
517                 if( backendInfo[i].bi_nDB == 0 ) {
518                         /* no database of this type */
519                         continue;
520                 }
521
522                 if( backendInfo[i].bi_close ) {
523                         backendInfo[i].bi_close(
524                                 &backendInfo[i] );
525                 }
526         }
527
528         /* close frontend, if required */
529         if ( frontendDB->bd_info->bi_db_close ) {
530                 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
531                 if ( rc != 0 ) {
532 #ifdef NEW_LOGGING
533                         LDAP_LOG( BACKEND, CRIT, 
534                                 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
535                                 rc, 0, 0 );
536 #else
537                         Debug( LDAP_DEBUG_ANY,
538                                 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
539                                 rc, 0, 0 );
540 #endif
541                 }
542         }
543
544         return 0;
545 }
546
547 int backend_destroy(void)
548 {
549         int i;
550         BackendDB *bd;
551
552         ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
553
554         /* destroy each backend database */
555         for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
556                 if ( bd->bd_info->bi_db_destroy ) {
557                         bd->bd_info->bi_db_destroy( bd );
558                 }
559                 ber_bvarray_free( bd->be_suffix );
560                 ber_bvarray_free( bd->be_nsuffix );
561                 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
562                 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
563                 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
564                 if ( bd->be_context_csn.bv_val ) free( bd->be_context_csn.bv_val );
565                 acl_destroy( bd->be_acl, frontendDB->be_acl );
566         }
567         free( backendDB );
568
569         /* destroy each backend type */
570         for( i = 0; i < nBackendInfo; i++ ) {
571                 if( backendInfo[i].bi_destroy ) {
572                         backendInfo[i].bi_destroy(
573                                 &backendInfo[i] );
574                 }
575         }
576
577 #ifdef SLAPD_MODULES
578         if (backendInfo != binfo) {
579            free(backendInfo);
580         }
581 #endif /* SLAPD_MODULES */
582
583         nBackendInfo = 0;
584         backendInfo = NULL;
585
586         /* destroy frontend database */
587         bd = frontendDB;
588         if ( bd->bd_info->bi_db_destroy ) {
589                 bd->bd_info->bi_db_destroy( bd );
590         }
591         ber_bvarray_free( bd->be_suffix );
592         ber_bvarray_free( bd->be_nsuffix );
593         if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
594         if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
595         if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
596         acl_destroy( bd->be_acl, frontendDB->be_acl );
597
598         return 0;
599 }
600
601 BackendInfo* backend_info(const char *type)
602 {
603         int i;
604
605         /* search for the backend type */
606         for( i = 0; i < nBackendInfo; i++ ) {
607                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
608                         return &backendInfo[i];
609                 }
610         }
611
612         return NULL;
613 }
614
615
616 BackendDB *
617 backend_db_init(
618     const char  *type )
619 {
620         Backend *be;
621         BackendInfo *bi = backend_info(type);
622         int     rc = 0;
623
624         if( bi == NULL ) {
625                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
626                 return NULL;
627         }
628
629         backendDB = (BackendDB *) ch_realloc(
630                         (char *) backendDB,
631                     (nBackendDB + 1) * sizeof(Backend) );
632
633         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
634
635         be = &backends[nbackends++];
636
637         be->bd_info = bi;
638         be->be_def_limit = frontendDB->be_def_limit;
639         be->be_dfltaccess = frontendDB->be_dfltaccess;
640
641         be->be_restrictops = frontendDB->be_restrictops;
642         be->be_requires = frontendDB->be_requires;
643         be->be_ssf_set = frontendDB->be_ssf_set;
644
645         be->be_context_csn.bv_len = 0;
646         be->be_context_csn.bv_val = NULL;
647         be->be_pcl_mutexp = &be->be_pcl_mutex;
648         ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
649         ldap_pvt_thread_mutex_init( &be->be_context_csn_mutex );
650
651         LDAP_STAILQ_INIT( &be->be_syncinfo );
652
653         /* assign a default depth limit for alias deref */
654         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
655
656         if(bi->bi_db_init) {
657                 rc = bi->bi_db_init( be );
658         }
659
660         if(rc != 0) {
661                 fprintf( stderr, "database init failed (%s)\n", type );
662                 nbackends--;
663                 return NULL;
664         }
665
666         bi->bi_nDB++;
667         return( be );
668 }
669
670 void
671 be_db_close( void )
672 {
673         int     i;
674
675         for ( i = 0; i < nbackends; i++ ) {
676                 if ( backends[i].bd_info->bi_db_close ) {
677                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
678                 }
679         }
680
681         if ( frontendDB->bd_info->bi_db_close ) {
682                 (*frontendDB->bd_info->bi_db_close)( frontendDB );
683         }
684 }
685
686 Backend *
687 select_backend(
688         struct berval * dn,
689         int manageDSAit,
690         int noSubs )
691 {
692         int     i, j;
693         ber_len_t len, dnlen = dn->bv_len;
694         Backend *be = NULL;
695
696         for ( i = 0; i < nbackends; i++ ) {
697                 for ( j = 0; backends[i].be_nsuffix != NULL &&
698                     backends[i].be_nsuffix[j].bv_val != NULL; j++ )
699                 {
700                         if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
701                                 && noSubs )
702                         {
703                                 continue;
704                         }
705
706                         len = backends[i].be_nsuffix[j].bv_len;
707
708                         if ( len > dnlen ) {
709                                 /* suffix is longer than DN */
710                                 continue;
711                         }
712                         
713                         /*
714                          * input DN is normalized, so the separator check
715                          * need not look at escaping
716                          */
717                         if ( len && len < dnlen &&
718                                 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
719                         {
720                                 continue;
721                         }
722
723                         if ( strcmp( backends[i].be_nsuffix[j].bv_val,
724                                 &dn->bv_val[dnlen-len] ) == 0 )
725                         {
726                                 if( be == NULL ) {
727                                         be = &backends[i];
728
729                                         if( manageDSAit && len == dnlen &&
730                                                 !SLAP_GLUE_SUBORDINATE( be ) ) {
731                                                 continue;
732                                         }
733                                 } else {
734                                         be = &backends[i];
735                                 }
736                                 return be;
737                         }
738                 }
739         }
740
741         return be;
742 }
743
744 int
745 be_issuffix(
746     Backend *be,
747     struct berval *bvsuffix )
748 {
749         int     i;
750
751         for ( i = 0;
752                 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
753                 i++ )
754         {
755                 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
756                         return( 1 );
757                 }
758         }
759
760         return( 0 );
761 }
762
763 int
764 be_isroot_dn( Backend *be, struct berval *ndn )
765 {
766         if ( !ndn->bv_len ) {
767                 return( 0 );
768         }
769
770         if ( !be->be_rootndn.bv_len ) {
771                 return( 0 );
772         }
773
774         return dn_match( &be->be_rootndn, ndn );
775 }
776
777 int
778 be_sync_update( Operation *op )
779 {
780         return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
781 }
782
783 int
784 be_slurp_update( Operation *op )
785 {
786         return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
787                 be_isupdate_dn( op->o_bd, &op->o_ndn ));
788 }
789
790 int
791 be_shadow_update( Operation *op )
792 {
793         return ( SLAP_SHADOW( op->o_bd ) &&
794                 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
795 }
796
797 int
798 be_isupdate_dn( Backend *be, struct berval *ndn )
799 {
800         if ( !ndn->bv_len ) return( 0 );
801
802         if ( !be->be_update_ndn.bv_len ) return( 0 );
803
804         return dn_match( &be->be_update_ndn, ndn );
805 }
806
807 struct berval *
808 be_root_dn( Backend *be )
809 {
810         return &be->be_rootdn;
811 }
812
813 int
814 be_isroot( Operation *op )
815 {
816         return be_isroot_dn( op->o_bd, &op->o_ndn );
817 }
818
819 int
820 be_isroot_pw( Operation *op )
821 {
822         int result;
823
824         if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
825                 return 0;
826         }
827
828         if( op->o_bd->be_rootpw.bv_len == 0 ) {
829                 return 0;
830         }
831
832 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
833         ldap_pvt_thread_mutex_lock( &passwd_mutex );
834 #ifdef SLAPD_SPASSWD
835         lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
836 #endif
837 #endif
838
839         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
840
841 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
842 #ifdef SLAPD_SPASSWD
843         lutil_passwd_sasl_conn = NULL;
844 #endif
845         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
846 #endif
847
848         return result == 0;
849 }
850
851 int
852 be_entry_release_rw(
853         Operation *op,
854         Entry *e,
855         int rw )
856 {
857         if ( op->o_bd->be_release ) {
858                 /* free and release entry from backend */
859                 return op->o_bd->be_release( op, e, rw );
860         } else {
861                 /* free entry */
862                 entry_free( e );
863                 return 0;
864         }
865 }
866
867 int
868 backend_unbind( Operation *op, SlapReply *rs )
869 {
870         int             i;
871
872         for ( i = 0; i < nbackends; i++ ) {
873 #if defined( LDAP_SLAPI )
874                 if ( op->o_pb ) {
875                         int rc;
876                         if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
877                         slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
878                         rc = slapi_int_call_plugins( &backends[i],
879                                 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
880                         if ( rc < 0 ) {
881                                 /*
882                                  * A preoperation plugin failure will abort the
883                                  * entire operation.
884                                  */
885 #ifdef NEW_LOGGING
886                                 LDAP_LOG( OPERATION, INFO,
887                                         "do_bind: Unbind preoperation plugin failed\n",
888                                         0, 0, 0);
889 #else
890                                 Debug(LDAP_DEBUG_TRACE,
891                                         "do_bind: Unbind preoperation plugin failed\n",
892                                         0, 0, 0);
893 #endif
894                                 return 0;
895                         }
896                 }
897 #endif /* defined( LDAP_SLAPI ) */
898
899                 if ( backends[i].be_unbind ) {
900                         op->o_bd = &backends[i];
901                         (*backends[i].be_unbind)( op, rs );
902                 }
903
904 #if defined( LDAP_SLAPI )
905                 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
906                         SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
907                 {
908 #ifdef NEW_LOGGING
909                         LDAP_LOG( OPERATION, INFO,
910                                 "do_unbind: Unbind postoperation plugins failed\n",
911                                 0, 0, 0);
912 #else
913                         Debug(LDAP_DEBUG_TRACE,
914                                 "do_unbind: Unbind postoperation plugins failed\n",
915                                 0, 0, 0);
916 #endif
917                 }
918 #endif /* defined( LDAP_SLAPI ) */
919         }
920
921         return 0;
922 }
923
924 int
925 backend_connection_init(
926         Connection   *conn )
927 {
928         int     i;
929
930         for ( i = 0; i < nbackends; i++ ) {
931                 if ( backends[i].be_connection_init ) {
932                         (*backends[i].be_connection_init)( &backends[i], conn);
933                 }
934         }
935
936         return 0;
937 }
938
939 int
940 backend_connection_destroy(
941         Connection   *conn )
942 {
943         int     i;
944
945         for ( i = 0; i < nbackends; i++ ) {
946                 if ( backends[i].be_connection_destroy ) {
947                         (*backends[i].be_connection_destroy)( &backends[i], conn);
948                 }
949         }
950
951         return 0;
952 }
953
954 static int
955 backend_check_controls(
956         Operation *op,
957         SlapReply *rs )
958 {
959         LDAPControl **ctrls = op->o_ctrls;
960         rs->sr_err = LDAP_SUCCESS;
961
962         if( ctrls ) {
963                 for( ; *ctrls != NULL ; ctrls++ ) {
964                         if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
965                                 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
966                         {
967                                 rs->sr_text = "control unavailable in context";
968                                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
969                                 break;
970                         }
971                 }
972         }
973
974         return rs->sr_err;
975 }
976
977 int
978 backend_check_restrictions(
979         Operation *op,
980         SlapReply *rs,
981         struct berval *opdata )
982 {
983         slap_mask_t restrictops;
984         slap_mask_t requires;
985         slap_mask_t opflag;
986         slap_mask_t exopflag = 0;
987         slap_ssf_set_t *ssf;
988         int updateop = 0;
989         int starttls = 0;
990         int session = 0;
991
992         if( op->o_bd ) {
993                 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
994                         return rs->sr_err;
995                 }
996
997                 restrictops = op->o_bd->be_restrictops;
998                 requires = op->o_bd->be_requires;
999                 ssf = &op->o_bd->be_ssf_set;
1000
1001         } else {
1002                 restrictops = frontendDB->be_restrictops;
1003                 requires = frontendDB->be_requires;
1004                 ssf = &frontendDB->be_ssf_set;
1005         }
1006
1007         switch( op->o_tag ) {
1008         case LDAP_REQ_ADD:
1009                 opflag = SLAP_RESTRICT_OP_ADD;
1010                 updateop++;
1011                 break;
1012         case LDAP_REQ_BIND:
1013                 opflag = SLAP_RESTRICT_OP_BIND;
1014                 session++;
1015                 break;
1016         case LDAP_REQ_COMPARE:
1017                 opflag = SLAP_RESTRICT_OP_COMPARE;
1018                 break;
1019         case LDAP_REQ_DELETE:
1020                 updateop++;
1021                 opflag = SLAP_RESTRICT_OP_DELETE;
1022                 break;
1023         case LDAP_REQ_EXTENDED:
1024                 opflag = SLAP_RESTRICT_OP_EXTENDED;
1025
1026                 if( !opdata ) {
1027                         /* treat unspecified as a modify */
1028                         opflag = SLAP_RESTRICT_OP_MODIFY;
1029                         updateop++;
1030                         break;
1031                 }
1032
1033                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
1034                         session++;
1035                         starttls++;
1036                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
1037                         break;
1038                 }
1039
1040                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
1041                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
1042                         break;
1043                 }
1044
1045                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1046                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1047                         break;
1048                 }
1049
1050                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1051                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1052                         updateop++;
1053                         break;
1054                 }
1055
1056                 /* treat everything else as a modify */
1057                 opflag = SLAP_RESTRICT_OP_MODIFY;
1058                 updateop++;
1059                 break;
1060
1061         case LDAP_REQ_MODIFY:
1062                 updateop++;
1063                 opflag = SLAP_RESTRICT_OP_MODIFY;
1064                 break;
1065         case LDAP_REQ_RENAME:
1066                 updateop++;
1067                 opflag = SLAP_RESTRICT_OP_RENAME;
1068                 break;
1069         case LDAP_REQ_SEARCH:
1070                 opflag = SLAP_RESTRICT_OP_SEARCH;
1071                 break;
1072         case LDAP_REQ_UNBIND:
1073                 session++;
1074                 opflag = 0;
1075                 break;
1076         default:
1077                 rs->sr_text = "restrict operations internal error";
1078                 rs->sr_err = LDAP_OTHER;
1079                 return rs->sr_err;
1080         }
1081
1082         if ( !starttls ) {
1083                 /* these checks don't apply to StartTLS */
1084
1085                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1086                 if( op->o_transport_ssf < ssf->sss_transport ) {
1087                         rs->sr_text = op->o_transport_ssf
1088                                 ? "stronger transport confidentiality required"
1089                                 : "transport confidentiality required";
1090                         return rs->sr_err;
1091                 }
1092
1093                 if( op->o_tls_ssf < ssf->sss_tls ) {
1094                         rs->sr_text = op->o_tls_ssf
1095                                 ? "stronger TLS confidentiality required"
1096                                 : "TLS confidentiality required";
1097                         return rs->sr_err;
1098                 }
1099
1100
1101                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1102                         /* simple bind specific check */
1103                         if( op->o_ssf < ssf->sss_simple_bind ) {
1104                                 rs->sr_text = op->o_ssf
1105                                         ? "stronger confidentiality required"
1106                                         : "confidentiality required";
1107                                 return rs->sr_err;
1108                         }
1109                 }
1110
1111                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1112                         /* these checks don't apply to SASL bind */
1113
1114                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
1115                                 rs->sr_text = op->o_sasl_ssf
1116                                         ? "stronger SASL confidentiality required"
1117                                         : "SASL confidentiality required";
1118                                 return rs->sr_err;
1119                         }
1120
1121                         if( op->o_ssf < ssf->sss_ssf ) {
1122                                 rs->sr_text = op->o_ssf
1123                                         ? "stronger confidentiality required"
1124                                         : "confidentiality required";
1125                                 return rs->sr_err;
1126                         }
1127                 }
1128
1129                 if( updateop ) {
1130                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
1131                                 rs->sr_text = op->o_transport_ssf
1132                                         ? "stronger transport confidentiality required for update"
1133                                         : "transport confidentiality required for update";
1134                                 return rs->sr_err;
1135                         }
1136
1137                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
1138                                 rs->sr_text = op->o_tls_ssf
1139                                         ? "stronger TLS confidentiality required for update"
1140                                         : "TLS confidentiality required for update";
1141                                 return rs->sr_err;
1142                         }
1143
1144                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1145                                 rs->sr_text = op->o_sasl_ssf
1146                                         ? "stronger SASL confidentiality required for update"
1147                                         : "SASL confidentiality required for update";
1148                                 return rs->sr_err;
1149                         }
1150
1151                         if( op->o_ssf < ssf->sss_update_ssf ) {
1152                                 rs->sr_text = op->o_ssf
1153                                         ? "stronger confidentiality required for update"
1154                                         : "confidentiality required for update";
1155                                 return rs->sr_err;
1156                         }
1157
1158                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1159                                 op->o_ndn.bv_len == 0 )
1160                         {
1161                                 rs->sr_text = "modifications require authentication";
1162                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1163                                 return rs->sr_err;
1164                         }
1165
1166 #ifdef SLAP_X_LISTENER_MOD
1167                         if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1168                                 /* no "w" mode means readonly */
1169                                 rs->sr_text = "modifications not allowed on this listener";
1170                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1171                                 return rs->sr_err;
1172                         }
1173 #endif /* SLAP_X_LISTENER_MOD */
1174                 }
1175         }
1176
1177         if ( !session ) {
1178                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1179
1180                 if( requires & SLAP_REQUIRE_STRONG ) {
1181                         /* should check mechanism */
1182                         if( ( op->o_transport_ssf < ssf->sss_transport
1183                                 && op->o_authtype == LDAP_AUTH_SIMPLE )
1184                                 || op->o_dn.bv_len == 0 )
1185                         {
1186                                 rs->sr_text = "strong(er) authentication required";
1187                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1188                                 return rs->sr_err;
1189                         }
1190                 }
1191
1192                 if( requires & SLAP_REQUIRE_SASL ) {
1193                         if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1194                                 rs->sr_text = "SASL authentication required";
1195                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1196                                 return rs->sr_err;
1197                         }
1198                 }
1199                         
1200                 if( requires & SLAP_REQUIRE_AUTHC ) {
1201                         if( op->o_dn.bv_len == 0 ) {
1202                                 rs->sr_text = "authentication required";
1203                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1204                                 return rs->sr_err;
1205                         }
1206                 }
1207
1208                 if( requires & SLAP_REQUIRE_BIND ) {
1209                         int version;
1210                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1211                         version = op->o_conn->c_protocol;
1212                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1213
1214                         if( !version ) {
1215                                 /* no bind has occurred */
1216                                 rs->sr_text = "BIND required";
1217                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1218                                 return rs->sr_err;
1219                         }
1220                 }
1221
1222                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1223                         if( op->o_protocol < LDAP_VERSION3 ) {
1224                                 /* no bind has occurred */
1225                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1226                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1227                                 return rs->sr_err;
1228                         }
1229                 }
1230
1231 #ifdef SLAP_X_LISTENER_MOD
1232                 if ( !starttls && op->o_dn.bv_len == 0 ) {
1233                         if ( op->o_conn->c_listener &&
1234                                 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1235                 {
1236                                 /* no "x" mode means bind required */
1237                                 rs->sr_text = "bind required on this listener";
1238                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1239                                 return rs->sr_err;
1240                         }
1241                 }
1242
1243                 if ( !starttls && !updateop ) {
1244                         if ( op->o_conn->c_listener &&
1245                                 !( op->o_conn->c_listener->sl_perms &
1246                                         ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1247                         {
1248                                 /* no "r" mode means no read */
1249                                 rs->sr_text = "read not allowed on this listener";
1250                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1251                                 return rs->sr_err;
1252                         }
1253                 }
1254 #endif /* SLAP_X_LISTENER_MOD */
1255
1256         }
1257
1258         if( ( restrictops & opflag )
1259                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1260                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1261                         rs->sr_text = "read operations restricted";
1262                 } else if ( restrictops & exopflag ) {
1263                         rs->sr_text = "extended operation restricted";
1264                 } else {
1265                         rs->sr_text = "operation restricted";
1266                 }
1267                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1268                 return rs->sr_err;
1269         }
1270
1271         rs->sr_err = LDAP_SUCCESS;
1272         return rs->sr_err;
1273 }
1274
1275 int backend_check_referrals( Operation *op, SlapReply *rs )
1276 {
1277         rs->sr_err = LDAP_SUCCESS;
1278
1279         if( op->o_bd->be_chk_referrals ) {
1280                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1281
1282                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1283                         send_ldap_result( op, rs );
1284                 }
1285         }
1286
1287         return rs->sr_err;
1288 }
1289
1290 int
1291 be_entry_get_rw(
1292         Operation *op,
1293         struct berval *ndn,
1294         ObjectClass *oc,
1295         AttributeDescription *at,
1296         int rw,
1297         Entry **e )
1298 {
1299         int rc;
1300
1301         *e = NULL;
1302
1303         if (op->o_bd == NULL) {
1304                 rc = LDAP_NO_SUCH_OBJECT;
1305         } else if ( op->o_bd->be_fetch ) {
1306                 rc = ( op->o_bd->be_fetch )( op, ndn,
1307                         oc, at, rw, e );
1308         } else {
1309                 rc = LDAP_UNWILLING_TO_PERFORM;
1310         }
1311         return rc;
1312 }
1313
1314 int 
1315 backend_group(
1316         Operation *op,
1317         Entry   *target,
1318         struct berval *gr_ndn,
1319         struct berval *op_ndn,
1320         ObjectClass *group_oc,
1321         AttributeDescription *group_at )
1322 {
1323         Entry *e;
1324         Attribute *a;
1325         int rc;
1326         GroupAssertion *g;
1327         Backend *be = op->o_bd;
1328
1329         if ( op->o_abandon ) return SLAPD_ABANDON;
1330
1331         op->o_bd = select_backend( gr_ndn, 0, 0 );
1332
1333         for (g = op->o_groups; g; g=g->ga_next) {
1334                 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1335                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1336                         continue;
1337                 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1338                         break;
1339         }
1340
1341         if (g) {
1342                 rc = g->ga_res;
1343                 goto done;
1344         }
1345
1346         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1347                 e = target;
1348                 rc = 0;
1349         } else {
1350                 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1351         }
1352         if ( e ) {
1353                 a = attr_find( e->e_attrs, group_at );
1354                 if ( a ) {
1355                         /* If the attribute is a subtype of labeledURI, treat this as
1356                          * a dynamic group ala groupOfURLs
1357                          */
1358                         if (is_at_subtype( group_at->ad_type,
1359                                 slap_schema.si_ad_labeledURI->ad_type ) )
1360                         {
1361                                 int i;
1362                                 LDAPURLDesc *ludp;
1363                                 struct berval bv, nbase;
1364                                 Filter *filter;
1365                                 Entry *user;
1366                                 Backend *b2 = op->o_bd;
1367
1368                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1369                                         user = target;
1370                                 } else {
1371                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1372                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1373                                 }
1374                                 
1375                                 if ( rc == 0 ) {
1376                                         rc = 1;
1377                                         for (i=0; a->a_vals[i].bv_val; i++) {
1378                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1379                                                         LDAP_SUCCESS )
1380                                                 {
1381                                                         continue;
1382                                                 }
1383                                                 nbase.bv_val = NULL;
1384                                                 /* host part must be empty */
1385                                                 /* attrs and extensions parts must be empty */
1386                                                 if (( ludp->lud_host && *ludp->lud_host ) ||
1387                                                         ludp->lud_attrs || ludp->lud_exts )
1388                                                 {
1389                                                         goto loopit;
1390                                                 }
1391                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1392                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1393                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1394                                                 {
1395                                                         goto loopit;
1396                                                 }
1397                                                 switch(ludp->lud_scope) {
1398                                                 case LDAP_SCOPE_BASE:
1399                                                         if ( !dn_match( &nbase, op_ndn )) goto loopit;
1400                                                         break;
1401                                                 case LDAP_SCOPE_ONELEVEL:
1402                                                         dnParent(op_ndn, &bv );
1403                                                         if ( !dn_match( &nbase, &bv )) goto loopit;
1404                                                         break;
1405                                                 case LDAP_SCOPE_SUBTREE:
1406                                                         if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1407                                                         break;
1408 #ifdef LDAP_SCOPE_SUBORDINATE
1409                                                 case LDAP_SCOPE_SUBORDINATE:
1410                                                         if ( dn_match( &nbase, op_ndn ) &&
1411                                                                 !dnIsSuffix(op_ndn, &nbase ))
1412                                                         {
1413                                                                 goto loopit;
1414                                                         }
1415 #endif
1416                                                 }
1417                                                 filter = str2filter_x( op, ludp->lud_filter );
1418                                                 if ( filter ) {
1419                                                         if ( test_filter( NULL, user, filter ) ==
1420                                                                 LDAP_COMPARE_TRUE )
1421                                                         {
1422                                                                 rc = 0;
1423                                                         }
1424                                                         filter_free_x( op, filter );
1425                                                 }
1426 loopit:
1427                                                 ldap_free_urldesc( ludp );
1428                                                 if ( nbase.bv_val ) {
1429                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1430                                                 }
1431                                                 if ( rc == 0 ) break;
1432                                         }
1433                                         if ( user != target ) {
1434                                                 be_entry_release_r( op, user );
1435                                         }
1436                                 }
1437                                 op->o_bd = b2;
1438                         } else {
1439                                 rc = value_find_ex( group_at,
1440                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1441                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1442                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1443                         }
1444                 } else {
1445                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1446                 }
1447                 if (e != target ) {
1448                         be_entry_release_r( op, e );
1449                 }
1450         } else {
1451                 rc = LDAP_NO_SUCH_OBJECT;
1452         }
1453
1454         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1455                 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1456                         op->o_tmpmemctx);
1457                 g->ga_be = op->o_bd;
1458                 g->ga_oc = group_oc;
1459                 g->ga_at = group_at;
1460                 g->ga_res = rc;
1461                 g->ga_len = gr_ndn->bv_len;
1462                 strcpy(g->ga_ndn, gr_ndn->bv_val);
1463                 g->ga_next = op->o_groups;
1464                 op->o_groups = g;
1465         }
1466 done:
1467         op->o_bd = be;
1468         return rc;
1469 }
1470
1471 int 
1472 backend_attribute(
1473         Operation *op,
1474         Entry   *target,
1475         struct berval   *edn,
1476         AttributeDescription *entry_at,
1477         BerVarray *vals )
1478 {
1479         Entry *e;
1480         Attribute *a;
1481         int i, j, rc = LDAP_SUCCESS;
1482         AccessControlState acl_state = ACL_STATE_INIT;
1483         Backend *be = op->o_bd;
1484
1485         op->o_bd = select_backend( edn, 0, 0 );
1486
1487         if ( target && dn_match( &target->e_nname, edn ) ) {
1488                 e = target;
1489         } else {
1490                 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1491         } 
1492
1493         if ( e ) {
1494                 a = attr_find( e->e_attrs, entry_at );
1495                 if ( a ) {
1496                         BerVarray v;
1497
1498                         if ( op->o_conn && access_allowed( op,
1499                                 e, entry_at, NULL, ACL_AUTH,
1500                                 &acl_state ) == 0 ) {
1501                                 rc = LDAP_INSUFFICIENT_ACCESS;
1502                                 goto freeit;
1503                         }
1504
1505                         for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1506                         
1507                         v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1508                                 op->o_tmpmemctx );
1509                         for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1510                                 if ( op->o_conn && access_allowed( op,
1511                                         e, entry_at,
1512                                         &a->a_nvals[i],
1513                                         ACL_AUTH, &acl_state ) == 0 ) {
1514                                         continue;
1515                                 }
1516                                 ber_dupbv_x( &v[j],
1517                                         &a->a_nvals[i], op->o_tmpmemctx );
1518                                 if (v[j].bv_val ) j++;
1519                         }
1520                         if (j == 0) {
1521                                 op->o_tmpfree( v, op->o_tmpmemctx );
1522                                 *vals = NULL;
1523                                 rc = LDAP_INSUFFICIENT_ACCESS;
1524                         } else {
1525                                 v[j].bv_val = NULL;
1526                                 v[j].bv_len = 0;
1527                                 *vals = v;
1528                                 rc = LDAP_SUCCESS;
1529                         }
1530                 }
1531 freeit:         if (e != target ) {
1532                         be_entry_release_r( op, e );
1533                 }
1534         }
1535
1536         op->o_bd = be;
1537         return rc;
1538 }
1539
1540 int backend_operational(
1541         Operation *op,
1542         SlapReply *rs )
1543 {
1544         Attribute       **ap;
1545         int             rc = 0;
1546
1547         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1548                 /* just count them */ ;
1549
1550         /*
1551          * If operational attributes (allegedly) are required, 
1552          * and the backend supports specific operational attributes, 
1553          * add them to the attribute list
1554          */
1555         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1556                 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )) ) {
1557                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1558
1559                 ap = &(*ap)->a_next;
1560         }
1561
1562         if ( ( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) && op->o_bd &&
1563                 op->o_bd->be_operational != NULL )
1564         {
1565                 Attribute       *a;
1566                 
1567                 a = rs->sr_operational_attrs;
1568                 rs->sr_operational_attrs = NULL;
1569                 rc = op->o_bd->be_operational( op, rs );
1570                 *ap = rs->sr_operational_attrs;
1571                 if ( a != NULL ) {
1572                         rs->sr_operational_attrs = a;
1573                 }
1574
1575                 for ( ; *ap; ap = &(*ap)->a_next )
1576                         /* just count them */ ;
1577         }
1578
1579         return rc;
1580 }
1581