ssh.scm 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. ;; Boolean | 'prohibit-password
  268. (permit-root-login openssh-configuration-permit-root-login
  269. (default #f))
  270. ;; Boolean
  271. (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
  272. (default #f))
  273. ;; Boolean
  274. (password-authentication? openssh-configuration-password-authentication?
  275. (default #t))
  276. ;; Boolean
  277. (public-key-authentication? openssh-configuration-public-key-authentication?
  278. (default #t))
  279. ;; Boolean
  280. (x11-forwarding? openssh-configuration-x11-forwarding?
  281. (default #f))
  282. ;; Boolean
  283. (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
  284. (default #t))
  285. ;; Boolean
  286. (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
  287. (default #t))
  288. ;; Boolean
  289. (gateway-ports? openssh-configuration-gateway-ports?
  290. (default #f))
  291. ;; Boolean
  292. (challenge-response-authentication? openssh-challenge-response-authentication?
  293. (default #f))
  294. ;; Boolean
  295. (use-pam? openssh-configuration-use-pam?
  296. (default #t))
  297. ;; Boolean
  298. (print-last-log? openssh-configuration-print-last-log?
  299. (default #t))
  300. ;; list of two-element lists
  301. (subsystems openssh-configuration-subsystems
  302. (default '(("sftp" "internal-sftp"))))
  303. ;; list of strings
  304. (accepted-environment openssh-configuration-accepted-environment
  305. (default '()))
  306. ;; symbol
  307. (log-level openssh-configuration-log-level
  308. (default 'info))
  309. ;; String
  310. ;; This is an "escape hatch" to provide configuration that isn't yet
  311. ;; supported by this configuration record.
  312. (extra-content openssh-configuration-extra-content
  313. (default ""))
  314. ;; list of user-name/file-like tuples
  315. (authorized-keys openssh-configuration-authorized-keys
  316. (default '()))
  317. ;; Boolean
  318. (generate-host-keys? openssh-configuration-generate-host-keys?
  319. (default #t))
  320. ;; Boolean
  321. ;; XXX: This should really be handled in an orthogonal way, for instance as
  322. ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
  323. ;; for now.
  324. (%auto-start? openssh-auto-start?
  325. (default #t)))
  326. (define %openssh-accounts
  327. (list (user-group (name "sshd") (system? #t))
  328. (user-account
  329. (name "sshd")
  330. (group "sshd")
  331. (system? #t)
  332. (comment "sshd privilege separation user")
  333. (home-directory "/var/run/sshd")
  334. (shell (file-append shadow "/sbin/nologin")))))
  335. (define (openssh-activation config)
  336. "Return the activation GEXP for CONFIG."
  337. (with-imported-modules '((guix build utils))
  338. #~(begin
  339. (use-modules (guix build utils))
  340. (define (touch file-name)
  341. (call-with-output-file file-name (const #t)))
  342. ;; Make sure /etc/ssh can be read by the 'sshd' user.
  343. (mkdir-p "/etc/ssh")
  344. (chmod "/etc/ssh" #o755)
  345. (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
  346. ;; 'sshd' complains if the authorized-key directory and its parents
  347. ;; are group-writable, which rules out /gnu/store. Thus we copy the
  348. ;; authorized-key directory to /etc.
  349. (catch 'system-error
  350. (lambda ()
  351. (delete-file-recursively "/etc/authorized_keys.d"))
  352. (lambda args
  353. (unless (= ENOENT (system-error-errno args))
  354. (apply throw args))))
  355. (copy-recursively #$(authorized-key-directory
  356. (openssh-configuration-authorized-keys config))
  357. "/etc/ssh/authorized_keys.d")
  358. (chmod "/etc/ssh/authorized_keys.d" #o555)
  359. (let ((lastlog "/var/log/lastlog"))
  360. (when #$(openssh-configuration-print-last-log? config)
  361. (unless (file-exists? lastlog)
  362. (touch lastlog))))
  363. (when #$(openssh-configuration-generate-host-keys? config)
  364. ;; Generate missing host keys.
  365. (system* (string-append #$(openssh-configuration-openssh config)
  366. "/bin/ssh-keygen") "-A")))))
  367. (define (authorized-key-directory keys)
  368. "Return a directory containing the authorized keys specified in KEYS, a list
  369. of user-name/file-like tuples."
  370. (define build
  371. (with-imported-modules (source-module-closure '((guix build utils)))
  372. #~(begin
  373. (use-modules (ice-9 match) (srfi srfi-26)
  374. (guix build utils))
  375. (mkdir #$output)
  376. (for-each (match-lambda
  377. ((user keys ...)
  378. (let ((file (string-append #$output "/" user)))
  379. (call-with-output-file file
  380. (lambda (port)
  381. (for-each (lambda (key)
  382. (call-with-input-file key
  383. (cut dump-port <> port)))
  384. keys))))))
  385. '#$keys))))
  386. (computed-file "openssh-authorized-keys" build))
  387. (define (openssh-config-file config)
  388. "Return the sshd configuration file corresponding to CONFIG."
  389. (computed-file
  390. "sshd_config"
  391. #~(begin
  392. (use-modules (ice-9 match))
  393. (call-with-output-file #$output
  394. (lambda (port)
  395. (display "# Generated by 'openssh-service'.\n" port)
  396. (format port "Port ~a\n"
  397. #$(number->string
  398. (openssh-configuration-port-number config)))
  399. (format port "PermitRootLogin ~a\n"
  400. #$(match (openssh-configuration-permit-root-login config)
  401. (#t "yes")
  402. (#f "no")
  403. ('without-password (warn-about-deprecation
  404. 'without-password #f
  405. #:replacement 'prohibit-password)
  406. "prohibit-password")
  407. ('prohibit-password "prohibit-password")))
  408. (format port "PermitEmptyPasswords ~a\n"
  409. #$(if (openssh-configuration-allow-empty-passwords? config)
  410. "yes" "no"))
  411. (format port "PasswordAuthentication ~a\n"
  412. #$(if (openssh-configuration-password-authentication? config)
  413. "yes" "no"))
  414. (format port "PubkeyAuthentication ~a\n"
  415. #$(if (openssh-configuration-public-key-authentication?
  416. config)
  417. "yes" "no"))
  418. (format port "X11Forwarding ~a\n"
  419. #$(if (openssh-configuration-x11-forwarding? config)
  420. "yes" "no"))
  421. (format port "AllowAgentForwarding ~a\n"
  422. #$(if (openssh-configuration-allow-agent-forwarding? config)
  423. "yes" "no"))
  424. (format port "AllowTcpForwarding ~a\n"
  425. #$(if (openssh-configuration-allow-tcp-forwarding? config)
  426. "yes" "no"))
  427. (format port "GatewayPorts ~a\n"
  428. #$(if (openssh-configuration-gateway-ports? config)
  429. "yes" "no"))
  430. (format port "PidFile ~a\n"
  431. #$(openssh-configuration-pid-file config))
  432. (format port "ChallengeResponseAuthentication ~a\n"
  433. #$(if (openssh-challenge-response-authentication? config)
  434. "yes" "no"))
  435. (format port "UsePAM ~a\n"
  436. #$(if (openssh-configuration-use-pam? config)
  437. "yes" "no"))
  438. (format port "PrintLastLog ~a\n"
  439. #$(if (openssh-configuration-print-last-log? config)
  440. "yes" "no"))
  441. (format port "LogLevel ~a\n"
  442. #$(string-upcase
  443. (symbol->string
  444. (openssh-configuration-log-level config))))
  445. ;; Add '/etc/authorized_keys.d/%u', which we populate.
  446. (format port "AuthorizedKeysFile \
  447. .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
  448. (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
  449. '#$(openssh-configuration-accepted-environment config))
  450. (for-each
  451. (match-lambda
  452. ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
  453. '#$(openssh-configuration-subsystems config))
  454. (format port "~a\n"
  455. #$(openssh-configuration-extra-content config))
  456. #t)))))
  457. (define (openssh-shepherd-service config)
  458. "Return a <shepherd-service> for openssh with CONFIG."
  459. (define pid-file
  460. (openssh-configuration-pid-file config))
  461. (define openssh-command
  462. #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
  463. "-D" "-f" #$(openssh-config-file config)))
  464. (list (shepherd-service
  465. (documentation "OpenSSH server.")
  466. (requirement '(syslogd loopback))
  467. (provision '(ssh-daemon ssh sshd))
  468. (start #~(make-forkexec-constructor #$openssh-command
  469. #:pid-file #$pid-file))
  470. (stop #~(make-kill-destructor))
  471. (auto-start? (openssh-auto-start? config)))))
  472. (define (openssh-pam-services config)
  473. "Return a list of <pam-services> for sshd with CONFIG."
  474. (list (unix-pam-service
  475. "sshd"
  476. #:login-uid? #t
  477. #:allow-empty-passwords?
  478. (openssh-configuration-allow-empty-passwords? config))))
  479. (define (extend-openssh-authorized-keys config keys)
  480. "Extend CONFIG with the extra authorized keys listed in KEYS."
  481. (openssh-configuration
  482. (inherit config)
  483. (authorized-keys
  484. (match (openssh-configuration-authorized-keys config)
  485. (((users _ ...) ...)
  486. ;; Build a user/key-list mapping.
  487. (let ((user-keys (alist->vhash
  488. (openssh-configuration-authorized-keys config))))
  489. ;; Coalesce the key lists associated with each user.
  490. (map (lambda (user)
  491. `(,user
  492. ,@(concatenate (vhash-fold* cons '() user user-keys))))
  493. users)))))))
  494. (define openssh-service-type
  495. (service-type (name 'openssh)
  496. (description
  497. "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
  498. (extensions
  499. (list (service-extension shepherd-root-service-type
  500. openssh-shepherd-service)
  501. (service-extension pam-root-service-type
  502. openssh-pam-services)
  503. (service-extension activation-service-type
  504. openssh-activation)
  505. (service-extension account-service-type
  506. (const %openssh-accounts))
  507. ;; Install OpenSSH in the system profile. That way,
  508. ;; 'scp' is found when someone tries to copy to or from
  509. ;; this machine.
  510. (service-extension profile-service-type
  511. (lambda (config)
  512. (list (openssh-configuration-openssh
  513. config))))))
  514. (compose concatenate)
  515. (extend extend-openssh-authorized-keys)
  516. (default-value (openssh-configuration))))
  517. ;;;
  518. ;;; Dropbear.
  519. ;;;
  520. (define-record-type* <dropbear-configuration>
  521. dropbear-configuration make-dropbear-configuration
  522. dropbear-configuration?
  523. (dropbear dropbear-configuration-dropbear
  524. (default dropbear))
  525. (port-number dropbear-configuration-port-number
  526. (default 22))
  527. (syslog-output? dropbear-configuration-syslog-output?
  528. (default #t))
  529. (pid-file dropbear-configuration-pid-file
  530. (default "/var/run/dropbear.pid"))
  531. (root-login? dropbear-configuration-root-login?
  532. (default #f))
  533. (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
  534. (default #f))
  535. (password-authentication? dropbear-configuration-password-authentication?
  536. (default #t)))
  537. (define (dropbear-activation config)
  538. "Return the activation gexp for CONFIG."
  539. #~(begin
  540. (use-modules (guix build utils))
  541. (mkdir-p "/etc/dropbear")))
  542. (define (dropbear-shepherd-service config)
  543. "Return a <shepherd-service> for dropbear with CONFIG."
  544. (define dropbear
  545. (dropbear-configuration-dropbear config))
  546. (define pid-file
  547. (dropbear-configuration-pid-file config))
  548. (define dropbear-command
  549. #~(list (string-append #$dropbear "/sbin/dropbear")
  550. ;; '-R' allows host keys to be automatically generated upon first
  551. ;; connection, at a time when /dev/urandom is more likely securely
  552. ;; seeded.
  553. "-F" "-R"
  554. "-p" #$(number->string (dropbear-configuration-port-number config))
  555. "-P" #$pid-file
  556. #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
  557. #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
  558. #$@(if (dropbear-configuration-password-authentication? config)
  559. '()
  560. '("-s" "-g"))
  561. #$@(if (dropbear-configuration-allow-empty-passwords? config)
  562. '("-B")
  563. '())))
  564. (define requires
  565. (if (dropbear-configuration-syslog-output? config)
  566. '(networking syslogd) '(networking)))
  567. (list (shepherd-service
  568. (documentation "Dropbear SSH server.")
  569. (requirement requires)
  570. (provision '(ssh-daemon ssh sshd))
  571. (start #~(make-forkexec-constructor #$dropbear-command
  572. #:pid-file #$pid-file))
  573. (stop #~(make-kill-destructor)))))
  574. (define dropbear-service-type
  575. (service-type (name 'dropbear)
  576. (description
  577. "Run the Dropbear secure shell (SSH) server.")
  578. (extensions
  579. (list (service-extension shepherd-root-service-type
  580. dropbear-shepherd-service)
  581. (service-extension activation-service-type
  582. dropbear-activation)))
  583. (default-value (dropbear-configuration))))
  584. (define* (dropbear-service #:optional (config (dropbear-configuration)))
  585. "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
  586. daemon} with the given @var{config}, a @code{<dropbear-configuration>}
  587. object."
  588. (service dropbear-service-type config))
  589. ;;;
  590. ;;; AutoSSH.
  591. ;;;
  592. (define-record-type* <autossh-configuration>
  593. autossh-configuration make-autossh-configuration
  594. autossh-configuration?
  595. (user autossh-configuration-user
  596. (default "autossh"))
  597. (poll autossh-configuration-poll
  598. (default 600))
  599. (first-poll autossh-configuration-first-poll
  600. (default #f))
  601. (gate-time autossh-configuration-gate-time
  602. (default 30))
  603. (log-level autossh-configuration-log-level
  604. (default 1))
  605. (max-start autossh-configuration-max-start
  606. (default #f))
  607. (message autossh-configuration-message
  608. (default ""))
  609. (port autossh-configuration-port
  610. (default "0"))
  611. (ssh-options autossh-configuration-ssh-options
  612. (default '())))
  613. (define (autossh-file-name config file)
  614. "Return a path in /var/run/autossh/ that is writable
  615. by @code{user} from @code{config}."
  616. (string-append "/var/run/autossh/"
  617. (autossh-configuration-user config)
  618. "/" file))
  619. (define (autossh-shepherd-service config)
  620. (shepherd-service
  621. (documentation "Automatically set up ssh connections (and keep them alive).")
  622. (provision '(autossh))
  623. (start #~(make-forkexec-constructor
  624. (list #$(file-append autossh "/bin/autossh")
  625. #$@(autossh-configuration-ssh-options config))
  626. #:user #$(autossh-configuration-user config)
  627. #:group (passwd:gid (getpw #$(autossh-configuration-user config)))
  628. #:pid-file #$(autossh-file-name config "pid")
  629. #:log-file #$(autossh-file-name config "log")
  630. #:environment-variables
  631. '(#$(string-append "AUTOSSH_PIDFILE="
  632. (autossh-file-name config "pid"))
  633. #$(string-append "AUTOSSH_LOGFILE="
  634. (autossh-file-name config "log"))
  635. #$(string-append "AUTOSSH_POLL="
  636. (number->string
  637. (autossh-configuration-poll config)))
  638. #$(string-append "AUTOSSH_FIRST_POLL="
  639. (number->string
  640. (or
  641. (autossh-configuration-first-poll config)
  642. (autossh-configuration-poll config))))
  643. #$(string-append "AUTOSSH_GATETIME="
  644. (number->string
  645. (autossh-configuration-gate-time config)))
  646. #$(string-append "AUTOSSH_LOGLEVEL="
  647. (number->string
  648. (autossh-configuration-log-level config)))
  649. #$(string-append "AUTOSSH_MAXSTART="
  650. (number->string
  651. (or (autossh-configuration-max-start config)
  652. -1)))
  653. #$(string-append "AUTOSSH_MESSAGE="
  654. (autossh-configuration-message config))
  655. #$(string-append "AUTOSSH_PORT="
  656. (autossh-configuration-port config)))))
  657. (stop #~(make-kill-destructor))))
  658. (define (autossh-service-activation config)
  659. (with-imported-modules '((guix build utils))
  660. #~(begin
  661. (use-modules (guix build utils))
  662. (define %user
  663. (getpw #$(autossh-configuration-user config)))
  664. (let* ((directory #$(autossh-file-name config ""))
  665. (log (string-append directory "/log")))
  666. (mkdir-p directory)
  667. (chown directory (passwd:uid %user) (passwd:gid %user))
  668. (call-with-output-file log (const #t))
  669. (chown log (passwd:uid %user) (passwd:gid %user))))))
  670. (define autossh-service-type
  671. (service-type
  672. (name 'autossh)
  673. (description "Automatically set up ssh connections (and keep them alive).")
  674. (extensions
  675. (list (service-extension shepherd-root-service-type
  676. (compose list autossh-shepherd-service))
  677. (service-extension activation-service-type
  678. autossh-service-activation)))
  679. (default-value (autossh-configuration))))
  680. ;;;
  681. ;;; WebSSH
  682. ;;;
  683. (define-record-type* <webssh-configuration>
  684. webssh-configuration make-webssh-configuration
  685. webssh-configuration?
  686. (package webssh-configuration-package ;file-like
  687. (default webssh))
  688. (user-name webssh-configuration-user-name ;string
  689. (default "webssh"))
  690. (group-name webssh-configuration-group-name ;string
  691. (default "webssh"))
  692. (policy webssh-configuration-policy ;symbol
  693. (default #f))
  694. (known-hosts webssh-configuration-known-hosts ;list of strings
  695. (default #f))
  696. (port webssh-configuration-port ;number
  697. (default #f))
  698. (address webssh-configuration-address ;string
  699. (default #f))
  700. (log-file webssh-configuration-log-file ;string
  701. (default "/var/log/webssh.log"))
  702. (log-level webssh-configuration-log-level ;symbol
  703. (default #f)))
  704. (define %webssh-configuration-nginx
  705. (nginx-server-configuration
  706. (listen '("80"))
  707. (locations
  708. (list (nginx-location-configuration
  709. (uri "/")
  710. (body '("proxy_pass http://127.0.0.1:8888;"
  711. "proxy_http_version 1.1;"
  712. "proxy_read_timeout 300;"
  713. "proxy_set_header Upgrade $http_upgrade;"
  714. "proxy_set_header Connection \"upgrade\";"
  715. "proxy_set_header Host $http_host;"
  716. "proxy_set_header X-Real-IP $remote_addr;"
  717. "proxy_set_header X-Real-PORT $remote_port;")))))))
  718. (define webssh-account
  719. ;; Return the user accounts and user groups for CONFIG.
  720. (match-lambda
  721. (($ <webssh-configuration> _ user-name group-name _ _ _ _ _ _)
  722. (list (user-group
  723. (name group-name))
  724. (user-account
  725. (name user-name)
  726. (group group-name)
  727. (comment "webssh privilege separation user")
  728. (home-directory (string-append "/var/run/" user-name))
  729. (shell #~(string-append #$shadow "/sbin/nologin")))))))
  730. (define webssh-activation
  731. ;; Return the activation GEXP for CONFIG.
  732. (match-lambda
  733. (($ <webssh-configuration> _ user-name group-name policy known-hosts _ _
  734. log-file _)
  735. (with-imported-modules '((guix build utils))
  736. #~(begin
  737. (let* ((home-dir (string-append "/var/run/" #$user-name))
  738. (ssh-dir (string-append home-dir "/.ssh"))
  739. (known-hosts-file (string-append ssh-dir "/known_hosts")))
  740. (call-with-output-file #$log-file (const #t))
  741. (mkdir-p ssh-dir)
  742. (case '#$policy
  743. ((reject)
  744. (if '#$known-hosts
  745. (call-with-output-file known-hosts-file
  746. (lambda (port)
  747. (for-each (lambda (host) (display host port) (newline port))
  748. '#$known-hosts)))
  749. (display-hint (G_ "webssh: reject policy requires `known-hosts'.")))))
  750. (for-each (lambda (file)
  751. (chown file
  752. (passwd:uid (getpw #$user-name))
  753. (group:gid (getpw #$group-name))))
  754. (list #$log-file ssh-dir known-hosts-file))
  755. (chmod ssh-dir #o700)))))))
  756. (define webssh-shepherd-service
  757. (match-lambda
  758. (($ <webssh-configuration> package user-name group-name policy _ port
  759. address log-file log-level)
  760. (list (shepherd-service
  761. (provision '(webssh))
  762. (documentation "Run webssh daemon.")
  763. (start #~(make-forkexec-constructor
  764. `(,(string-append #$webssh "/bin/wssh")
  765. ,(string-append "--log-file-prefix=" #$log-file)
  766. ,@(case '#$log-level
  767. ((debug) '("--logging=debug"))
  768. (else '()))
  769. ,@(case '#$policy
  770. ((reject) '("--policy=reject"))
  771. (else '()))
  772. ,@(if #$port
  773. (list (string-append "--port=" (number->string #$port)))
  774. '())
  775. ,@(if #$address
  776. (list (string-append "--address=" #$address))
  777. '()))
  778. #:user #$user-name
  779. #:group #$group-name))
  780. (stop #~(make-kill-destructor)))))))
  781. (define webssh-service-type
  782. (service-type
  783. (name 'webssh)
  784. (extensions
  785. (list (service-extension shepherd-root-service-type
  786. webssh-shepherd-service)
  787. (service-extension account-service-type
  788. webssh-account)
  789. (service-extension activation-service-type
  790. webssh-activation)))
  791. (default-value (webssh-configuration))
  792. (description
  793. "Run the webssh.")))
  794. ;;; ssh.scm ends here