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