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>
8 ;;; This file is part of GNU Guix.
10 ;;; GNU Guix is free software; you can redistribute it and/or modify it
11 ;;; under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or (at
13 ;;; your option) any later version.
15 ;;; GNU Guix is distributed in the hope that it will be useful, but
16 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23 (define-module (gnu services ssh)
24 #:use-module (gnu packages ssh)
25 #:use-module (gnu packages admin)
26 #:use-module (gnu services)
27 #:use-module (gnu services shepherd)
28 #:use-module (gnu system pam)
29 #:use-module (gnu system shadow)
30 #:use-module (guix gexp)
31 #:use-module (guix records)
32 #:use-module (guix modules)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-26)
35 #:use-module (ice-9 match)
36 #:export (lsh-configuration
42 openssh-configuration?
45 dropbear-configuration
46 dropbear-configuration?
52 ;;; This module implements secure shell (SSH) services.
56 (define-record-type* <lsh-configuration>
57 lsh-configuration make-lsh-configuration
59 (lsh lsh-configuration-lsh
61 (daemonic? lsh-configuration-daemonic?)
62 (host-key lsh-configuration-host-key)
63 (interfaces lsh-configuration-interfaces)
64 (port-number lsh-configuration-port-number)
65 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
66 (root-login? lsh-configuration-root-login?)
67 (syslog-output? lsh-configuration-syslog-output?)
68 (pid-file? lsh-configuration-pid-file?)
69 (pid-file lsh-configuration-pid-file)
70 (x11-forwarding? lsh-configuration-x11-forwarding?)
71 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
72 (password-authentication? lsh-configuration-password-authentication?)
73 (public-key-authentication? lsh-configuration-public-key-authentication?)
74 (initialize? lsh-configuration-initialize?))
77 "/var/spool/lsh/yarrow-seed-file")
79 (define (lsh-initialization lsh host-key)
80 "Return the gexp to initialize the LSH service for HOST-KEY."
82 (unless (file-exists? #$%yarrow-seed)
83 (system* (string-append #$lsh "/bin/lsh-make-seed")
84 "--sloppy" "-o" #$%yarrow-seed))
86 (unless (file-exists? #$host-key)
87 (mkdir-p (dirname #$host-key))
88 (format #t "creating SSH host key '~a'...~%" #$host-key)
90 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
91 ;; used yet because /bin/sh might be dangling; factorize this somehow.
92 (let* ((in+out (pipe))
93 (keygen (primitive-fork)))
96 (close-port (car in+out))
98 (dup2 (fileno (cdr in+out)) 1)
99 (execl (string-append #$lsh "/bin/lsh-keygen")
100 "lsh-keygen" "--server"))
102 (let ((write-key (primitive-fork)))
105 (close-port (cdr in+out))
107 (dup2 (fileno (car in+out)) 0)
108 (execl (string-append #$lsh "/bin/lsh-writekey")
109 "lsh-writekey" "--server" "-o" #$host-key))
111 (close-port (car in+out))
112 (close-port (cdr in+out))
114 (waitpid write-key))))))))))
116 (define (lsh-activation config)
117 "Return the activation gexp for CONFIG."
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))
126 (define (lsh-shepherd-service config)
127 "Return a <shepherd-service> for lsh with CONFIG."
128 (define lsh (lsh-configuration-lsh config))
129 (define pid-file (lsh-configuration-pid-file config))
130 (define pid-file? (lsh-configuration-pid-file? config))
131 (define daemonic? (lsh-configuration-daemonic? config))
132 (define interfaces (lsh-configuration-interfaces config))
136 (cons (file-append lsh "/sbin/lshd")
138 (let ((syslog (if (lsh-configuration-syslog-output? config)
140 (list "--no-syslog"))))
143 (cons #~(string-append "--pid-file=" #$pid-file)
145 (cons "--no-pid-file" syslog))))
147 (list #~(string-append "--pid-file=" #$pid-file))
149 (cons* #~(string-append "--host-key="
150 #$(lsh-configuration-host-key config))
151 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
152 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
153 "-p" (number->string (lsh-configuration-port-number config))
154 (if (lsh-configuration-password-authentication? config)
155 "--password" "--no-password")
156 (if (lsh-configuration-public-key-authentication? config)
157 "--publickey" "--no-publickey")
158 (if (lsh-configuration-root-login? config)
159 "--root-login" "--no-root-login")
160 (if (lsh-configuration-x11-forwarding? config)
161 "--x11-forward" "--no-x11-forward")
162 (if (lsh-configuration-tcp/ip-forwarding? config)
163 "--tcpip-forward" "--no-tcpip-forward")
164 (if (null? interfaces)
166 (map (cut string-append "--interface=" <>)
170 (if (and daemonic? (lsh-configuration-syslog-output? config))
171 '(networking syslogd)
174 (list (shepherd-service
175 (documentation "GNU lsh SSH server")
176 (provision '(ssh-daemon))
177 (requirement requires)
178 (start #~(make-forkexec-constructor (list #$@lsh-command)))
179 (stop #~(make-kill-destructor)))))
181 (define (lsh-pam-services config)
182 "Return a list of <pam-services> for lshd with CONFIG."
183 (list (unix-pam-service
186 #:allow-empty-passwords?
187 (lsh-configuration-allow-empty-passwords? config))))
189 (define lsh-service-type
190 (service-type (name 'lsh)
192 "Run the GNU@tie{}lsh secure shell (SSH) daemon,
195 (list (service-extension shepherd-root-service-type
196 lsh-shepherd-service)
197 (service-extension pam-root-service-type
199 (service-extension activation-service-type
202 (define* (lsh-service #:key
205 (host-key "/etc/lsh/host-key")
208 (allow-empty-passwords? #f)
212 (pid-file "/var/run/lshd.pid")
214 (tcp/ip-forwarding? #t)
215 (password-authentication? #t)
216 (public-key-authentication? #t)
218 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
219 @var{host-key} must designate a file containing the host key, and readable
222 When @var{daemonic?} is true, @command{lshd} will detach from the
223 controlling terminal and log its output to syslogd, unless one sets
224 @var{syslog-output?} to false. Obviously, it also makes lsh-service
225 depend on existence of syslogd service. When @var{pid-file?} is true,
226 @command{lshd} writes its PID to the file called @var{pid-file}.
228 When @var{initialize?} is true, automatically create the seed and host key
229 upon service activation if they do not exist yet. This may take long and
232 When @var{initialize?} is false, it is up to the user to initialize the
233 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
234 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
235 basics,,, lsh, LSH Manual}).
237 When @var{interfaces} is empty, lshd listens for connections on all the
238 network interfaces; otherwise, @var{interfaces} must be a list of host names
241 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
242 passwords, and @var{root-login?} specifies whether to accept log-ins as
245 The other options should be self-descriptive."
246 (service lsh-service-type
247 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
248 (host-key host-key) (interfaces interfaces)
249 (port-number port-number)
250 (allow-empty-passwords? allow-empty-passwords?)
251 (root-login? root-login?)
252 (syslog-output? syslog-output?)
253 (pid-file? pid-file?) (pid-file pid-file)
254 (x11-forwarding? x11-forwarding?)
255 (tcp/ip-forwarding? tcp/ip-forwarding?)
256 (password-authentication?
257 password-authentication?)
258 (public-key-authentication?
259 public-key-authentication?)
260 (initialize? initialize?))))
267 (define-record-type* <openssh-configuration>
268 openssh-configuration make-openssh-configuration
269 openssh-configuration?
271 (openssh openssh-configuration-openssh
274 (pid-file openssh-configuration-pid-file
275 (default "/var/run/sshd.pid"))
277 (port-number openssh-configuration-port-number
279 ;; Boolean | 'without-password
280 (permit-root-login openssh-configuration-permit-root-login
283 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
286 (password-authentication? openssh-configuration-password-authentication?
289 (public-key-authentication? openssh-configuration-public-key-authentication?
292 (x11-forwarding? openssh-configuration-x11-forwarding?
296 (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
300 (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
304 (gateway-ports? openssh-configuration-gateway-ports?
308 (challenge-response-authentication? openssh-challenge-response-authentication?
311 (use-pam? openssh-configuration-use-pam?
314 (print-last-log? openssh-configuration-print-last-log?
316 ;; list of two-element lists
317 (subsystems openssh-configuration-subsystems
318 (default '(("sftp" "internal-sftp"))))
321 (accepted-environment openssh-configuration-accepted-environment
325 (log-level openssh-configuration-log-level
329 ;; This is an "escape hatch" to provide configuration that isn't yet
330 ;; supported by this configuration record.
331 (extra-content openssh-configuration-extra-content
334 ;; list of user-name/file-like tuples
335 (authorized-keys openssh-authorized-keys
339 ;; XXX: This should really be handled in an orthogonal way, for instance as
340 ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
342 (%auto-start? openssh-auto-start?
345 (define %openssh-accounts
346 (list (user-group (name "sshd") (system? #t))
351 (comment "sshd privilege separation user")
352 (home-directory "/var/run/sshd")
353 (shell (file-append shadow "/sbin/nologin")))))
355 (define (openssh-activation config)
356 "Return the activation GEXP for CONFIG."
357 (with-imported-modules '((guix build utils))
359 (use-modules (guix build utils))
361 (define (touch file-name)
362 (call-with-output-file file-name (const #t)))
364 ;; Make sure /etc/ssh can be read by the 'sshd' user.
366 (chmod "/etc/ssh" #o755)
367 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
369 ;; 'sshd' complains if the authorized-key directory and its parents
370 ;; are group-writable, which rules out /gnu/store. Thus we copy the
371 ;; authorized-key directory to /etc.
374 (delete-file-recursively "/etc/authorized_keys.d"))
376 (unless (= ENOENT (system-error-errno args))
377 (apply throw args))))
378 (copy-recursively #$(authorized-key-directory
379 (openssh-authorized-keys config))
380 "/etc/ssh/authorized_keys.d")
382 (chmod "/etc/ssh/authorized_keys.d" #o555)
384 (let ((lastlog "/var/log/lastlog"))
385 (when #$(openssh-configuration-print-last-log? config)
386 (unless (file-exists? lastlog)
389 ;; Generate missing host keys.
390 (system* (string-append #$(openssh-configuration-openssh config)
391 "/bin/ssh-keygen") "-A"))))
393 (define (authorized-key-directory keys)
394 "Return a directory containing the authorized keys specified in KEYS, a list
395 of user-name/file-like tuples."
397 (with-imported-modules (source-module-closure '((guix build utils)))
399 (use-modules (ice-9 match) (srfi srfi-26)
403 (for-each (match-lambda
405 (let ((file (string-append #$output "/" user)))
406 (call-with-output-file file
408 (for-each (lambda (key)
409 (call-with-input-file key
410 (cut dump-port <> port)))
414 (computed-file "openssh-authorized-keys" build))
416 (define (openssh-config-file config)
417 "Return the sshd configuration file corresponding to CONFIG."
421 (use-modules (ice-9 match))
422 (call-with-output-file #$output
424 (display "# Generated by 'openssh-service'.\n" port)
425 (format port "Port ~a\n"
427 (openssh-configuration-port-number config)))
428 (format port "PermitRootLogin ~a\n"
429 #$(match (openssh-configuration-permit-root-login config)
432 ('without-password "without-password")))
433 (format port "PermitEmptyPasswords ~a\n"
434 #$(if (openssh-configuration-allow-empty-passwords? config)
436 (format port "PasswordAuthentication ~a\n"
437 #$(if (openssh-configuration-password-authentication? config)
439 (format port "PubkeyAuthentication ~a\n"
440 #$(if (openssh-configuration-public-key-authentication?
443 (format port "X11Forwarding ~a\n"
444 #$(if (openssh-configuration-x11-forwarding? config)
446 (format port "AllowAgentForwarding ~a\n"
447 #$(if (openssh-configuration-allow-agent-forwarding? config)
449 (format port "AllowTcpForwarding ~a\n"
450 #$(if (openssh-configuration-allow-tcp-forwarding? config)
452 (format port "GatewayPorts ~a\n"
453 #$(if (openssh-configuration-gateway-ports? config)
455 (format port "PidFile ~a\n"
456 #$(openssh-configuration-pid-file config))
457 (format port "ChallengeResponseAuthentication ~a\n"
458 #$(if (openssh-challenge-response-authentication? config)
460 (format port "UsePAM ~a\n"
461 #$(if (openssh-configuration-use-pam? config)
463 (format port "PrintLastLog ~a\n"
464 #$(if (openssh-configuration-print-last-log? config)
466 (format port "LogLevel ~a\n"
469 (openssh-configuration-log-level config))))
471 ;; Add '/etc/authorized_keys.d/%u', which we populate.
472 (format port "AuthorizedKeysFile \
473 .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
475 (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
476 '#$(openssh-configuration-accepted-environment config))
480 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
481 '#$(openssh-configuration-subsystems config))
484 #$(openssh-configuration-extra-content config))
487 (define (openssh-shepherd-service config)
488 "Return a <shepherd-service> for openssh with CONFIG."
491 (openssh-configuration-pid-file config))
493 (define openssh-command
494 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
495 "-D" "-f" #$(openssh-config-file config)))
497 (list (shepherd-service
498 (documentation "OpenSSH server.")
499 (requirement '(syslogd loopback))
500 (provision '(ssh-daemon))
501 (start #~(make-forkexec-constructor #$openssh-command
502 #:pid-file #$pid-file))
503 (stop #~(make-kill-destructor))
504 (auto-start? (openssh-auto-start? config)))))
506 (define (openssh-pam-services config)
507 "Return a list of <pam-services> for sshd with CONFIG."
508 (list (unix-pam-service
511 #:allow-empty-passwords?
512 (openssh-configuration-allow-empty-passwords? config))))
514 (define (extend-openssh-authorized-keys config keys)
515 "Extend CONFIG with the extra authorized keys listed in KEYS."
516 (openssh-configuration
519 (append (openssh-authorized-keys config) keys))))
521 (define openssh-service-type
522 (service-type (name 'openssh)
524 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
526 (list (service-extension shepherd-root-service-type
527 openssh-shepherd-service)
528 (service-extension pam-root-service-type
529 openssh-pam-services)
530 (service-extension activation-service-type
532 (service-extension account-service-type
533 (const %openssh-accounts))
535 ;; Install OpenSSH in the system profile. That way,
536 ;; 'scp' is found when someone tries to copy to or from
538 (service-extension profile-service-type
540 (list (openssh-configuration-openssh
542 (compose concatenate)
543 (extend extend-openssh-authorized-keys)
544 (default-value (openssh-configuration))))
551 (define-record-type* <dropbear-configuration>
552 dropbear-configuration make-dropbear-configuration
553 dropbear-configuration?
554 (dropbear dropbear-configuration-dropbear
556 (port-number dropbear-configuration-port-number
558 (syslog-output? dropbear-configuration-syslog-output?
560 (pid-file dropbear-configuration-pid-file
561 (default "/var/run/dropbear.pid"))
562 (root-login? dropbear-configuration-root-login?
564 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
566 (password-authentication? dropbear-configuration-password-authentication?
569 (define (dropbear-activation config)
570 "Return the activation gexp for CONFIG."
572 (use-modules (guix build utils))
573 (mkdir-p "/etc/dropbear")))
575 (define (dropbear-shepherd-service config)
576 "Return a <shepherd-service> for dropbear with CONFIG."
578 (dropbear-configuration-dropbear config))
581 (dropbear-configuration-pid-file config))
583 (define dropbear-command
584 #~(list (string-append #$dropbear "/sbin/dropbear")
586 ;; '-R' allows host keys to be automatically generated upon first
587 ;; connection, at a time when /dev/urandom is more likely securely
591 "-p" #$(number->string (dropbear-configuration-port-number config))
593 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
594 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
595 #$@(if (dropbear-configuration-password-authentication? config)
598 #$@(if (dropbear-configuration-allow-empty-passwords? config)
603 (if (dropbear-configuration-syslog-output? config)
604 '(networking syslogd) '(networking)))
606 (list (shepherd-service
607 (documentation "Dropbear SSH server.")
608 (requirement requires)
609 (provision '(ssh-daemon))
610 (start #~(make-forkexec-constructor #$dropbear-command
611 #:pid-file #$pid-file))
612 (stop #~(make-kill-destructor)))))
614 (define dropbear-service-type
615 (service-type (name 'dropbear)
617 "Run the Dropbear secure shell (SSH) server.")
619 (list (service-extension shepherd-root-service-type
620 dropbear-shepherd-service)
621 (service-extension activation-service-type
622 dropbear-activation)))
623 (default-value (dropbear-configuration))))
625 (define* (dropbear-service #:optional (config (dropbear-configuration)))
626 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
627 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
629 (service dropbear-service-type config))
631 ;;; ssh.scm ends here