Search in sources :

Example 1 with Buffer

use of org.apache.sshd.common.util.Buffer in project platformlayer by platformlayer.

the class BugFixChannelExec method pumpInputStream.

// I hit this bug (100% CPU); it's fixed in the latest SVN.
// 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);
            // keep buffer position to write data length later
            int wpos1 = buffer.wpos();
            buffer.putInt(0);
            // keep buffer position for data write
            int wpos2 = buffer.wpos();
            // Make room
            buffer.wpos(wpos2 + remoteWindow.getPacketSize());
            // read data into buffer
            int len = securedRead(in, buffer.array(), wpos2, remoteWindow.getPacketSize());
            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!!
                sendEof();
                break;
            }
        }
    } catch (Exception e) {
        if (!closing) {
            log.info("Caught exception", e);
            close(false);
        }
    }
}
Also used : Buffer(org.apache.sshd.common.util.Buffer) IOException(java.io.IOException)

Example 2 with Buffer

use of org.apache.sshd.common.util.Buffer in project gerrit by GerritCodeReview.

the class ConvertKey method main.

public static void main(String[] args) throws GeneralSecurityException, JSchException, IOException {
    SimpleGeneratorHostKeyProvider p;
    if (args.length != 1) {
        System.err.println("Error: requires path to the SSH host key");
        return;
    } else {
        File file = new File(args[0]);
        if (!file.exists() || !file.isFile() || !file.canRead()) {
            System.err.println("Error: ssh key should exist and be readable");
            return;
        }
    }
    p = new SimpleGeneratorHostKeyProvider();
    // Gerrit's SSH "simple" keys are always RSA.
    p.setPath(args[0]);
    p.setAlgorithm("RSA");
    // forces the key to generate.
    Iterable<KeyPair> keys = p.loadKeys();
    for (KeyPair k : keys) {
        System.out.println("Public Key (" + k.getPublic().getAlgorithm() + "):");
        // From Gerrit's SshDaemon class; use JSch to get the public
        // key/type
        final Buffer buf = new Buffer();
        buf.putRawPublicKey(k.getPublic());
        final byte[] keyBin = buf.getCompactData();
        HostKey pub = new HostKey("localhost", keyBin);
        System.out.println(pub.getType() + " " + pub.getKey());
        System.out.println("Private Key:");
        // Use Bouncy Castle to write the private key back in PEM format
        // (PKCS#1)
        // http://stackoverflow.com/questions/25129822/export-rsa-public-key-to-pem-string-using-java
        StringWriter privout = new StringWriter();
        JcaPEMWriter privWriter = new JcaPEMWriter(privout);
        privWriter.writeObject(k.getPrivate());
        privWriter.close();
        System.out.println(privout);
    }
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) Buffer(org.apache.sshd.common.util.Buffer) KeyPair(java.security.KeyPair) HostKey(com.jcraft.jsch.HostKey) StringWriter(java.io.StringWriter) JcaPEMWriter(org.bouncycastle.openssl.jcajce.JcaPEMWriter) File(java.io.File)

Example 3 with Buffer

use of org.apache.sshd.common.util.Buffer in project platformlayer by platformlayer.

the class BugFixChannelExec method doOpen.

@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);
}
Also used : Buffer(org.apache.sshd.common.util.Buffer)

Aggregations

Buffer (org.apache.sshd.common.util.Buffer)3 HostKey (com.jcraft.jsch.HostKey)1 File (java.io.File)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 KeyPair (java.security.KeyPair)1 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)1 JcaPEMWriter (org.bouncycastle.openssl.jcajce.JcaPEMWriter)1