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