]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
f1d49e2c07e39150f38b4752a520ee2ae544bd1b
[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_isroot_dn( Backend *be, struct berval *ndn )
698 {
699         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_rootndn ) ) {
700                 return 0;
701         }
702
703         return dn_match( &be->be_rootndn, ndn );
704 }
705
706 int
707 be_slurp_update( Operation *op )
708 {
709         return ( SLAP_SLURP_SHADOW( op->o_bd ) &&
710                 be_isupdate_dn( op->o_bd, &op->o_ndn ) );
711 }
712
713 int
714 be_shadow_update( Operation *op )
715 {
716         /* This assumes that all internal ops (connid == -1) on a syncrepl
717          * database are syncrepl operations.
718          */
719         return (( SLAP_SYNC_SHADOW( op->o_bd ) && op->o_connid == -1 ) ||
720                 ( SLAP_SHADOW( op->o_bd ) && be_isupdate_dn( op->o_bd, &op->o_ndn ) ) );
721 }
722
723 int
724 be_isupdate_dn( Backend *be, struct berval *ndn )
725 {
726         if ( BER_BVISEMPTY( ndn ) || BER_BVISEMPTY( &be->be_update_ndn ) ) {
727                 return 0;
728         }
729
730         return dn_match( &be->be_update_ndn, ndn );
731 }
732
733 struct berval *
734 be_root_dn( Backend *be )
735 {
736         return &be->be_rootdn;
737 }
738
739 int
740 be_isroot( Operation *op )
741 {
742         return be_isroot_dn( op->o_bd, &op->o_ndn );
743 }
744
745 int
746 be_isroot_pw( Operation *op )
747 {
748         int result;
749
750         if ( ! be_isroot_dn( op->o_bd, &op->o_req_ndn ) ) {
751                 return 0;
752         }
753
754         if ( BER_BVISEMPTY( &op->o_bd->be_rootpw ) ) {
755                 return 0;
756         }
757
758 #ifdef SLAPD_SPASSWD
759         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
760                 op->o_conn->c_sasl_authctx, NULL );
761 #endif
762
763         result = lutil_passwd( &op->o_bd->be_rootpw, &op->orb_cred, NULL, NULL );
764
765 #ifdef SLAPD_SPASSWD
766         ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind, NULL, NULL );
767 #endif
768
769         return result == 0;
770 }
771
772 int
773 be_entry_release_rw(
774         Operation *op,
775         Entry *e,
776         int rw )
777 {
778         if ( op->o_bd->be_release ) {
779                 /* free and release entry from backend */
780                 return op->o_bd->be_release( op, e, rw );
781         } else {
782                 /* free entry */
783                 entry_free( e );
784                 return 0;
785         }
786 }
787
788 int
789 backend_unbind( Operation *op, SlapReply *rs )
790 {
791         BackendDB *be;
792
793         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
794                 if ( be->be_unbind ) {
795                         op->o_bd = be;
796                         be->be_unbind( op, rs );
797                 }
798         }
799
800         return 0;
801 }
802
803 int
804 backend_connection_init(
805         Connection   *conn )
806 {
807         BackendDB *be;
808
809         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
810                 if ( be->be_connection_init ) {
811                         be->be_connection_init( be, conn );
812                 }
813         }
814
815         return 0;
816 }
817
818 int
819 backend_connection_destroy(
820         Connection   *conn )
821 {
822         BackendDB *be;
823
824         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
825                 if ( be->be_connection_destroy ) {
826                         be->be_connection_destroy( be, conn);
827                 }
828         }
829
830         return 0;
831 }
832
833 int
834 backend_check_controls(
835         Operation *op,
836         SlapReply *rs )
837 {
838         LDAPControl **ctrls = op->o_ctrls;
839         rs->sr_err = LDAP_SUCCESS;
840
841         if( ctrls ) {
842                 for( ; *ctrls != NULL ; ctrls++ ) {
843                         int cid;
844
845                         switch ( slap_global_control( op, (*ctrls)->ldctl_oid, &cid ) ) {
846                         case LDAP_CONTROL_NOT_FOUND:
847                                 /* unrecognized control */ 
848                                 if ( (*ctrls)->ldctl_iscritical ) {
849                                         /* should not be reachable */ 
850                                         Debug( LDAP_DEBUG_ANY, "backend_check_controls: "
851                                                 "unrecognized critical control: %s\n",
852                                                 (*ctrls)->ldctl_oid, 0, 0 );
853                                         assert( 0 );
854                                 } else {
855                                         Debug( LDAP_DEBUG_TRACE, "backend_check_controls: "
856                                                 "unrecognized non-critical control: %s\n",
857                                                 (*ctrls)->ldctl_oid, 0, 0 );
858                                 }
859                                 break;
860
861                         case LDAP_COMPARE_FALSE:
862                                 if ( !op->o_bd->be_ctrls[cid] && (*ctrls)->ldctl_iscritical ) {
863                                         /* RFC 4511 allows unavailableCriticalExtension to be
864                                          * returned when the server is unwilling to perform
865                                          * an operation extended by a recognized critical
866                                          * control.
867                                          */
868                                         rs->sr_text = "critical control unavailable in context";
869                                         rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
870                                         goto done;
871                                 }
872                                 break;
873
874                         case LDAP_COMPARE_TRUE:
875                                 break;
876
877                         default:
878                                 /* unreachable */
879                                 Debug( LDAP_DEBUG_ANY,
880                                         "backend_check_controls: unable to check control: %s\n",
881                                         (*ctrls)->ldctl_oid, 0, 0 );
882                                 assert( 0 );
883
884                                 rs->sr_text = "unable to check control";
885                                 rs->sr_err = LDAP_OTHER;
886                                 goto done;
887                         }
888                 }
889         }
890
891 #if 0 /* temporarily removed */
892         /* check should be generalized */
893         if( get_relax(op) && !be_isroot(op)) {
894                 rs->sr_text = "requires manager authorization";
895                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
896         }
897 #endif
898
899 done:;
900         return rs->sr_err;
901 }
902
903 int
904 backend_check_restrictions(
905         Operation *op,
906         SlapReply *rs,
907         struct berval *opdata )
908 {
909         slap_mask_t restrictops;
910         slap_mask_t requires;
911         slap_mask_t opflag;
912         slap_mask_t exopflag = 0;
913         slap_ssf_set_t *ssf;
914         int updateop = 0;
915         int starttls = 0;
916         int session = 0;
917
918         if ( op->o_bd ) {
919                 int     rc = SLAP_CB_CONTINUE;
920
921                 if ( op->o_bd->be_chk_controls ) {
922                         rc = ( *op->o_bd->be_chk_controls )( op, rs );
923                 }
924
925                 if ( rc == SLAP_CB_CONTINUE ) {
926                         rc = backend_check_controls( op, rs );
927                 }
928
929                 if ( rc != LDAP_SUCCESS ) {
930                         return rs->sr_err;
931                 }
932
933                 restrictops = op->o_bd->be_restrictops;
934                 requires = op->o_bd->be_requires;
935                 ssf = &op->o_bd->be_ssf_set;
936
937         } else {
938                 restrictops = frontendDB->be_restrictops;
939                 requires = frontendDB->be_requires;
940                 ssf = &frontendDB->be_ssf_set;
941         }
942
943         switch( op->o_tag ) {
944         case LDAP_REQ_ADD:
945                 opflag = SLAP_RESTRICT_OP_ADD;
946                 updateop++;
947                 break;
948         case LDAP_REQ_BIND:
949                 opflag = SLAP_RESTRICT_OP_BIND;
950                 session++;
951                 break;
952         case LDAP_REQ_COMPARE:
953                 opflag = SLAP_RESTRICT_OP_COMPARE;
954                 break;
955         case LDAP_REQ_DELETE:
956                 updateop++;
957                 opflag = SLAP_RESTRICT_OP_DELETE;
958                 break;
959         case LDAP_REQ_EXTENDED:
960                 opflag = SLAP_RESTRICT_OP_EXTENDED;
961
962                 if( !opdata ) {
963                         /* treat unspecified as a modify */
964                         opflag = SLAP_RESTRICT_OP_MODIFY;
965                         updateop++;
966                         break;
967                 }
968
969                 if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
970                         session++;
971                         starttls++;
972                         exopflag = SLAP_RESTRICT_EXOP_START_TLS;
973                         break;
974                 }
975
976                 if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
977                         exopflag = SLAP_RESTRICT_EXOP_WHOAMI;
978                         break;
979                 }
980
981                 if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
982                         exopflag = SLAP_RESTRICT_EXOP_CANCEL;
983                         break;
984                 }
985
986                 if ( bvmatch( opdata, &slap_EXOP_MODIFY_PASSWD ) ) {
987                         exopflag = SLAP_RESTRICT_EXOP_MODIFY_PASSWD;
988                         updateop++;
989                         break;
990                 }
991
992                 /* treat everything else as a modify */
993                 opflag = SLAP_RESTRICT_OP_MODIFY;
994                 updateop++;
995                 break;
996
997         case LDAP_REQ_MODIFY:
998                 updateop++;
999                 opflag = SLAP_RESTRICT_OP_MODIFY;
1000                 break;
1001         case LDAP_REQ_RENAME:
1002                 updateop++;
1003                 opflag = SLAP_RESTRICT_OP_RENAME;
1004                 break;
1005         case LDAP_REQ_SEARCH:
1006                 opflag = SLAP_RESTRICT_OP_SEARCH;
1007                 break;
1008         case LDAP_REQ_UNBIND:
1009                 session++;
1010                 opflag = 0;
1011                 break;
1012         default:
1013                 rs->sr_text = "restrict operations internal error";
1014                 rs->sr_err = LDAP_OTHER;
1015                 return rs->sr_err;
1016         }
1017
1018         if ( !starttls ) {
1019                 /* these checks don't apply to StartTLS */
1020
1021                 rs->sr_err = LDAP_CONFIDENTIALITY_REQUIRED;
1022                 if( op->o_transport_ssf < ssf->sss_transport ) {
1023                         rs->sr_text = op->o_transport_ssf
1024                                 ? "stronger transport confidentiality required"
1025                                 : "transport confidentiality required";
1026                         return rs->sr_err;
1027                 }
1028
1029                 if( op->o_tls_ssf < ssf->sss_tls ) {
1030                         rs->sr_text = op->o_tls_ssf
1031                                 ? "stronger TLS confidentiality required"
1032                                 : "TLS confidentiality required";
1033                         return rs->sr_err;
1034                 }
1035
1036
1037                 if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
1038                         /* simple bind specific check */
1039                         if( op->o_ssf < ssf->sss_simple_bind ) {
1040                                 rs->sr_text = op->o_ssf
1041                                         ? "stronger confidentiality required"
1042                                         : "confidentiality required";
1043                                 return rs->sr_err;
1044                         }
1045                 }
1046
1047                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
1048                         /* these checks don't apply to SASL bind */
1049
1050                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
1051                                 rs->sr_text = op->o_sasl_ssf
1052                                         ? "stronger SASL confidentiality required"
1053                                         : "SASL confidentiality required";
1054                                 return rs->sr_err;
1055                         }
1056
1057                         if( op->o_ssf < ssf->sss_ssf ) {
1058                                 rs->sr_text = op->o_ssf
1059                                         ? "stronger confidentiality required"
1060                                         : "confidentiality required";
1061                                 return rs->sr_err;
1062                         }
1063                 }
1064
1065                 if( updateop ) {
1066                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
1067                                 rs->sr_text = op->o_transport_ssf
1068                                         ? "stronger transport confidentiality required for update"
1069                                         : "transport confidentiality required for update";
1070                                 return rs->sr_err;
1071                         }
1072
1073                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
1074                                 rs->sr_text = op->o_tls_ssf
1075                                         ? "stronger TLS confidentiality required for update"
1076                                         : "TLS confidentiality required for update";
1077                                 return rs->sr_err;
1078                         }
1079
1080                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
1081                                 rs->sr_text = op->o_sasl_ssf
1082                                         ? "stronger SASL confidentiality required for update"
1083                                         : "SASL confidentiality required for update";
1084                                 return rs->sr_err;
1085                         }
1086
1087                         if( op->o_ssf < ssf->sss_update_ssf ) {
1088                                 rs->sr_text = op->o_ssf
1089                                         ? "stronger confidentiality required for update"
1090                                         : "confidentiality required for update";
1091                                 return rs->sr_err;
1092                         }
1093
1094                         if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
1095                                 BER_BVISEMPTY( &op->o_ndn ) )
1096                         {
1097                                 rs->sr_text = "modifications require authentication";
1098                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1099                                 return rs->sr_err;
1100                         }
1101
1102 #ifdef SLAP_X_LISTENER_MOD
1103                         if ( op->o_conn->c_listener &&
1104                                 ! ( op->o_conn->c_listener->sl_perms & ( !BER_BVISEMPTY( &op->o_ndn )
1105                                         ? (S_IWUSR|S_IWOTH) : S_IWOTH ) ) )
1106                         {
1107                                 /* no "w" mode means readonly */
1108                                 rs->sr_text = "modifications not allowed on this listener";
1109                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1110                                 return rs->sr_err;
1111                         }
1112 #endif /* SLAP_X_LISTENER_MOD */
1113                 }
1114         }
1115
1116         if ( !session ) {
1117                 /* these checks don't apply to Bind, StartTLS, or Unbind */
1118
1119                 if( requires & SLAP_REQUIRE_STRONG ) {
1120                         /* should check mechanism */
1121                         if( ( op->o_transport_ssf < ssf->sss_transport
1122                                 && op->o_authtype == LDAP_AUTH_SIMPLE )
1123                                 || BER_BVISEMPTY( &op->o_dn ) )
1124                         {
1125                                 rs->sr_text = "strong(er) authentication required";
1126                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1127                                 return rs->sr_err;
1128                         }
1129                 }
1130
1131                 if( requires & SLAP_REQUIRE_SASL ) {
1132                         if( op->o_authtype != LDAP_AUTH_SASL || BER_BVISEMPTY( &op->o_dn ) ) {
1133                                 rs->sr_text = "SASL authentication required";
1134                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1135                                 return rs->sr_err;
1136                         }
1137                 }
1138                         
1139                 if( requires & SLAP_REQUIRE_AUTHC ) {
1140                         if( BER_BVISEMPTY( &op->o_dn ) ) {
1141                                 rs->sr_text = "authentication required";
1142                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1143                                 return rs->sr_err;
1144                         }
1145                 }
1146
1147                 if( requires & SLAP_REQUIRE_BIND ) {
1148                         int version;
1149                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1150                         version = op->o_conn->c_protocol;
1151                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1152
1153                         if( !version ) {
1154                                 /* no bind has occurred */
1155                                 rs->sr_text = "BIND required";
1156                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1157                                 return rs->sr_err;
1158                         }
1159                 }
1160
1161                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
1162                         if( op->o_protocol < LDAP_VERSION3 ) {
1163                                 /* no bind has occurred */
1164                                 rs->sr_text = "operation restricted to LDAPv3 clients";
1165                                 rs->sr_err = LDAP_OPERATIONS_ERROR;
1166                                 return rs->sr_err;
1167                         }
1168                 }
1169
1170 #ifdef SLAP_X_LISTENER_MOD
1171                 if ( !starttls && BER_BVISEMPTY( &op->o_dn ) ) {
1172                         if ( op->o_conn->c_listener &&
1173                                 !( op->o_conn->c_listener->sl_perms & S_IXOTH ))
1174                 {
1175                                 /* no "x" mode means bind required */
1176                                 rs->sr_text = "bind required on this listener";
1177                                 rs->sr_err = LDAP_STRONG_AUTH_REQUIRED;
1178                                 return rs->sr_err;
1179                         }
1180                 }
1181
1182                 if ( !starttls && !updateop ) {
1183                         if ( op->o_conn->c_listener &&
1184                                 !( op->o_conn->c_listener->sl_perms &
1185                                         ( !BER_BVISEMPTY( &op->o_dn )
1186                                                 ? (S_IRUSR|S_IROTH) : S_IROTH )))
1187                         {
1188                                 /* no "r" mode means no read */
1189                                 rs->sr_text = "read not allowed on this listener";
1190                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1191                                 return rs->sr_err;
1192                         }
1193                 }
1194 #endif /* SLAP_X_LISTENER_MOD */
1195
1196         }
1197
1198         if( ( restrictops & opflag )
1199                         || ( exopflag && ( restrictops & exopflag ) ) ) {
1200                 if( ( restrictops & SLAP_RESTRICT_OP_MASK) == SLAP_RESTRICT_OP_READS ) {
1201                         rs->sr_text = "read operations restricted";
1202                 } else if ( restrictops & exopflag ) {
1203                         rs->sr_text = "extended operation restricted";
1204                 } else {
1205                         rs->sr_text = "operation restricted";
1206                 }
1207                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1208                 return rs->sr_err;
1209         }
1210
1211         rs->sr_err = LDAP_SUCCESS;
1212         return rs->sr_err;
1213 }
1214
1215 int backend_check_referrals( Operation *op, SlapReply *rs )
1216 {
1217         rs->sr_err = LDAP_SUCCESS;
1218
1219         if( op->o_bd->be_chk_referrals ) {
1220                 rs->sr_err = op->o_bd->be_chk_referrals( op, rs );
1221
1222                 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_REFERRAL ) {
1223                         send_ldap_result( op, rs );
1224                 }
1225         }
1226
1227         return rs->sr_err;
1228 }
1229
1230 int
1231 be_entry_get_rw(
1232         Operation *op,
1233         struct berval *ndn,
1234         ObjectClass *oc,
1235         AttributeDescription *at,
1236         int rw,
1237         Entry **e )
1238 {
1239         *e = NULL;
1240
1241         if ( op->o_bd == NULL ) {
1242                 return LDAP_NO_SUCH_OBJECT;
1243         }
1244
1245         if ( op->o_bd->be_fetch ) {
1246                 return op->o_bd->be_fetch( op, ndn, oc, at, rw, e );
1247         }
1248
1249         return LDAP_UNWILLING_TO_PERFORM;
1250 }
1251
1252 int 
1253 fe_acl_group(
1254         Operation *op,
1255         Entry   *target,
1256         struct berval *gr_ndn,
1257         struct berval *op_ndn,
1258         ObjectClass *group_oc,
1259         AttributeDescription *group_at )
1260 {
1261         Entry *e;
1262         void *o_priv = op->o_private, *e_priv = NULL;
1263         Attribute *a;
1264         int rc;
1265         GroupAssertion *g;
1266         Backend *be = op->o_bd;
1267
1268         op->o_bd = select_backend( gr_ndn, 0 );
1269
1270         for ( g = op->o_groups; g; g = g->ga_next ) {
1271                 if ( g->ga_be != op->o_bd || g->ga_oc != group_oc ||
1272                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len )
1273                 {
1274                         continue;
1275                 }
1276                 if ( strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0 ) {
1277                         break;
1278                 }
1279         }
1280
1281         if ( g ) {
1282                 rc = g->ga_res;
1283                 goto done;
1284         }
1285
1286         if ( target && dn_match( &target->e_nname, gr_ndn ) ) {
1287                 e = target;
1288                 rc = 0;
1289
1290         } else {
1291                 op->o_private = NULL;
1292                 rc = be_entry_get_rw( op, gr_ndn, group_oc, group_at, 0, &e );
1293                 e_priv = op->o_private;
1294                 op->o_private = o_priv;
1295         }
1296
1297         if ( e ) {
1298                 a = attr_find( e->e_attrs, group_at );
1299                 if ( a ) {
1300                         /* If the attribute is a subtype of labeledURI,
1301                          * treat this as a dynamic group ala groupOfURLs
1302                          */
1303                         if ( is_at_subtype( group_at->ad_type,
1304                                 slap_schema.si_ad_labeledURI->ad_type ) )
1305                         {
1306                                 int i;
1307                                 LDAPURLDesc *ludp;
1308                                 struct berval bv, nbase;
1309                                 Filter *filter;
1310                                 Entry *user = NULL;
1311                                 void *user_priv = NULL;
1312                                 Backend *b2 = op->o_bd;
1313
1314                                 if ( target && dn_match( &target->e_nname, op_ndn ) ) {
1315                                         user = target;
1316                                 }
1317                                 
1318                                 rc = LDAP_COMPARE_FALSE;
1319                                 for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ ) {
1320                                         if ( ldap_url_parse( a->a_vals[i].bv_val, &ludp ) !=
1321                                                 LDAP_URL_SUCCESS )
1322                                         {
1323                                                 continue;
1324                                         }
1325
1326                                         BER_BVZERO( &nbase );
1327
1328                                         /* host, attrs and extensions parts must be empty */
1329                                         if ( ( ludp->lud_host && *ludp->lud_host )
1330                                                 || ludp->lud_attrs
1331                                                 || ludp->lud_exts )
1332                                         {
1333                                                 goto loopit;
1334                                         }
1335
1336                                         ber_str2bv( ludp->lud_dn, 0, 0, &bv );
1337                                         if ( dnNormalize( 0, NULL, NULL, &bv, &nbase,
1338                                                 op->o_tmpmemctx ) != LDAP_SUCCESS )
1339                                         {
1340                                                 goto loopit;
1341                                         }
1342
1343                                         switch ( ludp->lud_scope ) {
1344                                         case LDAP_SCOPE_BASE:
1345                                                 if ( !dn_match( &nbase, op_ndn ) ) {
1346                                                         goto loopit;
1347                                                 }
1348                                                 break;
1349                                         case LDAP_SCOPE_ONELEVEL:
1350                                                 dnParent( op_ndn, &bv );
1351                                                 if ( !dn_match( &nbase, &bv ) ) {
1352                                                         goto loopit;
1353                                                 }
1354                                                 break;
1355                                         case LDAP_SCOPE_SUBTREE:
1356                                                 if ( !dnIsSuffix( op_ndn, &nbase ) ) {
1357                                                         goto loopit;
1358                                                 }
1359                                                 break;
1360                                         case LDAP_SCOPE_SUBORDINATE:
1361                                                 if ( dn_match( &nbase, op_ndn ) ||
1362                                                         !dnIsSuffix( op_ndn, &nbase ) )
1363                                                 {
1364                                                         goto loopit;
1365                                                 }
1366                                         }
1367
1368                                         /* NOTE: this could be NULL
1369                                          * if no filter is provided,
1370                                          * or if filter parsing fails.
1371                                          * In the latter case,
1372                                          * we should give up. */
1373                                         if ( ludp->lud_filter != NULL && ludp->lud_filter != '\0') {
1374                                                 filter = str2filter_x( op, ludp->lud_filter );
1375                                                 if ( filter == NULL ) {
1376                                                         /* give up... */
1377                                                         rc = LDAP_OTHER;
1378                                                         goto loopit;
1379                                                 }
1380
1381                                                 /* only get user if required
1382                                                  * and not available yet */
1383                                                 if ( user == NULL ) {   
1384                                                         int rc2;
1385
1386                                                         op->o_bd = select_backend( op_ndn, 0 );
1387                                                         op->o_private = NULL;
1388                                                         rc2 = be_entry_get_rw( op, op_ndn, NULL, NULL, 0, &user );
1389                                                         user_priv = op->o_private;
1390                                                         op->o_private = o_priv;
1391                                                         if ( rc2 != 0 ) {
1392                                                                 /* give up... */
1393                                                                 rc = LDAP_OTHER;
1394                                                                 goto loopit;
1395                                                         }
1396                                                 }
1397
1398                                                 if ( test_filter( NULL, user, filter ) ==
1399                                                         LDAP_COMPARE_TRUE )
1400                                                 {
1401                                                         rc = 0;
1402                                                 }
1403                                                 filter_free_x( op, filter );
1404                                         }
1405 loopit:
1406                                         ldap_free_urldesc( ludp );
1407                                         if ( !BER_BVISNULL( &nbase ) ) {
1408                                                 op->o_tmpfree( nbase.bv_val, op->o_tmpmemctx );
1409                                         }
1410                                         if ( rc != LDAP_COMPARE_FALSE ) {
1411                                                 break;
1412                                         }
1413                                 }
1414
1415                                 if ( user != NULL && user != target ) {
1416                                         op->o_private = user_priv;
1417                                         be_entry_release_r( op, user );
1418                                         op->o_private = o_priv;
1419                                 }
1420                                 op->o_bd = b2;
1421
1422                         } else {
1423                                 rc = value_find_ex( group_at,
1424                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1425                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1426                                         a->a_nvals, op_ndn, op->o_tmpmemctx );
1427                                 if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
1428                                         rc = LDAP_COMPARE_FALSE;
1429                                 }
1430                         }
1431
1432                 } else {
1433                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1434                 }
1435
1436                 if ( e != target ) {
1437                         op->o_private = e_priv;
1438                         be_entry_release_r( op, e );
1439                         op->o_private = o_priv;
1440                 }
1441
1442         } else {
1443                 rc = LDAP_NO_SUCH_OBJECT;
1444         }
1445
1446         if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
1447                 g = op->o_tmpalloc( sizeof( GroupAssertion ) + gr_ndn->bv_len,
1448                         op->o_tmpmemctx );
1449                 g->ga_be = op->o_bd;
1450                 g->ga_oc = group_oc;
1451                 g->ga_at = group_at;
1452                 g->ga_res = rc;
1453                 g->ga_len = gr_ndn->bv_len;
1454                 strcpy( g->ga_ndn, gr_ndn->bv_val );
1455                 g->ga_next = op->o_groups;
1456                 op->o_groups = g;
1457         }
1458
1459 done:
1460         op->o_bd = be;
1461         return rc;
1462 }
1463
1464 int 
1465 backend_group(
1466         Operation *op,
1467         Entry   *target,
1468         struct berval *gr_ndn,
1469         struct berval *op_ndn,
1470         ObjectClass *group_oc,
1471         AttributeDescription *group_at )
1472 {
1473         int                     rc;
1474         BackendDB               *be_orig;
1475
1476         if ( op->o_abandon ) {
1477                 return SLAPD_ABANDON;
1478         }
1479
1480         be_orig = op->o_bd;
1481         op->o_bd = frontendDB;
1482         rc = frontendDB->be_group( op, target, gr_ndn,
1483                 op_ndn, group_oc, group_at );
1484         op->o_bd = be_orig;
1485
1486         return rc;
1487 }
1488
1489 int 
1490 fe_acl_attribute(
1491         Operation *op,
1492         Entry   *target,
1493         struct berval   *edn,
1494         AttributeDescription *entry_at,
1495         BerVarray *vals,
1496         slap_access_t access )
1497 {
1498         Entry                   *e = NULL;
1499         void                    *o_priv = op->o_private, *e_priv = NULL;
1500         Attribute               *a = NULL;
1501         int                     freeattr = 0, i, j, rc = LDAP_SUCCESS;
1502         AccessControlState      acl_state = ACL_STATE_INIT;
1503         Backend                 *be = op->o_bd;
1504
1505         op->o_bd = select_backend( edn, 0 );
1506
1507         if ( target && dn_match( &target->e_nname, edn ) ) {
1508                 e = target;
1509
1510         } else {
1511                 op->o_private = NULL;
1512                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1513                 e_priv = op->o_private;
1514                 op->o_private = o_priv;
1515         } 
1516
1517         if ( e ) {
1518                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children ) {
1519                         assert( vals == NULL );
1520
1521                         rc = LDAP_SUCCESS;
1522                         if ( op->o_conn && access > ACL_NONE &&
1523                                 access_allowed( op, e, entry_at, NULL,
1524                                                 access, &acl_state ) == 0 )
1525                         {
1526                                 rc = LDAP_INSUFFICIENT_ACCESS;
1527                         }
1528                         goto freeit;
1529                 }
1530
1531                 a = attr_find( e->e_attrs, entry_at );
1532                 if ( a == NULL ) {
1533                         SlapReply       rs = { 0 };
1534                         AttributeName   anlist[ 2 ];
1535
1536                         anlist[ 0 ].an_name = entry_at->ad_cname;
1537                         anlist[ 0 ].an_desc = entry_at;
1538                         BER_BVZERO( &anlist[ 1 ].an_name );
1539                         rs.sr_attrs = anlist;
1540                         
1541                         /* NOTE: backend_operational() is also called
1542                          * when returning results, so it's supposed
1543                          * to do no harm to entries */
1544                         rs.sr_entry = e;
1545                         rc = backend_operational( op, &rs );
1546                         rs.sr_entry = NULL;
1547  
1548                         if ( rc == LDAP_SUCCESS ) {
1549                                 if ( rs.sr_operational_attrs ) {
1550                                         freeattr = 1;
1551                                         a = rs.sr_operational_attrs;
1552
1553                                 } else {
1554                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
1555                                 }
1556                         }
1557                 }
1558
1559                 if ( a ) {
1560                         BerVarray v;
1561
1562                         if ( op->o_conn && access > ACL_NONE &&
1563                                 access_allowed( op, e, entry_at, NULL,
1564                                                 access, &acl_state ) == 0 )
1565                         {
1566                                 rc = LDAP_INSUFFICIENT_ACCESS;
1567                                 goto freeit;
1568                         }
1569
1570                         for ( i = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1571                                 ;
1572                         
1573                         v = op->o_tmpalloc( sizeof(struct berval) * ( i + 1 ),
1574                                 op->o_tmpmemctx );
1575                         for ( i = 0, j = 0; !BER_BVISNULL( &a->a_vals[i] ); i++ )
1576                         {
1577                                 if ( op->o_conn && access > ACL_NONE && 
1578                                         access_allowed( op, e, entry_at,
1579                                                         &a->a_nvals[i],
1580                                                         access,
1581                                                         &acl_state ) == 0 )
1582                                 {
1583                                         continue;
1584                                 }
1585                                 ber_dupbv_x( &v[j], &a->a_nvals[i],
1586                                                 op->o_tmpmemctx );
1587                                 if ( !BER_BVISNULL( &v[j] ) ) {
1588                                         j++;
1589                                 }
1590                         }
1591                         if ( j == 0 ) {
1592                                 op->o_tmpfree( v, op->o_tmpmemctx );
1593                                 *vals = NULL;
1594                                 rc = LDAP_INSUFFICIENT_ACCESS;
1595
1596                         } else {
1597                                 BER_BVZERO( &v[j] );
1598                                 *vals = v;
1599                                 rc = LDAP_SUCCESS;
1600                         }
1601                 }
1602 freeit:         if ( e != target ) {
1603                         op->o_private = e_priv;
1604                         be_entry_release_r( op, e );
1605                         op->o_private = o_priv;
1606                 }
1607                 if ( freeattr ) {
1608                         attr_free( a );
1609                 }
1610         }
1611
1612         op->o_bd = be;
1613         return rc;
1614 }
1615
1616 int 
1617 backend_attribute(
1618         Operation *op,
1619         Entry   *target,
1620         struct berval   *edn,
1621         AttributeDescription *entry_at,
1622         BerVarray *vals,
1623         slap_access_t access )
1624 {
1625         int                     rc;
1626         BackendDB               *be_orig;
1627
1628         be_orig = op->o_bd;
1629         op->o_bd = frontendDB;
1630         rc = frontendDB->be_attribute( op, target, edn,
1631                 entry_at, vals, access );
1632         op->o_bd = be_orig;
1633
1634         return rc;
1635 }
1636
1637 int 
1638 backend_access(
1639         Operation               *op,
1640         Entry                   *target,
1641         struct berval           *edn,
1642         AttributeDescription    *entry_at,
1643         struct berval           *nval,
1644         slap_access_t           access,
1645         slap_mask_t             *mask )
1646 {
1647         Entry           *e = NULL;
1648         void            *o_priv = op->o_private, *e_priv = NULL;
1649         int             rc = LDAP_INSUFFICIENT_ACCESS;
1650         Backend         *be = op->o_bd;
1651
1652         /* pedantic */
1653         assert( op != NULL );
1654         assert( op->o_conn != NULL );
1655         assert( edn != NULL );
1656         assert( access > ACL_NONE );
1657
1658         op->o_bd = select_backend( edn, 0 );
1659
1660         if ( target && dn_match( &target->e_nname, edn ) ) {
1661                 e = target;
1662
1663         } else {
1664                 op->o_private = NULL;
1665                 rc = be_entry_get_rw( op, edn, NULL, entry_at, 0, &e );
1666                 e_priv = op->o_private;
1667                 op->o_private = o_priv;
1668         } 
1669
1670         if ( e ) {
1671                 Attribute       *a = NULL;
1672                 int             freeattr = 0;
1673
1674                 if ( entry_at == NULL ) {
1675                         entry_at = slap_schema.si_ad_entry;
1676                 }
1677
1678                 if ( entry_at == slap_schema.si_ad_entry || entry_at == slap_schema.si_ad_children )
1679                 {
1680                         if ( access_allowed_mask( op, e, entry_at,
1681                                         NULL, access, NULL, mask ) == 0 )
1682                         {
1683                                 rc = LDAP_INSUFFICIENT_ACCESS;
1684
1685                         } else {
1686                                 rc = LDAP_SUCCESS;
1687                         }
1688
1689                 } else {
1690                         a = attr_find( e->e_attrs, entry_at );
1691                         if ( a == NULL ) {
1692                                 SlapReply       rs = { 0 };
1693                                 AttributeName   anlist[ 2 ];
1694
1695                                 anlist[ 0 ].an_name = entry_at->ad_cname;
1696                                 anlist[ 0 ].an_desc = entry_at;
1697                                 BER_BVZERO( &anlist[ 1 ].an_name );
1698                                 rs.sr_attrs = anlist;
1699                         
1700                                 rs.sr_attr_flags = slap_attr_flags( rs.sr_attrs );
1701
1702                                 /* NOTE: backend_operational() is also called
1703                                  * when returning results, so it's supposed
1704                                  * to do no harm to entries */
1705                                 rs.sr_entry = e;
1706                                 rc = backend_operational( op, &rs );
1707                                 rs.sr_entry = NULL;
1708
1709                                 if ( rc == LDAP_SUCCESS ) {
1710                                         if ( rs.sr_operational_attrs ) {
1711                                                 freeattr = 1;
1712                                                 a = rs.sr_operational_attrs;
1713
1714                                         } else {
1715                                                 rc = LDAP_NO_SUCH_OBJECT;
1716                                         }
1717                                 }
1718                         }
1719
1720                         if ( a ) {
1721                                 if ( access_allowed_mask( op, e, entry_at,
1722                                                 nval, access, NULL, mask ) == 0 )
1723                                 {
1724                                         rc = LDAP_INSUFFICIENT_ACCESS;
1725                                         goto freeit;
1726                                 }
1727                                 rc = LDAP_SUCCESS;
1728                         }
1729                 }
1730 freeit:         if ( e != target ) {
1731                         op->o_private = e_priv;
1732                         be_entry_release_r( op, e );
1733                         op->o_private = o_priv;
1734                 }
1735                 if ( freeattr ) {
1736                         attr_free( a );
1737                 }
1738         }
1739
1740         op->o_bd = be;
1741         return rc;
1742 }
1743
1744 int
1745 fe_aux_operational(
1746         Operation *op,
1747         SlapReply *rs )
1748 {
1749         Attribute               **ap;
1750         int                     rc = 0;
1751
1752         for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1753                 /* just count them */ ;
1754
1755         /*
1756          * If operational attributes (allegedly) are required, 
1757          * and the backend supports specific operational attributes, 
1758          * add them to the attribute list
1759          */
1760         if ( !( rs->sr_flags & REP_NO_ENTRYDN )
1761                 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1762                 ad_inlist( slap_schema.si_ad_entryDN, rs->sr_attrs ) ) ) )
1763         {
1764                 *ap = slap_operational_entryDN( rs->sr_entry );
1765                 ap = &(*ap)->a_next;
1766         }
1767
1768         if ( !( rs->sr_flags & REP_NO_SUBSCHEMA)
1769                 && ( SLAP_OPATTRS( rs->sr_attr_flags ) || ( rs->sr_attrs &&
1770                 ad_inlist( slap_schema.si_ad_subschemaSubentry, rs->sr_attrs ) ) ) )
1771         {
1772                 *ap = slap_operational_subschemaSubentry( op->o_bd );
1773                 ap = &(*ap)->a_next;
1774         }
1775
1776         if ( op->o_bd != NULL ) {
1777                 BackendDB               *be_orig = op->o_bd;
1778
1779                 /* Let the overlays have a chance at this */
1780                 op->o_bd = select_backend( &op->o_req_ndn, 0 );
1781                 if ( op->o_bd != NULL && !be_match( op->o_bd, frontendDB ) &&
1782                         ( SLAP_OPATTRS( rs->sr_attr_flags ) || rs->sr_attrs ) &&
1783                         op->o_bd->be_operational != NULL )
1784                 {
1785                         rc = op->o_bd->be_operational( op, rs );
1786                 }
1787                 op->o_bd = be_orig;
1788         }
1789
1790         return rc;
1791 }
1792
1793 int backend_operational( Operation *op, SlapReply *rs )
1794 {
1795         int rc;
1796         BackendDB *be_orig;
1797
1798         /* Moved this into the frontend so global overlays are called */
1799
1800         be_orig = op->o_bd;
1801         op->o_bd = frontendDB;
1802         rc = frontendDB->be_operational( op, rs );
1803         op->o_bd = be_orig;
1804
1805         return rc;
1806 }
1807