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