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