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