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