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