ssh.scm 35 KB

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