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