public Boolean auth(ServerSession session, String username, Buffer buffer) throws Exception {
   boolean newPassword = buffer.getBoolean();
   if (newPassword) {
     throw new IllegalStateException("Password changes are not supported");
   }
   String password = buffer.getString();
   return checkPassword(session, username, password);
 }
 protected void sendInitialServiceRequest() throws IOException {
   if (initialServiceRequestSent) {
     return;
   }
   initialServiceRequestSent = true;
   log.debug("Send SSH_MSG_SERVICE_REQUEST for {}", currentServiceFactory.getName());
   Buffer request = createBuffer(SshConstants.SSH_MSG_SERVICE_REQUEST);
   request.putString(currentServiceFactory.getName());
   writePacket(request);
   // Assuming that MINA-SSHD only implements "explicit server authentication" it is permissible
   // for the client's service to start sending data before the service-accept has been received.
   // If "implicit authentication" were to ever be supported, then this would need to be
   // called after service-accept comes back.  See SSH-TRANSPORT.
   currentService.start();
 }
  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");
    Iterable<KeyPair> keys = p.loadKeys(); // forces the key to generate.
    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);
    }
  }
Exemple #4
0
  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("*****@*****.**");
      buffer.putBoolean(false);
      session.writePacket(buffer);
    }

    if (usePty) {
      log.info("Send SSH_MSG_CHANNEL_REQUEST pty-req");
      buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
      buffer.putInt(recipient);
      buffer.putString("pty-req");
      buffer.putBoolean(false);
      buffer.putString(ptyType);
      buffer.putInt(ptyColumns);
      buffer.putInt(ptyLines);
      buffer.putInt(ptyHeight);
      buffer.putInt(ptyWidth);
      Buffer modes = new Buffer();
      for (PtyMode mode : ptyModes.keySet()) {
        modes.putByte((byte) mode.toInt());
        modes.putInt(ptyModes.get(mode));
      }
      modes.putByte((byte) 0);
      buffer.putBytes(modes.getCompactData());
      session.writePacket(buffer);
    }

    if (!env.isEmpty()) {
      log.info("Send SSH_MSG_CHANNEL_REQUEST env");
      for (Map.Entry<String, String> entry : env.entrySet()) {
        buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
        buffer.putInt(recipient);
        buffer.putString("env");
        buffer.putBoolean(false);
        buffer.putString(entry.getKey());
        buffer.putString(entry.getValue());
        session.writePacket(buffer);
      }
    }

    log.info("Send SSH_MSG_CHANNEL_REQUEST shell");
    buffer = session.createBuffer(SshConstants.Message.SSH_MSG_CHANNEL_REQUEST, 0);
    buffer.putInt(recipient);
    buffer.putString("shell");
    buffer.putBoolean(false);
    session.writePacket(buffer);
  }
Exemple #5
0
 protected void reply(Buffer buf) throws IOException {
   int result = Socket.send(socket, buf.array(), buf.rpos(), buf.available());
   if (result < Status.APR_SUCCESS) {
     throwException(result);
   }
 }
Exemple #6
0
 public void putBuffer(Buffer buffer) {
   int r = buffer.available();
   ensureCapacity(r);
   System.arraycopy(buffer.data, buffer.rpos, data, wpos, r);
   wpos += r;
 }