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