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