ssh: Add 'authenticate-server*' and use it for offloading.
[jackhill/guix/guix.git] / guix / ssh.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix ssh)
20 #:use-module (guix store)
21 #:use-module (guix inferior)
22 #:use-module (guix i18n)
23 #:use-module ((guix utils) #:select (&fix-hint))
24 #:use-module (gcrypt pk-crypto)
25 #:use-module (ssh session)
26 #:use-module (ssh auth)
27 #:use-module (ssh key)
28 #:use-module (ssh channel)
29 #:use-module (ssh popen)
30 #:use-module (ssh session)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-11)
33 #:use-module (srfi srfi-26)
34 #:use-module (srfi srfi-34)
35 #:use-module (srfi srfi-35)
36 #:use-module (ice-9 match)
37 #:use-module (ice-9 format)
38 #:use-module (ice-9 binary-ports)
39 #:export (open-ssh-session
40 authenticate-server*
41
42 remote-inferior
43 remote-daemon-channel
44 connect-to-remote-daemon
45 remote-system
46 remote-authorize-signing-key
47 send-files
48 retrieve-files
49 retrieve-files*
50 remote-store-host
51
52 report-guile-error
53 report-module-error))
54
55 ;;; Commentary:
56 ;;;
57 ;;; This module provides tools to support communication with remote stores
58 ;;; over SSH, using Guile-SSH.
59 ;;;
60 ;;; Code:
61
62 (define %compression
63 "zlib@openssh.com,zlib")
64
65 (define (host-key->type+key host-key)
66 "Destructure HOST-KEY, an OpenSSH host key string, and return two values:
67 its key type as a symbol, and the actual base64-encoded string."
68 (define (type->symbol type)
69 (and (string-prefix? "ssh-" type)
70 (string->symbol (string-drop type 4))))
71
72 (match (string-tokenize host-key)
73 ((type key x)
74 (values (type->symbol type) key))
75 ((type key)
76 (values (type->symbol type) key))))
77
78 (define (authenticate-server* session key)
79 "Make sure the server for SESSION has the given KEY, where KEY is a string
80 such as \"ssh-ed25519 AAAAC3Nz… root@example.org\". Raise an exception if the
81 actual key does not match."
82 (let-values (((server) (get-server-public-key session))
83 ((type key) (host-key->type+key key)))
84 (unless (and (or (not (get-key-type server))
85 (eq? (get-key-type server) type))
86 (string=? (public-key->string server) key))
87 ;; Key mismatch: something's wrong. XXX: It could be that the server
88 ;; provided its Ed25519 key when we where expecting its RSA key. XXX:
89 ;; Guile-SSH 0.10.1 doesn't know about ed25519 keys and 'get-key-type'
90 ;; returns #f in that case.
91 (raise (condition
92 (&message
93 (message (format #f (G_ "server at '~a' returned host key \
94 '~a' of type '~a' instead of '~a' of type '~a'~%")
95 (session-get session 'host)
96 (public-key->string server)
97 (get-key-type server)
98 key type))))))))
99
100 (define* (open-ssh-session host #:key user port identity
101 (compression %compression)
102 (timeout 3600))
103 "Open an SSH session for HOST and return it. IDENTITY specifies the file
104 name of a private key to use for authenticating with the host. When USER,
105 PORT, or IDENTITY are #f, use default values or whatever '~/.ssh/config'
106 specifies; otherwise use them. Install TIMEOUT as the maximum time in seconds
107 after which a read or write operation on a channel of the returned session is
108 considered as failing.
109
110 Throw an error on failure."
111 (let ((session (make-session #:user user
112 #:identity identity
113 #:host host
114 #:port port
115 #:timeout 10 ;seconds
116 ;; #:log-verbosity 'protocol
117
118 ;; We need lightweight compression when
119 ;; exchanging full archives.
120 #:compression compression
121 #:compression-level 3)))
122
123 ;; Honor ~/.ssh/config.
124 (session-parse-config! session)
125
126 (match (connect! session)
127 ('ok
128 ;; Use public key authentication, via the SSH agent if it's available.
129 (match (userauth-public-key/auto! session)
130 ('success
131 (session-set! session 'timeout timeout)
132 session)
133 (x
134 (disconnect! session)
135 (raise (condition
136 (&message
137 (message (format #f (G_ "SSH authentication failed for '~a': ~a~%")
138 host (get-error session)))))))))
139 (x
140 ;; Connection failed or timeout expired.
141 (raise (condition
142 (&message
143 (message (format #f (G_ "SSH connection to '~a' failed: ~a~%")
144 host (get-error session))))))))))
145
146 (define* (remote-inferior session #:optional become-command)
147 "Return a remote inferior for the given SESSION. If BECOME-COMMAND is
148 given, use that to invoke the remote Guile REPL."
149 (let* ((repl-command (append (or become-command '())
150 '("guix" "repl" "-t" "machine")))
151 (pipe (apply open-remote-pipe* session OPEN_BOTH repl-command)))
152 (when (eof-object? (peek-char pipe))
153 (let ((status (channel-get-exit-status pipe)))
154 (close-port pipe)
155 (raise (condition
156 (&message
157 (message (format #f (G_ "remote command '~{~a~^ ~}' failed \
158 with status ~a")
159 repl-command status)))))))
160 (port->inferior pipe)))
161
162 (define* (inferior-remote-eval exp session #:optional become-command)
163 "Evaluate EXP in a new inferior running in SESSION, and close the inferior
164 right away. If BECOME-COMMAND is given, use that to invoke the remote Guile
165 REPL."
166 (let ((inferior (remote-inferior session become-command)))
167 (dynamic-wind
168 (const #t)
169 (lambda ()
170 (inferior-eval exp inferior))
171 (lambda ()
172 ;; Close INFERIOR right away to prevent finalization from happening in
173 ;; another thread at the wrong time (see
174 ;; <https://bugs.gnu.org/26976>.)
175 (close-inferior inferior)))))
176
177 (define* (remote-daemon-channel session
178 #:optional
179 (socket-name
180 "/var/guix/daemon-socket/socket"))
181 "Return an input/output port (an SSH channel) to the daemon at SESSION."
182 (define redirect
183 ;; Code run in SESSION to redirect the remote process' stdin/stdout to the
184 ;; daemon's socket, à la socat. The SSH protocol supports forwarding to
185 ;; Unix-domain sockets but libssh doesn't have an API for that, hence this
186 ;; hack.
187 `(begin
188 (use-modules (ice-9 match) (rnrs io ports)
189 (rnrs bytevectors))
190
191 (let ((sock (socket AF_UNIX SOCK_STREAM 0))
192 (stdin (current-input-port))
193 (stdout (current-output-port))
194 (select* (lambda (read write except)
195 ;; This is a workaround for
196 ;; <https://bugs.gnu.org/30365> in Guile < 2.2.4:
197 ;; since 'select' sometimes returns non-empty sets for
198 ;; no good reason, call 'select' a second time with a
199 ;; zero timeout to filter out incorrect replies.
200 (match (select read write except)
201 ((read write except)
202 (select read write except 0))))))
203 (setvbuf stdout 'none)
204
205 ;; Use buffered ports so that 'get-bytevector-some' returns up to the
206 ;; whole buffer like read(2) would--see <https://bugs.gnu.org/30066>.
207 (setvbuf stdin 'block 65536)
208 (setvbuf sock 'block 65536)
209
210 (connect sock AF_UNIX ,socket-name)
211
212 (let loop ()
213 (match (select* (list stdin sock) '() '())
214 ((reads () ())
215 (when (memq stdin reads)
216 (match (get-bytevector-some stdin)
217 ((? eof-object?)
218 (primitive-exit 0))
219 (bv
220 (put-bytevector sock bv)
221 (force-output sock))))
222 (when (memq sock reads)
223 (match (get-bytevector-some sock)
224 ((? eof-object?)
225 (primitive-exit 0))
226 (bv
227 (put-bytevector stdout bv))))
228 (loop))
229 (_
230 (primitive-exit 1)))))))
231
232 (open-remote-pipe* session OPEN_BOTH
233 ;; Sort-of shell-quote REDIRECT.
234 "guile" "-c"
235 (object->string
236 (object->string redirect))))
237
238 (define* (connect-to-remote-daemon session
239 #:optional
240 (socket-name
241 "/var/guix/daemon-socket/socket"))
242 "Connect to the remote build daemon listening on SOCKET-NAME over SESSION,
243 an SSH session. Return a <store-connection> object."
244 (open-connection #:port (remote-daemon-channel session socket-name)))
245
246
247 (define (store-import-channel session)
248 "Return an output port to which archives to be exported to SESSION's store
249 can be written."
250 ;; Using the 'import-paths' RPC on a remote store would be slow because it
251 ;; makes a round trip every time 32 KiB have been transferred. This
252 ;; procedure instead opens a separate channel to use the remote
253 ;; 'import-paths' procedure, which consumes all the data in a single round
254 ;; trip. This optimizes the successful case at the expense of error
255 ;; conditions: errors can only be reported once all the input has been
256 ;; consumed.
257 (define import
258 `(begin
259 (use-modules (guix) (srfi srfi-34)
260 (rnrs io ports) (rnrs bytevectors))
261
262 (define (consume-input port)
263 (let ((bv (make-bytevector 32768)))
264 (let loop ()
265 (let ((n (get-bytevector-n! port bv 0
266 (bytevector-length bv))))
267 (unless (eof-object? n)
268 (loop))))))
269
270 ;; Upon completion, write an sexp that denotes the status.
271 (write
272 (catch #t
273 (lambda ()
274 (guard (c ((nix-protocol-error? c)
275 ;; Consume all the input since the only time we can
276 ;; report the error is after everything has been
277 ;; consumed.
278 (consume-input (current-input-port))
279 (list 'protocol-error (nix-protocol-error-message c))))
280 (with-store store
281 (setvbuf (current-input-port) 'none)
282 (import-paths store (current-input-port))
283 '(success))))
284 (lambda args
285 (cons 'error args))))))
286
287 (open-remote-pipe session
288 (string-join
289 `("guile" "-c"
290 ,(object->string (object->string import))))
291 OPEN_BOTH))
292
293 (define* (store-export-channel session files
294 #:key recursive?)
295 "Return an input port from which an export of FILES from SESSION's store can
296 be read. When RECURSIVE? is true, the closure of FILES is exported."
297 ;; Same as above: this is more efficient than calling 'export-paths' on a
298 ;; remote store.
299 (define export
300 `(begin
301 (eval-when (load expand eval)
302 (unless (resolve-module '(guix) #:ensure #f)
303 (write `(module-error))
304 (exit 7)))
305
306 (use-modules (guix) (srfi srfi-1)
307 (srfi srfi-26) (srfi srfi-34))
308
309 (guard (c ((nix-connection-error? c)
310 (write `(connection-error ,(nix-connection-error-file c)
311 ,(nix-connection-error-code c))))
312 ((nix-protocol-error? c)
313 (write `(protocol-error ,(nix-protocol-error-status c)
314 ,(nix-protocol-error-message c))))
315 (else
316 (write `(exception))))
317 (with-store store
318 (let* ((files ',files)
319 (invalid (remove (cut valid-path? store <>)
320 files)))
321 (unless (null? invalid)
322 (write `(invalid-items ,invalid))
323 (exit 1))
324
325 ;; TODO: When RECURSIVE? is true, we could send the list of store
326 ;; items in the closure so that the other end can filter out
327 ;; those it already has.
328
329 (write '(exporting)) ;we're ready
330 (force-output)
331
332 (setvbuf (current-output-port) 'none)
333 (export-paths store files (current-output-port)
334 #:recursive? ,recursive?))))))
335
336 (open-remote-input-pipe session
337 (string-join
338 `("guile" "-c"
339 ,(object->string
340 (object->string export))))))
341
342 (define (remote-system session)
343 "Return the system type as expected by Nix, usually ARCHITECTURE-KERNEL, of
344 the machine on the other end of SESSION."
345 (inferior-remote-eval '(begin (use-modules (guix utils)) (%current-system))
346 session))
347
348 (define* (remote-authorize-signing-key key session #:optional become-command)
349 "Send KEY, a canonical sexp containing a public key, over SESSION and add it
350 to the system ACL file if it has not yet been authorized."
351 (inferior-remote-eval
352 `(begin
353 (use-modules (guix build utils)
354 (guix pki)
355 (guix utils)
356 (gcrypt pk-crypto)
357 (srfi srfi-26))
358
359 (define acl (current-acl))
360 (define key (string->canonical-sexp ,(canonical-sexp->string key)))
361
362 (unless (authorized-key? key)
363 (let ((acl (public-keys->acl (cons key (acl->public-keys acl)))))
364 (mkdir-p (dirname %acl-file))
365 (with-atomic-file-output %acl-file
366 (cut write-acl acl <>)))))
367 session
368 become-command))
369
370 (define* (send-files local files remote
371 #:key
372 recursive?
373 (log-port (current-error-port)))
374 "Send the subset of FILES from LOCAL (a local store) that's missing to
375 REMOTE, a remote store. When RECURSIVE? is true, send the closure of FILES.
376 Return the list of store items actually sent."
377 ;; Compute the subset of FILES missing on SESSION and send them.
378 (let* ((files (if recursive? (requisites local files) files))
379 (session (channel-get-session (store-connection-socket remote)))
380 (missing (inferior-remote-eval
381 `(begin
382 (use-modules (guix)
383 (srfi srfi-1) (srfi srfi-26))
384
385 (with-store store
386 (remove (cut valid-path? store <>)
387 ',files)))
388 session))
389 (count (length missing))
390 (sizes (map (lambda (item)
391 (path-info-nar-size (query-path-info local item)))
392 missing))
393 (port (store-import-channel session)))
394 (format log-port (N_ "sending ~a store item (~h MiB) to '~a'...~%"
395 "sending ~a store items (~h MiB) to '~a'...~%" count)
396 count
397 (inexact->exact (round (/ (reduce + 0 sizes) (expt 2. 20))))
398 (session-get session 'host))
399
400 ;; Send MISSING in topological order.
401 (export-paths local missing port)
402
403 ;; Tell the remote process that we're done. (In theory the end-of-archive
404 ;; mark of 'export-paths' would be enough, but in practice it's not.)
405 (channel-send-eof port)
406
407 ;; Wait for completion of the remote process and read the status sexp from
408 ;; PORT. Wait for the exit status only when 'read' completed; otherwise,
409 ;; we might wait forever if the other end is stuck.
410 (let* ((result (false-if-exception (read port)))
411 (status (and result
412 (zero? (channel-get-exit-status port)))))
413 (close-port port)
414 (match result
415 (('success . _)
416 missing)
417 (('protocol-error message)
418 (raise (condition
419 (&store-protocol-error (message message) (status 42)))))
420 (('error key args ...)
421 (raise (condition
422 (&store-protocol-error
423 (message (call-with-output-string
424 (lambda (port)
425 (print-exception port #f key args))))
426 (status 43)))))
427 (_
428 (raise (condition
429 (&store-protocol-error
430 (message "unknown error while sending files over SSH")
431 (status 44)))))))))
432
433 (define (remote-store-session remote)
434 "Return the SSH channel beneath REMOTE, a remote store as returned by
435 'connect-to-remote-daemon', or #f."
436 (channel-get-session (store-connection-socket remote)))
437
438 (define (remote-store-host remote)
439 "Return the name of the host REMOTE is connected to, where REMOTE is a
440 remote store as returned by 'connect-to-remote-daemon'."
441 (match (remote-store-session remote)
442 (#f #f)
443 ((? session? session)
444 (session-get session 'host))))
445
446 (define* (file-retrieval-port files remote
447 #:key recursive?)
448 "Return an input port from which to retrieve FILES (a list of store items)
449 from REMOTE, along with the number of items to retrieve (lower than or equal
450 to the length of FILES.)"
451 (values (store-export-channel (remote-store-session remote) files
452 #:recursive? recursive?)
453 (length files))) ;XXX: inaccurate when RECURSIVE? is true
454
455 (define-syntax raise-error
456 (syntax-rules (=>)
457 ((_ fmt args ... (=> hint-fmt hint-args ...))
458 (raise (condition
459 (&message
460 (message (format #f fmt args ...)))
461 (&fix-hint
462 (hint (format #f hint-fmt hint-args ...))))))
463 ((_ fmt args ...)
464 (raise (condition
465 (&message
466 (message (format #f fmt args ...))))))))
467
468 (define* (retrieve-files* files remote
469 #:key recursive? (log-port (current-error-port))
470 (import (const #f)))
471 "Pass IMPORT an input port from which to read the sequence of FILES coming
472 from REMOTE. When RECURSIVE? is true, retrieve the closure of FILES."
473 (let-values (((port count)
474 (file-retrieval-port files remote
475 #:recursive? recursive?)))
476 (match (read port) ;read the initial status
477 (('exporting)
478 (format #t (N_ "retrieving ~a store item from '~a'...~%"
479 "retrieving ~a store items from '~a'...~%" count)
480 count (remote-store-host remote))
481
482 (dynamic-wind
483 (const #t)
484 (lambda ()
485 (import port))
486 (lambda ()
487 (close-port port))))
488 ((? eof-object?)
489 (report-guile-error (remote-store-host remote)))
490 (('module-error . _)
491 (report-module-error (remote-store-host remote)))
492 (('connection-error file code . _)
493 (raise-error (G_ "failed to connect to '~A' on remote host '~A': ~a")
494 file (remote-store-host remote) (strerror code)))
495 (('invalid-items items . _)
496 (raise-error (N_ "no such item on remote host '~A':~{ ~a~}"
497 "no such items on remote host '~A':~{ ~a~}"
498 (length items))
499 (remote-store-host remote) items))
500 (('protocol-error status message . _)
501 (raise-error (G_ "protocol error on remote host '~A': ~a")
502 (remote-store-host remote) message))
503 (_
504 (raise-error (G_ "failed to retrieve store items from '~a'")
505 (remote-store-host remote))))))
506
507 (define* (retrieve-files local files remote
508 #:key recursive? (log-port (current-error-port)))
509 "Retrieve FILES from REMOTE and import them using the 'import-paths' RPC on
510 LOCAL. When RECURSIVE? is true, retrieve the closure of FILES."
511 (retrieve-files* (remove (cut valid-path? local <>) files)
512 remote
513 #:recursive? recursive?
514 #:log-port log-port
515 #:import (lambda (port)
516 (import-paths local port))))
517
518 \f
519 ;;;
520 ;;; Error reporting.
521 ;;;
522
523 (define (report-guile-error host)
524 (raise-error (G_ "failed to start Guile on remote host '~A'") host
525 (=> (G_ "Make sure @command{guile} can be found in
526 @code{$PATH} on the remote host. Run @command{ssh ~A guile --version} to
527 check.")
528 host)))
529
530 (define (report-module-error host)
531 "Report an error about missing Guix modules on HOST."
532 ;; TRANSLATORS: Leave "Guile" untranslated.
533 (raise-error (G_ "Guile modules not found on remote host '~A'") host
534 (=> (G_ "Make sure @code{GUILE_LOAD_PATH} includes Guix'
535 own module directory. Run @command{ssh ~A env | grep GUILE_LOAD_PATH} to
536 check.")
537 host)))
538
539 ;;; ssh.scm ends here