]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
fa749b85e90e85bf178e783884205e1d86b639e8
[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_sizelimit = defsize;
473         be->be_timelimit = deftime;
474         be->be_dfltaccess = global_default_access;
475
476         be->be_restrictops = global_restrictops;
477         be->be_requires = global_requires;
478
479         /* assign a default depth limit for alias deref */
480         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
481
482         if(bi->bi_db_init) {
483                 rc = bi->bi_db_init( be );
484         }
485
486         if(rc != 0) {
487                 fprintf( stderr, "database init failed (%s)\n", type );
488                 nbackends--;
489                 return NULL;
490         }
491
492         bi->bi_nDB++;
493         return( be );
494 }
495
496 void
497 be_db_close( void )
498 {
499         int     i;
500
501         for ( i = 0; i < nbackends; i++ ) {
502                 if ( backends[i].bd_info->bi_db_close ) {
503                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
504                 }
505         }
506 }
507
508 Backend *
509 select_backend(
510         const char * dn,
511         int manageDSAit )
512 {
513         int     i, j, len, dnlen;
514         Backend *be = NULL;
515
516         dnlen = strlen( dn );
517         for ( i = 0; i < nbackends; i++ ) {
518                 for ( j = 0; backends[i].be_nsuffix != NULL &&
519                     backends[i].be_nsuffix[j] != NULL; j++ )
520                 {
521                         len = strlen( backends[i].be_nsuffix[j] );
522
523                         if ( len > dnlen ) {
524                                 /* suffix is longer than DN */
525                                 continue;
526                         }
527
528                         
529                         if ( len && len < dnlen && !DN_SEPARATOR( dn[(dnlen-len)-1] ) ) {
530                                 /* make sure we have a separator */
531                                 continue;
532                         }
533                         
534
535                         if ( strcmp( backends[i].be_nsuffix[j], &dn[dnlen-len] ) == 0 ) {
536                                 if( be == NULL ) {
537                                         be = &backends[i];
538
539                                         if( manageDSAit && len == dnlen ) {
540                                                 continue;
541                                         }
542                                 } else {
543                                         be = &backends[i];
544                                 }
545                                 return be;
546                         }
547                 }
548         }
549
550         return be;
551 }
552
553 int
554 be_issuffix(
555     Backend     *be,
556     const char  *suffix
557 )
558 {
559         int     i;
560
561         for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
562                 if ( strcmp( be->be_nsuffix[i], suffix ) == 0 ) {
563                         return( 1 );
564                 }
565         }
566
567         return( 0 );
568 }
569
570 int
571 be_isroot( Backend *be, const char *ndn )
572 {
573         int rc;
574
575         if ( ndn == NULL || *ndn == '\0' ) {
576                 return( 0 );
577         }
578
579         if ( be->be_root_ndn == NULL || *be->be_root_ndn == '\0' ) {
580                 return( 0 );
581         }
582
583         rc = strcmp( be->be_root_ndn, ndn ) ? 0 : 1;
584
585         return(rc);
586 }
587
588 char *
589 be_root_dn( Backend *be )
590 {
591         if ( be->be_root_dn == NULL ) {
592                 return( "" );
593         }
594
595         return be->be_root_dn;
596 }
597
598 int
599 be_isroot_pw( Backend *be,
600         Connection *conn,
601         const char *ndn,
602         struct berval *cred )
603 {
604         int result;
605
606         if ( ! be_isroot( be, ndn ) ) {
607                 return 0;
608         }
609
610         if( be->be_root_pw.bv_len == 0 ) {
611                 return 0;
612         }
613
614 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
615         ldap_pvt_thread_mutex_lock( &passwd_mutex );
616 #ifdef SLAPD_SPASSWD
617         lutil_passwd_sasl_conn = conn->c_sasl_context;
618 #endif
619 #endif
620
621         result = lutil_passwd( &be->be_root_pw, cred, NULL );
622
623 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
624 #ifdef SLAPD_SPASSWD
625         lutil_passwd_sasl_conn = NULL;
626 #endif
627         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
628 #endif
629
630         return result == 0;
631 }
632
633 int
634 be_entry_release_rw(
635         BackendDB *be,
636         Connection *conn,
637         Operation *op,
638         Entry *e,
639         int rw )
640 {
641         if ( be->be_release ) {
642                 /* free and release entry from backend */
643                 return be->be_release( be, conn, op, e, rw );
644         } else {
645                 /* free entry */
646                 entry_free( e );
647                 return 0;
648         }
649 }
650
651 int
652 backend_unbind(
653         Connection   *conn,
654         Operation    *op
655 )
656 {
657         int     i;
658
659         for ( i = 0; i < nbackends; i++ ) {
660                 if ( backends[i].be_unbind ) {
661                         (*backends[i].be_unbind)( &backends[i], conn, op );
662                 }
663         }
664
665         return 0;
666 }
667
668 int
669 backend_connection_init(
670         Connection   *conn
671 )
672 {
673         int     i;
674
675         for ( i = 0; i < nbackends; i++ ) {
676                 if ( backends[i].be_connection_init ) {
677                         (*backends[i].be_connection_init)( &backends[i], conn);
678                 }
679         }
680
681         return 0;
682 }
683
684 int
685 backend_connection_destroy(
686         Connection   *conn
687 )
688 {
689         int     i;
690
691         for ( i = 0; i < nbackends; i++ ) {
692                 if ( backends[i].be_connection_destroy ) {
693                         (*backends[i].be_connection_destroy)( &backends[i], conn);
694                 }
695         }
696
697         return 0;
698 }
699
700 static int
701 backend_check_controls(
702         Backend *be,
703         Connection *conn,
704         Operation *op,
705         const char **text )
706 {
707         LDAPControl **ctrls;
708         ctrls = op->o_ctrls;
709         if( ctrls == NULL ) {
710                 return LDAP_SUCCESS;
711         }
712
713         for( ; *ctrls != NULL ; ctrls++ ) {
714                 if( (*ctrls)->ldctl_iscritical &&
715                         !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
716                 {
717                         *text = "control unavailable in NamingContext";
718                         return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
719                 }
720         }
721
722         return LDAP_SUCCESS;
723 }
724
725 int
726 backend_check_restrictions(
727         Backend *be,
728         Connection *conn,
729         Operation *op,
730         const void *opdata,
731         const char **text )
732 {
733         int rc;
734         slap_mask_t restrictops;
735         slap_mask_t requires;
736         slap_mask_t opflag;
737         slap_ssf_set_t *ssf;
738         int updateop = 0;
739
740         if( be ) {
741                 rc = backend_check_controls( be, conn, op, text );
742
743                 if( rc != LDAP_SUCCESS ) {
744                         return rc;
745                 }
746
747                 restrictops = be->be_restrictops;
748                 requires = be->be_requires;
749                 ssf = &be->be_ssf_set;
750
751         } else {
752                 restrictops = global_restrictops;
753                 requires = global_requires;
754                 ssf = &global_ssf_set;
755         }
756
757         switch( op->o_tag ) {
758         case LDAP_REQ_ADD:
759                 opflag = SLAP_RESTRICT_OP_ADD;
760                 updateop++;
761                 break;
762         case LDAP_REQ_BIND:
763                 opflag = SLAP_RESTRICT_OP_BIND;
764                 break;
765         case LDAP_REQ_COMPARE:
766                 opflag = SLAP_RESTRICT_OP_COMPARE;
767                 break;
768         case LDAP_REQ_DELETE:
769                 updateop++;
770                 opflag = SLAP_RESTRICT_OP_DELETE;
771                 break;
772         case LDAP_REQ_EXTENDED:
773                 opflag = SLAP_RESTRICT_OP_EXTENDED;
774                 break;
775         case LDAP_REQ_MODIFY:
776                 updateop++;
777                 opflag = SLAP_RESTRICT_OP_MODIFY;
778                 break;
779         case LDAP_REQ_RENAME:
780                 updateop++;
781                 opflag = SLAP_RESTRICT_OP_RENAME;
782                 break;
783         case LDAP_REQ_SEARCH:
784                 opflag = SLAP_RESTRICT_OP_SEARCH;
785                 break;
786         case LDAP_REQ_UNBIND:
787                 opflag = 0;
788                 break;
789         default:
790                 *text = "restrict operations internal error";
791                 return LDAP_OTHER;
792         }
793
794         if ( op->o_tag != LDAP_REQ_EXTENDED
795                 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
796         {
797                 /* these checks don't apply to StartTLS */
798
799                 if( op->o_tag == LDAP_REQ_EXTENDED ) {
800                         /* threat other extended operations as update ops */
801                         updateop++;
802                 }
803
804                 if( op->o_transport_ssf < ssf->sss_transport ) {
805                         *text = "transport confidentiality required";
806                         return LDAP_CONFIDENTIALITY_REQUIRED;
807                 }
808
809                 if( op->o_tls_ssf < ssf->sss_tls ) {
810                         *text = "TLS confidentiality required";
811                         return LDAP_CONFIDENTIALITY_REQUIRED;
812                 }
813
814                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
815                         /* these checks don't apply to SASL bind */
816
817                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
818                                 *text = "SASL confidentiality required";
819                                 return LDAP_CONFIDENTIALITY_REQUIRED;
820                         }
821
822                         if( op->o_ssf < ssf->sss_ssf ) {
823                                 *text = "confidentiality required";
824                                 return LDAP_CONFIDENTIALITY_REQUIRED;
825                         }
826                 }
827
828                 if( updateop ) {
829                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
830                                 *text = "transport update confidentiality required";
831                                 return LDAP_CONFIDENTIALITY_REQUIRED;
832                         }
833
834                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
835                                 *text = "TLS update confidentiality required";
836                                 return LDAP_CONFIDENTIALITY_REQUIRED;
837                         }
838
839                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
840                                 *text = "SASL update confidentiality required";
841                                 return LDAP_CONFIDENTIALITY_REQUIRED;
842                         }
843
844                         if( op->o_ssf < ssf->sss_update_ssf ) {
845                                 *text = "update confidentiality required";
846                                 return LDAP_CONFIDENTIALITY_REQUIRED;
847                         }
848
849                         if( op->o_ndn == NULL ) {
850                                 *text = "modifications require authentication";
851                                 return LDAP_OPERATIONS_ERROR;
852                         }
853                 }
854         }
855
856         if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
857                 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
858         {
859                 /* these checks don't apply to Bind or StartTLS */
860
861                 if( requires & SLAP_REQUIRE_STRONG ) {
862                         /* should check mechanism */
863                         if( op->o_authmech == NULL ||
864                                 op->o_dn == NULL || *op->o_dn == '\0' )
865                         {
866                                 *text = "strong authentication required";
867                                 return LDAP_STRONG_AUTH_REQUIRED;
868                         }
869                 }
870
871                 if( requires & SLAP_REQUIRE_SASL ) {
872                         if( op->o_authmech == NULL ||
873                                 op->o_dn == NULL || *op->o_dn == '\0' )
874                         {
875                                 *text = "SASL authentication required";
876                                 return LDAP_STRONG_AUTH_REQUIRED;
877                         }
878                 }
879                         
880                 if( requires & SLAP_REQUIRE_AUTHC ) {
881                         if( op->o_dn == NULL || *op->o_dn == '\0' ) {
882                                 *text = "authentication required";
883                                 return LDAP_UNWILLING_TO_PERFORM;
884                         }
885                 }
886
887                 if( requires & SLAP_REQUIRE_BIND ) {
888                         int version;
889                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
890                         version = conn->c_protocol;
891                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
892
893                         if( !version ) {
894                                 /* no bind has occurred */
895                                 *text = "BIND required";
896                                 return LDAP_OPERATIONS_ERROR;
897                         }
898                 }
899
900                 if( requires & SLAP_REQUIRE_LDAP_V3 ) {
901                         if( op->o_protocol < LDAP_VERSION3 ) {
902                                 /* no bind has occurred */
903                                 *text = "operation restricted to LDAPv3 clients";
904                                 return LDAP_OPERATIONS_ERROR;
905                         }
906                 }
907         }
908
909         if( restrictops & opflag ) {
910                 if( restrictops == SLAP_RESTRICT_OP_READS ) {
911                         *text = "read operations restricted";
912                 } else {
913                         *text = "operation restricted";
914                 }
915                 return LDAP_UNWILLING_TO_PERFORM;
916         }
917
918         return LDAP_SUCCESS;
919 }
920
921 int backend_check_referrals(
922         Backend *be,
923         Connection *conn,
924         Operation *op,
925         const char *dn,
926         const char *ndn )
927 {
928         int rc = LDAP_SUCCESS;
929
930         if( be->be_chk_referrals ) {
931                 const char *text;
932
933                 rc = be->be_chk_referrals( be,
934                         conn, op, dn, ndn, &text );
935
936                 if( rc != LDAP_SUCCESS && rc != LDAP_REFERRAL ) {
937                         send_ldap_result( conn, op, rc,
938                                 NULL, text, NULL, NULL );
939                 }
940         }
941
942         return rc;
943 }
944
945 int 
946 backend_group(
947         Backend *be,
948         Connection *conn,
949         Operation *op,
950         Entry   *target,
951         const char      *gr_ndn,
952         const char      *op_ndn,
953         ObjectClass *group_oc,
954         AttributeDescription *group_at
955 )
956 {
957         if( strcmp( target->e_ndn, gr_ndn ) != 0 ) {
958                 /* we won't attempt to send it to a different backend */
959                 
960                 be = select_backend(gr_ndn, 0);
961
962                 if (be == NULL) {
963                         return LDAP_NO_SUCH_OBJECT;
964                 }
965         } 
966
967         if( be->be_group ) {
968                 return be->be_group( be, conn, op,
969                         target, gr_ndn, op_ndn,
970                         group_oc, group_at );
971         }
972
973         return LDAP_UNWILLING_TO_PERFORM;
974 }
975
976 int 
977 backend_attribute(
978         Backend *be,
979         Connection *conn,
980         Operation *op,
981         Entry   *target,
982         const char      *e_ndn,
983         AttributeDescription *entry_at,
984         struct berval ***vals
985 )
986 {
987         if( target == NULL || strcmp( target->e_ndn, e_ndn ) != 0 ) {
988                 /* we won't attempt to send it to a different backend */
989                 
990                 be = select_backend(e_ndn, 0);
991
992                 if (be == NULL) {
993                         return LDAP_NO_SUCH_OBJECT;
994                 }
995         } 
996
997         if( be->be_attribute ) {
998                 return be->be_attribute( be, conn, op, target, e_ndn,
999                         entry_at, vals );
1000         }
1001
1002         return LDAP_UNWILLING_TO_PERFORM;
1003 }
1004
1005 Attribute *backend_operational(
1006         Backend *be,
1007         Connection *conn,
1008         Operation *op,
1009         Entry *e )
1010 {
1011         Attribute *a = NULL;
1012
1013 #ifdef SLAPD_SCHEMA_DN
1014         a = ch_malloc( sizeof( Attribute ) );
1015         a->a_desc = ad_dup( slap_schema.si_ad_subschemaSubentry );
1016
1017         /* Should be backend specific */
1018         a->a_vals = ch_malloc( 2 * sizeof( struct berval * ) );
1019         a->a_vals[0] = ber_bvstrdup( SLAPD_SCHEMA_DN );
1020         a->a_vals[1] = NULL;
1021
1022         a->a_next = NULL;
1023 #endif
1024
1025         return a;
1026 }