]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
More changes to let BDB build without LDBM.
[openldap] / servers / slapd / backend.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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
16 #include <sys/stat.h>
17
18 #include "slap.h"
19 #include "lutil.h"
20
21 #ifdef SLAPD_BDB
22 #include "back-bdb/external.h"
23 #endif
24 #ifdef SLAPD_DNSSRV
25 #include "back-dnssrv/external.h"
26 #endif
27 #ifdef SLAPD_LDAP
28 #include "back-ldap/external.h"
29 #endif
30 #ifdef SLAPD_LDBM
31 #include "back-ldbm/external.h"
32 #endif
33 #ifdef SLAPD_META
34 #include "back-meta/external.h"
35 #endif
36 #ifdef SLAPD_MONITOR
37 #include "back-monitor/external.h"
38 #endif
39 #ifdef SLAPD_PASSWD
40 #include "back-passwd/external.h"
41 #endif
42 #ifdef SLAPD_PERL
43 #include "back-perl/external.h"
44 #endif
45 #ifdef SLAPD_SHELL
46 #include "back-shell/external.h"
47 #endif
48 #ifdef SLAPD_TCL
49 #include "back-tcl/external.h"
50 #endif
51 #ifdef SLAPD_SQL
52 #include "back-sql/external.h"
53 #endif
54 #ifdef SLAPD_PRIVATE
55 #include "private/external.h"
56 #endif
57
58 static BackendInfo binfo[] = {
59 #if defined(SLAPD_BDB) && !defined(SLAPD_BDB_DYNAMIC)
60         {"bdb", bdb_initialize},
61 #endif
62 #if defined(SLAPD_DNSSRV) && !defined(SLAPD_DNSSRV_DYNAMIC)
63         {"dnssrv",      dnssrv_back_initialize},
64 #endif
65 #if defined(SLAPD_LDAP) && !defined(SLAPD_LDAP_DYNAMIC)
66         {"ldap",        ldap_back_initialize},
67 #endif
68 #if defined(SLAPD_LDBM) && !defined(SLAPD_LDBM_DYNAMIC)
69         {"ldbm",        ldbm_back_initialize},
70 #endif
71 #if defined(SLAPD_META) && !defined(SLAPD_META_DYNAMIC)
72         {"meta",        meta_back_initialize},
73 #endif
74 #if defined(SLAPD_MONITOR) && !defined(SLAPD_MONITOR_DYNAMIC)
75         {"monitor",     monitor_back_initialize},
76 #endif
77 #if defined(SLAPD_PASSWD) && !defined(SLAPD_PASSWD_DYNAMIC)
78         {"passwd",      passwd_back_initialize},
79 #endif
80 #if defined(SLAPD_PERL) && !defined(SLAPD_PERL_DYNAMIC)
81         {"perl",        perl_back_initialize},
82 #endif
83 #if defined(SLAPD_SHELL) && !defined(SLAPD_SHELL_DYNAMIC)
84         {"shell",       shell_back_initialize},
85 #endif
86 #if defined(SLAPD_TCL) && !defined(SLAPD_TCL_DYNAMIC)
87         {"tcl",         tcl_back_initialize},
88 #endif
89 #if defined(SLAPD_SQL) && !defined(SLAPD_SQL_DYNAMIC)
90         {"sql",         sql_back_initialize},
91 #endif
92         /* for any private backend */
93 #if defined(SLAPD_PRIVATE) && !defined(SLAPD_PRIVATE_DYNAMIC)
94         {"private",     private_back_initialize},
95 #endif
96         {NULL}
97 };
98
99 int                     nBackendInfo = 0;
100 BackendInfo     *backendInfo = NULL;
101
102 int                     nBackendDB = 0; 
103 BackendDB       *backendDB = NULL;
104
105 int backend_init(void)
106 {
107         int rc = -1;
108
109         if((nBackendInfo != 0) || (backendInfo != NULL)) {
110                 /* already initialized */
111 #ifdef NEW_LOGGING
112                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
113                            "backend_init:  backend already initialized\n" ));
114 #else
115                 Debug( LDAP_DEBUG_ANY,
116                         "backend_init: already initialized.\n", 0, 0, 0 );
117 #endif
118                 return -1;
119         }
120
121         for( ;
122                 binfo[nBackendInfo].bi_type != NULL;
123                 nBackendInfo++ )
124         {
125                 rc = binfo[nBackendInfo].bi_init( &binfo[nBackendInfo] );
126
127                 if(rc != 0) {
128 #ifdef NEW_LOGGING
129                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
130                                 "backend_init:  initialized for type \"%s\"\n",
131                                 binfo[nBackendInfo].bi_type ));
132 #else
133                         Debug( LDAP_DEBUG_ANY,
134                                 "backend_init: initialized for type \"%s\"\n",
135                                 binfo[nBackendInfo].bi_type, 0, 0 );
136 #endif
137                         /* destroy those we've already inited */
138                         for( nBackendInfo--;
139                                 nBackendInfo >= 0 ;
140                                 nBackendInfo-- )
141                         { 
142                                 if ( binfo[nBackendInfo].bi_destroy ) {
143                                         binfo[nBackendInfo].bi_destroy(
144                                                 &binfo[nBackendInfo] );
145                                 }
146                         }
147                         return rc;
148                 }
149         }
150
151         if ( nBackendInfo > 0) {
152                 backendInfo = binfo;
153                 return 0;
154         }
155
156 #ifdef SLAPD_MODULES    
157         return 0;
158 #else
159
160 #ifdef NEW_LOGGING
161         LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
162                 "backend_init: failed\n" ));
163 #else
164         Debug( LDAP_DEBUG_ANY,
165                 "backend_init: failed\n",
166                 0, 0, 0 );
167 #endif
168
169         return rc;
170 #endif /* SLAPD_MODULES */
171 }
172
173 int backend_add(BackendInfo *aBackendInfo)
174 {
175    int rc = 0;
176
177    if ((rc = aBackendInfo->bi_init(aBackendInfo)) != 0) {
178 #ifdef NEW_LOGGING
179        LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
180                   "backend_add:  initialization for type \"%s\" failed\n",
181                   aBackendInfo->bi_type ));
182 #else
183       Debug( LDAP_DEBUG_ANY,
184              "backend_add: initialization for type \"%s\" failed\n",
185              aBackendInfo->bi_type, 0, 0 );
186 #endif
187       return rc;
188    }
189
190    /* now add the backend type to the Backend Info List */
191    {
192       BackendInfo *newBackendInfo = 0;
193
194       /* if backendInfo == binfo no deallocation of old backendInfo */
195       if (backendInfo == binfo) {
196          newBackendInfo = ch_calloc(nBackendInfo + 1, sizeof(BackendInfo));
197          AC_MEMCPY(newBackendInfo, backendInfo, sizeof(BackendInfo) * 
198                 nBackendInfo);
199       } else {
200          newBackendInfo = ch_realloc(backendInfo, sizeof(BackendInfo) * 
201                                      (nBackendInfo + 1));
202       }
203       AC_MEMCPY(&newBackendInfo[nBackendInfo], aBackendInfo, 
204              sizeof(BackendInfo));
205       backendInfo = newBackendInfo;
206       nBackendInfo++;
207
208       return 0;
209    }        
210 }
211
212 int backend_startup(Backend *be)
213 {
214         int i;
215         int rc = 0;
216
217         if( ! ( nBackendDB > 0 ) ) {
218                 /* no databases */
219 #ifdef NEW_LOGGING
220                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
221                            "backend_startup: %d databases to startup. \n",
222                            nBackendDB ));
223 #else
224                 Debug( LDAP_DEBUG_ANY,
225                         "backend_startup: %d databases to startup.\n",
226                         nBackendDB, 0, 0 );
227 #endif
228                 return 1;
229         }
230
231         if(be != NULL) {
232                 /* startup a specific backend database */
233 #ifdef NEW_LOGGING
234                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
235                            "backend_startup:  starting \"%s\"\n",
236                            be->be_suffix[0] ));
237 #else
238                 Debug( LDAP_DEBUG_TRACE,
239                         "backend_startup: starting \"%s\"\n",
240                         be->be_suffix[0], 0, 0 );
241 #endif
242
243                 if ( be->bd_info->bi_open ) {
244                         rc = be->bd_info->bi_open( be->bd_info );
245                 }
246
247                 if(rc != 0) {
248 #ifdef NEW_LOGGING
249                         LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
250                                    "backend_startup: bi_open failed!\n" ));
251 #else
252                         Debug( LDAP_DEBUG_ANY,
253                                 "backend_startup: bi_open failed!\n",
254                                 0, 0, 0 );
255 #endif
256
257                         return rc;
258                 }
259
260                 if ( be->bd_info->bi_db_open ) {
261                         rc = be->bd_info->bi_db_open( be );
262                 }
263
264                 if(rc != 0) {
265 #ifdef NEW_LOGGING
266                         LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
267                                    "backend_startup: bi_db_open failed!\n" ));
268 #else
269                         Debug( LDAP_DEBUG_ANY,
270                                 "backend_startup: bi_db_open failed!\n",
271                                 0, 0, 0 );
272 #endif
273                         return rc;
274                 }
275
276                 return rc;
277         }
278
279         /* open each backend type */
280         for( i = 0; i < nBackendInfo; i++ ) {
281                 if( backendInfo[i].bi_nDB == 0) {
282                         /* no database of this type, don't open */
283                         continue;
284                 }
285
286                 if( backendInfo[i].bi_open ) {
287                         rc = backendInfo[i].bi_open(
288                                 &backendInfo[i] );
289                 }
290
291                 if(rc != 0) {
292 #ifdef NEW_LOGGING
293                         LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
294                                    "backend_startup: bi_open %d failed!\n", i ));
295 #else
296                         Debug( LDAP_DEBUG_ANY,
297                                 "backend_startup: bi_open %d failed!\n",
298                                 i, 0, 0 );
299 #endif
300                         return rc;
301                 }
302         }
303
304         /* open each backend database */
305         for( i = 0; i < nBackendDB; i++ ) {
306                 /* append global access controls */
307                 acl_append( &backendDB[i].be_acl, global_acl );
308
309                 if ( backendDB[i].bd_info->bi_db_open ) {
310                         rc = backendDB[i].bd_info->bi_db_open(
311                                 &backendDB[i] );
312                 }
313
314                 if(rc != 0) {
315 #ifdef NEW_LOGGING
316                         LDAP_LOG(( "backend", LDAP_LEVEL_CRIT,
317                                    "backend_startup: bi_db_open %d failed!\n", i ));
318 #else
319                         Debug( LDAP_DEBUG_ANY,
320                                 "backend_startup: bi_db_open %d failed!\n",
321                                 i, 0, 0 );
322 #endif
323                         return rc;
324                 }
325         }
326
327         return rc;
328 }
329
330 int backend_num( Backend *be )
331 {
332         int i;
333
334         if( be == NULL ) return -1;
335
336         for( i = 0; i < nBackendDB; i++ ) {
337                 if( be == &backendDB[i] ) return i;
338         }
339         return -1;
340 }
341
342 int backend_shutdown( Backend *be )
343 {
344         int i;
345         int rc = 0;
346
347         if( be != NULL ) {
348                 /* shutdown a specific backend database */
349
350                 if ( be->bd_info->bi_nDB == 0 ) {
351                         /* no database of this type, we never opened it */
352                         return 0;
353                 }
354
355                 if ( be->bd_info->bi_db_close ) {
356                         be->bd_info->bi_db_close( be );
357                 }
358
359                 if( be->bd_info->bi_close ) {
360                         be->bd_info->bi_close( be->bd_info );
361                 }
362
363                 return 0;
364         }
365
366         /* close each backend database */
367         for( i = 0; i < nBackendDB; i++ ) {
368                 if ( backendDB[i].bd_info->bi_db_close ) {
369                         backendDB[i].bd_info->bi_db_close(
370                                 &backendDB[i] );
371                 }
372
373                 if(rc != 0) {
374 #ifdef NEW_LOGGING
375                         LDAP_LOG(( "backend", LDAP_LEVEL_NOTICE,
376                                    "backend_shutdown: bi_close %s failed!\n",
377                                    backendDB[i].be_type ));
378 #else
379                         Debug( LDAP_DEBUG_ANY,
380                                 "backend_close: bi_close %s failed!\n",
381                                 backendDB[i].be_type, 0, 0 );
382 #endif
383                 }
384         }
385
386         /* close each backend type */
387         for( i = 0; i < nBackendInfo; i++ ) {
388                 if( backendInfo[i].bi_nDB == 0 ) {
389                         /* no database of this type */
390                         continue;
391                 }
392
393                 if( backendInfo[i].bi_close ) {
394                         backendInfo[i].bi_close(
395                                 &backendInfo[i] );
396                 }
397         }
398
399         return 0;
400 }
401
402 int backend_destroy(void)
403 {
404         int i;
405
406         /* destroy each backend database */
407         for( i = 0; i < nBackendDB; i++ ) {
408                 if ( backendDB[i].bd_info->bi_db_destroy ) {
409                         backendDB[i].bd_info->bi_db_destroy(
410                                 &backendDB[i] );
411                 }
412         }
413
414         /* destroy each backend type */
415         for( i = 0; i < nBackendInfo; i++ ) {
416                 if( backendInfo[i].bi_destroy ) {
417                         backendInfo[i].bi_destroy(
418                                 &backendInfo[i] );
419                 }
420         }
421
422 #ifdef SLAPD_MODULES
423         if (backendInfo != binfo) {
424            free(backendInfo);
425         }
426 #endif /* SLAPD_MODULES */
427
428         nBackendInfo = 0;
429         backendInfo = NULL;
430
431         return 0;
432 }
433
434 BackendInfo* backend_info(const char *type)
435 {
436         int i;
437
438         /* search for the backend type */
439         for( i = 0; i < nBackendInfo; i++ ) {
440                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
441                         return &backendInfo[i];
442                 }
443         }
444
445         return NULL;
446 }
447
448
449 BackendDB *
450 backend_db_init(
451     const char  *type
452 )
453 {
454         Backend *be;
455         BackendInfo *bi = backend_info(type);
456         int     rc = 0;
457
458         if( bi == NULL ) {
459                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
460                 return NULL;
461         }
462
463         backendDB = (BackendDB *) ch_realloc(
464                         (char *) backendDB,
465                     (nBackendDB + 1) * sizeof(Backend) );
466
467         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
468
469         be = &backends[nbackends++];
470
471         be->bd_info = bi;
472         be->be_def_limit = deflimit;
473         be->be_dfltaccess = global_default_access;
474
475         be->be_restrictops = global_restrictops;
476         be->be_requires = global_requires;
477
478         /* assign a default depth limit for alias deref */
479         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
480
481         if(bi->bi_db_init) {
482                 rc = bi->bi_db_init( be );
483         }
484
485         if(rc != 0) {
486                 fprintf( stderr, "database init failed (%s)\n", type );
487                 nbackends--;
488                 return NULL;
489         }
490
491         bi->bi_nDB++;
492         return( be );
493 }
494
495 void
496 be_db_close( void )
497 {
498         int     i;
499
500         for ( i = 0; i < nbackends; i++ ) {
501                 if ( backends[i].bd_info->bi_db_close ) {
502                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
503                 }
504         }
505 }
506
507 Backend *
508 select_backend(
509         const char * dn,
510         int manageDSAit )
511 {
512         int     i, j, len, dnlen;
513         Backend *be = NULL;
514
515         dnlen = strlen( dn );
516         for ( i = 0; i < nbackends; i++ ) {
517                 for ( j = 0; backends[i].be_nsuffix != NULL &&
518                     backends[i].be_nsuffix[j] != NULL; j++ )
519                 {
520                         len = strlen( backends[i].be_nsuffix[j] );
521
522                         if ( len > dnlen ) {
523                                 /* suffix is longer than DN */
524                                 continue;
525                         }
526
527                         
528                         if ( len && len < dnlen && !DN_SEPARATOR( dn[(dnlen-len)-1] ) ) {
529                                 /* make sure we have a separator */
530                                 continue;
531                         }
532                         
533
534                         if ( strcmp( backends[i].be_nsuffix[j], &dn[dnlen-len] ) == 0 ) {
535                                 if( be == NULL ) {
536                                         be = &backends[i];
537
538                                         if( manageDSAit && len == dnlen ) {
539                                                 continue;
540                                         }
541                                 } else {
542                                         be = &backends[i];
543                                 }
544                                 return be;
545                         }
546                 }
547         }
548
549         return be;
550 }
551
552 int
553 be_issuffix(
554     Backend     *be,
555     const char  *suffix
556 )
557 {
558         int     i;
559
560         for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
561                 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
562                         return( 1 );
563                 }
564         }
565
566         return( 0 );
567 }
568
569 int
570 be_isroot( Backend *be, const char *ndn )
571 {
572         int rc;
573
574         if ( ndn == NULL || *ndn == '\0' ) {
575                 return( 0 );
576         }
577
578         if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
579                 return( 0 );
580         }
581
582         rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
583
584         return(rc);
585 }
586
587 char *
588 be_root_dn( Backend *be )
589 {
590         if ( be->be_root_dn == NULL ) {
591                 return( "" );
592         }
593
594         return be->be_root_dn;
595 }
596
597 int
598 be_isroot_pw( Backend *be,
599         Connection *conn,
600         const char *ndn,
601         struct berval *cred )
602 {
603         int result;
604
605         if ( ! be_isroot( be, ndn ) ) {
606                 return 0;
607         }
608
609         if( be->be_root_pw.bv_len == 0 ) {
610                 return 0;
611         }
612
613 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
614         ldap_pvt_thread_mutex_lock( &passwd_mutex );
615 #ifdef SLAPD_SPASSWD
616         lutil_passwd_sasl_conn = conn->c_sasl_context;
617 #endif
618 #endif
619
620         result = lutil_passwd( &be->be_root_pw, cred, NULL );
621
622 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
623 #ifdef SLAPD_SPASSWD
624         lutil_passwd_sasl_conn = NULL;
625 #endif
626         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
627 #endif
628
629         return result == 0;
630 }
631
632 int
633 be_entry_release_rw(
634         BackendDB *be,
635         Connection *conn,
636         Operation *op,
637         Entry *e,
638         int rw )
639 {
640         if ( be->be_release ) {
641                 /* free and release entry from backend */
642                 return be->be_release( be, conn, op, e, rw );
643         } else {
644                 /* free entry */
645                 entry_free( e );
646                 return 0;
647         }
648 }
649
650 int
651 backend_unbind(
652         Connection   *conn,
653         Operation    *op
654 )
655 {
656         int     i;
657
658         for ( i = 0; i < nbackends; i++ ) {
659                 if ( backends[i].be_unbind ) {
660                         (*backends[i].be_unbind)( &backends[i], conn, op );
661                 }
662         }
663
664         return 0;
665 }
666
667 int
668 backend_connection_init(
669         Connection   *conn
670 )
671 {
672         int     i;
673
674         for ( i = 0; i < nbackends; i++ ) {
675                 if ( backends[i].be_connection_init ) {
676                         (*backends[i].be_connection_init)( &backends[i], conn);
677                 }
678         }
679
680         return 0;
681 }
682
683 int
684 backend_connection_destroy(
685         Connection   *conn
686 )
687 {
688         int     i;
689
690         for ( i = 0; i < nbackends; i++ ) {
691                 if ( backends[i].be_connection_destroy ) {
692                         (*backends[i].be_connection_destroy)( &backends[i], conn);
693                 }
694         }
695
696         return 0;
697 }
698
699 static int
700 backend_check_controls(
701         Backend *be,
702         Connection *conn,
703         Operation *op,
704         const char **text )
705 {
706         LDAPControl **ctrls;
707         ctrls = op->o_ctrls;
708         if( ctrls == NULL ) {
709                 return LDAP_SUCCESS;
710         }
711
712         for( ; *ctrls != NULL ; ctrls++ ) {
713                 if( (*ctrls)->ldctl_iscritical &&
714                         !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
715                 {
716                         *text = "control unavailable in NamingContext";
717                         return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
718                 }
719         }
720
721         return LDAP_SUCCESS;
722 }
723
724 int
725 backend_check_restrictions(
726         Backend *be,
727         Connection *conn,
728         Operation *op,
729         const void *opdata,
730         const char **text )
731 {
732         int rc;
733         slap_mask_t restrictops;
734         slap_mask_t requires;
735         slap_mask_t opflag;
736         slap_ssf_set_t *ssf;
737         int updateop = 0;
738
739         if( be ) {
740                 rc = backend_check_controls( be, conn, op, text );
741
742                 if( rc != LDAP_SUCCESS ) {
743                         return rc;
744                 }
745
746                 restrictops = be->be_restrictops;
747                 requires = be->be_requires;
748                 ssf = &be->be_ssf_set;
749
750         } else {
751                 restrictops = global_restrictops;
752                 requires = global_requires;
753                 ssf = &global_ssf_set;
754         }
755
756         switch( op->o_tag ) {
757         case LDAP_REQ_ADD:
758                 opflag = SLAP_RESTRICT_OP_ADD;
759                 updateop++;
760                 break;
761         case LDAP_REQ_BIND:
762                 opflag = SLAP_RESTRICT_OP_BIND;
763                 break;
764         case LDAP_REQ_COMPARE:
765                 opflag = SLAP_RESTRICT_OP_COMPARE;
766                 break;
767         case LDAP_REQ_DELETE:
768                 updateop++;
769                 opflag = SLAP_RESTRICT_OP_DELETE;
770                 break;
771         case LDAP_REQ_EXTENDED:
772                 opflag = SLAP_RESTRICT_OP_EXTENDED;
773                 break;
774         case LDAP_REQ_MODIFY:
775                 updateop++;
776                 opflag = SLAP_RESTRICT_OP_MODIFY;
777                 break;
778         case LDAP_REQ_RENAME:
779                 updateop++;
780                 opflag = SLAP_RESTRICT_OP_RENAME;
781                 break;
782         case LDAP_REQ_SEARCH:
783                 opflag = SLAP_RESTRICT_OP_SEARCH;
784                 break;
785         case LDAP_REQ_UNBIND:
786                 opflag = 0;
787                 break;
788         default:
789                 *text = "restrict operations internal error";
790                 return LDAP_OTHER;
791         }
792
793         if ( op->o_tag != LDAP_REQ_EXTENDED
794                 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
795         {
796                 /* these checks don't apply to StartTLS */
797
798                 if( op->o_tag == LDAP_REQ_EXTENDED ) {
799                         /* threat other extended operations as update ops */
800                         updateop++;
801                 }
802
803                 if( op->o_transport_ssf < ssf->sss_transport ) {
804                         *text = "transport confidentiality required";
805                         return LDAP_CONFIDENTIALITY_REQUIRED;
806                 }
807
808                 if( op->o_tls_ssf < ssf->sss_tls ) {
809                         *text = "TLS confidentiality required";
810                         return LDAP_CONFIDENTIALITY_REQUIRED;
811                 }
812
813                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
814                         /* these checks don't apply to SASL bind */
815
816                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
817                                 *text = "SASL confidentiality required";
818                                 return LDAP_CONFIDENTIALITY_REQUIRED;
819                         }
820
821                         if( op->o_ssf < ssf->sss_ssf ) {
822                                 *text = "confidentiality required";
823                                 return LDAP_CONFIDENTIALITY_REQUIRED;
824                         }
825                 }
826
827                 if( updateop ) {
828                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
829                                 *text = "transport update confidentiality required";
830                                 return LDAP_CONFIDENTIALITY_REQUIRED;
831                         }
832
833                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
834                                 *text = "TLS update confidentiality required";
835                                 return LDAP_CONFIDENTIALITY_REQUIRED;
836                         }
837
838                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
839                                 *text = "SASL update confidentiality required";
840                                 return LDAP_CONFIDENTIALITY_REQUIRED;
841                         }
842
843                         if( op->o_ssf < ssf->sss_update_ssf ) {
844                                 *text = "update confidentiality required";
845                                 return LDAP_CONFIDENTIALITY_REQUIRED;
846                         }
847
848                         if( op->o_ndn == NULL ) {
849                                 *text = "modifications require authentication";
850                                 return LDAP_OPERATIONS_ERROR;
851                         }
852                 }
853         }
854
855         if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
856                 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
857         {
858                 /* these checks don't apply to Bind or StartTLS */
859
860                 if( requires & SLAP_REQUIRE_STRONG ) {
861                         /* should check mechanism */
862                         if( op->o_authmech == NULL ||
863                                 op->o_dn == NULL || *op->o_dn == '\0' )
864                         {
865                                 *text = "strong authentication required";
866                                 return LDAP_STRONG_AUTH_REQUIRED;
867                         }
868                 }
869
870                 if( requires & SLAP_REQUIRE_SASL ) {
871                         if( op->o_authmech == NULL ||
872                                 op->o_dn == NULL || *op->o_dn == '\0' )
873                         {
874                                 *text = "SASL authentication required";
875                                 return LDAP_STRONG_AUTH_REQUIRED;
876                         }
877                 }
878                         
879                 if( requires & SLAP_REQUIRE_AUTHC ) {
880                         if( op->o_dn == NULL || *op->o_dn == '\0' ) {
881                                 *text = "authentication required";
882                                 return LDAP_UNWILLING_TO_PERFORM;
883                         }
884                 }
885
886                 if( requires & SLAP_REQUIRE_BIND ) {
887                         int version;
888                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
889                         version = conn->c_protocol;
890                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
891
892                         if( !version ) {
893                                 /* no bind has occurred */
894                                 *text = "BIND required";
895                                 return LDAP_OPERATIONS_ERROR;
896                         }
897                 }
898
899                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
900                         if( op->o_protocol < LDAP_VERSION3 ) {
901                                 /* no bind has occurred */
902                                 *text = "operation restricted to LDAPv3 clients";
903                                 return LDAP_OPERATIONS_ERROR;
904                         }
905                 }
906         }
907
908         if( restrictops & opflag ) {
909                 if( restrictops == SLAP_RESTRICT_OP_READS ) {
910                         *text = "read operations restricted";
911                 } else {
912                         *text = "operation restricted";
913                 }
914                 return LDAP_UNWILLING_TO_PERFORM;
915         }
916
917         return LDAP_SUCCESS;
918 }
919
920 int backend_check_referrals(
921         Backend *be,
922         Connection *conn,
923         Operation *op,
924         const char *dn,
925         const char *ndn )
926 {
927         int rc = LDAP_SUCCESS;
928
929         if( be->be_chk_referrals ) {
930                 const char *text;
931
932                 rc = be->be_chk_referrals( be,
933                         conn, op, dn, ndn, &text );
934
935                 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
936                         send_ldap_result( conn, op, rc,
937                                 NULL, text, NULL, NULL );
938                 }
939         }
940
941         return rc;
942 }
943
944 int 
945 backend_group(
946         Backend *be,
947         Connection *conn,
948         Operation *op,
949         Entry   *target,
950         const char      *gr_ndn,
951         const char      *op_ndn,
952         ObjectClass *group_oc,
953         AttributeDescription *group_at
954 )
955 {
956         if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
957                 /* we won't attempt to send it to a different backend */
958                 
959                 be = select_backend(gr_ndn, 0);
960
961                 if (be == NULL) {
962                         return LDAP_NO_SUCH_OBJECT;
963                 }
964         } 
965
966         if( be->be_group ) {
967                 return be->be_group( be, conn, op,
968                         target, gr_ndn, op_ndn,
969                         group_oc, group_at );
970         }
971
972         return LDAP_UNWILLING_TO_PERFORM;
973 }
974
975 int 
976 backend_attribute(
977         Backend *be,
978         Connection *conn,
979         Operation *op,
980         Entry   *target,
981         const char      *e_ndn,
982         AttributeDescription *entry_at,
983         struct berval ***vals
984 )
985 {
986         if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
987                 /* we won't attempt to send it to a different backend */
988                 
989                 be = select_backend(e_ndn, 0);
990
991                 if (be == NULL) {
992                         return LDAP_NO_SUCH_OBJECT;
993                 }
994         } 
995
996         if( be->be_attribute ) {
997                 return be->be_attribute( be, conn, op, target, e_ndn,
998                         entry_at, vals );
999         }
1000
1001         return LDAP_UNWILLING_TO_PERFORM;
1002 }
1003
1004 Attribute *backend_operational(
1005         Backend *be,
1006         Connection *conn,
1007         Operation *op,
1008         Entry *e )
1009 {
1010         Attribute *a = NULL;
1011
1012 #ifdef SLAPD_SCHEMA_DN
1013         a = ch_malloc( sizeof( Attribute ) );
1014         a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
1015
1016         /* Should be backend specific */
1017         a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
1018         a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );
1019         a->a_vals[1] = NULL;
1020
1021         a->a_next = NULL;
1022 #endif
1023
1024         return a;
1025 }