TremoloSecurity / MyVirtualDirectory

Open Source LDAP Virtual Directory
Apache License 2.0
45 stars 18 forks source link

Alias #23

Open Patrock opened 7 years ago

Patrock commented 7 years ago

i have a hierarchy of users in different OUs. one application i use (snipeit) needs the users in a flat hierarchy:

i have to enter a baseDN in the config. this baseDN is appended to my user (cn=userXYZ) to make a bind.

Are there any possibilities to create alias entries for my users, e.g. by a join or similar and add objectclass: alias and aliasedObjectName attributes?

What should be done to write an insert for this , perhaps i'll give it a chance ?

Would an insert on a rootObject with the target DN work ? The insert could then search other DNs for entries and adds objectclass: alias and aliasedObjectName attributes based on a mapping parameter ?

Patrock commented 7 years ago

@mlbiam i have managed to code an Insert which rewrites incoming DNs to multiple targetDNs. Now i can map multiple subtrees to a common DN...

Here is the code, perhaps you can tell me if thats the proper way. If you find it useful, feel free to use it ;)

Config: server.globalChain=rewrite server.globalChain.rewrite.className=net.sourceforge.myvd.inserts.mapping.RewriteDN server.globalChain.rewrite.config.targetBases=ou=test,dc=lan|ou=nextTest,dc=lan|ou=test234,dc=lan server.globalChain.rewrite.config.sourceBase=dc=alias

/*
 * Copyright 2017 Patrick Pogscheba 
 * 
 * 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 net.sourceforge.myvd.inserts.mapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.jar.Attributes.Name;

import com.novell.ldap.LDAPConstraints;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPModification;
import com.novell.ldap.LDAPSearchConstraints;
import com.novell.ldap.util.DN;

import net.sourceforge.myvd.chain.AddInterceptorChain;
import net.sourceforge.myvd.chain.BindInterceptorChain;
import net.sourceforge.myvd.chain.CompareInterceptorChain;
import net.sourceforge.myvd.chain.DeleteInterceptorChain;
import net.sourceforge.myvd.chain.ExetendedOperationInterceptorChain;
import net.sourceforge.myvd.chain.ModifyInterceptorChain;
import net.sourceforge.myvd.chain.PostSearchCompleteInterceptorChain;
import net.sourceforge.myvd.chain.PostSearchEntryInterceptorChain;
import net.sourceforge.myvd.chain.RenameInterceptorChain;
import net.sourceforge.myvd.chain.SearchInterceptorChain;
import net.sourceforge.myvd.core.NameSpace;
import net.sourceforge.myvd.inserts.Insert;
import net.sourceforge.myvd.router.Level;
import net.sourceforge.myvd.types.Attribute;
import net.sourceforge.myvd.types.Bool;
import net.sourceforge.myvd.types.DistinguishedName;
import net.sourceforge.myvd.types.Entry;
import net.sourceforge.myvd.types.ExtendedOperation;
import net.sourceforge.myvd.types.Filter;
import net.sourceforge.myvd.types.Int;
import net.sourceforge.myvd.types.Password;
import net.sourceforge.myvd.types.RequestVariables;
import net.sourceforge.myvd.types.Results;
import net.sourceforge.myvd.util.NamingUtils;

public class RewriteDN implements Insert {

    HashMap<DN,String[]> targetBases = new HashMap<>();

    private DN sourceBase;
    NamingUtils utils;
    String[] explodedSourceBase;

    String name;
    public void add(AddInterceptorChain chain, Entry entry,
            LDAPConstraints constraints) throws LDAPException {
        // TODO Auto-generated method stub
        chain.nextAdd(entry, constraints);
    }

    public void bind(BindInterceptorChain chain, DistinguishedName dn,
            Password pwd, LDAPConstraints constraints) throws LDAPException {
        // TODO Auto-generated method stub
        System.out.println("AliasInterceptor.bind : " + chain.getPos() + ", " + dn.toString());
        if(!dn.getDN().isDescendantOf(this.sourceBase))
        {
            chain.nextBind(dn, pwd, constraints);
        }
        else
        {
            int exceptions=0;
            for (HashMap.Entry<DN, String[]> entry : targetBases.entrySet()) {
                DistinguishedName newDN = new DistinguishedName(
                        utils.getRemoteMappedDN(dn.getDN(),explodedSourceBase,entry.getValue()));

                chain.setBindDN(newDN);
                try {
                    System.out.println("AliasInterceptor.bind : nextBind " + newDN.toString());
                    chain.nextBind(newDN, pwd, constraints);
                    System.out.println("AliasInterceptor.bind : nextBindFinished" + newDN.toString());
                    break;
                } 
                catch(LDAPException e)
                {
                    e.printStackTrace();
                    System.out.println("AliasInterceptor.bind : exception" + newDN.toString());
                    exceptions++;
                }
                finally
                {
                    System.out.println("AliasInterceptor.bind : finally" + newDN.toString());       
                }
            }

            if(exceptions== targetBases.size())
            {
                throw new LDAPException("Could not resolve or bind with rewritten DN", LDAPException.INVALID_DN_SYNTAX, "");
            }
        }

    }

    public void compare(CompareInterceptorChain chain, DistinguishedName dn,
            Attribute attrib, LDAPConstraints constraints) throws LDAPException {
        // TODO Auto-generated method stub
        chain.nextCompare(dn, attrib, constraints);
    }

    public void configure(String name, Properties props, NameSpace nameSpace)
            throws LDAPException {  
        this.name = name;   
        this.sourceBase = new DN(props.getProperty("sourceBase"));

        this.explodedSourceBase = this.sourceBase.explodeDN(false);
        this.utils = new NamingUtils();

        StringTokenizer toker;
        toker = new StringTokenizer(props.getProperty("targetBases"),"|");
        while (toker.hasMoreTokens()) {
            DN dn = new DN(toker.nextToken());      
            targetBases.put(dn, dn.explodeDN(false) );
        }   
    }

    public void delete(DeleteInterceptorChain chain, DistinguishedName dn,
            LDAPConstraints constraints) throws LDAPException {
        // TODO Auto-generated method stub
         chain.nextDelete(dn, constraints);
    }

    public void extendedOperation(ExetendedOperationInterceptorChain chain,
            ExtendedOperation op, LDAPConstraints constraints)
            throws LDAPException {
        // TODO Auto-generated method stub
        chain.nextExtendedOperations(op, constraints);
    }

    public void modify(ModifyInterceptorChain chain, DistinguishedName dn,
            ArrayList<LDAPModification> mods, LDAPConstraints constraints)
            throws LDAPException {
        // TODO Auto-generated method stub
        chain.nextModify(dn, mods, constraints);
    }

    public void postSearchComplete(PostSearchCompleteInterceptorChain chain,
            DistinguishedName base, Int scope, Filter filter,
            ArrayList<Attribute> attributes, Bool typesOnly,
            LDAPSearchConstraints constraints) throws LDAPException {
        // TODO Auto-generated method stub
        //System.out.println("AliasInterceptor.postSearchEntry : " + chain.getPos() + ", " + base.toString());
        chain.nextPostSearchComplete(base, scope, filter, attributes, typesOnly, constraints);

    }

    public void postSearchEntry(PostSearchEntryInterceptorChain chain,
            Entry entry, DistinguishedName base, Int scope, Filter filter,
            ArrayList<Attribute> attributes, Bool typesOnly,
            LDAPSearchConstraints constraints) throws LDAPException {
        // TODO Auto-generated method stub
        //System.out.println("AliasInterceptor.postSearchEntry : " + chain.getPos() + ", " + base.toString());
        chain.nextPostSearchEntry(entry, base, scope, filter, attributes, typesOnly, constraints);
    }

    public void rename(RenameInterceptorChain chain, DistinguishedName dn,
            DistinguishedName newRdn, Bool deleteOldRdn,
            LDAPConstraints constraints) throws LDAPException {
        // TODO Auto-generated method stub
        chain.nextRename(dn, newRdn, deleteOldRdn, constraints);
    }

    public void rename(RenameInterceptorChain chain, DistinguishedName dn,
            DistinguishedName newRdn, DistinguishedName newParentDN,
            Bool deleteOldRdn, LDAPConstraints constraints)
            throws LDAPException {
        // TODO Auto-generated method stub

        chain.nextRename(dn, newRdn, newParentDN, deleteOldRdn, constraints);
    }

    public void search(SearchInterceptorChain chain, DistinguishedName base,
            Int scope, Filter filter, ArrayList<Attribute> attributes,
            Bool typesOnly, Results results, LDAPSearchConstraints constraints)
            throws LDAPException {
        // TODO Auto-generated method stub
        System.out.println("AliasInterceptor.search : " + chain.getPos() + ", " + base.toString());
        if(!base.getDN().isDescendantOf(this.sourceBase))
        {
            chain.nextSearch(base, scope, filter, attributes, typesOnly, results, constraints);
        }
        else
        {
            int exceptions=0;
            for (HashMap.Entry<DN, String[]> entry : targetBases.entrySet()) {
                DistinguishedName newDN = new DistinguishedName(
                        utils.getRemoteMappedDN(base.getDN(),explodedSourceBase,entry.getValue()));

                //chain.setBindDN(newDN);
                try {
                    System.out.println("AliasInterceptor.search : nextSearch " + newDN.toString());
                    chain.nextSearch(newDN, scope, filter, attributes, typesOnly, results, constraints);
                    System.out.println("AliasInterceptor.search : nextSearchFinished" + newDN.toString());
                    //break;
                } 
                catch(LDAPException e)
                {
                    e.printStackTrace();
                    System.out.println("AliasInterceptor.search : exception" + newDN.toString());
                    exceptions++;
                }
                finally
                {
                    System.out.println("AliasInterceptor.search : finally" + newDN.toString());     
                }
            }

        }
    }

    public String getName() {
        return this.name;
    }

    public void shutdown() {
        // TODO Auto-generated method stub  
    }
}