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