]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
0a5934214c3eb656833c3939e7a18a7f8d58fc90
[openldap] / servers / slapd / backend.c
1 /* backend.c - routines for dealing with back-end databases */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27
28 #include "portable.h"
29
30 #include <stdio.h>
31
32 #include <ac/string.h>
33 #include <ac/socket.h>
34 #include <sys/stat.h>
35
36 #include "slap.h"
37 #include "lutil.h"
38 #include "lber_pvt.h"
39
40 /*
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                 assert( bd->be_replogfile == NULL );
481                 assert( bd->be_replica == NULL );
482         }
483
484         return 0;
485 }
486
487 BackendInfo* backend_info(const char *type)
488 {
489         BackendInfo *bi;
490
491         /* search for the backend type */
492         LDAP_STAILQ_FOREACH(bi,&backendInfo,bi_next) {
493                 if( strcasecmp(bi->bi_type, type) == 0 ) {
494                         return bi;
495                 }
496         }
497
498         return NULL;
499 }
500
501
502 BackendDB *
503 backend_db_init(
504     const char  *type )
505 {
506         Backend *be;
507         BackendInfo *bi = backend_info(type);
508         int     rc = 0;
509
510         if( bi == NULL ) {
511                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
512                 return NULL;
513         }
514
515         be = ch_calloc( 1, sizeof(Backend) );
516         nbackends++;
517         LDAP_STAILQ_INSERT_TAIL(&backendDB, be, be_next);
518
519         be->bd_info = bi;
520
521         be->be_def_limit = frontendDB->be_def_limit;
522         be->be_dfltaccess = frontendDB->be_dfltaccess;
523
524         be->be_restrictops = frontendDB->be_restrictops;
525         be->be_requires = frontendDB->be_requires;
526         be->be_ssf_set = frontendDB->be_ssf_set;
527
528         be->be_pcl_mutexp = &be->be_pcl_mutex;
529         ldap_pvt_thread_mutex_init( be->be_pcl_mutexp );
530
531         /* assign a default depth limit for alias deref */
532         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
533
534         if ( bi->bi_db_init ) {
535                 rc = bi->bi_db_init( be );
536         }
537
538         if ( rc != 0 ) {
539                 fprintf( stderr, "database init failed (%s)\n", type );
540                 nbackends--;
541                 return NULL;
542         }
543
544         bi->bi_nDB++;
545         return( be );
546 }
547
548 void
549 be_db_close( void )
550 {
551         BackendDB *be;
552
553         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
554                 if ( be->bd_info->bi_db_close ) {
555                         be->bd_info->bi_db_close( be );
556                 }
557         }
558
559         if ( frontendDB->bd_info->bi_db_close ) {
560                 (*frontendDB->bd_info->bi_db_close)( frontendDB );
561         }
562
563 }
564
565 Backend *
566 select_backend(
567         struct berval * dn,
568         int manageDSAit,
569         int noSubs )
570 {
571         int             j;
572         ber_len_t       len, dnlen = dn->bv_len;
573         Backend         *be, *b2 = NULL;
574
575         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
576                 if ( be->be_nsuffix == NULL ) {
577                         continue;
578                 }
579
580                 for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[j] ); j++ )
581                 {
582                         if ( ( SLAP_GLUE_SUBORDINATE( be ) ) && noSubs )
583                         {
584                                 continue;
585                         }
586
587                         len = be->be_nsuffix[j].bv_len;
588
589                         if ( len > dnlen ) {
590                                 /* suffix is longer than DN */
591                                 continue;
592                         }
593                         
594                         /*
595                          * input DN is normalized, so the separator check
596                          * need not look at escaping
597                          */
598                         if ( len && len < dnlen &&
599                                 !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ))
600                         {
601                                 continue;
602                         }
603
604                         if ( strcmp( be->be_nsuffix[j].bv_val,
605                                 &dn->bv_val[dnlen-len] ) == 0 )
606                         {
607                                 if( b2 == NULL ) {
608                                         b2 = be;
609
610                                         if( manageDSAit && len == dnlen &&
611                                                 !SLAP_GLUE_SUBORDINATE( be ) ) {
612                                                 continue;
613                                         }
614                                 } else {
615                                         b2 = be;
616                                 }
617                                 return b2;
618                         }
619                 }
620         }
621
622         return b2;
623 }
624
625 int
626 be_issuffix(
627     Backend *be,
628     struct berval *bvsuffix )
629 {
630         int     i;
631
632         if ( be->be_nsuffix == NULL ) {
633                 return 0;
634         }
635
636         for ( i = 0; !BER_BVISNULL( &be->be_nsuffix[i] ); i++ ) {
637                 if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
638                         return 1;
639                 }
640         }
641
642         return 0;
643 }
644
645 int
646 be_isroot_dn( Backend *be, struct berval *ndn )
647 {
648         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
649                 return 0;
650         }
651
652         return dn_match( &be->be_rootndn, ndn );
653 }
654
655 int
656 be_slurp_update( Operation *op )
657 {
658         return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
659                 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
660 }
661
662 int
663 be_shadow_update( Operation *op )
664 {
665         return ( SLAP_SYNC_SHADOW( op->o_bd ) ||
666                 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
667 }
668
669 int
670 be_isupdate_dn( Backend *be, struct berval *ndn )
671 {
672         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
673                 return 0;
674         }
675
676         return dn_match( &be->be_update_ndn, ndn );
677 }
678
679 struct berval *
680 be_root_dn( Backend *be )
681 {
682         return &be->be_rootdn;
683 }
684
685 int
686 be_isroot( Operation *op )
687 {
688         return be_isroot_dn( op->o_bd, &op->o_ndn );
689 }
690
691 int
692 be_isroot_pw( Operation *op )
693 {
694         int result;
695
696         if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
697                 return 0;
698         }
699
700         if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
701                 return 0;
702         }
703
704 #ifdef SLAPD_SPASSWD
705         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
706                 op->o_conn->c_sasl_authctx, NULL );
707 #endif
708
709         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
710
711 #ifdef SLAPD_SPASSWD
712         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
713 #endif
714
715         return result == 0;
716 }
717
718 int
719 be_entry_release_rw(
720         Operation *op,
721         Entry *e,
722         int rw )
723 {
724         if ( op->o_bd->be_release ) {
725                 /* free and release entry from backend */
726                 return op->o_bd->be_release( op, e, rw );
727         } else {
728                 /* free entry */
729                 entry_free( e );
730                 return 0;
731         }
732 }
733
734 int
735 backend_unbind( Operation *op, SlapReply *rs )
736 {
737         BackendDB *be;
738
739         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
740                 if ( be->be_unbind ) {
741                         op->o_bd = be;
742                         be->be_unbind( op, rs );
743                 }
744         }
745
746         return 0;
747 }
748
749 int
750 backend_connection_init(
751         Connection   *conn )
752 {
753         BackendDB *be;
754
755         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
756                 if ( be->be_connection_init ) {
757                         be->be_connection_init( be, conn );
758                 }
759         }
760
761         return 0;
762 }
763
764 int
765 backend_connection_destroy(
766         Connection   *conn )
767 {
768         BackendDB *be;
769
770         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
771                 if ( be->be_connection_destroy ) {
772                         be->be_connection_destroy( be, conn);
773                 }
774         }
775
776         return 0;
777 }
778
779 int
780 backend_check_controls(
781         Operation *op,
782         SlapReply *rs )
783 {
784         LDAPControl **ctrls = op->o_ctrls;
785         rs->sr_err = LDAP_SUCCESS;
786
787         if( ctrls ) {
788                 for( ; *ctrls != NULL ; ctrls++ ) {
789                         int cid;
790
791                         switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
792                         case LDAP_CONTROL_NOT_FOUND:
793                                 /* unrecognized control */ 
794                                 if ( (*ctrls)->ldctl_iscritical ) {
795                                         /* should not be reachable */ 
796                                         Debug( LDAP_DEBUG_ANY,
797                                                 "backend_check_controls: unrecognized control: %s\n",
798                                                 (*ctrls)->ldctl_oid, 0, 0 );
799                                         assert( 0 );
800                                 }
801                                 break;
802
803                         case LDAP_COMPARE_FALSE:
804                                 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
805                                         /* Per RFC 2251 (and LDAPBIS discussions), if the control
806                                          * is recognized and appropriate for the operation (which
807                                          * we've already verified), then the server should make
808                                          * use of the control when performing the operation.
809                                          * 
810                                          * Here we find that operation extended by the control
811                                          * is unavailable in a particular context, and the control
812                                          * is marked Critical, hence the return of
813                                          * unwillingToPerform.
814                                          */
815                                         rs->sr_text = "critical control unavailable in context";
816                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
817                                         goto done;
818                                 }
819                                 break;
820
821                         case LDAP_COMPARE_TRUE:
822                                 break;
823
824                         default:
825                                 /* unreachable */
826                                 Debug( LDAP_DEBUG_ANY,
827                                         "backend_check_controls: unable to check control: %s\n",
828                                         (*ctrls)->ldctl_oid, 0, 0 );
829                                 assert( 0 );
830
831                                 rs->sr_text = "unable to check control";
832                                 rs->sr_err = LDAP_OTHER;
833                                 goto done;
834                         }
835                 }
836         }
837
838         /* check should be generalized */
839         if( get_manageDIT(op) && !be_isroot(op)) {
840                 rs->sr_text = "requires manager authorization";
841                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
842         }
843
844 done:;
845         return rs->sr_err;
846 }
847
848 int
849 backend_check_restrictions(
850         Operation *op,
851         SlapReply *rs,
852         struct berval *opdata )
853 {
854         slap_mask_t restrictops;
855         slap_mask_t requires;
856         slap_mask_t opflag;
857         slap_mask_t exopflag = 0;
858         slap_ssf_set_t *ssf;
859         int updateop = 0;
860         int starttls = 0;
861         int session = 0;
862
863         if ( op->o_bd ) {
864                 int     rc = SLAP_CB_CONTINUE;
865
866                 if ( op->o_bd->be_chk_controls ) {
867                         rc = ( *op->o_bd->be_chk_controls )( op, rs );
868                 }
869
870                 if ( rc == SLAP_CB_CONTINUE ) {
871                         rc = backend_check_controls( op, rs );
872                 }
873
874                 if ( rc != LDAP_SUCCESS ) {
875                         return rs->sr_err;
876                 }
877
878                 restrictops = op->o_bd->be_restrictops;
879                 requires = op->o_bd->be_requires;
880                 ssf = &op->o_bd->be_ssf_set;
881
882         } else {
883                 restrictops = frontendDB->be_restrictops;
884                 requires = frontendDB->be_requires;
885                 ssf = &frontendDB->be_ssf_set;
886         }
887
888         switch( op->o_tag ) {
889         case LDAP_REQ_ADD:
890                 opflag = SLAP_RESTRICT_OP_ADD;
891                 updateop++;
892                 break;
893         case LDAP_REQ_BIND:
894                 opflag = SLAP_RESTRICT_OP_BIND;
895                 session++;
896                 break;
897         case LDAP_REQ_COMPARE:
898                 opflag = SLAP_RESTRICT_OP_COMPARE;
899                 break;
900         case LDAP_REQ_DELETE:
901                 updateop++;
902                 opflag = SLAP_RESTRICT_OP_DELETE;
903                 break;
904         case LDAP_REQ_EXTENDED:
905                 opflag = SLAP_RESTRICT_OP_EXTENDED;
906
907                 if( !opdata ) {
908                         /* treat unspecified as a modify */
909                         opflag = SLAP_RESTRICT_OP_MODIFY;
910                         updateop++;
911                         break;
912                 }
913
914                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
915                         session++;
916                         starttls++;
917                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
918                         break;
919                 }
920
921                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
922                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
923                         break;
924                 }
925
926                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
927                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
928                         break;
929                 }
930
931                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
932                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
933                         updateop++;
934                         break;
935                 }
936
937                 /* treat everything else as a modify */
938                 opflag = SLAP_RESTRICT_OP_MODIFY;
939                 updateop++;
940                 break;
941
942         case LDAP_REQ_MODIFY:
943                 updateop++;
944                 opflag = SLAP_RESTRICT_OP_MODIFY;
945                 break;
946         case LDAP_REQ_RENAME:
947                 updateop++;
948                 opflag = SLAP_RESTRICT_OP_RENAME;
949                 break;
950         case LDAP_REQ_SEARCH:
951                 opflag = SLAP_RESTRICT_OP_SEARCH;
952                 break;
953         case LDAP_REQ_UNBIND:
954                 session++;
955                 opflag = 0;
956                 break;
957         default:
958                 rs->sr_text = "restrict operations internal error";
959                 rs->sr_err = LDAP_OTHER;
960                 return rs->sr_err;
961         }
962
963         if ( !starttls ) {
964                 /* these checks don't apply to StartTLS */
965
966                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
967                 if( op->o_transport_ssf < ssf->sss_transport ) {
968                         rs->sr_text = op->o_transport_ssf
969                                 ? "stronger transport confidentiality required"
970                                 : "transport confidentiality required";
971                         return rs->sr_err;
972                 }
973
974                 if( op->o_tls_ssf < ssf->sss_tls ) {
975                         rs->sr_text = op->o_tls_ssf
976                                 ? "stronger TLS confidentiality required"
977                                 : "TLS confidentiality required";
978                         return rs->sr_err;
979                 }
980
981
982                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
983                         /* simple bind specific check */
984                         if( op->o_ssf < ssf->sss_simple_bind ) {
985                                 rs->sr_text = op->o_ssf
986                                         ? "stronger confidentiality required"
987                                         : "confidentiality required";
988                                 return rs->sr_err;
989                         }
990                 }
991
992                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
993                         /* these checks don't apply to SASL bind */
994
995                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
996                                 rs->sr_text = op->o_sasl_ssf
997                                         ? "stronger SASL confidentiality required"
998                                         : "SASL confidentiality required";
999                                 return rs->sr_err;
1000                         }
1001
1002                         if( op->o_ssf < ssf->sss_ssf ) {
1003                                 rs->sr_text = op->o_ssf
1004                                         ? "stronger confidentiality required"
1005                                         : "confidentiality required";
1006                                 return rs->sr_err;
1007                         }
1008                 }
1009
1010                 if( updateop ) {
1011                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
1012                                 rs->sr_text = op->o_transport_ssf
1013                                         ? "stronger transport confidentiality required for update"
1014                                         : "transport confidentiality required for update";
1015                                 return rs->sr_err;
1016                         }
1017
1018                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
1019                                 rs->sr_text = op->o_tls_ssf
1020                                         ? "stronger TLS confidentiality required for update"
1021                                         : "TLS confidentiality required for update";
1022                                 return rs->sr_err;
1023                         }
1024
1025                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1026                                 rs->sr_text = op->o_sasl_ssf
1027                                         ? "stronger SASL confidentiality required for update"
1028                                         : "SASL confidentiality required for update";
1029                                 return rs->sr_err;
1030                         }
1031
1032                         if( op->o_ssf < ssf->sss_update_ssf ) {
1033                                 rs->sr_text = op->o_ssf
1034                                         ? "stronger confidentiality required for update"
1035                                         : "confidentiality required for update";
1036                                 return rs->sr_err;
1037                         }
1038
1039                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1040                                 BER_BVISEMPTY( &op->o_ndn ) )
1041                         {
1042                                 rs->sr_text = "modifications require authentication";
1043                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1044                                 return rs->sr_err;
1045                         }
1046
1047 #ifdef SLAP_X_LISTENER_MOD
1048                         if ( op->o_conn->c_listener &&
1049                                 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1050                                         ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1051                         {
1052                                 /* no "w" mode means readonly */
1053                                 rs->sr_text = "modifications not allowed on this listener";
1054                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1055                                 return rs->sr_err;
1056                         }
1057 #endif /* SLAP_X_LISTENER_MOD */
1058                 }
1059         }
1060
1061         if ( !session ) {
1062                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1063
1064                 if( requires & SLAP_REQUIRE_STRONG ) {
1065                         /* should check mechanism */
1066                         if( ( op->o_transport_ssf < ssf->sss_transport
1067                                 && op->o_authtype == LDAP_AUTH_SIMPLE )
1068                                 || BER_BVISEMPTY( &op->o_dn ) )
1069                         {
1070                                 rs->sr_text = "strong(er) authentication required";
1071                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1072                                 return rs->sr_err;
1073                         }
1074                 }
1075
1076                 if( requires & SLAP_REQUIRE_SASL ) {
1077                         if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1078                                 rs->sr_text = "SASL authentication required";
1079                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1080                                 return rs->sr_err;
1081                         }
1082                 }
1083                         
1084                 if( requires & SLAP_REQUIRE_AUTHC ) {
1085                         if( BER_BVISEMPTY( &op->o_dn ) ) {
1086                                 rs->sr_text = "authentication required";
1087                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1088                                 return rs->sr_err;
1089                         }
1090                 }
1091
1092                 if( requires & SLAP_REQUIRE_BIND ) {
1093                         int version;
1094                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1095                         version = op->o_conn->c_protocol;
1096                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1097
1098                         if( !version ) {
1099                                 /* no bind has occurred */
1100                                 rs->sr_text = "BIND required";
1101                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1102                                 return rs->sr_err;
1103                         }
1104                 }
1105
1106                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1107                         if( op->o_protocol < LDAP_VERSION3 ) {
1108                                 /* no bind has occurred */
1109                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1110                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1111                                 return rs->sr_err;
1112                         }
1113                 }
1114
1115 #ifdef SLAP_X_LISTENER_MOD
1116                 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1117                         if ( op->o_conn->c_listener &&
1118                                 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1119                 {
1120                                 /* no "x" mode means bind required */
1121                                 rs->sr_text = "bind required on this listener";
1122                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1123                                 return rs->sr_err;
1124                         }
1125                 }
1126
1127                 if ( !starttls && !updateop ) {
1128                         if ( op->o_conn->c_listener &&
1129                                 !( op->o_conn->c_listener->sl_perms &
1130                                         ( !BER_BVISEMPTY( &op->o_dn )
1131                                                 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1132                         {
1133                                 /* no "r" mode means no read */
1134                                 rs->sr_text = "read not allowed on this listener";
1135                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1136                                 return rs->sr_err;
1137                         }
1138                 }
1139 #endif /* SLAP_X_LISTENER_MOD */
1140
1141         }
1142
1143         if( ( restrictops & opflag )
1144                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1145                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1146                         rs->sr_text = "read operations restricted";
1147                 } else if ( restrictops & exopflag ) {
1148                         rs->sr_text = "extended operation restricted";
1149                 } else {
1150                         rs->sr_text = "operation restricted";
1151                 }
1152                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1153                 return rs->sr_err;
1154         }
1155
1156         rs->sr_err = LDAP_SUCCESS;
1157         return rs->sr_err;
1158 }
1159
1160 int backend_check_referrals( Operation *op, SlapReply *rs )
1161 {
1162         rs->sr_err = LDAP_SUCCESS;
1163
1164         if( op->o_bd->be_chk_referrals ) {
1165                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1166
1167                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1168                         send_ldap_result( op, rs );
1169                 }
1170         }
1171
1172         return rs->sr_err;
1173 }
1174
1175 int
1176 be_entry_get_rw(
1177         Operation *op,
1178         struct berval *ndn,
1179         ObjectClass *oc,
1180         AttributeDescription *at,
1181         int rw,
1182         Entry **e )
1183 {
1184         int rc;
1185
1186         *e = NULL;
1187
1188         if (op->o_bd == NULL) {
1189                 rc = LDAP_NO_SUCH_OBJECT;
1190         } else if ( op->o_bd->be_fetch ) {
1191                 rc = ( op->o_bd->be_fetch )( op, ndn,
1192                         oc, at, rw, e );
1193         } else {
1194                 rc = LDAP_UNWILLING_TO_PERFORM;
1195         }
1196         return rc;
1197 }
1198
1199 int 
1200 fe_acl_group(
1201         Operation *op,
1202         Entry   *target,
1203         struct berval *gr_ndn,
1204         struct berval *op_ndn,
1205         ObjectClass *group_oc,
1206         AttributeDescription *group_at )
1207 {
1208         Entry *e;
1209         Attribute *a;
1210         int rc;
1211         GroupAssertion *g;
1212         Backend *be = op->o_bd;
1213
1214         op->o_bd = select_backend( gr_ndn, 0, 0 );
1215
1216         for ( g = op->o_groups; g; g = g->ga_next ) {
1217                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1218                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1219                 {
1220                         continue;
1221                 }
1222                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1223                         break;
1224                 }
1225         }
1226
1227         if ( g ) {
1228                 rc = g->ga_res;
1229                 goto done;
1230         }
1231
1232         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1233                 e = target;
1234                 rc = 0;
1235         } else {
1236                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1237         }
1238         if ( e ) {
1239                 a = attr_find( e->e_attrs, group_at );
1240                 if ( a ) {
1241                         /* If the attribute is a subtype of labeledURI, treat this as
1242                          * a dynamic group ala groupOfURLs
1243                          */
1244                         if (is_at_subtype( group_at->ad_type,
1245                                 slap_schema.si_ad_labeledURI->ad_type ) )
1246                         {
1247                                 int i;
1248                                 LDAPURLDesc *ludp;
1249                                 struct berval bv, nbase;
1250                                 Filter *filter;
1251                                 Entry *user;
1252                                 Backend *b2 = op->o_bd;
1253
1254                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1255                                         user = target;
1256                                 } else {
1257                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1258                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1259                                 }
1260                                 
1261                                 if ( rc == 0 ) {
1262                                         rc = LDAP_COMPARE_FALSE;
1263                                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1264                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1265                                                         LDAP_URL_SUCCESS )
1266                                                 {
1267                                                         continue;
1268                                                 }
1269                                                 BER_BVZERO( &nbase );
1270                                                 /* host part must be empty */
1271                                                 /* attrs and extensions parts must be empty */
1272                                                 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1273                                                         ludp->lud_attrs || ludp->lud_exts )
1274                                                 {
1275                                                         goto loopit;
1276                                                 }
1277                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1278                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1279                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1280                                                 {
1281                                                         goto loopit;
1282                                                 }
1283                                                 switch ( ludp->lud_scope ) {
1284                                                 case LDAP_SCOPE_BASE:
1285                                                         if ( !dn_match( &nbase, op_ndn ) ) {
1286                                                                 goto loopit;
1287                                                         }
1288                                                         break;
1289                                                 case LDAP_SCOPE_ONELEVEL:
1290                                                         dnParent( op_ndn, &bv );
1291                                                         if ( !dn_match( &nbase, &bv ) ) {
1292                                                                 goto loopit;
1293                                                         }
1294                                                         break;
1295                                                 case LDAP_SCOPE_SUBTREE:
1296                                                         if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1297                                                                 goto loopit;
1298                                                         }
1299                                                         break;
1300 #ifdef LDAP_SCOPE_SUBORDINATE
1301                                                 case LDAP_SCOPE_SUBORDINATE:
1302                                                         if ( dn_match( &nbase, op_ndn ) ||
1303                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1304                                                         {
1305                                                                 goto loopit;
1306                                                         }
1307 #endif
1308                                                 }
1309                                                 filter = str2filter_x( op, ludp->lud_filter );
1310                                                 if ( filter ) {
1311                                                         if ( test_filter( NULL, user, filter ) ==
1312                                                                 LDAP_COMPARE_TRUE )
1313                                                         {
1314                                                                 rc = 0;
1315                                                         }
1316                                                         filter_free_x( op, filter );
1317                                                 }
1318 loopit:
1319                                                 ldap_free_urldesc( ludp );
1320                                                 if ( !BER_BVISNULL( &nbase ) ) {
1321                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1322                                                 }
1323                                                 if ( rc == 0 ) break;
1324                                         }
1325                                         if ( user != target ) {
1326                                                 be_entry_release_r( op, user );
1327                                         }
1328                                 }
1329                                 op->o_bd = b2;
1330                         } else {
1331                                 rc = value_find_ex( group_at,
1332                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1333                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1334                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1335                                 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1336                                         rc = LDAP_COMPARE_FALSE;
1337                         }
1338                 } else {
1339                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1340                 }
1341                 if (e != target ) {
1342                         be_entry_release_r( op, e );
1343                 }
1344         } else {
1345                 rc = LDAP_NO_SUCH_OBJECT;
1346         }
1347
1348         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1349                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1350                         op->o_tmpmemctx );
1351                 g->ga_be = op->o_bd;
1352                 g->ga_oc = group_oc;
1353                 g->ga_at = group_at;
1354                 g->ga_res = rc;
1355                 g->ga_len = gr_ndn->bv_len;
1356                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1357                 g->ga_next = op->o_groups;
1358                 op->o_groups = g;
1359         }
1360 done:
1361         op->o_bd = be;
1362         return rc;
1363 }
1364
1365 int 
1366 backend_group(
1367         Operation *op,
1368         Entry   *target,
1369         struct berval *gr_ndn,
1370         struct berval *op_ndn,
1371         ObjectClass *group_oc,
1372         AttributeDescription *group_at )
1373 {
1374         int                     rc;
1375         BackendDB               *be_orig;
1376
1377         if ( op->o_abandon ) {
1378                 return SLAPD_ABANDON;
1379         }
1380
1381         be_orig = op->o_bd;
1382         op->o_bd = frontendDB;
1383         rc = frontendDB->be_group( op, target, gr_ndn,
1384                 op_ndn, group_oc, group_at );
1385         op->o_bd = be_orig;
1386
1387         return rc;
1388 }
1389
1390 int 
1391 fe_acl_attribute(
1392         Operation *op,
1393         Entry   *target,
1394         struct berval   *edn,
1395         AttributeDescription *entry_at,
1396         BerVarray *vals,
1397         slap_access_t access )
1398 {
1399         Entry                   *e = NULL;
1400         Attribute               *a = NULL;
1401         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1402         AccessControlState      acl_state = ACL_STATE_INIT;
1403         Backend                 *be = op->o_bd;
1404
1405         op->o_bd = select_backend( edn, 0, 0 );
1406
1407         if ( target && dn_match( &target->e_nname, edn ) ) {
1408                 e = target;
1409
1410         } else {
1411                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1412         } 
1413
1414         if ( e ) {
1415                 a = attr_find( e->e_attrs, entry_at );
1416                 if ( a == NULL ) {
1417                         SlapReply       rs = { 0 };
1418                         AttributeName   anlist[ 2 ];
1419
1420                         anlist[ 0 ].an_name = entry_at->ad_cname;
1421                         anlist[ 0 ].an_desc = entry_at;
1422                         BER_BVZERO( &anlist[ 1 ].an_name );
1423                         rs.sr_attrs = anlist;
1424                         
1425                         /* NOTE: backend_operational() is also called
1426                          * when returning results, so it's supposed
1427                          * to do no harm to entries */
1428                         rs.sr_entry = e;
1429                         rc = backend_operational( op, &rs );
1430                         rs.sr_entry = NULL;
1431  
1432                         if ( rc == LDAP_SUCCESS ) {
1433                                 if ( rs.sr_operational_attrs ) {
1434                                         freeattr = 1;
1435                                         a = rs.sr_operational_attrs;
1436
1437                                 } else {
1438                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1439                                 }
1440                         }
1441                 }
1442
1443                 if ( a ) {
1444                         BerVarray v;
1445
1446                         if ( op->o_conn && access > ACL_NONE &&
1447                                 access_allowed( op, e, entry_at, NULL,
1448                                                 access, &acl_state ) == 0 )
1449                         {
1450                                 rc = LDAP_INSUFFICIENT_ACCESS;
1451                                 goto freeit;
1452                         }
1453
1454                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1455                                 ;
1456                         
1457                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1458                                 op->o_tmpmemctx );
1459                         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1460                         {
1461                                 if ( op->o_conn && access > ACL_NONE && 
1462                                         access_allowed( op, e, entry_at,
1463                                                         &a->a_nvals[i],
1464                                                         access,
1465                                                         &acl_state ) == 0 )
1466                                 {
1467                                         continue;
1468                                 }
1469                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1470                                                 op->o_tmpmemctx );
1471                                 if ( !BER_BVISNULL( &v[j] ) ) {
1472                                         j++;
1473                                 }
1474                         }
1475                         if ( j == 0 ) {
1476                                 op->o_tmpfree( v, op->o_tmpmemctx );
1477                                 *vals = NULL;
1478                                 rc = LDAP_INSUFFICIENT_ACCESS;
1479
1480                         } else {
1481                                 BER_BVZERO( &v[j] );
1482                                 *vals = v;
1483                                 rc = LDAP_SUCCESS;
1484                         }
1485                 }
1486 freeit:         if ( e != target ) {
1487                         be_entry_release_r( op, e );
1488                 }
1489                 if ( freeattr ) {
1490                         attr_free( a );
1491                 }
1492         }
1493
1494         op->o_bd = be;
1495         return rc;
1496 }
1497
1498 int 
1499 backend_attribute(
1500         Operation *op,
1501         Entry   *target,
1502         struct berval   *edn,
1503         AttributeDescription *entry_at,
1504         BerVarray *vals,
1505         slap_access_t access )
1506 {
1507         int                     rc;
1508         BackendDB               *be_orig;
1509
1510         be_orig = op->o_bd;
1511         op->o_bd = frontendDB;
1512         rc = frontendDB->be_attribute( op, target, edn,
1513                 entry_at, vals, access );
1514         op->o_bd = be_orig;
1515
1516         return rc;
1517 }
1518
1519 int 
1520 backend_access(
1521         Operation               *op,
1522         Entry                   *target,
1523         struct berval           *edn,
1524         AttributeDescription    *entry_at,
1525         struct berval           *nval,
1526         slap_access_t           access,
1527         slap_mask_t             *mask )
1528 {
1529         Entry           *e = NULL;
1530         int             rc = LDAP_INSUFFICIENT_ACCESS;
1531         Backend         *be = op->o_bd;
1532
1533         /* pedantic */
1534         assert( op != NULL );
1535         assert( op->o_conn != NULL );
1536         assert( edn != NULL );
1537         assert( access > ACL_NONE );
1538
1539         op->o_bd = select_backend( edn, 0, 0 );
1540
1541         if ( target && dn_match( &target->e_nname, edn ) ) {
1542                 e = target;
1543
1544         } else {
1545                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1546         } 
1547
1548         if ( e ) {
1549                 Attribute       *a = NULL;
1550                 int             freeattr = 0;
1551
1552                 if ( entry_at == NULL ) {
1553                         entry_at = slap_schema.si_ad_entry;
1554                 }
1555
1556                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1557                 {
1558                         if ( access_allowed_mask( op, e, entry_at,
1559                                         NULL, access, NULL, mask ) == 0 )
1560                         {
1561                                 rc = LDAP_INSUFFICIENT_ACCESS;
1562
1563                         } else {
1564                                 rc = LDAP_SUCCESS;
1565                         }
1566
1567                 } else {
1568                         a = attr_find( e->e_attrs, entry_at );
1569                         if ( a == NULL ) {
1570                                 SlapReply       rs = { 0 };
1571                                 AttributeName   anlist[ 2 ];
1572
1573                                 anlist[ 0 ].an_name = entry_at->ad_cname;
1574                                 anlist[ 0 ].an_desc = entry_at;
1575                                 BER_BVZERO( &anlist[ 1 ].an_name );
1576                                 rs.sr_attrs = anlist;
1577                         
1578                                 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1579
1580                                 /* NOTE: backend_operational() is also called
1581                                  * when returning results, so it's supposed
1582                                  * to do no harm to entries */
1583                                 rs.sr_entry = e;
1584                                 rc = backend_operational( op, &rs );
1585                                 rs.sr_entry = NULL;
1586
1587                                 if ( rc == LDAP_SUCCESS ) {
1588                                         if ( rs.sr_operational_attrs ) {
1589                                                 freeattr = 1;
1590                                                 a = rs.sr_operational_attrs;
1591
1592                                         } else {
1593                                                 rc = LDAP_NO_SUCH_OBJECT;
1594                                         }
1595                                 }
1596                         }
1597
1598                         if ( a ) {
1599                                 if ( access_allowed_mask( op, e, entry_at,
1600                                                 nval, access, NULL, mask ) == 0 )
1601                                 {
1602                                         rc = LDAP_INSUFFICIENT_ACCESS;
1603                                         goto freeit;
1604                                 }
1605                                 rc = LDAP_SUCCESS;
1606                         }
1607                 }
1608 freeit:         if ( e != target ) {
1609                         be_entry_release_r( op, e );
1610                 }
1611                 if ( freeattr ) {
1612                         attr_free( a );
1613                 }
1614         }
1615
1616         op->o_bd = be;
1617         return rc;
1618 }
1619
1620 int
1621 fe_aux_operational(
1622         Operation *op,
1623         SlapReply *rs )
1624 {
1625         Attribute               **ap;
1626         int                     rc = 0;
1627         BackendDB               *be_orig;
1628
1629         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1630                 /* just count them */ ;
1631
1632         /*
1633          * If operational attributes (allegedly) are required, 
1634          * and the backend supports specific operational attributes, 
1635          * add them to the attribute list
1636          */
1637         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1638                 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1639         {
1640                 *ap = slap_operational_entryDN( rs->sr_entry );
1641                 ap = &(*ap)->a_next;
1642         }
1643
1644         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1645                 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1646         {
1647                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1648                 ap = &(*ap)->a_next;
1649         }
1650
1651         if ( op->o_bd != NULL )
1652         {
1653                 /* Let the overlays have a chance at this */
1654                 be_orig = op->o_bd;
1655                 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
1656                 if ( op->o_bd != frontendDB &&
1657                         ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1658                         op->o_bd != NULL && op->o_bd->be_operational != NULL )
1659                 {
1660                         rc = op->o_bd->be_operational( op, rs );
1661                 }
1662                 op->o_bd = be_orig;
1663         }
1664
1665         return rc;
1666 }
1667
1668 int backend_operational( Operation *op, SlapReply *rs )
1669 {
1670         int rc;
1671         BackendDB *be_orig;
1672
1673         /* Moved this into the frontend so global overlays are called */
1674
1675         be_orig = op->o_bd;
1676         op->o_bd = frontendDB;
1677         rc = frontendDB->be_operational( op, rs );
1678         op->o_bd = be_orig;
1679
1680         return rc;
1681 }
1682