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