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