Java Code Examples for org.apache.sshd.client.session.ClientSession#writePacket()

The following examples show how to use org.apache.sshd.client.session.ClientSession#writePacket() . 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: 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 2
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 3
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 4
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;
        }
    }
}