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