]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
02e41b82ad1c286bd960b76a67be9e6648b5b033
[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-2005 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 int                     nBackendInfo = 0;
60 BackendInfo             *backendInfo = NULL;
61
62 int                     nBackendDB = 0; 
63 BackendDB               *backendDB = NULL;
64
65 ldap_pvt_thread_pool_t  syncrepl_pool;
66 int                     syncrepl_pool_max = SLAP_MAX_SYNCREPL_THREADS;
67
68 static int
69 backend_init_controls( BackendInfo *bi )
70 {
71         if ( bi->bi_controls ) {
72                 int     i;
73
74                 for ( i = 0; bi->bi_controls[ i ]; i++ ) {
75                         int     cid;
76
77                         if ( slap_find_control_id( bi->bi_controls[ i ], &cid )
78                                         == LDAP_CONTROL_NOT_FOUND )
79                         {
80                                 if ( !( slapMode & SLAP_TOOL_MODE ) ) {
81                                         assert( 0 );
82                                 }
83
84                                 return -1;
85                         }
86
87                         bi->bi_ctrls[ cid ] = 1;
88                 }
89         }
90
91         return 0;
92 }
93
94 int backend_init(void)
95 {
96         int rc = -1;
97
98         ldap_pvt_thread_pool_init( &syncrepl_pool, syncrepl_pool_max, 0 );
99
100         if((nBackendInfo != 0) || (backendInfo != NULL)) {
101                 /* already initialized */
102                 Debug( LDAP_DEBUG_ANY,
103                         "backend_init: already initialized\n", 0, 0, 0 );
104                 return -1;
105         }
106
107         for( ;
108                 slap_binfo[nBackendInfo].bi_type != NULL;
109                 nBackendInfo++ )
110         {
111                 assert( slap_binfo[nBackendInfo].bi_init );
112
113                 rc = slap_binfo[nBackendInfo].bi_init( &slap_binfo[nBackendInfo] );
114
115                 if(rc != 0) {
116                         Debug( LDAP_DEBUG_ANY,
117                                 "backend_init: initialized for type \"%s\"\n",
118                                 slap_binfo[nBackendInfo].bi_type, 0, 0 );
119                         /* destroy those we've already inited */
120                         for( nBackendInfo--;
121                                 nBackendInfo >= 0 ;
122                                 nBackendInfo-- )
123                         { 
124                                 if ( slap_binfo[nBackendInfo].bi_destroy ) {
125                                         slap_binfo[nBackendInfo].bi_destroy(
126                                                 &slap_binfo[nBackendInfo] );
127                                 }
128                         }
129                         return rc;
130                 }
131         }
132
133         if ( nBackendInfo > 0) {
134                 backendInfo = slap_binfo;
135                 return 0;
136         }
137
138 #ifdef SLAPD_MODULES    
139         return 0;
140 #else
141
142         Debug( LDAP_DEBUG_ANY,
143                 "backend_init: failed\n",
144                 0, 0, 0 );
145
146         return rc;
147 #endif /* SLAPD_MODULES */
148 }
149
150 int backend_add(BackendInfo *aBackendInfo)
151 {
152         int rc = 0;
153
154         if ( aBackendInfo->bi_init == NULL ) {
155                 Debug( LDAP_DEBUG_ANY, "backend_add: "
156                         "backend type \"%s\" does not have the (mandatory)init function\n",
157                         aBackendInfo->bi_type, 0, 0 );
158                 return -1;
159         }
160
161         rc = aBackendInfo->bi_init(aBackendInfo);
162         if ( rc != 0) {
163                 Debug( LDAP_DEBUG_ANY,
164                         "backend_add:  initialization for type \"%s\" failed\n",
165                         aBackendInfo->bi_type, 0, 0 );
166                 return rc;
167         }
168
169         (void)backend_init_controls( aBackendInfo );
170
171         /* now add the backend type to the Backend Info List */
172         {
173                 BackendInfo *newBackendInfo = 0;
174
175                 /* if backendInfo == slap_binfo no deallocation of old backendInfo */
176                 if (backendInfo == slap_binfo) {
177                         newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
178                         AC_MEMCPY(newBackendInfo, backendInfo,
179                                 sizeof(BackendInfo) * nBackendInfo);
180                 } else {
181                         newBackendInfo = ch_realloc(backendInfo,
182                                 sizeof(BackendInfo) * (nBackendInfo + 1));
183                 }
184
185                 AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo,
186                         sizeof(BackendInfo));
187                 backendInfo = newBackendInfo;
188                 nBackendInfo++;
189                 return 0;
190         }
191 }
192
193 /* startup a specific backend database */
194 int backend_startup_one(Backend *be)
195 {
196         int             rc = 0;
197         BackendInfo     *bi = be->bd_info;
198
199         assert(be);
200
201         be->be_pending_csn_list = (struct be_pcl *)
202                 ch_calloc( 1, sizeof( struct be_pcl ));
203
204         LDAP_TAILQ_INIT( be->be_pending_csn_list );
205
206         /* back-relay takes care of itself; so may do other */
207         if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
208                 if ( overlay_is_over( be ) ) {
209                         bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
210                 }
211
212                 if ( bi->bi_controls ) {
213                         AC_MEMCPY( be->be_ctrls, bi->bi_ctrls, sizeof( be->be_ctrls ) );
214                 }
215
216                 be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
217         }
218
219         Debug( LDAP_DEBUG_TRACE,
220                 "backend_startup_one: starting \"%s\"\n",
221                 be->be_suffix ? be->be_suffix[0].bv_val : "(unknown)",
222                 0, 0 );
223         if ( be->bd_info->bi_db_open ) {
224                 rc = be->bd_info->bi_db_open( be );
225                 if ( rc != 0 ) {
226                         Debug( LDAP_DEBUG_ANY,
227                                 "backend_startup_one: bi_db_open failed! (%d)\n",
228                                 rc, 0, 0 );
229                 }
230         }
231
232         /* back-relay takes care of itself; so may do other */
233         bi = be->bd_info;
234         if ( overlay_is_over( be ) ) {
235                 bi = ((slap_overinfo *)be->bd_info->bi_private)->oi_orig;
236         }
237
238         if ( bi->bi_controls ) {
239                 if ( be->be_ctrls[ SLAP_MAX_CIDS ] == 0 ) {
240                         AC_MEMCPY( be->be_ctrls, bi->bi_ctrls, sizeof( be->be_ctrls ) );
241                         be->be_ctrls[ SLAP_MAX_CIDS ] = 1;
242
243                 } else {
244                         int     i;
245
246                         for ( i = 0; i < SLAP_MAX_CIDS; i++ ) {
247                                 if ( bi->bi_ctrls[ i ] ) {
248                                         be->be_ctrls[ i ] = 1;
249                                 }
250                         }
251                 }
252         }
253
254         return rc;
255 }
256
257 int backend_startup(Backend *be)
258 {
259         int i;
260         int rc = 0;
261
262         if( ! ( nBackendDB > 0 ) ) {
263                 /* no databases */
264                 Debug( LDAP_DEBUG_ANY,
265                         "backend_startup: %d databases to startup.\n",
266                         nBackendDB, 0, 0 );
267                 return 1;
268         }
269
270         if(be != NULL) {
271                 if ( be->bd_info->bi_open ) {
272                         rc = be->bd_info->bi_open( be->bd_info );
273                         if ( rc != 0 ) {
274                                 Debug( LDAP_DEBUG_ANY,
275                                         "backend_startup: bi_open failed!\n",
276                                         0, 0, 0 );
277
278                                 return rc;
279                         }
280                 }
281
282                 return backend_startup_one( be );
283         }
284
285         /* open frontend, if required */
286         if ( frontendDB->bd_info->bi_db_open ) {
287                 rc = frontendDB->bd_info->bi_db_open( frontendDB );
288                 if ( rc != 0 ) {
289                         Debug( LDAP_DEBUG_ANY,
290                                 "backend_startup: bi_db_open(frontend) failed! (%d)\n",
291                                 rc, 0, 0 );
292                         return rc;
293                 }
294         }
295
296         /* open each backend type */
297         for( i = 0; i < nBackendInfo; i++ ) {
298                 if( backendInfo[i].bi_nDB == 0) {
299                         /* no database of this type, don't open */
300                         continue;
301                 }
302
303                 if( backendInfo[i].bi_open ) {
304                         rc = backendInfo[i].bi_open( &backendInfo[i] );
305                         if ( rc != 0 ) {
306                                 Debug( LDAP_DEBUG_ANY,
307                                         "backend_startup: bi_open %d failed!\n",
308                                         i, 0, 0 );
309                                 return rc;
310                         }
311                 }
312
313                 (void)backend_init_controls( &backendInfo[i] );
314         }
315
316         ldap_pvt_thread_mutex_init( &slapd_rq.rq_mutex );
317         LDAP_STAILQ_INIT( &slapd_rq.task_list );
318         LDAP_STAILQ_INIT( &slapd_rq.run_list );
319
320         /* open each backend database */
321         for( i = 0; i < nBackendDB; i++ ) {
322                 if ( backendDB[i].be_suffix == NULL ) {
323                         Debug( LDAP_DEBUG_ANY,
324                                 "backend_startup: warning, database %d (%s) "
325                                 "has no suffix\n",
326                                 i, backendDB[i].bd_info->bi_type, 0 );
327                 }
328                 /* append global access controls */
329                 acl_append( &backendDB[i].be_acl, frontendDB->be_acl );
330
331                 rc = backend_startup_one( &backendDB[i] );
332
333                 if ( rc ) return rc;
334
335
336                 if ( backendDB[i].be_syncinfo ) {
337                         syncinfo_t *si;
338
339                         if ( !( backendDB[i].be_search && backendDB[i].be_add &&
340                                 backendDB[i].be_modify && backendDB[i].be_delete )) {
341                                 Debug( LDAP_DEBUG_ANY,
342                                         "backend_startup: database(%d) does not support "
343                                         "operations required for syncrepl", i, 0, 0 );
344                                 continue;
345                         }
346
347                         {
348                                 si = backendDB[i].be_syncinfo;
349                                 si->si_be = &backendDB[i];
350                                 init_syncrepl( si );
351                                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
352                                 ldap_pvt_runqueue_insert( &slapd_rq,
353                                                 si->si_interval, do_syncrepl, (void *) si );
354                                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
355                         }
356                 }
357         }
358
359         return rc;
360 }
361
362 int backend_num( Backend *be )
363 {
364         int i;
365
366         if( be == NULL ) return -1;
367
368         for( i = 0; i < nBackendDB; i++ ) {
369                 if( be == &backendDB[i] ) return i;
370         }
371         return -1;
372 }
373
374 int backend_shutdown( Backend *be )
375 {
376         int i;
377         int rc = 0;
378
379         if( be != NULL ) {
380                 /* shutdown a specific backend database */
381
382                 if ( be->bd_info->bi_nDB == 0 ) {
383                         /* no database of this type, we never opened it */
384                         return 0;
385                 }
386
387                 if ( be->bd_info->bi_db_close ) {
388                         be->bd_info->bi_db_close( be );
389                 }
390
391                 if( be->bd_info->bi_close ) {
392                         be->bd_info->bi_close( be->bd_info );
393                 }
394
395                 return 0;
396         }
397
398         /* close each backend database */
399         for( i = 0; i < nBackendDB; i++ ) {
400                 if ( backendDB[i].bd_info->bi_db_close ) {
401                         backendDB[i].bd_info->bi_db_close(
402                                 &backendDB[i] );
403                 }
404
405                 if(rc != 0) {
406                         Debug( LDAP_DEBUG_ANY,
407                                 "backend_close: bi_db_close %s failed!\n",
408                                 backendDB[i].be_type, 0, 0 );
409                 }
410         }
411
412         /* close each backend type */
413         for( i = 0; i < nBackendInfo; i++ ) {
414                 if( backendInfo[i].bi_nDB == 0 ) {
415                         /* no database of this type */
416                         continue;
417                 }
418
419                 if( backendInfo[i].bi_close ) {
420                         backendInfo[i].bi_close(
421                                 &backendInfo[i] );
422                 }
423         }
424
425         /* close frontend, if required */
426         if ( frontendDB->bd_info->bi_db_close ) {
427                 rc = frontendDB->bd_info->bi_db_close ( frontendDB );
428                 if ( rc != 0 ) {
429                         Debug( LDAP_DEBUG_ANY,
430                                 "backend_startup: bi_db_close(frontend) failed! (%d)\n",
431                                 rc, 0, 0 );
432                 }
433         }
434
435         return 0;
436 }
437
438 int backend_destroy(void)
439 {
440         int i;
441         BackendDB *bd;
442         struct slap_csn_entry *csne;
443
444         ldap_pvt_thread_pool_destroy( &syncrepl_pool, 1 );
445
446         /* destroy each backend database */
447         for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
448
449                 if ( bd->be_syncinfo ) {
450                         syncinfo_free( bd->be_syncinfo );
451                 }
452
453                 if ( bd->be_pending_csn_list ) {
454                         csne = LDAP_TAILQ_FIRST( bd->be_pending_csn_list );
455                         while ( csne ) {
456                                 struct slap_csn_entry *tmp_csne = csne;
457
458                                 LDAP_TAILQ_REMOVE( bd->be_pending_csn_list, csne, ce_csn_link );
459                                 ch_free( csne->ce_csn.bv_val );
460                                 csne = LDAP_TAILQ_NEXT( csne, ce_csn_link );
461                                 ch_free( tmp_csne );
462                         }
463                 }
464                 
465                 if ( bd->bd_info->bi_db_destroy ) {
466                         bd->bd_info->bi_db_destroy( bd );
467                 }
468                 ber_bvarray_free( bd->be_suffix );
469                 ber_bvarray_free( bd->be_nsuffix );
470                 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
471                         free( bd->be_rootdn.bv_val );
472                 }
473                 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
474                         free( bd->be_rootndn.bv_val );
475                 }
476                 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
477                         free( bd->be_rootpw.bv_val );
478                 }
479                 acl_destroy( bd->be_acl, frontendDB->be_acl );
480         }
481         free( backendDB );
482
483         /* destroy each backend type */
484         for( i = 0; i < nBackendInfo; i++ ) {
485                 if( backendInfo[i].bi_destroy ) {
486                         backendInfo[i].bi_destroy(
487                                 &backendInfo[i] );
488                 }
489         }
490
491 #ifdef SLAPD_MODULES
492         if (backendInfo != slap_binfo) {
493            free(backendInfo);
494         }
495 #endif /* SLAPD_MODULES */
496
497         nBackendInfo = 0;
498         backendInfo = NULL;
499
500         /* destroy frontend database */
501         bd = frontendDB;
502         if ( bd ) {
503                 if ( bd->bd_info->bi_db_destroy ) {
504                         bd->bd_info->bi_db_destroy( bd );
505                 }
506                 ber_bvarray_free( bd->be_suffix );
507                 ber_bvarray_free( bd->be_nsuffix );
508                 if ( !BER_BVISNULL( &bd->be_rootdn ) ) {
509                         free( bd->be_rootdn.bv_val );
510                 }
511                 if ( !BER_BVISNULL( &bd->be_rootndn ) ) {
512                         free( bd->be_rootndn.bv_val );
513                 }
514                 if ( !BER_BVISNULL( &bd->be_rootpw ) ) {
515                         free( bd->be_rootpw.bv_val );
516                 }
517                 acl_destroy( bd->be_acl, frontendDB->be_acl );
518         }
519
520         return 0;
521 }
522
523 BackendInfo* backend_info(const char *type)
524 {
525         int i;
526
527         /* search for the backend type */
528         for( i = 0; i < nBackendInfo; i++ ) {
529                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
530                         return &backendInfo[i];
531                 }
532         }
533
534         return NULL;
535 }
536
537
538 BackendDB *
539 backend_db_init(
540     const char  *type )
541 {
542         Backend *be;
543         BackendInfo *bi = backend_info(type);
544         int     rc = 0;
545
546         if( bi == NULL ) {
547                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
548                 return NULL;
549         }
550
551         be = backendDB;
552
553         backendDB = (BackendDB *) ch_realloc(
554                         (char *) backendDB,
555                     (nBackendDB + 1) * sizeof(Backend) );
556
557         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
558
559         /* did realloc move our table? if so, fix up dependent pointers */
560         if ( be != backendDB ) {
561                 int i;
562                 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
563                         be->be_pcl_mutexp = &be->be_pcl_mutex;
564                 }
565         }
566
567         be = &backends[nbackends++];
568
569         be->bd_info = bi;
570
571         be->be_def_limit = frontendDB->be_def_limit;
572         be->be_dfltaccess = frontendDB->be_dfltaccess;
573
574         be->be_restrictops = frontendDB->be_restrictops;
575         be->be_requires = frontendDB->be_requires;
576         be->be_ssf_set = frontendDB->be_ssf_set;
577
578         be->be_pcl_mutexp = &be->be_pcl_mutex;
579         ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
580
581         /* assign a default depth limit for alias deref */
582         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
583
584         if ( bi->bi_db_init ) {
585                 rc = bi->bi_db_init( be );
586         }
587
588         if ( rc != 0 ) {
589                 fprintf( stderr, "database init failed (%s)\n", type );
590                 nbackends--;
591                 return NULL;
592         }
593
594         bi->bi_nDB++;
595         return( be );
596 }
597
598 void
599 be_db_close( void )
600 {
601         int     i;
602
603         for ( i = 0; i < nbackends; i++ ) {
604                 if ( backends[i].bd_info->bi_db_close ) {
605                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
606                 }
607         }
608
609         if ( frontendDB->bd_info->bi_db_close ) {
610                 (*frontendDB->bd_info->bi_db_close)( frontendDB );
611         }
612
613 }
614
615 Backend *
616 select_backend(
617         struct berval * dn,
618         int manageDSAit,
619         int noSubs )
620 {
621         int             i, j;
622         ber_len_t       len, dnlen = dn->bv_len;
623         Backend         *be = NULL;
624
625         for ( i = 0; i < nbackends; i++ ) {
626                 if ( backends[i].be_nsuffix == NULL ) {
627                         continue;
628                 }
629
630                 for ( j = 0; !BER_BVISNULL( &backends[i].be_nsuffix[j] ); j++ )
631                 {
632                         if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
633                                 && noSubs )
634                         {
635                                 continue;
636                         }
637
638                         len = backends[i].be_nsuffix[j].bv_len;
639
640                         if ( len > dnlen ) {
641                                 /* suffix is longer than DN */
642                                 continue;
643                         }
644                         
645                         /*
646                          * input DN is normalized, so the separator check
647                          * need not look at escaping
648                          */
649                         if ( len && len < dnlen &&
650                                 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
651                         {
652                                 continue;
653                         }
654
655                         if ( strcmp( backends[i].be_nsuffix[j].bv_val,
656                                 &dn->bv_val[dnlen-len] ) == 0 )
657                         {
658                                 if( be == NULL ) {
659                                         be = &backends[i];
660
661                                         if( manageDSAit && len == dnlen &&
662                                                 !SLAP_GLUE_SUBORDINATE( be ) ) {
663                                                 continue;
664                                         }
665                                 } else {
666                                         be = &backends[i];
667                                 }
668                                 return be;
669                         }
670                 }
671         }
672
673         return be;
674 }
675
676 int
677 be_issuffix(
678     Backend *be,
679     struct berval *bvsuffix )
680 {
681         int     i;
682
683         if ( be->be_nsuffix == NULL ) {
684                 return 0;
685         }
686
687         for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
688                 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
689                         return 1;
690                 }
691         }
692
693         return 0;
694 }
695
696 int
697 be_isroot_dn( Backend *be, struct berval *ndn )
698 {
699         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
700                 return 0;
701         }
702
703         return dn_match( &be->be_rootndn, ndn );
704 }
705
706 int
707 be_slurp_update( Operation *op )
708 {
709         return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
710                 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
711 }
712
713 int
714 be_shadow_update( Operation *op )
715 {
716         return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
717                 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
718 }
719
720 int
721 be_isupdate_dn( Backend *be, struct berval *ndn )
722 {
723         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
724                 return 0;
725         }
726
727         return dn_match( &be->be_update_ndn, ndn );
728 }
729
730 struct berval *
731 be_root_dn( Backend *be )
732 {
733         return &be->be_rootdn;
734 }
735
736 int
737 be_isroot( Operation *op )
738 {
739         return be_isroot_dn( op->o_bd, &op->o_ndn );
740 }
741
742 int
743 be_isroot_pw( Operation *op )
744 {
745         int result;
746
747         if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
748                 return 0;
749         }
750
751         if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
752                 return 0;
753         }
754
755 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
756         ldap_pvt_thread_mutex_lock( &passwd_mutex );
757 #ifdef SLAPD_SPASSWD
758         lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
759 #endif
760 #endif
761
762         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
763
764 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
765 #ifdef SLAPD_SPASSWD
766         lutil_passwd_sasl_conn = NULL;
767 #endif
768         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
769 #endif
770
771         return result == 0;
772 }
773
774 int
775 be_entry_release_rw(
776         Operation *op,
777         Entry *e,
778         int rw )
779 {
780         if ( op->o_bd->be_release ) {
781                 /* free and release entry from backend */
782                 return op->o_bd->be_release( op, e, rw );
783         } else {
784                 /* free entry */
785                 entry_free( e );
786                 return 0;
787         }
788 }
789
790 int
791 backend_unbind( Operation *op, SlapReply *rs )
792 {
793         int             i;
794
795         for ( i = 0; i < nbackends; i++ ) {
796 #if defined( LDAP_SLAPI )
797                 if ( op->o_pb ) {
798                         int rc;
799                         if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
800                         slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
801                         rc = slapi_int_call_plugins( &backends[i],
802                                 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
803                         if ( rc < 0 ) {
804                                 /*
805                                  * A preoperation plugin failure will abort the
806                                  * entire operation.
807                                  */
808                                 Debug(LDAP_DEBUG_TRACE,
809                                         "do_bind: Unbind preoperation plugin failed\n",
810                                         0, 0, 0);
811                                 return 0;
812                         }
813                 }
814 #endif /* defined( LDAP_SLAPI ) */
815
816                 if ( backends[i].be_unbind ) {
817                         op->o_bd = &backends[i];
818                         (*backends[i].be_unbind)( op, rs );
819                 }
820
821 #if defined( LDAP_SLAPI )
822                 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
823                         SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
824                 {
825                         Debug(LDAP_DEBUG_TRACE,
826                                 "do_unbind: Unbind postoperation plugins failed\n",
827                                 0, 0, 0);
828                 }
829 #endif /* defined( LDAP_SLAPI ) */
830         }
831
832         return 0;
833 }
834
835 int
836 backend_connection_init(
837         Connection   *conn )
838 {
839         int     i;
840
841         for ( i = 0; i < nbackends; i++ ) {
842                 if ( backends[i].be_connection_init ) {
843                         (*backends[i].be_connection_init)( &backends[i], conn);
844                 }
845         }
846
847         return 0;
848 }
849
850 int
851 backend_connection_destroy(
852         Connection   *conn )
853 {
854         int     i;
855
856         for ( i = 0; i < nbackends; i++ ) {
857                 if ( backends[i].be_connection_destroy ) {
858                         (*backends[i].be_connection_destroy)( &backends[i], conn);
859                 }
860         }
861
862         return 0;
863 }
864
865 static int
866 backend_check_controls(
867         Operation *op,
868         SlapReply *rs )
869 {
870         LDAPControl **ctrls = op->o_ctrls;
871         rs->sr_err = LDAP_SUCCESS;
872
873         if( ctrls ) {
874                 for( ; *ctrls != NULL ; ctrls++ ) {
875                         int cid;
876
877                         switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
878                         case LDAP_CONTROL_NOT_FOUND:
879                                 /* unrecognized control */ 
880                                 if ( (*ctrls)->ldctl_iscritical ) {
881                                         /* should not be reachable */ 
882                                         Debug( LDAP_DEBUG_ANY,
883                                                 "backend_check_controls: unrecognized control: %s\n",
884                                                 (*ctrls)->ldctl_oid, 0, 0 );
885                                         assert( 0 );
886                                 }
887                                 break;
888
889                         case LDAP_COMPARE_FALSE:
890                                 if ( !op->o_bd->be_ctrls[ cid ] )
891                                 {
892                                         /* Per RFC 2251 (and LDAPBIS discussions), if the control
893                                          * is recognized and appropriate for the operation (which
894                                          * we've already verified), then the server should make
895                                          * use of the control when performing the operation.
896                                          * 
897                                          * Here we find that operation extended by the control
898                                          * is not unavailable in a particular context, hence the
899                                          * return of unwillingToPerform.
900                                          */
901                                         rs->sr_text = "control unavailable in context";
902                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
903                                         goto done;
904                                 }
905                                 break;
906
907                         case LDAP_COMPARE_TRUE:
908                                 break;
909
910                         default:
911                                 /* unreachable */
912                                 rs->sr_err = "unable to check control";
913                                 rs->sr_err = LDAP_OTHER;
914                                 goto done;
915                         }
916                 }
917         }
918
919 done:;
920         return rs->sr_err;
921 }
922
923 int
924 backend_check_restrictions(
925         Operation *op,
926         SlapReply *rs,
927         struct berval *opdata )
928 {
929         slap_mask_t restrictops;
930         slap_mask_t requires;
931         slap_mask_t opflag;
932         slap_mask_t exopflag = 0;
933         slap_ssf_set_t *ssf;
934         int updateop = 0;
935         int starttls = 0;
936         int session = 0;
937
938         if ( op->o_bd ) {
939                 int     rc = SLAP_CB_CONTINUE;
940
941                 if ( op->o_bd->be_chk_controls ) {
942                         rc = ( *op->o_bd->be_chk_controls )( op, rs );
943                 }
944
945                 if ( rc == SLAP_CB_CONTINUE ) {
946                         rc = backend_check_controls( op, rs );
947                 }
948
949                 if ( rc != LDAP_SUCCESS ) {
950                         return rs->sr_err;
951                 }
952
953                 restrictops = op->o_bd->be_restrictops;
954                 requires = op->o_bd->be_requires;
955                 ssf = &op->o_bd->be_ssf_set;
956
957         } else {
958                 restrictops = frontendDB->be_restrictops;
959                 requires = frontendDB->be_requires;
960                 ssf = &frontendDB->be_ssf_set;
961         }
962
963         switch( op->o_tag ) {
964         case LDAP_REQ_ADD:
965                 opflag = SLAP_RESTRICT_OP_ADD;
966                 updateop++;
967                 break;
968         case LDAP_REQ_BIND:
969                 opflag = SLAP_RESTRICT_OP_BIND;
970                 session++;
971                 break;
972         case LDAP_REQ_COMPARE:
973                 opflag = SLAP_RESTRICT_OP_COMPARE;
974                 break;
975         case LDAP_REQ_DELETE:
976                 updateop++;
977                 opflag = SLAP_RESTRICT_OP_DELETE;
978                 break;
979         case LDAP_REQ_EXTENDED:
980                 opflag = SLAP_RESTRICT_OP_EXTENDED;
981
982                 if( !opdata ) {
983                         /* treat unspecified as a modify */
984                         opflag = SLAP_RESTRICT_OP_MODIFY;
985                         updateop++;
986                         break;
987                 }
988
989                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
990                         session++;
991                         starttls++;
992                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
993                         break;
994                 }
995
996                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
997                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
998                         break;
999                 }
1000
1001                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
1002                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
1003                         break;
1004                 }
1005
1006                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
1007                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
1008                         updateop++;
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                                 BER_BVISEMPTY( &op->o_ndn ) )
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 & ( !BER_BVISEMPTY( &op->o_ndn ) ? 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                                 || BER_BVISEMPTY( &op->o_dn ) )
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 || BER_BVISEMPTY( &op->o_dn ) ) {
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( BER_BVISEMPTY( &op->o_dn ) ) {
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 && BER_BVISEMPTY( &op->o_dn ) ) {
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                                         ( !BER_BVISEMPTY( &op->o_dn ) ? 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                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1216                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1217                         rs->sr_text = "read operations restricted";
1218                 } else if ( restrictops & exopflag ) {
1219                         rs->sr_text = "extended operation restricted";
1220                 } else {
1221                         rs->sr_text = "operation restricted";
1222                 }
1223                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1224                 return rs->sr_err;
1225         }
1226
1227         rs->sr_err = LDAP_SUCCESS;
1228         return rs->sr_err;
1229 }
1230
1231 int backend_check_referrals( Operation *op, SlapReply *rs )
1232 {
1233         rs->sr_err = LDAP_SUCCESS;
1234
1235         if( op->o_bd->be_chk_referrals ) {
1236                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1237
1238                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1239                         send_ldap_result( op, rs );
1240                 }
1241         }
1242
1243         return rs->sr_err;
1244 }
1245
1246 int
1247 be_entry_get_rw(
1248         Operation *op,
1249         struct berval *ndn,
1250         ObjectClass *oc,
1251         AttributeDescription *at,
1252         int rw,
1253         Entry **e )
1254 {
1255         int rc;
1256
1257         *e = NULL;
1258
1259         if (op->o_bd == NULL) {
1260                 rc = LDAP_NO_SUCH_OBJECT;
1261         } else if ( op->o_bd->be_fetch ) {
1262                 rc = ( op->o_bd->be_fetch )( op, ndn,
1263                         oc, at, rw, e );
1264         } else {
1265                 rc = LDAP_UNWILLING_TO_PERFORM;
1266         }
1267         return rc;
1268 }
1269
1270 int 
1271 backend_group(
1272         Operation *op,
1273         Entry   *target,
1274         struct berval *gr_ndn,
1275         struct berval *op_ndn,
1276         ObjectClass *group_oc,
1277         AttributeDescription *group_at )
1278 {
1279         Entry *e;
1280         Attribute *a;
1281         int rc;
1282         GroupAssertion *g;
1283         Backend *be = op->o_bd;
1284
1285         if ( op->o_abandon ) return SLAPD_ABANDON;
1286
1287         op->o_bd = select_backend( gr_ndn, 0, 0 );
1288
1289         for ( g = op->o_groups; g; g = g->ga_next ) {
1290                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1291                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1292                 {
1293                         continue;
1294                 }
1295                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1296                         break;
1297                 }
1298         }
1299
1300         if ( g ) {
1301                 rc = g->ga_res;
1302                 goto done;
1303         }
1304
1305         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1306                 e = target;
1307                 rc = 0;
1308         } else {
1309                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1310         }
1311         if ( e ) {
1312 #ifdef LDAP_SLAPI
1313                 if ( op->o_pb != NULL ) {
1314                         init_group_pblock( op, target, e, op_ndn, group_at );
1315
1316                         rc = call_group_preop_plugins( op );
1317                         if ( rc == LDAP_SUCCESS ) {
1318                                 goto done;
1319                         }
1320                 }
1321 #endif /* LDAP_SLAPI */
1322
1323                 a = attr_find( e->e_attrs, group_at );
1324                 if ( a ) {
1325                         /* If the attribute is a subtype of labeledURI, treat this as
1326                          * a dynamic group ala groupOfURLs
1327                          */
1328                         if (is_at_subtype( group_at->ad_type,
1329                                 slap_schema.si_ad_labeledURI->ad_type ) )
1330                         {
1331                                 int i;
1332                                 LDAPURLDesc *ludp;
1333                                 struct berval bv, nbase;
1334                                 Filter *filter;
1335                                 Entry *user;
1336                                 Backend *b2 = op->o_bd;
1337
1338                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1339                                         user = target;
1340                                 } else {
1341                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1342                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1343                                 }
1344                                 
1345                                 if ( rc == 0 ) {
1346                                         rc = 1;
1347                                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1348                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1349                                                         LDAP_URL_SUCCESS )
1350                                                 {
1351                                                         continue;
1352                                                 }
1353                                                 BER_BVZERO( &nbase );
1354                                                 /* host part must be empty */
1355                                                 /* attrs and extensions parts must be empty */
1356                                                 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1357                                                         ludp->lud_attrs || ludp->lud_exts )
1358                                                 {
1359                                                         goto loopit;
1360                                                 }
1361                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1362                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1363                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1364                                                 {
1365                                                         goto loopit;
1366                                                 }
1367                                                 switch ( ludp->lud_scope ) {
1368                                                 case LDAP_SCOPE_BASE:
1369                                                         if ( !dn_match( &nbase, op_ndn ) ) {
1370                                                                 goto loopit;
1371                                                         }
1372                                                         break;
1373                                                 case LDAP_SCOPE_ONELEVEL:
1374                                                         dnParent( op_ndn, &bv );
1375                                                         if ( !dn_match( &nbase, &bv ) ) {
1376                                                                 goto loopit;
1377                                                         }
1378                                                         break;
1379                                                 case LDAP_SCOPE_SUBTREE:
1380                                                         if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1381                                                                 goto loopit;
1382                                                         }
1383                                                         break;
1384 #ifdef LDAP_SCOPE_SUBORDINATE
1385                                                 case LDAP_SCOPE_SUBORDINATE:
1386                                                         if ( dn_match( &nbase, op_ndn ) ||
1387                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1388                                                         {
1389                                                                 goto loopit;
1390                                                         }
1391 #endif
1392                                                 }
1393                                                 filter = str2filter_x( op, ludp->lud_filter );
1394                                                 if ( filter ) {
1395                                                         if ( test_filter( NULL, user, filter ) ==
1396                                                                 LDAP_COMPARE_TRUE )
1397                                                         {
1398                                                                 rc = 0;
1399                                                         }
1400                                                         filter_free_x( op, filter );
1401                                                 }
1402 loopit:
1403                                                 ldap_free_urldesc( ludp );
1404                                                 if ( !BER_BVISNULL( &nbase ) ) {
1405                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1406                                                 }
1407                                                 if ( rc == 0 ) break;
1408                                         }
1409                                         if ( user != target ) {
1410                                                 be_entry_release_r( op, user );
1411                                         }
1412                                 }
1413                                 op->o_bd = b2;
1414                         } else {
1415                                 rc = value_find_ex( group_at,
1416                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1417                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1418                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1419                         }
1420                 } else {
1421                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1422                 }
1423                 if (e != target ) {
1424                         be_entry_release_r( op, e );
1425                 }
1426         } else {
1427                 rc = LDAP_NO_SUCH_OBJECT;
1428         }
1429
1430 #ifdef LDAP_SLAPI
1431         if ( op->o_pb ) call_group_postop_plugins( op );
1432 #endif /* LDAP_SLAPI */
1433
1434         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1435                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1436                         op->o_tmpmemctx );
1437                 g->ga_be = op->o_bd;
1438                 g->ga_oc = group_oc;
1439                 g->ga_at = group_at;
1440                 g->ga_res = rc;
1441                 g->ga_len = gr_ndn->bv_len;
1442                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1443                 g->ga_next = op->o_groups;
1444                 op->o_groups = g;
1445         }
1446 done:
1447         op->o_bd = be;
1448         return rc;
1449 }
1450
1451 #ifdef LDAP_SLAPI
1452 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1453 {
1454         BerVarray v;
1455         int rc;
1456         BerVarray *vals = (BerVarray *)c->cac_private;
1457         Operation *op = NULL;
1458         int i, j;
1459
1460         slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1461         if ( op == NULL ) {
1462                 return 1;
1463         }
1464
1465         if ( op->o_conn && access_allowed( op,
1466                 e, a->a_desc, NULL, ACL_AUTH,
1467                 &c->cac_acl_state ) == 0 ) {
1468                 return 1;
1469         }
1470
1471         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) ;
1472                         
1473         v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1474                 op->o_tmpmemctx );
1475         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1476                 if ( op->o_conn && access_allowed( op,
1477                         e, a->a_desc,
1478                         &a->a_nvals[i],
1479                         ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1480                         continue;
1481                 }
1482                 ber_dupbv_x( &v[j],
1483                         &a->a_nvals[i], op->o_tmpmemctx );
1484                 if ( !BER_BVISNULL( &v[j] ) ) {
1485                         j++;
1486                 }
1487         }
1488
1489         if ( j == 0 ) {
1490                 op->o_tmpfree( v, op->o_tmpmemctx );
1491                 *vals = NULL;
1492                 rc = 1;
1493         } else {
1494                 BER_BVZERO( &v[j] );
1495                 *vals = v;
1496                 rc = 0;
1497         }
1498
1499         return rc;
1500 }
1501 #endif /* LDAP_SLAPI */
1502
1503 int 
1504 backend_attribute(
1505         Operation *op,
1506         Entry   *target,
1507         struct berval   *edn,
1508         AttributeDescription *entry_at,
1509         BerVarray *vals,
1510         slap_access_t access )
1511 {
1512         Entry                   *e = NULL;
1513         Attribute               *a = NULL;
1514         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1515         AccessControlState      acl_state = ACL_STATE_INIT;
1516         Backend                 *be = op->o_bd;
1517
1518         op->o_bd = select_backend( edn, 0, 0 );
1519
1520         if ( target && dn_match( &target->e_nname, edn ) ) {
1521                 e = target;
1522
1523         } else {
1524                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1525         } 
1526
1527         if ( e ) {
1528                 a = attr_find( e->e_attrs, entry_at );
1529                 if ( a == NULL ) {
1530                         SlapReply       rs = { 0 };
1531                         AttributeName   anlist[ 2 ];
1532
1533                         anlist[ 0 ].an_name = entry_at->ad_cname;
1534                         anlist[ 0 ].an_desc = entry_at;
1535                         BER_BVZERO( &anlist[ 1 ].an_name );
1536                         rs.sr_attrs = anlist;
1537                         
1538                         /* NOTE: backend_operational() is also called
1539                          * when returning results, so it's supposed
1540                          * to do no harm to entries */
1541                         rs.sr_entry = e;
1542                         rc = backend_operational( op, &rs );
1543                         rs.sr_entry = NULL;
1544  
1545                         if ( rc == LDAP_SUCCESS ) {
1546                                 if ( rs.sr_operational_attrs ) {
1547                                         freeattr = 1;
1548                                         a = rs.sr_operational_attrs;
1549
1550                                 } else {
1551                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1552                                 }
1553                         }
1554                 }
1555
1556                 if ( a ) {
1557                         BerVarray v;
1558
1559                         if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1560                                 e, entry_at, NULL, access,
1561                                 &acl_state ) == 0 ) {
1562                                 rc = LDAP_INSUFFICIENT_ACCESS;
1563                                 goto freeit;
1564                         }
1565
1566                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1567                                 ;
1568                         
1569                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1570                                 op->o_tmpmemctx );
1571                         for ( i = 0,j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1572                         {
1573                                 if ( op->o_conn && access > ACL_NONE && 
1574                                                 access_allowed( op, e,
1575                                                         entry_at,
1576                                                         &a->a_nvals[i],
1577                                                         access,
1578                                                         &acl_state ) == 0 )
1579                                 {
1580                                         continue;
1581                                 }
1582                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1583                                                 op->o_tmpmemctx );
1584                                 if ( !BER_BVISNULL( &v[j] ) ) {
1585                                         j++;
1586                                 }
1587                         }
1588                         if ( j == 0 ) {
1589                                 op->o_tmpfree( v, op->o_tmpmemctx );
1590                                 *vals = NULL;
1591                                 rc = LDAP_INSUFFICIENT_ACCESS;
1592
1593                         } else {
1594                                 BER_BVZERO( &v[j] );
1595                                 *vals = v;
1596                                 rc = LDAP_SUCCESS;
1597                         }
1598                 }
1599 #ifdef LDAP_SLAPI
1600                 else if ( op->o_pb ) {
1601                         /* try any computed attributes */
1602                         computed_attr_context   ctx;
1603
1604                         slapi_int_pblock_set_operation( op->o_pb, op );
1605
1606                         ctx.cac_pb = op->o_pb;
1607                         ctx.cac_attrs = NULL;
1608                         ctx.cac_userattrs = 0;
1609                         ctx.cac_opattrs = 0;
1610                         ctx.cac_acl_state = acl_state;
1611                         ctx.cac_private = (void *)vals;
1612
1613                         rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr );
1614                         if ( rc == 1 ) {
1615                                 rc = LDAP_INSUFFICIENT_ACCESS;
1616
1617                         } else {
1618                                 rc = LDAP_SUCCESS;
1619                         }
1620                 }
1621 #endif /* LDAP_SLAPI */
1622 freeit:         if ( e != target ) {
1623                         be_entry_release_r( op, e );
1624                 }
1625                 if ( freeattr ) {
1626                         attr_free( a );
1627                 }
1628         }
1629
1630         op->o_bd = be;
1631         return rc;
1632 }
1633
1634 #ifdef LDAP_SLAPI
1635 static int backend_compute_output_attr_access(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1636 {
1637         struct berval   *nval = (struct berval *)c->cac_private;
1638         Operation       *op = NULL;
1639
1640         slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1641         if ( op == NULL ) {
1642                 return 1;
1643         }
1644
1645         return access_allowed( op, e, a->a_desc, nval, ACL_AUTH, NULL ) == 0;
1646 }
1647 #endif /* LDAP_SLAPI */
1648
1649 int 
1650 backend_access(
1651         Operation               *op,
1652         Entry                   *target,
1653         struct berval           *edn,
1654         AttributeDescription    *entry_at,
1655         struct berval           *nval,
1656         slap_access_t           access,
1657         slap_mask_t             *mask )
1658 {
1659         Entry           *e = NULL;
1660         int             rc = LDAP_INSUFFICIENT_ACCESS;
1661         Backend         *be = op->o_bd;
1662
1663         /* pedantic */
1664         assert( op );
1665         assert( op->o_conn );
1666         assert( edn );
1667         assert( access > ACL_NONE );
1668
1669         op->o_bd = select_backend( edn, 0, 0 );
1670
1671         if ( target && dn_match( &target->e_nname, edn ) ) {
1672                 e = target;
1673
1674         } else {
1675                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1676         } 
1677
1678         if ( e ) {
1679                 Attribute       *a = NULL;
1680                 int             freeattr = 0;
1681
1682                 if ( entry_at == NULL ) {
1683                         entry_at = slap_schema.si_ad_entry;
1684                 }
1685
1686                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1687                 {
1688                         if ( access_allowed_mask( op, e, entry_at,
1689                                         NULL, access, NULL, mask ) == 0 )
1690                         {
1691                                 rc = LDAP_INSUFFICIENT_ACCESS;
1692
1693                         } else {
1694                                 rc = LDAP_SUCCESS;
1695                         }
1696
1697                 } else {
1698                         a = attr_find( e->e_attrs, entry_at );
1699                         if ( a == NULL ) {
1700                                 SlapReply       rs = { 0 };
1701                                 AttributeName   anlist[ 2 ];
1702
1703                                 anlist[ 0 ].an_name = entry_at->ad_cname;
1704                                 anlist[ 0 ].an_desc = entry_at;
1705                                 BER_BVZERO( &anlist[ 1 ].an_name );
1706                                 rs.sr_attrs = anlist;
1707                         
1708                                 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1709
1710                                 /* NOTE: backend_operational() is also called
1711                                  * when returning results, so it's supposed
1712                                  * to do no harm to entries */
1713                                 rs.sr_entry = e;
1714                                 rc = backend_operational( op, &rs );
1715                                 rs.sr_entry = NULL;
1716
1717                                 if ( rc == LDAP_SUCCESS ) {
1718                                         if ( rs.sr_operational_attrs ) {
1719                                                 freeattr = 1;
1720                                                 a = rs.sr_operational_attrs;
1721
1722                                         } else {
1723                                                 rc = LDAP_NO_SUCH_OBJECT;
1724                                         }
1725                                 }
1726                         }
1727
1728                         if ( a ) {
1729                                 if ( access_allowed_mask( op, e, entry_at,
1730                                                 nval, access, NULL, mask ) == 0 )
1731                                 {
1732                                         rc = LDAP_INSUFFICIENT_ACCESS;
1733                                         goto freeit;
1734                                 }
1735                                 rc = LDAP_SUCCESS;
1736                         }
1737 #ifdef LDAP_SLAPI
1738                         else if ( op->o_pb ) {
1739                                 /* try any computed attributes */
1740                                 computed_attr_context   ctx;
1741
1742                                 slapi_int_pblock_set_operation( op->o_pb, op );
1743
1744                                 ctx.cac_pb = op->o_pb;
1745                                 ctx.cac_attrs = NULL;
1746                                 ctx.cac_userattrs = 0;
1747                                 ctx.cac_opattrs = 0;
1748                                 ctx.cac_private = (void *)nval;
1749
1750                                 rc = compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr_access );
1751                                 if ( rc == 1 ) {
1752                                         rc = LDAP_INSUFFICIENT_ACCESS;
1753
1754                                 } else {
1755                                         rc = LDAP_SUCCESS;
1756                                 }
1757                         }
1758 #endif /* LDAP_SLAPI */
1759                 }
1760 freeit:         if ( e != target ) {
1761                         be_entry_release_r( op, e );
1762                 }
1763                 if ( freeattr ) {
1764                         attr_free( a );
1765                 }
1766         }
1767
1768         op->o_bd = be;
1769         return rc;
1770 }
1771
1772 int backend_operational(
1773         Operation *op,
1774         SlapReply *rs )
1775 {
1776         Attribute       **ap;
1777         int             rc = 0;
1778         BackendDB       *be_orig;
1779
1780         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1781                 /* just count them */ ;
1782
1783         /*
1784          * If operational attributes (allegedly) are required, 
1785          * and the backend supports specific operational attributes, 
1786          * add them to the attribute list
1787          */
1788         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1789                 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs )))
1790         {
1791                 *ap = slap_operational_entryDN( rs->sr_entry );
1792                 ap = &(*ap)->a_next;
1793         }
1794
1795         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1796                 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs )))
1797         {
1798                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1799                 ap = &(*ap)->a_next;
1800         }
1801
1802         /* Let the overlays have a chance at this */
1803         be_orig = op->o_bd;
1804         if ( SLAP_ISOVERLAY( be_orig ))
1805                 op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1806
1807         if (( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1808                 op->o_bd && op->o_bd->be_operational != NULL )
1809         {
1810                 Attribute       *a;
1811                 
1812                 a = rs->sr_operational_attrs;
1813                 rs->sr_operational_attrs = NULL;
1814                 rc = op->o_bd->be_operational( op, rs );
1815                 *ap = rs->sr_operational_attrs;
1816                 if ( a != NULL ) {
1817                         rs->sr_operational_attrs = a;
1818                 }
1819
1820                 for ( ; *ap; ap = &(*ap)->a_next )
1821                         /* just count them */ ;
1822         }
1823         op->o_bd = be_orig;
1824
1825         return rc;
1826 }
1827
1828 #ifdef LDAP_SLAPI
1829 static void init_group_pblock( Operation *op, Entry *target,
1830         Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1831 {
1832         slapi_int_pblock_set_operation( op->o_pb, op );
1833
1834         slapi_pblock_set( op->o_pb,
1835                 SLAPI_X_GROUP_ENTRY, (void *)e );
1836         slapi_pblock_set( op->o_pb,
1837                 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1838         slapi_pblock_set( op->o_pb,
1839                 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1840         slapi_pblock_set( op->o_pb,
1841                 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1842 }
1843
1844 static int call_group_preop_plugins( Operation *op )
1845 {
1846         int rc;
1847
1848         rc = slapi_int_call_plugins( op->o_bd,
1849                 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1850         if ( rc < 0 ) {
1851                 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1852                         (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1853                 {
1854                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1855                 }
1856         } else {
1857                 rc = LDAP_SUCCESS;
1858         }
1859
1860         return rc;
1861 }
1862
1863 static void call_group_postop_plugins( Operation *op )
1864 {
1865         (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1866 }
1867 #endif /* LDAP_SLAPI */
1868