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