!SSHd - an SSH server for RISC OS 5
====================================

SSHd lets you use this machine from another computer with ssh: run
*commands, work at an interactive command line, and copy files with sftp
or scp.  Logins use public keys only, apart from a one-time code for
adding the first key with ssh-copy-id.

Its files live in the SSHd directory inside !Boot.Choices (Shift-double-
click !Boot to open it): the host key, the authorized keys (authkeys),
the settings (Options), the log, and the optional Banner and Motd texts.

Quick start
-----------
1. Double-click !SSHd.  An icon appears on the icon bar.  The first start
   creates the host key and the Options file.
2. The SSH Keys window opens by itself.  Add your public key: paste the
   line from your id_ed25519.pub into the field and press Return, or drop
   the .pub file on the window, use Remote Import, or pair with
   ssh-copy-id (both below).
3. The log window (Select on the icon) shows the command to connect with,
   for example:  ssh riscos@192.168.1.20
4. On the first connection ssh prints "ED25519 key fingerprint is
   SHA256:...".  Answer yes only if it matches the Host Key Fingerprint
   at the top of the SSH Keys window.
5. Type *commands.  Ctrl-D, or exit, ends the session.

Desktop
-------
  Select  opens the SSH Keys window
  Adjust  opens the log window (follows new lines, like tail -f); Menu
          over it opens the log file in your editor or clears the window
  Menu    Info / Help... / Log window / Verbose (tick: more log detail) /
          SSH Keys... / Choices... (opens the settings file in your
          editor) / Quit (asks first while sessions are open)
  Interactive help (the Help application) describes each icon and menu
  item.

The SSH Keys window has five sections:
  Host Key Fingerprint  the command to connect with and this server's
      host key fingerprint, which ssh shows on the first connection.
  Public Key Import  paste a public key into the field (the line
      ssh-keygen writes to id_ed25519.pub), then Add or Return.  Dropping
      such a file anywhere on the window adds it too.
  Remote Import  enter the account (user@host, or user@host:port) and the
      password of the machine whose keys you want to allow.  SSHd logs in
      there over SSH, shows the machine's host key fingerprint for you to
      confirm, reads ~/.ssh/id_ed25519.pub, id_ecdsa.pub and id_rsa.pub
      and imports them.  The password is used once and not stored.  The
      desktop pauses while this runs.  Import needs an SSH server on that
      machine (macOS: Sharing > Remote Login; Linux: openssh-server);
      without one, copy its ~/.ssh/id_ed25519.pub here some other way and
      drop it on the window.
  ssh-copy-id Pairing  click Pair for a one-time code, then on the
      other machine run the ssh-copy-id command the section shows and
      type the code when it asks for a password.  The code works for one
      login within 2 minutes (Stop ends it sooner, as do three wrong
      codes), and that login can only add keys: no commands, no shell, no
      SFTP.  Without pairing SSHd accepts no passwords at all.  With a key
      that is already authorized, ssh-copy-id adds more keys without
      pairing.
  Authorized Keys  the keys that may log in, by fingerprint and comment,
      each with a Delete button.  A key that is already listed is not
      added twice.

Any authorized key has full control of the machine: every *command, and
over SFTP every filing system.  Only the user name "riscos" is accepted
(the user setting); it is a filter, not a password: the key decides.

Connecting
----------
  ssh riscos@riscos-host Cat       run one command, output comes back
  ssh riscos@riscos-host           interactive * command line in a
                                   TaskWindow

The leading * is optional; if you type it, quote the command
('*Cat'), as Unix shells expand a bare *.  Both single commands and
interactive sessions run in a TaskWindow child task (as used by Edit):
line input, output as it is produced, and the same limits for Wimp
applications.  In an interactive session Ctrl-C is Escape, Ctrl-D on an
empty line or exit ends the session, and the cursor keys do nothing
(there is no command history).  A command is run from a small Obey file,
so it may contain quotes and be up to 1000 characters (control
characters other than tab are refused), and Obey replaces %0..%9 with
nothing and %% with % before it runs; its exit status is 0 if it
completed and 1 if it raised a RISC OS error (printed in the output) or
was interrupted.

File transfer
-------------
sftp and scp work (scp in its modern SFTP mode; the old scp -O protocol
does not).  Clients start in the directory of the root setting, by
default the boot disc, and see RISC OS paths the UnixLib way:
HostFS::HostFS.$.Public is /HostFS::HostFS.$/Public, and dir.file/txt is
/dir/file.txt.  Other filing systems are reached the same way, for
example cd /ADFS::HardDisc4.$ in sftp.  Files cannot be read or written
beyond 2 GB.

Filetypes follow the HostFS convention: a name ending in ,xxx names the
file without the suffix and sets type xxx on upload or rename (put prog
prog,ffb stores a BASIC file "prog"); otherwise the extension is looked up
with MimeMap (notes.txt becomes Text), and a name MimeMap does not know
gets the filetype setting.  Listings show ,xxx on files whose type the
extension does not imply, and get prog,ffb fetches prog.

Settings
--------
Choices... on the icon bar menu opens the Options file, written on first
run with every setting as a comment showing its default; remove the # in
front of a setting to change it.  A line is "name value"; anything from #
to the end of the line is a comment.  The file survives package upgrades.
Settings take effect when SSHd is next started.
  port 22            TCP port to listen on
  bind 0.0.0.0       address to listen on (all interfaces)
  root <Boot$Dir>.^  directory SFTP clients start in
  user riscos        the one user name that may log in; empty for any
  shell on           off refuses interactive sessions
  exec on            off refuses single commands (ssh host Cat)
  sftp on            off refuses SFTP and scp
  auth-timeout 60    seconds a connection has to log in; 0 unlimited
  idle-timeout 120   seconds a logged-in connection may sit with no
                     session open; 0 unlimited
  max-sessions 8     connections held at once (one address may hold at
                     most two that have not logged in yet, and open at
                     most ten in any ten seconds)
  banner on          send the text file Banner (one you create) to
                     clients before they log in
  motd on            show the text file Motd when a shell starts
  filetype ffd       type for uploads with no ,xxx suffix and no MimeMap
                     match (ffd Data, fff Text)
  verbose 0          1 for more log lines, 2 for SSH protocol detail
  log on             off stops writing the log file
  log-size 256       past this many KB the log becomes log-old (replacing
                     the previous one) and a new log starts

Command line options override the file (run sshd by hand in a TaskWindow
with -t for text mode): -p port, -b address, -r sftp-dir, -d config-dir,
-t, -v.

Troubleshooting
---------------
The log window says what happened; the log file keeps the same lines and
is where to look after a crash.  If the server cannot start (no
CryptRandom module, port in use), the icon stays on the bar, the log
window opens with the reason, and Quit removes it; nothing blocks a boot
sequence that runs !SSHd.  A refused login is logged with its reason
(wrong user name, or a key that is not authorized).

After upgrading the package, Quit the running !SSHd and run the new one;
the Info window shows which version is running.

Licences
--------
SSHd is MIT licensed.  The binary statically links libssh (LGPL 2.1, some
parts BSD), mbedTLS (Apache 2.0) and GCCSDK's UnixLib; their licence
texts are in the package's RiscPkg.Copyright, and the build scripts at
the website let anyone relink against a modified libssh or UnixLib as
the LGPL requires.
