Package org.apache.sshd.common.util

Examples of org.apache.sshd.common.util.Buffer


  // TODO: Remove BugFixChannelExec when the if (len < 0) sendEof() logic is in mina-sshd.
  @Override
  protected void pumpInputStream() {
    try {
      while (!closeFuture.isClosed()) {
        Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_DATA, 0);
        buffer.putInt(recipient);
        int wpos1 = buffer.wpos(); // keep buffer position to write data length later
        buffer.putInt(0);
        int wpos2 = buffer.wpos(); // keep buffer position for data write
        buffer.wpos(wpos2 + remoteWindow.getPacketSize()); // Make room
        int len = securedRead(in, buffer.array(), wpos2, remoteWindow.getPacketSize()); // read data into buffer
        if (len > 0) {
          buffer.wpos(wpos1);
          buffer.putInt(len);
          buffer.wpos(wpos2 + len);
          remoteWindow.waitAndConsume(len);
          log.debug("Send SSH_MSG_CHANNEL_DATA on channel {}", id);
          session.writePacket(buffer);
        } else {
          // This is the bug fix we need!!
View Full Code Here


  @Override
  protected void doOpen() throws Exception {
    super.doOpen();

    Buffer buffer;

    if (agentForwarding) {
      log.info("Send agent forwarding request");
      buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
      buffer.putInt(recipient);
      buffer.putString("auth-agent-req@openssh.com");
      buffer.putBoolean(false);
      session.writePacket(buffer);
    }

    log.info("Send SSH_MSG_CHANNEL_REQUEST exec");
    buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
    buffer.putInt(recipient);
    buffer.putString("exec");
    buffer.putBoolean(false);
    buffer.putString(command);
    session.writePacket(buffer);

  }
View Full Code Here

        final int rmpsize = buffer.getInt();

        log.debug("Received SSH_MSG_CHANNEL_OPEN {}", type);

        if (closing) {
            Buffer buf = createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
            buf.putInt(id);
            buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
            buf.putString("SSH server is shutting down: " + type);
            buf.putString("");
            writePacket(buf);
            return;
        }
        if (!allowMoreSessions) {
            Buffer buf = createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
            buf.putInt(id);
            buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
            buf.putString("additional sessions disabled");
            buf.putString("");
            writePacket(buf);
            return;
        }

        final Channel channel = NamedFactory.Utils.create(getServerFactoryManager().getChannelFactories(), type);
        if (channel == null) {
            Buffer buf = createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
            buf.putInt(id);
            buf.putInt(SshConstants.SSH_OPEN_UNKNOWN_CHANNEL_TYPE);
            buf.putString("Unsupported channel type: " + type);
            buf.putString("");
            writePacket(buf);
            return;
        }

        final int channelId = getNextChannelId();
        channels.put(channelId, channel);
        channel.init(this, channelId);
        channel.open(id, rwsize, rmpsize, buffer).addListener(new SshFutureListener<OpenFuture>() {
            public void operationComplete(OpenFuture future) {
                try {
                    if (future.isOpened()) {
                        Buffer buf = createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, 0);
                        buf.putInt(id);
                        buf.putInt(channelId);
                        buf.putInt(channel.getLocalWindow().getSize());
                        buf.putInt(channel.getLocalWindow().getPacketSize());
                        writePacket(buf);
                    } else if (future.getException() != null) {
                        Buffer buf = createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
                        buf.putInt(id);
                        if (future.getException() instanceof OpenChannelException) {
                            buf.putInt(((OpenChannelException) future.getException()).getReasonCode());
                            buf.putString(future.getException().getMessage());
                        } else {
                            buf.putInt(0);
                            buf.putString("Error opening channel: " + future.getException().getMessage());
                        }
                        buf.putString("");
                        writePacket(buf);
                    }
                } catch (IOException e) {
                    exceptionCaught(e);
                }
View Full Code Here

        if (!authenticator.authenticate(username, key, session)) {
            return false;
        }
        if (!hasSig) {
            Buffer buf = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_PK_OK, 0);
            buf.putString(alg);
            buf.putRawBytes(buffer.array(), oldPos, 4 + len);
            session.writePacket(buf);
            return null;
        } else {
            Buffer buf = new Buffer();
            buf.putString(session.getKex().getH());
            buf.putCommand(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST);
            buf.putString(username);
            buf.putString("ssh-connection");
            buf.putString("publickey");
            buf.putByte((byte) 1);
            buf.putString(keyAlg);
            buffer.rpos(oldPos);
            buffer.wpos(oldPos + 4 + len);
            buf.putBuffer(buffer);
            verif.update(buf.array(), buf.rpos(), buf.available());
            if (!verif.verify(sig)) {
                throw new Exception("Key verification failed");
            }
            return true;
        }
View Full Code Here

                    context = mgr.createContext(creds);

                    // Send the matching mechanism back to the client

                    Buffer b = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_INFO_REQUEST, 0);
                    byte[] out = oid.getDER();

                    b.putBytes(out);
                    session.writePacket(b);

                    return null;
                }
            }

            // No matching mechanism found

            return Boolean.FALSE;
        }
        else
        {
            SshConstants.Message msg = buffer.getCommand();
            if (!(msg == SshConstants.Message.SSH_MSG_USERAUTH_INFO_RESPONSE ||
                    msg == SshConstants.Message.SSH_MSG_USERAUTH_GSSAPI_MIC && context.isEstablished())) {
                throw new SshException(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR,
                        "Packet not supported by user authentication method");
            }

            log.debug("In krb5.next: msg = " + msg);

            // If the context is established, this must be a MIC message

            if (context.isEstablished()) {

                if (msg != SshConstants.Message.SSH_MSG_USERAUTH_GSSAPI_MIC) {
                    return Boolean.FALSE;
                }

                // Make the MIC message so the token can be verified

                Buffer msgbuf = new Buffer();

                msgbuf.putString(session.getSessionId());
                msgbuf.putByte(SshConstants.Message.SSH_MSG_USERAUTH_REQUEST.toByte());
                msgbuf.putString(username.getBytes("UTF-8"));
                msgbuf.putString(service);
                msgbuf.putString("gssapi-with-mic");

                byte[] msgbytes = msgbuf.getCompactData();
                byte[] inmic = buffer.getBytes();

                try {
                    context.verifyMIC(inmic, 0, inmic.length, msgbytes, 0, msgbytes.length, new MessageProp(false));
                    log.debug("MIC verified");
                    return Boolean.TRUE;
                } catch (GSSException e) {
                    log.info("GSS verification error: {}", e.toString());
                    return Boolean.FALSE;
                }

            } else {

                // Not established - new token to process

                byte[] tok = buffer.getBytes();
                byte[] out = context.acceptSecContext(tok, 0, tok.length);
                boolean established = context.isEstablished();

                // Validate identity if context is now established

                if (established && identity == null) {
                    identity = context.getSrcName().toString();
                    log.info("GSS identity is {}", identity);

                    if (!auth.validateIdentity(session, identity)) {
                        return Boolean.FALSE;
                    }
                }

                // Send return token if necessary

                if (out != null && out.length > 0) {
                    Buffer b = session.createBuffer(SshConstants.Message.SSH_MSG_USERAUTH_INFO_RESPONSE, 0);

                    b.putBytes(out);
                    session.writePacket(b);
                    return null;
                } else {
                    return established;
                }
View Full Code Here

    protected void sendExitStatus(int v) throws IOException {
        if (!exitStatusSent) {
            exitStatusSent = true;
            log.debug("Send SSH_MSG_CHANNEL_REQUEST exit-status on channel {}", id);
            Buffer buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
            buffer.putInt(recipient);
            buffer.putString("exit-status");
            buffer.putByte((byte) 0);
            buffer.putInt(v);
            writePacket(buffer);
            notifyStateChanged();
        }
    }
View Full Code Here

            while (true) {
                int length = dis.readInt();
                if (length < 5) {
                    throw new IllegalArgumentException();
                }
                Buffer buffer = new Buffer(length + 4);
                buffer.putInt(length);
                int nb = length;
                while (nb > 0) {
                    int l = dis.read(buffer.array(), buffer.wpos(), nb);
                    if (l < 0) {
                        throw new IllegalArgumentException();
                    }
                    buffer.wpos(buffer.wpos() + l);
                    nb -= l;
                }
                process(buffer);
            }
        } catch (Throwable t) {
View Full Code Here

                    } else {
                        FileHandle fh = (FileHandle) p;
                        byte[] b = new byte[Math.min(len, Buffer.MAX_LEN)];
                        len = fh.read(b, offset);
                        if (len >= 0) {
                            Buffer buf = new Buffer(len + 5);
                            buf.putByte((byte) SSH_FXP_DATA);
                            buf.putInt(id);
                            buf.putBytes(b, 0, len);
                            send(buf);
                        } else {
                            sendStatus(id, SSH_FX_EOF, "");
                        }
                    }
View Full Code Here

            }
        }
    }

    protected void sendHandle(int id, String handle) throws IOException {
        Buffer buffer = new Buffer();
        buffer.putByte((byte) SSH_FXP_HANDLE);
        buffer.putInt(id);
        buffer.putString(handle);
        send(buffer);
    }
View Full Code Here

        buffer.putString(handle);
        send(buffer);
    }

    protected void sendAttrs(int id, SshFile file, boolean followLinks) throws IOException {
        Buffer buffer = new Buffer();
        buffer.putByte((byte) SSH_FXP_ATTRS);
        buffer.putInt(id);
        writeAttrs(buffer, file, followLinks);
        send(buffer);
    }
View Full Code Here

TOP

Related Classes of org.apache.sshd.common.util.Buffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.