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.daemon.platform.*;
029:
030: import com.sshtools.j2ssh.authentication.*;
031: import com.sshtools.j2ssh.transport.*;
032:
033: import org.apache.commons.logging.*;
034:
035: import java.io.*;
036:
037: import java.util.*;
038:
039: /**
040: *
041: *
042: * @author $author$
043: * @version $Revision: 1.10 $
044: */
045: public class KBIPasswordAuthenticationServer extends
046: SshAuthenticationServer {
047: private static Log log = LogFactory
048: .getLog(KBIPasswordAuthenticationServer.class);
049:
050: /**
051: *
052: *
053: * @return
054: */
055: public final String getMethodName() {
056: return "keyboard-interactive";
057: }
058:
059: /**
060: *
061: *
062: * @param tokens
063: */
064: public void setAuthenticatedTokens(Map tokens) {
065: }
066:
067: /**
068: *
069: *
070: * @param authentication
071: * @param msg
072: *
073: * @return
074: *
075: * @throws IOException
076: */
077: public int authenticate(
078: AuthenticationProtocolServer authentication,
079: SshMsgUserAuthRequest msg) throws IOException { //, Map nativeSettings)
080:
081: NativeAuthenticationProvider authImpl = NativeAuthenticationProvider
082: .getInstance();
083:
084: if (authImpl == null) {
085: log
086: .error("Cannot perfrom authentication witout native authentication provider");
087:
088: return AuthenticationProtocolState.FAILED;
089: }
090:
091: authentication
092: .registerMessage(
093: SshMsgUserAuthInfoResponse.SSH_MSG_USERAUTH_INFO_RESPONSE,
094: SshMsgUserAuthInfoResponse.class);
095:
096: SshMsgUserAuthInfoRequest info = new SshMsgUserAuthInfoRequest(
097: "Password authentication", "", "");
098: info.addPrompt(msg.getUsername() + "'s password", false);
099: authentication.sendMessage(info);
100:
101: SshMessage response = authentication.readMessage();
102:
103: if (response instanceof SshMsgUserAuthInfoResponse) {
104: String[] responses = ((SshMsgUserAuthInfoResponse) response)
105: .getResponses();
106:
107: if (responses.length == 1) {
108: String password = responses[0];
109:
110: try {
111: if (authImpl.logonUser(msg.getUsername(), password)) { //, nativeSettings)) {
112: log
113: .info(msg.getUsername()
114: + " has passed password authentication");
115:
116: return AuthenticationProtocolState.COMPLETE;
117: } else {
118: log
119: .info(msg.getUsername()
120: + " has failed password authentication");
121:
122: return AuthenticationProtocolState.FAILED;
123: }
124: } catch (PasswordChangeException ex) {
125: info = new SshMsgUserAuthInfoRequest(
126: "Password change required", "", "");
127: info.addPrompt("New password", false);
128: info.addPrompt("Confirm password", false);
129: authentication.sendMessage(info);
130: response = authentication.readMessage();
131:
132: if (response instanceof SshMsgUserAuthInfoResponse) {
133: responses = ((SshMsgUserAuthInfoResponse) response)
134: .getResponses();
135:
136: if (responses.length == 2) {
137: if (responses[0].equals(responses[1])) {
138: if (authImpl.changePassword(msg
139: .getUsername(), password,
140: responses[0])) {
141: return AuthenticationProtocolState.COMPLETE;
142: } else {
143: return AuthenticationProtocolState.FAILED;
144: }
145: } else {
146: return AuthenticationProtocolState.FAILED;
147: }
148: } else {
149: log
150: .error("Client replied with an invalid message "
151: + response.getMessageName());
152:
153: return AuthenticationProtocolState.FAILED;
154: }
155: } else {
156: log
157: .error("Client replied with an invalid message "
158: + response.getMessageName());
159:
160: return AuthenticationProtocolState.FAILED;
161: }
162: }
163: } else {
164: log.error("Client responded with too many values!");
165:
166: return AuthenticationProtocolState.FAILED;
167: }
168: } else {
169: log.error("Client replied with an invalid message "
170: + response.getMessageName());
171:
172: return AuthenticationProtocolState.FAILED;
173: }
174: }
175: }
|