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