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