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