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