]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/backend.c
cleanup bind
[openldap] / servers / slapd / backend.c
index a9d5253785d82ff7fa097fc12912c75f1d59eaf1..637a1dfc31559ca78458fdecac31221459512ff0 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 /* backend.c - routines for dealing with back-end databases */
 
 #include <ac/string.h>
 #include <ac/socket.h>
-
 #include <sys/stat.h>
 
 #include "slap.h"
 #include "lutil.h"
 #include "lber_pvt.h"
 
+#ifdef LDAP_SLAPI
+#include "slapi.h"
+#endif
+
 /*
  * If a module is configured as dynamic, its header should not
  * get included into slapd. While this is a general rule and does
@@ -254,34 +257,32 @@ int backend_startup(Backend *be)
 
                if ( be->bd_info->bi_open ) {
                        rc = be->bd_info->bi_open( be->bd_info );
-               }
-
-               if(rc != 0) {
+                       if ( rc != 0 ) {
 #ifdef NEW_LOGGING
-                       LDAP_LOG( BACKEND, CRIT, "backend_startup: bi_open failed!\n", 0, 0, 0 );
+                               LDAP_LOG( BACKEND, CRIT, "backend_startup: bi_open failed!\n", 0, 0, 0 );
 #else
-                       Debug( LDAP_DEBUG_ANY,
-                               "backend_startup: bi_open failed!\n",
-                               0, 0, 0 );
+                               Debug( LDAP_DEBUG_ANY,
+                                       "backend_startup: bi_open failed!\n",
+                                       0, 0, 0 );
 #endif
 
-                       return rc;
+                               return rc;
+                       }
                }
 
                if ( be->bd_info->bi_db_open ) {
                        rc = be->bd_info->bi_db_open( be );
-               }
-
-               if(rc != 0) {
+                       if ( rc != 0 ) {
 #ifdef NEW_LOGGING
-                       LDAP_LOG( BACKEND, CRIT, 
-                               "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
+                               LDAP_LOG( BACKEND, CRIT, 
+                                       "backend_startup: bi_db_open failed! (%d)\n", rc, 0, 0 );
 #else
-                       Debug( LDAP_DEBUG_ANY,
-                               "backend_startup: bi_db_open failed! (%d)\n",
-                               rc, 0, 0 );
+                               Debug( LDAP_DEBUG_ANY,
+                                       "backend_startup: bi_db_open failed! (%d)\n",
+                                       rc, 0, 0 );
 #endif
-                       return rc;
+                               return rc;
+                       }
                }
 
                return rc;
@@ -297,18 +298,17 @@ int backend_startup(Backend *be)
                if( backendInfo[i].bi_open ) {
                        rc = backendInfo[i].bi_open(
                                &backendInfo[i] );
-               }
-
-               if(rc != 0) {
+                       if ( rc != 0 ) {
 #ifdef NEW_LOGGING
-                       LDAP_LOG( BACKEND, CRIT, 
-                               "backend_startup: bi_open %d failed!\n", i, 0, 0 );
+                               LDAP_LOG( BACKEND, CRIT, 
+                                       "backend_startup: bi_open %d failed!\n", i, 0, 0 );
 #else
-                       Debug( LDAP_DEBUG_ANY,
-                               "backend_startup: bi_open %d failed!\n",
-                               i, 0, 0 );
+                               Debug( LDAP_DEBUG_ANY,
+                                       "backend_startup: bi_open %d failed!\n",
+                                       i, 0, 0 );
 #endif
-                       return rc;
+                               return rc;
+                       }
                }
        }
 
@@ -320,18 +320,17 @@ int backend_startup(Backend *be)
                if ( backendDB[i].bd_info->bi_db_open ) {
                        rc = backendDB[i].bd_info->bi_db_open(
                                &backendDB[i] );
-               }
-
-               if(rc != 0) {
+                       if ( rc != 0 ) {
 #ifdef NEW_LOGGING
-                       LDAP_LOG( BACKEND, CRIT, 
-                               "backend_startup: bi_db_open(%d) failed! (%d)\n", i, rc, 0 );
+                               LDAP_LOG( BACKEND, CRIT, 
+                                       "backend_startup: bi_db_open(%d) failed! (%d)\n", i, rc, 0 );
 #else
-                       Debug( LDAP_DEBUG_ANY,
-                               "backend_startup: bi_db_open(%d) failed! (%d)\n",
-                               i, rc, 0 );
+                               Debug( LDAP_DEBUG_ANY,
+                                       "backend_startup: bi_db_open(%d) failed! (%d)\n",
+                                       i, rc, 0 );
 #endif
-                       return rc;
+                               return rc;
+                       }
                }
        }
 
@@ -537,7 +536,7 @@ select_backend(
                for ( j = 0; backends[i].be_nsuffix != NULL &&
                    backends[i].be_nsuffix[j].bv_val != NULL; j++ )
                {
-                       if (( backends[i].be_flags & SLAP_BFLAG_GLUE_SUBORDINATE )
+                       if ( ( SLAP_GLUE_SUBORDINATE( &backends[i] ) )
                                && noSubs )
                        {
                                continue;
@@ -589,7 +588,7 @@ be_issuffix(
        int     i;
 
        for ( i = 0; be->be_nsuffix != NULL && be->be_nsuffix[i].bv_val != NULL; i++ ) {
-               if ( ber_bvcmp( &be->be_nsuffix[i], bvsuffix ) == 0 ) {
+               if ( bvmatch( &be->be_nsuffix[i], bvsuffix ) ) {
                        return( 1 );
                }
        }
@@ -690,12 +689,52 @@ backend_unbind(
        Operation    *op
 )
 {
-       int     i;
+       int             i;
+#if defined( LDAP_SLAPI )
+       Slapi_PBlock *pb = op->o_pb;
+
+       int     rc;
+       slapi_x_connection_set_pb( pb, conn );
+       slapi_x_operation_set_pb( pb, op );
+#endif /* defined( LDAP_SLAPI ) */
 
        for ( i = 0; i < nbackends; i++ ) {
+#if defined( LDAP_SLAPI )
+               slapi_pblock_set( pb, SLAPI_BACKEND, (void *)&backends[i] );
+               rc = doPluginFNs( &backends[i], SLAPI_PLUGIN_PRE_UNBIND_FN,
+                               (Slapi_PBlock *)pb );
+               if ( rc != 0 ) {
+                       /*
+                        * A preoperation plugin failure will abort the
+                        * entire operation.
+                        */
+#ifdef NEW_LOGGING
+                       LDAP_LOG( OPERATION, INFO, "do_bind: Unbind preoperation plugin "
+                                       "failed\n", 0, 0, 0);
+#else
+                       Debug(LDAP_DEBUG_TRACE, "do_bind: Unbind preoperation plugin "
+                                       "failed.\n", 0, 0, 0);
+#endif
+                       return 0;
+               }
+#endif /* defined( LDAP_SLAPI ) */
+
                if ( backends[i].be_unbind ) {
                        (*backends[i].be_unbind)( &backends[i], conn, op );
                }
+
+#if defined( LDAP_SLAPI )
+               if ( doPluginFNs( &backends[i], SLAPI_PLUGIN_POST_UNBIND_FN,
+                               (Slapi_PBlock *)pb ) != 0 ) {
+#ifdef NEW_LOGGING
+                       LDAP_LOG( OPERATION, INFO, "do_unbind: Unbind postoperation plugins "
+                                       "failed\n", 0, 0, 0);
+#else
+                       Debug(LDAP_DEBUG_TRACE, "do_unbind: Unbind postoperation plugins "
+                                       "failed.\n", 0, 0, 0);
+#endif
+               }
+#endif /* defined( LDAP_SLAPI ) */
        }
 
        return 0;
@@ -740,15 +779,13 @@ backend_check_controls(
        Operation *op,
        const char **text )
 {
-       LDAPControl **ctrls;
-       ctrls = op->o_ctrls;
-       if( ctrls == NULL ) {
-               return LDAP_SUCCESS;
-       }
+       LDAPControl **ctrls = op->o_ctrls;
+
+       if( ctrls == NULL ) return LDAP_SUCCESS;
 
        for( ; *ctrls != NULL ; ctrls++ ) {
                if( (*ctrls)->ldctl_iscritical &&
-                       !charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
+                       !ldap_charray_inlist( be->be_controls, (*ctrls)->ldctl_oid ) )
                {
                        *text = "control unavailable in context";
                        return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
@@ -819,8 +856,7 @@ backend_check_restrictions(
                }
 
                {
-                       struct berval bv = BER_BVC( LDAP_EXOP_START_TLS );
-                       if( ber_bvcmp( opdata, &bv ) == 0 ) {
+                       if( bvmatch( opdata, &slap_EXOP_START_TLS ) ) {
                                session++;
                                starttls++;
                                break;
@@ -828,11 +864,18 @@ backend_check_restrictions(
                }
 
                {
-                       struct berval bv = BER_BVC( LDAP_EXOP_X_WHO_AM_I );
-                       if( ber_bvcmp( opdata, &bv ) == 0 ) {
+                       if( bvmatch( opdata, &slap_EXOP_WHOAMI ) ) {
+                               break;
+                       }
+               }
+
+#ifdef LDAP_EXOP_X_CANCEL
+               {
+                       if ( bvmatch( opdata, &slap_EXOP_CANCEL ) ) {
                                break;
                        }
                }
+#endif
 
                /* treat everything else as a modify */
                opflag = SLAP_RESTRICT_OP_MODIFY;
@@ -872,6 +915,15 @@ backend_check_restrictions(
                        return LDAP_CONFIDENTIALITY_REQUIRED;
                }
 
+
+               if( op->o_tag == LDAP_REQ_BIND && opdata == NULL ) {
+                       /* simple bind specific check */
+                       if( op->o_ssf < ssf->sss_simple_bind ) {
+                               *text = "confidentiality required";
+                               return LDAP_CONFIDENTIALITY_REQUIRED;
+                       }
+               }
+
                if( op->o_tag != LDAP_REQ_BIND || opdata == NULL ) {
                        /* these checks don't apply to SASL bind */
 
@@ -907,10 +959,20 @@ backend_check_restrictions(
                                return LDAP_CONFIDENTIALITY_REQUIRED;
                        }
 
-                       if( op->o_ndn.bv_len == 0 ) {
+                       if( !( global_allows & SLAP_ALLOW_UPDATE_ANON ) &&
+                               op->o_ndn.bv_len == 0 )
+                       {
                                *text = "modifications require authentication";
-                               return LDAP_OPERATIONS_ERROR;
+                               return LDAP_STRONG_AUTH_REQUIRED;
                        }
+
+#ifdef SLAP_X_LISTENER_MOD
+                       if ( conn->c_listener && ! ( conn->c_listener->sl_perms & S_IWUSR ) ) {
+                               /* no "w" mode means readonly */
+                               *text = "modifications not allowed on this listener";
+                               return LDAP_UNWILLING_TO_PERFORM;
+                       }
+#endif /* SLAP_X_LISTENER_MOD */
                }
        }
 
@@ -961,6 +1023,25 @@ backend_check_restrictions(
                                return LDAP_OPERATIONS_ERROR;
                        }
                }
+
+#ifdef SLAP_X_LISTENER_MOD
+               if ( !starttls && op->o_dn.bv_len == 0 ) {
+                       if ( conn->c_listener && ! ( conn->c_listener->sl_perms & S_IXUSR ) ) {
+                               /* no "x" mode means bind required */
+                               *text = "bind required on this listener";
+                               return LDAP_STRONG_AUTH_REQUIRED;
+                       }
+               }
+
+               if ( !starttls && !updateop ) {
+                       if ( conn->c_listener && ! ( conn->c_listener->sl_perms & S_IRUSR ) ) {
+                               /* no "r" mode means no read */
+                               *text = "read not allowed on this listener";
+                               return LDAP_UNWILLING_TO_PERFORM;
+                       }
+               }
+#endif /* SLAP_X_LISTENER_MOD */
+
        }
 
        if( restrictops & opflag ) {
@@ -1026,6 +1107,7 @@ backend_group(
        } 
 
        ldap_pvt_thread_mutex_lock( &conn->c_mutex );
+
        for (g = conn->c_groups; g; g=g->ga_next) {
                if (g->ga_be != be || g->ga_oc != group_oc ||
                        g->ga_at != group_at || g->ga_len != gr_ndn->bv_len)
@@ -1033,7 +1115,9 @@ backend_group(
                if (strcmp( g->ga_ndn, gr_ndn->bv_val ) == 0)
                        break;
        }
+
        ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
+
        if (g) {
                return g->ga_res;
        }
@@ -1043,7 +1127,7 @@ backend_group(
                        target, gr_ndn, op_ndn,
                        group_oc, group_at );
                
-               if (op->o_tag != LDAP_REQ_BIND) {
+               if ( op->o_tag != LDAP_REQ_BIND && !op->o_do_not_cache ) {
                        g = ch_malloc(sizeof(GroupAssertion) + gr_ndn->bv_len);
                        g->ga_be = be;
                        g->ga_oc = group_oc;
@@ -1102,16 +1186,17 @@ Attribute *backend_operational(
 {
        Attribute *a = NULL, **ap = &a;
 
-#ifdef SLAPD_SCHEMA_DN
-       *ap = slap_operational_subschemaSubentry();
-       ap = &(*ap)->a_next;
-#endif
-
        /*
         * If operational attributes (allegedly) are required, 
         * and the backend supports specific operational attributes, 
         * add them to the attribute list
         */
+       if ( opattrs || ( attrs &&
+               ad_inlist( slap_schema.si_ad_subschemaSubentry, attrs )) ) {
+               *ap = slap_operational_subschemaSubentry( be );
+               ap = &(*ap)->a_next;
+       }
+
        if ( ( opattrs || attrs ) && be && be->be_operational != NULL ) {
                ( void )be->be_operational( be, conn, op, e, attrs, opattrs, ap );
        }