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