001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.daemon.authentication;
027:
028: import com.sshtools.j2ssh.configuration.ConfigurationException;
029: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
030: import com.sshtools.j2ssh.configuration.ExtensionAlgorithm;
031: import com.sshtools.j2ssh.configuration.SshAPIConfiguration;
032: import com.sshtools.j2ssh.transport.AlgorithmNotSupportedException;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import java.util.ArrayList;
038: import java.util.HashMap;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Map;
042:
043: /**
044: *
045: *
046: * @author $author$
047: * @version $Revision: 1.10 $
048: */
049: public class SshAuthenticationServerFactory {
050: private static Map auths;
051: private static Log log = LogFactory
052: .getLog(SshAuthenticationServerFactory.class);
053:
054: /** */
055: public final static String AUTH_PASSWORD = "password";
056:
057: /** */
058: public final static String AUTH_PK = "publickey";
059:
060: /** */
061: public final static String AUTH_KBI = "keyboard-interactive";
062:
063: static {
064: auths = new HashMap();
065: log.info("Loading supported authentication methods");
066: auths.put(AUTH_PASSWORD, PasswordAuthenticationServer.class);
067: auths.put(AUTH_PK, PublicKeyAuthenticationServer.class);
068: auths.put(AUTH_KBI, KBIPasswordAuthenticationServer.class);
069:
070: try {
071: if (ConfigurationLoader
072: .isConfigurationAvailable(SshAPIConfiguration.class)) {
073: SshAPIConfiguration config = (SshAPIConfiguration) ConfigurationLoader
074: .getConfiguration(SshAPIConfiguration.class);
075: List addons = config.getAuthenticationExtensions();
076: Iterator it = addons.iterator();
077:
078: // Add the methods to our supported list
079: while (it.hasNext()) {
080: ExtensionAlgorithm method = (ExtensionAlgorithm) it
081: .next();
082: String name = method.getAlgorithmName();
083:
084: if (auths.containsKey(name)) {
085: log
086: .debug("Standard authentication implementation for "
087: + name
088: + " is being overidden by "
089: + method
090: .getImplementationClass());
091: } else {
092: log.debug(name
093: + " authentication is implemented by "
094: + method.getImplementationClass());
095: }
096:
097: try {
098: Class cls = ConfigurationLoader
099: .getExtensionClass(method
100: .getImplementationClass());
101: Object obj = cls.newInstance();
102:
103: if (obj instanceof SshAuthenticationServer) {
104: auths.put(name, cls);
105: }
106: } catch (Exception e) {
107: log
108: .warn(
109: "Failed to load extension authentication implementation "
110: + method
111: .getImplementationClass(),
112: e);
113: }
114: }
115: }
116: } catch (ConfigurationException ex) {
117: }
118: }
119:
120: /**
121: * Creates a new SshAuthenticationServerFactory object.
122: */
123: protected SshAuthenticationServerFactory() {
124: }
125:
126: /**
127: *
128: */
129: public static void initialize() {
130: }
131:
132: /**
133: *
134: *
135: * @return
136: */
137: public static List getSupportedMethods() {
138: // Get the list of ciphers
139: ArrayList list = new ArrayList(auths.keySet());
140:
141: // Return the list
142: return list;
143: }
144:
145: /**
146: *
147: *
148: * @param methodName
149: *
150: * @return
151: *
152: * @throws AlgorithmNotSupportedException
153: */
154: public static SshAuthenticationServer newInstance(String methodName)
155: throws AlgorithmNotSupportedException {
156: try {
157: return (SshAuthenticationServer) ((Class) auths
158: .get(methodName)).newInstance();
159: } catch (Exception e) {
160: throw new AlgorithmNotSupportedException(methodName
161: + " is not supported!");
162: }
163: }
164: }
|