summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java
blob: d97fa1f6b43b3094c83875d85e1b673db24e8bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.c2kernel.lookup.ldap;

import com.c2kernel.common.InvalidData;
import com.c2kernel.common.ObjectNotFound;
import com.c2kernel.process.Gateway;
import com.c2kernel.process.auth.Authenticator;
import com.c2kernel.utils.Logger;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;

public class LDAPAuthManager implements Authenticator {

    protected LDAPConnection mLDAPConn;
    protected LDAPProperties ldapProps;
    

	@Override
	public boolean authenticate(String agentName,
			String password, String resource) throws InvalidData, ObjectNotFound {
		
		ldapProps = new LDAPProperties(Gateway.getProperties());
		
        if (ldapProps.mHost!=null && ldapProps.mPort!= null && ldapProps.mLocalPath!=null )
        {
            try { // anonymously bind to LDAP and find the agent entry for the username
                ldapProps.mUser = "";
                ldapProps.mPassword = "";
                mLDAPConn = LDAPLookupUtils.createConnection(ldapProps);
                LDAPLookup anonLookup = new LDAPLookup();
                anonLookup.initPaths(ldapProps);
                anonLookup.open(this);
                String agentDN = anonLookup.getFullDN(anonLookup.getAgentPath(agentName));

                //found agentDN, try to log in with it
                ldapProps.mUser = agentDN;
                ldapProps.mPassword = password;
                mLDAPConn = LDAPLookupUtils.createConnection(ldapProps);
                return true;
            } catch (LDAPException e) {
                return false;
            }
        }
        else
        {
            throw new InvalidData("Cannot log in. Some connection properties are not set.");
        }

	}
	
	@Override
	public boolean authenticate(String resource) throws InvalidData, ObjectNotFound {
		ldapProps = new LDAPProperties(Gateway.getProperties());
		
		if (ldapProps.mUser == null || ldapProps.mUser.length()==0 ||
			ldapProps.mPassword == null || ldapProps.mPassword.length()==0)
			throw new InvalidData("LDAP root user properties not found in config.");
		try {
			mLDAPConn = LDAPLookupUtils.createConnection(ldapProps);
			return true;
		} catch (LDAPException e) {
            return false;
        }
	}

	/* (non-Javadoc)
	 * @see com.c2kernel.process.auth.Authenticator#getAuthObject()
	 */
	@Override
	public LDAPConnection getAuthObject() {
        
		if (mLDAPConn==null || !mLDAPConn.isConnected()) {
            Logger.warning("LDAPAuthManager - lost connection to LDAP server. Attempting to reconnect.");
            try {
                mLDAPConn = LDAPLookupUtils.createConnection(ldapProps);
            } catch (LDAPException ex) { }
        }
        return mLDAPConn;
	}

	@Override
	public void disconnect() {
        Logger.msg(1, "LDAP Lookup: Shutting down LDAP connection.");
        if (mLDAPConn != null) {
            try {
                mLDAPConn.disconnect();
            } catch (LDAPException e) {
                Logger.error(e);
            }
            mLDAPConn = null;
        }
		
	}

	public LDAPAuthManager() {
		// TODO Auto-generated constructor stub
	}


}