]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Some misc cleanup
[openldap] / servers / slapd / backend.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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]->bv_val ));
245 #else
246                 Debug( LDAP_DEBUG_TRACE,
247                         "backend_startup: starting \"%s\"\n",
248                         be->be_suffix[0]->bv_val, 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! (%d)\n", rc ));
276 #else
277                         Debug( LDAP_DEBUG_ANY,
278                                 "backend_startup: bi_db_open failed! (%d)\n",
279                                 rc, 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! (%d)\n",
326                                 i, rc ));
327 #else
328                         Debug( LDAP_DEBUG_ANY,
329                                 "backend_startup: bi_db_open(%d) failed! (%d)\n",
330                                 i, rc, 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         BackendDB *bd;
415
416         /* destroy each backend database */
417         for( i = 0, bd = backendDB; i < nBackendDB; i++, bd++ ) {
418                 if ( bd->bd_info->bi_db_destroy ) {
419                         bd->bd_info->bi_db_destroy( bd );
420                 }
421                 ber_bvecfree( bd->be_suffix );
422                 ber_bvecfree( bd->be_nsuffix );
423                 if ( bd->be_rootdn.bv_val ) free( bd->be_rootdn.bv_val );
424                 if ( bd->be_rootndn.bv_val ) free( bd->be_rootndn.bv_val );
425                 if ( bd->be_rootpw.bv_val ) free( bd->be_rootpw.bv_val );
426                 acl_destroy( bd->be_acl, global_acl );
427         }
428         free( backendDB );
429
430         /* destroy each backend type */
431         for( i = 0; i < nBackendInfo; i++ ) {
432                 if( backendInfo[i].bi_destroy ) {
433                         backendInfo[i].bi_destroy(
434                                 &backendInfo[i] );
435                 }
436         }
437
438 #ifdef SLAPD_MODULES
439         if (backendInfo != binfo) {
440            free(backendInfo);
441         }
442 #endif /* SLAPD_MODULES */
443
444         nBackendInfo = 0;
445         backendInfo = NULL;
446
447         return 0;
448 }
449
450 BackendInfo* backend_info(const char *type)
451 {
452         int i;
453
454         /* search for the backend type */
455         for( i = 0; i < nBackendInfo; i++ ) {
456                 if( strcasecmp(backendInfo[i].bi_type, type) == 0 ) {
457                         return &backendInfo[i];
458                 }
459         }
460
461         return NULL;
462 }
463
464
465 BackendDB *
466 backend_db_init(
467     const char  *type
468 )
469 {
470         Backend *be;
471         BackendInfo *bi = backend_info(type);
472         int     rc = 0;
473
474         if( bi == NULL ) {
475                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
476                 return NULL;
477         }
478
479         backendDB = (BackendDB *) ch_realloc(
480                         (char *) backendDB,
481                     (nBackendDB + 1) * sizeof(Backend) );
482
483         memset( &backendDB[nbackends], '\0', sizeof(Backend) );
484
485         be = &backends[nbackends++];
486
487         be->bd_info = bi;
488         be->be_def_limit = deflimit;
489         be->be_dfltaccess = global_default_access;
490
491         be->be_restrictops = global_restrictops;
492         be->be_requires = global_requires;
493         be->be_ssf_set = global_ssf_set;
494
495         /* assign a default depth limit for alias deref */
496         be->be_max_deref_depth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
497
498         if(bi->bi_db_init) {
499                 rc = bi->bi_db_init( be );
500         }
501
502         if(rc != 0) {
503                 fprintf( stderr, "database init failed (%s)\n", type );
504                 nbackends--;
505                 return NULL;
506         }
507
508         bi->bi_nDB++;
509         return( be );
510 }
511
512 void
513 be_db_close( void )
514 {
515         int     i;
516
517         for ( i = 0; i < nbackends; i++ ) {
518                 if ( backends[i].bd_info->bi_db_close ) {
519                         (*backends[i].bd_info->bi_db_close)( &backends[i] );
520                 }
521         }
522 }
523
524 Backend *
525 select_backend(
526         struct berval * dn,
527         int manageDSAit,
528         int noSubs )
529 {
530         int     i, j;
531         ber_len_t len, dnlen = dn->bv_len;
532         Backend *be = NULL;
533
534         for ( i = 0; i < nbackends; i++ ) {
535                 for ( j = 0; backends[i].be_nsuffix != NULL &&
536                     backends[i].be_nsuffix[j] != NULL; j++ )
537                 {
538                         if (( backends[i].be_flags & SLAP_BFLAG_GLUE_SUBORDINATE )
539                                 && noSubs )
540                         {
541                                 continue;
542                         }
543
544                         len = backends[i].be_nsuffix[j]->bv_len;
545
546                         if ( len > dnlen ) {
547                                 /* suffix is longer than DN */
548                                 continue;
549                         }
550                         
551                         if ( len && len < dnlen &&
552                                 ( !DN_SEPARATOR( dn->bv_val[(dnlen-len)-1] ) ||
553                                         DN_ESCAPE( dn->bv_val[(dnlen-len)-2] ) ) )
554                         {
555                                 /* make sure we have a separator */
556                                 continue;
557                         }
558
559                         if ( strcmp( backends[i].be_nsuffix[j]->bv_val,
560                                 &dn->bv_val[dnlen-len] ) == 0 )
561                         {
562                                 if( be == NULL ) {
563                                         be = &backends[i];
564
565                                         if( manageDSAit && len == dnlen ) {
566                                                 continue;
567                                         }
568                                 } else {
569                                         be = &backends[i];
570                                 }
571                                 return be;
572                         }
573                 }
574         }
575
576         return be;
577 }
578
579 int
580 be_issuffix(
581     Backend     *be,
582     struct berval       *bvsuffix
583 )
584 {
585         int     i;
586
587         for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i] != NULL; i++ ) {
588                 if ( ber_bvcmp( be->be_nsuffix[i], bvsuffix ) == 0 ) {
589                         return( 1 );
590                 }
591         }
592
593         return( 0 );
594 }
595
596 int
597 be_isroot( Backend *be, struct berval *ndn )
598 {
599         if ( !ndn->bv_len ) {
600                 return( 0 );
601         }
602
603         if ( !be->be_rootndn.bv_len ) {
604                 return( 0 );
605         }
606
607         return dn_match( &be->be_rootndn, ndn );
608 }
609
610 int
611 be_isupdate( Backend *be, struct berval *ndn )
612 {
613         if ( !ndn->bv_len ) {
614                 return( 0 );
615         }
616
617         if ( !be->be_update_ndn.bv_len ) {
618                 return( 0 );
619         }
620
621         return dn_match( &be->be_update_ndn, ndn );
622 }
623
624 struct berval *
625 be_root_dn( Backend *be )
626 {
627         return &be->be_rootdn;
628 }
629
630 int
631 be_isroot_pw( Backend *be,
632         Connection *conn,
633         struct berval *ndn,
634         struct berval *cred )
635 {
636         int result;
637
638         if ( ! be_isroot( be, ndn ) ) {
639                 return 0;
640         }
641
642         if( be->be_rootpw.bv_len == 0 ) {
643                 return 0;
644         }
645
646 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
647         ldap_pvt_thread_mutex_lock( &passwd_mutex );
648 #ifdef SLAPD_SPASSWD
649         lutil_passwd_sasl_conn = conn->c_sasl_context;
650 #endif
651 #endif
652
653         result = lutil_passwd( &be->be_rootpw, cred, NULL );
654
655 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
656 #ifdef SLAPD_SPASSWD
657         lutil_passwd_sasl_conn = NULL;
658 #endif
659         ldap_pvt_thread_mutex_unlock( &passwd_mutex );
660 #endif
661
662         return result == 0;
663 }
664
665 int
666 be_entry_release_rw(
667         BackendDB *be,
668         Connection *conn,
669         Operation *op,
670         Entry *e,
671         int rw )
672 {
673         if ( be->be_release ) {
674                 /* free and release entry from backend */
675                 return be->be_release( be, conn, op, e, rw );
676         } else {
677                 /* free entry */
678                 entry_free( e );
679                 return 0;
680         }
681 }
682
683 int
684 backend_unbind(
685         Connection   *conn,
686         Operation    *op
687 )
688 {
689         int     i;
690
691         for ( i = 0; i < nbackends; i++ ) {
692                 if ( backends[i].be_unbind ) {
693                         (*backends[i].be_unbind)( &backends[i], conn, op );
694                 }
695         }
696
697         return 0;
698 }
699
700 int
701 backend_connection_init(
702         Connection   *conn
703 )
704 {
705         int     i;
706
707         for ( i = 0; i < nbackends; i++ ) {
708                 if ( backends[i].be_connection_init ) {
709                         (*backends[i].be_connection_init)( &backends[i], conn);
710                 }
711         }
712
713         return 0;
714 }
715
716 int
717 backend_connection_destroy(
718         Connection   *conn
719 )
720 {
721         int     i;
722
723         for ( i = 0; i < nbackends; i++ ) {
724                 if ( backends[i].be_connection_destroy ) {
725                         (*backends[i].be_connection_destroy)( &backends[i], conn);
726                 }
727         }
728
729         return 0;
730 }
731
732 static int
733 backend_check_controls(
734         Backend *be,
735         Connection *conn,
736         Operation *op,
737         const char **text )
738 {
739         LDAPControl **ctrls;
740         ctrls = op->o_ctrls;
741         if( ctrls == NULL ) {
742                 return LDAP_SUCCESS;
743         }
744
745         for( ; *ctrls != NULL ; ctrls++ ) {
746                 if( (*ctrls)->ldctl_iscritical &&
747                         !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
748                 {
749                         *text = "control unavailable in context";
750                         return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
751                 }
752         }
753
754         return LDAP_SUCCESS;
755 }
756
757 int
758 backend_check_restrictions(
759         Backend *be,
760         Connection *conn,
761         Operation *op,
762         const void *opdata,
763         const char **text )
764 {
765         int rc;
766         slap_mask_t restrictops;
767         slap_mask_t requires;
768         slap_mask_t opflag;
769         slap_ssf_set_t *ssf;
770         int updateop = 0;
771
772         if( be ) {
773                 rc = backend_check_controls( be, conn, op, text );
774
775                 if( rc != LDAP_SUCCESS ) {
776                         return rc;
777                 }
778
779                 restrictops = be->be_restrictops;
780                 requires = be->be_requires;
781                 ssf = &be->be_ssf_set;
782
783         } else {
784                 restrictops = global_restrictops;
785                 requires = global_requires;
786                 ssf = &global_ssf_set;
787         }
788
789         switch( op->o_tag ) {
790         case LDAP_REQ_ADD:
791                 opflag = SLAP_RESTRICT_OP_ADD;
792                 updateop++;
793                 break;
794         case LDAP_REQ_BIND:
795                 opflag = SLAP_RESTRICT_OP_BIND;
796                 break;
797         case LDAP_REQ_COMPARE:
798                 opflag = SLAP_RESTRICT_OP_COMPARE;
799                 break;
800         case LDAP_REQ_DELETE:
801                 updateop++;
802                 opflag = SLAP_RESTRICT_OP_DELETE;
803                 break;
804         case LDAP_REQ_EXTENDED:
805                 opflag = SLAP_RESTRICT_OP_EXTENDED;
806                 break;
807         case LDAP_REQ_MODIFY:
808                 updateop++;
809                 opflag = SLAP_RESTRICT_OP_MODIFY;
810                 break;
811         case LDAP_REQ_RENAME:
812                 updateop++;
813                 opflag = SLAP_RESTRICT_OP_RENAME;
814                 break;
815         case LDAP_REQ_SEARCH:
816                 opflag = SLAP_RESTRICT_OP_SEARCH;
817                 break;
818         case LDAP_REQ_UNBIND:
819                 opflag = 0;
820                 break;
821         default:
822                 *text = "restrict operations internal error";
823                 return LDAP_OTHER;
824         }
825
826         if ( op->o_tag != LDAP_REQ_EXTENDED
827                 || strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) )
828         {
829                 /* these checks don't apply to StartTLS */
830
831                 if( op->o_tag == LDAP_REQ_EXTENDED ) {
832                         /* threat other extended operations as update ops */
833                         updateop++;
834                 }
835
836                 if( op->o_transport_ssf < ssf->sss_transport ) {
837                         *text = "transport confidentiality required";
838                         return LDAP_CONFIDENTIALITY_REQUIRED;
839                 }
840
841                 if( op->o_tls_ssf < ssf->sss_tls ) {
842                         *text = "TLS confidentiality required";
843                         return LDAP_CONFIDENTIALITY_REQUIRED;
844                 }
845
846                 if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
847                         /* these checks don't apply to SASL bind */
848
849                         if( op->o_sasl_ssf < ssf->sss_sasl ) {
850                                 *text = "SASL confidentiality required";
851                                 return LDAP_CONFIDENTIALITY_REQUIRED;
852                         }
853
854                         if( op->o_ssf < ssf->sss_ssf ) {
855                                 *text = "confidentiality required";
856                                 return LDAP_CONFIDENTIALITY_REQUIRED;
857                         }
858                 }
859
860                 if( updateop ) {
861                         if( op->o_transport_ssf < ssf->sss_update_transport ) {
862                                 *text = "transport update confidentiality required";
863                                 return LDAP_CONFIDENTIALITY_REQUIRED;
864                         }
865
866                         if( op->o_tls_ssf < ssf->sss_update_tls ) {
867                                 *text = "TLS update confidentiality required";
868                                 return LDAP_CONFIDENTIALITY_REQUIRED;
869                         }
870
871                         if( op->o_sasl_ssf < ssf->sss_update_sasl ) {
872                                 *text = "SASL update confidentiality required";
873                                 return LDAP_CONFIDENTIALITY_REQUIRED;
874                         }
875
876                         if( op->o_ssf < ssf->sss_update_ssf ) {
877                                 *text = "update confidentiality required";
878                                 return LDAP_CONFIDENTIALITY_REQUIRED;
879                         }
880
881                         if( op->o_ndn.bv_len == 0 ) {
882                                 *text = "modifications require authentication";
883                                 return LDAP_OPERATIONS_ERROR;
884                         }
885                 }
886         }
887
888         if ( op->o_tag != LDAP_REQ_BIND && ( op->o_tag != LDAP_REQ_EXTENDED ||
889                 strcmp( (const char *) opdata, LDAP_EXOP_START_TLS ) ) )
890         {
891                 /* these checks don't apply to Bind or StartTLS */
892
893                 if( requires & SLAP_REQUIRE_STRONG ) {
894                         /* should check mechanism */
895                         if( op->o_authmech.bv_len == 0 || op->o_dn.bv_len == 0 )
896                         {
897                                 *text = "strong authentication required";
898                                 return LDAP_STRONG_AUTH_REQUIRED;
899                         }
900                 }
901
902                 if( requires & SLAP_REQUIRE_SASL ) {
903                         if( op->o_authmech.bv_len == 0 || op->o_dn.bv_len == 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.bv_len == 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         struct berval *dn,
956         struct berval *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         struct berval *gr_ndn,
982         struct berval *op_ndn,
983         ObjectClass *group_oc,
984         AttributeDescription *group_at
985 )
986 {
987         GroupAssertion *g;
988         int i;
989
990         ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
991         i = op->o_abandon;
992         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
993         if (i) return SLAPD_ABANDON;
994
995         if ( !dn_match( &target->e_nname, gr_ndn ) ) {
996                 /* we won't attempt to send it to a different backend */
997                 
998                 be = select_backend( gr_ndn, 0,
999                         (be->be_flags & SLAP_BFLAG_GLUE_INSTANCE));
1000
1001                 if (be == NULL) {
1002                         return LDAP_NO_SUCH_OBJECT;
1003                 }
1004         } 
1005
1006         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1007         for (g = conn->c_groups; g; g=g->ga_next) {
1008                 if (g->ga_be != be || g->ga_oc != group_oc ||
1009                         g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
1010                         continue;
1011                 if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
1012                         break;
1013         }
1014         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1015         if (g) {
1016                 return g->ga_res;
1017         }
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) + gr_ndn->bv_len);
1026                         g->ga_be = be;
1027                         g->ga_oc = group_oc;
1028                         g->ga_at = group_at;
1029                         g->ga_res = res;
1030                         g->ga_len = gr_ndn->bv_len;
1031                         strcpy(g->ga_ndn, gr_ndn->bv_val);
1032                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1033                         g->ga_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         struct berval   *edn,
1051         AttributeDescription *entry_at,
1052         BerVarray *vals
1053 )
1054 {
1055         if ( target == NULL || !dn_match( &target->e_nname, edn ) ) {
1056                 /* we won't attempt to send it to a different backend */
1057                 
1058                 be = select_backend( edn, 0,
1059                         (be->be_flags & SLAP_BFLAG_GLUE_INSTANCE));
1060
1061                 if (be == NULL) {
1062                         return LDAP_NO_SUCH_OBJECT;
1063                 }
1064         } 
1065
1066         if( be->be_attribute ) {
1067                 return be->be_attribute( be, conn, op, target, edn,
1068                         entry_at, vals );
1069         }
1070
1071         return LDAP_UNWILLING_TO_PERFORM;
1072 }
1073
1074 Attribute *backend_operational(
1075         Backend *be,
1076         Connection *conn,
1077         Operation *op,
1078         Entry *e,
1079         AttributeName *attrs,
1080         int opattrs     )
1081 {
1082         Attribute *a = NULL, **ap = &a;
1083
1084 #ifdef SLAPD_SCHEMA_DN
1085         *ap = slap_operational_subschemaSubentry();
1086         ap = &(*ap)->a_next;
1087 #endif
1088
1089         /*
1090          * If operational attributes (allegedly) are required, 
1091          * and the backend supports specific operational attributes, 
1092          * add them to the attribute list
1093          */
1094         if ( ( opattrs || attrs ) && be && be->be_operational != NULL ) {
1095                 ( void )be->be_operational( be, conn, op, e, attrs, opattrs, ap );
1096         }
1097
1098         return a;
1099 }
1100