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