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