]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
25e009e1ca56b1ba65d329f216c2770617fc47db
[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         *e = NULL;
1188
1189         if ( op->o_bd == NULL ) {
1190                 return LDAP_NO_SUCH_OBJECT;
1191         }
1192
1193         if ( op->o_bd->be_fetch ) {
1194                 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1195         }
1196
1197         return LDAP_UNWILLING_TO_PERFORM;
1198 }
1199
1200 int 
1201 fe_acl_group(
1202         Operation *op,
1203         Entry   *target,
1204         struct berval *gr_ndn,
1205         struct berval *op_ndn,
1206         ObjectClass *group_oc,
1207         AttributeDescription *group_at )
1208 {
1209         Entry *e;
1210         Attribute *a;
1211         int rc;
1212         GroupAssertion *g;
1213         Backend *be = op->o_bd;
1214
1215         op->o_bd = select_backend( gr_ndn, 0, 0 );
1216
1217         for ( g = op->o_groups; g; g = g->ga_next ) {
1218                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1219                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1220                 {
1221                         continue;
1222                 }
1223                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1224                         break;
1225                 }
1226         }
1227
1228         if ( g ) {
1229                 rc = g->ga_res;
1230                 goto done;
1231         }
1232
1233         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1234                 e = target;
1235                 rc = 0;
1236         } else {
1237                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1238         }
1239         if ( e ) {
1240                 a = attr_find( e->e_attrs, group_at );
1241                 if ( a ) {
1242                         /* If the attribute is a subtype of labeledURI, treat this as
1243                          * a dynamic group ala groupOfURLs
1244                          */
1245                         if (is_at_subtype( group_at->ad_type,
1246                                 slap_schema.si_ad_labeledURI->ad_type ) )
1247                         {
1248                                 int i;
1249                                 LDAPURLDesc *ludp;
1250                                 struct berval bv, nbase;
1251                                 Filter *filter;
1252                                 Entry *user;
1253                                 Backend *b2 = op->o_bd;
1254
1255                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1256                                         user = target;
1257                                 } else {
1258                                         op->o_bd = select_backend( op_ndn, 0, 0 );
1259                                         rc = be_entry_get_rw(op, op_ndn, NULL, NULL, 0, &user );
1260                                 }
1261                                 
1262                                 if ( rc == 0 ) {
1263                                         rc = LDAP_COMPARE_FALSE;
1264                                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1265                                                 if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1266                                                         LDAP_URL_SUCCESS )
1267                                                 {
1268                                                         continue;
1269                                                 }
1270                                                 BER_BVZERO( &nbase );
1271                                                 /* host part must be empty */
1272                                                 /* attrs and extensions parts must be empty */
1273                                                 if ( ( ludp->lud_host && *ludp->lud_host ) ||
1274                                                         ludp->lud_attrs || ludp->lud_exts )
1275                                                 {
1276                                                         goto loopit;
1277                                                 }
1278                                                 ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1279                                                 if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1280                                                         op->o_tmpmemctx ) != LDAP_SUCCESS )
1281                                                 {
1282                                                         goto loopit;
1283                                                 }
1284                                                 switch ( ludp->lud_scope ) {
1285                                                 case LDAP_SCOPE_BASE:
1286                                                         if ( !dn_match( &nbase, op_ndn ) ) {
1287                                                                 goto loopit;
1288                                                         }
1289                                                         break;
1290                                                 case LDAP_SCOPE_ONELEVEL:
1291                                                         dnParent( op_ndn, &bv );
1292                                                         if ( !dn_match( &nbase, &bv ) ) {
1293                                                                 goto loopit;
1294                                                         }
1295                                                         break;
1296                                                 case LDAP_SCOPE_SUBTREE:
1297                                                         if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1298                                                                 goto loopit;
1299                                                         }
1300                                                         break;
1301 #ifdef LDAP_SCOPE_SUBORDINATE
1302                                                 case LDAP_SCOPE_SUBORDINATE:
1303                                                         if ( dn_match( &nbase, op_ndn ) ||
1304                                                                 !dnIsSuffix( op_ndn, &nbase ) )
1305                                                         {
1306                                                                 goto loopit;
1307                                                         }
1308 #endif
1309                                                 }
1310                                                 filter = str2filter_x( op, ludp->lud_filter );
1311                                                 if ( filter ) {
1312                                                         if ( test_filter( NULL, user, filter ) ==
1313                                                                 LDAP_COMPARE_TRUE )
1314                                                         {
1315                                                                 rc = 0;
1316                                                         }
1317                                                         filter_free_x( op, filter );
1318                                                 }
1319 loopit:
1320                                                 ldap_free_urldesc( ludp );
1321                                                 if ( !BER_BVISNULL( &nbase ) ) {
1322                                                         op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1323                                                 }
1324                                                 if ( rc == 0 ) break;
1325                                         }
1326                                         if ( user != target ) {
1327                                                 be_entry_release_r( op, user );
1328                                         }
1329                                 }
1330                                 op->o_bd = b2;
1331                         } else {
1332                                 rc = value_find_ex( group_at,
1333                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1334                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1335                                 a->a_nvals, op_ndn, op->o_tmpmemctx );
1336                                 if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
1337                                         rc = LDAP_COMPARE_FALSE;
1338                         }
1339                 } else {
1340                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1341                 }
1342                 if (e != target ) {
1343                         be_entry_release_r( op, e );
1344                 }
1345         } else {
1346                 rc = LDAP_NO_SUCH_OBJECT;
1347         }
1348
1349         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1350                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1351                         op->o_tmpmemctx );
1352                 g->ga_be = op->o_bd;
1353                 g->ga_oc = group_oc;
1354                 g->ga_at = group_at;
1355                 g->ga_res = rc;
1356                 g->ga_len = gr_ndn->bv_len;
1357                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1358                 g->ga_next = op->o_groups;
1359                 op->o_groups = g;
1360         }
1361 done:
1362         op->o_bd = be;
1363         return rc;
1364 }
1365
1366 int 
1367 backend_group(
1368         Operation *op,
1369         Entry   *target,
1370         struct berval *gr_ndn,
1371         struct berval *op_ndn,
1372         ObjectClass *group_oc,
1373         AttributeDescription *group_at )
1374 {
1375         int                     rc;
1376         BackendDB               *be_orig;
1377
1378         if ( op->o_abandon ) {
1379                 return SLAPD_ABANDON;
1380         }
1381
1382         be_orig = op->o_bd;
1383         op->o_bd = frontendDB;
1384 #ifdef SLAP_OVERLAY_ACCESS
1385         rc = frontendDB->be_group( op, target, gr_ndn,
1386                 op_ndn, group_oc, group_at );
1387 #else /* ! SLAP_OVERLAY_ACCESS */
1388         rc = fe_acl_group( op, target, gr_ndn,
1389                 op_ndn, group_oc, group_at );
1390 #endif /* ! SLAP_OVERLAY_ACCESS */
1391         op->o_bd = be_orig;
1392
1393         return rc;
1394 }
1395
1396 int 
1397 fe_acl_attribute(
1398         Operation *op,
1399         Entry   *target,
1400         struct berval   *edn,
1401         AttributeDescription *entry_at,
1402         BerVarray *vals,
1403         slap_access_t access )
1404 {
1405         Entry                   *e = NULL;
1406         Attribute               *a = NULL;
1407         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1408         AccessControlState      acl_state = ACL_STATE_INIT;
1409         Backend                 *be = op->o_bd;
1410
1411         op->o_bd = select_backend( edn, 0, 0 );
1412
1413         if ( target && dn_match( &target->e_nname, edn ) ) {
1414                 e = target;
1415
1416         } else {
1417                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1418         } 
1419
1420         if ( e ) {
1421                 a = attr_find( e->e_attrs, entry_at );
1422                 if ( a == NULL ) {
1423                         SlapReply       rs = { 0 };
1424                         AttributeName   anlist[ 2 ];
1425
1426                         anlist[ 0 ].an_name = entry_at->ad_cname;
1427                         anlist[ 0 ].an_desc = entry_at;
1428                         BER_BVZERO( &anlist[ 1 ].an_name );
1429                         rs.sr_attrs = anlist;
1430                         
1431                         /* NOTE: backend_operational() is also called
1432                          * when returning results, so it's supposed
1433                          * to do no harm to entries */
1434                         rs.sr_entry = e;
1435                         rc = backend_operational( op, &rs );
1436                         rs.sr_entry = NULL;
1437  
1438                         if ( rc == LDAP_SUCCESS ) {
1439                                 if ( rs.sr_operational_attrs ) {
1440                                         freeattr = 1;
1441                                         a = rs.sr_operational_attrs;
1442
1443                                 } else {
1444                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1445                                 }
1446                         }
1447                 }
1448
1449                 if ( a ) {
1450                         BerVarray v;
1451
1452                         if ( op->o_conn && access > ACL_NONE &&
1453                                 access_allowed( op, e, entry_at, NULL,
1454                                                 access, &acl_state ) == 0 )
1455                         {
1456                                 rc = LDAP_INSUFFICIENT_ACCESS;
1457                                 goto freeit;
1458                         }
1459
1460                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1461                                 ;
1462                         
1463                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1464                                 op->o_tmpmemctx );
1465                         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1466                         {
1467                                 if ( op->o_conn && access > ACL_NONE && 
1468                                         access_allowed( op, e, entry_at,
1469                                                         &a->a_nvals[i],
1470                                                         access,
1471                                                         &acl_state ) == 0 )
1472                                 {
1473                                         continue;
1474                                 }
1475                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1476                                                 op->o_tmpmemctx );
1477                                 if ( !BER_BVISNULL( &v[j] ) ) {
1478                                         j++;
1479                                 }
1480                         }
1481                         if ( j == 0 ) {
1482                                 op->o_tmpfree( v, op->o_tmpmemctx );
1483                                 *vals = NULL;
1484                                 rc = LDAP_INSUFFICIENT_ACCESS;
1485
1486                         } else {
1487                                 BER_BVZERO( &v[j] );
1488                                 *vals = v;
1489                                 rc = LDAP_SUCCESS;
1490                         }
1491                 }
1492 freeit:         if ( e != target ) {
1493                         be_entry_release_r( op, e );
1494                 }
1495                 if ( freeattr ) {
1496                         attr_free( a );
1497                 }
1498         }
1499
1500         op->o_bd = be;
1501         return rc;
1502 }
1503
1504 int 
1505 backend_attribute(
1506         Operation *op,
1507         Entry   *target,
1508         struct berval   *edn,
1509         AttributeDescription *entry_at,
1510         BerVarray *vals,
1511         slap_access_t access )
1512 {
1513         int                     rc;
1514         BackendDB               *be_orig;
1515
1516         be_orig = op->o_bd;
1517         op->o_bd = frontendDB;
1518 #ifdef SLAP_OVERLAY_ACCESS
1519         rc = frontendDB->be_attribute( op, target, edn,
1520                 entry_at, vals, access );
1521 #else /* !SLAP_OVERLAY_ACCESS */
1522         rc = fe_acl_attribute( op, target, edn,
1523                 entry_at, vals, access );
1524 #endif /* !SLAP_OVERLAY_ACCESS */
1525         op->o_bd = be_orig;
1526
1527         return rc;
1528 }
1529
1530 int 
1531 backend_access(
1532         Operation               *op,
1533         Entry                   *target,
1534         struct berval           *edn,
1535         AttributeDescription    *entry_at,
1536         struct berval           *nval,
1537         slap_access_t           access,
1538         slap_mask_t             *mask )
1539 {
1540         Entry           *e = NULL;
1541         int             rc = LDAP_INSUFFICIENT_ACCESS;
1542         Backend         *be = op->o_bd;
1543
1544         /* pedantic */
1545         assert( op != NULL );
1546         assert( op->o_conn != NULL );
1547         assert( edn != NULL );
1548         assert( access > ACL_NONE );
1549
1550         op->o_bd = select_backend( edn, 0, 0 );
1551
1552         if ( target && dn_match( &target->e_nname, edn ) ) {
1553                 e = target;
1554
1555         } else {
1556                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1557         } 
1558
1559         if ( e ) {
1560                 Attribute       *a = NULL;
1561                 int             freeattr = 0;
1562
1563                 if ( entry_at == NULL ) {
1564                         entry_at = slap_schema.si_ad_entry;
1565                 }
1566
1567                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1568                 {
1569                         if ( access_allowed_mask( op, e, entry_at,
1570                                         NULL, access, NULL, mask ) == 0 )
1571                         {
1572                                 rc = LDAP_INSUFFICIENT_ACCESS;
1573
1574                         } else {
1575                                 rc = LDAP_SUCCESS;
1576                         }
1577
1578                 } else {
1579                         a = attr_find( e->e_attrs, entry_at );
1580                         if ( a == NULL ) {
1581                                 SlapReply       rs = { 0 };
1582                                 AttributeName   anlist[ 2 ];
1583
1584                                 anlist[ 0 ].an_name = entry_at->ad_cname;
1585                                 anlist[ 0 ].an_desc = entry_at;
1586                                 BER_BVZERO( &anlist[ 1 ].an_name );
1587                                 rs.sr_attrs = anlist;
1588                         
1589                                 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1590
1591                                 /* NOTE: backend_operational() is also called
1592                                  * when returning results, so it's supposed
1593                                  * to do no harm to entries */
1594                                 rs.sr_entry = e;
1595                                 rc = backend_operational( op, &rs );
1596                                 rs.sr_entry = NULL;
1597
1598                                 if ( rc == LDAP_SUCCESS ) {
1599                                         if ( rs.sr_operational_attrs ) {
1600                                                 freeattr = 1;
1601                                                 a = rs.sr_operational_attrs;
1602
1603                                         } else {
1604                                                 rc = LDAP_NO_SUCH_OBJECT;
1605                                         }
1606                                 }
1607                         }
1608
1609                         if ( a ) {
1610                                 if ( access_allowed_mask( op, e, entry_at,
1611                                                 nval, access, NULL, mask ) == 0 )
1612                                 {
1613                                         rc = LDAP_INSUFFICIENT_ACCESS;
1614                                         goto freeit;
1615                                 }
1616                                 rc = LDAP_SUCCESS;
1617                         }
1618                 }
1619 freeit:         if ( e != target ) {
1620                         be_entry_release_r( op, e );
1621                 }
1622                 if ( freeattr ) {
1623                         attr_free( a );
1624                 }
1625         }
1626
1627         op->o_bd = be;
1628         return rc;
1629 }
1630
1631 int
1632 fe_aux_operational(
1633         Operation *op,
1634         SlapReply *rs )
1635 {
1636         Attribute               **ap;
1637         int                     rc = 0;
1638         BackendDB               *be_orig;
1639
1640         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1641                 /* just count them */ ;
1642
1643         /*
1644          * If operational attributes (allegedly) are required, 
1645          * and the backend supports specific operational attributes, 
1646          * add them to the attribute list
1647          */
1648         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1649                 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) )
1650         {
1651                 *ap = slap_operational_entryDN( rs->sr_entry );
1652                 ap = &(*ap)->a_next;
1653         }
1654
1655         if ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1656                 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) )
1657         {
1658                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1659                 ap = &(*ap)->a_next;
1660         }
1661
1662         if ( op->o_bd != NULL )
1663         {
1664                 /* Let the overlays have a chance at this */
1665                 be_orig = op->o_bd;
1666                 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
1667                 if ( !be_match( op->o_bd, frontendDB ) &&
1668                         ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1669                         op->o_bd != NULL && op->o_bd->be_operational != NULL )
1670                 {
1671                         rc = op->o_bd->be_operational( op, rs );
1672                 }
1673                 op->o_bd = be_orig;
1674         }
1675
1676         return rc;
1677 }
1678
1679 int backend_operational( Operation *op, SlapReply *rs )
1680 {
1681         int rc;
1682         BackendDB *be_orig;
1683
1684         /* Moved this into the frontend so global overlays are called */
1685
1686         be_orig = op->o_bd;
1687         op->o_bd = frontendDB;
1688         rc = frontendDB->be_operational( op, rs );
1689         op->o_bd = be_orig;
1690
1691         return rc;
1692 }
1693