Search in sources :

Example 11 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class SFTPv3Client method readLink.

/**
	 * Read the target of a symbolic link.
	 * 
	 * @param path See the {@link SFTPv3Client comment} for the class for more details.
	 * @return The target of the link.
	 * @throws IOException
	 */
public String readLink(String path) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_READLINK...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_READLINK, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    if (debug != null) {
        debug.println("Got REPLY.");
        debug.flush();
    }
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_NAME) {
        int count = tr.readUINT32();
        if (count != 1)
            throw new IOException("The server sent an invalid SSH_FXP_NAME packet.");
        return tr.readString(charsetName);
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 12 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class SFTPv3Client method init.

private void init() throws IOException {
    /* Send SSH_FXP_INIT (version 3) */
    final int client_version = 3;
    if (debug != null)
        debug.println("Sending SSH_FXP_INIT (" + client_version + ")...");
    TypesWriter tw = new TypesWriter();
    tw.writeUINT32(client_version);
    sendMessage(Packet.SSH_FXP_INIT, 0, tw.getBytes());
    if (debug != null)
        debug.println("Waiting for SSH_FXP_VERSION...");
    TypesReader tr = new TypesReader(receiveMessage(34000));
    /* Should be enough for any reasonable server */
    int type = tr.readByte();
    if (type != Packet.SSH_FXP_VERSION) {
        throw new IOException("The server did not send a SSH_FXP_VERSION packet (got " + type + ")");
    }
    protocol_version = tr.readUINT32();
    if (debug != null)
        debug.println("SSH_FXP_VERSION: protocol_version = " + protocol_version);
    if (protocol_version != 3)
        throw new IOException("Server version " + protocol_version + " is currently not supported");
    while (tr.remain() != 0) {
        String name = tr.readString();
        byte[] value = tr.readByteString();
        server_extensions.put(name, value);
        if (debug != null)
            debug.println("SSH_FXP_VERSION: extension: " + name + " = '" + expandString(value, 0, value.length) + "'");
    }
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 13 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class SFTPv3Client method fstat.

/**
	 * Retrieve the file attributes of an open file.
	 * 
	 * @param handle a SFTPv3FileHandle handle.
	 * @return a SFTPv3FileAttributes object.
	 * @throws IOException
	 */
public SFTPv3FileAttributes fstat(SFTPv3FileHandle handle) throws IOException {
    checkHandleValidAndOpen(handle);
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
    if (debug != null) {
        debug.println("Sending SSH_FXP_FSTAT...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    if (debug != null) {
        debug.println("Got REPLY.");
        debug.flush();
    }
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_ATTRS) {
        return readAttrs(tr);
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 14 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class SFTPv3Client method openFile.

private SFTPv3FileHandle openFile(String fileName, int flags, SFTPv3FileAttributes attr) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(fileName, charsetName);
    tw.writeUINT32(flags);
    tw.writeBytes(createAttrs(attr));
    if (debug != null) {
        debug.println("Sending SSH_FXP_OPEN...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_OPEN, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_HANDLE) {
        if (debug != null) {
            debug.println("Got SSH_FXP_HANDLE.");
            debug.flush();
        }
        return new SFTPv3FileHandle(this, tr.readByteString());
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    String errorMessage = tr.readString();
    throw new SFTPException(errorMessage, errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 15 with TypesReader

use of com.trilead.ssh2.packets.TypesReader in project intellij-community by JetBrains.

the class DSASHA1Verify method decodeSSHDSASignature.

public static DSASignature decodeSSHDSASignature(byte[] sig) throws IOException {
    byte[] rsArray = null;
    if (sig.length == 40) {
        /* OK, another broken SSH server. */
        rsArray = sig;
    } else {
        /* Hopefully a server obeing the standard... */
        TypesReader tr = new TypesReader(sig);
        String sig_format = tr.readString();
        if (sig_format.equals("ssh-dss") == false)
            throw new IOException("Peer sent wrong signature format");
        rsArray = tr.readByteString();
        if (rsArray.length != 40)
            throw new IOException("Peer sent corrupt signature");
        if (tr.remain() != 0)
            throw new IOException("Padding in DSA signature!");
    }
    /* Remember, s and r are unsigned ints. */
    byte[] tmp = new byte[20];
    System.arraycopy(rsArray, 0, tmp, 0, 20);
    BigInteger r = new BigInteger(1, tmp);
    System.arraycopy(rsArray, 20, tmp, 0, 20);
    BigInteger s = new BigInteger(1, tmp);
    if (log.isEnabled()) {
        log.log(30, "decoded ssh-dss signature: first bytes r(" + ((rsArray[0]) & 0xff) + "), s(" + ((rsArray[20]) & 0xff) + ")");
    }
    return new DSASignature(r, s);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) BigInteger(java.math.BigInteger) IOException(java.io.IOException)

Aggregations

TypesReader (com.trilead.ssh2.packets.TypesReader)16 TypesWriter (com.trilead.ssh2.packets.TypesWriter)10 IOException (java.io.IOException)5 BigInteger (java.math.BigInteger)4 Vector (java.util.Vector)1