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