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