ssh.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017 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. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu services ssh)
  22. #:use-module (gnu packages ssh)
  23. #:use-module (gnu packages admin)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services shepherd)
  26. #:use-module (gnu system pam)
  27. #:use-module (gnu system shadow)
  28. #:use-module (guix gexp)
  29. #:use-module (guix records)
  30. #:use-module (guix modules)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-26)
  33. #:use-module (ice-9 match)
  34. #:export (lsh-configuration
  35. lsh-configuration?
  36. lsh-service
  37. lsh-service-type
  38. openssh-configuration
  39. openssh-configuration?
  40. openssh-service-type
  41. dropbear-configuration
  42. dropbear-configuration?
  43. dropbear-service-type
  44. dropbear-service))
  45. ;;; Commentary:
  46. ;;;
  47. ;;; This module implements secure shell (SSH) services.
  48. ;;;
  49. ;;; Code:
  50. (define-record-type* <lsh-configuration>
  51. lsh-configuration make-lsh-configuration
  52. lsh-configuration?
  53. (lsh lsh-configuration-lsh
  54. (default lsh))
  55. (daemonic? lsh-configuration-daemonic?)
  56. (host-key lsh-configuration-host-key)
  57. (interfaces lsh-configuration-interfaces)
  58. (port-number lsh-configuration-port-number)
  59. (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
  60. (root-login? lsh-configuration-root-login?)
  61. (syslog-output? lsh-configuration-syslog-output?)
  62. (pid-file? lsh-configuration-pid-file?)
  63. (pid-file lsh-configuration-pid-file)
  64. (x11-forwarding? lsh-configuration-x11-forwarding?)
  65. (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
  66. (password-authentication? lsh-configuration-password-authentication?)
  67. (public-key-authentication? lsh-configuration-public-key-authentication?)
  68. (initialize? lsh-configuration-initialize?))
  69. (define %yarrow-seed
  70. "/var/spool/lsh/yarrow-seed-file")
  71. (define (lsh-initialization lsh host-key)
  72. "Return the gexp to initialize the LSH service for HOST-KEY."
  73. #~(begin
  74. (unless (file-exists? #$%yarrow-seed)
  75. (system* (string-append #$lsh "/bin/lsh-make-seed")
  76. "--sloppy" "-o" #$%yarrow-seed))
  77. (unless (file-exists? #$host-key)
  78. (mkdir-p (dirname #$host-key))
  79. (format #t "creating SSH host key '~a'...~%" #$host-key)
  80. ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
  81. ;; used yet because /bin/sh might be dangling; factorize this somehow.
  82. (let* ((in+out (pipe))
  83. (keygen (primitive-fork)))
  84. (case keygen
  85. ((0)
  86. (close-port (car in+out))
  87. (close-fdes 1)
  88. (dup2 (fileno (cdr in+out)) 1)
  89. (execl (string-append #$lsh "/bin/lsh-keygen")
  90. "lsh-keygen" "--server"))
  91. (else
  92. (let ((write-key (primitive-fork)))
  93. (case write-key
  94. ((0)
  95. (close-port (cdr in+out))
  96. (close-fdes 0)
  97. (dup2 (fileno (car in+out)) 0)
  98. (execl (string-append #$lsh "/bin/lsh-writekey")
  99. "lsh-writekey" "--server" "-o" #$host-key))
  100. (else
  101. (close-port (car in+out))
  102. (close-port (cdr in+out))
  103. (waitpid keygen)
  104. (waitpid write-key))))))))))
  105. (define (lsh-activation config)
  106. "Return the activation gexp for CONFIG."
  107. #~(begin
  108. (use-modules (guix build utils))
  109. (mkdir-p "/var/spool/lsh")
  110. #$(if (lsh-configuration-initialize? config)
  111. (lsh-initialization (lsh-configuration-lsh config)
  112. (lsh-configuration-host-key config))
  113. #t)))
  114. (define (lsh-shepherd-service config)
  115. "Return a <shepherd-service> for lsh with CONFIG."
  116. (define lsh (lsh-configuration-lsh config))
  117. (define pid-file (lsh-configuration-pid-file config))
  118. (define pid-file? (lsh-configuration-pid-file? config))
  119. (define daemonic? (lsh-configuration-daemonic? config))
  120. (define interfaces (lsh-configuration-interfaces config))
  121. (define lsh-command
  122. (append
  123. (cons (file-append lsh "/sbin/lshd")
  124. (if daemonic?
  125. (let ((syslog (if (lsh-configuration-syslog-output? config)
  126. '()
  127. (list "--no-syslog"))))
  128. (cons "--daemonic"
  129. (if pid-file?
  130. (cons #~(string-append "--pid-file=" #$pid-file)
  131. syslog)
  132. (cons "--no-pid-file" syslog))))
  133. (if pid-file?
  134. (list #~(string-append "--pid-file=" #$pid-file))
  135. '())))
  136. (cons* #~(string-append "--host-key="
  137. #$(lsh-configuration-host-key config))
  138. #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
  139. #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
  140. "-p" (number->string (lsh-configuration-port-number config))
  141. (if (lsh-configuration-password-authentication? config)
  142. "--password" "--no-password")
  143. (if (lsh-configuration-public-key-authentication? config)
  144. "--publickey" "--no-publickey")
  145. (if (lsh-configuration-root-login? config)
  146. "--root-login" "--no-root-login")
  147. (if (lsh-configuration-x11-forwarding? config)
  148. "--x11-forward" "--no-x11-forward")
  149. (if (lsh-configuration-tcp/ip-forwarding? config)
  150. "--tcpip-forward" "--no-tcpip-forward")
  151. (if (null? interfaces)
  152. '()
  153. (map (cut string-append "--interface=" <>)
  154. interfaces)))))
  155. (define requires
  156. (if (and daemonic? (lsh-configuration-syslog-output? config))
  157. '(networking syslogd)
  158. '(networking)))
  159. (list (shepherd-service
  160. (documentation "GNU lsh SSH server")
  161. (provision '(ssh-daemon))
  162. (requirement requires)
  163. (start #~(make-forkexec-constructor (list #$@lsh-command)))
  164. (stop #~(make-kill-destructor)))))
  165. (define (lsh-pam-services config)
  166. "Return a list of <pam-services> for lshd with CONFIG."
  167. (list (unix-pam-service
  168. "lshd"
  169. #:allow-empty-passwords?
  170. (lsh-configuration-allow-empty-passwords? config))))
  171. (define lsh-service-type
  172. (service-type (name 'lsh)
  173. (description
  174. "Run the GNU@tie{}lsh secure shell (SSH) daemon,
  175. @command{lshd}.")
  176. (extensions
  177. (list (service-extension shepherd-root-service-type
  178. lsh-shepherd-service)
  179. (service-extension pam-root-service-type
  180. lsh-pam-services)
  181. (service-extension activation-service-type
  182. lsh-activation)))))
  183. (define* (lsh-service #:key
  184. (lsh lsh)
  185. (daemonic? #t)
  186. (host-key "/etc/lsh/host-key")
  187. (interfaces '())
  188. (port-number 22)
  189. (allow-empty-passwords? #f)
  190. (root-login? #f)
  191. (syslog-output? #t)
  192. (pid-file? #f)
  193. (pid-file "/var/run/lshd.pid")
  194. (x11-forwarding? #t)
  195. (tcp/ip-forwarding? #t)
  196. (password-authentication? #t)
  197. (public-key-authentication? #t)
  198. (initialize? #t))
  199. "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
  200. @var{host-key} must designate a file containing the host key, and readable
  201. only by root.
  202. When @var{daemonic?} is true, @command{lshd} will detach from the
  203. controlling terminal and log its output to syslogd, unless one sets
  204. @var{syslog-output?} to false. Obviously, it also makes lsh-service
  205. depend on existence of syslogd service. When @var{pid-file?} is true,
  206. @command{lshd} writes its PID to the file called @var{pid-file}.
  207. When @var{initialize?} is true, automatically create the seed and host key
  208. upon service activation if they do not exist yet. This may take long and
  209. require interaction.
  210. When @var{initialize?} is false, it is up to the user to initialize the
  211. randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
  212. a key pair with the private key stored in file @var{host-key} (@pxref{lshd
  213. basics,,, lsh, LSH Manual}).
  214. When @var{interfaces} is empty, lshd listens for connections on all the
  215. network interfaces; otherwise, @var{interfaces} must be a list of host names
  216. or addresses.
  217. @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
  218. passwords, and @var{root-login?} specifies whether to accept log-ins as
  219. root.
  220. The other options should be self-descriptive."
  221. (service lsh-service-type
  222. (lsh-configuration (lsh lsh) (daemonic? daemonic?)
  223. (host-key host-key) (interfaces interfaces)
  224. (port-number port-number)
  225. (allow-empty-passwords? allow-empty-passwords?)
  226. (root-login? root-login?)
  227. (syslog-output? syslog-output?)
  228. (pid-file? pid-file?) (pid-file pid-file)
  229. (x11-forwarding? x11-forwarding?)
  230. (tcp/ip-forwarding? tcp/ip-forwarding?)
  231. (password-authentication?
  232. password-authentication?)
  233. (public-key-authentication?
  234. public-key-authentication?)
  235. (initialize? initialize?))))
  236. ;;;
  237. ;;; OpenSSH.
  238. ;;;
  239. (define-record-type* <openssh-configuration>
  240. openssh-configuration make-openssh-configuration
  241. openssh-configuration?
  242. ;; <package>
  243. (openssh openssh-configuration-openssh
  244. (default openssh))
  245. ;; string
  246. (pid-file openssh-configuration-pid-file
  247. (default "/var/run/sshd.pid"))
  248. ;; integer
  249. (port-number openssh-configuration-port-number
  250. (default 22))
  251. ;; Boolean | 'without-password
  252. (permit-root-login openssh-configuration-permit-root-login
  253. (default #f))
  254. ;; Boolean
  255. (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
  256. (default #f))
  257. ;; Boolean
  258. (password-authentication? openssh-configuration-password-authentication?
  259. (default #t))
  260. ;; Boolean
  261. (public-key-authentication? openssh-configuration-public-key-authentication?
  262. (default #t))
  263. ;; Boolean
  264. (x11-forwarding? openssh-configuration-x11-forwarding?
  265. (default #f))
  266. ;; Boolean
  267. (challenge-response-authentication? openssh-challenge-response-authentication?
  268. (default #f))
  269. ;; Boolean
  270. (use-pam? openssh-configuration-use-pam?
  271. (default #t))
  272. ;; Boolean
  273. (print-last-log? openssh-configuration-print-last-log?
  274. (default #t))
  275. ;; list of two-element lists
  276. (subsystems openssh-configuration-subsystems
  277. (default '(("sftp" "internal-sftp"))))
  278. ;; list of user-name/file-like tuples
  279. (authorized-keys openssh-authorized-keys
  280. (default '()))
  281. ;; Boolean
  282. ;; XXX: This should really be handled in an orthogonal way, for instance as
  283. ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
  284. ;; for now.
  285. (%auto-start? openssh-auto-start?
  286. (default #t)))
  287. (define %openssh-accounts
  288. (list (user-group (name "sshd") (system? #t))
  289. (user-account
  290. (name "sshd")
  291. (group "sshd")
  292. (system? #t)
  293. (comment "sshd privilege separation user")
  294. (home-directory "/var/run/sshd")
  295. (shell #~(string-append #$shadow "/sbin/nologin")))))
  296. (define (openssh-activation config)
  297. "Return the activation GEXP for CONFIG."
  298. (with-imported-modules '((guix build utils))
  299. #~(begin
  300. (use-modules (guix build utils))
  301. (define (touch file-name)
  302. (call-with-output-file file-name (const #t)))
  303. ;; Make sure /etc/ssh can be read by the 'sshd' user.
  304. (mkdir-p "/etc/ssh")
  305. (chmod "/etc/ssh" #o755)
  306. (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
  307. ;; 'sshd' complains if the authorized-key directory and its parents
  308. ;; are group-writable, which rules out /gnu/store. Thus we copy the
  309. ;; authorized-key directory to /etc.
  310. (catch 'system-error
  311. (lambda ()
  312. (delete-file-recursively "/etc/authorized_keys.d"))
  313. (lambda args
  314. (unless (= ENOENT (system-error-errno args))
  315. (apply throw args))))
  316. (copy-recursively #$(authorized-key-directory
  317. (openssh-authorized-keys config))
  318. "/etc/ssh/authorized_keys.d")
  319. (chmod "/etc/ssh/authorized_keys.d" #o555)
  320. (let ((lastlog "/var/log/lastlog"))
  321. (when #$(openssh-configuration-print-last-log? config)
  322. (unless (file-exists? lastlog)
  323. (touch lastlog))))
  324. ;; Generate missing host keys.
  325. (system* (string-append #$(openssh-configuration-openssh config)
  326. "/bin/ssh-keygen") "-A"))))
  327. (define (authorized-key-directory keys)
  328. "Return a directory containing the authorized keys specified in KEYS, a list
  329. of user-name/file-like tuples."
  330. (define build
  331. (with-imported-modules (source-module-closure '((guix build utils)))
  332. #~(begin
  333. (use-modules (ice-9 match) (srfi srfi-26)
  334. (guix build utils))
  335. (mkdir #$output)
  336. (for-each (match-lambda
  337. ((user keys ...)
  338. (let ((file (string-append #$output "/" user)))
  339. (call-with-output-file file
  340. (lambda (port)
  341. (for-each (lambda (key)
  342. (call-with-input-file key
  343. (cut dump-port <> port)))
  344. keys))))))
  345. '#$keys))))
  346. (computed-file "openssh-authorized-keys" build))
  347. (define (openssh-config-file config)
  348. "Return the sshd configuration file corresponding to CONFIG."
  349. (computed-file
  350. "sshd_config"
  351. #~(begin
  352. (use-modules (ice-9 match))
  353. (call-with-output-file #$output
  354. (lambda (port)
  355. (display "# Generated by 'openssh-service'.\n" port)
  356. (format port "Port ~a\n"
  357. #$(number->string
  358. (openssh-configuration-port-number config)))
  359. (format port "PermitRootLogin ~a\n"
  360. #$(match (openssh-configuration-permit-root-login config)
  361. (#t "yes")
  362. (#f "no")
  363. ('without-password "without-password")))
  364. (format port "PermitEmptyPasswords ~a\n"
  365. #$(if (openssh-configuration-allow-empty-passwords? config)
  366. "yes" "no"))
  367. (format port "PasswordAuthentication ~a\n"
  368. #$(if (openssh-configuration-password-authentication? config)
  369. "yes" "no"))
  370. (format port "PubkeyAuthentication ~a\n"
  371. #$(if (openssh-configuration-public-key-authentication?
  372. config)
  373. "yes" "no"))
  374. (format port "X11Forwarding ~a\n"
  375. #$(if (openssh-configuration-x11-forwarding? config)
  376. "yes" "no"))
  377. (format port "PidFile ~a\n"
  378. #$(openssh-configuration-pid-file config))
  379. (format port "ChallengeResponseAuthentication ~a\n"
  380. #$(if (openssh-challenge-response-authentication? config)
  381. "yes" "no"))
  382. (format port "UsePAM ~a\n"
  383. #$(if (openssh-configuration-use-pam? config)
  384. "yes" "no"))
  385. (format port "PrintLastLog ~a\n"
  386. #$(if (openssh-configuration-print-last-log? config)
  387. "yes" "no"))
  388. ;; Add '/etc/authorized_keys.d/%u', which we populate.
  389. (format port "AuthorizedKeysFile \
  390. .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
  391. (for-each
  392. (match-lambda
  393. ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
  394. '#$(openssh-configuration-subsystems config))
  395. #t)))))
  396. (define (openssh-shepherd-service config)
  397. "Return a <shepherd-service> for openssh with CONFIG."
  398. (define pid-file
  399. (openssh-configuration-pid-file config))
  400. (define openssh-command
  401. #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
  402. "-D" "-f" #$(openssh-config-file config)))
  403. (list (shepherd-service
  404. (documentation "OpenSSH server.")
  405. (requirement '(syslogd))
  406. (provision '(ssh-daemon))
  407. (start #~(make-forkexec-constructor #$openssh-command
  408. #:pid-file #$pid-file))
  409. (stop #~(make-kill-destructor))
  410. (auto-start? (openssh-auto-start? config)))))
  411. (define (openssh-pam-services config)
  412. "Return a list of <pam-services> for sshd with CONFIG."
  413. (list (unix-pam-service
  414. "sshd"
  415. #:allow-empty-passwords?
  416. (openssh-configuration-allow-empty-passwords? config))))
  417. (define (extend-openssh-authorized-keys config keys)
  418. "Extend CONFIG with the extra authorized keys listed in KEYS."
  419. (openssh-configuration
  420. (inherit config)
  421. (authorized-keys
  422. (append (openssh-authorized-keys config) keys))))
  423. (define openssh-service-type
  424. (service-type (name 'openssh)
  425. (description
  426. "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
  427. (extensions
  428. (list (service-extension shepherd-root-service-type
  429. openssh-shepherd-service)
  430. (service-extension pam-root-service-type
  431. openssh-pam-services)
  432. (service-extension activation-service-type
  433. openssh-activation)
  434. (service-extension account-service-type
  435. (const %openssh-accounts))))
  436. (compose concatenate)
  437. (extend extend-openssh-authorized-keys)
  438. (default-value (openssh-configuration))))
  439. ;;;
  440. ;;; Dropbear.
  441. ;;;
  442. (define-record-type* <dropbear-configuration>
  443. dropbear-configuration make-dropbear-configuration
  444. dropbear-configuration?
  445. (dropbear dropbear-configuration-dropbear
  446. (default dropbear))
  447. (port-number dropbear-configuration-port-number
  448. (default 22))
  449. (syslog-output? dropbear-configuration-syslog-output?
  450. (default #t))
  451. (pid-file dropbear-configuration-pid-file
  452. (default "/var/run/dropbear.pid"))
  453. (root-login? dropbear-configuration-root-login?
  454. (default #f))
  455. (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
  456. (default #f))
  457. (password-authentication? dropbear-configuration-password-authentication?
  458. (default #t)))
  459. (define (dropbear-activation config)
  460. "Return the activation gexp for CONFIG."
  461. #~(begin
  462. (use-modules (guix build utils))
  463. (mkdir-p "/etc/dropbear")))
  464. (define (dropbear-shepherd-service config)
  465. "Return a <shepherd-service> for dropbear with CONFIG."
  466. (define dropbear
  467. (dropbear-configuration-dropbear config))
  468. (define pid-file
  469. (dropbear-configuration-pid-file config))
  470. (define dropbear-command
  471. #~(list (string-append #$dropbear "/sbin/dropbear")
  472. ;; '-R' allows host keys to be automatically generated upon first
  473. ;; connection, at a time when /dev/urandom is more likely securely
  474. ;; seeded.
  475. "-F" "-R"
  476. "-p" #$(number->string (dropbear-configuration-port-number config))
  477. "-P" #$pid-file
  478. #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
  479. #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
  480. #$@(if (dropbear-configuration-password-authentication? config)
  481. '()
  482. '("-s" "-g"))
  483. #$@(if (dropbear-configuration-allow-empty-passwords? config)
  484. '("-B")
  485. '())))
  486. (define requires
  487. (if (dropbear-configuration-syslog-output? config)
  488. '(networking syslogd) '(networking)))
  489. (list (shepherd-service
  490. (documentation "Dropbear SSH server.")
  491. (requirement requires)
  492. (provision '(ssh-daemon))
  493. (start #~(make-forkexec-constructor #$dropbear-command
  494. #:pid-file #$pid-file))
  495. (stop #~(make-kill-destructor)))))
  496. (define dropbear-service-type
  497. (service-type (name 'dropbear)
  498. (description
  499. "Run the Dropbear secure shell (SSH) server.")
  500. (extensions
  501. (list (service-extension shepherd-root-service-type
  502. dropbear-shepherd-service)
  503. (service-extension activation-service-type
  504. dropbear-activation)))))
  505. (define* (dropbear-service #:optional (config (dropbear-configuration)))
  506. "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
  507. daemon} with the given @var{config}, a @code{<dropbear-configuration>}
  508. object."
  509. (service dropbear-service-type config))
  510. ;;; ssh.scm ends here