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