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