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