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