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