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