2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.callhome.protocol;
11 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.net.InetSocketAddress;
14 import java.net.SocketAddress;
15 import java.security.PublicKey;
16 import org.apache.sshd.ClientSession;
17 import org.apache.sshd.SshClient;
18 import org.apache.sshd.client.ServerKeyVerifier;
19 import org.apache.sshd.client.SessionFactory;
20 import org.apache.sshd.client.future.AuthFuture;
21 import org.apache.sshd.client.session.ClientSessionImpl;
22 import org.apache.sshd.common.KeyExchange;
23 import org.apache.sshd.common.Session;
24 import org.apache.sshd.common.SessionListener;
25 import org.apache.sshd.common.future.SshFutureListener;
26 import org.apache.sshd.common.io.IoAcceptor;
27 import org.apache.sshd.common.io.IoServiceFactory;
28 import org.apache.sshd.common.io.mina.MinaServiceFactory;
29 import org.apache.sshd.common.io.nio2.Nio2ServiceFactory;
30 import org.opendaylight.netconf.callhome.protocol.CallHomeSessionContext.Factory;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class NetconfCallHomeServer implements AutoCloseable, ServerKeyVerifier {
36 private static final Logger LOG = LoggerFactory.getLogger(NetconfCallHomeServer.class);
38 private final IoAcceptor acceptor;
39 private final SshClient client;
40 private final CallHomeAuthorizationProvider authProvider;
41 private final CallHomeSessionContext.Factory sessionFactory;
42 private final InetSocketAddress bindAddress;
43 private final StatusRecorder recorder;
45 NetconfCallHomeServer(SshClient sshClient, CallHomeAuthorizationProvider authProvider, Factory factory,
46 InetSocketAddress socketAddress, StatusRecorder recorder) {
47 this.client = Preconditions.checkNotNull(sshClient);
48 this.authProvider = Preconditions.checkNotNull(authProvider);
49 this.sessionFactory = Preconditions.checkNotNull(factory);
50 this.bindAddress = socketAddress;
51 this.recorder = recorder;
53 sshClient.setServerKeyVerifier(this);
55 SessionFactory clientSessions = new SessionFactory();
56 clientSessions.setClient(sshClient);
57 clientSessions.addListener(createSessionListener());
59 IoServiceFactory minaFactory = createServiceFactory(sshClient);
60 this.acceptor = minaFactory.createAcceptor(clientSessions);
63 private IoServiceFactory createServiceFactory(SshClient sshClient) {
65 return createMinaServiceFactory(sshClient);
66 } catch (NoClassDefFoundError e) {
67 LOG.warn("Mina is not available, defaulting to NIO.");
68 return new Nio2ServiceFactory(sshClient);
72 protected IoServiceFactory createMinaServiceFactory(SshClient sshClient) {
73 return new MinaServiceFactory(sshClient);
76 SessionListener createSessionListener() {
77 return new SessionListener() {
79 public void sessionEvent(Session session, Event event) {
80 ClientSession clientSession = (ClientSession) session;
81 LOG.debug("SSH session {} event {}", session, event);
84 doAuth(clientSession);
87 doPostAuth(clientSession);
95 public void sessionCreated(Session session) {
96 LOG.debug("SSH session {} created", session);
100 public void sessionClosed(Session session) {
101 CallHomeSessionContext ctx = CallHomeSessionContext.getFrom((ClientSession) session);
105 LOG.debug("SSH Session {} closed", session);
110 private void doPostAuth(final ClientSession session) {
111 CallHomeSessionContext.getFrom(session).openNetconfChannel();
114 private void doAuth(final ClientSession session) {
116 final AuthFuture authFuture = CallHomeSessionContext.getFrom(session).authorize();
117 authFuture.addListener(newAuthSshFutureListener(session));
118 } catch (IOException e) {
119 LOG.error("Failed to authorize session {}", session, e);
123 private SshFutureListener<AuthFuture> newAuthSshFutureListener(final ClientSession session) {
124 return new SshFutureListener<AuthFuture>() {
126 public void operationComplete(AuthFuture authFuture) {
127 if (authFuture.isSuccess()) {
129 } else if (authFuture.isFailure()) {
130 onFailure(authFuture.getException());
131 } else if (authFuture.isCanceled()) {
134 authFuture.removeListener(this);
137 private void onSuccess() {
138 LOG.debug("Authorize success");
141 private void onFailure(Throwable throwable) {
142 ClientSessionImpl impl = (ClientSessionImpl) session;
143 LOG.error("Authorize failed for session {}", session, throwable);
145 KeyExchange kex = impl.getKex();
146 PublicKey key = kex.getServerKey();
147 recorder.reportFailedAuth(key);
152 private void onCanceled() {
153 LOG.warn("Authorize cancelled");
160 public boolean verifyServerKey(ClientSession sshClientSession, SocketAddress remoteAddress, PublicKey serverKey) {
161 final CallHomeAuthorization authorization = authProvider.provideAuth(remoteAddress, serverKey);
162 // server is not authorized
163 if (!authorization.isServerAllowed()) {
164 LOG.info("Incoming session {} was rejected by Authorization Provider.", sshClientSession);
167 CallHomeSessionContext session = sessionFactory.createIfNotExists(
168 sshClientSession, authorization, remoteAddress);
169 // Session was created, session with same name does not exists
170 if (session != null) {
173 // Session was not created, session with same name exists
174 LOG.info("Incoming session {} was rejected. Session with same name {} is already active.",
175 sshClientSession, authorization.getSessionName());
179 public void bind() throws IOException {
182 acceptor.bind(bindAddress);
183 } catch (IOException e) {
184 LOG.error("Unable to start NETCONF CallHome Service", e);
190 public void close() throws Exception {
191 acceptor.close(true);