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