ssh.scm 35 KB

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