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