]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Update build tools based upon OPENLDAP_AC work
[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         /* temporarily removed */
839 #if 0
840         /* check should be generalized */
841         if( get_manageDIT(op) && !be_isroot(op)) {
842                 rs->sr_text = "requires manager authorization";
843                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
844         }
845 #endif
846
847 done:;
848         return rs->sr_err;
849 }
850
851 int
852 backend_check_restrictions(
853         Operation *op,
854         SlapReply *rs,
855         struct berval *opdata )
856 {
857         slap_mask_t restrictops;
858         slap_mask_t requires;
859         slap_mask_t opflag;
860         slap_mask_t exopflag = 0;
861         slap_ssf_set_t *ssf;
862         int updateop = 0;
863         int starttls = 0;
864         int session = 0;
865
866         if ( op->o_bd ) {
867                 int     rc = SLAP_CB_CONTINUE;
868
869                 if ( op->o_bd->be_chk_controls ) {
870                         rc = ( *op->o_bd->be_chk_controls )( op, rs );
871                 }
872
873                 if ( rc == SLAP_CB_CONTINUE ) {
874                         rc = backend_check_controls( op, rs );
875                 }
876
877                 if ( rc != LDAP_SUCCESS ) {
878                         return rs->sr_err;
879                 }
880
881                 restrictops = op->o_bd->be_restrictops;
882                 requires = op->o_bd->be_requires;
883                 ssf = &op->o_bd->be_ssf_set;
884
885         } else {
886                 restrictops = frontendDB->be_restrictops;
887                 requires = frontendDB->be_requires;
888                 ssf = &frontendDB->be_ssf_set;
889         }
890
891         switch( op->o_tag ) {
892         case LDAP_REQ_ADD:
893                 opflag = SLAP_RESTRICT_OP_ADD;
894                 updateop++;
895                 break;
896         case LDAP_REQ_BIND:
897                 opflag = SLAP_RESTRICT_OP_BIND;
898                 session++;
899                 break;
900         case LDAP_REQ_COMPARE:
901                 opflag = SLAP_RESTRICT_OP_COMPARE;
902                 break;
903         case LDAP_REQ_DELETE:
904                 updateop++;
905                 opflag = SLAP_RESTRICT_OP_DELETE;
906                 break;
907         case LDAP_REQ_EXTENDED:
908                 opflag = SLAP_RESTRICT_OP_EXTENDED;
909
910                 if( !opdata ) {
911                         /* treat unspecified as a modify */
912                         opflag = SLAP_RESTRICT_OP_MODIFY;
913                         updateop++;
914                         break;
915                 }
916
917                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
918                         session++;
919                         starttls++;
920                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
921                         break;
922                 }
923
924                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
925                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
926                         break;
927                 }
928
929                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
930                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
931                         break;
932                 }
933
934                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
935                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
936                         updateop++;
937                         break;
938                 }
939
940                 /* treat everything else as a modify */
941                 opflag = SLAP_RESTRICT_OP_MODIFY;
942                 updateop++;
943                 break;
944
945         case LDAP_REQ_MODIFY:
946                 updateop++;
947                 opflag = SLAP_RESTRICT_OP_MODIFY;
948                 break;
949         case LDAP_REQ_RENAME:
950                 updateop++;
951                 opflag = SLAP_RESTRICT_OP_RENAME;
952                 break;
953         case LDAP_REQ_SEARCH:
954                 opflag = SLAP_RESTRICT_OP_SEARCH;
955                 break;
956         case LDAP_REQ_UNBIND:
957                 session++;
958                 opflag = 0;
959                 break;
960         default:
961                 rs->sr_text = "restrict operations internal error";
962                 rs->sr_err = LDAP_OTHER;
963                 return rs->sr_err;
964         }
965
966         if ( !starttls ) {
967                 /* these checks don't apply to StartTLS */
968
969                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
970                 if( op->o_transport_ssf < ssf->sss_transport ) {
971                         rs->sr_text = op->o_transport_ssf
972                                 ? "stronger transport confidentiality required"
973                                 : "transport confidentiality required";
974                         return rs->sr_err;
975                 }
976
977                 if( op->o_tls_ssf < ssf->sss_tls ) {
978                         rs->sr_text = op->o_tls_ssf
979                                 ? "stronger TLS confidentiality required"
980                                 : "TLS confidentiality required";
981                         return rs->sr_err;
982                 }
983
984
985                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
986                         /* simple bind specific check */
987                         if( op->o_ssf < ssf->sss_simple_bind ) {
988                                 rs->sr_text = op->o_ssf
989                                         ? "stronger confidentiality required"
990                                         : "confidentiality required";
991                                 return rs->sr_err;
992                         }
993                 }
994
995                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
996                         /* these checks don't apply to SASL bind */
997
998                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
999                                 rs->sr_text = op->o_sasl_ssf
1000                                         ? "stronger SASL confidentiality required"
1001                                         : "SASL confidentiality required";
1002                                 return rs->sr_err;
1003                         }
1004
1005                         if( op->o_ssf < ssf->sss_ssf ) {
1006                                 rs->sr_text = op->o_ssf
1007                                         ? "stronger confidentiality required"
1008                                         : "confidentiality required";
1009                                 return rs->sr_err;
1010                         }
1011                 }
1012
1013                 if( updateop ) {
1014                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
1015                                 rs->sr_text = op->o_transport_ssf
1016                                         ? "stronger transport confidentiality required for update"
1017                                         : "transport confidentiality required for update";
1018                                 return rs->sr_err;
1019                         }
1020
1021                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
1022                                 rs->sr_text = op->o_tls_ssf
1023                                         ? "stronger TLS confidentiality required for update"
1024                                         : "TLS confidentiality required for update";
1025                                 return rs->sr_err;
1026                         }
1027
1028                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1029                                 rs->sr_text = op->o_sasl_ssf
1030                                         ? "stronger SASL confidentiality required for update"
1031                                         : "SASL confidentiality required for update";
1032                                 return rs->sr_err;
1033                         }
1034
1035                         if( op->o_ssf < ssf->sss_update_ssf ) {
1036                                 rs->sr_text = op->o_ssf
1037                                         ? "stronger confidentiality required for update"
1038                                         : "confidentiality required for update";
1039                                 return rs->sr_err;
1040                         }
1041
1042                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1043                                 BER_BVISEMPTY( &op->o_ndn ) )
1044                         {
1045                                 rs->sr_text = "modifications require authentication";
1046                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1047                                 return rs->sr_err;
1048                         }
1049
1050 #ifdef SLAP_X_LISTENER_MOD
1051                         if ( op->o_conn->c_listener &&
1052                                 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1053                                         ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1054                         {
1055                                 /* no "w" mode means readonly */
1056                                 rs->sr_text = "modifications not allowed on this listener";
1057                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1058                                 return rs->sr_err;
1059                         }
1060 #endif /* SLAP_X_LISTENER_MOD */
1061                 }
1062         }
1063
1064         if ( !session ) {
1065                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1066
1067                 if( requires & SLAP_REQUIRE_STRONG ) {
1068                         /* should check mechanism */
1069                         if( ( op->o_transport_ssf < ssf->sss_transport
1070                                 && op->o_authtype == LDAP_AUTH_SIMPLE )
1071                                 || BER_BVISEMPTY( &op->o_dn ) )
1072                         {
1073                                 rs->sr_text = "strong(er) authentication required";
1074                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1075                                 return rs->sr_err;
1076                         }
1077                 }
1078
1079                 if( requires & SLAP_REQUIRE_SASL ) {
1080                         if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1081                                 rs->sr_text = "SASL authentication required";
1082                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1083                                 return rs->sr_err;
1084                         }
1085                 }
1086                         
1087                 if( requires & SLAP_REQUIRE_AUTHC ) {
1088                         if( BER_BVISEMPTY( &op->o_dn ) ) {
1089                                 rs->sr_text = "authentication required";
1090                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1091                                 return rs->sr_err;
1092                         }
1093                 }
1094
1095                 if( requires & SLAP_REQUIRE_BIND ) {
1096                         int version;
1097                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1098                         version = op->o_conn->c_protocol;
1099                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1100
1101                         if( !version ) {
1102                                 /* no bind has occurred */
1103                                 rs->sr_text = "BIND required";
1104                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1105                                 return rs->sr_err;
1106                         }
1107                 }
1108
1109                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1110                         if( op->o_protocol < LDAP_VERSION3 ) {
1111                                 /* no bind has occurred */
1112                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1113                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1114                                 return rs->sr_err;
1115                         }
1116                 }
1117
1118 #ifdef SLAP_X_LISTENER_MOD
1119                 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1120                         if ( op->o_conn->c_listener &&
1121                                 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1122                 {
1123                                 /* no "x" mode means bind required */
1124                                 rs->sr_text = "bind required on this listener";
1125                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1126                                 return rs->sr_err;
1127                         }
1128                 }
1129
1130                 if ( !starttls && !updateop ) {
1131                         if ( op->o_conn->c_listener &&
1132                                 !( op->o_conn->c_listener->sl_perms &
1133                                         ( !BER_BVISEMPTY( &op->o_dn )
1134                                                 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1135                         {
1136                                 /* no "r" mode means no read */
1137                                 rs->sr_text = "read not allowed on this listener";
1138                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1139                                 return rs->sr_err;
1140                         }
1141                 }
1142 #endif /* SLAP_X_LISTENER_MOD */
1143
1144         }
1145
1146         if( ( restrictops & opflag )
1147                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1148                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1149                         rs->sr_text = "read operations restricted";
1150                 } else if ( restrictops & exopflag ) {
1151                         rs->sr_text = "extended operation restricted";
1152                 } else {
1153                         rs->sr_text = "operation restricted";
1154                 }
1155                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1156                 return rs->sr_err;
1157         }
1158
1159         rs->sr_err = LDAP_SUCCESS;
1160         return rs->sr_err;
1161 }
1162
1163 int backend_check_referrals( Operation *op, SlapReply *rs )
1164 {
1165         rs->sr_err = LDAP_SUCCESS;
1166
1167         if( op->o_bd->be_chk_referrals ) {
1168                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1169
1170                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1171                         send_ldap_result( op, rs );
1172                 }
1173         }
1174
1175         return rs->sr_err;
1176 }
1177
1178 int
1179 be_entry_get_rw(
1180         Operation *op,
1181         struct berval *ndn,
1182         ObjectClass *oc,
1183         AttributeDescription *at,
1184         int rw,
1185         Entry **e )
1186 {
1187         int rc;
1188
1189         *e = NULL;
1190
1191         if (op->o_bd == NULL) {
1192                 rc = LDAP_NO_SUCH_OBJECT;
1193         } else if ( op->o_bd->be_fetch ) {
1194                 rc = ( op->o_bd->be_fetch )( op, ndn,
1195                         oc, at, rw, e );
1196         } else {
1197                 rc = LDAP_UNWILLING_TO_PERFORM;
1198         }
1199         return rc;
1200 }
1201
1202 int 
1203 fe_acl_group(
1204         Operation *op,
1205         Entry   *target,
1206         struct berval *gr_ndn,
1207         struct berval *op_ndn,
1208         ObjectClass *group_oc,
1209         AttributeDescription *group_at )
1210 {
1211         Entry *e;
1212         Attribute *a;
1213         int rc;
1214         GroupAssertion *g;
1215         Backend *be = op->o_bd;
1216
1217         op->o_bd = select_backend( gr_ndn, 0, 0 );
1218
1219         for ( g = op->o_groups; g; g = g->ga_next ) {
1220                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1221                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1222                 {
1223                         continue;
1224                 }
1225                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1226                         break;
1227                 }
1228         }
1229
1230         if ( g ) {
1231                 rc = g->ga_res;
1232                 goto done;
1233         }
1234
1235         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1236                 e = target;
1237                 rc = 0;
1238         } else {
1239                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1240         }
1241         if ( e ) {
1242                 a = attr_find( e->e_attrs, group_at );
1243                 if ( a ) {
1244                         /* If the attribute is a subtype of labeledURI, treat this as
1245                          * a dynamic group ala groupOfURLs
1246                          */
1247                         if (is_at_subtype( group_at->ad_type,
1248                                 slap_schema.si_ad_labeledURI->ad_type ) )
1249                         {
1250                                 int i;
1251                                 LDAPURLDesc *ludp;
1252                                 struct berval bv, nbase;
1253                                 Filter *filter;
1254                                 Entry *user;
1255                                 Backend *b2 = op->o_bd;
1256
1257                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1258                                         user = target;
1259                                 } else {
1260                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1261                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1262                                 }
1263                                 
1264                                 if ( rc == 0 ) {
1265                                         rc = LDAP_COMPARE_FALSE;
1266                                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1267                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1268                                                         LDAP_URL_SUCCESS )
1269                                                 {
1270                                                         continue;
1271                                                 }
1272                                                 BER_BVZERO( &nbase );
1273                                                 /* host part must be empty */
1274                                                 /* attrs and extensions parts must be empty */
1275                                                 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1276                                                         ludp->lud_attrs || ludp->lud_exts )
1277                                                 {
1278                                                         goto loopit;
1279                                                 }
1280                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1281                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1282                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1283                                                 {
1284                                                         goto loopit;
1285                                                 }
1286                                                 switch ( ludp->lud_scope ) {
1287                                                 case LDAP_SCOPE_BASE:
1288                                                         if ( !dn_match( &nbase, op_ndn ) ) {
1289                                                                 goto loopit;
1290                                                         }
1291                                                         break;
1292                                                 case LDAP_SCOPE_ONELEVEL:
1293                                                         dnParent( op_ndn, &bv );
1294                                                         if ( !dn_match( &nbase, &bv ) ) {
1295                                                                 goto loopit;
1296                                                         }
1297                                                         break;
1298                                                 case LDAP_SCOPE_SUBTREE:
1299                                                         if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1300                                                                 goto loopit;
1301                                                         }
1302                                                         break;
1303 #ifdef LDAP_SCOPE_SUBORDINATE
1304                                                 case LDAP_SCOPE_SUBORDINATE:
1305                                                         if ( dn_match( &nbase, op_ndn ) ||
1306                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1307                                                         {
1308                                                                 goto loopit;
1309                                                         }
1310 #endif
1311                                                 }
1312                                                 filter = str2filter_x( op, ludp->lud_filter );
1313                                                 if ( filter ) {
1314                                                         if ( test_filter( NULL, user, filter ) ==
1315                                                                 LDAP_COMPARE_TRUE )
1316                                                         {
1317                                                                 rc = 0;
1318                                                         }
1319                                                         filter_free_x( op, filter );
1320                                                 }
1321 loopit:
1322                                                 ldap_free_urldesc( ludp );
1323                                                 if ( !BER_BVISNULL( &nbase ) ) {
1324                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1325                                                 }
1326                                                 if ( rc == 0 ) break;
1327                                         }
1328                                         if ( user != target ) {
1329                                                 be_entry_release_r( op, user );
1330                                         }
1331                                 }
1332                                 op->o_bd = b2;
1333                         } else {
1334                                 rc = value_find_ex( group_at,
1335                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1336                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1337                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1338                                 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1339                                         rc = LDAP_COMPARE_FALSE;
1340                         }
1341                 } else {
1342                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1343                 }
1344                 if (e != target ) {
1345                         be_entry_release_r( op, e );
1346                 }
1347         } else {
1348                 rc = LDAP_NO_SUCH_OBJECT;
1349         }
1350
1351         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1352                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1353                         op->o_tmpmemctx );
1354                 g->ga_be = op->o_bd;
1355                 g->ga_oc = group_oc;
1356                 g->ga_at = group_at;
1357                 g->ga_res = rc;
1358                 g->ga_len = gr_ndn->bv_len;
1359                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1360                 g->ga_next = op->o_groups;
1361                 op->o_groups = g;
1362         }
1363 done:
1364         op->o_bd = be;
1365         return rc;
1366 }
1367
1368 int 
1369 backend_group(
1370         Operation *op,
1371         Entry   *target,
1372         struct berval *gr_ndn,
1373         struct berval *op_ndn,
1374         ObjectClass *group_oc,
1375         AttributeDescription *group_at )
1376 {
1377         int                     rc;
1378         BackendDB               *be_orig;
1379
1380         if ( op->o_abandon ) {
1381                 return SLAPD_ABANDON;
1382         }
1383
1384         be_orig = op->o_bd;
1385         op->o_bd = frontendDB;
1386 #ifdef SLAP_OVERLAY_ACCESS
1387         rc = frontendDB->be_group( op, target, gr_ndn,
1388                 op_ndn, group_oc, group_at );
1389 #else /* ! SLAP_OVERLAY_ACCESS */
1390         rc = fe_acl_group( op, target, gr_ndn,
1391                 op_ndn, group_oc, group_at );
1392 #endif /* ! SLAP_OVERLAY_ACCESS */
1393         op->o_bd = be_orig;
1394
1395         return rc;
1396 }
1397
1398 int 
1399 fe_acl_attribute(
1400         Operation *op,
1401         Entry   *target,
1402         struct berval   *edn,
1403         AttributeDescription *entry_at,
1404         BerVarray *vals,
1405         slap_access_t access )
1406 {
1407         Entry                   *e = NULL;
1408         Attribute               *a = NULL;
1409         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1410         AccessControlState      acl_state = ACL_STATE_INIT;
1411         Backend                 *be = op->o_bd;
1412
1413         op->o_bd = select_backend( edn, 0, 0 );
1414
1415         if ( target && dn_match( &target->e_nname, edn ) ) {
1416                 e = target;
1417
1418         } else {
1419                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1420         } 
1421
1422         if ( e ) {
1423                 a = attr_find( e->e_attrs, entry_at );
1424                 if ( a == NULL ) {
1425                         SlapReply       rs = { 0 };
1426                         AttributeName   anlist[ 2 ];
1427
1428                         anlist[ 0 ].an_name = entry_at->ad_cname;
1429                         anlist[ 0 ].an_desc = entry_at;
1430                         BER_BVZERO( &anlist[ 1 ].an_name );
1431                         rs.sr_attrs = anlist;
1432                         
1433                         /* NOTE: backend_operational() is also called
1434                          * when returning results, so it's supposed
1435                          * to do no harm to entries */
1436                         rs.sr_entry = e;
1437                         rc = backend_operational( op, &rs );
1438                         rs.sr_entry = NULL;
1439  
1440                         if ( rc == LDAP_SUCCESS ) {
1441                                 if ( rs.sr_operational_attrs ) {
1442                                         freeattr = 1;
1443                                         a = rs.sr_operational_attrs;
1444
1445                                 } else {
1446                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1447                                 }
1448                         }
1449                 }
1450
1451                 if ( a ) {
1452                         BerVarray v;
1453
1454                         if ( op->o_conn && access > ACL_NONE &&
1455                                 access_allowed( op, e, entry_at, NULL,
1456                                                 access, &acl_state ) == 0 )
1457                         {
1458                                 rc = LDAP_INSUFFICIENT_ACCESS;
1459                                 goto freeit;
1460                         }
1461
1462                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1463                                 ;
1464                         
1465                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1466                                 op->o_tmpmemctx );
1467                         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1468                         {
1469                                 if ( op->o_conn && access > ACL_NONE && 
1470                                         access_allowed( op, e, entry_at,
1471                                                         &a->a_nvals[i],
1472                                                         access,
1473                                                         &acl_state ) == 0 )
1474                                 {
1475                                         continue;
1476                                 }
1477                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1478                                                 op->o_tmpmemctx );
1479                                 if ( !BER_BVISNULL( &v[j] ) ) {
1480                                         j++;
1481                                 }
1482                         }
1483                         if ( j == 0 ) {
1484                                 op->o_tmpfree( v, op->o_tmpmemctx );
1485                                 *vals = NULL;
1486                                 rc = LDAP_INSUFFICIENT_ACCESS;
1487
1488                         } else {
1489                                 BER_BVZERO( &v[j] );
1490                                 *vals = v;
1491                                 rc = LDAP_SUCCESS;
1492                         }
1493                 }
1494 freeit:         if ( e != target ) {
1495                         be_entry_release_r( op, e );
1496                 }
1497                 if ( freeattr ) {
1498                         attr_free( a );
1499                 }
1500         }
1501
1502         op->o_bd = be;
1503         return rc;
1504 }
1505
1506 int 
1507 backend_attribute(
1508         Operation *op,
1509         Entry   *target,
1510         struct berval   *edn,
1511         AttributeDescription *entry_at,
1512         BerVarray *vals,
1513         slap_access_t access )
1514 {
1515         int                     rc;
1516         BackendDB               *be_orig;
1517
1518         be_orig = op->o_bd;
1519         op->o_bd = frontendDB;
1520 #ifdef SLAP_OVERLAY_ACCESS
1521         rc = frontendDB->be_attribute( op, target, edn,
1522                 entry_at, vals, access );
1523 #else /* !SLAP_OVERLAY_ACCESS */
1524         rc = fe_acl_attribute( op, target, edn,
1525                 entry_at, vals, access );
1526 #endif /* !SLAP_OVERLAY_ACCESS */
1527         op->o_bd = be_orig;
1528
1529         return rc;
1530 }
1531
1532 int 
1533 backend_access(
1534         Operation               *op,
1535         Entry                   *target,
1536         struct berval           *edn,
1537         AttributeDescription    *entry_at,
1538         struct berval           *nval,
1539         slap_access_t           access,
1540         slap_mask_t             *mask )
1541 {
1542         Entry           *e = NULL;
1543         int             rc = LDAP_INSUFFICIENT_ACCESS;
1544         Backend         *be = op->o_bd;
1545
1546         /* pedantic */
1547         assert( op != NULL );
1548         assert( op->o_conn != NULL );
1549         assert( edn != NULL );
1550         assert( access > ACL_NONE );
1551
1552         op->o_bd = select_backend( edn, 0, 0 );
1553
1554         if ( target && dn_match( &target->e_nname, edn ) ) {
1555                 e = target;
1556
1557         } else {
1558                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1559         } 
1560
1561         if ( e ) {
1562                 Attribute       *a = NULL;
1563                 int             freeattr = 0;
1564
1565                 if ( entry_at == NULL ) {
1566                         entry_at = slap_schema.si_ad_entry;
1567                 }
1568
1569                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1570                 {
1571                         if ( access_allowed_mask( op, e, entry_at,
1572                                         NULL, access, NULL, mask ) == 0 )
1573                         {
1574                                 rc = LDAP_INSUFFICIENT_ACCESS;
1575
1576                         } else {
1577                                 rc = LDAP_SUCCESS;
1578                         }
1579
1580                 } else {
1581                         a = attr_find( e->e_attrs, entry_at );
1582                         if ( a == NULL ) {
1583                                 SlapReply       rs = { 0 };
1584                                 AttributeName   anlist[ 2 ];
1585
1586                                 anlist[ 0 ].an_name = entry_at->ad_cname;
1587                                 anlist[ 0 ].an_desc = entry_at;
1588                                 BER_BVZERO( &anlist[ 1 ].an_name );
1589                                 rs.sr_attrs = anlist;
1590                         
1591                                 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1592
1593                                 /* NOTE: backend_operational() is also called
1594                                  * when returning results, so it's supposed
1595                                  * to do no harm to entries */
1596                                 rs.sr_entry = e;
1597                                 rc = backend_operational( op, &rs );
1598                                 rs.sr_entry = NULL;
1599
1600                                 if ( rc == LDAP_SUCCESS ) {
1601                                         if ( rs.sr_operational_attrs ) {
1602                                                 freeattr = 1;
1603                                                 a = rs.sr_operational_attrs;
1604
1605                                         } else {
1606                                                 rc = LDAP_NO_SUCH_OBJECT;
1607                                         }
1608                                 }
1609                         }
1610
1611                         if ( a ) {
1612                                 if ( access_allowed_mask( op, e, entry_at,
1613                                                 nval, access, NULL, mask ) == 0 )
1614                                 {
1615                                         rc = LDAP_INSUFFICIENT_ACCESS;
1616                                         goto freeit;
1617                                 }
1618                                 rc = LDAP_SUCCESS;
1619                         }
1620                 }
1621 freeit:         if ( e != target ) {
1622                         be_entry_release_r( op, e );
1623                 }
1624                 if ( freeattr ) {
1625                         attr_free( a );
1626                 }
1627         }
1628
1629         op->o_bd = be;
1630         return rc;
1631 }
1632
1633 int
1634 fe_aux_operational(
1635         Operation *op,
1636         SlapReply *rs )
1637 {
1638         Attribute               **ap;
1639         int                     rc = 0;
1640         BackendDB               *be_orig;
1641
1642         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1643                 /* just count them */ ;
1644
1645         /*
1646          * If operational attributes (allegedly) are required, 
1647          * and the backend supports specific operational attributes, 
1648          * add them to the attribute list
1649          */
1650         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1651                 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1652         {
1653                 *ap = slap_operational_entryDN( rs->sr_entry );
1654                 ap = &(*ap)->a_next;
1655         }
1656
1657         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1658                 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1659         {
1660                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1661                 ap = &(*ap)->a_next;
1662         }
1663
1664         if ( op->o_bd != NULL )
1665         {
1666                 /* Let the overlays have a chance at this */
1667                 be_orig = op->o_bd;
1668                 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
1669                 if ( !be_match( op->o_bd, frontendDB ) &&
1670                         ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1671                         op->o_bd != NULL && op->o_bd->be_operational != NULL )
1672                 {
1673                         rc = op->o_bd->be_operational( op, rs );
1674                 }
1675                 op->o_bd = be_orig;
1676         }
1677
1678         return rc;
1679 }
1680
1681 int backend_operational( Operation *op, SlapReply *rs )
1682 {
1683         int rc;
1684         BackendDB *be_orig;
1685
1686         /* Moved this into the frontend so global overlays are called */
1687
1688         be_orig = op->o_bd;
1689         op->o_bd = frontendDB;
1690         rc = frontendDB->be_operational( op, rs );
1691         op->o_bd = be_orig;
1692
1693         return rc;
1694 }
1695