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