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