ssh.scm 35 KB

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