Skip to content

Commit 6e7fb96

Browse files
authored
Support SSHClient.authPassword on FreeBSD (#815)
* Support SSHClient.authPassword on FreeBSD FreeBSD "keyboard-interactive" prompt is "Password for user@host:" * Add test for PasswordResponseProvider
1 parent d5d6096 commit 6e7fb96

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

src/main/java/net/schmizz/sshj/userauth/method/PasswordResponseProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
public class PasswordResponseProvider
2828
implements ChallengeResponseProvider {
2929

30-
public static final Pattern DEFAULT_PROMPT_PATTERN = Pattern.compile(".*[pP]assword:\\s?\\z", Pattern.DOTALL);
30+
// FreeBSD prompt is "Password for user@host:"
31+
public static final Pattern DEFAULT_PROMPT_PATTERN = Pattern.compile(".*[pP]assword(?: for .*)?:\\s?\\z", Pattern.DOTALL);
3132

3233
private static final char[] EMPTY_RESPONSE = new char[0];
3334

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (C)2009 - SSHJ Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hierynomus.sshj.userauth.method;
17+
18+
import net.schmizz.sshj.userauth.method.PasswordResponseProvider;
19+
import net.schmizz.sshj.userauth.password.AccountResource;
20+
import net.schmizz.sshj.userauth.password.PasswordFinder;
21+
import net.schmizz.sshj.userauth.password.Resource;
22+
import org.jetbrains.annotations.NotNull;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
import java.util.Collections;
27+
import java.util.regex.Pattern;
28+
29+
public class PasswordResponseProviderTest {
30+
private static final char[] PASSWORD = "the_password".toCharArray();
31+
private static final AccountResource ACCOUNT_RESOURCE = new AccountResource("user", "host");
32+
33+
@Test
34+
public void shouldMatchCommonPrompts() {
35+
PasswordResponseProvider responseProvider = createDefaultResponseProvider(false);
36+
shouldMatch(responseProvider, "Password: ");
37+
shouldMatch(responseProvider, "password: ");
38+
shouldMatch(responseProvider, "Password:");
39+
shouldMatch(responseProvider, "password:");
40+
shouldMatch(responseProvider, "user@host's Password: ");
41+
shouldMatch(responseProvider, "user@host's password: ");
42+
shouldMatch(responseProvider, "user@host's Password:");
43+
shouldMatch(responseProvider, "user@host's password:");
44+
shouldMatch(responseProvider, "user@host: Password: ");
45+
shouldMatch(responseProvider, "(user@host) Password: ");
46+
shouldMatch(responseProvider, "any prefix Password for user@host: ");
47+
shouldMatch(responseProvider, "any prefix password for user@host: ");
48+
shouldMatch(responseProvider, "any prefix Password for user@host:");
49+
shouldMatch(responseProvider, "any prefix password for user@host:");
50+
}
51+
52+
@Test
53+
public void shouldNotMatchOtherPrompts() {
54+
PasswordResponseProvider responseProvider = createDefaultResponseProvider(false);
55+
shouldNotMatch(responseProvider, "Password");
56+
shouldNotMatch(responseProvider, "password");
57+
shouldNotMatch(responseProvider, "Password: ");
58+
shouldNotMatch(responseProvider, "password: suffix");
59+
shouldNotMatch(responseProvider, "Password of user@host:");
60+
shouldNotMatch(responseProvider, "");
61+
shouldNotMatch(responseProvider, "password :");
62+
shouldNotMatch(responseProvider, "something else");
63+
}
64+
65+
@Test
66+
public void shouldPassRetry() {
67+
Assert.assertFalse(createDefaultResponseProvider(false).shouldRetry());
68+
Assert.assertTrue(createDefaultResponseProvider(true).shouldRetry());
69+
}
70+
71+
@Test
72+
public void shouldHaveNoSubmethods() {
73+
Assert.assertEquals(createDefaultResponseProvider(true).getSubmethods(), Collections.emptyList());
74+
}
75+
76+
@Test
77+
public void shouldWorkWithCustomPattern() {
78+
PasswordFinder passwordFinder = new TestPasswordFinder(true);
79+
PasswordResponseProvider responseProvider = new PasswordResponseProvider(passwordFinder, Pattern.compile(".*custom.*"));
80+
responseProvider.init(ACCOUNT_RESOURCE, "name", "instruction");
81+
shouldMatch(responseProvider, "prefix custom suffix: ");
82+
shouldNotMatch(responseProvider, "something else");
83+
}
84+
85+
private static void shouldMatch(PasswordResponseProvider responseProvider, String prompt) {
86+
checkPrompt(responseProvider, prompt, PASSWORD);
87+
}
88+
89+
private static void shouldNotMatch(PasswordResponseProvider responseProvider, String prompt) {
90+
checkPrompt(responseProvider, prompt, new char[0]);
91+
}
92+
93+
private static void checkPrompt(PasswordResponseProvider responseProvider, String prompt, char[] expected) {
94+
Assert.assertArrayEquals("Prompt '" + prompt + "'", expected, responseProvider.getResponse(prompt, false));
95+
}
96+
97+
@NotNull
98+
private static PasswordResponseProvider createDefaultResponseProvider(final boolean shouldRetry) {
99+
PasswordFinder passwordFinder = new TestPasswordFinder(shouldRetry);
100+
PasswordResponseProvider responseProvider = new PasswordResponseProvider(passwordFinder);
101+
responseProvider.init(ACCOUNT_RESOURCE, "name", "instruction");
102+
return responseProvider;
103+
}
104+
105+
private static class TestPasswordFinder implements PasswordFinder {
106+
private final boolean shouldRetry;
107+
108+
public TestPasswordFinder(boolean shouldRetry) {
109+
this.shouldRetry = shouldRetry;
110+
}
111+
112+
@Override
113+
public char[] reqPassword(Resource<?> resource) {
114+
Assert.assertEquals(resource, ACCOUNT_RESOURCE);
115+
return PASSWORD;
116+
}
117+
118+
@Override
119+
public boolean shouldRetry(Resource<?> resource) {
120+
Assert.assertEquals(resource, ACCOUNT_RESOURCE);
121+
return shouldRetry;
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)