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