You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2005/10/28 21:36:05 UTC

svn commit: r329274 - /directory/shared/protocol/trunk/common/src/main/java/org/apache/protocol/common/store/LdifFileLoader.java

Author: erodriguez
Date: Fri Oct 28 12:36:01 2005
New Revision: 329274

URL: http://svn.apache.org/viewcvs?rev=329274&view=rev
Log:
Moved the Kerberos-aware LDIF loader to protocol-common.

Added:
    directory/shared/protocol/trunk/common/src/main/java/org/apache/protocol/common/store/LdifFileLoader.java   (with props)

Added: directory/shared/protocol/trunk/common/src/main/java/org/apache/protocol/common/store/LdifFileLoader.java
URL: http://svn.apache.org/viewcvs/directory/shared/protocol/trunk/common/src/main/java/org/apache/protocol/common/store/LdifFileLoader.java?rev=329274&view=auto
==============================================================================
--- directory/shared/protocol/trunk/common/src/main/java/org/apache/protocol/common/store/LdifFileLoader.java (added)
+++ directory/shared/protocol/trunk/common/src/main/java/org/apache/protocol/common/store/LdifFileLoader.java Fri Oct 28 12:36:01 2005
@@ -0,0 +1,256 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.protocol.common.store;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import javax.naming.CompoundName;
+import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
+import javax.security.auth.kerberos.KerberosKey;
+import javax.security.auth.kerberos.KerberosPrincipal;
+
+import org.apache.kerberos.store.KerberosAttribute;
+import org.apache.ldap.common.ldif.LdifIterator;
+import org.apache.ldap.common.ldif.LdifParser;
+import org.apache.ldap.common.ldif.LdifParserImpl;
+import org.apache.ldap.common.message.LockableAttributesImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Support for commands to load an LDIF file that contains Kerberos principals into a DirContext.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class LdifFileLoader
+{
+    /** the log for this class */
+    private static final Logger log = LoggerFactory.getLogger( LdifFileLoader.class );
+
+    /** a handle on the top initial context: get new context from this */
+    protected DirContext ctx;
+
+    protected String ldifPath;
+
+    /**
+     * Creates the LDIF file loader command.
+     *
+     * @param ctx the context to load the entries into.
+     * @param ldifPath the path to the file of LDIF entries.
+     */
+    public LdifFileLoader( DirContext ctx, String ldifPath )
+    {
+        this.ctx = ctx;
+        this.ldifPath = ldifPath;
+    }
+
+    /**
+     * Opens the LDIF file and loads the entries into the context.
+     */
+    public void execute()
+    {
+        Name rdn = null;
+
+        try
+        {
+            InputStream in = getLdifStream();
+
+            LdifIterator iterator = new LdifIterator( in );
+
+            LdifParser ldifParser = new LdifParserImpl();
+
+            while ( iterator.hasNext() )
+            {
+                String ldif = (String) iterator.next();
+
+                Attributes attributes = new LockableAttributesImpl();
+
+                ldifParser.parse( attributes, ldif );
+
+                String dn = (String) attributes.remove( "dn" ).get();
+
+                if ( attributes.get( "objectClass" ).contains( "krb5KDCEntry" ) )
+                {
+                    String pw = (String) attributes.get( "userpassword" ).get();
+
+                    String krbPrincipal = (String) attributes.get( KerberosAttribute.PRINCIPAL ).get();
+
+                    KerberosPrincipal principal = new KerberosPrincipal( krbPrincipal );
+
+                    KerberosKey key = new KerberosKey( principal, pw.toCharArray(), "DES" );
+
+                    byte[] encodedKey = key.getEncoded();
+
+                    attributes.put( KerberosAttribute.KEY, encodedKey );
+                    attributes.put( KerberosAttribute.VERSION, Integer.toString( key.getVersionNumber() ) );
+                    attributes.put( KerberosAttribute.TYPE, Integer.toString( key.getKeyType() ) );
+                }
+
+                rdn = getRelativeName( ctx, dn );
+
+                try
+                {
+                    ctx.lookup( rdn );
+
+                    log.info( "Found " + rdn + ", will not create." );
+                }
+                catch ( Exception e )
+                {
+                    ctx.createSubcontext( rdn, attributes );
+
+                    log.info( "Created " + rdn + "." );
+                }
+            }
+        }
+        catch ( FileNotFoundException fnfe )
+        {
+            log.error( "LDIF file does not exist." );
+            return;
+        }
+        catch ( IOException ioe )
+        {
+            log.error( "Failed to import LDIF into backing store.", ioe );
+            return;
+        }
+        catch ( NamingException ne )
+        {
+            log.error( "Failed to import LDIF into backing store.", ne );
+            return;
+        }
+
+        try
+        {
+            InputStream in = getLdifStream();
+
+            LdifIterator iterator = new LdifIterator( in );
+
+            LdifParser ldifParser = new LdifParserImpl();
+
+            while ( iterator.hasNext() )
+            {
+                String ldif = (String) iterator.next();
+
+                Attributes attributes = new LockableAttributesImpl();
+
+                ldifParser.parse( attributes, ldif );
+
+                String dn = (String) attributes.remove( "dn" ).get();
+
+                rdn = getRelativeName( ctx, dn );
+
+                Object stored = ctx.lookup( rdn );
+
+                log.debug( "Lookup for " + rdn + " returned " + stored + "." );
+
+                if ( stored == null )
+                {
+                    log.error( rdn + " was null." );
+
+                    throw new IllegalStateException( "LDIF entries not being pushed to disk." );
+                }
+            }
+        }
+        catch ( Exception e )
+        {
+            log.error( "Failed to find " + rdn );
+
+            if ( log.isDebugEnabled() )
+            {
+                log.error( "Failed to import LDIF into backing store.", e );
+            }
+            else
+            {
+                log.error( "Failed to import LDIF into backing store." );
+            }
+
+            return;
+        }
+    }
+
+    private Name getRelativeName( DirContext ctx, String baseDn ) throws NamingException
+    {
+        Properties props = new Properties();
+        props.setProperty( "jndi.syntax.direction", "right_to_left" );
+        props.setProperty( "jndi.syntax.separator", "," );
+        props.setProperty( "jndi.syntax.ignorecase", "true" );
+        props.setProperty( "jndi.syntax.trimblanks", "true" );
+
+        Name searchBaseDn = null;
+
+        try
+        {
+            Name ctxRoot = new CompoundName( ctx.getNameInNamespace(), props );
+            searchBaseDn = new CompoundName( baseDn, props );
+
+            if ( !searchBaseDn.startsWith( ctxRoot ) )
+            {
+                throw new NamingException( "Invalid search base " + baseDn );
+            }
+
+            for ( int ii = 0; ii < ctxRoot.size(); ii++ )
+            {
+                searchBaseDn.remove( 0 );
+            }
+        }
+        catch ( NamingException e )
+        {
+            throw new NamingException( "Failed to initialize search base " + baseDn );
+        }
+
+        return searchBaseDn;
+    }
+
+    /**
+     * Tries to find an LDIF file either on the file system or packaged within a jar.
+     *
+     * @return the input stream to the ldif file.
+     * @throws FileNotFoundException if the file cannot be found.
+     */
+    private InputStream getLdifStream() throws FileNotFoundException
+    {
+        File file = new File( ldifPath );
+
+        InputStream in = null;
+
+        if ( file.exists() )
+        {
+            in = new FileInputStream( file );
+        }
+        else
+        {
+            // if file not on system see if something is bundled with the jar ...
+            in = getClass().getResourceAsStream( ldifPath );
+
+            if ( in == null )
+            {
+                throw new FileNotFoundException( "LDIF file does not exist." );
+            }
+        }
+
+        return in;
+    }
+}

Propchange: directory/shared/protocol/trunk/common/src/main/java/org/apache/protocol/common/store/LdifFileLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native