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