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