services: openssh: Change 'authorized-keys' accessor name.
[jackhill/guix/guix.git] / gnu / services / ssh.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014-2019, 2022 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 ;;; Copyright © 2020 pinoaffe <pinoaffe@airmail.cc>
8 ;;; Copyright © 2020 Oleg Pykhalov <go.wigust@gmail.com>
9 ;;; Copyright © 2020 Brice Waegeneire <brice@waegenei.re>
10 ;;; Copyright © 2021 Tobias Geerinckx-Rice <me@tobias.gr>
11 ;;;
12 ;;; This file is part of GNU Guix.
13 ;;;
14 ;;; GNU Guix is free software; you can redistribute it and/or modify it
15 ;;; under the terms of the GNU General Public License as published by
16 ;;; the Free Software Foundation; either version 3 of the License, or (at
17 ;;; your option) any later version.
18 ;;;
19 ;;; GNU Guix is distributed in the hope that it will be useful, but
20 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;;; GNU General Public License for more details.
23 ;;;
24 ;;; You should have received a copy of the GNU General Public License
25 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
26
27 (define-module (gnu services ssh)
28 #:use-module (gnu packages ssh)
29 #:use-module (gnu packages admin)
30 #:use-module (gnu services)
31 #:use-module (gnu services shepherd)
32 #:use-module (gnu services web)
33 #:use-module (gnu system pam)
34 #:use-module (gnu system shadow)
35 #:use-module (guix deprecation)
36 #:use-module (guix gexp)
37 #:use-module (guix records)
38 #:use-module (guix modules)
39 #:use-module (srfi srfi-1)
40 #:use-module (srfi srfi-26)
41 #:use-module (ice-9 match)
42 #:use-module (ice-9 vlist)
43 #:export (lsh-configuration
44 lsh-configuration?
45 lsh-service
46 lsh-service-type
47
48 openssh-configuration
49 openssh-configuration?
50 openssh-service-type
51
52 dropbear-configuration
53 dropbear-configuration?
54 dropbear-service-type
55 dropbear-service
56
57 autossh-configuration
58 autossh-configuration?
59 autossh-service-type
60
61 webssh-configuration
62 webssh-configuration?
63 webssh-service-type
64 %webssh-configuration-nginx))
65
66 ;;; Commentary:
67 ;;;
68 ;;; This module implements secure shell (SSH) services.
69 ;;;
70 ;;; Code:
71
72 (define-record-type* <lsh-configuration>
73 lsh-configuration make-lsh-configuration
74 lsh-configuration?
75 (lsh lsh-configuration-lsh
76 (default lsh))
77 (daemonic? lsh-configuration-daemonic?)
78 (host-key lsh-configuration-host-key)
79 (interfaces lsh-configuration-interfaces)
80 (port-number lsh-configuration-port-number)
81 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
82 (root-login? lsh-configuration-root-login?)
83 (syslog-output? lsh-configuration-syslog-output?)
84 (pid-file? lsh-configuration-pid-file?)
85 (pid-file lsh-configuration-pid-file)
86 (x11-forwarding? lsh-configuration-x11-forwarding?)
87 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
88 (password-authentication? lsh-configuration-password-authentication?)
89 (public-key-authentication? lsh-configuration-public-key-authentication?)
90 (initialize? lsh-configuration-initialize?))
91
92 (define %yarrow-seed
93 "/var/spool/lsh/yarrow-seed-file")
94
95 (define (lsh-initialization lsh host-key)
96 "Return the gexp to initialize the LSH service for HOST-KEY."
97 #~(begin
98 (unless (file-exists? #$%yarrow-seed)
99 (system* (string-append #$lsh "/bin/lsh-make-seed")
100 "--sloppy" "-o" #$%yarrow-seed))
101
102 (unless (file-exists? #$host-key)
103 (mkdir-p (dirname #$host-key))
104 (format #t "creating SSH host key '~a'...~%" #$host-key)
105
106 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
107 ;; used yet because /bin/sh might be dangling; factorize this somehow.
108 (let* ((in+out (pipe))
109 (keygen (primitive-fork)))
110 (case keygen
111 ((0)
112 (close-port (car in+out))
113 (close-fdes 1)
114 (dup2 (fileno (cdr in+out)) 1)
115 (execl (string-append #$lsh "/bin/lsh-keygen")
116 "lsh-keygen" "--server"))
117 (else
118 (let ((write-key (primitive-fork)))
119 (case write-key
120 ((0)
121 (close-port (cdr in+out))
122 (close-fdes 0)
123 (dup2 (fileno (car in+out)) 0)
124 (execl (string-append #$lsh "/bin/lsh-writekey")
125 "lsh-writekey" "--server" "-o" #$host-key))
126 (else
127 (close-port (car in+out))
128 (close-port (cdr in+out))
129 (waitpid keygen)
130 (waitpid write-key))))))))))
131
132 (define (lsh-activation config)
133 "Return the activation gexp for CONFIG."
134 #~(begin
135 (use-modules (guix build utils))
136 (mkdir-p "/var/spool/lsh")
137 #$(if (lsh-configuration-initialize? config)
138 (lsh-initialization (lsh-configuration-lsh config)
139 (lsh-configuration-host-key config))
140 #t)))
141
142 (define (lsh-shepherd-service config)
143 "Return a <shepherd-service> for lsh with CONFIG."
144 (define lsh (lsh-configuration-lsh config))
145 (define pid-file (lsh-configuration-pid-file config))
146 (define pid-file? (lsh-configuration-pid-file? config))
147 (define daemonic? (lsh-configuration-daemonic? config))
148 (define interfaces (lsh-configuration-interfaces config))
149
150 (define lsh-command
151 (append
152 (cons (file-append lsh "/sbin/lshd")
153 (if daemonic?
154 (let ((syslog (if (lsh-configuration-syslog-output? config)
155 '()
156 (list "--no-syslog"))))
157 (cons "--daemonic"
158 (if pid-file?
159 (cons #~(string-append "--pid-file=" #$pid-file)
160 syslog)
161 (cons "--no-pid-file" syslog))))
162 (if pid-file?
163 (list #~(string-append "--pid-file=" #$pid-file))
164 '())))
165 (cons* #~(string-append "--host-key="
166 #$(lsh-configuration-host-key config))
167 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
168 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
169 "-p" (number->string (lsh-configuration-port-number config))
170 (if (lsh-configuration-password-authentication? config)
171 "--password" "--no-password")
172 (if (lsh-configuration-public-key-authentication? config)
173 "--publickey" "--no-publickey")
174 (if (lsh-configuration-root-login? config)
175 "--root-login" "--no-root-login")
176 (if (lsh-configuration-x11-forwarding? config)
177 "--x11-forward" "--no-x11-forward")
178 (if (lsh-configuration-tcp/ip-forwarding? config)
179 "--tcpip-forward" "--no-tcpip-forward")
180 (if (null? interfaces)
181 '()
182 (map (cut string-append "--interface=" <>)
183 interfaces)))))
184
185 (define requires
186 (if (and daemonic? (lsh-configuration-syslog-output? config))
187 '(networking syslogd)
188 '(networking)))
189
190 (list (shepherd-service
191 (documentation "GNU lsh SSH server")
192 (provision '(ssh-daemon ssh sshd))
193 (requirement requires)
194 (start #~(make-forkexec-constructor (list #$@lsh-command)))
195 (stop #~(make-kill-destructor)))))
196
197 (define (lsh-pam-services config)
198 "Return a list of <pam-services> for lshd with CONFIG."
199 (list (unix-pam-service
200 "lshd"
201 #:login-uid? #t
202 #:allow-empty-passwords?
203 (lsh-configuration-allow-empty-passwords? config))))
204
205 (define lsh-service-type
206 (service-type (name 'lsh)
207 (description
208 "Run the GNU@tie{}lsh secure shell (SSH) daemon,
209 @command{lshd}.")
210 (extensions
211 (list (service-extension shepherd-root-service-type
212 lsh-shepherd-service)
213 (service-extension pam-root-service-type
214 lsh-pam-services)
215 (service-extension activation-service-type
216 lsh-activation)))))
217
218 (define* (lsh-service #:key
219 (lsh lsh)
220 (daemonic? #t)
221 (host-key "/etc/lsh/host-key")
222 (interfaces '())
223 (port-number 22)
224 (allow-empty-passwords? #f)
225 (root-login? #f)
226 (syslog-output? #t)
227 (pid-file? #f)
228 (pid-file "/var/run/lshd.pid")
229 (x11-forwarding? #t)
230 (tcp/ip-forwarding? #t)
231 (password-authentication? #t)
232 (public-key-authentication? #t)
233 (initialize? #t))
234 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
235 @var{host-key} must designate a file containing the host key, and readable
236 only by root.
237
238 When @var{daemonic?} is true, @command{lshd} will detach from the
239 controlling terminal and log its output to syslogd, unless one sets
240 @var{syslog-output?} to false. Obviously, it also makes lsh-service
241 depend on existence of syslogd service. When @var{pid-file?} is true,
242 @command{lshd} writes its PID to the file called @var{pid-file}.
243
244 When @var{initialize?} is true, automatically create the seed and host key
245 upon service activation if they do not exist yet. This may take long and
246 require interaction.
247
248 When @var{initialize?} is false, it is up to the user to initialize the
249 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
250 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
251 basics,,, lsh, LSH Manual}).
252
253 When @var{interfaces} is empty, lshd listens for connections on all the
254 network interfaces; otherwise, @var{interfaces} must be a list of host names
255 or addresses.
256
257 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
258 passwords, and @var{root-login?} specifies whether to accept log-ins as
259 root.
260
261 The other options should be self-descriptive."
262 (service lsh-service-type
263 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
264 (host-key host-key) (interfaces interfaces)
265 (port-number port-number)
266 (allow-empty-passwords? allow-empty-passwords?)
267 (root-login? root-login?)
268 (syslog-output? syslog-output?)
269 (pid-file? pid-file?) (pid-file pid-file)
270 (x11-forwarding? x11-forwarding?)
271 (tcp/ip-forwarding? tcp/ip-forwarding?)
272 (password-authentication?
273 password-authentication?)
274 (public-key-authentication?
275 public-key-authentication?)
276 (initialize? initialize?))))
277
278 \f
279 ;;;
280 ;;; OpenSSH.
281 ;;;
282
283 (define-record-type* <openssh-configuration>
284 openssh-configuration make-openssh-configuration
285 openssh-configuration?
286 ;; file-like object
287 (openssh openssh-configuration-openssh
288 (default openssh))
289 ;; string
290 (pid-file openssh-configuration-pid-file
291 (default "/var/run/sshd.pid"))
292 ;; integer
293 (port-number openssh-configuration-port-number
294 (default 22))
295 ;; Boolean | 'prohibit-password
296 (permit-root-login openssh-configuration-permit-root-login
297 (default #f))
298 ;; Boolean
299 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
300 (default #f))
301 ;; Boolean
302 (password-authentication? openssh-configuration-password-authentication?
303 (default #t))
304 ;; Boolean
305 (public-key-authentication? openssh-configuration-public-key-authentication?
306 (default #t))
307 ;; Boolean
308 (x11-forwarding? openssh-configuration-x11-forwarding?
309 (default #f))
310
311 ;; Boolean
312 (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
313 (default #t))
314
315 ;; Boolean
316 (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
317 (default #t))
318
319 ;; Boolean
320 (gateway-ports? openssh-configuration-gateway-ports?
321 (default #f))
322
323 ;; Boolean
324 (challenge-response-authentication? openssh-challenge-response-authentication?
325 (default #f))
326 ;; Boolean
327 (use-pam? openssh-configuration-use-pam?
328 (default #t))
329 ;; Boolean
330 (print-last-log? openssh-configuration-print-last-log?
331 (default #t))
332 ;; list of two-element lists
333 (subsystems openssh-configuration-subsystems
334 (default '(("sftp" "internal-sftp"))))
335
336 ;; list of strings
337 (accepted-environment openssh-configuration-accepted-environment
338 (default '()))
339
340 ;; symbol
341 (log-level openssh-configuration-log-level
342 (default 'info))
343
344 ;; String
345 ;; This is an "escape hatch" to provide configuration that isn't yet
346 ;; supported by this configuration record.
347 (extra-content openssh-configuration-extra-content
348 (default ""))
349
350 ;; list of user-name/file-like tuples
351 (authorized-keys openssh-configuration-authorized-keys
352 (default '()))
353
354 ;; Boolean
355 (generate-host-keys? openssh-configuration-generate-host-keys?
356 (default #t))
357
358 ;; Boolean
359 ;; XXX: This should really be handled in an orthogonal way, for instance as
360 ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
361 ;; for now.
362 (%auto-start? openssh-auto-start?
363 (default #t)))
364
365 (define %openssh-accounts
366 (list (user-group (name "sshd") (system? #t))
367 (user-account
368 (name "sshd")
369 (group "sshd")
370 (system? #t)
371 (comment "sshd privilege separation user")
372 (home-directory "/var/run/sshd")
373 (shell (file-append shadow "/sbin/nologin")))))
374
375 (define (openssh-activation config)
376 "Return the activation GEXP for CONFIG."
377 (with-imported-modules '((guix build utils))
378 #~(begin
379 (use-modules (guix build utils))
380
381 (define (touch file-name)
382 (call-with-output-file file-name (const #t)))
383
384 ;; Make sure /etc/ssh can be read by the 'sshd' user.
385 (mkdir-p "/etc/ssh")
386 (chmod "/etc/ssh" #o755)
387 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
388
389 ;; 'sshd' complains if the authorized-key directory and its parents
390 ;; are group-writable, which rules out /gnu/store. Thus we copy the
391 ;; authorized-key directory to /etc.
392 (catch 'system-error
393 (lambda ()
394 (delete-file-recursively "/etc/authorized_keys.d"))
395 (lambda args
396 (unless (= ENOENT (system-error-errno args))
397 (apply throw args))))
398 (copy-recursively #$(authorized-key-directory
399 (openssh-configuration-authorized-keys config))
400 "/etc/ssh/authorized_keys.d")
401
402 (chmod "/etc/ssh/authorized_keys.d" #o555)
403
404 (let ((lastlog "/var/log/lastlog"))
405 (when #$(openssh-configuration-print-last-log? config)
406 (unless (file-exists? lastlog)
407 (touch lastlog))))
408
409 (when #$(openssh-configuration-generate-host-keys? config)
410 ;; Generate missing host keys.
411 (system* (string-append #$(openssh-configuration-openssh config)
412 "/bin/ssh-keygen") "-A")))))
413
414 (define (authorized-key-directory keys)
415 "Return a directory containing the authorized keys specified in KEYS, a list
416 of user-name/file-like tuples."
417 (define build
418 (with-imported-modules (source-module-closure '((guix build utils)))
419 #~(begin
420 (use-modules (ice-9 match) (srfi srfi-26)
421 (guix build utils))
422
423 (mkdir #$output)
424 (for-each (match-lambda
425 ((user keys ...)
426 (let ((file (string-append #$output "/" user)))
427 (call-with-output-file file
428 (lambda (port)
429 (for-each (lambda (key)
430 (call-with-input-file key
431 (cut dump-port <> port)))
432 keys))))))
433 '#$keys))))
434
435 (computed-file "openssh-authorized-keys" build))
436
437 (define (openssh-config-file config)
438 "Return the sshd configuration file corresponding to CONFIG."
439 (computed-file
440 "sshd_config"
441 #~(begin
442 (use-modules (ice-9 match))
443 (call-with-output-file #$output
444 (lambda (port)
445 (display "# Generated by 'openssh-service'.\n" port)
446 (format port "Port ~a\n"
447 #$(number->string
448 (openssh-configuration-port-number config)))
449 (format port "PermitRootLogin ~a\n"
450 #$(match (openssh-configuration-permit-root-login config)
451 (#t "yes")
452 (#f "no")
453 ('without-password (warn-about-deprecation
454 'without-password #f
455 #:replacement 'prohibit-password)
456 "prohibit-password")
457 ('prohibit-password "prohibit-password")))
458 (format port "PermitEmptyPasswords ~a\n"
459 #$(if (openssh-configuration-allow-empty-passwords? config)
460 "yes" "no"))
461 (format port "PasswordAuthentication ~a\n"
462 #$(if (openssh-configuration-password-authentication? config)
463 "yes" "no"))
464 (format port "PubkeyAuthentication ~a\n"
465 #$(if (openssh-configuration-public-key-authentication?
466 config)
467 "yes" "no"))
468 (format port "X11Forwarding ~a\n"
469 #$(if (openssh-configuration-x11-forwarding? config)
470 "yes" "no"))
471 (format port "AllowAgentForwarding ~a\n"
472 #$(if (openssh-configuration-allow-agent-forwarding? config)
473 "yes" "no"))
474 (format port "AllowTcpForwarding ~a\n"
475 #$(if (openssh-configuration-allow-tcp-forwarding? config)
476 "yes" "no"))
477 (format port "GatewayPorts ~a\n"
478 #$(if (openssh-configuration-gateway-ports? config)
479 "yes" "no"))
480 (format port "PidFile ~a\n"
481 #$(openssh-configuration-pid-file config))
482 (format port "ChallengeResponseAuthentication ~a\n"
483 #$(if (openssh-challenge-response-authentication? config)
484 "yes" "no"))
485 (format port "UsePAM ~a\n"
486 #$(if (openssh-configuration-use-pam? config)
487 "yes" "no"))
488 (format port "PrintLastLog ~a\n"
489 #$(if (openssh-configuration-print-last-log? config)
490 "yes" "no"))
491 (format port "LogLevel ~a\n"
492 #$(string-upcase
493 (symbol->string
494 (openssh-configuration-log-level config))))
495
496 ;; Add '/etc/authorized_keys.d/%u', which we populate.
497 (format port "AuthorizedKeysFile \
498 .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
499
500 (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
501 '#$(openssh-configuration-accepted-environment config))
502
503 (for-each
504 (match-lambda
505 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
506 '#$(openssh-configuration-subsystems config))
507
508 (format port "~a\n"
509 #$(openssh-configuration-extra-content config))
510 #t)))))
511
512 (define (openssh-shepherd-service config)
513 "Return a <shepherd-service> for openssh with CONFIG."
514
515 (define pid-file
516 (openssh-configuration-pid-file config))
517
518 (define openssh-command
519 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
520 "-D" "-f" #$(openssh-config-file config)))
521
522 (list (shepherd-service
523 (documentation "OpenSSH server.")
524 (requirement '(syslogd loopback))
525 (provision '(ssh-daemon ssh sshd))
526 (start #~(make-forkexec-constructor #$openssh-command
527 #:pid-file #$pid-file))
528 (stop #~(make-kill-destructor))
529 (auto-start? (openssh-auto-start? config)))))
530
531 (define (openssh-pam-services config)
532 "Return a list of <pam-services> for sshd with CONFIG."
533 (list (unix-pam-service
534 "sshd"
535 #:login-uid? #t
536 #:allow-empty-passwords?
537 (openssh-configuration-allow-empty-passwords? config))))
538
539 (define (extend-openssh-authorized-keys config keys)
540 "Extend CONFIG with the extra authorized keys listed in KEYS."
541 (openssh-configuration
542 (inherit config)
543 (authorized-keys
544 (match (openssh-configuration-authorized-keys config)
545 (((users _ ...) ...)
546 ;; Build a user/key-list mapping.
547 (let ((user-keys (alist->vhash
548 (openssh-configuration-authorized-keys config))))
549 ;; Coalesce the key lists associated with each user.
550 (map (lambda (user)
551 `(,user
552 ,@(concatenate (vhash-fold* cons '() user user-keys))))
553 users)))))))
554
555 (define openssh-service-type
556 (service-type (name 'openssh)
557 (description
558 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
559 (extensions
560 (list (service-extension shepherd-root-service-type
561 openssh-shepherd-service)
562 (service-extension pam-root-service-type
563 openssh-pam-services)
564 (service-extension activation-service-type
565 openssh-activation)
566 (service-extension account-service-type
567 (const %openssh-accounts))
568
569 ;; Install OpenSSH in the system profile. That way,
570 ;; 'scp' is found when someone tries to copy to or from
571 ;; this machine.
572 (service-extension profile-service-type
573 (lambda (config)
574 (list (openssh-configuration-openssh
575 config))))))
576 (compose concatenate)
577 (extend extend-openssh-authorized-keys)
578 (default-value (openssh-configuration))))
579
580 \f
581 ;;;
582 ;;; Dropbear.
583 ;;;
584
585 (define-record-type* <dropbear-configuration>
586 dropbear-configuration make-dropbear-configuration
587 dropbear-configuration?
588 (dropbear dropbear-configuration-dropbear
589 (default dropbear))
590 (port-number dropbear-configuration-port-number
591 (default 22))
592 (syslog-output? dropbear-configuration-syslog-output?
593 (default #t))
594 (pid-file dropbear-configuration-pid-file
595 (default "/var/run/dropbear.pid"))
596 (root-login? dropbear-configuration-root-login?
597 (default #f))
598 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
599 (default #f))
600 (password-authentication? dropbear-configuration-password-authentication?
601 (default #t)))
602
603 (define (dropbear-activation config)
604 "Return the activation gexp for CONFIG."
605 #~(begin
606 (use-modules (guix build utils))
607 (mkdir-p "/etc/dropbear")))
608
609 (define (dropbear-shepherd-service config)
610 "Return a <shepherd-service> for dropbear with CONFIG."
611 (define dropbear
612 (dropbear-configuration-dropbear config))
613
614 (define pid-file
615 (dropbear-configuration-pid-file config))
616
617 (define dropbear-command
618 #~(list (string-append #$dropbear "/sbin/dropbear")
619
620 ;; '-R' allows host keys to be automatically generated upon first
621 ;; connection, at a time when /dev/urandom is more likely securely
622 ;; seeded.
623 "-F" "-R"
624
625 "-p" #$(number->string (dropbear-configuration-port-number config))
626 "-P" #$pid-file
627 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
628 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
629 #$@(if (dropbear-configuration-password-authentication? config)
630 '()
631 '("-s" "-g"))
632 #$@(if (dropbear-configuration-allow-empty-passwords? config)
633 '("-B")
634 '())))
635
636 (define requires
637 (if (dropbear-configuration-syslog-output? config)
638 '(networking syslogd) '(networking)))
639
640 (list (shepherd-service
641 (documentation "Dropbear SSH server.")
642 (requirement requires)
643 (provision '(ssh-daemon ssh sshd))
644 (start #~(make-forkexec-constructor #$dropbear-command
645 #:pid-file #$pid-file))
646 (stop #~(make-kill-destructor)))))
647
648 (define dropbear-service-type
649 (service-type (name 'dropbear)
650 (description
651 "Run the Dropbear secure shell (SSH) server.")
652 (extensions
653 (list (service-extension shepherd-root-service-type
654 dropbear-shepherd-service)
655 (service-extension activation-service-type
656 dropbear-activation)))
657 (default-value (dropbear-configuration))))
658
659 (define* (dropbear-service #:optional (config (dropbear-configuration)))
660 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
661 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
662 object."
663 (service dropbear-service-type config))
664
665 \f
666 ;;;
667 ;;; AutoSSH.
668 ;;;
669
670
671 (define-record-type* <autossh-configuration>
672 autossh-configuration make-autossh-configuration
673 autossh-configuration?
674 (user autossh-configuration-user
675 (default "autossh"))
676 (poll autossh-configuration-poll
677 (default 600))
678 (first-poll autossh-configuration-first-poll
679 (default #f))
680 (gate-time autossh-configuration-gate-time
681 (default 30))
682 (log-level autossh-configuration-log-level
683 (default 1))
684 (max-start autossh-configuration-max-start
685 (default #f))
686 (message autossh-configuration-message
687 (default ""))
688 (port autossh-configuration-port
689 (default "0"))
690 (ssh-options autossh-configuration-ssh-options
691 (default '())))
692
693 (define (autossh-file-name config file)
694 "Return a path in /var/run/autossh/ that is writable
695 by @code{user} from @code{config}."
696 (string-append "/var/run/autossh/"
697 (autossh-configuration-user config)
698 "/" file))
699
700 (define (autossh-shepherd-service config)
701 (shepherd-service
702 (documentation "Automatically set up ssh connections (and keep them alive).")
703 (provision '(autossh))
704 (start #~(make-forkexec-constructor
705 (list #$(file-append autossh "/bin/autossh")
706 #$@(autossh-configuration-ssh-options config))
707 #:user #$(autossh-configuration-user config)
708 #:group (passwd:gid (getpw #$(autossh-configuration-user config)))
709 #:pid-file #$(autossh-file-name config "pid")
710 #:log-file #$(autossh-file-name config "log")
711 #:environment-variables
712 '(#$(string-append "AUTOSSH_PIDFILE="
713 (autossh-file-name config "pid"))
714 #$(string-append "AUTOSSH_LOGFILE="
715 (autossh-file-name config "log"))
716 #$(string-append "AUTOSSH_POLL="
717 (number->string
718 (autossh-configuration-poll config)))
719 #$(string-append "AUTOSSH_FIRST_POLL="
720 (number->string
721 (or
722 (autossh-configuration-first-poll config)
723 (autossh-configuration-poll config))))
724 #$(string-append "AUTOSSH_GATETIME="
725 (number->string
726 (autossh-configuration-gate-time config)))
727 #$(string-append "AUTOSSH_LOGLEVEL="
728 (number->string
729 (autossh-configuration-log-level config)))
730 #$(string-append "AUTOSSH_MAXSTART="
731 (number->string
732 (or (autossh-configuration-max-start config)
733 -1)))
734 #$(string-append "AUTOSSH_MESSAGE="
735 (autossh-configuration-message config))
736 #$(string-append "AUTOSSH_PORT="
737 (autossh-configuration-port config)))))
738 (stop #~(make-kill-destructor))))
739
740 (define (autossh-service-activation config)
741 (with-imported-modules '((guix build utils))
742 #~(begin
743 (use-modules (guix build utils))
744 (define %user
745 (getpw #$(autossh-configuration-user config)))
746 (let* ((directory #$(autossh-file-name config ""))
747 (log (string-append directory "/log")))
748 (mkdir-p directory)
749 (chown directory (passwd:uid %user) (passwd:gid %user))
750 (call-with-output-file log (const #t))
751 (chown log (passwd:uid %user) (passwd:gid %user))))))
752
753 (define autossh-service-type
754 (service-type
755 (name 'autossh)
756 (description "Automatically set up ssh connections (and keep them alive).")
757 (extensions
758 (list (service-extension shepherd-root-service-type
759 (compose list autossh-shepherd-service))
760 (service-extension activation-service-type
761 autossh-service-activation)))
762 (default-value (autossh-configuration))))
763
764 \f
765 ;;;
766 ;;; WebSSH
767 ;;;
768
769 (define-record-type* <webssh-configuration>
770 webssh-configuration make-webssh-configuration
771 webssh-configuration?
772 (package webssh-configuration-package ;file-like
773 (default webssh))
774 (user-name webssh-configuration-user-name ;string
775 (default "webssh"))
776 (group-name webssh-configuration-group-name ;string
777 (default "webssh"))
778 (policy webssh-configuration-policy ;symbol
779 (default #f))
780 (known-hosts webssh-configuration-known-hosts ;list of strings
781 (default #f))
782 (port webssh-configuration-port ;number
783 (default #f))
784 (address webssh-configuration-address ;string
785 (default #f))
786 (log-file webssh-configuration-log-file ;string
787 (default "/var/log/webssh.log"))
788 (log-level webssh-configuration-log-level ;symbol
789 (default #f)))
790
791 (define %webssh-configuration-nginx
792 (nginx-server-configuration
793 (listen '("80"))
794 (locations
795 (list (nginx-location-configuration
796 (uri "/")
797 (body '("proxy_pass http://127.0.0.1:8888;"
798 "proxy_http_version 1.1;"
799 "proxy_read_timeout 300;"
800 "proxy_set_header Upgrade $http_upgrade;"
801 "proxy_set_header Connection \"upgrade\";"
802 "proxy_set_header Host $http_host;"
803 "proxy_set_header X-Real-IP $remote_addr;"
804 "proxy_set_header X-Real-PORT $remote_port;")))))))
805
806 (define webssh-account
807 ;; Return the user accounts and user groups for CONFIG.
808 (match-lambda
809 (($ <webssh-configuration> _ user-name group-name _ _ _ _ _ _)
810 (list (user-group
811 (name group-name))
812 (user-account
813 (name user-name)
814 (group group-name)
815 (comment "webssh privilege separation user")
816 (home-directory (string-append "/var/run/" user-name))
817 (shell #~(string-append #$shadow "/sbin/nologin")))))))
818
819 (define webssh-activation
820 ;; Return the activation GEXP for CONFIG.
821 (match-lambda
822 (($ <webssh-configuration> _ user-name group-name policy known-hosts _ _
823 log-file _)
824 (with-imported-modules '((guix build utils))
825 #~(begin
826 (let* ((home-dir (string-append "/var/run/" #$user-name))
827 (ssh-dir (string-append home-dir "/.ssh"))
828 (known-hosts-file (string-append ssh-dir "/known_hosts")))
829 (call-with-output-file #$log-file (const #t))
830 (mkdir-p ssh-dir)
831 (case '#$policy
832 ((reject)
833 (if '#$known-hosts
834 (call-with-output-file known-hosts-file
835 (lambda (port)
836 (for-each (lambda (host) (display host port) (newline port))
837 '#$known-hosts)))
838 (display-hint (G_ "webssh: reject policy requires `known-hosts'.")))))
839 (for-each (lambda (file)
840 (chown file
841 (passwd:uid (getpw #$user-name))
842 (group:gid (getpw #$group-name))))
843 (list #$log-file ssh-dir known-hosts-file))
844 (chmod ssh-dir #o700)))))))
845
846 (define webssh-shepherd-service
847 (match-lambda
848 (($ <webssh-configuration> package user-name group-name policy _ port
849 address log-file log-level)
850 (list (shepherd-service
851 (provision '(webssh))
852 (documentation "Run webssh daemon.")
853 (start #~(make-forkexec-constructor
854 `(,(string-append #$webssh "/bin/wssh")
855 ,(string-append "--log-file-prefix=" #$log-file)
856 ,@(case '#$log-level
857 ((debug) '("--logging=debug"))
858 (else '()))
859 ,@(case '#$policy
860 ((reject) '("--policy=reject"))
861 (else '()))
862 ,@(if #$port
863 (list (string-append "--port=" (number->string #$port)))
864 '())
865 ,@(if #$address
866 (list (string-append "--address=" #$address))
867 '()))
868 #:user #$user-name
869 #:group #$group-name))
870 (stop #~(make-kill-destructor)))))))
871
872 (define webssh-service-type
873 (service-type
874 (name 'webssh)
875 (extensions
876 (list (service-extension shepherd-root-service-type
877 webssh-shepherd-service)
878 (service-extension account-service-type
879 webssh-account)
880 (service-extension activation-service-type
881 webssh-activation)))
882 (default-value (webssh-configuration))
883 (description
884 "Run the webssh.")))
885
886 ;;; ssh.scm ends here