gnu: emacs-ebib: Update to 2.39.3.
[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 (define ipv6-support?
540 ;; Expression that returns true if IPv6 support is available.
541 #~(catch 'system-error
542 (lambda ()
543 (let ((sock (socket AF_INET6 SOCK_STREAM 0)))
544 (close-port sock)
545 #t))
546 (const #f)))
547
548 (list (shepherd-service
549 (documentation "OpenSSH server.")
550 (requirement '(syslogd loopback))
551 (provision '(ssh-daemon ssh sshd))
552
553 (start #~(if #$inetd-style?
554 (make-inetd-constructor
555 (append #$openssh-command '("-i"))
556 (cons (endpoint
557 (make-socket-address AF_INET INADDR_ANY
558 #$port-number))
559 (if #$ipv6-support?
560 (list
561 (endpoint
562 (make-socket-address AF_INET6 IN6ADDR_ANY
563 #$port-number)))
564 '()))
565 #:max-connections #$max-connections)
566 (make-forkexec-constructor #$openssh-command
567 #:pid-file #$pid-file)))
568 (stop #~(if #$inetd-style?
569 (make-inetd-destructor)
570 (make-kill-destructor)))
571 (auto-start? (openssh-auto-start? config)))))
572
573 (define (openssh-pam-services config)
574 "Return a list of <pam-services> for sshd with CONFIG."
575 (list (unix-pam-service
576 "sshd"
577 #:login-uid? #t
578 #:allow-empty-passwords?
579 (openssh-configuration-allow-empty-passwords? config))))
580
581 (define (extend-openssh-authorized-keys config keys)
582 "Extend CONFIG with the extra authorized keys listed in KEYS."
583 (openssh-configuration
584 (inherit config)
585 (authorized-keys
586 (match (append (openssh-configuration-authorized-keys config) keys)
587 ((and alist ((users _ ...) ...))
588 ;; Build a user/key-list mapping.
589 (let ((user-keys (alist->vhash alist)))
590 ;; Coalesce the key lists associated with each user.
591 (map (lambda (user)
592 `(,user
593 ,@(concatenate (vhash-fold* cons '() user user-keys))))
594 users)))))))
595
596 (define openssh-service-type
597 (service-type (name 'openssh)
598 (description
599 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
600 (extensions
601 (list (service-extension shepherd-root-service-type
602 openssh-shepherd-service)
603 (service-extension pam-root-service-type
604 openssh-pam-services)
605 (service-extension activation-service-type
606 openssh-activation)
607 (service-extension account-service-type
608 (const %openssh-accounts))
609
610 ;; Install OpenSSH in the system profile. That way,
611 ;; 'scp' is found when someone tries to copy to or from
612 ;; this machine.
613 (service-extension profile-service-type
614 (lambda (config)
615 (list (openssh-configuration-openssh
616 config))))))
617 (compose concatenate)
618 (extend extend-openssh-authorized-keys)
619 (default-value (openssh-configuration))))
620
621 \f
622 ;;;
623 ;;; Dropbear.
624 ;;;
625
626 (define-record-type* <dropbear-configuration>
627 dropbear-configuration make-dropbear-configuration
628 dropbear-configuration?
629 (dropbear dropbear-configuration-dropbear
630 (default dropbear))
631 (port-number dropbear-configuration-port-number
632 (default 22))
633 (syslog-output? dropbear-configuration-syslog-output?
634 (default #t))
635 (pid-file dropbear-configuration-pid-file
636 (default "/var/run/dropbear.pid"))
637 (root-login? dropbear-configuration-root-login?
638 (default #f))
639 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
640 (default #f))
641 (password-authentication? dropbear-configuration-password-authentication?
642 (default #t)))
643
644 (define (dropbear-activation config)
645 "Return the activation gexp for CONFIG."
646 #~(begin
647 (use-modules (guix build utils))
648 (mkdir-p "/etc/dropbear")))
649
650 (define (dropbear-shepherd-service config)
651 "Return a <shepherd-service> for dropbear with CONFIG."
652 (define dropbear
653 (dropbear-configuration-dropbear config))
654
655 (define pid-file
656 (dropbear-configuration-pid-file config))
657
658 (define dropbear-command
659 #~(list (string-append #$dropbear "/sbin/dropbear")
660
661 ;; '-R' allows host keys to be automatically generated upon first
662 ;; connection, at a time when /dev/urandom is more likely securely
663 ;; seeded.
664 "-F" "-R"
665
666 "-p" #$(number->string (dropbear-configuration-port-number config))
667 "-P" #$pid-file
668 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
669 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
670 #$@(if (dropbear-configuration-password-authentication? config)
671 '()
672 '("-s" "-g"))
673 #$@(if (dropbear-configuration-allow-empty-passwords? config)
674 '("-B")
675 '())))
676
677 (define requires
678 (if (dropbear-configuration-syslog-output? config)
679 '(networking syslogd) '(networking)))
680
681 (list (shepherd-service
682 (documentation "Dropbear SSH server.")
683 (requirement requires)
684 (provision '(ssh-daemon ssh sshd))
685 (start #~(make-forkexec-constructor #$dropbear-command
686 #:pid-file #$pid-file))
687 (stop #~(make-kill-destructor)))))
688
689 (define dropbear-service-type
690 (service-type (name 'dropbear)
691 (description
692 "Run the Dropbear secure shell (SSH) server.")
693 (extensions
694 (list (service-extension shepherd-root-service-type
695 dropbear-shepherd-service)
696 (service-extension activation-service-type
697 dropbear-activation)))
698 (default-value (dropbear-configuration))))
699
700 (define* (dropbear-service #:optional (config (dropbear-configuration)))
701 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
702 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
703 object."
704 (service dropbear-service-type config))
705
706 \f
707 ;;;
708 ;;; AutoSSH.
709 ;;;
710
711
712 (define-record-type* <autossh-configuration>
713 autossh-configuration make-autossh-configuration
714 autossh-configuration?
715 (user autossh-configuration-user
716 (default "autossh"))
717 (poll autossh-configuration-poll
718 (default 600))
719 (first-poll autossh-configuration-first-poll
720 (default #f))
721 (gate-time autossh-configuration-gate-time
722 (default 30))
723 (log-level autossh-configuration-log-level
724 (default 1))
725 (max-start autossh-configuration-max-start
726 (default #f))
727 (message autossh-configuration-message
728 (default ""))
729 (port autossh-configuration-port
730 (default "0"))
731 (ssh-options autossh-configuration-ssh-options
732 (default '())))
733
734 (define (autossh-file-name config file)
735 "Return a path in /var/run/autossh/ that is writable
736 by @code{user} from @code{config}."
737 (string-append "/var/run/autossh/"
738 (autossh-configuration-user config)
739 "/" file))
740
741 (define (autossh-shepherd-service config)
742 (shepherd-service
743 (documentation "Automatically set up ssh connections (and keep them alive).")
744 (provision '(autossh))
745 (start #~(make-forkexec-constructor
746 (list #$(file-append autossh "/bin/autossh")
747 #$@(autossh-configuration-ssh-options config))
748 #:user #$(autossh-configuration-user config)
749 #:group (passwd:gid (getpw #$(autossh-configuration-user config)))
750 #:pid-file #$(autossh-file-name config "pid")
751 #:log-file #$(autossh-file-name config "log")
752 #:environment-variables
753 '(#$(string-append "AUTOSSH_PIDFILE="
754 (autossh-file-name config "pid"))
755 #$(string-append "AUTOSSH_LOGFILE="
756 (autossh-file-name config "log"))
757 #$(string-append "AUTOSSH_POLL="
758 (number->string
759 (autossh-configuration-poll config)))
760 #$(string-append "AUTOSSH_FIRST_POLL="
761 (number->string
762 (or
763 (autossh-configuration-first-poll config)
764 (autossh-configuration-poll config))))
765 #$(string-append "AUTOSSH_GATETIME="
766 (number->string
767 (autossh-configuration-gate-time config)))
768 #$(string-append "AUTOSSH_LOGLEVEL="
769 (number->string
770 (autossh-configuration-log-level config)))
771 #$(string-append "AUTOSSH_MAXSTART="
772 (number->string
773 (or (autossh-configuration-max-start config)
774 -1)))
775 #$(string-append "AUTOSSH_MESSAGE="
776 (autossh-configuration-message config))
777 #$(string-append "AUTOSSH_PORT="
778 (autossh-configuration-port config)))))
779 (stop #~(make-kill-destructor))))
780
781 (define (autossh-service-activation config)
782 (with-imported-modules '((guix build utils))
783 #~(begin
784 (use-modules (guix build utils))
785 (define %user
786 (getpw #$(autossh-configuration-user config)))
787 (let* ((directory #$(autossh-file-name config ""))
788 (log (string-append directory "/log")))
789 (mkdir-p directory)
790 (chown directory (passwd:uid %user) (passwd:gid %user))
791 (call-with-output-file log (const #t))
792 (chown log (passwd:uid %user) (passwd:gid %user))))))
793
794 (define autossh-service-type
795 (service-type
796 (name 'autossh)
797 (description "Automatically set up ssh connections (and keep them alive).")
798 (extensions
799 (list (service-extension shepherd-root-service-type
800 (compose list autossh-shepherd-service))
801 (service-extension activation-service-type
802 autossh-service-activation)))
803 (default-value (autossh-configuration))))
804
805 \f
806 ;;;
807 ;;; WebSSH
808 ;;;
809
810 (define-record-type* <webssh-configuration>
811 webssh-configuration make-webssh-configuration
812 webssh-configuration?
813 (package webssh-configuration-package ;file-like
814 (default webssh))
815 (user-name webssh-configuration-user-name ;string
816 (default "webssh"))
817 (group-name webssh-configuration-group-name ;string
818 (default "webssh"))
819 (policy webssh-configuration-policy ;symbol
820 (default #f))
821 (known-hosts webssh-configuration-known-hosts ;list of strings
822 (default #f))
823 (port webssh-configuration-port ;number
824 (default #f))
825 (address webssh-configuration-address ;string
826 (default #f))
827 (log-file webssh-configuration-log-file ;string
828 (default "/var/log/webssh.log"))
829 (log-level webssh-configuration-log-level ;symbol
830 (default #f)))
831
832 (define %webssh-configuration-nginx
833 (nginx-server-configuration
834 (listen '("80"))
835 (locations
836 (list (nginx-location-configuration
837 (uri "/")
838 (body '("proxy_pass http://127.0.0.1:8888;"
839 "proxy_http_version 1.1;"
840 "proxy_read_timeout 300;"
841 "proxy_set_header Upgrade $http_upgrade;"
842 "proxy_set_header Connection \"upgrade\";"
843 "proxy_set_header Host $http_host;"
844 "proxy_set_header X-Real-IP $remote_addr;"
845 "proxy_set_header X-Real-PORT $remote_port;")))))))
846
847 (define webssh-account
848 ;; Return the user accounts and user groups for CONFIG.
849 (match-lambda
850 (($ <webssh-configuration> _ user-name group-name _ _ _ _ _ _)
851 (list (user-group
852 (name group-name))
853 (user-account
854 (name user-name)
855 (group group-name)
856 (comment "webssh privilege separation user")
857 (home-directory (string-append "/var/run/" user-name))
858 (shell #~(string-append #$shadow "/sbin/nologin")))))))
859
860 (define webssh-activation
861 ;; Return the activation GEXP for CONFIG.
862 (match-lambda
863 (($ <webssh-configuration> _ user-name group-name policy known-hosts _ _
864 log-file _)
865 (with-imported-modules '((guix build utils))
866 #~(begin
867 (let* ((home-dir (string-append "/var/run/" #$user-name))
868 (ssh-dir (string-append home-dir "/.ssh"))
869 (known-hosts-file (string-append ssh-dir "/known_hosts")))
870 (call-with-output-file #$log-file (const #t))
871 (mkdir-p ssh-dir)
872 (case '#$policy
873 ((reject)
874 (if '#$known-hosts
875 (call-with-output-file known-hosts-file
876 (lambda (port)
877 (for-each (lambda (host) (display host port) (newline port))
878 '#$known-hosts)))
879 (display-hint (G_ "webssh: reject policy requires `known-hosts'.")))))
880 (for-each (lambda (file)
881 (chown file
882 (passwd:uid (getpw #$user-name))
883 (group:gid (getpw #$group-name))))
884 (list #$log-file ssh-dir known-hosts-file))
885 (chmod ssh-dir #o700)))))))
886
887 (define webssh-shepherd-service
888 (match-lambda
889 (($ <webssh-configuration> package user-name group-name policy _ port
890 address log-file log-level)
891 (list (shepherd-service
892 (provision '(webssh))
893 (documentation "Run webssh daemon.")
894 (start #~(make-forkexec-constructor
895 `(,(string-append #$webssh "/bin/wssh")
896 ,(string-append "--log-file-prefix=" #$log-file)
897 ,@(case '#$log-level
898 ((debug) '("--logging=debug"))
899 (else '()))
900 ,@(case '#$policy
901 ((reject) '("--policy=reject"))
902 (else '()))
903 ,@(if #$port
904 (list (string-append "--port=" (number->string #$port)))
905 '())
906 ,@(if #$address
907 (list (string-append "--address=" #$address))
908 '()))
909 #:user #$user-name
910 #:group #$group-name))
911 (stop #~(make-kill-destructor)))))))
912
913 (define webssh-service-type
914 (service-type
915 (name 'webssh)
916 (extensions
917 (list (service-extension shepherd-root-service-type
918 webssh-shepherd-service)
919 (service-extension account-service-type
920 webssh-account)
921 (service-extension activation-service-type
922 webssh-activation)))
923 (default-value (webssh-configuration))
924 (description
925 "Run the webssh.")))
926
927 ;;; ssh.scm ends here