services: SSH services: Provide 'ssh' and 'sshd' Shepherd services.
[jackhill/guix/guix.git] / gnu / services / ssh.scm
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 ;;;
8 ;;; This file is part of GNU Guix.
9 ;;;
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.
14 ;;;
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.
19 ;;;
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/>.
22
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
37 lsh-configuration?
38 lsh-service
39 lsh-service-type
40
41 openssh-configuration
42 openssh-configuration?
43 openssh-service-type
44
45 dropbear-configuration
46 dropbear-configuration?
47 dropbear-service-type
48 dropbear-service))
49
50 ;;; Commentary:
51 ;;;
52 ;;; This module implements secure shell (SSH) services.
53 ;;;
54 ;;; Code:
55
56 (define-record-type* <lsh-configuration>
57 lsh-configuration make-lsh-configuration
58 lsh-configuration?
59 (lsh lsh-configuration-lsh
60 (default 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?))
75
76 (define %yarrow-seed
77 "/var/spool/lsh/yarrow-seed-file")
78
79 (define (lsh-initialization lsh host-key)
80 "Return the gexp to initialize the LSH service for HOST-KEY."
81 #~(begin
82 (unless (file-exists? #$%yarrow-seed)
83 (system* (string-append #$lsh "/bin/lsh-make-seed")
84 "--sloppy" "-o" #$%yarrow-seed))
85
86 (unless (file-exists? #$host-key)
87 (mkdir-p (dirname #$host-key))
88 (format #t "creating SSH host key '~a'...~%" #$host-key)
89
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)))
94 (case keygen
95 ((0)
96 (close-port (car in+out))
97 (close-fdes 1)
98 (dup2 (fileno (cdr in+out)) 1)
99 (execl (string-append #$lsh "/bin/lsh-keygen")
100 "lsh-keygen" "--server"))
101 (else
102 (let ((write-key (primitive-fork)))
103 (case write-key
104 ((0)
105 (close-port (cdr in+out))
106 (close-fdes 0)
107 (dup2 (fileno (car in+out)) 0)
108 (execl (string-append #$lsh "/bin/lsh-writekey")
109 "lsh-writekey" "--server" "-o" #$host-key))
110 (else
111 (close-port (car in+out))
112 (close-port (cdr in+out))
113 (waitpid keygen)
114 (waitpid write-key))))))))))
115
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
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))
133
134 (define lsh-command
135 (append
136 (cons (file-append lsh "/sbin/lshd")
137 (if daemonic?
138 (let ((syslog (if (lsh-configuration-syslog-output? config)
139 '()
140 (list "--no-syslog"))))
141 (cons "--daemonic"
142 (if pid-file?
143 (cons #~(string-append "--pid-file=" #$pid-file)
144 syslog)
145 (cons "--no-pid-file" syslog))))
146 (if pid-file?
147 (list #~(string-append "--pid-file=" #$pid-file))
148 '())))
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)
165 '()
166 (map (cut string-append "--interface=" <>)
167 interfaces)))))
168
169 (define requires
170 (if (and daemonic? (lsh-configuration-syslog-output? config))
171 '(networking syslogd)
172 '(networking)))
173
174 (list (shepherd-service
175 (documentation "GNU lsh SSH server")
176 (provision '(ssh-daemon ssh sshd))
177 (requirement requires)
178 (start #~(make-forkexec-constructor (list #$@lsh-command)))
179 (stop #~(make-kill-destructor)))))
180
181 (define (lsh-pam-services config)
182 "Return a list of <pam-services> for lshd with CONFIG."
183 (list (unix-pam-service
184 "lshd"
185 #:login-uid? #t
186 #:allow-empty-passwords?
187 (lsh-configuration-allow-empty-passwords? config))))
188
189 (define lsh-service-type
190 (service-type (name 'lsh)
191 (description
192 "Run the GNU@tie{}lsh secure shell (SSH) daemon,
193 @command{lshd}.")
194 (extensions
195 (list (service-extension shepherd-root-service-type
196 lsh-shepherd-service)
197 (service-extension pam-root-service-type
198 lsh-pam-services)
199 (service-extension activation-service-type
200 lsh-activation)))))
201
202 (define* (lsh-service #:key
203 (lsh lsh)
204 (daemonic? #t)
205 (host-key "/etc/lsh/host-key")
206 (interfaces '())
207 (port-number 22)
208 (allow-empty-passwords? #f)
209 (root-login? #f)
210 (syslog-output? #t)
211 (pid-file? #f)
212 (pid-file "/var/run/lshd.pid")
213 (x11-forwarding? #t)
214 (tcp/ip-forwarding? #t)
215 (password-authentication? #t)
216 (public-key-authentication? #t)
217 (initialize? #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
220 only by root.
221
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}.
227
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
230 require interaction.
231
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}).
236
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
239 or addresses.
240
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
243 root.
244
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?))))
261
262 \f
263 ;;;
264 ;;; OpenSSH.
265 ;;;
266
267 (define-record-type* <openssh-configuration>
268 openssh-configuration make-openssh-configuration
269 openssh-configuration?
270 ;; <package>
271 (openssh openssh-configuration-openssh
272 (default openssh))
273 ;; string
274 (pid-file openssh-configuration-pid-file
275 (default "/var/run/sshd.pid"))
276 ;; integer
277 (port-number openssh-configuration-port-number
278 (default 22))
279 ;; Boolean | 'without-password
280 (permit-root-login openssh-configuration-permit-root-login
281 (default #f))
282 ;; Boolean
283 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
284 (default #f))
285 ;; Boolean
286 (password-authentication? openssh-configuration-password-authentication?
287 (default #t))
288 ;; Boolean
289 (public-key-authentication? openssh-configuration-public-key-authentication?
290 (default #t))
291 ;; Boolean
292 (x11-forwarding? openssh-configuration-x11-forwarding?
293 (default #f))
294
295 ;; Boolean
296 (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
297 (default #t))
298
299 ;; Boolean
300 (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
301 (default #t))
302
303 ;; Boolean
304 (gateway-ports? openssh-configuration-gateway-ports?
305 (default #f))
306
307 ;; Boolean
308 (challenge-response-authentication? openssh-challenge-response-authentication?
309 (default #f))
310 ;; Boolean
311 (use-pam? openssh-configuration-use-pam?
312 (default #t))
313 ;; Boolean
314 (print-last-log? openssh-configuration-print-last-log?
315 (default #t))
316 ;; list of two-element lists
317 (subsystems openssh-configuration-subsystems
318 (default '(("sftp" "internal-sftp"))))
319
320 ;; list of strings
321 (accepted-environment openssh-configuration-accepted-environment
322 (default '()))
323
324 ;; symbol
325 (log-level openssh-configuration-log-level
326 (default 'info))
327
328 ;; String
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
332 (default ""))
333
334 ;; list of user-name/file-like tuples
335 (authorized-keys openssh-authorized-keys
336 (default '()))
337
338 ;; Boolean
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
341 ;; for now.
342 (%auto-start? openssh-auto-start?
343 (default #t)))
344
345 (define %openssh-accounts
346 (list (user-group (name "sshd") (system? #t))
347 (user-account
348 (name "sshd")
349 (group "sshd")
350 (system? #t)
351 (comment "sshd privilege separation user")
352 (home-directory "/var/run/sshd")
353 (shell (file-append shadow "/sbin/nologin")))))
354
355 (define (openssh-activation config)
356 "Return the activation GEXP for CONFIG."
357 (with-imported-modules '((guix build utils))
358 #~(begin
359 (use-modules (guix build utils))
360
361 (define (touch file-name)
362 (call-with-output-file file-name (const #t)))
363
364 ;; Make sure /etc/ssh can be read by the 'sshd' user.
365 (mkdir-p "/etc/ssh")
366 (chmod "/etc/ssh" #o755)
367 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
368
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.
372 (catch 'system-error
373 (lambda ()
374 (delete-file-recursively "/etc/authorized_keys.d"))
375 (lambda args
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")
381
382 (chmod "/etc/ssh/authorized_keys.d" #o555)
383
384 (let ((lastlog "/var/log/lastlog"))
385 (when #$(openssh-configuration-print-last-log? config)
386 (unless (file-exists? lastlog)
387 (touch lastlog))))
388
389 ;; Generate missing host keys.
390 (system* (string-append #$(openssh-configuration-openssh config)
391 "/bin/ssh-keygen") "-A"))))
392
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."
396 (define build
397 (with-imported-modules (source-module-closure '((guix build utils)))
398 #~(begin
399 (use-modules (ice-9 match) (srfi srfi-26)
400 (guix build utils))
401
402 (mkdir #$output)
403 (for-each (match-lambda
404 ((user keys ...)
405 (let ((file (string-append #$output "/" user)))
406 (call-with-output-file file
407 (lambda (port)
408 (for-each (lambda (key)
409 (call-with-input-file key
410 (cut dump-port <> port)))
411 keys))))))
412 '#$keys))))
413
414 (computed-file "openssh-authorized-keys" build))
415
416 (define (openssh-config-file config)
417 "Return the sshd configuration file corresponding to CONFIG."
418 (computed-file
419 "sshd_config"
420 #~(begin
421 (use-modules (ice-9 match))
422 (call-with-output-file #$output
423 (lambda (port)
424 (display "# Generated by 'openssh-service'.\n" port)
425 (format port "Port ~a\n"
426 #$(number->string
427 (openssh-configuration-port-number config)))
428 (format port "PermitRootLogin ~a\n"
429 #$(match (openssh-configuration-permit-root-login config)
430 (#t "yes")
431 (#f "no")
432 ('without-password "without-password")))
433 (format port "PermitEmptyPasswords ~a\n"
434 #$(if (openssh-configuration-allow-empty-passwords? config)
435 "yes" "no"))
436 (format port "PasswordAuthentication ~a\n"
437 #$(if (openssh-configuration-password-authentication? config)
438 "yes" "no"))
439 (format port "PubkeyAuthentication ~a\n"
440 #$(if (openssh-configuration-public-key-authentication?
441 config)
442 "yes" "no"))
443 (format port "X11Forwarding ~a\n"
444 #$(if (openssh-configuration-x11-forwarding? config)
445 "yes" "no"))
446 (format port "AllowAgentForwarding ~a\n"
447 #$(if (openssh-configuration-allow-agent-forwarding? config)
448 "yes" "no"))
449 (format port "AllowTcpForwarding ~a\n"
450 #$(if (openssh-configuration-allow-tcp-forwarding? config)
451 "yes" "no"))
452 (format port "GatewayPorts ~a\n"
453 #$(if (openssh-configuration-gateway-ports? config)
454 "yes" "no"))
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)
459 "yes" "no"))
460 (format port "UsePAM ~a\n"
461 #$(if (openssh-configuration-use-pam? config)
462 "yes" "no"))
463 (format port "PrintLastLog ~a\n"
464 #$(if (openssh-configuration-print-last-log? config)
465 "yes" "no"))
466 (format port "LogLevel ~a\n"
467 #$(string-upcase
468 (symbol->string
469 (openssh-configuration-log-level config))))
470
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")
474
475 (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
476 '#$(openssh-configuration-accepted-environment config))
477
478 (for-each
479 (match-lambda
480 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
481 '#$(openssh-configuration-subsystems config))
482
483 (format port "~a\n"
484 #$(openssh-configuration-extra-content config))
485 #t)))))
486
487 (define (openssh-shepherd-service config)
488 "Return a <shepherd-service> for openssh with CONFIG."
489
490 (define pid-file
491 (openssh-configuration-pid-file config))
492
493 (define openssh-command
494 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
495 "-D" "-f" #$(openssh-config-file config)))
496
497 (list (shepherd-service
498 (documentation "OpenSSH server.")
499 (requirement '(syslogd loopback))
500 (provision '(ssh-daemon ssh sshd))
501 (start #~(make-forkexec-constructor #$openssh-command
502 #:pid-file #$pid-file))
503 (stop #~(make-kill-destructor))
504 (auto-start? (openssh-auto-start? config)))))
505
506 (define (openssh-pam-services config)
507 "Return a list of <pam-services> for sshd with CONFIG."
508 (list (unix-pam-service
509 "sshd"
510 #:login-uid? #t
511 #:allow-empty-passwords?
512 (openssh-configuration-allow-empty-passwords? config))))
513
514 (define (extend-openssh-authorized-keys config keys)
515 "Extend CONFIG with the extra authorized keys listed in KEYS."
516 (openssh-configuration
517 (inherit config)
518 (authorized-keys
519 (append (openssh-authorized-keys config) keys))))
520
521 (define openssh-service-type
522 (service-type (name 'openssh)
523 (description
524 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
525 (extensions
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
531 openssh-activation)
532 (service-extension account-service-type
533 (const %openssh-accounts))
534
535 ;; Install OpenSSH in the system profile. That way,
536 ;; 'scp' is found when someone tries to copy to or from
537 ;; this machine.
538 (service-extension profile-service-type
539 (lambda (config)
540 (list (openssh-configuration-openssh
541 config))))))
542 (compose concatenate)
543 (extend extend-openssh-authorized-keys)
544 (default-value (openssh-configuration))))
545
546 \f
547 ;;;
548 ;;; Dropbear.
549 ;;;
550
551 (define-record-type* <dropbear-configuration>
552 dropbear-configuration make-dropbear-configuration
553 dropbear-configuration?
554 (dropbear dropbear-configuration-dropbear
555 (default dropbear))
556 (port-number dropbear-configuration-port-number
557 (default 22))
558 (syslog-output? dropbear-configuration-syslog-output?
559 (default #t))
560 (pid-file dropbear-configuration-pid-file
561 (default "/var/run/dropbear.pid"))
562 (root-login? dropbear-configuration-root-login?
563 (default #f))
564 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
565 (default #f))
566 (password-authentication? dropbear-configuration-password-authentication?
567 (default #t)))
568
569 (define (dropbear-activation config)
570 "Return the activation gexp for CONFIG."
571 #~(begin
572 (use-modules (guix build utils))
573 (mkdir-p "/etc/dropbear")))
574
575 (define (dropbear-shepherd-service config)
576 "Return a <shepherd-service> for dropbear with CONFIG."
577 (define dropbear
578 (dropbear-configuration-dropbear config))
579
580 (define pid-file
581 (dropbear-configuration-pid-file config))
582
583 (define dropbear-command
584 #~(list (string-append #$dropbear "/sbin/dropbear")
585
586 ;; '-R' allows host keys to be automatically generated upon first
587 ;; connection, at a time when /dev/urandom is more likely securely
588 ;; seeded.
589 "-F" "-R"
590
591 "-p" #$(number->string (dropbear-configuration-port-number config))
592 "-P" #$pid-file
593 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
594 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
595 #$@(if (dropbear-configuration-password-authentication? config)
596 '()
597 '("-s" "-g"))
598 #$@(if (dropbear-configuration-allow-empty-passwords? config)
599 '("-B")
600 '())))
601
602 (define requires
603 (if (dropbear-configuration-syslog-output? config)
604 '(networking syslogd) '(networking)))
605
606 (list (shepherd-service
607 (documentation "Dropbear SSH server.")
608 (requirement requires)
609 (provision '(ssh-daemon ssh sshd))
610 (start #~(make-forkexec-constructor #$dropbear-command
611 #:pid-file #$pid-file))
612 (stop #~(make-kill-destructor)))))
613
614 (define dropbear-service-type
615 (service-type (name 'dropbear)
616 (description
617 "Run the Dropbear secure shell (SSH) server.")
618 (extensions
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))))
624
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>}
628 object."
629 (service dropbear-service-type config))
630
631 ;;; ssh.scm ends here