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