Package com.trilead.ssh2.packets

Examples of com.trilead.ssh2.packets.TypesReader


    return tw.getBytes();
  }

  public static RSASignature decodeSSHRSASignature(byte[] sig) throws IOException
  {
    TypesReader tr = new TypesReader(sig);

    String sig_format = tr.readString();

    if (sig_format.equals("ssh-rsa") == false)
      throw new IOException("Peer sent wrong signature format");

    /* S is NOT an MPINT. "The value for 'rsa_signature_blob' is encoded as a string
     * containing s (which is an integer, without lengths or padding, unsigned and in
     * network byte order)." See also below.
     */

    byte[] s = tr.readByteString();

    if (s.length == 0)
      throw new IOException("Error in RSA signature, S is empty.");

    if (log.isDebugEnabled())
    {
      log.debug("Decoding ssh-rsa signature string (length: " + s.length + ")");
    }

    if (tr.remain() != 0)
      throw new IOException("Padding in RSA signature!");

    return new RSASignature(new BigInteger(1, s));
  }
View Full Code Here


                continue;
            }

            if(type == Packets.SSH_MSG_DEBUG) {
                if(log.isDebugEnabled()) {
                    TypesReader tr = new TypesReader(msg, 0, msglen);
                    tr.readByte();
                    tr.readBoolean();
                    StringBuilder debugMessageBuffer = new StringBuilder();
                    debugMessageBuffer.append(tr.readString("UTF-8"));

                    for(int i = 0; i < debugMessageBuffer.length(); i++) {
                        char c = debugMessageBuffer.charAt(i);

                        if((c >= 32) && (c <= 126)) {
                            continue;
                        }
                        debugMessageBuffer.setCharAt(i, '\uFFFD');
                    }

                    log.debug("DEBUG Message from remote: '" + debugMessageBuffer.toString() + "'");
                }
                continue;
            }

            if(type == Packets.SSH_MSG_UNIMPLEMENTED) {
                throw new IOException("Peer sent UNIMPLEMENTED message, that should not happen.");
            }

            if(type == Packets.SSH_MSG_DISCONNECT) {
                TypesReader tr = new TypesReader(msg, 0, msglen);
                tr.readByte();
                int reason_code = tr.readUINT32();
                StringBuilder reasonBuffer = new StringBuilder();
                reasonBuffer.append(tr.readString("UTF-8"));

        /*
         * Do not get fooled by servers that send abnormal long error
         * messages
         */
 
View Full Code Here

    log.debug("Sending SSH_FXP_OPEN...");
    sendMessage(Packet.SSH_FXP_OPEN, req_id, tw.getBytes());

    byte[] resp = receiveMessage(34000);

    TypesReader tr = new TypesReader(resp);

    int t = tr.readByte();
    listener.read(Packet.forName(t));

    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)
    {
      log.debug("Got SSH_FXP_HANDLE.");
      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();
    listener.read(errorMessage);
    throw new SFTPException(errorMessage, errorCode);
  }
View Full Code Here

        break;
      }

      // Receive a single answer
      byte[] resp = receiveMessage(34000);
      TypesReader tr = new TypesReader(resp);

      int t = tr.readByte();
      listener.read(Packet.forName(t));

      // Search the pending queue
      OutstandingReadRequest req = pendingReadQueue.remove(tr.readUINT32());
      if (null == req)
      {
        throw new IOException("The server sent an invalid id field.");
      }
      // Evaluate the answer
      if (t == Packet.SSH_FXP_STATUS)
      {
        /* In any case, stop sending more packets */

        int code = tr.readUINT32();
        String msg = tr.readString();
        listener.read(msg);

        if (log.isDebugEnabled())
        {
          String[] desc = ErrorCodes.getDescription(code);
          log.debug("Got SSH_FXP_STATUS (" + req.req_id + ") (" + ((desc != null) ? desc[0] : "UNKNOWN") + ")");
        }
        // Flag to read all pending requests but don't send any more.
        errorOccured = true;
        if (pendingReadQueue.isEmpty())
        {
          if (ErrorCodes.SSH_FX_EOF == code)
          {
            return -1;
          }
          throw new SFTPException(msg, code);
        }
      }
      else if (t == Packet.SSH_FXP_DATA)
      {
        // OK, collect data
        int readLen = tr.readUINT32();

        if ((readLen < 0) || (readLen > req.len))
        {
          throw new IOException("The server sent an invalid length field in a SSH_FXP_DATA packet.");
        }

        if (log.isDebugEnabled())
        {
          log.debug("Got SSH_FXP_DATA (" + req.req_id + ") " + req.serverOffset + "/" + readLen
              + " (requested: " + req.len + ")");
        }

        // Read bytes into buffer
        tr.readBytes(req.buffer, req.dstOffset, readLen);

        if (readLen < req.len)
        {
          /* Send this request packet again to request the remaing data in this slot. */
          req.req_id = generateNextRequestID();
View Full Code Here

  private void readStatus() throws IOException
  {
    byte[] resp = receiveMessage(34000);

    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    listener.read(Packet.forName(t));

    // Search the pending queue
    OutstandingStatusRequest status = pendingStatusQueue.remove(tr.readUINT32());
    if (null == status)
    {
      throw new IOException("The server sent an invalid id field.");
    }

    // Evaluate the answer
    if (t == Packet.SSH_FXP_STATUS)
    {
      // In any case, stop sending more packets
      int code = tr.readUINT32();
      if (log.isDebugEnabled())
      {
        String[] desc = ErrorCodes.getDescription(code);
        log.debug("Got SSH_FXP_STATUS (" + status.req_id + ") (" + ((desc != null) ? desc[0] : "UNKNOWN") + ")");
      }
      if (code == ErrorCodes.SSH_FX_OK)
      {
        return;
      }
      String msg = tr.readString();
      listener.read(msg);
      throw new SFTPException(msg, code);
    }
    throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
  }
View Full Code Here

  private void readPendingReadStatus() throws IOException
  {
    byte[] resp = receiveMessage(34000);

    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    listener.read(Packet.forName(t));

    // Search the pending queue
        OutstandingReadRequest status = pendingReadQueue.remove(tr.readUINT32());
        if (null == status)
    {
      throw new IOException("The server sent an invalid id field.");
    }

    // Evaluate the answer
    if (t == Packet.SSH_FXP_STATUS)
    {
      // In any case, stop sending more packets
      int code = tr.readUINT32();
      if (log.isDebugEnabled())
      {
        String[] desc = ErrorCodes.getDescription(code);
        log.debug("Got SSH_FXP_STATUS (" + status.req_id + ") (" + ((desc != null) ? desc[0] : "UNKNOWN") + ")");
      }
      if (code == ErrorCodes.SSH_FX_OK)
      {
        return;
      }
            if (code == ErrorCodes.SSH_FX_EOF)
            {
                return;
            }
      String msg = tr.readString();
      listener.read(msg);
      throw new SFTPException(msg, code);
    }
    throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
  }
View Full Code Here

    log.debug("Got SSH_MSG_CHANNEL_WINDOW_ADJUST (channel " + id + ", " + windowChange + ")");
  }

  public void msgChannelOpen(byte[] msg, int msglen) throws IOException
  {
    TypesReader tr = new TypesReader(msg, 0, msglen);

    tr.readByte(); // skip packet type
    String channelType = tr.readString();
    int remoteID = tr.readUINT32(); /* sender channel */
    int remoteWindow = tr.readUINT32(); /* initial window size */
    int remoteMaxPacketSize = tr.readUINT32(); /* maximum packet size */

    if ("x11".equals(channelType))
    {
      synchronized (x11_magic_cookies)
      {
        /* If we did not request X11 forwarding, then simply ignore this bogus request. */

        if (x11_magic_cookies.size() == 0)
        {
          PacketChannelOpenFailure pcof = new PacketChannelOpenFailure(remoteID,
              Packets.SSH_OPEN_ADMINISTRATIVELY_PROHIBITED, "X11 forwarding not activated", "");

          tm.sendAsynchronousMessage(pcof.getPayload());

          log.warning("Unexpected X11 request, denying it!");

          return;
        }
      }

      String remoteOriginatorAddress = tr.readString();
      int remoteOriginatorPort = tr.readUINT32();

      Channel c = new Channel(this);

      synchronized (c)
      {
        c.remoteID = remoteID;
        c.remoteWindow = remoteWindow & 0xFFFFffffL; /* properly convert UINT32 to long */
        c.remoteMaxPacketSize = remoteMaxPacketSize;
        c.localID = addChannel(c);
      }

      /*
       * The open confirmation message will be sent from another thread
       */

      RemoteX11AcceptThread rxat = new RemoteX11AcceptThread(c, remoteOriginatorAddress, remoteOriginatorPort);
      rxat.setDaemon(true);
      rxat.start();

      return;
    }

    if ("forwarded-tcpip".equals(channelType))
    {
      String remoteConnectedAddress = tr.readString(); /* address that was connected */
      int remoteConnectedPort = tr.readUINT32(); /* port that was connected */
      String remoteOriginatorAddress = tr.readString(); /* originator IP address */
      int remoteOriginatorPort = tr.readUINT32(); /* originator port */

      RemoteForwardingData rfd = null;

      synchronized (remoteForwardings)
      {
View Full Code Here

    t.start();   
  }
 
  public void msgChannelRequest(byte[] msg, int msglen) throws IOException
  {
    TypesReader tr = new TypesReader(msg, 0, msglen);

    tr.readByte(); // skip packet type
    int id = tr.readUINT32();

    Channel c = getChannel(id);

    if (c == null)
      throw new IOException("Unexpected SSH_MSG_CHANNEL_REQUEST message for non-existent channel " + id);

    ServerSessionImpl server_session = null;

    if (server_state != null)
    {
      synchronized (c)
      {
        server_session = c.ss;
      }
    }

    String type = tr.readString("US-ASCII");
    boolean wantReply = tr.readBoolean();

    log.debug("Got SSH_MSG_CHANNEL_REQUEST (channel " + id + ", '" + type + "')");

    if (type.equals("exit-status"))
    {
      if (wantReply != false)
        throw new IOException(
            "Badly formatted SSH_MSG_CHANNEL_REQUEST exit-status message, 'want reply' is true");

      int exit_status = tr.readUINT32();

      if (tr.remain() != 0)
        throw new IOException("Badly formatted SSH_MSG_CHANNEL_REQUEST message");

      synchronized (c)
      {
        c.exit_status = new Integer(exit_status);
        c.notifyAll();
      }

      log.debug("Got EXIT STATUS (channel " + id + ", status " + exit_status + ")");

      return;
    }

    if ((server_state == null) && (type.equals("exit-signal")))
    {
      if (wantReply != false)
        throw new IOException(
            "Badly formatted SSH_MSG_CHANNEL_REQUEST exit-signal message, 'want reply' is true");

      String signame = tr.readString("US-ASCII");
      tr.readBoolean();
      tr.readString();
      tr.readString();

      if (tr.remain() != 0)
        throw new IOException("Badly formatted SSH_MSG_CHANNEL_REQUEST message");

      synchronized (c)
      {
        c.exit_signal = signame;
        c.notifyAll();
      }

      log.debug("Got EXIT SIGNAL (channel " + id + ", signal " + signame + ")");

      return;
    }

    if ((server_session != null) && (type.equals("pty-req")))
    {
      PtySettings pty = new PtySettings();

      pty.term = tr.readString();
      pty.term_width_characters = tr.readUINT32();
      pty.term_height_characters = tr.readUINT32();
      pty.term_width_pixels = tr.readUINT32();
      pty.term_height_pixels = tr.readUINT32();
      pty.terminal_modes = tr.readByteString();

      if (tr.remain() != 0)
        throw new IOException("Badly formatted SSH_MSG_CHANNEL_REQUEST message");
     
      Runnable run_after_sending_success = null;
     
      ServerSessionCallback sscb = server_session.getServerSessionCallback();

      if (sscb != null)
        run_after_sending_success = sscb.requestPtyReq(server_session, pty);

      if (wantReply)
      {
        if (run_after_sending_success != null)
        {
          tm.sendAsynchronousMessage(new PacketChannelSuccess(c.remoteID).getPayload());
        }
        else
        {
          tm.sendAsynchronousMessage(new PacketChannelFailure(c.remoteID).getPayload());
        }     
      }
     
      if (run_after_sending_success != null)
      {
        runAsync(run_after_sending_success);
      }
     
      return;
    }

    if ((server_session != null) && (type.equals("shell")))
    {
      if (tr.remain() != 0)
        throw new IOException("Badly formatted SSH_MSG_CHANNEL_REQUEST message");
     
      Runnable run_after_sending_success = null;
      ServerSessionCallback sscb = server_session.getServerSessionCallback();

      if (sscb != null)
        run_after_sending_success = sscb.requestShell(server_session);

      if (wantReply)
      {
        if (run_after_sending_success != null)
        {
          tm.sendAsynchronousMessage(new PacketChannelSuccess(c.remoteID).getPayload());
        }
        else
        {
          tm.sendAsynchronousMessage(new PacketChannelFailure(c.remoteID).getPayload());
        }
      }
     
      if (run_after_sending_success != null)
      {
        runAsync(run_after_sending_success);
      }
     
      return;
    }
   
    if ((server_session != null) && (type.equals("exec")))
    {
      String command = tr.readString();
     
      if (tr.remain() != 0)
        throw new IOException("Badly formatted SSH_MSG_CHANNEL_REQUEST message");
     
      Runnable run_after_sending_success = null;
      ServerSessionCallback sscb = server_session.getServerSessionCallback();
View Full Code Here

  public void msgChannelOpenFailure(byte[] msg, int msglen) throws IOException
  {
    if (msglen < 5)
      throw new IOException("SSH_MSG_CHANNEL_OPEN_FAILURE message has wrong size (" + msglen + ")");

    TypesReader tr = new TypesReader(msg, 0, msglen);

    tr.readByte(); // skip packet type
    int id = tr.readUINT32(); /* sender channel */

    Channel c = getChannel(id);

    if (c == null)
      throw new IOException("Unexpected SSH_MSG_CHANNEL_OPEN_FAILURE message for non-existent channel " + id);

    int reasonCode = tr.readUINT32();
    String description = tr.readString("UTF-8");

    String reasonCodeSymbolicName = null;

    switch (reasonCode)
    {
View Full Code Here

    log.debug("Sending SSH_FXP_FSTAT...");
    sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes());

    byte[] resp = receiveMessage(34000);

    TypesReader tr = new TypesReader(resp);

    int t = tr.readByte();
    listener.read(Packet.forName(t));

    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();
    String errorMessage = tr.readString();
    listener.read(errorMessage);
    throw new SFTPException(errorMessage, errorCode);
  }
View Full Code Here

TOP

Related Classes of com.trilead.ssh2.packets.TypesReader

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.