ssh.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.org>
  3. ;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu machine ssh)
  20. #:use-module (gnu bootloader)
  21. #:use-module (gnu machine)
  22. #:autoload (gnu packages gnupg) (guile-gcrypt)
  23. #:use-module (gnu system)
  24. #:use-module (gnu system file-systems)
  25. #:use-module (gnu system uuid)
  26. #:use-module ((gnu services) #:select (sexp->system-provenance))
  27. #:use-module (guix diagnostics)
  28. #:use-module (guix gexp)
  29. #:use-module (guix i18n)
  30. #:use-module (guix modules)
  31. #:use-module (guix monads)
  32. #:use-module (guix pki)
  33. #:use-module (guix records)
  34. #:use-module (guix remote)
  35. #:use-module (guix scripts system reconfigure)
  36. #:use-module (guix ssh)
  37. #:use-module (guix store)
  38. #:use-module (guix utils)
  39. #:use-module (gcrypt pk-crypto)
  40. #:use-module (ice-9 format)
  41. #:use-module (ice-9 match)
  42. #:use-module (ice-9 textual-ports)
  43. #:use-module (srfi srfi-1)
  44. #:use-module (srfi srfi-9)
  45. #:use-module (srfi srfi-19)
  46. #:use-module (srfi srfi-26)
  47. #:use-module (srfi srfi-34)
  48. #:use-module (srfi srfi-35)
  49. #:export (managed-host-environment-type
  50. machine-ssh-configuration
  51. machine-ssh-configuration?
  52. machine-ssh-configuration
  53. machine-ssh-configuration-host-name
  54. machine-ssh-configuration-build-locally?
  55. machine-ssh-configuration-authorize?
  56. machine-ssh-configuration-allow-downgrades?
  57. machine-ssh-configuration-port
  58. machine-ssh-configuration-user
  59. machine-ssh-configuration-host-key
  60. machine-ssh-configuration-session))
  61. ;;; Commentary:
  62. ;;;
  63. ;;; This module implements remote evaluation and system deployment for
  64. ;;; machines that are accessible over SSH and have a known host-name. In the
  65. ;;; sense of the broader "machine" interface, we describe the environment for
  66. ;;; such machines as 'managed-host.
  67. ;;;
  68. ;;; Code:
  69. ;;;
  70. ;;; Parameters for the SSH client.
  71. ;;;
  72. (define-record-type* <machine-ssh-configuration> machine-ssh-configuration
  73. make-machine-ssh-configuration
  74. machine-ssh-configuration?
  75. (host-name machine-ssh-configuration-host-name) ; string
  76. (system machine-ssh-configuration-system) ; string
  77. (build-locally? machine-ssh-configuration-build-locally? ; boolean
  78. (default #t))
  79. (authorize? machine-ssh-configuration-authorize? ; boolean
  80. (default #t))
  81. (allow-downgrades? machine-ssh-configuration-allow-downgrades? ; boolean
  82. (default #f))
  83. (port machine-ssh-configuration-port ; integer
  84. (default 22))
  85. (user machine-ssh-configuration-user ; string
  86. (default "root"))
  87. (identity machine-ssh-configuration-identity ; path to a private key
  88. (default #f))
  89. (session machine-ssh-configuration-session ; session
  90. (default #f))
  91. (host-key machine-ssh-configuration-host-key ; #f | string
  92. (default #f)))
  93. (define (machine-ssh-session machine)
  94. "Return the SSH session that was given in MACHINE's configuration, or create
  95. one from the configuration's parameters if one was not provided."
  96. (maybe-raise-unsupported-configuration-error machine)
  97. (let ((config (machine-configuration machine)))
  98. (or (machine-ssh-configuration-session config)
  99. (let ((host-name (machine-ssh-configuration-host-name config))
  100. (user (machine-ssh-configuration-user config))
  101. (port (machine-ssh-configuration-port config))
  102. (identity (machine-ssh-configuration-identity config))
  103. (host-key (machine-ssh-configuration-host-key config)))
  104. (unless host-key
  105. (warning (G_ "<machine-ssh-configuration> without a 'host-key' \
  106. is deprecated~%")))
  107. (open-ssh-session host-name
  108. #:user user
  109. #:port port
  110. #:identity identity
  111. #:host-key host-key)))))
  112. ;;;
  113. ;;; Remote evaluation.
  114. ;;;
  115. (define (machine-become-command machine)
  116. "Return as a list of strings the program and arguments necessary to run a
  117. shell command with escalated privileges for MACHINE's configuration."
  118. (if (string= "root" (machine-ssh-configuration-user
  119. (machine-configuration machine)))
  120. '()
  121. '("/run/setuid-programs/sudo" "-n" "--")))
  122. (define (managed-host-remote-eval machine exp)
  123. "Internal implementation of 'machine-remote-eval' for MACHINE instances with
  124. an environment type of 'managed-host."
  125. (maybe-raise-unsupported-configuration-error machine)
  126. (let ((config (machine-configuration machine)))
  127. (remote-eval exp (machine-ssh-session machine)
  128. #:build-locally?
  129. (machine-ssh-configuration-build-locally? config)
  130. #:system
  131. (machine-ssh-configuration-system config)
  132. #:become-command
  133. (machine-become-command machine))))
  134. ;;;
  135. ;;; Safety checks.
  136. ;;;
  137. ;; Assertion to be executed remotely. This abstraction exists to allow us to
  138. ;; gather a list of expressions to be evaluated and eventually evaluate them
  139. ;; all at once instead of one by one. (This is pretty much a monad.)
  140. (define-record-type <remote-assertion>
  141. (remote-assertion exp proc)
  142. remote-assertion?
  143. (exp remote-assertion-expression)
  144. (proc remote-assertion-procedure))
  145. (define-syntax-rule (remote-let ((var exp)) body ...)
  146. "Return a <remote-assertion> that binds VAR to the result of evaluating EXP,
  147. a gexp, remotely, and evaluate BODY in that context."
  148. (remote-assertion exp (lambda (var) body ...)))
  149. (define (machine-check-file-system-availability machine)
  150. "Return a list of <remote-assertion> that raise a '&message' error condition
  151. if any of the file-systems specified in MACHINE's 'system' declaration do not
  152. exist on the machine."
  153. (define file-systems
  154. (filter (lambda (fs)
  155. (and (file-system-mount? fs)
  156. (not (member (file-system-type fs)
  157. %pseudo-file-system-types))
  158. ;; Don't try to validate network file systems.
  159. (not (string-prefix? "nfs" (file-system-type fs)))
  160. (not (memq 'bind-mount (file-system-flags fs)))))
  161. (operating-system-file-systems (machine-operating-system machine))))
  162. (define (check-literal-file-system fs)
  163. (remote-let ((errno #~(catch 'system-error
  164. (lambda ()
  165. (stat #$(file-system-device fs))
  166. #t)
  167. (lambda args
  168. (system-error-errno args)))))
  169. (when (number? errno)
  170. (raise (formatted-message (G_ "device '~a' not found: ~a")
  171. (file-system-device fs)
  172. (strerror errno))))))
  173. (define (check-labeled-file-system fs)
  174. (define remote-exp
  175. (with-imported-modules (source-module-closure
  176. '((gnu build file-systems)))
  177. #~(begin
  178. (use-modules (gnu build file-systems))
  179. (find-partition-by-label #$(file-system-label->string
  180. (file-system-device fs))))))
  181. (remote-let ((result remote-exp))
  182. (unless result
  183. (raise (formatted-message (G_ "no file system with label '~a'")
  184. (file-system-label->string
  185. (file-system-device fs)))))))
  186. (define (check-uuid-file-system fs)
  187. (define remote-exp
  188. (with-imported-modules (source-module-closure
  189. '((gnu build file-systems)
  190. (gnu system uuid)))
  191. #~(begin
  192. (use-modules (gnu build file-systems)
  193. (gnu system uuid))
  194. (let ((uuid (uuid #$(uuid->string (file-system-device fs))
  195. '#$(uuid-type (file-system-device fs)))))
  196. (find-partition-by-uuid uuid)))))
  197. (remote-let ((result remote-exp))
  198. (unless result
  199. (raise (formatted-message (G_ "no file system with UUID '~a'")
  200. (uuid->string (file-system-device fs)))))))
  201. (append (map check-literal-file-system
  202. (filter (lambda (fs)
  203. (string? (file-system-device fs)))
  204. file-systems))
  205. (map check-labeled-file-system
  206. (filter (lambda (fs)
  207. (file-system-label? (file-system-device fs)))
  208. file-systems))
  209. (map check-uuid-file-system
  210. (filter (lambda (fs)
  211. (uuid? (file-system-device fs)))
  212. file-systems))))
  213. (define (machine-check-initrd-modules machine)
  214. "Return a list of <remote-assertion> that raise a '&message' error condition
  215. if any of the modules needed by 'needed-for-boot' file systems in MACHINE are
  216. not available in the initrd."
  217. (define file-systems
  218. (filter file-system-needed-for-boot?
  219. (operating-system-file-systems (machine-operating-system machine))))
  220. (define (missing-modules fs)
  221. (define remote-exp
  222. (let ((device (file-system-device fs)))
  223. (with-imported-modules (source-module-closure
  224. '((gnu build file-systems)
  225. (gnu build linux-modules)
  226. (gnu system uuid)))
  227. #~(begin
  228. (use-modules (gnu build file-systems)
  229. (gnu build linux-modules)
  230. (gnu system uuid))
  231. (define dev
  232. #$(cond ((string? device) device)
  233. ((uuid? device) #~(find-partition-by-uuid
  234. (string->uuid
  235. #$(uuid->string device))))
  236. ((file-system-label? device)
  237. #~(find-partition-by-label
  238. #$(file-system-label->string device)))))
  239. (missing-modules dev '#$(operating-system-initrd-modules
  240. (machine-operating-system machine)))))))
  241. (remote-let ((missing remote-exp))
  242. (unless (null? missing)
  243. (raise (condition
  244. (&message
  245. (message (format #f (G_ "missing modules for ~a:~{ ~a~}~%")
  246. (file-system-device fs)
  247. missing))))))))
  248. (map missing-modules file-systems))
  249. (define* (machine-check-forward-update machine)
  250. "Check whether we are making a forward update for MACHINE. Depending on its
  251. 'allow-upgrades?' field, raise an error or display a warning if we are
  252. potentially downgrading it."
  253. (define config
  254. (machine-configuration machine))
  255. (define validate-reconfigure
  256. (if (machine-ssh-configuration-allow-downgrades? config)
  257. warn-about-backward-reconfigure
  258. ensure-forward-reconfigure))
  259. (remote-let ((provenance #~(call-with-input-file
  260. "/run/current-system/provenance"
  261. read)))
  262. (define channels
  263. (sexp->system-provenance provenance))
  264. (check-forward-update validate-reconfigure
  265. #:current-channels channels)))
  266. (define (machine-check-building-for-appropriate-system machine)
  267. "Raise a '&message' error condition if MACHINE is configured to be built
  268. locally and the 'system' field does not match the '%current-system' reported
  269. by MACHINE."
  270. (let ((config (machine-configuration machine))
  271. (system (remote-system (machine-ssh-session machine))))
  272. (when (and (machine-ssh-configuration-build-locally? config)
  273. (not (string= system (machine-ssh-configuration-system config))))
  274. (raise (formatted-message (G_ "incorrect target system\
  275. ('~a' was given, while the system reports that it is '~a')~%")
  276. (machine-ssh-configuration-system config)
  277. system)))))
  278. (define (check-deployment-sanity machine)
  279. "Raise a '&message' error condition if it is clear that deploying MACHINE's
  280. 'system' declaration would fail."
  281. (define assertions
  282. (append (machine-check-file-system-availability machine)
  283. (machine-check-initrd-modules machine)
  284. (list (machine-check-forward-update machine))))
  285. (define aggregate-exp
  286. ;; Gather all the expressions so that a single round-trip is enough to
  287. ;; evaluate all the ASSERTIONS remotely.
  288. #~(map (lambda (file)
  289. (false-if-exception (primitive-load file)))
  290. '#$(map (lambda (assertion)
  291. (scheme-file "remote-assertion.scm"
  292. (remote-assertion-expression assertion)))
  293. assertions)))
  294. ;; First check MACHINE's system type--an incorrect value for 'system' would
  295. ;; cause subsequent invocations of 'remote-eval' to fail.
  296. (machine-check-building-for-appropriate-system machine)
  297. (mlet %store-monad ((values (machine-remote-eval machine aggregate-exp)))
  298. (for-each (lambda (proc value)
  299. (proc value))
  300. (map remote-assertion-procedure assertions)
  301. values)
  302. (return #t)))
  303. ;;;
  304. ;;; System deployment.
  305. ;;;
  306. (define (machine-boot-parameters machine)
  307. "Monadic procedure returning a list of 'boot-parameters' for the generations
  308. of MACHINE's system profile, ordered from most recent to oldest."
  309. (define bootable-kernel-arguments
  310. (@@ (gnu system) bootable-kernel-arguments))
  311. (define remote-exp
  312. (with-extensions (list guile-gcrypt)
  313. (with-imported-modules (source-module-closure '((guix config)
  314. (guix profiles)))
  315. #~(begin
  316. (use-modules (guix config)
  317. (guix profiles)
  318. (ice-9 textual-ports))
  319. (define %system-profile
  320. (string-append %state-directory "/profiles/system"))
  321. (define (read-file path)
  322. (call-with-input-file path
  323. (lambda (port)
  324. (get-string-all port))))
  325. (map (lambda (generation)
  326. (let* ((system-path (generation-file-name %system-profile
  327. generation))
  328. (boot-parameters-path (string-append system-path
  329. "/parameters"))
  330. (time (stat:mtime (lstat system-path))))
  331. (list generation
  332. system-path
  333. time
  334. (read-file boot-parameters-path))))
  335. (reverse (generation-numbers %system-profile)))))))
  336. (mlet* %store-monad ((generations (machine-remote-eval machine remote-exp)))
  337. (return
  338. (map (lambda (generation)
  339. (match generation
  340. ((generation system-path time serialized-params)
  341. (let* ((params (call-with-input-string serialized-params
  342. read-boot-parameters))
  343. (root (boot-parameters-root-device params))
  344. (label (boot-parameters-label params)))
  345. (boot-parameters
  346. (inherit params)
  347. (label
  348. (string-append label " (#"
  349. (number->string generation) ", "
  350. (let ((time (make-time time-utc 0 time)))
  351. (date->string (time-utc->date time)
  352. "~Y-~m-~d ~H:~M"))
  353. ")"))
  354. (kernel-arguments
  355. (append (bootable-kernel-arguments system-path root)
  356. (boot-parameters-kernel-arguments params))))))))
  357. generations))))
  358. (define-syntax-rule (with-roll-back should-roll-back? mbody ...)
  359. "Catch exceptions that arise when binding MBODY, a monadic expression in
  360. %STORE-MONAD, and collect their arguments in a &deploy-error condition, with
  361. the 'should-roll-back' field set to SHOULD-ROLL-BACK?"
  362. (catch #t
  363. (lambda ()
  364. mbody ...)
  365. (lambda args
  366. (raise (condition (&deploy-error
  367. (should-roll-back should-roll-back?)
  368. (captured-args args)))))))
  369. (define (deploy-managed-host machine)
  370. "Internal implementation of 'deploy-machine' for MACHINE instances with an
  371. environment type of 'managed-host."
  372. (maybe-raise-unsupported-configuration-error machine)
  373. (when (machine-ssh-configuration-authorize?
  374. (machine-configuration machine))
  375. (unless (file-exists? %public-key-file)
  376. (raise (formatted-message (G_ "no signing key '~a'. \
  377. have you run 'guix archive --generate-key?'")
  378. %public-key-file)))
  379. (remote-authorize-signing-key (call-with-input-file %public-key-file
  380. (lambda (port)
  381. (string->canonical-sexp
  382. (get-string-all port))))
  383. (machine-ssh-session machine)
  384. (machine-become-command machine)))
  385. (mlet %store-monad ((_ (check-deployment-sanity machine))
  386. (boot-parameters (machine-boot-parameters machine)))
  387. (let* ((os (machine-operating-system machine))
  388. (eval (cut machine-remote-eval machine <>))
  389. (menu-entries (map boot-parameters->menu-entry boot-parameters))
  390. (bootloader-configuration (operating-system-bootloader os))
  391. (bootcfg (operating-system-bootcfg os menu-entries)))
  392. (mbegin %store-monad
  393. (with-roll-back #f
  394. (switch-to-system eval os))
  395. (with-roll-back #t
  396. (mbegin %store-monad
  397. (upgrade-shepherd-services eval os)
  398. (install-bootloader eval bootloader-configuration bootcfg)))))))
  399. ;;;
  400. ;;; Roll-back.
  401. ;;;
  402. (define (roll-back-managed-host machine)
  403. "Internal implementation of 'roll-back-machine' for MACHINE instances with
  404. an environment type of 'managed-host."
  405. (define remote-exp
  406. (with-extensions (list guile-gcrypt)
  407. (with-imported-modules (source-module-closure '((guix config)
  408. (guix profiles)))
  409. #~(begin
  410. (use-modules (guix config)
  411. (guix profiles))
  412. (define %system-profile
  413. (string-append %state-directory "/profiles/system"))
  414. (define target-generation
  415. (relative-generation %system-profile -1))
  416. (if target-generation
  417. (switch-to-generation %system-profile target-generation)
  418. 'error)))))
  419. (define roll-back-failure
  420. (condition (&message (message (G_ "could not roll-back machine")))))
  421. (mlet* %store-monad ((boot-parameters (machine-boot-parameters machine))
  422. (_ -> (if (< (length boot-parameters) 2)
  423. (raise roll-back-failure)))
  424. (entries -> (map boot-parameters->menu-entry
  425. (list (second boot-parameters))))
  426. (old-entries -> (map boot-parameters->menu-entry
  427. (drop boot-parameters 2)))
  428. (bootloader -> (operating-system-bootloader
  429. (machine-operating-system machine)))
  430. (bootcfg (lower-object
  431. ((bootloader-configuration-file-generator
  432. (bootloader-configuration-bootloader
  433. bootloader))
  434. bootloader entries
  435. #:old-entries old-entries)))
  436. (remote-result (machine-remote-eval machine remote-exp)))
  437. (when (eqv? 'error remote-result)
  438. (raise roll-back-failure))))
  439. ;;;
  440. ;;; Environment type.
  441. ;;;
  442. (define managed-host-environment-type
  443. (environment-type
  444. (machine-remote-eval managed-host-remote-eval)
  445. (deploy-machine deploy-managed-host)
  446. (roll-back-machine roll-back-managed-host)
  447. (name 'managed-host-environment-type)
  448. (description "Provisioning for machines that are accessible over SSH
  449. and have a known host-name. This entails little more than maintaining an SSH
  450. connection to the host.")))
  451. (define (maybe-raise-unsupported-configuration-error machine)
  452. "Raise an error if MACHINE's configuration is not an instance of
  453. <machine-ssh-configuration>."
  454. (let ((config (machine-configuration machine))
  455. (environment (environment-type-name (machine-environment machine))))
  456. (unless (and config (machine-ssh-configuration? config))
  457. (raise (formatted-message (G_ "unsupported machine configuration '~a'
  458. for environment of type '~a'")
  459. config
  460. environment)))))
  461. ;; Local Variables:
  462. ;; eval: (put 'remote-let 'scheme-indent-function 1)
  463. ;; End: