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