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