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