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