/
Ssh2Admin.java
493 lines (428 loc) · 17.8 KB
/
Ssh2Admin.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package org.dcache.services.ssh2;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import org.apache.sshd.common.Factory;
import org.apache.sshd.common.FactoryManager;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.PropertyResolverUtils;
import org.apache.sshd.common.config.keys.KeyUtils;
import org.apache.sshd.common.keyprovider.KeyPairProvider;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.session.SessionListener;
import org.apache.sshd.common.util.security.SecurityUtils;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.gss.GSSAuthenticator;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
import org.apache.sshd.server.session.ServerSession;
import org.dcache.auth.LoginNamePrincipal;
import org.dcache.auth.LoginReply;
import org.dcache.auth.LoginStrategy;
import org.dcache.auth.Origin;
import org.dcache.auth.PasswordCredential;
import org.dcache.auth.Subjects;
import org.dcache.util.Glob;
import org.dcache.util.Subnet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import javax.security.auth.Subject;
import javax.security.auth.kerberos.KerberosPrincipal;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import diskCacheV111.util.AuthorizedKeyParser;
import diskCacheV111.util.CacheException;
import diskCacheV111.util.PermissionDeniedCacheException;
import dmg.cells.nucleus.CellCommandListener;
import dmg.cells.nucleus.CellLifeCycleAware;
import org.dcache.util.Files;
import org.dcache.util.NetLoggerBuilder;
/**
* This class starts the ssh server. It is however not started in the
* constructor, but in afterStart() to avoid race conditions. The class starts
* the UserAdminShell via the factory CommandFactory, which in turn create the
* Command_ConsoleReader that actually creates an instance of UserAdminShell.
*
* @author bernardt
*/
public class Ssh2Admin implements CellCommandListener, CellLifeCycleAware
{
private static final Logger _log = LoggerFactory.getLogger(Ssh2Admin.class);
private static final Logger _accessLog =
LoggerFactory.getLogger("org.dcache.access.ssh2");
// get the user part from a public key line like
// ... ssh-rsa AAAA...... user@hostname
private static final Pattern _pubKeyFileUsername =
Pattern.compile(".*ssh-.* (.*?)@.*");
private final SshServer _server;
// UniversalSpringCell injected parameters
private Path _hostKey;
private File _authorizedKeyList;
private String _host;
private int _port;
private int _adminGroupId;
private LoginStrategy _loginStrategy;
private TimeUnit _idleTimeoutUnit;
private long _idleTimeout;
private enum Outcome { ALLOW, DENY, DEFER }
// switches for different Authenticators
private enum AuthenticationMechanism {KERBEROS, PASSWORD, PUBLICKEY}
private EnumSet<AuthenticationMechanism> enabledAuthenticationMechanisms;
private String keyTabFile;
public Ssh2Admin() {
_server = SshServer.setUpDefaultServer();
}
public LoginStrategy getLoginStrategy() {
return _loginStrategy;
}
public void setLoginStrategy(LoginStrategy loginStrategy) {
_loginStrategy = loginStrategy;
}
public void setPort(int port) {
_log.debug("Ssh2 port set to: {}", String.valueOf(port));
_port = port;
}
public int getPort() {
return _port;
}
public void setHost(String host) {
_host = host;
}
public String getHost() {
return _host;
}
public void setAdminGroupId(int groupId) {
_adminGroupId = groupId;
}
public int getAdminGroupId() {
return _adminGroupId;
}
public void setHostKeys(String key) throws IOException {
Files.checkFile(key);
_hostKey = Paths.get(key);
}
public File getAuthorizedKeyList() {
return _authorizedKeyList;
}
public void setAuthorizedKeyList(File authorizedKeyList) {
_authorizedKeyList = authorizedKeyList;
}
@Required
public void setShellFactory(Factory<Command> shellCommand)
{
_server.setShellFactory(shellCommand);
}
@Required
public void setSubsystemFactories(List<NamedFactory<Command>> subsystemFactories)
{
_server.setSubsystemFactories(subsystemFactories);
}
@Required
public void setIdleTimeout(long timeout)
{
_idleTimeout = timeout;
}
@Required
public void setIdleTimeoutUnit(TimeUnit unit) {
_idleTimeoutUnit = unit;
}
@Required
public void setEnabledAuthenticationMechanisms(String mechanisms) {
enabledAuthenticationMechanisms = EnumSet.copyOf(
Arrays.stream(
Iterables.toArray(
Splitter.on(',')
.trimResults()
.split(mechanisms.toUpperCase()),
String.class))
.map(AuthenticationMechanism::valueOf)
.collect(Collectors.toList()));
}
@Required
public void setKeyTabFile(String name) {
keyTabFile = name;
}
private void configureAuthentication() {
_server.setGSSAuthenticator(enabledAuthenticationMechanisms.contains(AuthenticationMechanism.KERBEROS) ?
new AdminGssAuthenticator() : null);
_server.setPasswordAuthenticator(enabledAuthenticationMechanisms.contains(AuthenticationMechanism.PASSWORD) ?
new AdminPasswordAuthenticator() : null);
_server.setPublickeyAuthenticator(enabledAuthenticationMechanisms.contains(AuthenticationMechanism.PUBLICKEY) ?
new AdminPublickeyAuthenticator() : null);
}
@Override
public void afterStart() {
configureAuthentication();
configureKeyFiles();
startServer();
_log.debug("Ssh2 Admin Interface started!");
}
@Override
public void beforeStop() {
try {
_server.stop();
} catch (IOException e) {
_log.warn("SSH failure during shutdown: {}", e.getMessage());
}
}
private void configureKeyFiles() {
KeyPairProvider keyPair = SecurityUtils.createGeneratorHostKeyProvider(_hostKey);
_server.setKeyPairProvider(keyPair);
}
private void startServer() {
long effectiveTimeout = _idleTimeoutUnit.toMillis(_idleTimeout);
PropertyResolverUtils.updateProperty(_server, FactoryManager.IDLE_TIMEOUT, effectiveTimeout);
// esure, that read timeout is longer than idle timeout
PropertyResolverUtils.updateProperty(_server, FactoryManager.NIO2_READ_TIMEOUT, effectiveTimeout*2);
_server.setPort(_port);
_server.setHost(_host);
_server.addSessionListener(new AdminConnectionLogger());
try {
_server.start();
} catch (IOException ioe) {
throw new RuntimeException("Ssh2 server was interrupted while starting: ", ioe);
}
}
private void addOrigin(ServerSession session, Subject subject)
{
SocketAddress remote = session.getIoSession().getRemoteAddress();
if (remote instanceof InetSocketAddress) {
InetAddress address = ((InetSocketAddress) remote).getAddress();
subject.getPrincipals().add(new Origin(address));
}
}
private void logLoginTry(String username, ServerSession session,
String method, boolean successful, String reason)
{
NetLoggerBuilder.Level logLevel = NetLoggerBuilder.Level.INFO;
if (!successful) {
logLevel = NetLoggerBuilder.Level.WARN;
}
new NetLoggerBuilder(logLevel, "org.dcache.services.ssh2.login")
.omitNullValues()
.add("username", username)
.add("remote.socket", session.getClientAddress())
.add("method", method)
.add("successful", successful)
.add("reason", reason)
.toLogger(_accessLog);
// set session parameter
if (successful) {
try {
session.setAuthenticated();
session.setUsername(username);
} catch (IOException e) {
_log.error("Failed to set Authenticated: {}",
e.getMessage());
}
}
}
private class AdminPasswordAuthenticator implements PasswordAuthenticator {
@Override
public boolean authenticate(String userName, String password,
ServerSession session) {
boolean successful = false;
String reason = null;
Subject subject = new Subject();
addOrigin(session, subject);
subject.getPrivateCredentials().add(new PasswordCredential(userName,
password));
try {
LoginReply reply = _loginStrategy.login(subject);
Subject authenticatedSubject = reply.getSubject();
if (!Subjects.hasGid(authenticatedSubject, _adminGroupId)) {
reason = "not member of admin gid";
throw new PermissionDeniedCacheException(reason);
}
successful = true;
} catch (PermissionDeniedCacheException e) {
_log.warn("Login for {} denied: {}", userName, e.getMessage());
} catch (CacheException e) {
reason = e.toString();
_log.warn("Login for {} failed: {}", userName, e.toString());
}
logLoginTry(userName, session, "Password", successful, reason);
return successful;
}
}
private class AdminPublickeyAuthenticator implements PublickeyAuthenticator {
private PublicKey toPublicKey(String s) {
try {
AuthorizedKeyParser decoder = new AuthorizedKeyParser();
return decoder.decodePublicKey(s);
} catch (InvalidKeySpecException | NoSuchAlgorithmException | IllegalArgumentException e) {
_log.warn("can't decode public key from file: {} ", e.getMessage());
}
return null;
}
private String getUserFromKeyLine(String line) {
Matcher m = _pubKeyFileUsername.matcher(line);
if (m.matches()) {
return m.group(1);
}
return "";
}
@Override
public boolean authenticate(String userName, PublicKey key,
ServerSession session) {
boolean successful = false;
String reason = null;
_log.debug("Authentication username set to: {} publicKey: {}",
userName, key);
try {
try(Stream<String> fileStream = java.nio.file.Files.lines(_authorizedKeyList.toPath())) {
String matchedPubKey = fileStream
.filter(l -> !l.isEmpty() && !l.matches(" *#.*"))
.filter(l -> key.equals(toPublicKey(l)))
.findFirst()
.orElse(null);
if (null == matchedPubKey) {
reason = "key file: no match";
} else if (!isValidHost(matchedPubKey, session)) {
reason = "key file: publickey used from unallowed host";
} else {
String keyUsername = getUserFromKeyLine(matchedPubKey);
if (keyUsername.isEmpty()) {
reason =
"key file: no username@host for publickey set";
} else if (userName.equals(keyUsername)) {
successful = true;
} else {
reason = "key file: different username for "
+ "publickey expected";
}
}
}
} catch (FileNotFoundException e) {
_log.debug("File not found: {}", _authorizedKeyList);
} catch (IOException e) {
_log.error("Failed to read {}: {}", _authorizedKeyList,
e.getMessage());
}
// the method gets called twice while pubkey authentication,
// to avoid duplicate logging, check if already authenticated
if (!session.isAuthenticated()) {
String method = "PublicKey (" + key.getAlgorithm() + " "
+ KeyUtils.getFingerPrint(key) + ")";
logLoginTry(userName, session, method, successful, reason);
}
return successful;
}
private boolean isValidHost(String line, ServerSession session) {
for (String linePart : Splitter.on(" ").trimResults().omitEmptyStrings().split(line)) {
if (linePart.startsWith("from=\"") && linePart.endsWith("\"")) {
String from = linePart.substring(6, linePart.length()-1);
Set<Outcome> outcomes = EnumSet.noneOf(Outcome.class);
for (String pattern : Splitter.on(",").trimResults().omitEmptyStrings().split(from)) {
outcomes.add(patternMatchesHost(pattern, session));
}
return outcomes.contains(Outcome.ALLOW) && !outcomes.contains(Outcome.DENY);
}
}
return true;
}
private Outcome patternMatchesHost(String pattern, ServerSession session) {
boolean patternIsNegated = pattern.startsWith("!");
Outcome matchOutcome = patternIsNegated ? Outcome.DENY : Outcome.ALLOW;
if (patternIsNegated) {
pattern = pattern.substring(1);
}
SocketAddress rawRemote = session.getClientAddress();
if (rawRemote instanceof InetSocketAddress) {
InetSocketAddress remote = (InetSocketAddress) rawRemote;
InetAddress remoteAddress = remote.getAddress();
if (Subnet.isValid(pattern)) {
if (Subnet.create(pattern).contains(remoteAddress)) {
return matchOutcome;
}
} else {
Glob glob = new Glob(pattern);
if (glob.matches(remote.getHostName()) || glob.matches(remoteAddress.getHostAddress())) {
return matchOutcome;
}
}
}
return Outcome.DEFER;
}
}
private class AdminConnectionLogger implements SessionListener {
@Override
public void sessionCreated(Session session) {
logEvent("org.dcache.services.ssh2.connect", session);
}
@Override
public void sessionClosed(Session session) {
logEvent("org.dcache.services.ssh2.disconnect", session);
}
private void logEvent(String name, Session session) {
new NetLoggerBuilder(NetLoggerBuilder.Level.INFO, name)
.omitNullValues()
.add("username", session.getUsername())
.add("remote.socket", session.getIoSession()
.getRemoteAddress())
.toLogger(_accessLog);
}
}
private class AdminGssAuthenticator extends GSSAuthenticator {
AdminGssAuthenticator() {
setKeytabFile(keyTabFile);
}
@Override
public boolean validateInitialUser(ServerSession session, String user) {
session.setUsername(user);
return true;
}
@Override
public boolean validateIdentity(ServerSession session, String identity) {
boolean successful = false;
String reason = null;
Subject subject = new Subject();
subject.getPrincipals().add(new KerberosPrincipal(identity));
String userName = session.getUsername();
if (userName != null) {
subject.getPrincipals().add(new LoginNamePrincipal(userName));
}
addOrigin(session, subject);
try {
LoginReply reply = _loginStrategy.login(subject);
Subject authenticatedSubject = reply.getSubject();
if (!Subjects.hasGid(authenticatedSubject, _adminGroupId)) {
throw new PermissionDeniedCacheException("not member of admin gid");
}
successful = true;
} catch (PermissionDeniedCacheException e) {
_log.error("Login for {} denied: {}",
Strings.nullToEmpty(userName),
e.getMessage());
reason = e.getMessage();
} catch (CacheException e) {
reason = e.toString();
_log.error("Login for {} failed: {}",
Strings.nullToEmpty(userName),
e.toString());
}
logLoginTry(userName, session, "GSS", successful, reason);
return successful;
}
}
}