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