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