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