Allow the daemon's host key to authenticate to itself
We now honor the daemon's own host key as an authentication token, provided it comes in with the username 'Gerrit Code Review'. This requires that the key be in the standard SSH key format, which means the daemon must also be using the Bouncy Castle Crypto library. When we authenticate with our own key we give ourselves full administrative access. This can be a useful backdoor if an administrator has a running server, has full local UNIX access, but has locked themselves out of their administrative account. They can still SSH in locally using the server's host key and flush the caches to pick up any edits made to their own user account through direct database updates. Change-Id: Id2c4d8edd575a002944cbb1acaec149c52e25071 Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
// Copyright (C) 2010 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server;
|
||||
|
||||
import com.google.gerrit.reviewdb.AccountGroup;
|
||||
import com.google.gerrit.reviewdb.Change;
|
||||
import com.google.gerrit.server.config.AuthConfig;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/** Identity of a peer daemon process that isn't this JVM. */
|
||||
public class PeerDaemonUser extends CurrentUser {
|
||||
/** Magic username used by peers when they authenticate. */
|
||||
public static final String USER_NAME = "Gerrit Code Review";
|
||||
|
||||
public interface Factory {
|
||||
PeerDaemonUser create(@Assisted SocketAddress peer);
|
||||
}
|
||||
|
||||
private final Set<AccountGroup.Id> effectiveGroups;
|
||||
private final SocketAddress peer;
|
||||
|
||||
@Inject
|
||||
protected PeerDaemonUser(AuthConfig authConfig, @Assisted SocketAddress peer) {
|
||||
super(AccessPath.SSH, authConfig);
|
||||
|
||||
final HashSet<AccountGroup.Id> g = new HashSet<AccountGroup.Id>();
|
||||
g.add(authConfig.getAdministratorsGroup());
|
||||
this.effectiveGroups = Collections.unmodifiableSet(g);
|
||||
this.peer = peer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<AccountGroup.Id> getEffectiveGroups() {
|
||||
return effectiveGroups;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Change.Id> getStarredChanges() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
public SocketAddress getRemoteAddress() {
|
||||
return peer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PeerDaemon[address " + getRemoteAddress() + "]";
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,9 @@ package com.google.gerrit.sshd;
|
||||
|
||||
import com.google.gerrit.reviewdb.AccountSshKey;
|
||||
import com.google.gerrit.server.AccessPath;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.PeerDaemonUser;
|
||||
import com.google.gerrit.sshd.SshScope.Context;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
@@ -24,11 +26,16 @@ import com.google.inject.Singleton;
|
||||
|
||||
import org.apache.mina.core.future.IoFuture;
|
||||
import org.apache.mina.core.future.IoFutureListener;
|
||||
import org.apache.sshd.common.KeyPairProvider;
|
||||
import org.apache.sshd.server.PublickeyAuthenticator;
|
||||
import org.apache.sshd.server.session.ServerSession;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.security.KeyPair;
|
||||
import java.security.PublicKey;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Authenticates by public key through {@link AccountSshKey} entities.
|
||||
@@ -38,19 +45,50 @@ class DatabasePubKeyAuth implements PublickeyAuthenticator {
|
||||
private final SshKeyCacheImpl sshKeyCache;
|
||||
private final SshLog log;
|
||||
private final IdentifiedUser.GenericFactory userFactory;
|
||||
private final PeerDaemonUser.Factory peerFactory;
|
||||
private final Set<PublicKey> myHostKeys;
|
||||
|
||||
@Inject
|
||||
DatabasePubKeyAuth(final SshKeyCacheImpl skc, final SshLog l,
|
||||
final IdentifiedUser.GenericFactory uf) {
|
||||
final IdentifiedUser.GenericFactory uf, final PeerDaemonUser.Factory pf,
|
||||
final KeyPairProvider hostKeyProvider) {
|
||||
sshKeyCache = skc;
|
||||
log = l;
|
||||
userFactory = uf;
|
||||
peerFactory = pf;
|
||||
myHostKeys = myHostKeys(hostKeyProvider);
|
||||
}
|
||||
|
||||
private static Set<PublicKey> myHostKeys(KeyPairProvider p) {
|
||||
final Set<PublicKey> keys = new HashSet<PublicKey>(2);
|
||||
addPublicKey(keys, p, KeyPairProvider.SSH_RSA);
|
||||
addPublicKey(keys, p, KeyPairProvider.SSH_DSS);
|
||||
return keys;
|
||||
}
|
||||
|
||||
private static void addPublicKey(final Collection<PublicKey> out,
|
||||
final KeyPairProvider p, final String type) {
|
||||
final KeyPair pair = p.loadKey(type);
|
||||
if (pair != null && pair.getPublic() != null) {
|
||||
out.add(pair.getPublic());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean authenticate(final String username,
|
||||
final PublicKey suppliedKey, final ServerSession session) {
|
||||
final SshSession sd = session.getAttribute(SshSession.KEY);
|
||||
|
||||
if (PeerDaemonUser.USER_NAME.equals(username)) {
|
||||
if (myHostKeys.contains(suppliedKey)) {
|
||||
PeerDaemonUser user = peerFactory.create(sd.getRemoteAddress());
|
||||
return success(username, session, sd, user);
|
||||
|
||||
} else {
|
||||
sd.authenticationError(username, "no-matching-key");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
final Iterable<SshKeyCacheEntry> keyList = sshKeyCache.get(username);
|
||||
final SshKeyCacheEntry key = find(keyList, suppliedKey);
|
||||
if (key == null) {
|
||||
@@ -79,8 +117,13 @@ class DatabasePubKeyAuth implements PublickeyAuthenticator {
|
||||
}
|
||||
}
|
||||
|
||||
return success(username, session, sd, createUser(sd, key));
|
||||
}
|
||||
|
||||
private boolean success(final String username, final ServerSession session,
|
||||
final SshSession sd, final CurrentUser user) {
|
||||
if (sd.getCurrentUser() == null) {
|
||||
sd.authenticationSuccess(username, createUser(sd, key));
|
||||
sd.authenticationSuccess(username, user);
|
||||
|
||||
// If this is the first time we've authenticated this
|
||||
// session, record a login event in the log and add
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.google.gerrit.sshd;
|
||||
import com.google.gerrit.lifecycle.LifecycleListener;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.PeerDaemonUser;
|
||||
import com.google.gerrit.server.config.SitePaths;
|
||||
import com.google.gerrit.server.util.IdGenerator;
|
||||
import com.google.gerrit.sshd.SshScope.Context;
|
||||
@@ -179,6 +180,10 @@ class SshLog implements LifecycleListener {
|
||||
IdentifiedUser u = (IdentifiedUser) user;
|
||||
userName = u.getAccount().getUserName();
|
||||
accountId = "a/" + u.getAccountId().toString();
|
||||
|
||||
} else if (user instanceof PeerDaemonUser) {
|
||||
userName = PeerDaemonUser.USER_NAME;
|
||||
|
||||
}
|
||||
|
||||
event.setProperty(P_USER_NAME, userName);
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.google.gerrit.reviewdb.AccountGroup;
|
||||
import com.google.gerrit.reviewdb.PatchSet;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.PeerDaemonUser;
|
||||
import com.google.gerrit.server.RemotePeer;
|
||||
import com.google.gerrit.server.config.FactoryModule;
|
||||
import com.google.gerrit.server.config.GerritRequestModule;
|
||||
@@ -66,6 +67,7 @@ public class SshModule extends FactoryModule {
|
||||
bind(SshInfo.class).to(SshDaemon.class).in(SINGLETON);
|
||||
factory(DispatchCommand.Factory.class);
|
||||
factory(QueryShell.Factory.class);
|
||||
factory(PeerDaemonUser.Factory.class);
|
||||
|
||||
bind(DispatchCommandProvider.class).annotatedWith(Commands.CMD_ROOT)
|
||||
.toInstance(new DispatchCommandProvider(NAME, Commands.CMD_ROOT));
|
||||
|
||||
Reference in New Issue
Block a user