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