services: syslogd: Do not fsync at each line.
[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 ;; integer
296 (max-connections openssh-configuration-max-connections
297 (default 200))
298 ;; Boolean | 'prohibit-password
299 (permit-root-login openssh-configuration-permit-root-login
300 (default #f))
301 ;; Boolean
302 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
303 (default #f))
304 ;; Boolean
305 (password-authentication? openssh-configuration-password-authentication?
306 (default #t))
307 ;; Boolean
308 (public-key-authentication? openssh-configuration-public-key-authentication?
309 (default #t))
310 ;; Boolean
311 (x11-forwarding? openssh-configuration-x11-forwarding?
312 (default #f))
313
314 ;; Boolean
315 (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
316 (default #t))
317
318 ;; Boolean
319 (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
320 (default #t))
321
322 ;; Boolean
323 (gateway-ports? openssh-configuration-gateway-ports?
324 (default #f))
325
326 ;; Boolean
327 (challenge-response-authentication? openssh-challenge-response-authentication?
328 (default #f))
329 ;; Boolean
330 (use-pam? openssh-configuration-use-pam?
331 (default #t))
332 ;; Boolean
333 (print-last-log? openssh-configuration-print-last-log?
334 (default #t))
335 ;; list of two-element lists
336 (subsystems openssh-configuration-subsystems
337 (default '(("sftp" "internal-sftp"))))
338
339 ;; list of strings
340 (accepted-environment openssh-configuration-accepted-environment
341 (default '()))
342
343 ;; symbol
344 (log-level openssh-configuration-log-level
345 (default 'info))
346
347 ;; String
348 ;; This is an "escape hatch" to provide configuration that isn't yet
349 ;; supported by this configuration record.
350 (extra-content openssh-configuration-extra-content
351 (default ""))
352
353 ;; list of user-name/file-like tuples
354 (authorized-keys openssh-configuration-authorized-keys
355 (default '()))
356
357 ;; Boolean
358 (generate-host-keys? openssh-configuration-generate-host-keys?
359 (default #t))
360
361 ;; Boolean
362 ;; XXX: This should really be handled in an orthogonal way, for instance as
363 ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
364 ;; for now.
365 (%auto-start? openssh-auto-start?
366 (default #t)))
367
368 (define %openssh-accounts
369 (list (user-group (name "sshd") (system? #t))
370 (user-account
371 (name "sshd")
372 (group "sshd")
373 (system? #t)
374 (comment "sshd privilege separation user")
375 (home-directory "/var/run/sshd")
376 (shell (file-append shadow "/sbin/nologin")))))
377
378 (define (openssh-activation config)
379 "Return the activation GEXP for CONFIG."
380 (with-imported-modules '((guix build utils))
381 #~(begin
382 (use-modules (guix build utils))
383
384 (define (touch file-name)
385 (call-with-output-file file-name (const #t)))
386
387 ;; Make sure /etc/ssh can be read by the 'sshd' user.
388 (mkdir-p "/etc/ssh")
389 (chmod "/etc/ssh" #o755)
390 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
391
392 ;; 'sshd' complains if the authorized-key directory and its parents
393 ;; are group-writable, which rules out /gnu/store. Thus we copy the
394 ;; authorized-key directory to /etc.
395 (catch 'system-error
396 (lambda ()
397 (delete-file-recursively "/etc/ssh/authorized_keys.d"))
398 (lambda args
399 (unless (= ENOENT (system-error-errno args))
400 (apply throw args))))
401 (copy-recursively #$(authorized-key-directory
402 (openssh-configuration-authorized-keys config))
403 "/etc/ssh/authorized_keys.d")
404
405 (chmod "/etc/ssh/authorized_keys.d" #o555)
406
407 (let ((lastlog "/var/log/lastlog"))
408 (when #$(openssh-configuration-print-last-log? config)
409 (unless (file-exists? lastlog)
410 (touch lastlog))))
411
412 (when #$(openssh-configuration-generate-host-keys? config)
413 ;; Generate missing host keys.
414 (system* (string-append #$(openssh-configuration-openssh config)
415 "/bin/ssh-keygen") "-A")))))
416
417 (define (authorized-key-directory keys)
418 "Return a directory containing the authorized keys specified in KEYS, a list
419 of user-name/file-like tuples."
420 (define build
421 (with-imported-modules (source-module-closure '((guix build utils)))
422 #~(begin
423 (use-modules (ice-9 match) (srfi srfi-26)
424 (guix build utils))
425
426 (mkdir #$output)
427 (for-each (match-lambda
428 ((user keys ...)
429 (let ((file (string-append #$output "/" user)))
430 (call-with-output-file file
431 (lambda (port)
432 (for-each (lambda (key)
433 (call-with-input-file key
434 (cut dump-port <> port)))
435 keys))))))
436 '#$keys))))
437
438 (computed-file "openssh-authorized-keys" build))
439
440 (define (openssh-config-file config)
441 "Return the sshd configuration file corresponding to CONFIG."
442 (computed-file
443 "sshd_config"
444 #~(begin
445 (use-modules (ice-9 match))
446 (call-with-output-file #$output
447 (lambda (port)
448 (display "# Generated by 'openssh-service'.\n" port)
449 (format port "Port ~a\n"
450 #$(number->string
451 (openssh-configuration-port-number config)))
452 (format port "PermitRootLogin ~a\n"
453 #$(match (openssh-configuration-permit-root-login config)
454 (#t "yes")
455 (#f "no")
456 ('without-password (warn-about-deprecation
457 'without-password #f
458 #:replacement 'prohibit-password)
459 "prohibit-password")
460 ('prohibit-password "prohibit-password")))
461 (format port "PermitEmptyPasswords ~a\n"
462 #$(if (openssh-configuration-allow-empty-passwords? config)
463 "yes" "no"))
464 (format port "PasswordAuthentication ~a\n"
465 #$(if (openssh-configuration-password-authentication? config)
466 "yes" "no"))
467 (format port "PubkeyAuthentication ~a\n"
468 #$(if (openssh-configuration-public-key-authentication?
469 config)
470 "yes" "no"))
471 (format port "X11Forwarding ~a\n"
472 #$(if (openssh-configuration-x11-forwarding? config)
473 "yes" "no"))
474 (format port "AllowAgentForwarding ~a\n"
475 #$(if (openssh-configuration-allow-agent-forwarding? config)
476 "yes" "no"))
477 (format port "AllowTcpForwarding ~a\n"
478 #$(if (openssh-configuration-allow-tcp-forwarding? config)
479 "yes" "no"))
480 (format port "GatewayPorts ~a\n"
481 #$(if (openssh-configuration-gateway-ports? config)
482 "yes" "no"))
483 (format port "PidFile ~a\n"
484 #$(openssh-configuration-pid-file config))
485 (format port "ChallengeResponseAuthentication ~a\n"
486 #$(if (openssh-challenge-response-authentication? config)
487 "yes" "no"))
488 (format port "UsePAM ~a\n"
489 #$(if (openssh-configuration-use-pam? config)
490 "yes" "no"))
491 (format port "PrintLastLog ~a\n"
492 #$(if (openssh-configuration-print-last-log? config)
493 "yes" "no"))
494 (format port "LogLevel ~a\n"
495 #$(string-upcase
496 (symbol->string
497 (openssh-configuration-log-level config))))
498
499 ;; Add '/etc/authorized_keys.d/%u', which we populate.
500 (format port "AuthorizedKeysFile \
501 .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
502
503 (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
504 '#$(openssh-configuration-accepted-environment config))
505
506 (for-each
507 (match-lambda
508 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
509 '#$(openssh-configuration-subsystems config))
510
511 (format port "~a\n"
512 #$(openssh-configuration-extra-content config))
513 #t)))))
514
515 (define (openssh-shepherd-service config)
516 "Return a <shepherd-service> for openssh with CONFIG."
517
518 (define pid-file
519 (openssh-configuration-pid-file config))
520
521 (define port-number
522 (openssh-configuration-port-number config))
523
524 (define max-connections
525 (openssh-configuration-max-connections config))
526
527 (define openssh-command
528 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
529 "-D" "-f" #$(openssh-config-file config)))
530
531 (define inetd-style?
532 ;; Whether to use 'make-inetd-constructor'. That procedure appeared in
533 ;; Shepherd 0.9.0, but in 0.9.0, 'make-inetd-constructor' wouldn't let us
534 ;; pass a list of endpoints, and it wouldn't let us define a service
535 ;; listening on both IPv4 and IPv6, hence the conditional below.
536 #~(and (defined? 'make-inetd-constructor)
537 (not (string=? (@ (shepherd config) Version) "0.9.0"))))
538
539 (list (shepherd-service
540 (documentation "OpenSSH server.")
541 (requirement '(syslogd loopback))
542 (provision '(ssh-daemon ssh sshd))
543
544 (start #~(if #$inetd-style?
545 (make-inetd-constructor
546 (append #$openssh-command '("-i"))
547 (list (endpoint
548 (make-socket-address AF_INET INADDR_ANY
549 #$port-number))
550 (endpoint
551 (make-socket-address AF_INET6 IN6ADDR_ANY
552 #$port-number)))
553 #:max-connections #$max-connections)
554 (make-forkexec-constructor #$openssh-command
555 #:pid-file #$pid-file)))
556 (stop #~(if #$inetd-style?
557 (make-inetd-destructor)
558 (make-kill-destructor)))
559 (auto-start? (openssh-auto-start? config)))))
560
561 (define (openssh-pam-services config)
562 "Return a list of <pam-services> for sshd with CONFIG."
563 (list (unix-pam-service
564 "sshd"
565 #:login-uid? #t
566 #:allow-empty-passwords?
567 (openssh-configuration-allow-empty-passwords? config))))
568
569 (define (extend-openssh-authorized-keys config keys)
570 "Extend CONFIG with the extra authorized keys listed in KEYS."
571 (openssh-configuration
572 (inherit config)
573 (authorized-keys
574 (match (append (openssh-configuration-authorized-keys config) keys)
575 ((and alist ((users _ ...) ...))
576 ;; Build a user/key-list mapping.
577 (let ((user-keys (alist->vhash alist)))
578 ;; Coalesce the key lists associated with each user.
579 (map (lambda (user)
580 `(,user
581 ,@(concatenate (vhash-fold* cons '() user user-keys))))
582 users)))))))
583
584 (define openssh-service-type
585 (service-type (name 'openssh)
586 (description
587 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
588 (extensions
589 (list (service-extension shepherd-root-service-type
590 openssh-shepherd-service)
591 (service-extension pam-root-service-type
592 openssh-pam-services)
593 (service-extension activation-service-type
594 openssh-activation)
595 (service-extension account-service-type
596 (const %openssh-accounts))
597
598 ;; Install OpenSSH in the system profile. That way,
599 ;; 'scp' is found when someone tries to copy to or from
600 ;; this machine.
601 (service-extension profile-service-type
602 (lambda (config)
603 (list (openssh-configuration-openssh
604 config))))))
605 (compose concatenate)
606 (extend extend-openssh-authorized-keys)
607 (default-value (openssh-configuration))))
608
609 \f
610 ;;;
611 ;;; Dropbear.
612 ;;;
613
614 (define-record-type* <dropbear-configuration>
615 dropbear-configuration make-dropbear-configuration
616 dropbear-configuration?
617 (dropbear dropbear-configuration-dropbear
618 (default dropbear))
619 (port-number dropbear-configuration-port-number
620 (default 22))
621 (syslog-output? dropbear-configuration-syslog-output?
622 (default #t))
623 (pid-file dropbear-configuration-pid-file
624 (default "/var/run/dropbear.pid"))
625 (root-login? dropbear-configuration-root-login?
626 (default #f))
627 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
628 (default #f))
629 (password-authentication? dropbear-configuration-password-authentication?
630 (default #t)))
631
632 (define (dropbear-activation config)
633 "Return the activation gexp for CONFIG."
634 #~(begin
635 (use-modules (guix build utils))
636 (mkdir-p "/etc/dropbear")))
637
638 (define (dropbear-shepherd-service config)
639 "Return a <shepherd-service> for dropbear with CONFIG."
640 (define dropbear
641 (dropbear-configuration-dropbear config))
642
643 (define pid-file
644 (dropbear-configuration-pid-file config))
645
646 (define dropbear-command
647 #~(list (string-append #$dropbear "/sbin/dropbear")
648
649 ;; '-R' allows host keys to be automatically generated upon first
650 ;; connection, at a time when /dev/urandom is more likely securely
651 ;; seeded.
652 "-F" "-R"
653
654 "-p" #$(number->string (dropbear-configuration-port-number config))
655 "-P" #$pid-file
656 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
657 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
658 #$@(if (dropbear-configuration-password-authentication? config)
659 '()
660 '("-s" "-g"))
661 #$@(if (dropbear-configuration-allow-empty-passwords? config)
662 '("-B")
663 '())))
664
665 (define requires
666 (if (dropbear-configuration-syslog-output? config)
667 '(networking syslogd) '(networking)))
668
669 (list (shepherd-service
670 (documentation "Dropbear SSH server.")
671 (requirement requires)
672 (provision '(ssh-daemon ssh sshd))
673 (start #~(make-forkexec-constructor #$dropbear-command
674 #:pid-file #$pid-file))
675 (stop #~(make-kill-destructor)))))
676
677 (define dropbear-service-type
678 (service-type (name 'dropbear)
679 (description
680 "Run the Dropbear secure shell (SSH) server.")
681 (extensions
682 (list (service-extension shepherd-root-service-type
683 dropbear-shepherd-service)
684 (service-extension activation-service-type
685 dropbear-activation)))
686 (default-value (dropbear-configuration))))
687
688 (define* (dropbear-service #:optional (config (dropbear-configuration)))
689 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
690 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
691 object."
692 (service dropbear-service-type config))
693
694 \f
695 ;;;
696 ;;; AutoSSH.
697 ;;;
698
699
700 (define-record-type* <autossh-configuration>
701 autossh-configuration make-autossh-configuration
702 autossh-configuration?
703 (user autossh-configuration-user
704 (default "autossh"))
705 (poll autossh-configuration-poll
706 (default 600))
707 (first-poll autossh-configuration-first-poll
708 (default #f))
709 (gate-time autossh-configuration-gate-time
710 (default 30))
711 (log-level autossh-configuration-log-level
712 (default 1))
713 (max-start autossh-configuration-max-start
714 (default #f))
715 (message autossh-configuration-message
716 (default ""))
717 (port autossh-configuration-port
718 (default "0"))
719 (ssh-options autossh-configuration-ssh-options
720 (default '())))
721
722 (define (autossh-file-name config file)
723 "Return a path in /var/run/autossh/ that is writable
724 by @code{user} from @code{config}."
725 (string-append "/var/run/autossh/"
726 (autossh-configuration-user config)
727 "/" file))
728
729 (define (autossh-shepherd-service config)
730 (shepherd-service
731 (documentation "Automatically set up ssh connections (and keep them alive).")
732 (provision '(autossh))
733 (start #~(make-forkexec-constructor
734 (list #$(file-append autossh "/bin/autossh")
735 #$@(autossh-configuration-ssh-options config))
736 #:user #$(autossh-configuration-user config)
737 #:group (passwd:gid (getpw #$(autossh-configuration-user config)))
738 #:pid-file #$(autossh-file-name config "pid")
739 #:log-file #$(autossh-file-name config "log")
740 #:environment-variables
741 '(#$(string-append "AUTOSSH_PIDFILE="
742 (autossh-file-name config "pid"))
743 #$(string-append "AUTOSSH_LOGFILE="
744 (autossh-file-name config "log"))
745 #$(string-append "AUTOSSH_POLL="
746 (number->string
747 (autossh-configuration-poll config)))
748 #$(string-append "AUTOSSH_FIRST_POLL="
749 (number->string
750 (or
751 (autossh-configuration-first-poll config)
752 (autossh-configuration-poll config))))
753 #$(string-append "AUTOSSH_GATETIME="
754 (number->string
755 (autossh-configuration-gate-time config)))
756 #$(string-append "AUTOSSH_LOGLEVEL="
757 (number->string
758 (autossh-configuration-log-level config)))
759 #$(string-append "AUTOSSH_MAXSTART="
760 (number->string
761 (or (autossh-configuration-max-start config)
762 -1)))
763 #$(string-append "AUTOSSH_MESSAGE="
764 (autossh-configuration-message config))
765 #$(string-append "AUTOSSH_PORT="
766 (autossh-configuration-port config)))))
767 (stop #~(make-kill-destructor))))
768
769 (define (autossh-service-activation config)
770 (with-imported-modules '((guix build utils))
771 #~(begin
772 (use-modules (guix build utils))
773 (define %user
774 (getpw #$(autossh-configuration-user config)))
775 (let* ((directory #$(autossh-file-name config ""))
776 (log (string-append directory "/log")))
777 (mkdir-p directory)
778 (chown directory (passwd:uid %user) (passwd:gid %user))
779 (call-with-output-file log (const #t))
780 (chown log (passwd:uid %user) (passwd:gid %user))))))
781
782 (define autossh-service-type
783 (service-type
784 (name 'autossh)
785 (description "Automatically set up ssh connections (and keep them alive).")
786 (extensions
787 (list (service-extension shepherd-root-service-type
788 (compose list autossh-shepherd-service))
789 (service-extension activation-service-type
790 autossh-service-activation)))
791 (default-value (autossh-configuration))))
792
793 \f
794 ;;;
795 ;;; WebSSH
796 ;;;
797
798 (define-record-type* <webssh-configuration>
799 webssh-configuration make-webssh-configuration
800 webssh-configuration?
801 (package webssh-configuration-package ;file-like
802 (default webssh))
803 (user-name webssh-configuration-user-name ;string
804 (default "webssh"))
805 (group-name webssh-configuration-group-name ;string
806 (default "webssh"))
807 (policy webssh-configuration-policy ;symbol
808 (default #f))
809 (known-hosts webssh-configuration-known-hosts ;list of strings
810 (default #f))
811 (port webssh-configuration-port ;number
812 (default #f))
813 (address webssh-configuration-address ;string
814 (default #f))
815 (log-file webssh-configuration-log-file ;string
816 (default "/var/log/webssh.log"))
817 (log-level webssh-configuration-log-level ;symbol
818 (default #f)))
819
820 (define %webssh-configuration-nginx
821 (nginx-server-configuration
822 (listen '("80"))
823 (locations
824 (list (nginx-location-configuration
825 (uri "/")
826 (body '("proxy_pass http://127.0.0.1:8888;"
827 "proxy_http_version 1.1;"
828 "proxy_read_timeout 300;"
829 "proxy_set_header Upgrade $http_upgrade;"
830 "proxy_set_header Connection \"upgrade\";"
831 "proxy_set_header Host $http_host;"
832 "proxy_set_header X-Real-IP $remote_addr;"
833 "proxy_set_header X-Real-PORT $remote_port;")))))))
834
835 (define webssh-account
836 ;; Return the user accounts and user groups for CONFIG.
837 (match-lambda
838 (($ <webssh-configuration> _ user-name group-name _ _ _ _ _ _)
839 (list (user-group
840 (name group-name))
841 (user-account
842 (name user-name)
843 (group group-name)
844 (comment "webssh privilege separation user")
845 (home-directory (string-append "/var/run/" user-name))
846 (shell #~(string-append #$shadow "/sbin/nologin")))))))
847
848 (define webssh-activation
849 ;; Return the activation GEXP for CONFIG.
850 (match-lambda
851 (($ <webssh-configuration> _ user-name group-name policy known-hosts _ _
852 log-file _)
853 (with-imported-modules '((guix build utils))
854 #~(begin
855 (let* ((home-dir (string-append "/var/run/" #$user-name))
856 (ssh-dir (string-append home-dir "/.ssh"))
857 (known-hosts-file (string-append ssh-dir "/known_hosts")))
858 (call-with-output-file #$log-file (const #t))
859 (mkdir-p ssh-dir)
860 (case '#$policy
861 ((reject)
862 (if '#$known-hosts
863 (call-with-output-file known-hosts-file
864 (lambda (port)
865 (for-each (lambda (host) (display host port) (newline port))
866 '#$known-hosts)))
867 (display-hint (G_ "webssh: reject policy requires `known-hosts'.")))))
868 (for-each (lambda (file)
869 (chown file
870 (passwd:uid (getpw #$user-name))
871 (group:gid (getpw #$group-name))))
872 (list #$log-file ssh-dir known-hosts-file))
873 (chmod ssh-dir #o700)))))))
874
875 (define webssh-shepherd-service
876 (match-lambda
877 (($ <webssh-configuration> package user-name group-name policy _ port
878 address log-file log-level)
879 (list (shepherd-service
880 (provision '(webssh))
881 (documentation "Run webssh daemon.")
882 (start #~(make-forkexec-constructor
883 `(,(string-append #$webssh "/bin/wssh")
884 ,(string-append "--log-file-prefix=" #$log-file)
885 ,@(case '#$log-level
886 ((debug) '("--logging=debug"))
887 (else '()))
888 ,@(case '#$policy
889 ((reject) '("--policy=reject"))
890 (else '()))
891 ,@(if #$port
892 (list (string-append "--port=" (number->string #$port)))
893 '())
894 ,@(if #$address
895 (list (string-append "--address=" #$address))
896 '()))
897 #:user #$user-name
898 #:group #$group-name))
899 (stop #~(make-kill-destructor)))))))
900
901 (define webssh-service-type
902 (service-type
903 (name 'webssh)
904 (extensions
905 (list (service-extension shepherd-root-service-type
906 webssh-shepherd-service)
907 (service-extension account-service-type
908 webssh-account)
909 (service-extension activation-service-type
910 webssh-activation)))
911 (default-value (webssh-configuration))
912 (description
913 "Run the webssh.")))
914
915 ;;; ssh.scm ends here