Search in sources :

Example 1 with UserSchSessions

use of io.bastillion.manage.model.UserSchSessions in project KeyBox by skavanagh.

the class SecureShellKtrl method setPtyType.

@Kontrol(path = "/admin/setPtyType", method = MethodType.GET)
public String setPtyType() throws ServletException {
    Long sessionId = null;
    try {
        sessionId = AuthUtil.getSessionId(getRequest().getSession());
    } catch (GeneralSecurityException ex) {
        log.error(ex.toString(), ex);
        throw new ServletException(ex.toString(), ex);
    }
    if (SecureShellKtrl.getUserSchSessionMap() != null) {
        UserSchSessions userSchSessions = SecureShellKtrl.getUserSchSessionMap().get(sessionId);
        if (userSchSessions != null && userSchSessions.getSchSessionMap() != null) {
            SchSession schSession = userSchSessions.getSchSessionMap().get(id);
            ChannelShell channel = (ChannelShell) schSession.getChannel();
            channel.setPtySize((int) Math.floor(userSettings.getPtyWidth() / 8.0000), (int) Math.floor(userSettings.getPtyHeight() / 14.4166), userSettings.getPtyWidth(), userSettings.getPtyHeight());
            schSession.setChannel(channel);
        }
    }
    return null;
}
Also used : ServletException(javax.servlet.ServletException) GeneralSecurityException(java.security.GeneralSecurityException) UserSchSessions(io.bastillion.manage.model.UserSchSessions) SchSession(io.bastillion.manage.model.SchSession) ChannelShell(com.jcraft.jsch.ChannelShell) Kontrol(loophole.mvc.annotation.Kontrol)

Example 2 with UserSchSessions

use of io.bastillion.manage.model.UserSchSessions in project KeyBox by skavanagh.

the class SecureShellWS method onMessage.

@OnMessage
public void onMessage(String message) {
    if (session.isOpen() && StringUtils.isNotEmpty(message) && !"heartbeat".equals(message)) {
        try {
            Map jsonRoot = new Gson().fromJson(message, Map.class);
            String command = (String) jsonRoot.get("command");
            Integer keyCode = null;
            Double keyCodeDbl = (Double) jsonRoot.get("keyCode");
            if (keyCodeDbl != null) {
                keyCode = keyCodeDbl.intValue();
            }
            for (String idStr : (ArrayList<String>) jsonRoot.get("id")) {
                Integer id = Integer.parseInt(idStr);
                // get servletRequest.getSession() for user
                UserSchSessions userSchSessions = SecureShellKtrl.getUserSchSessionMap().get(sessionId);
                if (userSchSessions != null) {
                    SchSession schSession = userSchSessions.getSchSessionMap().get(id);
                    if (keyCode != null) {
                        if (keyMap.containsKey(keyCode)) {
                            schSession.getCommander().write(keyMap.get(keyCode));
                        }
                    } else {
                        schSession.getCommander().print(command);
                    }
                }
            }
            // update timeout
            AuthUtil.setTimeout(httpSession);
        } catch (IllegalStateException | JsonSyntaxException | IOException ex) {
            log.error(ex.toString(), ex);
        }
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) UserSchSessions(io.bastillion.manage.model.UserSchSessions) SchSession(io.bastillion.manage.model.SchSession) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with UserSchSessions

use of io.bastillion.manage.model.UserSchSessions in project KeyBox by skavanagh.

the class SecureShellKtrl method disconnectTerm.

@Kontrol(path = "/admin/disconnectTerm", method = MethodType.GET)
public String disconnectTerm() throws ServletException {
    Long sessionId = null;
    try {
        sessionId = AuthUtil.getSessionId(getRequest().getSession());
    } catch (GeneralSecurityException ex) {
        log.error(ex.toString(), ex);
        throw new ServletException(ex.toString(), ex);
    }
    if (SecureShellKtrl.getUserSchSessionMap() != null) {
        UserSchSessions userSchSessions = SecureShellKtrl.getUserSchSessionMap().get(sessionId);
        if (userSchSessions != null) {
            SchSession schSession = userSchSessions.getSchSessionMap().get(id);
            // disconnect ssh session
            if (schSession != null) {
                if (schSession.getChannel() != null)
                    schSession.getChannel().disconnect();
                if (schSession.getSession() != null)
                    schSession.getSession().disconnect();
                schSession.setChannel(null);
                schSession.setSession(null);
                schSession.setInputToChannel(null);
                schSession.setCommander(null);
                schSession.setOutFromChannel(null);
            }
            // remove from map
            userSchSessions.getSchSessionMap().remove(id);
        }
    }
    return null;
}
Also used : ServletException(javax.servlet.ServletException) GeneralSecurityException(java.security.GeneralSecurityException) UserSchSessions(io.bastillion.manage.model.UserSchSessions) SchSession(io.bastillion.manage.model.SchSession) Kontrol(loophole.mvc.annotation.Kontrol)

Example 4 with UserSchSessions

use of io.bastillion.manage.model.UserSchSessions in project KeyBox by skavanagh.

the class SSHUtil method openSSHTermOnSystem.

/**
 * open new ssh session on host system
 *
 * @param passphrase     key passphrase for instance
 * @param password       password for instance
 * @param userId         user id
 * @param sessionId      session id
 * @param hostSystem     host system
 * @param userSessionMap user session map
 * @return status of systems
 */
public static HostSystem openSSHTermOnSystem(String passphrase, String password, Long userId, Long sessionId, HostSystem hostSystem, Map<Long, UserSchSessions> userSessionMap) throws SQLException, GeneralSecurityException {
    JSch jsch = new JSch();
    int instanceId = getNextInstanceId(sessionId, userSessionMap);
    hostSystem.setStatusCd(HostSystem.SUCCESS_STATUS);
    hostSystem.setInstanceId(instanceId);
    SchSession schSession = null;
    try {
        ApplicationKey appKey = PrivateKeyDB.getApplicationKey();
        // check to see if passphrase has been provided
        if (passphrase == null || passphrase.trim().equals("")) {
            passphrase = appKey.getPassphrase();
            // check for null inorder to use key without passphrase
            if (passphrase == null) {
                passphrase = "";
            }
        }
        // add private key
        jsch.addIdentity(appKey.getId().toString(), appKey.getPrivateKey().trim().getBytes(), appKey.getPublicKey().getBytes(), passphrase.getBytes());
        // create session
        Session session = jsch.getSession(hostSystem.getUser(), hostSystem.getHost(), hostSystem.getPort());
        // set password if it exists
        if (password != null && !password.trim().equals("")) {
            session.setPassword(password);
        }
        session.setConfig("StrictHostKeyChecking", "no");
        session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
        session.setServerAliveInterval(SERVER_ALIVE_INTERVAL);
        session.connect(SESSION_TIMEOUT);
        Channel channel = session.openChannel("shell");
        if ("true".equals(AppConfig.getProperty("agentForwarding"))) {
            ((ChannelShell) channel).setAgentForwarding(true);
        }
        ((ChannelShell) channel).setPtyType("xterm");
        InputStream outFromChannel = channel.getInputStream();
        // new session output
        SessionOutput sessionOutput = new SessionOutput(sessionId, hostSystem);
        Runnable run = new SecureShellTask(sessionOutput, outFromChannel);
        Thread thread = new Thread(run);
        thread.start();
        OutputStream inputToChannel = channel.getOutputStream();
        PrintStream commander = new PrintStream(inputToChannel, true);
        channel.connect();
        schSession = new SchSession();
        schSession.setUserId(userId);
        schSession.setSession(session);
        schSession.setChannel(channel);
        schSession.setCommander(commander);
        schSession.setInputToChannel(inputToChannel);
        schSession.setOutFromChannel(outFromChannel);
        schSession.setHostSystem(hostSystem);
        // refresh keys for session
        addPubKey(hostSystem, session, appKey.getPublicKey());
    } catch (JSchException | IOException | GeneralSecurityException ex) {
        log.info(ex.toString(), ex);
        hostSystem.setErrorMsg(ex.getMessage());
        if (ex.getMessage().toLowerCase().contains("userauth fail")) {
            hostSystem.setStatusCd(HostSystem.PUBLIC_KEY_FAIL_STATUS);
        } else if (ex.getMessage().toLowerCase().contains("auth fail") || ex.getMessage().toLowerCase().contains("auth cancel")) {
            hostSystem.setStatusCd(HostSystem.AUTH_FAIL_STATUS);
        } else if (ex.getMessage().toLowerCase().contains("unknownhostexception")) {
            hostSystem.setErrorMsg("DNS Lookup Failed");
            hostSystem.setStatusCd(HostSystem.HOST_FAIL_STATUS);
        } else {
            hostSystem.setStatusCd(HostSystem.GENERIC_FAIL_STATUS);
        }
    }
    // add session to map
    if (hostSystem.getStatusCd().equals(HostSystem.SUCCESS_STATUS)) {
        // get the server maps for user
        UserSchSessions userSchSessions = userSessionMap.get(sessionId);
        // if no user session create a new one
        if (userSchSessions == null) {
            userSchSessions = new UserSchSessions();
        }
        Map<Integer, SchSession> schSessionMap = userSchSessions.getSchSessionMap();
        // add server information
        schSessionMap.put(instanceId, schSession);
        userSchSessions.setSchSessionMap(schSessionMap);
        // add back to map
        userSessionMap.put(sessionId, userSchSessions);
    }
    SystemStatusDB.updateSystemStatus(hostSystem, userId);
    SystemDB.updateSystem(hostSystem);
    return hostSystem;
}
Also used : JSchException(com.jcraft.jsch.JSchException) ApplicationKey(io.bastillion.manage.model.ApplicationKey) PrintStream(java.io.PrintStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Channel(com.jcraft.jsch.Channel) OutputStream(java.io.OutputStream) GeneralSecurityException(java.security.GeneralSecurityException) ChannelShell(com.jcraft.jsch.ChannelShell) SecureShellTask(io.bastillion.manage.task.SecureShellTask) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) SessionOutput(io.bastillion.manage.model.SessionOutput) SchSession(io.bastillion.manage.model.SchSession) UserSchSessions(io.bastillion.manage.model.UserSchSessions) SchSession(io.bastillion.manage.model.SchSession) Session(com.jcraft.jsch.Session)

Example 5 with UserSchSessions

use of io.bastillion.manage.model.UserSchSessions in project KeyBox by skavanagh.

the class SecureShellWS method onClose.

@OnClose
public void onClose() {
    if (SecureShellKtrl.getUserSchSessionMap() != null) {
        UserSchSessions userSchSessions = SecureShellKtrl.getUserSchSessionMap().get(sessionId);
        if (userSchSessions != null) {
            Map<Integer, SchSession> schSessionMap = userSchSessions.getSchSessionMap();
            for (Integer sessionKey : schSessionMap.keySet()) {
                SchSession schSession = schSessionMap.get(sessionKey);
                // disconnect ssh session
                schSession.getChannel().disconnect();
                schSession.getSession().disconnect();
                schSession.setChannel(null);
                schSession.setSession(null);
                schSession.setInputToChannel(null);
                schSession.setCommander(null);
                schSession.setOutFromChannel(null);
                schSession = null;
                // remove from map
                schSessionMap.remove(sessionKey);
            }
            // clear and remove session map for user
            schSessionMap.clear();
            SecureShellKtrl.getUserSchSessionMap().remove(sessionId);
            SessionOutputUtil.removeUserSession(sessionId);
        }
    }
}
Also used : UserSchSessions(io.bastillion.manage.model.UserSchSessions) SchSession(io.bastillion.manage.model.SchSession)

Aggregations

SchSession (io.bastillion.manage.model.SchSession)5 UserSchSessions (io.bastillion.manage.model.UserSchSessions)5 GeneralSecurityException (java.security.GeneralSecurityException)3 ChannelShell (com.jcraft.jsch.ChannelShell)2 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 Kontrol (loophole.mvc.annotation.Kontrol)2 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 Channel (com.jcraft.jsch.Channel)1 JSch (com.jcraft.jsch.JSch)1 JSchException (com.jcraft.jsch.JSchException)1 Session (com.jcraft.jsch.Session)1 ApplicationKey (io.bastillion.manage.model.ApplicationKey)1 SessionOutput (io.bastillion.manage.model.SessionOutput)1 SecureShellTask (io.bastillion.manage.task.SecureShellTask)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1