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