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