Search in sources :

Example 1 with SchSession

use of io.bastillion.manage.model.SchSession 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 SchSession

use of io.bastillion.manage.model.SchSession 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 SchSession

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

the class SecureShellKtrl method setSystemList.

/**
 * set system list once all connections have been attempted
 *
 * @param userId    user id
 * @param sessionId session id
 */
private void setSystemList(Long userId, Long sessionId) throws ServletException {
    // check user map
    if (userSchSessionMap != null && !userSchSessionMap.isEmpty() && userSchSessionMap.get(sessionId) != null) {
        // get user sessions
        Map<Integer, SchSession> schSessionMap = userSchSessionMap.get(sessionId).getSchSessionMap();
        for (SchSession schSession : schSessionMap.values()) {
            // add to host system list
            systemList.add(schSession.getHostSystem());
            // run script it exists
            if (script != null && script.getId() != null && script.getId() > 0) {
                try {
                    script = ScriptDB.getScript(script.getId(), userId);
                    BufferedReader reader = new BufferedReader(new StringReader(script.getScript()));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        schSession.getCommander().println(line);
                    }
                } catch (SQLException | IOException | GeneralSecurityException ex) {
                    log.error(ex.toString(), ex);
                    throw new ServletException(ex.toString(), ex);
                }
            }
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) SQLException(java.sql.SQLException) SchSession(io.bastillion.manage.model.SchSession) GeneralSecurityException(java.security.GeneralSecurityException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 4 with SchSession

use of io.bastillion.manage.model.SchSession 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 5 with SchSession

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

the class UploadAndPushKtrl method push.

@Kontrol(path = "/admin/push", method = MethodType.POST)
public String push() throws ServletException {
    try {
        Long userId = AuthUtil.getUserId(getRequest().getSession());
        Long sessionId = AuthUtil.getSessionId(getRequest().getSession());
        // get next pending system
        pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
        if (pendingSystemStatus != null) {
            // get session for system
            SchSession session = null;
            for (Integer instanceId : SecureShellKtrl.getUserSchSessionMap().get(sessionId).getSchSessionMap().keySet()) {
                // if host system id matches pending system then upload
                if (pendingSystemStatus.getId().equals(SecureShellKtrl.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId).getHostSystem().getId())) {
                    session = SecureShellKtrl.getUserSchSessionMap().get(sessionId).getSchSessionMap().get(instanceId);
                }
            }
            if (session != null) {
                // push upload to system
                currentSystemStatus = SSHUtil.pushUpload(pendingSystemStatus, session.getSession(), UPLOAD_PATH + "/" + uploadFileName, pushDir + "/" + uploadFileName);
                // update system status
                SystemStatusDB.updateSystemStatus(currentSystemStatus, userId);
                pendingSystemStatus = SystemStatusDB.getNextPendingSystem(userId);
            }
        }
        // if push has finished to all servers then delete uploaded file
        if (pendingSystemStatus == null) {
            File delFile = new File(UPLOAD_PATH, uploadFileName);
            FileUtils.deleteQuietly(delFile);
            // delete all expired files in upload path
            File delDir = new File(UPLOAD_PATH);
            if (delDir.isDirectory()) {
                // set expire time to delete all files older than 48 hrs
                Calendar expireTime = Calendar.getInstance();
                expireTime.add(Calendar.HOUR, -48);
                Iterator<File> filesToDelete = FileUtils.iterateFiles(delDir, new AgeFileFilter(expireTime.getTime()), TrueFileFilter.TRUE);
                while (filesToDelete.hasNext()) {
                    delFile = filesToDelete.next();
                    delFile.delete();
                }
            }
        }
        hostSystemList = SystemStatusDB.getAllSystemStatus(userId);
    } catch (SQLException | GeneralSecurityException ex) {
        log.error(ex.toString(), ex);
        throw new ServletException(ex.toString(), ex);
    }
    // reset csrf token back since it's already set on page load
    getRequest().getSession().setAttribute(SecurityFilter._CSRF, getRequest().getParameter(SecurityFilter._CSRF));
    return "/admin/upload_result.html";
}
Also used : ServletException(javax.servlet.ServletException) AgeFileFilter(org.apache.commons.io.filefilter.AgeFileFilter) SQLException(java.sql.SQLException) SchSession(io.bastillion.manage.model.SchSession) Calendar(java.util.Calendar) GeneralSecurityException(java.security.GeneralSecurityException) File(java.io.File) Kontrol(loophole.mvc.annotation.Kontrol)

Aggregations

SchSession (io.bastillion.manage.model.SchSession)7 UserSchSessions (io.bastillion.manage.model.UserSchSessions)5 GeneralSecurityException (java.security.GeneralSecurityException)5 ServletException (javax.servlet.ServletException)4 IOException (java.io.IOException)3 Kontrol (loophole.mvc.annotation.Kontrol)3 ChannelShell (com.jcraft.jsch.ChannelShell)2 SQLException (java.sql.SQLException)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 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1