channels: Build user channels with '-O1'.
[jackhill/guix/guix.git] / guix / ssh.scm
index 7bc499a..457d189 100644 (file)
@@ -1,5 +1,5 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
   #:use-module (guix store)
   #:use-module (guix inferior)
   #:use-module (guix i18n)
-  #:use-module ((guix utils) #:select (&fix-hint))
+  #:use-module ((guix diagnostics)
+                #:select (info &fix-hint formatted-message))
+  #:use-module ((guix progress)
+                #:select (progress-bar
+                          erase-current-line current-terminal-columns))
   #:use-module (gcrypt pk-crypto)
   #:use-module (ssh session)
   #:use-module (ssh auth)
   #:use-module (ice-9 match)
   #:use-module (ice-9 format)
   #:use-module (ice-9 binary-ports)
+  #:use-module (ice-9 vlist)
   #:export (open-ssh-session
+            authenticate-server*
+
             remote-inferior
             remote-daemon-channel
             connect-to-remote-daemon
@@ -47,8 +54,7 @@
             retrieve-files*
             remote-store-host
 
-            report-guile-error
-            report-module-error))
+            report-guile-error))
 
 ;;; Commentary:
 ;;;
 (define %compression
   "zlib@openssh.com,zlib")
 
+(define (host-key->type+key host-key)
+  "Destructure HOST-KEY, an OpenSSH host key string, and return two values:
+its key type as a symbol, and the actual base64-encoded string."
+  (define (type->symbol type)
+    (and (string-prefix? "ssh-" type)
+         (string->symbol (string-drop type 4))))
+
+  (match (string-tokenize host-key)
+    ((type key x)
+     (values (type->symbol type) key))
+    ((type key)
+     (values (type->symbol type) key))))
+
+(define (authenticate-server* session key)
+  "Make sure the server for SESSION has the given KEY, where KEY is a string
+such as \"ssh-ed25519 AAAAC3Nz… root@example.org\".  Raise an exception if the
+actual key does not match."
+  (let-values (((server)   (get-server-public-key session))
+               ((type key) (host-key->type+key key)))
+    (unless (and (or (not (get-key-type server))
+                     (eq? (get-key-type server) type))
+                 (string=? (public-key->string server) key))
+      ;; Key mismatch: something's wrong.  XXX: It could be that the server
+      ;; provided its Ed25519 key when we where expecting its RSA key.  XXX:
+      ;; Guile-SSH 0.10.1 doesn't know about ed25519 keys and 'get-key-type'
+      ;; returns #f in that case.
+      (raise (formatted-message (G_ "server at '~a' returned host key \
+'~a' of type '~a' instead of '~a' of type '~a'~%")
+                                (session-get session 'host)
+                                (public-key->string server)
+                                (get-key-type server)
+                                key type)))))
+
 (define* (open-ssh-session host #:key user port identity
-                           (compression %compression))
+                           host-key
+                           (compression %compression)
+                           (timeout 3600))
   "Open an SSH session for HOST and return it.  IDENTITY specifies the file
 name of a private key to use for authenticating with the host.  When USER,
 PORT, or IDENTITY are #f, use default values or whatever '~/.ssh/config'
-specifies; otherwise use them.  Throw an error on failure."
+specifies; otherwise use them.
+
+When HOST-KEY is true, it must be a string like \"ssh-ed25519 AAAAC3Nz…
+root@example.org\"; the server is authenticated and an error is raised if its
+host key is different from HOST-KEY.
+
+Install TIMEOUT as the maximum time in seconds after which a read or write
+operation on a channel of the returned session is considered as failing.
+
+Throw an error on failure."
   (let ((session (make-session #:user user
                                #:identity identity
                                #:host host
@@ -73,32 +123,58 @@ specifies; otherwise use them.  Throw an error on failure."
                                #:timeout 10       ;seconds
                                ;; #:log-verbosity 'protocol
 
+                               ;; Prevent libssh from reading
+                               ;; ~/.ssh/known_hosts when the caller provides
+                               ;; a HOST-KEY to match against.
+                               #:knownhosts (and host-key "/dev/null")
+
                                ;; We need lightweight compression when
                                ;; exchanging full archives.
                                #:compression compression
-                               #:compression-level 3)))
+                               #:compression-level 3
+
+                               ;; Speed up RPCs by creating sockets with
+                               ;; TCP_NODELAY.
+                               #:nodelay #t)))
 
     ;; Honor ~/.ssh/config.
     (session-parse-config! session)
 
     (match (connect! session)
       ('ok
+       (if host-key
+           ;; Make sure the server's key is what we expect.
+           (authenticate-server* session host-key)
+
+           ;; Authenticate against ~/.ssh/known_hosts.
+           (match (authenticate-server session)
+             ('ok #f)
+             (reason
+              (raise (formatted-message (G_ "failed to authenticate \
+server at '~a': ~a")
+                                        (session-get session 'host)
+                                        reason)))))
+
        ;; Use public key authentication, via the SSH agent if it's available.
        (match (userauth-public-key/auto! session)
          ('success
+          (session-set! session 'timeout timeout)
           session)
          (x
-          (disconnect! session)
-          (raise (condition
-                  (&message
-                   (message (format #f (G_ "SSH authentication failed for '~a': ~a~%")
-                                    host (get-error session)))))))))
+          (match (userauth-gssapi! session)
+            ('success
+             (session-set! session 'timeout timeout)
+             session)
+            (x
+             (disconnect! session)
+             (raise (condition
+                     (&message
+                      (message (format #f (G_ "SSH authentication failed for '~a': ~a~%")
+                                       host (get-error session)))))))))))
       (x
        ;; Connection failed or timeout expired.
-       (raise (condition
-               (&message
-                (message (format #f (G_ "SSH connection to '~a' failed: ~a~%")
-                                 host (get-error session))))))))))
+       (raise (formatted-message (G_ "SSH connection to '~a' failed: ~a~%")
+                                 host (get-error session)))))))
 
 (define* (remote-inferior session #:optional become-command)
   "Return a remote inferior for the given SESSION.  If BECOME-COMMAND is
@@ -106,14 +182,12 @@ given, use that to invoke the remote Guile REPL."
   (let* ((repl-command (append (or become-command '())
                                '("guix" "repl" "-t" "machine")))
          (pipe (apply open-remote-pipe* session OPEN_BOTH repl-command)))
-    ;; XXX: 'channel-get-exit-status' would be better here, but hangs if the
-    ;; process does succeed. This doesn't reflect the documentation, so it's
-    ;; possible that it's a bug in guile-ssh.
     (when (eof-object? (peek-char pipe))
-      (raise (condition
-              (&message
-               (message (format #f (G_ "failed to run '~{~a~^ ~}'")
-                                repl-command))))))
+      (let ((status (channel-get-exit-status pipe)))
+        (close-port pipe)
+        (raise (formatted-message (G_ "remote command '~{~a~^ ~}' failed \
+with status ~a")
+                                  repl-command status))))
     (port->inferior pipe)))
 
 (define* (inferior-remote-eval exp session #:optional become-command)
@@ -131,6 +205,40 @@ REPL."
         ;; <https://bugs.gnu.org/26976>.)
         (close-inferior inferior)))))
 
+(define (remote-run exp session)
+  "Run EXP in a new process in SESSION and return a remote pipe.
+
+Unlike 'inferior-remote-eval', this is used for side effects and may
+communicate over stdout/stdin as it sees fit.  EXP is typically a loop that
+processes data from stdin and/or sends data to stdout.  The assumption is that
+EXP never returns or calls 'primitive-exit' when it's done."
+  (define pipe
+    (open-remote-pipe* session OPEN_BOTH
+                       "guix" "repl" "-t" "machine"))
+
+  (match (read pipe)
+    (('repl-version _ ...)
+     #t)
+    ((? eof-object?)
+     (close-port pipe)
+     (raise (formatted-message
+             (G_ "failed to start 'guix repl' on '~a'")
+             (session-get session 'host)))))
+
+  ;; Disable buffering so 'guix repl' does not read more than what's really
+  ;; sent to itself.
+  (write '(setvbuf (current-input-port) 'none) pipe)
+  (force-output pipe)
+
+  ;; Read the reply and subsequent newline.
+  (read pipe) (get-u8 pipe)
+
+  (write exp pipe)
+  (force-output pipe)
+
+  ;; From now on, we stop following the inferior protocol.
+  pipe)
+
 (define* (remote-daemon-channel session
                                 #:optional
                                 (socket-name
@@ -186,11 +294,7 @@ REPL."
              (_
               (primitive-exit 1)))))))
 
-  (open-remote-pipe* session OPEN_BOTH
-                     ;; Sort-of shell-quote REDIRECT.
-                     "guile" "-c"
-                     (object->string
-                      (object->string redirect))))
+  (remote-run redirect session))
 
 (define* (connect-to-remote-daemon session
                                    #:optional
@@ -235,17 +339,17 @@ can be written."
                        (consume-input (current-input-port))
                        (list 'protocol-error (nix-protocol-error-message c))))
               (with-store store
+                (write '(importing))              ;we're ready
+                (force-output)
+
                 (setvbuf (current-input-port) 'none)
                 (import-paths store (current-input-port))
                 '(success))))
           (lambda args
-            (cons 'error args))))))
+            (cons 'error args))))
+       (primitive-exit 0)))
 
-  (open-remote-pipe session
-                    (string-join
-                     `("guile" "-c"
-                       ,(object->string (object->string import))))
-                    OPEN_BOTH))
+  (remote-run import session))
 
 (define* (store-export-channel session files
                                #:key recursive?)
@@ -255,22 +359,20 @@ be read.  When RECURSIVE? is true, the closure of FILES is exported."
   ;; remote store.
   (define export
     `(begin
-       (eval-when (load expand eval)
-         (unless (resolve-module '(guix) #:ensure #f)
-           (write `(module-error))
-           (exit 7)))
-
        (use-modules (guix) (srfi srfi-1)
                     (srfi srfi-26) (srfi srfi-34))
 
        (guard (c ((nix-connection-error? c)
                   (write `(connection-error ,(nix-connection-error-file c)
-                                            ,(nix-connection-error-code c))))
+                                            ,(nix-connection-error-code c)))
+                  (primitive-exit 1))
                  ((nix-protocol-error? c)
                   (write `(protocol-error ,(nix-protocol-error-status c)
-                                          ,(nix-protocol-error-message c))))
+                                          ,(nix-protocol-error-message c)))
+                  (primitive-exit 2))
                  (else
-                  (write `(exception))))
+                  (write `(exception))
+                  (primitive-exit 3)))
          (with-store store
            (let* ((files ',files)
                   (invalid (remove (cut valid-path? store <>)
@@ -288,13 +390,10 @@ be read.  When RECURSIVE? is true, the closure of FILES is exported."
 
              (setvbuf (current-output-port) 'none)
              (export-paths store files (current-output-port)
-                           #:recursive? ,recursive?))))))
+                           #:recursive? ,recursive?)
+             (primitive-exit 0))))))
 
-  (open-remote-input-pipe session
-                          (string-join
-                           `("guile" "-c"
-                             ,(object->string
-                               (object->string export))))))
+  (remote-run export session))
 
 (define (remote-system session)
   "Return the system type as expected by Nix, usually ARCHITECTURE-KERNEL, of
@@ -324,6 +423,56 @@ to the system ACL file if it has not yet been authorized."
    session
    become-command))
 
+(define (prepare-to-send store host log-port items)
+  "Notify the user that we're about to send ITEMS to HOST.  Return three
+values allowing 'notify-send-progress' to track the state of this transfer."
+  (let* ((count (length items))
+         (sizes (fold (lambda (item result)
+                        (vhash-cons item
+                                    (path-info-nar-size
+                                     (query-path-info store item))
+                                    result))
+                      vlist-null
+                      items))
+         (total  (vlist-fold (lambda (pair result)
+                               (match pair
+                                 ((_ . size) (+ size result))))
+                             0
+                             sizes)))
+    (info (N_ "sending ~a store item (~h MiB) to '~a'...~%"
+              "sending ~a store items (~h MiB) to '~a'...~%" count)
+          count
+          (inexact->exact (round (/ total (expt 2. 20))))
+          host)
+
+    (values log-port sizes total 0)))
+
+(define (notify-transfer-progress item port sizes total sent)
+  "Notify the user that we've already transferred SENT bytes out of TOTAL.
+Use SIZES to determine the size of ITEM, which is about to be sent."
+  (define (display-bar %)
+    (erase-current-line port)
+    (format port "~3@a% ~a"
+            (inexact->exact (round (* 100. (/ sent total))))
+            (progress-bar % (- (max (current-terminal-columns) 5) 5)))
+    (force-output port))
+
+  (unless (zero? total)
+    (let ((% (* 100. (/ sent total))))
+      (match (vhash-assoc item sizes)
+        (#f
+         (display-bar %)
+         (values port sizes total sent))
+        ((_ . size)
+         (display-bar %)
+         (values port sizes total (+ sent size)))))))
+
+(define (notify-transfer-completion port . args)
+  "Notify the user that the transfer has completed."
+  (apply notify-transfer-progress "" port args) ;display the 100% progress bar
+  (erase-current-line port)
+  (force-output port))
+
 (define* (send-files local files remote
                      #:key
                      recursive?
@@ -343,19 +492,21 @@ Return the list of store items actually sent."
                         (remove (cut valid-path? store <>)
                                 ',files)))
                    session))
-         (count   (length missing))
-         (sizes   (map (lambda (item)
-                         (path-info-nar-size (query-path-info local item)))
-                       missing))
-         (port    (store-import-channel session)))
-    (format log-port (N_ "sending ~a store item (~h MiB) to '~a'...~%"
-                         "sending ~a store items (~h MiB) to '~a'...~%" count)
-            count
-            (inexact->exact (round (/ (reduce + 0 sizes) (expt 2. 20))))
-            (session-get session 'host))
+         (port    (store-import-channel session))
+         (host    (session-get session 'host)))
+    ;; Make sure everything alright on the remote side.
+    (match (read port)
+      (('importing)
+       #t)
+      (sexp
+       (handle-import/export-channel-error sexp remote)))
 
     ;; Send MISSING in topological order.
-    (export-paths local missing port)
+    (let ((tty? (isatty? log-port)))
+      (export-paths local missing port
+                    #:start (cut prepare-to-send local host log-port <>)
+                    #:progress (if tty? notify-transfer-progress (const #f))
+                    #:finish (if tty? notify-transfer-completion (const #f))))
 
     ;; Tell the remote process that we're done.  (In theory the end-of-archive
     ;; mark of 'export-paths' would be enough, but in practice it's not.)
@@ -422,6 +573,27 @@ to the length of FILES.)"
              (&message
               (message (format #f fmt args ...))))))))
 
+(define (handle-import/export-channel-error sexp remote)
+  "Report an error corresponding to SEXP, the EOF object or an sexp read from
+REMOTE."
+  (match sexp
+    ((? eof-object?)
+     (report-guile-error (remote-store-host remote)))
+    (('connection-error file code . _)
+     (raise-error (G_ "failed to connect to '~A' on remote host '~A': ~a")
+                  file (remote-store-host remote) (strerror code)))
+    (('invalid-items items . _)
+     (raise-error (N_ "no such item on remote host '~A':~{ ~a~}"
+                      "no such items on remote host '~A':~{ ~a~}"
+                      (length items))
+                  (remote-store-host remote) items))
+    (('protocol-error status message . _)
+     (raise-error (G_ "protocol error on remote host '~A': ~a")
+                  (remote-store-host remote) message))
+    (_
+     (raise-error (G_ "failed to retrieve store items from '~a'")
+                  (remote-store-host remote)))))
+
 (define* (retrieve-files* files remote
                           #:key recursive? (log-port (current-error-port))
                           (import (const #f)))
@@ -442,24 +614,8 @@ from REMOTE.  When RECURSIVE? is true, retrieve the closure of FILES."
            (import port))
          (lambda ()
            (close-port port))))
-      ((? eof-object?)
-       (report-guile-error (remote-store-host remote)))
-      (('module-error . _)
-       (report-module-error (remote-store-host remote)))
-      (('connection-error file code . _)
-       (raise-error (G_ "failed to connect to '~A' on remote host '~A': ~a")
-                    file (remote-store-host remote) (strerror code)))
-      (('invalid-items items . _)
-       (raise-error (N_ "no such item on remote host '~A':~{ ~a~}"
-                        "no such items on remote host '~A':~{ ~a~}"
-                        (length items))
-                    (remote-store-host remote) items))
-      (('protocol-error status message . _)
-       (raise-error (G_ "protocol error on remote host '~A': ~a")
-                    (remote-store-host remote) message))
-      (_
-       (raise-error (G_ "failed to retrieve store items from '~a'")
-                    (remote-store-host remote))))))
+      (sexp
+       (handle-import/export-channel-error sexp remote)))))
 
 (define* (retrieve-files local files remote
                          #:key recursive? (log-port (current-error-port)))
@@ -484,13 +640,9 @@ LOCAL.  When RECURSIVE? is true, retrieve the closure of FILES."
 check.")
                    host)))
 
-(define (report-module-error host)
-  "Report an error about missing Guix modules on HOST."
-  ;; TRANSLATORS: Leave "Guile" untranslated.
-  (raise-error (G_ "Guile modules not found on remote host '~A'") host
-               (=> (G_ "Make sure @code{GUILE_LOAD_PATH} includes Guix'
-own module directory.  Run @command{ssh ~A env | grep GUILE_LOAD_PATH} to
-check.")
-                   host)))
+(define (report-inferior-exception exception host)
+  "Report EXCEPTION, an &inferior-exception that occurred on HOST."
+  (raise-error (G_ "exception occurred on remote host '~A': ~s")
+               host (inferior-exception-arguments exception)))
 
 ;;; ssh.scm ends here