Java Code Examples for org.apache.sshd.common.util.buffer.Buffer#getString()

The following examples show how to use org.apache.sshd.common.util.buffer.Buffer#getString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientUserAuthServiceOld.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void process(int cmd, Buffer buffer) throws Exception {
    if (this.authFuture.isSuccess()) {
        throw new IllegalStateException("UserAuth message delivered to authenticated client");
    } else if (this.authFuture.isDone()) {
        log.debug("Ignoring random message");
        // ignore for now; TODO: random packets
    } else if (cmd == SshConstants.SSH_MSG_USERAUTH_BANNER) {
        String welcome = buffer.getString();
        String lang = buffer.getString();
        log.debug("Welcome banner[{}]: {}", lang, welcome);
        UserInteraction ui = session.getUserInteraction();
        if ((ui != null) && ui.isInteractionAllowed(session)) {
            ui.welcome(session, welcome, lang);
        }
    } else {
        buffer.rpos(buffer.rpos() - 1);
        processUserAuth(buffer);
    }
}
 
Example 2
Source File: ClientUserAuthServiceOld.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void process(int cmd, Buffer buffer) throws Exception {
    if (this.authFuture.isSuccess()) {
        throw new IllegalStateException("UserAuth message delivered to authenticated client");
    } else if (this.authFuture.isDone()) {
        log.debug("Ignoring random message");
        // ignore for now; TODO: random packets
    } else if (cmd == SshConstants.SSH_MSG_USERAUTH_BANNER) {
        String welcome = buffer.getString();
        String lang = buffer.getString();
        log.debug("Welcome banner[{}]: {}", lang, welcome);
        UserInteraction ui = session.getUserInteraction();
        if ((ui != null) && ui.isInteractionAllowed(session)) {
            ui.welcome(session, welcome, lang);
        }
    } else {
        buffer.rpos(buffer.rpos() - 1);
        processUserAuth(buffer);
    }
}
 
Example 3
Source File: UserAuthAgent.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public Result next(Buffer buffer) throws IOException {
    if (buffer == null) {
        if (keys.hasNext()) {
            sendNextKey(keys.next().getFirst());
            return Result.Continued;
        } else {
            agent.close();
            return Result.Failure;
        }
    } else {
        int cmd = buffer.getUByte();
        if (cmd == SshConstants.SSH_MSG_USERAUTH_SUCCESS) {
            log.info("Received SSH_MSG_USERAUTH_SUCCESS");
            agent.close();
            return Result.Success;
        }
        if (cmd == SshConstants.SSH_MSG_USERAUTH_FAILURE) {
            String methods = buffer.getString();
            boolean partial = buffer.getBoolean();
            if (log.isDebugEnabled()) {
                log.debug("Received SSH_MSG_USERAUTH_FAILURE - partial={}, methods={}", partial, methods);
            }
            if (keys.hasNext()) {
                sendNextKey(keys.next().getFirst());
                return Result.Continued;
            } else {
                agent.close();
                return Result.Failure;
            }
        } else {
            // TODO: check packets
            log.info("Received unknown packet: {}", Integer.valueOf(cmd));
            return Result.Continued;
        }
    }
}
 
Example 4
Source File: UserAuthPassword.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public Result next(Buffer buffer) throws IOException {
    ClientSession session = getClientSession();
    String service = getService();
    if (buffer == null) {
        log.debug("Send SSH_MSG_USERAUTH_REQUEST for password");
        buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
        buffer.putString(session.getUsername());
        buffer.putString(service);
        buffer.putString(UserAuthMethodFactory.PASSWORD);
        buffer.putBoolean(false);
        buffer.putString(password);
        session.writePacket(buffer);
        return Result.Continued;
    } else {
        int cmd = buffer.getUByte();
        if (cmd == SshConstants.SSH_MSG_USERAUTH_SUCCESS) {
            log.debug("Received SSH_MSG_USERAUTH_SUCCESS");
            return Result.Success;
        }
        if (cmd == SshConstants.SSH_MSG_USERAUTH_FAILURE) {
            String methods = buffer.getString();
            boolean partial = buffer.getBoolean();
            if (log.isDebugEnabled()) {
                log.debug("Received SSH_MSG_USERAUTH_FAILURE - partial={}, methods={}", partial, methods);
            }
            return Result.Failure;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Received unkown packet {}", Integer.valueOf(cmd & 0xFF));
            }
            // TODO: check packets
            return Result.Continued;
        }
    }
}
 
Example 5
Source File: UserAuthAgent.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public Result next(Buffer buffer) throws IOException {
    if (buffer == null) {
        if (keys.hasNext()) {
            sendNextKey(keys.next().getFirst());
            return Result.Continued;
        } else {
            agent.close();
            return Result.Failure;
        }
    } else {
        int cmd = buffer.getUByte();
        if (cmd == SshConstants.SSH_MSG_USERAUTH_SUCCESS) {
            log.info("Received SSH_MSG_USERAUTH_SUCCESS");
            agent.close();
            return Result.Success;
        }
        if (cmd == SshConstants.SSH_MSG_USERAUTH_FAILURE) {
            String methods = buffer.getString();
            boolean partial = buffer.getBoolean();
            if (log.isDebugEnabled()) {
                log.debug("Received SSH_MSG_USERAUTH_FAILURE - partial={}, methods={}", partial, methods);
            }
            if (keys.hasNext()) {
                sendNextKey(keys.next().getFirst());
                return Result.Continued;
            } else {
                agent.close();
                return Result.Failure;
            }
        } else {
            // TODO: check packets
            log.info("Received unknown packet: {}", Integer.valueOf(cmd));
            return Result.Continued;
        }
    }
}
 
Example 6
Source File: UserAuthPassword.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public Result next(Buffer buffer) throws IOException {
    ClientSession session = getClientSession();
    String service = getService();
    if (buffer == null) {
        log.debug("Send SSH_MSG_USERAUTH_REQUEST for password");
        buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
        buffer.putString(session.getUsername());
        buffer.putString(service);
        buffer.putString(UserAuthMethodFactory.PASSWORD);
        buffer.putBoolean(false);
        buffer.putString(password);
        session.writePacket(buffer);
        return Result.Continued;
    } else {
        int cmd = buffer.getUByte();
        if (cmd == SshConstants.SSH_MSG_USERAUTH_SUCCESS) {
            log.debug("Received SSH_MSG_USERAUTH_SUCCESS");
            return Result.Success;
        }
        if (cmd == SshConstants.SSH_MSG_USERAUTH_FAILURE) {
            String methods = buffer.getString();
            boolean partial = buffer.getBoolean();
            if (log.isDebugEnabled()) {
                log.debug("Received SSH_MSG_USERAUTH_FAILURE - partial={}, methods={}", partial, methods);
            }
            return Result.Failure;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Received unkown packet {}", Integer.valueOf(cmd & 0xFF));
            }
            // TODO: check packets
            return Result.Continued;
        }
    }
}
 
Example 7
Source File: UserAuthKeyboardInteractive.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public Result next(Buffer buffer) throws IOException {
    ClientSession session = getClientSession();
    String service = getService();
    if (buffer == null) {
        log.debug("Send SSH_MSG_USERAUTH_REQUEST for password");
        buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
        buffer.putString(session.getUsername());
        buffer.putString(service);
        buffer.putString("keyboard-interactive");
        buffer.putString("");
        buffer.putString("");
        session.writePacket(buffer);
        return Result.Continued;
    } else {
        int cmd = buffer.getUByte();
        switch (cmd) {
            case SshConstants.SSH_MSG_USERAUTH_INFO_REQUEST: {
                String name = buffer.getString();
                String instruction = buffer.getString();
                String language_tag = buffer.getString();
                if (log.isDebugEnabled()) {
                    log.debug("next({}) Received SSH_MSG_USERAUTH_INFO_REQUEST - name={}, instruction={}, lang={}",
                             session, name, instruction, language_tag);
                }
                int num = buffer.getInt();
                String[] prompt = new String[num];
                boolean[] echo = new boolean[num];
                for (int i = 0; i < num; i++) {
                    prompt[i] = buffer.getString();
                    echo[i] = buffer.getBoolean();
                }
                log.debug("Promt: {}", Arrays.toString(prompt));
                log.debug("Echo: {}", echo);

                String[] rep = null;
                if (num == 0) {
                    rep = new String[0];
                } else if (num == 1 && password != null && !echo[0] && prompt[0].toLowerCase().startsWith("password:")) {
                    rep = new String[]{password};
                } else {
                    UserInteraction ui = session.getUserInteraction();
                    if ((ui != null) && ui.isInteractionAllowed(session)) {
                        rep = ui.interactive(session, name, instruction, language_tag, prompt, echo);
                    }
                }
                if (rep == null) {
                    return Result.Failure;
                }

                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_RESPONSE);
                buffer.putInt(rep.length);
                for (String r : rep) {
                    buffer.putString(r);
                }
                session.writePacket(buffer);
                return Result.Continued;
            }
            case SshConstants.SSH_MSG_USERAUTH_SUCCESS:
                log.debug("Received SSH_MSG_USERAUTH_SUCCESS");
                return Result.Success;
            case SshConstants.SSH_MSG_USERAUTH_FAILURE:
                {
                    String methods = buffer.getString();
                    boolean partial = buffer.getBoolean();
                    if (log.isDebugEnabled()) {
                        log.debug("Received SSH_MSG_USERAUTH_FAILURE - partial={}, methods={}", partial, methods);
                    }
                    return Result.Failure;
                }
            default:
                log.debug("Received unknown packet {}", Integer.valueOf(cmd));
                return Result.Continued;
        }
    }
}
 
Example 8
Source File: UserAuthKeyboardInteractive.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public Result next(Buffer buffer) throws IOException {
    ClientSession session = getClientSession();
    String service = getService();
    if (buffer == null) {
        log.debug("Send SSH_MSG_USERAUTH_REQUEST for password");
        buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
        buffer.putString(session.getUsername());
        buffer.putString(service);
        buffer.putString("keyboard-interactive");
        buffer.putString("");
        buffer.putString("");
        session.writePacket(buffer);
        return Result.Continued;
    } else {
        int cmd = buffer.getUByte();
        switch (cmd) {
            case SshConstants.SSH_MSG_USERAUTH_INFO_REQUEST: {
                String name = buffer.getString();
                String instruction = buffer.getString();
                String language_tag = buffer.getString();
                if (log.isDebugEnabled()) {
                    log.debug("next({}) Received SSH_MSG_USERAUTH_INFO_REQUEST - name={}, instruction={}, lang={}",
                             session, name, instruction, language_tag);
                }
                int num = buffer.getInt();
                String[] prompt = new String[num];
                boolean[] echo = new boolean[num];
                for (int i = 0; i < num; i++) {
                    prompt[i] = buffer.getString();
                    echo[i] = buffer.getBoolean();
                }
                log.debug("Promt: {}", Arrays.toString(prompt));
                log.debug("Echo: {}", echo);

                String[] rep = null;
                if (num == 0) {
                    rep = new String[0];
                } else if (num == 1 && password != null && !echo[0] && prompt[0].toLowerCase().startsWith("password:")) {
                    rep = new String[]{password};
                } else {
                    UserInteraction ui = session.getUserInteraction();
                    if ((ui != null) && ui.isInteractionAllowed(session)) {
                        rep = ui.interactive(session, name, instruction, language_tag, prompt, echo);
                    }
                }
                if (rep == null) {
                    return Result.Failure;
                }

                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_RESPONSE);
                buffer.putInt(rep.length);
                for (String r : rep) {
                    buffer.putString(r);
                }
                session.writePacket(buffer);
                return Result.Continued;
            }
            case SshConstants.SSH_MSG_USERAUTH_SUCCESS:
                log.debug("Received SSH_MSG_USERAUTH_SUCCESS");
                return Result.Success;
            case SshConstants.SSH_MSG_USERAUTH_FAILURE:
                {
                    String methods = buffer.getString();
                    boolean partial = buffer.getBoolean();
                    if (log.isDebugEnabled()) {
                        log.debug("Received SSH_MSG_USERAUTH_FAILURE - partial={}, methods={}", partial, methods);
                    }
                    return Result.Failure;
                }
            default:
                log.debug("Received unknown packet {}", Integer.valueOf(cmd));
                return Result.Continued;
        }
    }
}