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