001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.core.impl.checker;
017
018import java.util.Hashtable;
019import java.util.List;
020
021import javax.naming.Context;
022import javax.naming.NamingException;
023import javax.naming.directory.DirContext;
024import javax.naming.directory.InitialDirContext;
025
026import org.apache.avalon.framework.logger.AbstractLogEnabled;
027
028import org.ametys.runtime.model.checker.ItemChecker;
029import org.ametys.runtime.model.checker.ItemCheckerTestFailureException;
030
031/**
032 * Check the connection to a LDAP directory
033 *
034 */
035public class LDAPConnectionChecker extends AbstractLogEnabled implements ItemChecker
036{
037    @Override
038    public void check(List<String> values) throws ItemCheckerTestFailureException
039    {
040        Hashtable<String, String> env = new Hashtable<>();
041        
042        // Get the parameter values
043        String baseUrl = values.get(0);
044        String authMethod = values.get(1);
045        String adminDN = values.get(2);
046        String adminPassword = values.get(3);
047        String useSSL = values.get(4);
048        String followReferrals = values.get(5);
049        String baseDN = values.get(6);
050        
051        // Define the corresponding context
052        env.put(Context.PROVIDER_URL, baseUrl);
053        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
054        env.put(Context.SECURITY_AUTHENTICATION, authMethod);
055        
056        if (authMethod.equals("simple"))
057        {
058            env.put(Context.SECURITY_PRINCIPAL, adminDN);
059            env.put(Context.SECURITY_CREDENTIALS, adminPassword);
060        }
061        if (useSSL.equals("true"))
062        {
063            env.put(Context.SECURITY_PROTOCOL, "ssl");
064        }
065        if (followReferrals.equals("true"))
066        {
067            env.put(Context.REFERRAL, "follow");
068        }
069        
070        DirContext context = null;
071        try
072        {
073            // Try and connect
074            context = new InitialDirContext(env);
075            
076            // Check base DN
077            context.search(baseDN, null);
078        }
079        catch (NamingException e)
080        {
081            throw new ItemCheckerTestFailureException(e);
082        }
083        finally
084        {
085            // Close environment
086            if (context != null)
087            {
088                try
089                {
090                    context.close();
091                }
092                catch (NamingException e)
093                {
094                    getLogger().error("Closing the LDAP connection during test failed.", e);
095                }
096            }
097        }
098    }
099}