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