ssh.scm 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014-2019, 2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 David Craven <david@craven.ch>
  4. ;;; Copyright © 2016 Julien Lepiller <julien@lepiller.eu>
  5. ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
  6. ;;; Copyright © 2019 Ricardo Wurmus <rekado@elephly.net>
  7. ;;; Copyright © 2020 pinoaffe <pinoaffe@airmail.cc>
  8. ;;; Copyright © 2020 Oleg Pykhalov <go.wigust@gmail.com>
  9. ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
  10. ;;; Copyright © 2021 Tobias Geerinckx-Rice <me@tobias.gr>
  11. ;;;
  12. ;;; This file is part of GNU Guix.
  13. ;;;
  14. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  15. ;;; under the terms of the GNU General Public License as published by
  16. ;;; the Free Software Foundation; either version 3 of the License, or (at
  17. ;;; your option) any later version.
  18. ;;;
  19. ;;; GNU Guix is distributed in the hope that it will be useful, but
  20. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. ;;; GNU General Public License for more details.
  23. ;;;
  24. ;;; You should have received a copy of the GNU General Public License
  25. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  26. (define-module (gnu services ssh)
  27. #:use-module (gnu packages ssh)
  28. #:use-module (gnu packages admin)
  29. #:use-module (gnu services)
  30. #:use-module (gnu services shepherd)
  31. #:use-module (gnu services web)
  32. #:use-module (gnu system pam)
  33. #:use-module (gnu system shadow)
  34. #:use-module (guix deprecation)
  35. #:use-module (guix gexp)
  36. #:use-module (guix records)
  37. #:use-module (guix modules)
  38. #:use-module (srfi srfi-1)
  39. #:use-module (srfi srfi-26)
  40. #:use-module (ice-9 match)
  41. #:use-module (ice-9 vlist)
  42. #:export (lsh-configuration
  43. lsh-configuration?
  44. lsh-service
  45. lsh-service-type
  46. openssh-configuration
  47. openssh-configuration?
  48. openssh-service-type
  49. dropbear-configuration
  50. dropbear-configuration?
  51. dropbear-service-type
  52. dropbear-service
  53. autossh-configuration
  54. autossh-configuration?
  55. autossh-service-type
  56. webssh-configuration
  57. webssh-configuration?
  58. webssh-service-type
  59. %webssh-configuration-nginx))
  60. ;;; Commentary:
  61. ;;;
  62. ;;; This module implements secure shell (SSH) services.
  63. ;;;
  64. ;;; Code:
  65. (define-record-type* <lsh-configuration>
  66. lsh-configuration make-lsh-configuration
  67. lsh-configuration?
  68. (lsh lsh-configuration-lsh
  69. (default lsh))
  70. (daemonic? lsh-configuration-daemonic?)
  71. (host-key lsh-configuration-host-key)
  72. (interfaces lsh-configuration-interfaces)
  73. (port-number lsh-configuration-port-number)
  74. (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
  75. (root-login? lsh-configuration-root-login?)
  76. (syslog-output? lsh-configuration-syslog-output?)
  77. (pid-file? lsh-configuration-pid-file?)
  78. (pid-file lsh-configuration-pid-file)
  79. (x11-forwarding? lsh-configuration-x11-forwarding?)
  80. (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
  81. (password-authentication? lsh-configuration-password-authentication?)
  82. (public-key-authentication? lsh-configuration-public-key-authentication?)
  83. (initialize? lsh-configuration-initialize?))
  84. (define %yarrow-seed
  85. "/var/spool/lsh/yarrow-seed-file")
  86. (define (lsh-initialization lsh host-key)
  87. "Return the gexp to initialize the LSH service for HOST-KEY."
  88. #~(begin
  89. (unless (file-exists? #$%yarrow-seed)
  90. (system* (string-append #$lsh "/bin/lsh-make-seed")
  91. "--sloppy" "-o" #$%yarrow-seed))
  92. (unless (file-exists? #$host-key)
  93. (mkdir-p (dirname #$host-key))
  94. (format #t "creating SSH host key '~a'...~%" #$host-key)
  95. ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
  96. ;; used yet because /bin/sh might be dangling; factorize this somehow.
  97. (let* ((in+out (pipe))
  98. (keygen (primitive-fork)))
  99. (case keygen
  100. ((0)
  101. (close-port (car in+out))
  102. (close-fdes 1)
  103. (dup2 (fileno (cdr in+out)) 1)
  104. (execl (string-append #$lsh "/bin/lsh-keygen")
  105. "lsh-keygen" "--server"))
  106. (else
  107. (let ((write-key (primitive-fork)))
  108. (case write-key
  109. ((0)
  110. (close-port (cdr in+out))
  111. (close-fdes 0)
  112. (dup2 (fileno (car in+out)) 0)
  113. (execl (string-append #$lsh "/bin/lsh-writekey")
  114. "lsh-writekey" "--server" "-o" #$host-key))
  115. (else
  116. (close-port (car in+out))
  117. (close-port (cdr in+out))
  118. (waitpid keygen)
  119. (waitpid write-key))))))))))
  120. (define (lsh-activation config)
  121. "Return the activation gexp for CONFIG."
  122. #~(begin
  123. (use-modules (guix build utils))
  124. (mkdir-p "/var/spool/lsh")
  125. #$(if (lsh-configuration-initialize? config)
  126. (lsh-initialization (lsh-configuration-lsh config)
  127. (lsh-configuration-host-key config))
  128. #t)))
  129. (define (lsh-shepherd-service config)
  130. "Return a <shepherd-service> for lsh with CONFIG."
  131. (define lsh (lsh-configuration-lsh config))
  132. (define pid-file (lsh-configuration-pid-file config))
  133. (define pid-file? (lsh-configuration-pid-file? config))
  134. (define daemonic? (lsh-configuration-daemonic? config))
  135. (define interfaces (lsh-configuration-interfaces config))
  136. (define lsh-command
  137. (append
  138. (cons (file-append lsh "/sbin/lshd")
  139. (if daemonic?
  140. (let ((syslog (if (lsh-configuration-syslog-output? config)
  141. '()
  142. (list "--no-syslog"))))
  143. (cons "--daemonic"
  144. (if pid-file?
  145. (cons #~(string-append "--pid-file=" #$pid-file)
  146. syslog)
  147. (cons "--no-pid-file" syslog))))
  148. (if pid-file?
  149. (list #~(string-append "--pid-file=" #$pid-file))
  150. '())))
  151. (cons* #~(string-append "--host-key="
  152. #$(lsh-configuration-host-key config))
  153. #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
  154. #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
  155. "-p" (number->string (lsh-configuration-port-number config))
  156. (if (lsh-configuration-password-authentication? config)
  157. "--password" "--no-password")
  158. (if (lsh-configuration-public-key-authentication? config)
  159. "--publickey" "--no-publickey")
  160. (if (lsh-configuration-root-login? config)
  161. "--root-login" "--no-root-login")
  162. (if (lsh-configuration-x11-forwarding? config)
  163. "--x11-forward" "--no-x11-forward")
  164. (if (lsh-configuration-tcp/ip-forwarding? config)
  165. "--tcpip-forward" "--no-tcpip-forward")
  166. (if (null? interfaces)
  167. '()
  168. (map (cut string-append "--interface=" <>)
  169. interfaces)))))
  170. (define requires
  171. (if (and daemonic? (lsh-configuration-syslog-output? config))
  172. '(networking syslogd)
  173. '(networking)))
  174. (list (shepherd-service
  175. (documentation "GNU lsh SSH server")
  176. (provision '(ssh-daemon ssh sshd))
  177. (requirement requires)
  178. (start #~(make-forkexec-constructor (list #$@lsh-command)))
  179. (stop #~(make-kill-destructor)))))
  180. (define (lsh-pam-services config)
  181. "Return a list of <pam-services> for lshd with CONFIG."
  182. (list (unix-pam-service
  183. "lshd"
  184. #:login-uid? #t
  185. #:allow-empty-passwords?
  186. (lsh-configuration-allow-empty-passwords? config))))
  187. (define lsh-service-type
  188. (service-type (name 'lsh)
  189. (description
  190. "Run the GNU@tie{}lsh secure shell (SSH) daemon,
  191. @command{lshd}.")
  192. (extensions
  193. (list (service-extension shepherd-root-service-type
  194. lsh-shepherd-service)
  195. (service-extension pam-root-service-type
  196. lsh-pam-services)
  197. (service-extension activation-service-type
  198. lsh-activation)))))
  199. (define* (lsh-service #:key
  200. (lsh lsh)
  201. (daemonic? #t)
  202. (host-key "/etc/lsh/host-key")
  203. (interfaces '())
  204. (port-number 22)
  205. (allow-empty-passwords? #f)
  206. (root-login? #f)
  207. (syslog-output? #t)
  208. (pid-file? #f)
  209. (pid-file "/var/run/lshd.pid")
  210. (x11-forwarding? #t)
  211. (tcp/ip-forwarding? #t)
  212. (password-authentication? #t)
  213. (public-key-authentication? #t)
  214. (initialize? #t))
  215. "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
  216. @var{host-key} must designate a file containing the host key, and readable
  217. only by root.
  218. When @var{daemonic?} is true, @command{lshd} will detach from the
  219. controlling terminal and log its output to syslogd, unless one sets
  220. @var{syslog-output?} to false. Obviously, it also makes lsh-service
  221. depend on existence of syslogd service. When @var{pid-file?} is true,
  222. @command{lshd} writes its PID to the file called @var{pid-file}.
  223. When @var{initialize?} is true, automatically create the seed and host key
  224. upon service activation if they do not exist yet. This may take long and
  225. require interaction.
  226. When @var{initialize?} is false, it is up to the user to initialize the
  227. randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
  228. a key pair with the private key stored in file @var{host-key} (@pxref{lshd
  229. basics,,, lsh, LSH Manual}).
  230. When @var{interfaces} is empty, lshd listens for connections on all the
  231. network interfaces; otherwise, @var{interfaces} must be a list of host names
  232. or addresses.
  233. @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
  234. passwords, and @var{root-login?} specifies whether to accept log-ins as
  235. root.
  236. The other options should be self-descriptive."
  237. (service lsh-service-type
  238. (lsh-configuration (lsh lsh) (daemonic? daemonic?)
  239. (host-key host-key) (interfaces interfaces)
  240. (port-number port-number)
  241. (allow-empty-passwords? allow-empty-passwords?)
  242. (root-login? root-login?)
  243. (syslog-output? syslog-output?)
  244. (pid-file? pid-file?) (pid-file pid-file)
  245. (x11-forwarding? x11-forwarding?)
  246. (tcp/ip-forwarding? tcp/ip-forwarding?)
  247. (password-authentication?
  248. password-authentication?)
  249. (public-key-authentication?
  250. public-key-authentication?)
  251. (initialize? initialize?))))
  252. ;;;
  253. ;;; OpenSSH.
  254. ;;;
  255. (define-record-type* <openssh-configuration>
  256. openssh-configuration make-openssh-configuration
  257. openssh-configuration?
  258. ;; file-like object
  259. (openssh openssh-configuration-openssh
  260. (default openssh))
  261. ;; string
  262. (pid-file openssh-configuration-pid-file
  263. (default "/var/run/sshd.pid"))
  264. ;; integer
  265. (port-number openssh-configuration-port-number
  266. (default 22))
  267. ;; integer
  268. (max-connections openssh-configuration-max-connections
  269. (default 200))
  270. ;; Boolean | 'prohibit-password
  271. (permit-root-login openssh-configuration-permit-root-login
  272. (default #f))
  273. ;; Boolean
  274. (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
  275. (default #f))
  276. ;; Boolean
  277. (password-authentication? openssh-configuration-password-authentication?
  278. (default #t))
  279. ;; Boolean
  280. (public-key-authentication? openssh-configuration-public-key-authentication?
  281. (default #t))
  282. ;; Boolean
  283. (x11-forwarding? openssh-configuration-x11-forwarding?
  284. (default #f))
  285. ;; Boolean
  286. (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
  287. (default #t))
  288. ;; Boolean
  289. (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
  290. (default #t))
  291. ;; Boolean
  292. (gateway-ports? openssh-configuration-gateway-ports?
  293. (default #f))
  294. ;; Boolean
  295. (challenge-response-authentication? openssh-challenge-response-authentication?
  296. (default #f))
  297. ;; Boolean
  298. (use-pam? openssh-configuration-use-pam?
  299. (default #t))
  300. ;; Boolean
  301. (print-last-log? openssh-configuration-print-last-log?
  302. (default #t))
  303. ;; list of two-element lists
  304. (subsystems openssh-configuration-subsystems
  305. (default '(("sftp" "internal-sftp"))))
  306. ;; list of strings
  307. (accepted-environment openssh-configuration-accepted-environment
  308. (default '()))
  309. ;; symbol
  310. (log-level openssh-configuration-log-level
  311. (default 'info))
  312. ;; String
  313. ;; This is an "escape hatch" to provide configuration that isn't yet
  314. ;; supported by this configuration record.
  315. (extra-content openssh-configuration-extra-content
  316. (default ""))
  317. ;; list of user-name/file-like tuples
  318. (authorized-keys openssh-configuration-authorized-keys
  319. (default '()))
  320. ;; Boolean
  321. (generate-host-keys? openssh-configuration-generate-host-keys?
  322. (default #t))
  323. ;; Boolean
  324. ;; XXX: This should really be handled in an orthogonal way, for instance as
  325. ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
  326. ;; for now.
  327. (%auto-start? openssh-auto-start?
  328. (default #t)))
  329. (define %openssh-accounts
  330. (list (user-group (name "sshd") (system? #t))
  331. (user-account
  332. (name "sshd")
  333. (group "sshd")
  334. (system? #t)
  335. (comment "sshd privilege separation user")
  336. (home-directory "/var/run/sshd")
  337. (shell (file-append shadow "/sbin/nologin")))))
  338. (define (openssh-activation config)
  339. "Return the activation GEXP for CONFIG."
  340. (with-imported-modules '((guix build utils))
  341. #~(begin
  342. (use-modules (guix build utils))
  343. (define (touch file-name)
  344. (call-with-output-file file-name (const #t)))
  345. ;; Make sure /etc/ssh can be read by the 'sshd' user.
  346. (mkdir-p "/etc/ssh")
  347. (chmod "/etc/ssh" #o755)
  348. (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
  349. ;; 'sshd' complains if the authorized-key directory and its parents
  350. ;; are group-writable, which rules out /gnu/store. Thus we copy the
  351. ;; authorized-key directory to /etc.
  352. (catch 'system-error
  353. (lambda ()
  354. (delete-file-recursively "/etc/authorized_keys.d"))
  355. (lambda args
  356. (unless (= ENOENT (system-error-errno args))
  357. (apply throw args))))
  358. (copy-recursively #$(authorized-key-directory
  359. (openssh-configuration-authorized-keys config))
  360. "/etc/ssh/authorized_keys.d")
  361. (chmod "/etc/ssh/authorized_keys.d" #o555)
  362. (let ((lastlog "/var/log/lastlog"))
  363. (when #$(openssh-configuration-print-last-log? config)
  364. (unless (file-exists? lastlog)
  365. (touch lastlog))))
  366. (when #$(openssh-configuration-generate-host-keys? config)
  367. ;; Generate missing host keys.
  368. (system* (string-append #$(openssh-configuration-openssh config)
  369. "/bin/ssh-keygen") "-A")))))
  370. (define (authorized-key-directory keys)
  371. "Return a directory containing the authorized keys specified in KEYS, a list
  372. of user-name/file-like tuples."
  373. (define build
  374. (with-imported-modules (source-module-closure '((guix build utils)))
  375. #~(begin
  376. (use-modules (ice-9 match) (srfi srfi-26)
  377. (guix build utils))
  378. (mkdir #$output)
  379. (for-each (match-lambda
  380. ((user keys ...)
  381. (let ((file (string-append #$output "/" user)))
  382. (call-with-output-file file
  383. (lambda (port)
  384. (for-each (lambda (key)
  385. (call-with-input-file key
  386. (cut dump-port <> port)))
  387. keys))))))
  388. '#$keys))))
  389. (computed-file "openssh-authorized-keys" build))
  390. (define (openssh-config-file config)
  391. "Return the sshd configuration file corresponding to CONFIG."
  392. (computed-file
  393. "sshd_config"
  394. #~(begin
  395. (use-modules (ice-9 match))
  396. (call-with-output-file #$output
  397. (lambda (port)
  398. (display "# Generated by 'openssh-service'.\n" port)
  399. (format port "Port ~a\n"
  400. #$(number->string
  401. (openssh-configuration-port-number config)))
  402. (format port "PermitRootLogin ~a\n"
  403. #$(match (openssh-configuration-permit-root-login config)
  404. (#t "yes")
  405. (#f "no")
  406. ('without-password (warn-about-deprecation
  407. 'without-password #f
  408. #:replacement 'prohibit-password)
  409. "prohibit-password")
  410. ('prohibit-password "prohibit-password")))
  411. (format port "PermitEmptyPasswords ~a\n"
  412. #$(if (openssh-configuration-allow-empty-passwords? config)
  413. "yes" "no"))
  414. (format port "PasswordAuthentication ~a\n"
  415. #$(if (openssh-configuration-password-authentication? config)
  416. "yes" "no"))
  417. (format port "PubkeyAuthentication ~a\n"
  418. #$(if (openssh-configuration-public-key-authentication?
  419. config)
  420. "yes" "no"))
  421. (format port "X11Forwarding ~a\n"
  422. #$(if (openssh-configuration-x11-forwarding? config)
  423. "yes" "no"))
  424. (format port "AllowAgentForwarding ~a\n"
  425. #$(if (openssh-configuration-allow-agent-forwarding? config)
  426. "yes" "no"))
  427. (format port "AllowTcpForwarding ~a\n"
  428. #$(if (openssh-configuration-allow-tcp-forwarding? config)
  429. "yes" "no"))
  430. (format port "GatewayPorts ~a\n"
  431. #$(if (openssh-configuration-gateway-ports? config)
  432. "yes" "no"))
  433. (format port "PidFile ~a\n"
  434. #$(openssh-configuration-pid-file config))
  435. (format port "ChallengeResponseAuthentication ~a\n"
  436. #$(if (openssh-challenge-response-authentication? config)
  437. "yes" "no"))
  438. (format port "UsePAM ~a\n"
  439. #$(if (openssh-configuration-use-pam? config)
  440. "yes" "no"))
  441. (format port "PrintLastLog ~a\n"
  442. #$(if (openssh-configuration-print-last-log? config)
  443. "yes" "no"))
  444. (format port "LogLevel ~a\n"
  445. #$(string-upcase
  446. (symbol->string
  447. (openssh-configuration-log-level config))))
  448. ;; Add '/etc/authorized_keys.d/%u', which we populate.
  449. (format port "AuthorizedKeysFile \
  450. .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
  451. (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
  452. '#$(openssh-configuration-accepted-environment config))
  453. (for-each
  454. (match-lambda
  455. ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
  456. '#$(openssh-configuration-subsystems config))
  457. (format port "~a\n"
  458. #$(openssh-configuration-extra-content config))
  459. #t)))))
  460. (define (openssh-shepherd-service config)
  461. "Return a <shepherd-service> for openssh with CONFIG."
  462. (define pid-file
  463. (openssh-configuration-pid-file config))
  464. (define port-number
  465. (openssh-configuration-port-number config))
  466. (define max-connections
  467. (openssh-configuration-max-connections config))
  468. (define openssh-command
  469. #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
  470. "-D" "-f" #$(openssh-config-file config)))
  471. (list (shepherd-service
  472. (documentation "OpenSSH server.")
  473. (requirement '(syslogd loopback))
  474. (provision '(ssh-daemon ssh sshd))
  475. (start #~(if (defined? 'make-inetd-constructor)
  476. (make-inetd-constructor
  477. (append #$openssh-command '("-i"))
  478. (make-socket-address AF_INET INADDR_ANY
  479. #$port-number)
  480. #:max-connections #$max-connections)
  481. (make-forkexec-constructor #$openssh-command
  482. #:pid-file #$pid-file)))
  483. (stop #~(if (defined? 'make-inetd-destructor)
  484. (make-inetd-destructor)
  485. (make-kill-destructor)))
  486. (auto-start? (openssh-auto-start? config)))))
  487. (define (openssh-pam-services config)
  488. "Return a list of <pam-services> for sshd with CONFIG."
  489. (list (unix-pam-service
  490. "sshd"
  491. #:login-uid? #t
  492. #:allow-empty-passwords?
  493. (openssh-configuration-allow-empty-passwords? config))))
  494. (define (extend-openssh-authorized-keys config keys)
  495. "Extend CONFIG with the extra authorized keys listed in KEYS."
  496. (openssh-configuration
  497. (inherit config)
  498. (authorized-keys
  499. (match (openssh-configuration-authorized-keys config)
  500. (((users _ ...) ...)
  501. ;; Build a user/key-list mapping.
  502. (let ((user-keys (alist->vhash
  503. (openssh-configuration-authorized-keys config))))
  504. ;; Coalesce the key lists associated with each user.
  505. (map (lambda (user)
  506. `(,user
  507. ,@(concatenate (vhash-fold* cons '() user user-keys))))
  508. users)))))))
  509. (define openssh-service-type
  510. (service-type (name 'openssh)
  511. (description
  512. "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
  513. (extensions
  514. (list (service-extension shepherd-root-service-type
  515. openssh-shepherd-service)
  516. (service-extension pam-root-service-type
  517. openssh-pam-services)
  518. (service-extension activation-service-type
  519. openssh-activation)
  520. (service-extension account-service-type
  521. (const %openssh-accounts))
  522. ;; Install OpenSSH in the system profile. That way,
  523. ;; 'scp' is found when someone tries to copy to or from
  524. ;; this machine.
  525. (service-extension profile-service-type
  526. (lambda (config)
  527. (list (openssh-configuration-openssh
  528. config))))))
  529. (compose concatenate)
  530. (extend extend-openssh-authorized-keys)
  531. (default-value (openssh-configuration))))
  532. ;;;
  533. ;;; Dropbear.
  534. ;;;
  535. (define-record-type* <dropbear-configuration>
  536. dropbear-configuration make-dropbear-configuration
  537. dropbear-configuration?
  538. (dropbear dropbear-configuration-dropbear
  539. (default dropbear))
  540. (port-number dropbear-configuration-port-number
  541. (default 22))
  542. (syslog-output? dropbear-configuration-syslog-output?
  543. (default #t))
  544. (pid-file dropbear-configuration-pid-file
  545. (default "/var/run/dropbear.pid"))
  546. (root-login? dropbear-configuration-root-login?
  547. (default #f))
  548. (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
  549. (default #f))
  550. (password-authentication? dropbear-configuration-password-authentication?
  551. (default #t)))
  552. (define (dropbear-activation config)
  553. "Return the activation gexp for CONFIG."
  554. #~(begin
  555. (use-modules (guix build utils))
  556. (mkdir-p "/etc/dropbear")))
  557. (define (dropbear-shepherd-service config)
  558. "Return a <shepherd-service> for dropbear with CONFIG."
  559. (define dropbear
  560. (dropbear-configuration-dropbear config))
  561. (define pid-file
  562. (dropbear-configuration-pid-file config))
  563. (define dropbear-command
  564. #~(list (string-append #$dropbear "/sbin/dropbear")
  565. ;; '-R' allows host keys to be automatically generated upon first
  566. ;; connection, at a time when /dev/urandom is more likely securely
  567. ;; seeded.
  568. "-F" "-R"
  569. "-p" #$(number->string (dropbear-configuration-port-number config))
  570. "-P" #$pid-file
  571. #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
  572. #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
  573. #$@(if (dropbear-configuration-password-authentication? config)
  574. '()
  575. '("-s" "-g"))
  576. #$@(if (dropbear-configuration-allow-empty-passwords? config)
  577. '("-B")
  578. '())))
  579. (define requires
  580. (if (dropbear-configuration-syslog-output? config)
  581. '(networking syslogd) '(networking)))
  582. (list (shepherd-service
  583. (documentation "Dropbear SSH server.")
  584. (requirement requires)
  585. (provision '(ssh-daemon ssh sshd))
  586. (start #~(make-forkexec-constructor #$dropbear-command
  587. #:pid-file #$pid-file))
  588. (stop #~(make-kill-destructor)))))
  589. (define dropbear-service-type
  590. (service-type (name 'dropbear)
  591. (description
  592. "Run the Dropbear secure shell (SSH) server.")
  593. (extensions
  594. (list (service-extension shepherd-root-service-type
  595. dropbear-shepherd-service)
  596. (service-extension activation-service-type
  597. dropbear-activation)))
  598. (default-value (dropbear-configuration))))
  599. (define* (dropbear-service #:optional (config (dropbear-configuration)))
  600. "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
  601. daemon} with the given @var{config}, a @code{<dropbear-configuration>}
  602. object."
  603. (service dropbear-service-type config))
  604. ;;;
  605. ;;; AutoSSH.
  606. ;;;
  607. (define-record-type* <autossh-configuration>
  608. autossh-configuration make-autossh-configuration
  609. autossh-configuration?
  610. (user autossh-configuration-user
  611. (default "autossh"))
  612. (poll autossh-configuration-poll
  613. (default 600))
  614. (first-poll autossh-configuration-first-poll
  615. (default #f))
  616. (gate-time autossh-configuration-gate-time
  617. (default 30))
  618. (log-level autossh-configuration-log-level
  619. (default 1))
  620. (max-start autossh-configuration-max-start
  621. (default #f))
  622. (message autossh-configuration-message
  623. (default ""))
  624. (port autossh-configuration-port
  625. (default "0"))
  626. (ssh-options autossh-configuration-ssh-options
  627. (default '())))
  628. (define (autossh-file-name config file)
  629. "Return a path in /var/run/autossh/ that is writable
  630. by @code{user} from @code{config}."
  631. (string-append "/var/run/autossh/"
  632. (autossh-configuration-user config)
  633. "/" file))
  634. (define (autossh-shepherd-service config)
  635. (shepherd-service
  636. (documentation "Automatically set up ssh connections (and keep them alive).")
  637. (provision '(autossh))
  638. (start #~(make-forkexec-constructor
  639. (list #$(file-append autossh "/bin/autossh")
  640. #$@(autossh-configuration-ssh-options config))
  641. #:user #$(autossh-configuration-user config)
  642. #:group (passwd:gid (getpw #$(autossh-configuration-user config)))
  643. #:pid-file #$(autossh-file-name config "pid")
  644. #:log-file #$(autossh-file-name config "log")
  645. #:environment-variables
  646. '(#$(string-append "AUTOSSH_PIDFILE="
  647. (autossh-file-name config "pid"))
  648. #$(string-append "AUTOSSH_LOGFILE="
  649. (autossh-file-name config "log"))
  650. #$(string-append "AUTOSSH_POLL="
  651. (number->string
  652. (autossh-configuration-poll config)))
  653. #$(string-append "AUTOSSH_FIRST_POLL="
  654. (number->string
  655. (or
  656. (autossh-configuration-first-poll config)
  657. (autossh-configuration-poll config))))
  658. #$(string-append "AUTOSSH_GATETIME="
  659. (number->string
  660. (autossh-configuration-gate-time config)))
  661. #$(string-append "AUTOSSH_LOGLEVEL="
  662. (number->string
  663. (autossh-configuration-log-level config)))
  664. #$(string-append "AUTOSSH_MAXSTART="
  665. (number->string
  666. (or (autossh-configuration-max-start config)
  667. -1)))
  668. #$(string-append "AUTOSSH_MESSAGE="
  669. (autossh-configuration-message config))
  670. #$(string-append "AUTOSSH_PORT="
  671. (autossh-configuration-port config)))))
  672. (stop #~(make-kill-destructor))))
  673. (define (autossh-service-activation config)
  674. (with-imported-modules '((guix build utils))
  675. #~(begin
  676. (use-modules (guix build utils))
  677. (define %user
  678. (getpw #$(autossh-configuration-user config)))
  679. (let* ((directory #$(autossh-file-name config ""))
  680. (log (string-append directory "/log")))
  681. (mkdir-p directory)
  682. (chown directory (passwd:uid %user) (passwd:gid %user))
  683. (call-with-output-file log (const #t))
  684. (chown log (passwd:uid %user) (passwd:gid %user))))))
  685. (define autossh-service-type
  686. (service-type
  687. (name 'autossh)
  688. (description "Automatically set up ssh connections (and keep them alive).")
  689. (extensions
  690. (list (service-extension shepherd-root-service-type
  691. (compose list autossh-shepherd-service))
  692. (service-extension activation-service-type
  693. autossh-service-activation)))
  694. (default-value (autossh-configuration))))
  695. ;;;
  696. ;;; WebSSH
  697. ;;;
  698. (define-record-type* <webssh-configuration>
  699. webssh-configuration make-webssh-configuration
  700. webssh-configuration?
  701. (package webssh-configuration-package ;file-like
  702. (default webssh))
  703. (user-name webssh-configuration-user-name ;string
  704. (default "webssh"))
  705. (group-name webssh-configuration-group-name ;string
  706. (default "webssh"))
  707. (policy webssh-configuration-policy ;symbol
  708. (default #f))
  709. (known-hosts webssh-configuration-known-hosts ;list of strings
  710. (default #f))
  711. (port webssh-configuration-port ;number
  712. (default #f))
  713. (address webssh-configuration-address ;string
  714. (default #f))
  715. (log-file webssh-configuration-log-file ;string
  716. (default "/var/log/webssh.log"))
  717. (log-level webssh-configuration-log-level ;symbol
  718. (default #f)))
  719. (define %webssh-configuration-nginx
  720. (nginx-server-configuration
  721. (listen '("80"))
  722. (locations
  723. (list (nginx-location-configuration
  724. (uri "/")
  725. (body '("proxy_pass http://127.0.0.1:8888;"
  726. "proxy_http_version 1.1;"
  727. "proxy_read_timeout 300;"
  728. "proxy_set_header Upgrade $http_upgrade;"
  729. "proxy_set_header Connection \"upgrade\";"
  730. "proxy_set_header Host $http_host;"
  731. "proxy_set_header X-Real-IP $remote_addr;"
  732. "proxy_set_header X-Real-PORT $remote_port;")))))))
  733. (define webssh-account
  734. ;; Return the user accounts and user groups for CONFIG.
  735. (match-lambda
  736. (($ <webssh-configuration> _ user-name group-name _ _ _ _ _ _)
  737. (list (user-group
  738. (name group-name))
  739. (user-account
  740. (name user-name)
  741. (group group-name)
  742. (comment "webssh privilege separation user")
  743. (home-directory (string-append "/var/run/" user-name))
  744. (shell #~(string-append #$shadow "/sbin/nologin")))))))
  745. (define webssh-activation
  746. ;; Return the activation GEXP for CONFIG.
  747. (match-lambda
  748. (($ <webssh-configuration> _ user-name group-name policy known-hosts _ _
  749. log-file _)
  750. (with-imported-modules '((guix build utils))
  751. #~(begin
  752. (let* ((home-dir (string-append "/var/run/" #$user-name))
  753. (ssh-dir (string-append home-dir "/.ssh"))
  754. (known-hosts-file (string-append ssh-dir "/known_hosts")))
  755. (call-with-output-file #$log-file (const #t))
  756. (mkdir-p ssh-dir)
  757. (case '#$policy
  758. ((reject)
  759. (if '#$known-hosts
  760. (call-with-output-file known-hosts-file
  761. (lambda (port)
  762. (for-each (lambda (host) (display host port) (newline port))
  763. '#$known-hosts)))
  764. (display-hint (G_ "webssh: reject policy requires `known-hosts'.")))))
  765. (for-each (lambda (file)
  766. (chown file
  767. (passwd:uid (getpw #$user-name))
  768. (group:gid (getpw #$group-name))))
  769. (list #$log-file ssh-dir known-hosts-file))
  770. (chmod ssh-dir #o700)))))))
  771. (define webssh-shepherd-service
  772. (match-lambda
  773. (($ <webssh-configuration> package user-name group-name policy _ port
  774. address log-file log-level)
  775. (list (shepherd-service
  776. (provision '(webssh))
  777. (documentation "Run webssh daemon.")
  778. (start #~(make-forkexec-constructor
  779. `(,(string-append #$webssh "/bin/wssh")
  780. ,(string-append "--log-file-prefix=" #$log-file)
  781. ,@(case '#$log-level
  782. ((debug) '("--logging=debug"))
  783. (else '()))
  784. ,@(case '#$policy
  785. ((reject) '("--policy=reject"))
  786. (else '()))
  787. ,@(if #$port
  788. (list (string-append "--port=" (number->string #$port)))
  789. '())
  790. ,@(if #$address
  791. (list (string-append "--address=" #$address))
  792. '()))
  793. #:user #$user-name
  794. #:group #$group-name))
  795. (stop #~(make-kill-destructor)))))))
  796. (define webssh-service-type
  797. (service-type
  798. (name 'webssh)
  799. (extensions
  800. (list (service-extension shepherd-root-service-type
  801. webssh-shepherd-service)
  802. (service-extension account-service-type
  803. webssh-account)
  804. (service-extension activation-service-type
  805. webssh-activation)))
  806. (default-value (webssh-configuration))
  807. (description
  808. "Run the webssh.")))
  809. ;;; ssh.scm ends here