services: openssh: Add escape hatch.
[jackhill/guix/guix.git] / gnu / services / ssh.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 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))
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 #:allow-empty-passwords?
186 (lsh-configuration-allow-empty-passwords? config))))
187
188 (define lsh-service-type
189 (service-type (name 'lsh)
190 (description
191 "Run the GNU@tie{}lsh secure shell (SSH) daemon,
192 @command{lshd}.")
193 (extensions
194 (list (service-extension shepherd-root-service-type
195 lsh-shepherd-service)
196 (service-extension pam-root-service-type
197 lsh-pam-services)
198 (service-extension activation-service-type
199 lsh-activation)))))
200
201 (define* (lsh-service #:key
202 (lsh lsh)
203 (daemonic? #t)
204 (host-key "/etc/lsh/host-key")
205 (interfaces '())
206 (port-number 22)
207 (allow-empty-passwords? #f)
208 (root-login? #f)
209 (syslog-output? #t)
210 (pid-file? #f)
211 (pid-file "/var/run/lshd.pid")
212 (x11-forwarding? #t)
213 (tcp/ip-forwarding? #t)
214 (password-authentication? #t)
215 (public-key-authentication? #t)
216 (initialize? #t))
217 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
218 @var{host-key} must designate a file containing the host key, and readable
219 only by root.
220
221 When @var{daemonic?} is true, @command{lshd} will detach from the
222 controlling terminal and log its output to syslogd, unless one sets
223 @var{syslog-output?} to false. Obviously, it also makes lsh-service
224 depend on existence of syslogd service. When @var{pid-file?} is true,
225 @command{lshd} writes its PID to the file called @var{pid-file}.
226
227 When @var{initialize?} is true, automatically create the seed and host key
228 upon service activation if they do not exist yet. This may take long and
229 require interaction.
230
231 When @var{initialize?} is false, it is up to the user to initialize the
232 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
233 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
234 basics,,, lsh, LSH Manual}).
235
236 When @var{interfaces} is empty, lshd listens for connections on all the
237 network interfaces; otherwise, @var{interfaces} must be a list of host names
238 or addresses.
239
240 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
241 passwords, and @var{root-login?} specifies whether to accept log-ins as
242 root.
243
244 The other options should be self-descriptive."
245 (service lsh-service-type
246 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
247 (host-key host-key) (interfaces interfaces)
248 (port-number port-number)
249 (allow-empty-passwords? allow-empty-passwords?)
250 (root-login? root-login?)
251 (syslog-output? syslog-output?)
252 (pid-file? pid-file?) (pid-file pid-file)
253 (x11-forwarding? x11-forwarding?)
254 (tcp/ip-forwarding? tcp/ip-forwarding?)
255 (password-authentication?
256 password-authentication?)
257 (public-key-authentication?
258 public-key-authentication?)
259 (initialize? initialize?))))
260
261 \f
262 ;;;
263 ;;; OpenSSH.
264 ;;;
265
266 (define-record-type* <openssh-configuration>
267 openssh-configuration make-openssh-configuration
268 openssh-configuration?
269 ;; <package>
270 (openssh openssh-configuration-openssh
271 (default openssh))
272 ;; string
273 (pid-file openssh-configuration-pid-file
274 (default "/var/run/sshd.pid"))
275 ;; integer
276 (port-number openssh-configuration-port-number
277 (default 22))
278 ;; Boolean | 'without-password
279 (permit-root-login openssh-configuration-permit-root-login
280 (default #f))
281 ;; Boolean
282 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
283 (default #f))
284 ;; Boolean
285 (password-authentication? openssh-configuration-password-authentication?
286 (default #t))
287 ;; Boolean
288 (public-key-authentication? openssh-configuration-public-key-authentication?
289 (default #t))
290 ;; Boolean
291 (x11-forwarding? openssh-configuration-x11-forwarding?
292 (default #f))
293
294 ;; Boolean
295 (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
296 (default #t))
297
298 ;; Boolean
299 (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
300 (default #t))
301
302 ;; Boolean
303 (gateway-ports? openssh-configuration-gateway-ports?
304 (default #f))
305
306 ;; Boolean
307 (challenge-response-authentication? openssh-challenge-response-authentication?
308 (default #f))
309 ;; Boolean
310 (use-pam? openssh-configuration-use-pam?
311 (default #t))
312 ;; Boolean
313 (print-last-log? openssh-configuration-print-last-log?
314 (default #t))
315 ;; list of two-element lists
316 (subsystems openssh-configuration-subsystems
317 (default '(("sftp" "internal-sftp"))))
318
319 ;; list of strings
320 (accepted-environment openssh-configuration-accepted-environment
321 (default '()))
322
323 ;; symbol
324 (log-level openssh-configuration-log-level
325 (default 'info))
326
327 ;; String
328 ;; This is an "escape hatch" to provide configuration that isn't yet
329 ;; supported by this configuration record.
330 (extra-content openssh-configuration-extra-content
331 (default ""))
332
333 ;; list of user-name/file-like tuples
334 (authorized-keys openssh-authorized-keys
335 (default '()))
336
337 ;; Boolean
338 ;; XXX: This should really be handled in an orthogonal way, for instance as
339 ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
340 ;; for now.
341 (%auto-start? openssh-auto-start?
342 (default #t)))
343
344 (define %openssh-accounts
345 (list (user-group (name "sshd") (system? #t))
346 (user-account
347 (name "sshd")
348 (group "sshd")
349 (system? #t)
350 (comment "sshd privilege separation user")
351 (home-directory "/var/run/sshd")
352 (shell #~(string-append #$shadow "/sbin/nologin")))))
353
354 (define (openssh-activation config)
355 "Return the activation GEXP for CONFIG."
356 (with-imported-modules '((guix build utils))
357 #~(begin
358 (use-modules (guix build utils))
359
360 (define (touch file-name)
361 (call-with-output-file file-name (const #t)))
362
363 ;; Make sure /etc/ssh can be read by the 'sshd' user.
364 (mkdir-p "/etc/ssh")
365 (chmod "/etc/ssh" #o755)
366 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
367
368 ;; 'sshd' complains if the authorized-key directory and its parents
369 ;; are group-writable, which rules out /gnu/store. Thus we copy the
370 ;; authorized-key directory to /etc.
371 (catch 'system-error
372 (lambda ()
373 (delete-file-recursively "/etc/authorized_keys.d"))
374 (lambda args
375 (unless (= ENOENT (system-error-errno args))
376 (apply throw args))))
377 (copy-recursively #$(authorized-key-directory
378 (openssh-authorized-keys config))
379 "/etc/ssh/authorized_keys.d")
380
381 (chmod "/etc/ssh/authorized_keys.d" #o555)
382
383 (let ((lastlog "/var/log/lastlog"))
384 (when #$(openssh-configuration-print-last-log? config)
385 (unless (file-exists? lastlog)
386 (touch lastlog))))
387
388 ;; Generate missing host keys.
389 (system* (string-append #$(openssh-configuration-openssh config)
390 "/bin/ssh-keygen") "-A"))))
391
392 (define (authorized-key-directory keys)
393 "Return a directory containing the authorized keys specified in KEYS, a list
394 of user-name/file-like tuples."
395 (define build
396 (with-imported-modules (source-module-closure '((guix build utils)))
397 #~(begin
398 (use-modules (ice-9 match) (srfi srfi-26)
399 (guix build utils))
400
401 (mkdir #$output)
402 (for-each (match-lambda
403 ((user keys ...)
404 (let ((file (string-append #$output "/" user)))
405 (call-with-output-file file
406 (lambda (port)
407 (for-each (lambda (key)
408 (call-with-input-file key
409 (cut dump-port <> port)))
410 keys))))))
411 '#$keys))))
412
413 (computed-file "openssh-authorized-keys" build))
414
415 (define (openssh-config-file config)
416 "Return the sshd configuration file corresponding to CONFIG."
417 (computed-file
418 "sshd_config"
419 #~(begin
420 (use-modules (ice-9 match))
421 (call-with-output-file #$output
422 (lambda (port)
423 (display "# Generated by 'openssh-service'.\n" port)
424 (format port "Port ~a\n"
425 #$(number->string
426 (openssh-configuration-port-number config)))
427 (format port "PermitRootLogin ~a\n"
428 #$(match (openssh-configuration-permit-root-login config)
429 (#t "yes")
430 (#f "no")
431 ('without-password "without-password")))
432 (format port "PermitEmptyPasswords ~a\n"
433 #$(if (openssh-configuration-allow-empty-passwords? config)
434 "yes" "no"))
435 (format port "PasswordAuthentication ~a\n"
436 #$(if (openssh-configuration-password-authentication? config)
437 "yes" "no"))
438 (format port "PubkeyAuthentication ~a\n"
439 #$(if (openssh-configuration-public-key-authentication?
440 config)
441 "yes" "no"))
442 (format port "X11Forwarding ~a\n"
443 #$(if (openssh-configuration-x11-forwarding? config)
444 "yes" "no"))
445 (format port "AllowAgentForwarding ~a\n"
446 #$(if (openssh-configuration-allow-agent-forwarding? config)
447 "yes" "no"))
448 (format port "AllowTcpForwarding ~a\n"
449 #$(if (openssh-configuration-allow-tcp-forwarding? config)
450 "yes" "no"))
451 (format port "GatewayPorts ~a\n"
452 #$(if (openssh-configuration-gateway-ports? config)
453 "yes" "no"))
454 (format port "PidFile ~a\n"
455 #$(openssh-configuration-pid-file config))
456 (format port "ChallengeResponseAuthentication ~a\n"
457 #$(if (openssh-challenge-response-authentication? config)
458 "yes" "no"))
459 (format port "UsePAM ~a\n"
460 #$(if (openssh-configuration-use-pam? config)
461 "yes" "no"))
462 (format port "PrintLastLog ~a\n"
463 #$(if (openssh-configuration-print-last-log? config)
464 "yes" "no"))
465 (format port "LogLevel ~a\n"
466 #$(string-upcase
467 (symbol->string
468 (openssh-configuration-log-level config))))
469
470 ;; Add '/etc/authorized_keys.d/%u', which we populate.
471 (format port "AuthorizedKeysFile \
472 .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
473
474 (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
475 '#$(openssh-configuration-accepted-environment config))
476
477 (for-each
478 (match-lambda
479 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
480 '#$(openssh-configuration-subsystems config))
481
482 (format port "~a\n"
483 #$(openssh-configuration-extra-content config))
484 #t)))))
485
486 (define (openssh-shepherd-service config)
487 "Return a <shepherd-service> for openssh with CONFIG."
488
489 (define pid-file
490 (openssh-configuration-pid-file config))
491
492 (define openssh-command
493 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
494 "-D" "-f" #$(openssh-config-file config)))
495
496 (list (shepherd-service
497 (documentation "OpenSSH server.")
498 (requirement '(syslogd loopback))
499 (provision '(ssh-daemon))
500 (start #~(make-forkexec-constructor #$openssh-command
501 #:pid-file #$pid-file))
502 (stop #~(make-kill-destructor))
503 (auto-start? (openssh-auto-start? config)))))
504
505 (define (openssh-pam-services config)
506 "Return a list of <pam-services> for sshd with CONFIG."
507 (list (unix-pam-service
508 "sshd"
509 #:allow-empty-passwords?
510 (openssh-configuration-allow-empty-passwords? config))))
511
512 (define (extend-openssh-authorized-keys config keys)
513 "Extend CONFIG with the extra authorized keys listed in KEYS."
514 (openssh-configuration
515 (inherit config)
516 (authorized-keys
517 (append (openssh-authorized-keys config) keys))))
518
519 (define openssh-service-type
520 (service-type (name 'openssh)
521 (description
522 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
523 (extensions
524 (list (service-extension shepherd-root-service-type
525 openssh-shepherd-service)
526 (service-extension pam-root-service-type
527 openssh-pam-services)
528 (service-extension activation-service-type
529 openssh-activation)
530 (service-extension account-service-type
531 (const %openssh-accounts))
532
533 ;; Install OpenSSH in the system profile. That way,
534 ;; 'scp' is found when someone tries to copy to or from
535 ;; this machine.
536 (service-extension profile-service-type
537 (lambda (config)
538 (list (openssh-configuration-openssh
539 config))))))
540 (compose concatenate)
541 (extend extend-openssh-authorized-keys)
542 (default-value (openssh-configuration))))
543
544 \f
545 ;;;
546 ;;; Dropbear.
547 ;;;
548
549 (define-record-type* <dropbear-configuration>
550 dropbear-configuration make-dropbear-configuration
551 dropbear-configuration?
552 (dropbear dropbear-configuration-dropbear
553 (default dropbear))
554 (port-number dropbear-configuration-port-number
555 (default 22))
556 (syslog-output? dropbear-configuration-syslog-output?
557 (default #t))
558 (pid-file dropbear-configuration-pid-file
559 (default "/var/run/dropbear.pid"))
560 (root-login? dropbear-configuration-root-login?
561 (default #f))
562 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
563 (default #f))
564 (password-authentication? dropbear-configuration-password-authentication?
565 (default #t)))
566
567 (define (dropbear-activation config)
568 "Return the activation gexp for CONFIG."
569 #~(begin
570 (use-modules (guix build utils))
571 (mkdir-p "/etc/dropbear")))
572
573 (define (dropbear-shepherd-service config)
574 "Return a <shepherd-service> for dropbear with CONFIG."
575 (define dropbear
576 (dropbear-configuration-dropbear config))
577
578 (define pid-file
579 (dropbear-configuration-pid-file config))
580
581 (define dropbear-command
582 #~(list (string-append #$dropbear "/sbin/dropbear")
583
584 ;; '-R' allows host keys to be automatically generated upon first
585 ;; connection, at a time when /dev/urandom is more likely securely
586 ;; seeded.
587 "-F" "-R"
588
589 "-p" #$(number->string (dropbear-configuration-port-number config))
590 "-P" #$pid-file
591 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
592 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
593 #$@(if (dropbear-configuration-password-authentication? config)
594 '()
595 '("-s" "-g"))
596 #$@(if (dropbear-configuration-allow-empty-passwords? config)
597 '("-B")
598 '())))
599
600 (define requires
601 (if (dropbear-configuration-syslog-output? config)
602 '(networking syslogd) '(networking)))
603
604 (list (shepherd-service
605 (documentation "Dropbear SSH server.")
606 (requirement requires)
607 (provision '(ssh-daemon))
608 (start #~(make-forkexec-constructor #$dropbear-command
609 #:pid-file #$pid-file))
610 (stop #~(make-kill-destructor)))))
611
612 (define dropbear-service-type
613 (service-type (name 'dropbear)
614 (description
615 "Run the Dropbear secure shell (SSH) server.")
616 (extensions
617 (list (service-extension shepherd-root-service-type
618 dropbear-shepherd-service)
619 (service-extension activation-service-type
620 dropbear-activation)))))
621
622 (define* (dropbear-service #:optional (config (dropbear-configuration)))
623 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
624 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
625 object."
626 (service dropbear-service-type config))
627
628 ;;; ssh.scm ends here