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