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