ssh.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. (extensions
  174. (list (service-extension shepherd-root-service-type
  175. lsh-shepherd-service)
  176. (service-extension pam-root-service-type
  177. lsh-pam-services)
  178. (service-extension activation-service-type
  179. lsh-activation)))))
  180. (define* (lsh-service #:key
  181. (lsh lsh)
  182. (daemonic? #t)
  183. (host-key "/etc/lsh/host-key")
  184. (interfaces '())
  185. (port-number 22)
  186. (allow-empty-passwords? #f)
  187. (root-login? #f)
  188. (syslog-output? #t)
  189. (pid-file? #f)
  190. (pid-file "/var/run/lshd.pid")
  191. (x11-forwarding? #t)
  192. (tcp/ip-forwarding? #t)
  193. (password-authentication? #t)
  194. (public-key-authentication? #t)
  195. (initialize? #t))
  196. "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
  197. @var{host-key} must designate a file containing the host key, and readable
  198. only by root.
  199. When @var{daemonic?} is true, @command{lshd} will detach from the
  200. controlling terminal and log its output to syslogd, unless one sets
  201. @var{syslog-output?} to false. Obviously, it also makes lsh-service
  202. depend on existence of syslogd service. When @var{pid-file?} is true,
  203. @command{lshd} writes its PID to the file called @var{pid-file}.
  204. When @var{initialize?} is true, automatically create the seed and host key
  205. upon service activation if they do not exist yet. This may take long and
  206. require interaction.
  207. When @var{initialize?} is false, it is up to the user to initialize the
  208. randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
  209. a key pair with the private key stored in file @var{host-key} (@pxref{lshd
  210. basics,,, lsh, LSH Manual}).
  211. When @var{interfaces} is empty, lshd listens for connections on all the
  212. network interfaces; otherwise, @var{interfaces} must be a list of host names
  213. or addresses.
  214. @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
  215. passwords, and @var{root-login?} specifies whether to accept log-ins as
  216. root.
  217. The other options should be self-descriptive."
  218. (service lsh-service-type
  219. (lsh-configuration (lsh lsh) (daemonic? daemonic?)
  220. (host-key host-key) (interfaces interfaces)
  221. (port-number port-number)
  222. (allow-empty-passwords? allow-empty-passwords?)
  223. (root-login? root-login?)
  224. (syslog-output? syslog-output?)
  225. (pid-file? pid-file?) (pid-file pid-file)
  226. (x11-forwarding? x11-forwarding?)
  227. (tcp/ip-forwarding? tcp/ip-forwarding?)
  228. (password-authentication?
  229. password-authentication?)
  230. (public-key-authentication?
  231. public-key-authentication?)
  232. (initialize? initialize?))))
  233. ;;;
  234. ;;; OpenSSH.
  235. ;;;
  236. (define-record-type* <openssh-configuration>
  237. openssh-configuration make-openssh-configuration
  238. openssh-configuration?
  239. ;; <package>
  240. (openssh openssh-configuration-openssh
  241. (default openssh))
  242. ;; string
  243. (pid-file openssh-configuration-pid-file
  244. (default "/var/run/sshd.pid"))
  245. ;; integer
  246. (port-number openssh-configuration-port-number
  247. (default 22))
  248. ;; Boolean | 'without-password
  249. (permit-root-login openssh-configuration-permit-root-login
  250. (default #f))
  251. ;; Boolean
  252. (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
  253. (default #f))
  254. ;; Boolean
  255. (password-authentication? openssh-configuration-password-authentication?
  256. (default #t))
  257. ;; Boolean
  258. (public-key-authentication? openssh-configuration-public-key-authentication?
  259. (default #t))
  260. ;; Boolean
  261. (x11-forwarding? openssh-configuration-x11-forwarding?
  262. (default #f))
  263. ;; Boolean
  264. (challenge-response-authentication? openssh-challenge-response-authentication?
  265. (default #f))
  266. ;; Boolean
  267. (use-pam? openssh-configuration-use-pam?
  268. (default #t))
  269. ;; Boolean
  270. (print-last-log? openssh-configuration-print-last-log?
  271. (default #t))
  272. ;; list of two-element lists
  273. (subsystems openssh-configuration-subsystems
  274. (default '(("sftp" "internal-sftp"))))
  275. ;; list of user-name/file-like tuples
  276. (authorized-keys openssh-authorized-keys
  277. (default '())))
  278. (define %openssh-accounts
  279. (list (user-group (name "sshd") (system? #t))
  280. (user-account
  281. (name "sshd")
  282. (group "sshd")
  283. (system? #t)
  284. (comment "sshd privilege separation user")
  285. (home-directory "/var/run/sshd")
  286. (shell #~(string-append #$shadow "/sbin/nologin")))))
  287. (define (openssh-activation config)
  288. "Return the activation GEXP for CONFIG."
  289. (with-imported-modules '((guix build utils))
  290. #~(begin
  291. (use-modules (guix build utils))
  292. (define (touch file-name)
  293. (call-with-output-file file-name (const #t)))
  294. ;; Make sure /etc/ssh can be read by the 'sshd' user.
  295. (mkdir-p "/etc/ssh")
  296. (chmod "/etc/ssh" #o755)
  297. (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
  298. ;; 'sshd' complains if the authorized-key directory and its parents
  299. ;; are group-writable, which rules out /gnu/store. Thus we copy the
  300. ;; authorized-key directory to /etc.
  301. (catch 'system-error
  302. (lambda ()
  303. (delete-file-recursively "/etc/authorized_keys.d"))
  304. (lambda args
  305. (unless (= ENOENT (system-error-errno args))
  306. (apply throw args))))
  307. (copy-recursively #$(authorized-key-directory
  308. (openssh-authorized-keys config))
  309. "/etc/ssh/authorized_keys.d")
  310. (chmod "/etc/ssh/authorized_keys.d" #o555)
  311. (let ((lastlog "/var/log/lastlog"))
  312. (when #$(openssh-configuration-print-last-log? config)
  313. (unless (file-exists? lastlog)
  314. (touch lastlog))))
  315. ;; Generate missing host keys.
  316. (system* (string-append #$(openssh-configuration-openssh config)
  317. "/bin/ssh-keygen") "-A"))))
  318. (define (authorized-key-directory keys)
  319. "Return a directory containing the authorized keys specified in KEYS, a list
  320. of user-name/file-like tuples."
  321. (define build
  322. (with-imported-modules (source-module-closure '((guix build utils)))
  323. #~(begin
  324. (use-modules (ice-9 match) (srfi srfi-26)
  325. (guix build utils))
  326. (mkdir #$output)
  327. (for-each (match-lambda
  328. ((user keys ...)
  329. (let ((file (string-append #$output "/" user)))
  330. (call-with-output-file file
  331. (lambda (port)
  332. (for-each (lambda (key)
  333. (call-with-input-file key
  334. (cut dump-port <> port)))
  335. keys))))))
  336. '#$keys))))
  337. (computed-file "openssh-authorized-keys" build))
  338. (define (openssh-config-file config)
  339. "Return the sshd configuration file corresponding to CONFIG."
  340. (computed-file
  341. "sshd_config"
  342. #~(begin
  343. (use-modules (ice-9 match))
  344. (call-with-output-file #$output
  345. (lambda (port)
  346. (display "# Generated by 'openssh-service'.\n" port)
  347. (format port "Port ~a\n"
  348. #$(number->string
  349. (openssh-configuration-port-number config)))
  350. (format port "PermitRootLogin ~a\n"
  351. #$(match (openssh-configuration-permit-root-login config)
  352. (#t "yes")
  353. (#f "no")
  354. ('without-password "without-password")))
  355. (format port "PermitEmptyPasswords ~a\n"
  356. #$(if (openssh-configuration-allow-empty-passwords? config)
  357. "yes" "no"))
  358. (format port "PasswordAuthentication ~a\n"
  359. #$(if (openssh-configuration-password-authentication? config)
  360. "yes" "no"))
  361. (format port "PubkeyAuthentication ~a\n"
  362. #$(if (openssh-configuration-public-key-authentication?
  363. config)
  364. "yes" "no"))
  365. (format port "X11Forwarding ~a\n"
  366. #$(if (openssh-configuration-x11-forwarding? config)
  367. "yes" "no"))
  368. (format port "PidFile ~a\n"
  369. #$(openssh-configuration-pid-file config))
  370. (format port "ChallengeResponseAuthentication ~a\n"
  371. #$(if (openssh-challenge-response-authentication? config)
  372. "yes" "no"))
  373. (format port "UsePAM ~a\n"
  374. #$(if (openssh-configuration-use-pam? config)
  375. "yes" "no"))
  376. (format port "PrintLastLog ~a\n"
  377. #$(if (openssh-configuration-print-last-log? config)
  378. "yes" "no"))
  379. ;; Add '/etc/authorized_keys.d/%u', which we populate.
  380. (format port "AuthorizedKeysFile \
  381. .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
  382. (for-each
  383. (match-lambda
  384. ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
  385. '#$(openssh-configuration-subsystems config))
  386. #t)))))
  387. (define (openssh-shepherd-service config)
  388. "Return a <shepherd-service> for openssh with CONFIG."
  389. (define pid-file
  390. (openssh-configuration-pid-file config))
  391. (define openssh-command
  392. #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
  393. "-D" "-f" #$(openssh-config-file config)))
  394. (list (shepherd-service
  395. (documentation "OpenSSH server.")
  396. (requirement '(syslogd))
  397. (provision '(ssh-daemon))
  398. (start #~(make-forkexec-constructor #$openssh-command
  399. #:pid-file #$pid-file))
  400. (stop #~(make-kill-destructor)))))
  401. (define (openssh-pam-services config)
  402. "Return a list of <pam-services> for sshd with CONFIG."
  403. (list (unix-pam-service
  404. "sshd"
  405. #:allow-empty-passwords?
  406. (openssh-configuration-allow-empty-passwords? config))))
  407. (define (extend-openssh-authorized-keys config keys)
  408. "Extend CONFIG with the extra authorized keys listed in KEYS."
  409. (openssh-configuration
  410. (inherit config)
  411. (authorized-keys
  412. (append (openssh-authorized-keys config) keys))))
  413. (define openssh-service-type
  414. (service-type (name 'openssh)
  415. (extensions
  416. (list (service-extension shepherd-root-service-type
  417. openssh-shepherd-service)
  418. (service-extension pam-root-service-type
  419. openssh-pam-services)
  420. (service-extension activation-service-type
  421. openssh-activation)
  422. (service-extension account-service-type
  423. (const %openssh-accounts))))
  424. (compose concatenate)
  425. (extend extend-openssh-authorized-keys)
  426. (default-value (openssh-configuration))))
  427. ;;;
  428. ;;; Dropbear.
  429. ;;;
  430. (define-record-type* <dropbear-configuration>
  431. dropbear-configuration make-dropbear-configuration
  432. dropbear-configuration?
  433. (dropbear dropbear-configuration-dropbear
  434. (default dropbear))
  435. (port-number dropbear-configuration-port-number
  436. (default 22))
  437. (syslog-output? dropbear-configuration-syslog-output?
  438. (default #t))
  439. (pid-file dropbear-configuration-pid-file
  440. (default "/var/run/dropbear.pid"))
  441. (root-login? dropbear-configuration-root-login?
  442. (default #f))
  443. (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
  444. (default #f))
  445. (password-authentication? dropbear-configuration-password-authentication?
  446. (default #t)))
  447. (define (dropbear-activation config)
  448. "Return the activation gexp for CONFIG."
  449. #~(begin
  450. (use-modules (guix build utils))
  451. (mkdir-p "/etc/dropbear")))
  452. (define (dropbear-shepherd-service config)
  453. "Return a <shepherd-service> for dropbear with CONFIG."
  454. (define dropbear
  455. (dropbear-configuration-dropbear config))
  456. (define pid-file
  457. (dropbear-configuration-pid-file config))
  458. (define dropbear-command
  459. #~(list (string-append #$dropbear "/sbin/dropbear")
  460. ;; '-R' allows host keys to be automatically generated upon first
  461. ;; connection, at a time when /dev/urandom is more likely securely
  462. ;; seeded.
  463. "-F" "-R"
  464. "-p" #$(number->string (dropbear-configuration-port-number config))
  465. "-P" #$pid-file
  466. #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
  467. #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
  468. #$@(if (dropbear-configuration-password-authentication? config)
  469. '()
  470. '("-s" "-g"))
  471. #$@(if (dropbear-configuration-allow-empty-passwords? config)
  472. '("-B")
  473. '())))
  474. (define requires
  475. (if (dropbear-configuration-syslog-output? config)
  476. '(networking syslogd) '(networking)))
  477. (list (shepherd-service
  478. (documentation "Dropbear SSH server.")
  479. (requirement requires)
  480. (provision '(ssh-daemon))
  481. (start #~(make-forkexec-constructor #$dropbear-command
  482. #:pid-file #$pid-file))
  483. (stop #~(make-kill-destructor)))))
  484. (define dropbear-service-type
  485. (service-type (name 'dropbear)
  486. (extensions
  487. (list (service-extension shepherd-root-service-type
  488. dropbear-shepherd-service)
  489. (service-extension activation-service-type
  490. dropbear-activation)))))
  491. (define* (dropbear-service #:optional (config (dropbear-configuration)))
  492. "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
  493. daemon} with the given @var{config}, a @code{<dropbear-configuration>}
  494. object."
  495. (service dropbear-service-type config))
  496. ;;; ssh.scm ends here