src/Pure/General/ssh.scala
author wenzelm
Mon, 10 Oct 2016 10:25:59 +0200
changeset 64125 a034dac5ca3c
parent 64124 818265654e60
child 64126 42bcd207598d
permissions -rw-r--r--
clarified (hardwired!) default (see also jEdit/FTP);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/ssh.scala
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     3
64124
818265654e60 tuned comment;
wenzelm
parents: 64123
diff changeset
     4
SSH client based on JSch (see also http://www.jcraft.com/jsch/examples).
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     5
*/
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     6
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     7
package isabelle
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     8
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     9
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    10
import com.jcraft.jsch.{JSch, Logger => JSch_Logger, Session => JSch_Session,
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    11
  OpenSSHConfig, UserInfo, ChannelExec, ChannelSftp}
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    12
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    13
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    14
object SSH
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    15
{
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    16
  /* init */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    17
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    18
  def apply(config_dir: Path = Path.explode("~/.ssh"),
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    19
    config_file: Path = Path.explode("~/.ssh/config"),
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    20
    identity_files: List[Path] =
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    21
      List("~/.ssh/id_dsa", "~/.ssh/id_ecdsa", "~/.ssh/id_rsa").map(Path.explode(_))): SSH =
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    22
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    23
    if (!config_dir.is_dir) error("Bad ssh config directory: " + config_dir)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    24
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    25
    val jsch = new JSch
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    26
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    27
    if (config_file.is_file)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    28
      jsch.setConfigRepository(OpenSSHConfig.parseFile(File.platform_path(config_file)))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    29
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    30
    val known_hosts = config_dir + Path.explode("known_hosts")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    31
    if (!known_hosts.is_file) known_hosts.file.createNewFile
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    32
    jsch.setKnownHosts(File.platform_path(known_hosts))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    33
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    34
    for (identity_file <- identity_files if identity_file.is_file)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    35
      jsch.addIdentity(File.platform_path(identity_file))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    36
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    37
    new SSH(jsch)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    38
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    39
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    40
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    41
  /* logging */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    42
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    43
  def logging(verbose: Boolean = true, debug: Boolean = false)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    44
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    45
    JSch.setLogger(if (verbose) new Logger(debug) else null)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    46
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    47
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    48
  private class Logger(debug: Boolean) extends JSch_Logger
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    49
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    50
    def isEnabled(level: Int): Boolean = level != JSch_Logger.DEBUG || debug
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    51
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    52
    def log(level: Int, msg: String)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    53
    {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    54
      level match {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    55
        case JSch_Logger.ERROR | JSch_Logger.FATAL => Output.error_message(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    56
        case JSch_Logger.WARN => Output.warning(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    57
        case _ => Output.writeln(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    58
      }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    59
    }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    60
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    61
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    62
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    63
  /* session */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    64
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    65
  class Session private[SSH](val jsch: JSch, val session: JSch_Session)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    66
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    67
    override def toString: String =
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    68
      (if (session.getUserName == null) "" else session.getUserName + "@") +
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    69
      (if (session.getHost == null) "" else session.getHost) +
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    70
      (if (session.getPort == 22) "" else ":" + session.getPort)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    71
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    72
    def open: Session = { session.connect; this }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    73
    def close: Session = { session.disconnect; this }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    74
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    75
    def channel_exec: ChannelExec =
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    76
      session.openChannel("exec").asInstanceOf[ChannelExec]
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    77
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    78
    def channel_sftp: ChannelSftp =
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    79
      session.openChannel("sftp").asInstanceOf[ChannelSftp]
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    80
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    81
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    82
  object No_User_Info extends UserInfo
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    83
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    84
    def getPassphrase: String = null
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    85
    def getPassword: String = null
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    86
    def promptPassword(msg: String): Boolean = false
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    87
    def promptPassphrase(msg: String): Boolean = false
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    88
    def promptYesNo(msg: String): Boolean = false
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    89
    def showMessage(msg: String): Unit = Output.writeln(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    90
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    91
}
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    92
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    93
class SSH private(val jsch: JSch)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    94
{
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    95
  def session(host: String, port: Int = 22, user: String = null,
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    96
      compression: Boolean = true): SSH.Session =
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    97
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    98
    val session = jsch.getSession(user, host, port)
64125
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
    99
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
   100
    session.setUserInfo(SSH.No_User_Info)
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
   101
    session.setConfig("MaxAuthTries", "3")
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
   102
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   103
    if (compression) {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   104
      session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   105
      session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   106
      session.setConfig("compression_level", "9")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   107
    }
64125
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
   108
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   109
    new SSH.Session(jsch, session)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   110
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   111
}