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>
7 ;;; This file is part of GNU Guix.
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.
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.
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/>.
22 (define-module (gnu services ssh)
23 #:use-module (gnu packages ssh)
24 #:use-module (gnu packages admin)
25 #:use-module (gnu services)
26 #:use-module (gnu services shepherd)
27 #:use-module (gnu system pam)
28 #:use-module (gnu system shadow)
29 #:use-module (guix gexp)
30 #:use-module (guix records)
31 #:use-module (guix modules)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-26)
34 #:use-module (ice-9 match)
35 #:export (lsh-configuration
41 openssh-configuration?
44 dropbear-configuration
45 dropbear-configuration?
51 ;;; This module implements secure shell (SSH) services.
55 (define-record-type* <lsh-configuration>
56 lsh-configuration make-lsh-configuration
58 (lsh lsh-configuration-lsh
60 (daemonic? lsh-configuration-daemonic?)
61 (host-key lsh-configuration-host-key)
62 (interfaces lsh-configuration-interfaces)
63 (port-number lsh-configuration-port-number)
64 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
65 (root-login? lsh-configuration-root-login?)
66 (syslog-output? lsh-configuration-syslog-output?)
67 (pid-file? lsh-configuration-pid-file?)
68 (pid-file lsh-configuration-pid-file)
69 (x11-forwarding? lsh-configuration-x11-forwarding?)
70 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
71 (password-authentication? lsh-configuration-password-authentication?)
72 (public-key-authentication? lsh-configuration-public-key-authentication?)
73 (initialize? lsh-configuration-initialize?))
76 "/var/spool/lsh/yarrow-seed-file")
78 (define (lsh-initialization lsh host-key)
79 "Return the gexp to initialize the LSH service for HOST-KEY."
81 (unless (file-exists? #$%yarrow-seed)
82 (system* (string-append #$lsh "/bin/lsh-make-seed")
83 "--sloppy" "-o" #$%yarrow-seed))
85 (unless (file-exists? #$host-key)
86 (mkdir-p (dirname #$host-key))
87 (format #t "creating SSH host key '~a'...~%" #$host-key)
89 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
90 ;; used yet because /bin/sh might be dangling; factorize this somehow.
91 (let* ((in+out (pipe))
92 (keygen (primitive-fork)))
95 (close-port (car in+out))
97 (dup2 (fileno (cdr in+out)) 1)
98 (execl (string-append #$lsh "/bin/lsh-keygen")
99 "lsh-keygen" "--server"))
101 (let ((write-key (primitive-fork)))
104 (close-port (cdr in+out))
106 (dup2 (fileno (car in+out)) 0)
107 (execl (string-append #$lsh "/bin/lsh-writekey")
108 "lsh-writekey" "--server" "-o" #$host-key))
110 (close-port (car in+out))
111 (close-port (cdr in+out))
113 (waitpid write-key))))))))))
115 (define (lsh-activation config)
116 "Return the activation gexp for CONFIG."
118 (use-modules (guix build utils))
119 (mkdir-p "/var/spool/lsh")
120 #$(if (lsh-configuration-initialize? config)
121 (lsh-initialization (lsh-configuration-lsh config)
122 (lsh-configuration-host-key config))
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))
135 (cons (file-append lsh "/sbin/lshd")
137 (let ((syslog (if (lsh-configuration-syslog-output? config)
139 (list "--no-syslog"))))
142 (cons #~(string-append "--pid-file=" #$pid-file)
144 (cons "--no-pid-file" syslog))))
146 (list #~(string-append "--pid-file=" #$pid-file))
148 (cons* #~(string-append "--host-key="
149 #$(lsh-configuration-host-key config))
150 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
151 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
152 "-p" (number->string (lsh-configuration-port-number config))
153 (if (lsh-configuration-password-authentication? config)
154 "--password" "--no-password")
155 (if (lsh-configuration-public-key-authentication? config)
156 "--publickey" "--no-publickey")
157 (if (lsh-configuration-root-login? config)
158 "--root-login" "--no-root-login")
159 (if (lsh-configuration-x11-forwarding? config)
160 "--x11-forward" "--no-x11-forward")
161 (if (lsh-configuration-tcp/ip-forwarding? config)
162 "--tcpip-forward" "--no-tcpip-forward")
163 (if (null? interfaces)
165 (map (cut string-append "--interface=" <>)
169 (if (and daemonic? (lsh-configuration-syslog-output? config))
170 '(networking syslogd)
173 (list (shepherd-service
174 (documentation "GNU lsh SSH server")
175 (provision '(ssh-daemon))
176 (requirement requires)
177 (start #~(make-forkexec-constructor (list #$@lsh-command)))
178 (stop #~(make-kill-destructor)))))
180 (define (lsh-pam-services config)
181 "Return a list of <pam-services> for lshd with CONFIG."
182 (list (unix-pam-service
184 #:allow-empty-passwords?
185 (lsh-configuration-allow-empty-passwords? config))))
187 (define lsh-service-type
188 (service-type (name 'lsh)
190 "Run the GNU@tie{}lsh secure shell (SSH) daemon,
193 (list (service-extension shepherd-root-service-type
194 lsh-shepherd-service)
195 (service-extension pam-root-service-type
197 (service-extension activation-service-type
200 (define* (lsh-service #:key
203 (host-key "/etc/lsh/host-key")
206 (allow-empty-passwords? #f)
210 (pid-file "/var/run/lshd.pid")
212 (tcp/ip-forwarding? #t)
213 (password-authentication? #t)
214 (public-key-authentication? #t)
216 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
217 @var{host-key} must designate a file containing the host key, and readable
220 When @var{daemonic?} is true, @command{lshd} will detach from the
221 controlling terminal and log its output to syslogd, unless one sets
222 @var{syslog-output?} to false. Obviously, it also makes lsh-service
223 depend on existence of syslogd service. When @var{pid-file?} is true,
224 @command{lshd} writes its PID to the file called @var{pid-file}.
226 When @var{initialize?} is true, automatically create the seed and host key
227 upon service activation if they do not exist yet. This may take long and
230 When @var{initialize?} is false, it is up to the user to initialize the
231 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
232 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
233 basics,,, lsh, LSH Manual}).
235 When @var{interfaces} is empty, lshd listens for connections on all the
236 network interfaces; otherwise, @var{interfaces} must be a list of host names
239 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
240 passwords, and @var{root-login?} specifies whether to accept log-ins as
243 The other options should be self-descriptive."
244 (service lsh-service-type
245 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
246 (host-key host-key) (interfaces interfaces)
247 (port-number port-number)
248 (allow-empty-passwords? allow-empty-passwords?)
249 (root-login? root-login?)
250 (syslog-output? syslog-output?)
251 (pid-file? pid-file?) (pid-file pid-file)
252 (x11-forwarding? x11-forwarding?)
253 (tcp/ip-forwarding? tcp/ip-forwarding?)
254 (password-authentication?
255 password-authentication?)
256 (public-key-authentication?
257 public-key-authentication?)
258 (initialize? initialize?))))
265 (define-record-type* <openssh-configuration>
266 openssh-configuration make-openssh-configuration
267 openssh-configuration?
269 (openssh openssh-configuration-openssh
272 (pid-file openssh-configuration-pid-file
273 (default "/var/run/sshd.pid"))
275 (port-number openssh-configuration-port-number
277 ;; Boolean | 'without-password
278 (permit-root-login openssh-configuration-permit-root-login
281 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
284 (password-authentication? openssh-configuration-password-authentication?
287 (public-key-authentication? openssh-configuration-public-key-authentication?
290 (x11-forwarding? openssh-configuration-x11-forwarding?
293 (challenge-response-authentication? openssh-challenge-response-authentication?
296 (use-pam? openssh-configuration-use-pam?
299 (print-last-log? openssh-configuration-print-last-log?
301 ;; list of two-element lists
302 (subsystems openssh-configuration-subsystems
303 (default '(("sftp" "internal-sftp"))))
305 ;; list of user-name/file-like tuples
306 (authorized-keys openssh-authorized-keys
310 ;; XXX: This should really be handled in an orthogonal way, for instance as
311 ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
313 (%auto-start? openssh-auto-start?
316 (define %openssh-accounts
317 (list (user-group (name "sshd") (system? #t))
322 (comment "sshd privilege separation user")
323 (home-directory "/var/run/sshd")
324 (shell #~(string-append #$shadow "/sbin/nologin")))))
326 (define (openssh-activation config)
327 "Return the activation GEXP for CONFIG."
328 (with-imported-modules '((guix build utils))
330 (use-modules (guix build utils))
332 (define (touch file-name)
333 (call-with-output-file file-name (const #t)))
335 ;; Make sure /etc/ssh can be read by the 'sshd' user.
337 (chmod "/etc/ssh" #o755)
338 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
340 ;; 'sshd' complains if the authorized-key directory and its parents
341 ;; are group-writable, which rules out /gnu/store. Thus we copy the
342 ;; authorized-key directory to /etc.
345 (delete-file-recursively "/etc/authorized_keys.d"))
347 (unless (= ENOENT (system-error-errno args))
348 (apply throw args))))
349 (copy-recursively #$(authorized-key-directory
350 (openssh-authorized-keys config))
351 "/etc/ssh/authorized_keys.d")
353 (chmod "/etc/ssh/authorized_keys.d" #o555)
355 (let ((lastlog "/var/log/lastlog"))
356 (when #$(openssh-configuration-print-last-log? config)
357 (unless (file-exists? lastlog)
360 ;; Generate missing host keys.
361 (system* (string-append #$(openssh-configuration-openssh config)
362 "/bin/ssh-keygen") "-A"))))
364 (define (authorized-key-directory keys)
365 "Return a directory containing the authorized keys specified in KEYS, a list
366 of user-name/file-like tuples."
368 (with-imported-modules (source-module-closure '((guix build utils)))
370 (use-modules (ice-9 match) (srfi srfi-26)
374 (for-each (match-lambda
376 (let ((file (string-append #$output "/" user)))
377 (call-with-output-file file
379 (for-each (lambda (key)
380 (call-with-input-file key
381 (cut dump-port <> port)))
385 (computed-file "openssh-authorized-keys" build))
387 (define (openssh-config-file config)
388 "Return the sshd configuration file corresponding to CONFIG."
392 (use-modules (ice-9 match))
393 (call-with-output-file #$output
395 (display "# Generated by 'openssh-service'.\n" port)
396 (format port "Port ~a\n"
398 (openssh-configuration-port-number config)))
399 (format port "PermitRootLogin ~a\n"
400 #$(match (openssh-configuration-permit-root-login config)
403 ('without-password "without-password")))
404 (format port "PermitEmptyPasswords ~a\n"
405 #$(if (openssh-configuration-allow-empty-passwords? config)
407 (format port "PasswordAuthentication ~a\n"
408 #$(if (openssh-configuration-password-authentication? config)
410 (format port "PubkeyAuthentication ~a\n"
411 #$(if (openssh-configuration-public-key-authentication?
414 (format port "X11Forwarding ~a\n"
415 #$(if (openssh-configuration-x11-forwarding? config)
417 (format port "PidFile ~a\n"
418 #$(openssh-configuration-pid-file config))
419 (format port "ChallengeResponseAuthentication ~a\n"
420 #$(if (openssh-challenge-response-authentication? config)
422 (format port "UsePAM ~a\n"
423 #$(if (openssh-configuration-use-pam? config)
425 (format port "PrintLastLog ~a\n"
426 #$(if (openssh-configuration-print-last-log? config)
429 ;; Add '/etc/authorized_keys.d/%u', which we populate.
430 (format port "AuthorizedKeysFile \
431 .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
435 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
436 '#$(openssh-configuration-subsystems config))
439 (define (openssh-shepherd-service config)
440 "Return a <shepherd-service> for openssh with CONFIG."
443 (openssh-configuration-pid-file config))
445 (define openssh-command
446 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
447 "-D" "-f" #$(openssh-config-file config)))
449 (list (shepherd-service
450 (documentation "OpenSSH server.")
451 (requirement '(syslogd))
452 (provision '(ssh-daemon))
453 (start #~(make-forkexec-constructor #$openssh-command
454 #:pid-file #$pid-file))
455 (stop #~(make-kill-destructor))
456 (auto-start? (openssh-auto-start? config)))))
458 (define (openssh-pam-services config)
459 "Return a list of <pam-services> for sshd with CONFIG."
460 (list (unix-pam-service
462 #:allow-empty-passwords?
463 (openssh-configuration-allow-empty-passwords? config))))
465 (define (extend-openssh-authorized-keys config keys)
466 "Extend CONFIG with the extra authorized keys listed in KEYS."
467 (openssh-configuration
470 (append (openssh-authorized-keys config) keys))))
472 (define openssh-service-type
473 (service-type (name 'openssh)
475 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
477 (list (service-extension shepherd-root-service-type
478 openssh-shepherd-service)
479 (service-extension pam-root-service-type
480 openssh-pam-services)
481 (service-extension activation-service-type
483 (service-extension account-service-type
484 (const %openssh-accounts))))
485 (compose concatenate)
486 (extend extend-openssh-authorized-keys)
487 (default-value (openssh-configuration))))
494 (define-record-type* <dropbear-configuration>
495 dropbear-configuration make-dropbear-configuration
496 dropbear-configuration?
497 (dropbear dropbear-configuration-dropbear
499 (port-number dropbear-configuration-port-number
501 (syslog-output? dropbear-configuration-syslog-output?
503 (pid-file dropbear-configuration-pid-file
504 (default "/var/run/dropbear.pid"))
505 (root-login? dropbear-configuration-root-login?
507 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
509 (password-authentication? dropbear-configuration-password-authentication?
512 (define (dropbear-activation config)
513 "Return the activation gexp for CONFIG."
515 (use-modules (guix build utils))
516 (mkdir-p "/etc/dropbear")))
518 (define (dropbear-shepherd-service config)
519 "Return a <shepherd-service> for dropbear with CONFIG."
521 (dropbear-configuration-dropbear config))
524 (dropbear-configuration-pid-file config))
526 (define dropbear-command
527 #~(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
534 "-p" #$(number->string (dropbear-configuration-port-number config))
536 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
537 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
538 #$@(if (dropbear-configuration-password-authentication? config)
541 #$@(if (dropbear-configuration-allow-empty-passwords? config)
546 (if (dropbear-configuration-syslog-output? config)
547 '(networking syslogd) '(networking)))
549 (list (shepherd-service
550 (documentation "Dropbear SSH server.")
551 (requirement requires)
552 (provision '(ssh-daemon))
553 (start #~(make-forkexec-constructor #$dropbear-command
554 #:pid-file #$pid-file))
555 (stop #~(make-kill-destructor)))))
557 (define dropbear-service-type
558 (service-type (name 'dropbear)
560 "Run the Dropbear secure shell (SSH) server.")
562 (list (service-extension shepherd-root-service-type
563 dropbear-shepherd-service)
564 (service-extension activation-service-type
565 dropbear-activation)))))
567 (define* (dropbear-service #:optional (config (dropbear-configuration)))
568 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
569 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
571 (service dropbear-service-type config))
573 ;;; ssh.scm ends here