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