]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-meta/init.c
fix previous commit (ITS#4861)
[openldap] / servers / slapd / back-meta / init.c
index 07612765c77c2c0d843ca61924dc30ec883b7430..6c8c972f6d98ce4981630458ed31414fd52ebe3b 100644 (file)
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- *
- * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
- *
- * This work has been developed to fulfill the requirements
- * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
- * to the OpenLDAP Foundation in the hope that it may be useful
- * to the Open Source community, but WITHOUT ANY WARRANTY.
- *
- * Permission is granted to anyone to use this software for any purpose
- * on any computer system, and to alter it and redistribute it, subject
- * to the following restrictions:
- *
- * 1. The author and SysNet s.n.c. are not responsible for the consequences
- *    of use of this software, no matter how awful, even if they arise from 
- *    flaws in it.
- *
- * 2. The origin of this software must not be misrepresented, either by
- *    explicit claim or by omission.  Since few users ever read sources,
- *    credits should appear in the documentation.
- *
- * 3. Altered versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.  Since few users
- *    ever read sources, credits should appear in the documentation.
- *    SysNet s.n.c. cannot be responsible for the consequences of the
- *    alterations.
- *
- * 4. This notice may not be removed or altered.
- *
- *
- * This software is based on the backend back-ldap, implemented
- * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
- * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
- * contributors. The contribution of the original software to the present
- * implementation is acknowledged in this copyright statement.
- *
- * A special acknowledgement goes to Howard for the overall architecture
- * (and for borrowing large pieces of code), and to Mark, who implemented
- * from scratch the attribute/objectclass mapping.
- *
- * The original copyright statement follows.
- *
- * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Permission is granted to anyone to use this software for any purpose
- * on any computer system, and to alter it and redistribute it, subject
- * to the following restrictions:
+ * Copyright 1999-2007 The OpenLDAP Foundation.
+ * Portions Copyright 2001-2003 Pierangelo Masarati.
+ * Portions Copyright 1999-2003 Howard Chu.
+ * All rights reserved.
  *
- * 1. The author is not responsible for the consequences of use of this
- *    software, no matter how awful, even if they arise from flaws in it.
- *
- * 2. The origin of this software must not be misrepresented, either by
- *    explicit claim or by omission.  Since few users ever read sources,
- *    credits should appear in the documentation.
- *
- * 3. Altered versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.  Since few users
- *    ever read sources, credits should appear in the
- *    documentation.
- *
- * 4. This notice may not be removed or altered.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
  *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
 
 #include <stdio.h>
 
+#include <ac/string.h>
 #include <ac/socket.h>
 
 #include "slap.h"
 #include "../back-ldap/back-ldap.h"
 #include "back-meta.h"
 
-#ifdef SLAPD_META_DYNAMIC
-
 int
-back_meta_LTX_init_module( int argc, char *argv[] ) {
-    BackendInfo bi;
-
-    memset( &bi, '\0', sizeof( bi ) );
-    bi.bi_type = "meta";
-    bi.bi_init = meta_back_initialize;
+meta_back_open(
+       BackendInfo     *bi )
+{
+       /* FIXME: need to remove the pagedResults, and likely more... */
+       bi->bi_controls = slap_known_controls;
 
-    backend_add( &bi );
-    return 0;
+       return 0;
 }
 
-#endif /* SLAPD_META_DYNAMIC */
-
 int
 meta_back_initialize(
-               BackendInfo     *bi
-)
+       BackendInfo     *bi )
 {
-       bi->bi_controls = slap_known_controls;
-
-       bi->bi_open = 0;
+       bi->bi_open = meta_back_open;
        bi->bi_config = 0;
        bi->bi_close = 0;
        bi->bi_destroy = 0;
 
        bi->bi_db_init = meta_back_db_init;
        bi->bi_db_config = meta_back_db_config;
-       bi->bi_db_open = 0;
+       bi->bi_db_open = meta_back_db_open;
        bi->bi_db_close = 0;
        bi->bi_db_destroy = meta_back_db_destroy;
 
@@ -130,187 +73,318 @@ meta_back_initialize(
 
 int
 meta_back_db_init(
-               Backend *be
-)
+       Backend         *be )
 {
-       struct metainfo *li;
-
-#ifdef LDAP_CACHING
-       struct rewrite_info     *rwinfo;
-       cache_manager           *cm;
-       query_manager           *qm;
-
-       rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
-       if ( rwinfo == NULL ) {
-               return -1;
-       }
-               
-       cm = (cache_manager *)ch_malloc(sizeof(cache_manager)); 
-       if ( cm == NULL ) {
-               rewrite_info_delete( rwinfo );
-               return -1;
-       }
+       metainfo_t      *mi;
+       int             i;
 
-       qm = (query_manager*)ch_malloc(sizeof(query_manager)); 
-       if ( qm == NULL ) {
-               rewrite_info_delete( rwinfo );
-               ch_free( cm );
-               return -1;
-       }
-
-        cm->caching = 0; 
-        cm->qm = qm; 
-       cm->numattrsets = 0; 
-       cm->numtemplates = 0;   
-        cm->num_entries_limit = 5;
-       cm->cache_size = 0;
-       cm->thresh_hi = 500000;
-       cm->thresh_lo = 700000;
-       cm->num_cached_queries = 0; 
-       cm->total_entries = 0; 
-       cm->max_queries = 10000; 
-       cm->threads = 0; 
-       cm->consistency_time = slap_get_time(); 
-       cm->consistency_cycle_time = 1000; 
-       
-       qm->attr_sets = NULL; 
-       qm->templates = NULL; 
-       qm->lru_top = NULL;
-       qm->lru_bottom = NULL;
-
-       qm->qcfunc = query_containment; 
-       qm->crfunc = cache_replacement; 
-       qm->addfunc = add_query; 
-        ldap_pvt_thread_mutex_init(&qm->lru_mutex); 
-        
-        ldap_pvt_thread_mutex_init(&cm->cache_mutex); 
-        ldap_pvt_thread_mutex_init(&cm->remove_mutex); 
-       ldap_pvt_thread_mutex_init( &cm->consistency_mutex );
-#endif /* LDAP_CACHING */
-
-       li = ch_calloc( 1, sizeof( struct metainfo ) );
-       if ( li == NULL ) {
+       mi = ch_calloc( 1, sizeof( metainfo_t ) );
+       if ( mi == NULL ) {
                return -1;
        }
-       
+
        /*
         * At present the default is no default target;
         * this may change
         */
-       li->defaulttarget = META_DEFAULT_TARGET_NONE;
-#ifdef LDAP_CACHING
-       li->cm = cm; 
-       li->rwinfo = rwinfo;
-       /* FIXME: what about qm ? */
-#endif /* LDAP_CACHING */
+       mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
+       mi->mi_bind_timeout.tv_sec = 0;
+       mi->mi_bind_timeout.tv_usec = META_BIND_TIMEOUT;
 
-       ldap_pvt_thread_mutex_init( &li->conn_mutex );
-       ldap_pvt_thread_mutex_init( &li->cache.mutex );
-       be->be_private = li;
+       mi->mi_rebind_f = meta_back_default_rebind;
+
+       ldap_pvt_thread_mutex_init( &mi->mi_conninfo.lai_mutex );
+       ldap_pvt_thread_mutex_init( &mi->mi_cache.mutex );
+
+       /* safe default */
+       mi->mi_nretries = META_RETRY_DEFAULT;
+       mi->mi_version = LDAP_VERSION3;
+
+       for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
+               mi->mi_conn_priv[ i ].mic_num = 0;
+               LDAP_TAILQ_INIT( &mi->mi_conn_priv[ i ].mic_priv );
+       }
+       mi->mi_conn_priv_max = LDAP_BACK_CONN_PRIV_DEFAULT;
+       
+       be->be_private = mi;
 
        return 0;
 }
 
-static void
-conn_free( 
-       void *v_lc
-)
+int
+meta_back_db_open(
+       Backend         *be )
 {
-       struct metaconn *lc = v_lc;
-       struct metasingleconn *lsc;
+       metainfo_t      *mi = (metainfo_t *)be->be_private;
+
+       int             i,
+                       not_always = 0,
+                       not_always_anon_proxyauthz = 0,
+                       not_always_anon_non_prescriptive = 0,
+                       rc;
+
+       if ( mi->mi_ntargets == 0 ) {
+               Debug( LDAP_DEBUG_ANY,
+                       "meta_back_db_open: no targets defined\n",
+                       0, 0, 0 );
+               return 1;
+       }
 
-       for ( lsc = lc->conns; !META_LAST(lsc); lsc++ ) {
-               if ( lsc->ld != NULL ) {
-                       ldap_unbind( lsc->ld );
+       for ( i = 0; i < mi->mi_ntargets; i++ ) {
+               metatarget_t    *mt = mi->mi_targets[ i ];
+
+               if ( META_BACK_TGT_T_F_DISCOVER( mt ) ) {
+                       rc = slap_discover_feature( mt->mt_uri, mt->mt_version,
+                                       slap_schema.si_ad_supportedFeatures->ad_cname.bv_val,
+                                       LDAP_FEATURE_ABSOLUTE_FILTERS );
+                       if ( rc == LDAP_COMPARE_TRUE ) {
+                               mt->mt_flags |= LDAP_BACK_F_T_F;
+                       }
                }
-               if ( lsc->bound_dn.bv_val ) {
-                       ber_memfree( lsc->bound_dn.bv_val );
+
+               if ( META_BACK_TGT_CANCEL_DISCOVER( mt ) ) {
+                       rc = slap_discover_feature( mt->mt_uri, mt->mt_version,
+                                       slap_schema.si_ad_supportedExtension->ad_cname.bv_val,
+                                       LDAP_EXOP_CANCEL );
+                       if ( rc == LDAP_COMPARE_TRUE ) {
+                               mt->mt_flags |= LDAP_BACK_F_CANCEL_EXOP;
+                       }
+               }
+
+               if ( not_always == 0 ) {
+                       if ( !( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE )
+                               || mt->mt_idassert_authz != NULL )
+                       {
+                               not_always = 1;
+                       }
+               }
+
+               if ( ( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL )
+                       && !( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) )
+               {
+                       Debug( LDAP_DEBUG_ANY, "meta_back_db_open(%s): "
+                               "target #%d inconsistent idassert configuration "
+                               "(likely authz=\"*\" used with \"non-prescriptive\" flag)\n",
+                               be->be_suffix[ 0 ].bv_val, i, 0 );
+                       return 1;
+               }
+
+               if ( not_always_anon_proxyauthz == 0 ) {
+                       if ( !( mt->mt_idassert_flags & LDAP_BACK_AUTH_AUTHZ_ALL ) )
+                       {
+                               not_always_anon_proxyauthz = 1;
+                       }
+               }
+
+               if ( not_always_anon_non_prescriptive == 0 ) {
+                       if ( ( mt->mt_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) )
+                       {
+                               not_always_anon_non_prescriptive = 1;
+                       }
                }
        }
-       free( lc->conns );
-       free( lc );
+
+       if ( not_always == 0 ) {
+               mi->mi_flags |= META_BACK_F_PROXYAUTHZ_ALWAYS;
+       }
+
+       if ( not_always_anon_proxyauthz == 0 ) {
+               mi->mi_flags |= META_BACK_F_PROXYAUTHZ_ANON;
+
+       } else if ( not_always_anon_non_prescriptive == 0 ) {
+               mi->mi_flags |= META_BACK_F_PROXYAUTHZ_NOANON;
+       }
+
+       return 0;
+}
+
+/*
+ * meta_back_conn_free()
+ *
+ * actually frees a connection; the reference count must be 0,
+ * and it must not (or no longer) be in the cache.
+ */
+void
+meta_back_conn_free( 
+       void            *v_mc )
+{
+       metaconn_t              *mc = v_mc;
+       int                     ntargets;
+
+       assert( mc != NULL );
+       assert( mc->mc_refcnt == 0 );
+
+       /* at least one must be present... */
+       ntargets = mc->mc_info->mi_ntargets;
+       assert( ntargets > 0 );
+
+       for ( ; ntargets--; ) {
+               (void)meta_clear_one_candidate( NULL, mc, ntargets );
+       }
+
+       if ( !BER_BVISNULL( &mc->mc_local_ndn ) ) {
+               free( mc->mc_local_ndn.bv_val );
+       }
+
+       free( mc );
+}
+
+static void
+mapping_free(
+       void            *v_mapping )
+{
+       struct ldapmapping *mapping = v_mapping;
+       ch_free( mapping->src.bv_val );
+       ch_free( mapping->dst.bv_val );
+       ch_free( mapping );
+}
+
+static void
+mapping_dst_free(
+       void            *v_mapping )
+{
+       struct ldapmapping *mapping = v_mapping;
+
+       if ( BER_BVISEMPTY( &mapping->dst ) ) {
+               mapping_free( &mapping[ -1 ] );
+       }
 }
 
 static void
 target_free(
-               struct metatarget *lt
-)
+       metatarget_t    *mt )
 {
-       if ( lt->uri ) {
-               free( lt->uri );
+       if ( mt->mt_uri ) {
+               free( mt->mt_uri );
+               ldap_pvt_thread_mutex_destroy( &mt->mt_uri_mutex );
+       }
+       if ( mt->mt_subtree_exclude ) {
+               ber_bvarray_free( mt->mt_subtree_exclude );
+       }
+       if ( !BER_BVISNULL( &mt->mt_psuffix ) ) {
+               free( mt->mt_psuffix.bv_val );
+       }
+       if ( !BER_BVISNULL( &mt->mt_nsuffix ) ) {
+               free( mt->mt_nsuffix.bv_val );
+       }
+       if ( !BER_BVISNULL( &mt->mt_binddn ) ) {
+               free( mt->mt_binddn.bv_val );
        }
-       if ( lt->psuffix.bv_val ) {
-               free( lt->psuffix.bv_val );
+       if ( !BER_BVISNULL( &mt->mt_bindpw ) ) {
+               free( mt->mt_bindpw.bv_val );
        }
-       if ( lt->suffix.bv_val ) {
-               free( lt->suffix.bv_val );
+       if ( !BER_BVISNULL( &mt->mt_idassert_authcID ) ) {
+               ch_free( mt->mt_idassert_authcID.bv_val );
        }
-       if ( lt->binddn.bv_val ) {
-               free( lt->binddn.bv_val );
+       if ( !BER_BVISNULL( &mt->mt_idassert_authcDN ) ) {
+               ch_free( mt->mt_idassert_authcDN.bv_val );
        }
-       if ( lt->bindpw.bv_val ) {
-               free( lt->bindpw.bv_val );
+       if ( !BER_BVISNULL( &mt->mt_idassert_passwd ) ) {
+               ch_free( mt->mt_idassert_passwd.bv_val );
        }
-       if ( lt->pseudorootdn.bv_val ) {
-               free( lt->pseudorootdn.bv_val );
+       if ( !BER_BVISNULL( &mt->mt_idassert_authzID ) ) {
+               ch_free( mt->mt_idassert_authzID.bv_val );
        }
-       if ( lt->pseudorootpw.bv_val ) {
-               free( lt->pseudorootpw.bv_val );
+       if ( !BER_BVISNULL( &mt->mt_idassert_sasl_mech ) ) {
+               ch_free( mt->mt_idassert_sasl_mech.bv_val );
        }
-       if ( lt->rwinfo ) {
-               rewrite_info_delete( lt->rwinfo );
+       if ( !BER_BVISNULL( &mt->mt_idassert_sasl_realm ) ) {
+               ch_free( mt->mt_idassert_sasl_realm.bv_val );
        }
-       avl_free( lt->oc_map.remap, NULL );
-       avl_free( lt->oc_map.map, mapping_free );
-       avl_free( lt->at_map.remap, NULL );
-       avl_free( lt->at_map.map, mapping_free );
+       if ( mt->mt_idassert_authz != NULL ) {
+               ber_bvarray_free( mt->mt_idassert_authz );
+       }
+       if ( mt->mt_rwmap.rwm_rw ) {
+               rewrite_info_delete( &mt->mt_rwmap.rwm_rw );
+       }
+       avl_free( mt->mt_rwmap.rwm_oc.remap, mapping_dst_free );
+       avl_free( mt->mt_rwmap.rwm_oc.map, mapping_free );
+       avl_free( mt->mt_rwmap.rwm_at.remap, mapping_dst_free );
+       avl_free( mt->mt_rwmap.rwm_at.map, mapping_free );
+
+       free( mt );
 }
 
 int
 meta_back_db_destroy(
-    Backend    *be
-)
+       Backend         *be )
 {
-       struct metainfo *li;
+       metainfo_t      *mi;
 
        if ( be->be_private ) {
                int i;
 
-               li = ( struct metainfo * )be->be_private;
+               mi = ( metainfo_t * )be->be_private;
 
                /*
                 * Destroy the connection tree
                 */
-               ldap_pvt_thread_mutex_lock( &li->conn_mutex );
+               ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
 
-               if ( li->conntree ) {
-                       avl_free( li->conntree, conn_free );
+               if ( mi->mi_conninfo.lai_tree ) {
+                       avl_free( mi->mi_conninfo.lai_tree, meta_back_conn_free );
+               }
+               for ( i = LDAP_BACK_PCONN_FIRST; i < LDAP_BACK_PCONN_LAST; i++ ) {
+                       while ( !LDAP_TAILQ_EMPTY( &mi->mi_conn_priv[ i ].mic_priv ) ) {
+                               metaconn_t      *mc = LDAP_TAILQ_FIRST( &mi->mi_conn_priv[ i ].mic_priv );
+
+                               LDAP_TAILQ_REMOVE( &mi->mi_conn_priv[ i ].mic_priv, mc, mc_q );
+                               meta_back_conn_free( mc );
+                       }
                }
 
                /*
                 * Destroy the per-target stuff (assuming there's at
                 * least one ...)
                 */
-               for ( i = 0; i < li->ntargets; i++ ) {
-                       target_free( li->targets[ i ] );
-                       free( li->targets[ i ] );
-               }
+               if ( mi->mi_targets != NULL ) {
+                       for ( i = 0; i < mi->mi_ntargets; i++ ) {
+                               metatarget_t    *mt = mi->mi_targets[ i ];
 
-               free( li->targets );
+                               if ( META_BACK_TGT_QUARANTINE( mt ) ) {
+                                       if ( mt->mt_quarantine.ri_num != mi->mi_quarantine.ri_num )
+                                       {
+                                               slap_retry_info_destroy( &mt->mt_quarantine );
+                                       }
 
-               ldap_pvt_thread_mutex_lock( &li->cache.mutex );
-               if ( li->cache.tree ) {
-                       avl_free( li->cache.tree, meta_dncache_free );
+                                       ldap_pvt_thread_mutex_destroy( &mt->mt_quarantine_mutex );
+                               }
+
+                               target_free( mt );
+                       }
+
+                       free( mi->mi_targets );
+               }
+
+               ldap_pvt_thread_mutex_lock( &mi->mi_cache.mutex );
+               if ( mi->mi_cache.tree ) {
+                       avl_free( mi->mi_cache.tree, meta_dncache_free );
                }
                
-               ldap_pvt_thread_mutex_unlock( &li->cache.mutex );
-               ldap_pvt_thread_mutex_destroy( &li->cache.mutex );
+               ldap_pvt_thread_mutex_unlock( &mi->mi_cache.mutex );
+               ldap_pvt_thread_mutex_destroy( &mi->mi_cache.mutex );
+
+               ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
+               ldap_pvt_thread_mutex_destroy( &mi->mi_conninfo.lai_mutex );
+
+               if ( mi->mi_candidates != NULL ) {
+                       ber_memfree_x( mi->mi_candidates, NULL );
+               }
 
-               ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
-               ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
+               if ( META_BACK_QUARANTINE( mi ) ) {
+                       slap_retry_info_destroy( &mi->mi_quarantine );
+               }
        }
 
        free( be->be_private );
        return 0;
 }
 
+#if SLAPD_META == SLAPD_MOD_DYNAMIC
+
+/* conditionally define the init_module() function */
+SLAP_BACKEND_INIT_MODULE( meta )
+
+#endif /* SLAPD_META == SLAPD_MOD_DYNAMIC */
+
+