]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
0469f9b1187a82649a36c09d5781604fc406e527
[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( &syncrepl_rq.rq_mutex );
265         LDAP_STAILQ_INIT( &syncrepl_rq.task_list );
266         LDAP_STAILQ_INIT( &syncrepl_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( &syncrepl_rq.rq_mutex );
299                                 ldap_pvt_runqueue_insert( &syncrepl_rq,
300                                                 si->si_interval, do_syncrepl, (void *) si );
301                                 ldap_pvt_thread_mutex_unlock( &syncrepl_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->bd_info->bi_db_destroy ) {
449                 bd->bd_info->bi_db_destroy( bd );
450         }
451         ber_bvarray_free( bd->be_suffix );
452         ber_bvarray_free( bd->be_nsuffix );
453         if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
454         if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
455         if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
456         acl_destroy( bd->be_acl, frontendDB->be_acl );
457
458         return 0;
459 }
460
461 BackendInfo* backend_info(const char *type)
462 {
463         int i;
464
465         /* search for the backend type */
466         for( i = 0; i < nBackendInfo; i++ ) {
467                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
468                         return &backendInfo[i];
469                 }
470         }
471
472         return NULL;
473 }
474
475
476 BackendDB *
477 backend_db_init(
478     const char  *type )
479 {
480         Backend *be;
481         BackendInfo *bi = backend_info(type);
482         int     rc = 0;
483
484         if( bi == NULL ) {
485                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
486                 return NULL;
487         }
488
489         be = backendDB;
490
491         backendDB = (BackendDB *) ch_realloc(
492                         (char *) backendDB,
493                     (nBackendDB + 1) * sizeof(Backend) );
494
495         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
496
497         /* did realloc move our table? if so, fix up dependent pointers */
498         if ( be != backendDB ) {
499                 int i;
500                 for ( i=0, be=backendDB; i<nbackends; i++, be++ ) {
501                         be->be_pcl_mutexp = &be->be_pcl_mutex;
502                 }
503         }
504
505         be = &backends[nbackends++];
506
507         be->bd_info = bi;
508         be->be_def_limit = frontendDB->be_def_limit;
509         be->be_dfltaccess = frontendDB->be_dfltaccess;
510
511         be->be_restrictops = frontendDB->be_restrictops;
512         be->be_requires = frontendDB->be_requires;
513         be->be_ssf_set = frontendDB->be_ssf_set;
514
515         be->be_context_csn.bv_len = 0;
516         be->be_context_csn.bv_val = NULL;
517         be->be_pcl_mutexp = &be->be_pcl_mutex;
518         ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
519
520         LDAP_STAILQ_INIT( &be->be_syncinfo );
521
522         /* assign a default depth limit for alias deref */
523         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
524
525         if(bi->bi_db_init) {
526                 rc = bi->bi_db_init( be );
527         }
528
529         if(rc != 0) {
530                 fprintf( stderr, "database init failed (%s)\n", type );
531                 nbackends--;
532                 return NULL;
533         }
534
535         bi->bi_nDB++;
536         return( be );
537 }
538
539 void
540 be_db_close( void )
541 {
542         int     i;
543
544         for ( i = 0; i < nbackends; i++ ) {
545                 if ( backends[i].bd_info->bi_db_close ) {
546                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
547                 }
548         }
549
550         if ( frontendDB->bd_info->bi_db_close ) {
551                 (*frontendDB->bd_info->bi_db_close)( frontendDB );
552         }
553 }
554
555 Backend *
556 select_backend(
557         struct berval * dn,
558         int manageDSAit,
559         int noSubs )
560 {
561         int     i, j;
562         ber_len_t len, dnlen = dn->bv_len;
563         Backend *be = NULL;
564
565         for ( i = 0; i < nbackends; i++ ) {
566                 for ( j = 0; backends[i].be_nsuffix != NULL &&
567                     backends[i].be_nsuffix[j].bv_val != NULL; j++ )
568                 {
569                         if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
570                                 && noSubs )
571                         {
572                                 continue;
573                         }
574
575                         len = backends[i].be_nsuffix[j].bv_len;
576
577                         if ( len > dnlen ) {
578                                 /* suffix is longer than DN */
579                                 continue;
580                         }
581                         
582                         /*
583                          * input DN is normalized, so the separator check
584                          * need not look at escaping
585                          */
586                         if ( len && len < dnlen &&
587                                 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
588                         {
589                                 continue;
590                         }
591
592                         if ( strcmp( backends[i].be_nsuffix[j].bv_val,
593                                 &dn->bv_val[dnlen-len] ) == 0 )
594                         {
595                                 if( be == NULL ) {
596                                         be = &backends[i];
597
598                                         if( manageDSAit && len == dnlen &&
599                                                 !SLAP_GLUE_SUBORDINATE( be ) ) {
600                                                 continue;
601                                         }
602                                 } else {
603                                         be = &backends[i];
604                                 }
605                                 return be;
606                         }
607                 }
608         }
609
610         return be;
611 }
612
613 int
614 be_issuffix(
615     Backend *be,
616     struct berval *bvsuffix )
617 {
618         int     i;
619
620         for ( i = 0;
621                 be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL;
622                 i++ )
623         {
624                 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
625                         return( 1 );
626                 }
627         }
628
629         return( 0 );
630 }
631
632 int
633 be_isroot_dn( Backend *be, struct berval *ndn )
634 {
635         if ( !ndn->bv_len ) {
636                 return( 0 );
637         }
638
639         if ( !be->be_rootndn.bv_len ) {
640                 return( 0 );
641         }
642
643         return dn_match( &be->be_rootndn, ndn );
644 }
645
646 int
647 be_sync_update( Operation *op )
648 {
649         return ( SLAP_SYNC_SHADOW( op->o_bd ) && syncrepl_isupdate( op ) );
650 }
651
652 int
653 be_slurp_update( Operation *op )
654 {
655         return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
656                 be_isupdate_dn( op->o_bd, &op->o_ndn ));
657 }
658
659 int
660 be_shadow_update( Operation *op )
661 {
662         return ( SLAP_SHADOW( op->o_bd ) &&
663                 ( syncrepl_isupdate( op ) || be_isupdate_dn( op->o_bd, &op->o_ndn )));
664 }
665
666 int
667 be_isupdate_dn( Backend *be, struct berval *ndn )
668 {
669         if ( !ndn->bv_len ) return( 0 );
670
671         if ( !be->be_update_ndn.bv_len ) return( 0 );
672
673         return dn_match( &be->be_update_ndn, ndn );
674 }
675
676 struct berval *
677 be_root_dn( Backend *be )
678 {
679         return &be->be_rootdn;
680 }
681
682 int
683 be_isroot( Operation *op )
684 {
685         return be_isroot_dn( op->o_bd, &op->o_ndn );
686 }
687
688 int
689 be_isroot_pw( Operation *op )
690 {
691         int result;
692
693         if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
694                 return 0;
695         }
696
697         if( op->o_bd->be_rootpw.bv_len == 0 ) {
698                 return 0;
699         }
700
701 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
702         ldap_pvt_thread_mutex_lock( &passwd_mutex );
703 #ifdef SLAPD_SPASSWD
704         lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
705 #endif
706 #endif
707
708         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
709
710 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
711 #ifdef SLAPD_SPASSWD
712         lutil_passwd_sasl_conn = NULL;
713 #endif
714         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
715 #endif
716
717         return result == 0;
718 }
719
720 int
721 be_entry_release_rw(
722         Operation *op,
723         Entry *e,
724         int rw )
725 {
726         if ( op->o_bd->be_release ) {
727                 /* free and release entry from backend */
728                 return op->o_bd->be_release( op, e, rw );
729         } else {
730                 /* free entry */
731                 entry_free( e );
732                 return 0;
733         }
734 }
735
736 int
737 backend_unbind( Operation *op, SlapReply *rs )
738 {
739         int             i;
740
741         for ( i = 0; i < nbackends; i++ ) {
742 #if defined( LDAP_SLAPI )
743                 if ( op->o_pb ) {
744                         int rc;
745                         if ( i == 0 ) slapi_int_pblock_set_operation( op->o_pb, op );
746                         slapi_pblock_set( op->o_pb, SLAPI_BACKEND, (void *)&backends[i] );
747                         rc = slapi_int_call_plugins( &backends[i],
748                                 SLAPI_PLUGIN_PRE_UNBIND_FN, (Slapi_PBlock *)op->o_pb );
749                         if ( rc < 0 ) {
750                                 /*
751                                  * A preoperation plugin failure will abort the
752                                  * entire operation.
753                                  */
754                                 Debug(LDAP_DEBUG_TRACE,
755                                         "do_bind: Unbind preoperation plugin failed\n",
756                                         0, 0, 0);
757                                 return 0;
758                         }
759                 }
760 #endif /* defined( LDAP_SLAPI ) */
761
762                 if ( backends[i].be_unbind ) {
763                         op->o_bd = &backends[i];
764                         (*backends[i].be_unbind)( op, rs );
765                 }
766
767 #if defined( LDAP_SLAPI )
768                 if ( op->o_pb != NULL && slapi_int_call_plugins( &backends[i],
769                         SLAPI_PLUGIN_POST_UNBIND_FN, (Slapi_PBlock *)op->o_pb ) < 0 )
770                 {
771                         Debug(LDAP_DEBUG_TRACE,
772                                 "do_unbind: Unbind postoperation plugins failed\n",
773                                 0, 0, 0);
774                 }
775 #endif /* defined( LDAP_SLAPI ) */
776         }
777
778         return 0;
779 }
780
781 int
782 backend_connection_init(
783         Connection   *conn )
784 {
785         int     i;
786
787         for ( i = 0; i < nbackends; i++ ) {
788                 if ( backends[i].be_connection_init ) {
789                         (*backends[i].be_connection_init)( &backends[i], conn);
790                 }
791         }
792
793         return 0;
794 }
795
796 int
797 backend_connection_destroy(
798         Connection   *conn )
799 {
800         int     i;
801
802         for ( i = 0; i < nbackends; i++ ) {
803                 if ( backends[i].be_connection_destroy ) {
804                         (*backends[i].be_connection_destroy)( &backends[i], conn);
805                 }
806         }
807
808         return 0;
809 }
810
811 static int
812 backend_check_controls(
813         Operation *op,
814         SlapReply *rs )
815 {
816         LDAPControl **ctrls = op->o_ctrls;
817         rs->sr_err = LDAP_SUCCESS;
818
819         if( ctrls ) {
820                 for( ; *ctrls != NULL ; ctrls++ ) {
821                         if( (*ctrls)->ldctl_iscritical && !ldap_charray_inlist(
822                                 op->o_bd->be_controls, (*ctrls)->ldctl_oid ) )
823                         {
824                                 /* FIXME: standards compliance issue
825                                  *
826                                  * Per RFC 2251 (and LDAPBIS discussions), if the control
827                                  * is recognized and appropriate for the operation (which
828                                  * we've already verified), then the server should make
829                                  * use of the control when performing the operation
830                                  * (without regard to criticality).  This code is incorrect
831                                  * on two counts.
832                                  * 1) a service error (e.g., unwillingToPerform) should be
833                                  * returned where a particular backend cannot service the
834                                  * operation,
835                                  * 2) this error should be returned irregardless of the
836                                  * criticality of the control.
837                                  */
838                                 rs->sr_text = "control unavailable in context";
839                                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
840                                 break;
841                         }
842                 }
843         }
844
845         return rs->sr_err;
846 }
847
848 int
849 backend_check_restrictions(
850         Operation *op,
851         SlapReply *rs,
852         struct berval *opdata )
853 {
854         slap_mask_t restrictops;
855         slap_mask_t requires;
856         slap_mask_t opflag;
857         slap_mask_t exopflag = 0;
858         slap_ssf_set_t *ssf;
859         int updateop = 0;
860         int starttls = 0;
861         int session = 0;
862
863         if( op->o_bd ) {
864                 if ( backend_check_controls( op, rs ) != LDAP_SUCCESS ) {
865                         return rs->sr_err;
866                 }
867
868                 restrictops = op->o_bd->be_restrictops;
869                 requires = op->o_bd->be_requires;
870                 ssf = &op->o_bd->be_ssf_set;
871
872         } else {
873                 restrictops = frontendDB->be_restrictops;
874                 requires = frontendDB->be_requires;
875                 ssf = &frontendDB->be_ssf_set;
876         }
877
878         switch( op->o_tag ) {
879         case LDAP_REQ_ADD:
880                 opflag = SLAP_RESTRICT_OP_ADD;
881                 updateop++;
882                 break;
883         case LDAP_REQ_BIND:
884                 opflag = SLAP_RESTRICT_OP_BIND;
885                 session++;
886                 break;
887         case LDAP_REQ_COMPARE:
888                 opflag = SLAP_RESTRICT_OP_COMPARE;
889                 break;
890         case LDAP_REQ_DELETE:
891                 updateop++;
892                 opflag = SLAP_RESTRICT_OP_DELETE;
893                 break;
894         case LDAP_REQ_EXTENDED:
895                 opflag = SLAP_RESTRICT_OP_EXTENDED;
896
897                 if( !opdata ) {
898                         /* treat unspecified as a modify */
899                         opflag = SLAP_RESTRICT_OP_MODIFY;
900                         updateop++;
901                         break;
902                 }
903
904                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
905                         session++;
906                         starttls++;
907                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
908                         break;
909                 }
910
911                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
912                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
913                         break;
914                 }
915
916                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
917                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
918                         break;
919                 }
920
921                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
922                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
923                         updateop++;
924                         break;
925                 }
926
927                 /* treat everything else as a modify */
928                 opflag = SLAP_RESTRICT_OP_MODIFY;
929                 updateop++;
930                 break;
931
932         case LDAP_REQ_MODIFY:
933                 updateop++;
934                 opflag = SLAP_RESTRICT_OP_MODIFY;
935                 break;
936         case LDAP_REQ_RENAME:
937                 updateop++;
938                 opflag = SLAP_RESTRICT_OP_RENAME;
939                 break;
940         case LDAP_REQ_SEARCH:
941                 opflag = SLAP_RESTRICT_OP_SEARCH;
942                 break;
943         case LDAP_REQ_UNBIND:
944                 session++;
945                 opflag = 0;
946                 break;
947         default:
948                 rs->sr_text = "restrict operations internal error";
949                 rs->sr_err = LDAP_OTHER;
950                 return rs->sr_err;
951         }
952
953         if ( !starttls ) {
954                 /* these checks don't apply to StartTLS */
955
956                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
957                 if( op->o_transport_ssf < ssf->sss_transport ) {
958                         rs->sr_text = op->o_transport_ssf
959                                 ? "stronger transport confidentiality required"
960                                 : "transport confidentiality required";
961                         return rs->sr_err;
962                 }
963
964                 if( op->o_tls_ssf < ssf->sss_tls ) {
965                         rs->sr_text = op->o_tls_ssf
966                                 ? "stronger TLS confidentiality required"
967                                 : "TLS confidentiality required";
968                         return rs->sr_err;
969                 }
970
971
972                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
973                         /* simple bind specific check */
974                         if( op->o_ssf < ssf->sss_simple_bind ) {
975                                 rs->sr_text = op->o_ssf
976                                         ? "stronger confidentiality required"
977                                         : "confidentiality required";
978                                 return rs->sr_err;
979                         }
980                 }
981
982                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
983                         /* these checks don't apply to SASL bind */
984
985                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
986                                 rs->sr_text = op->o_sasl_ssf
987                                         ? "stronger SASL confidentiality required"
988                                         : "SASL confidentiality required";
989                                 return rs->sr_err;
990                         }
991
992                         if( op->o_ssf < ssf->sss_ssf ) {
993                                 rs->sr_text = op->o_ssf
994                                         ? "stronger confidentiality required"
995                                         : "confidentiality required";
996                                 return rs->sr_err;
997                         }
998                 }
999
1000                 if( updateop ) {
1001                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
1002                                 rs->sr_text = op->o_transport_ssf
1003                                         ? "stronger transport confidentiality required for update"
1004                                         : "transport confidentiality required for update";
1005                                 return rs->sr_err;
1006                         }
1007
1008                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
1009                                 rs->sr_text = op->o_tls_ssf
1010                                         ? "stronger TLS confidentiality required for update"
1011                                         : "TLS confidentiality required for update";
1012                                 return rs->sr_err;
1013                         }
1014
1015                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1016                                 rs->sr_text = op->o_sasl_ssf
1017                                         ? "stronger SASL confidentiality required for update"
1018                                         : "SASL confidentiality required for update";
1019                                 return rs->sr_err;
1020                         }
1021
1022                         if( op->o_ssf < ssf->sss_update_ssf ) {
1023                                 rs->sr_text = op->o_ssf
1024                                         ? "stronger confidentiality required for update"
1025                                         : "confidentiality required for update";
1026                                 return rs->sr_err;
1027                         }
1028
1029                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1030                                 op->o_ndn.bv_len == 0 )
1031                         {
1032                                 rs->sr_text = "modifications require authentication";
1033                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1034                                 return rs->sr_err;
1035                         }
1036
1037 #ifdef SLAP_X_LISTENER_MOD
1038                         if ( op->o_conn->c_listener && ! ( op->o_conn->c_listener->sl_perms & ( op->o_ndn.bv_len > 0 ? S_IWUSR : S_IWOTH ) ) ) {
1039                                 /* no "w" mode means readonly */
1040                                 rs->sr_text = "modifications not allowed on this listener";
1041                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1042                                 return rs->sr_err;
1043                         }
1044 #endif /* SLAP_X_LISTENER_MOD */
1045                 }
1046         }
1047
1048         if ( !session ) {
1049                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1050
1051                 if( requires & SLAP_REQUIRE_STRONG ) {
1052                         /* should check mechanism */
1053                         if( ( op->o_transport_ssf < ssf->sss_transport
1054                                 && op->o_authtype == LDAP_AUTH_SIMPLE )
1055                                 || op->o_dn.bv_len == 0 )
1056                         {
1057                                 rs->sr_text = "strong(er) authentication required";
1058                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1059                                 return rs->sr_err;
1060                         }
1061                 }
1062
1063                 if( requires & SLAP_REQUIRE_SASL ) {
1064                         if( op->o_authtype != LDAP_AUTH_SASL || op->o_dn.bv_len == 0 ) {
1065                                 rs->sr_text = "SASL authentication required";
1066                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1067                                 return rs->sr_err;
1068                         }
1069                 }
1070                         
1071                 if( requires & SLAP_REQUIRE_AUTHC ) {
1072                         if( op->o_dn.bv_len == 0 ) {
1073                                 rs->sr_text = "authentication required";
1074                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1075                                 return rs->sr_err;
1076                         }
1077                 }
1078
1079                 if( requires & SLAP_REQUIRE_BIND ) {
1080                         int version;
1081                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1082                         version = op->o_conn->c_protocol;
1083                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1084
1085                         if( !version ) {
1086                                 /* no bind has occurred */
1087                                 rs->sr_text = "BIND required";
1088                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1089                                 return rs->sr_err;
1090                         }
1091                 }
1092
1093                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1094                         if( op->o_protocol < LDAP_VERSION3 ) {
1095                                 /* no bind has occurred */
1096                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1097                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1098                                 return rs->sr_err;
1099                         }
1100                 }
1101
1102 #ifdef SLAP_X_LISTENER_MOD
1103                 if ( !starttls && op->o_dn.bv_len == 0 ) {
1104                         if ( op->o_conn->c_listener &&
1105                                 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1106                 {
1107                                 /* no "x" mode means bind required */
1108                                 rs->sr_text = "bind required on this listener";
1109                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1110                                 return rs->sr_err;
1111                         }
1112                 }
1113
1114                 if ( !starttls && !updateop ) {
1115                         if ( op->o_conn->c_listener &&
1116                                 !( op->o_conn->c_listener->sl_perms &
1117                                         ( op->o_dn.bv_len > 0 ? S_IRUSR : S_IROTH )))
1118                         {
1119                                 /* no "r" mode means no read */
1120                                 rs->sr_text = "read not allowed on this listener";
1121                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1122                                 return rs->sr_err;
1123                         }
1124                 }
1125 #endif /* SLAP_X_LISTENER_MOD */
1126
1127         }
1128
1129         if( ( restrictops & opflag )
1130                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1131                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1132                         rs->sr_text = "read operations restricted";
1133                 } else if ( restrictops & exopflag ) {
1134                         rs->sr_text = "extended operation restricted";
1135                 } else {
1136                         rs->sr_text = "operation restricted";
1137                 }
1138                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1139                 return rs->sr_err;
1140         }
1141
1142         rs->sr_err = LDAP_SUCCESS;
1143         return rs->sr_err;
1144 }
1145
1146 int backend_check_referrals( Operation *op, SlapReply *rs )
1147 {
1148         rs->sr_err = LDAP_SUCCESS;
1149
1150         if( op->o_bd->be_chk_referrals ) {
1151                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1152
1153                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1154                         send_ldap_result( op, rs );
1155                 }
1156         }
1157
1158         return rs->sr_err;
1159 }
1160
1161 int
1162 be_entry_get_rw(
1163         Operation *op,
1164         struct berval *ndn,
1165         ObjectClass *oc,
1166         AttributeDescription *at,
1167         int rw,
1168         Entry **e )
1169 {
1170         int rc;
1171
1172         *e = NULL;
1173
1174         if (op->o_bd == NULL) {
1175                 rc = LDAP_NO_SUCH_OBJECT;
1176         } else if ( op->o_bd->be_fetch ) {
1177                 rc = ( op->o_bd->be_fetch )( op, ndn,
1178                         oc, at, rw, e );
1179         } else {
1180                 rc = LDAP_UNWILLING_TO_PERFORM;
1181         }
1182         return rc;
1183 }
1184
1185 int 
1186 backend_group(
1187         Operation *op,
1188         Entry   *target,
1189         struct berval *gr_ndn,
1190         struct berval *op_ndn,
1191         ObjectClass *group_oc,
1192         AttributeDescription *group_at )
1193 {
1194         Entry *e;
1195         Attribute *a;
1196         int rc;
1197         GroupAssertion *g;
1198         Backend *be = op->o_bd;
1199
1200         if ( op->o_abandon ) return SLAPD_ABANDON;
1201
1202         op->o_bd = select_backend( gr_ndn, 0, 0 );
1203
1204         for (g = op->o_groups; g; g=g->ga_next) {
1205                 if (g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1206                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1207                         continue;
1208                 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1209                         break;
1210         }
1211
1212         if (g) {
1213                 rc = g->ga_res;
1214                 goto done;
1215         }
1216
1217         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1218                 e = target;
1219                 rc = 0;
1220         } else {
1221                 rc = be_entry_get_rw(op, gr_ndn, group_oc, group_at, 0, &e );
1222         }
1223         if ( e ) {
1224 #ifdef LDAP_SLAPI
1225                 if ( op->o_pb != NULL ) {
1226                         init_group_pblock( op, target, e, op_ndn, group_at );
1227
1228                         rc = call_group_preop_plugins( op );
1229                         if ( rc == LDAP_SUCCESS ) {
1230                                 goto done;
1231                         }
1232                 }
1233 #endif /* LDAP_SLAPI */
1234
1235                 a = attr_find( e->e_attrs, group_at );
1236                 if ( a ) {
1237                         /* If the attribute is a subtype of labeledURI, treat this as
1238                          * a dynamic group ala groupOfURLs
1239                          */
1240                         if (is_at_subtype( group_at->ad_type,
1241                                 slap_schema.si_ad_labeledURI->ad_type ) )
1242                         {
1243                                 int i;
1244                                 LDAPURLDesc *ludp;
1245                                 struct berval bv, nbase;
1246                                 Filter *filter;
1247                                 Entry *user;
1248                                 Backend *b2 = op->o_bd;
1249                                 void *o_private = op->o_private;
1250
1251                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1252                                         user = target;
1253                                 } else {
1254                                         /* back-bdb stored lockinfo here, we saved it
1255                                          * above. Clear it out so that a new lock can be used.
1256                                          */
1257                                         op->o_private = NULL;
1258                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1259                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1260                                 }
1261                                 
1262                                 if ( rc == 0 ) {
1263                                         rc = 1;
1264                                         for (i=0; a->a_vals[i].bv_val; i++) {
1265                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1266                                                         LDAP_URL_SUCCESS )
1267                                                 {
1268                                                         continue;
1269                                                 }
1270                                                 nbase.bv_val = NULL;
1271                                                 /* host part must be empty */
1272                                                 /* attrs and extensions parts must be empty */
1273                                                 if (( ludp->lud_host && *ludp->lud_host ) ||
1274                                                         ludp->lud_attrs || ludp->lud_exts )
1275                                                 {
1276                                                         goto loopit;
1277                                                 }
1278                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1279                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1280                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1281                                                 {
1282                                                         goto loopit;
1283                                                 }
1284                                                 switch(ludp->lud_scope) {
1285                                                 case LDAP_SCOPE_BASE:
1286                                                         if ( !dn_match( &nbase, op_ndn )) goto loopit;
1287                                                         break;
1288                                                 case LDAP_SCOPE_ONELEVEL:
1289                                                         dnParent(op_ndn, &bv );
1290                                                         if ( !dn_match( &nbase, &bv )) goto loopit;
1291                                                         break;
1292                                                 case LDAP_SCOPE_SUBTREE:
1293                                                         if ( !dnIsSuffix( op_ndn, &nbase )) goto loopit;
1294                                                         break;
1295 #ifdef LDAP_SCOPE_SUBORDINATE
1296                                                 case LDAP_SCOPE_SUBORDINATE:
1297                                                         if ( dn_match( &nbase, op_ndn ) ||
1298                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1299                                                         {
1300                                                                 goto loopit;
1301                                                         }
1302 #endif
1303                                                 }
1304                                                 filter = str2filter_x( op, ludp->lud_filter );
1305                                                 if ( filter ) {
1306                                                         if ( test_filter( NULL, user, filter ) ==
1307                                                                 LDAP_COMPARE_TRUE )
1308                                                         {
1309                                                                 rc = 0;
1310                                                         }
1311                                                         filter_free_x( op, filter );
1312                                                 }
1313 loopit:
1314                                                 ldap_free_urldesc( ludp );
1315                                                 if ( nbase.bv_val ) {
1316                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1317                                                 }
1318                                                 if ( rc == 0 ) break;
1319                                         }
1320                                         if ( user != target ) {
1321                                                 be_entry_release_r( op, user );
1322                                                 /* restore previous lockinfo, if any */
1323                                                 op->o_private = o_private;
1324                                         }
1325                                 }
1326                                 op->o_bd = b2;
1327                         } else {
1328                                 rc = value_find_ex( group_at,
1329                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1330                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1331                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1332                         }
1333                 } else {
1334                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1335                 }
1336                 if (e != target ) {
1337                         be_entry_release_r( op, e );
1338                 }
1339         } else {
1340                 rc = LDAP_NO_SUCH_OBJECT;
1341         }
1342
1343 #ifdef LDAP_SLAPI
1344         if ( op->o_pb ) call_group_postop_plugins( op );
1345 #endif /* LDAP_SLAPI */
1346
1347         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1348                 g = op->o_tmpalloc(sizeof(GroupAssertion) + gr_ndn->bv_len,
1349                         op->o_tmpmemctx);
1350                 g->ga_be = op->o_bd;
1351                 g->ga_oc = group_oc;
1352                 g->ga_at = group_at;
1353                 g->ga_res = rc;
1354                 g->ga_len = gr_ndn->bv_len;
1355                 strcpy(g->ga_ndn, gr_ndn->bv_val);
1356                 g->ga_next = op->o_groups;
1357                 op->o_groups = g;
1358         }
1359 done:
1360         op->o_bd = be;
1361         return rc;
1362 }
1363
1364 #ifdef LDAP_SLAPI
1365 static int backend_compute_output_attr(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
1366 {
1367         BerVarray v;
1368         int rc;
1369         BerVarray *vals = (BerVarray *)c->cac_private;
1370         Operation *op = NULL;
1371         int i, j;
1372
1373         slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, &op );
1374         if ( op == NULL ) {
1375                 return 1;
1376         }
1377
1378         if ( op->o_conn && access_allowed( op,
1379                 e, a->a_desc, NULL, ACL_AUTH,
1380                 &c->cac_acl_state ) == 0 ) {
1381                 return 1;
1382         }
1383
1384         for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1385                         
1386         v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1387                 op->o_tmpmemctx );
1388         for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1389                 if ( op->o_conn && access_allowed( op,
1390                         e, a->a_desc,
1391                         &a->a_nvals[i],
1392                         ACL_AUTH, &c->cac_acl_state ) == 0 ) {
1393                         continue;
1394                 }
1395                 ber_dupbv_x( &v[j],
1396                         &a->a_nvals[i], op->o_tmpmemctx );
1397                 if (v[j].bv_val ) j++;
1398         }
1399
1400         if (j == 0) {
1401                 op->o_tmpfree( v, op->o_tmpmemctx );
1402                 *vals = NULL;
1403                 rc = 1;
1404         } else {
1405                 v[j].bv_val = NULL;
1406                 v[j].bv_len = 0;
1407                 *vals = v;
1408                 rc = 0;
1409         }
1410
1411         return rc;
1412 }
1413 #endif /* LDAP_SLAPI */
1414
1415 int 
1416 backend_attribute(
1417         Operation *op,
1418         Entry   *target,
1419         struct berval   *edn,
1420         AttributeDescription *entry_at,
1421         BerVarray *vals,
1422         slap_access_t access )
1423 {
1424         Entry *e;
1425         Attribute *a;
1426         int i, j, rc = LDAP_SUCCESS;
1427         AccessControlState acl_state = ACL_STATE_INIT;
1428         Backend *be = op->o_bd;
1429
1430         op->o_bd = select_backend( edn, 0, 0 );
1431
1432         if ( target && dn_match( &target->e_nname, edn ) ) {
1433                 e = target;
1434         } else {
1435                 rc = be_entry_get_rw(op, edn, NULL, entry_at, 0, &e );
1436         } 
1437
1438         if ( e ) {
1439                 a = attr_find( e->e_attrs, entry_at );
1440                 if ( a ) {
1441                         BerVarray v;
1442
1443                         if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1444                                 e, entry_at, NULL, access,
1445                                 &acl_state ) == 0 ) {
1446                                 rc = LDAP_INSUFFICIENT_ACCESS;
1447                                 goto freeit;
1448                         }
1449
1450                         for ( i=0; a->a_vals[i].bv_val; i++ ) ;
1451                         
1452                         v = op->o_tmpalloc( sizeof(struct berval) * (i+1),
1453                                 op->o_tmpmemctx );
1454                         for ( i=0,j=0; a->a_vals[i].bv_val; i++ ) {
1455                                 if ( op->o_conn && access > ACL_NONE && access_allowed( op,
1456                                         e, entry_at,
1457                                         &a->a_nvals[i],
1458                                         access, &acl_state ) == 0 ) {
1459                                         continue;
1460                                 }
1461                                 ber_dupbv_x( &v[j],
1462                                         &a->a_nvals[i], op->o_tmpmemctx );
1463                                 if (v[j].bv_val ) j++;
1464                         }
1465                         if (j == 0) {
1466                                 op->o_tmpfree( v, op->o_tmpmemctx );
1467                                 *vals = NULL;
1468                                 rc = LDAP_INSUFFICIENT_ACCESS;
1469                         } else {
1470                                 v[j].bv_val = NULL;
1471                                 v[j].bv_len = 0;
1472                                 *vals = v;
1473                                 rc = LDAP_SUCCESS;
1474                         }
1475                 }
1476 #ifdef LDAP_SLAPI
1477                 else if ( op->o_pb ) {
1478                         /* try any computed attributes */
1479                         computed_attr_context ctx;
1480                         AttributeName aname;
1481
1482                         slapi_int_pblock_set_operation( op->o_pb, op );
1483
1484                         ctx.cac_pb = op->o_pb;
1485                         ctx.cac_attrs = NULL;
1486                         ctx.cac_userattrs = 0;
1487                         ctx.cac_opattrs = 0;
1488                         ctx.cac_acl_state = acl_state;
1489                         ctx.cac_private = (void *)vals;
1490
1491                         if ( compute_evaluator( &ctx, entry_at->ad_cname.bv_val, e, backend_compute_output_attr ) == 1)
1492                                 rc = LDAP_INSUFFICIENT_ACCESS;
1493                         else
1494                                 rc = LDAP_SUCCESS;
1495                 }
1496 #endif /* LDAP_SLAPI */
1497 freeit:         if (e != target ) {
1498                         be_entry_release_r( op, e );
1499                 }
1500         }
1501
1502         op->o_bd = be;
1503         return rc;
1504 }
1505
1506 int backend_operational(
1507         Operation *op,
1508         SlapReply *rs )
1509 {
1510         Attribute       **ap;
1511         int             rc = 0;
1512         BackendDB *be_orig;
1513
1514         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1515                 /* just count them */ ;
1516
1517         /*
1518          * If operational attributes (allegedly) are required, 
1519          * and the backend supports specific operational attributes, 
1520          * add them to the attribute list
1521          */
1522         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1523                 ad_inlist( slap_schema.si_ad_entryDN, op->ors_attrs )))
1524         {
1525                 *ap = slap_operational_entryDN( rs->sr_entry );
1526                 ap = &(*ap)->a_next;
1527         }
1528
1529         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( op->ors_attrs &&
1530                 ad_inlist( slap_schema.si_ad_subschemaSubentry, op->ors_attrs )))
1531         {
1532                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1533                 ap = &(*ap)->a_next;
1534         }
1535
1536         /* Let the overlays have a chance at this */
1537         be_orig = op->o_bd;
1538         op->o_bd = select_backend( be_orig->be_nsuffix, 0, 0 );
1539
1540         if (( SLAP_OPATTRS( rs->sr_attr_flags ) || op->ors_attrs ) &&
1541                 op->o_bd && op->o_bd->be_operational != NULL )
1542         {
1543                 Attribute       *a;
1544                 
1545                 a = rs->sr_operational_attrs;
1546                 rs->sr_operational_attrs = NULL;
1547                 rc = op->o_bd->be_operational( op, rs );
1548                 *ap = rs->sr_operational_attrs;
1549                 if ( a != NULL ) {
1550                         rs->sr_operational_attrs = a;
1551                 }
1552
1553                 for ( ; *ap; ap = &(*ap)->a_next )
1554                         /* just count them */ ;
1555         }
1556         op->o_bd = be_orig;
1557
1558         return rc;
1559 }
1560
1561 #ifdef LDAP_SLAPI
1562 static void init_group_pblock( Operation *op, Entry *target,
1563         Entry *e, struct berval *op_ndn, AttributeDescription *group_at )
1564 {
1565         slapi_int_pblock_set_operation( op->o_pb, op );
1566
1567         slapi_pblock_set( op->o_pb,
1568                 SLAPI_X_GROUP_ENTRY, (void *)e );
1569         slapi_pblock_set( op->o_pb,
1570                 SLAPI_X_GROUP_OPERATION_DN, (void *)op_ndn->bv_val );
1571         slapi_pblock_set( op->o_pb,
1572                 SLAPI_X_GROUP_ATTRIBUTE, (void *)group_at->ad_cname.bv_val );
1573         slapi_pblock_set( op->o_pb,
1574                 SLAPI_X_GROUP_TARGET_ENTRY, (void *)target );
1575 }
1576
1577 static int call_group_preop_plugins( Operation *op )
1578 {
1579         int rc;
1580
1581         rc = slapi_int_call_plugins( op->o_bd,
1582                 SLAPI_X_PLUGIN_PRE_GROUP_FN, op->o_pb );
1583         if ( rc < 0 ) {
1584                 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
1585                         (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
1586                 {
1587                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1588                 }
1589         } else {
1590                 rc = LDAP_SUCCESS;
1591         }
1592
1593         return rc;
1594 }
1595
1596 static void call_group_postop_plugins( Operation *op )
1597 {
1598         (void) slapi_int_call_plugins( op->o_bd, SLAPI_X_PLUGIN_POST_GROUP_FN, op->o_pb );
1599 }
1600 #endif /* LDAP_SLAPI */
1601