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>
12 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
49 openssh-configuration?
52 dropbear-configuration
53 dropbear-configuration?
58 autossh-configuration?
64 %webssh-configuration-nginx))
68 ;;; This module implements secure shell (SSH) services.
72 (define-record-type* <lsh-configuration>
73 lsh-configuration make-lsh-configuration
75 (lsh lsh-configuration-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?))
93 "/var/spool/lsh/yarrow-seed-file")
95 (define (lsh-initialization lsh host-key)
96 "Return the gexp to initialize the LSH service for HOST-KEY."
98 (unless (file-exists? #$%yarrow-seed)
99 (system* (string-append #$lsh "/bin/lsh-make-seed")
100 "--sloppy" "-o" #$%yarrow-seed))
102 (unless (file-exists? #$host-key)
103 (mkdir-p (dirname #$host-key))
104 (format #t "creating SSH host key '~a'...~%" #$host-key)
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)))
112 (close-port (car in+out))
114 (dup2 (fileno (cdr in+out)) 1)
115 (execl (string-append #$lsh "/bin/lsh-keygen")
116 "lsh-keygen" "--server"))
118 (let ((write-key (primitive-fork)))
121 (close-port (cdr in+out))
123 (dup2 (fileno (car in+out)) 0)
124 (execl (string-append #$lsh "/bin/lsh-writekey")
125 "lsh-writekey" "--server" "-o" #$host-key))
127 (close-port (car in+out))
128 (close-port (cdr in+out))
130 (waitpid write-key))))))))))
132 (define (lsh-activation config)
133 "Return the activation gexp for CONFIG."
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))
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))
152 (cons (file-append lsh "/sbin/lshd")
154 (let ((syslog (if (lsh-configuration-syslog-output? config)
156 (list "--no-syslog"))))
159 (cons #~(string-append "--pid-file=" #$pid-file)
161 (cons "--no-pid-file" syslog))))
163 (list #~(string-append "--pid-file=" #$pid-file))
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)
182 (map (cut string-append "--interface=" <>)
186 (if (and daemonic? (lsh-configuration-syslog-output? config))
187 '(networking syslogd)
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)))))
197 (define (lsh-pam-services config)
198 "Return a list of <pam-services> for lshd with CONFIG."
199 (list (unix-pam-service
202 #:allow-empty-passwords?
203 (lsh-configuration-allow-empty-passwords? config))))
205 (define lsh-service-type
206 (service-type (name 'lsh)
208 "Run the GNU@tie{}lsh secure shell (SSH) daemon,
211 (list (service-extension shepherd-root-service-type
212 lsh-shepherd-service)
213 (service-extension pam-root-service-type
215 (service-extension activation-service-type
218 (define* (lsh-service #:key
221 (host-key "/etc/lsh/host-key")
224 (allow-empty-passwords? #f)
228 (pid-file "/var/run/lshd.pid")
230 (tcp/ip-forwarding? #t)
231 (password-authentication? #t)
232 (public-key-authentication? #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
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}.
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
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}).
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
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
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?))))
283 (define-record-type* <openssh-configuration>
284 openssh-configuration make-openssh-configuration
285 openssh-configuration?
287 (openssh openssh-configuration-openssh
290 (pid-file openssh-configuration-pid-file
291 (default "/var/run/sshd.pid"))
293 (port-number openssh-configuration-port-number
296 (max-connections openssh-configuration-max-connections
298 ;; Boolean | 'prohibit-password
299 (permit-root-login openssh-configuration-permit-root-login
302 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
305 (password-authentication? openssh-configuration-password-authentication?
308 (public-key-authentication? openssh-configuration-public-key-authentication?
311 (x11-forwarding? openssh-configuration-x11-forwarding?
315 (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
319 (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
323 (gateway-ports? openssh-configuration-gateway-ports?
327 (challenge-response-authentication? openssh-challenge-response-authentication?
330 (use-pam? openssh-configuration-use-pam?
333 (print-last-log? openssh-configuration-print-last-log?
335 ;; list of two-element lists
336 (subsystems openssh-configuration-subsystems
337 (default '(("sftp" "internal-sftp"))))
340 (accepted-environment openssh-configuration-accepted-environment
344 (log-level openssh-configuration-log-level
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
353 ;; list of user-name/file-like tuples
354 (authorized-keys openssh-configuration-authorized-keys
358 (generate-host-keys? openssh-configuration-generate-host-keys?
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
365 (%auto-start? openssh-auto-start?
368 (define %openssh-accounts
369 (list (user-group (name "sshd") (system? #t))
374 (comment "sshd privilege separation user")
375 (home-directory "/var/run/sshd")
376 (shell (file-append shadow "/sbin/nologin")))))
378 (define (openssh-activation config)
379 "Return the activation GEXP for CONFIG."
380 (with-imported-modules '((guix build utils))
382 (use-modules (guix build utils))
384 (define (touch file-name)
385 (call-with-output-file file-name (const #t)))
387 ;; Make sure /etc/ssh can be read by the 'sshd' user.
389 (chmod "/etc/ssh" #o755)
390 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
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.
397 (delete-file-recursively "/etc/ssh/authorized_keys.d"))
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")
405 (chmod "/etc/ssh/authorized_keys.d" #o555)
407 (let ((lastlog "/var/log/lastlog"))
408 (when #$(openssh-configuration-print-last-log? config)
409 (unless (file-exists? lastlog)
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")))))
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."
421 (with-imported-modules (source-module-closure '((guix build utils)))
423 (use-modules (ice-9 match) (srfi srfi-26)
427 (for-each (match-lambda
429 (let ((file (string-append #$output "/" user)))
430 (call-with-output-file file
432 (for-each (lambda (key)
433 (call-with-input-file key
434 (cut dump-port <> port)))
438 (computed-file "openssh-authorized-keys" build))
440 (define (openssh-config-file config)
441 "Return the sshd configuration file corresponding to CONFIG."
445 (use-modules (ice-9 match))
446 (call-with-output-file #$output
448 (display "# Generated by 'openssh-service'.\n" port)
449 (format port "Port ~a\n"
451 (openssh-configuration-port-number config)))
452 (format port "PermitRootLogin ~a\n"
453 #$(match (openssh-configuration-permit-root-login config)
456 ('without-password (warn-about-deprecation
458 #:replacement 'prohibit-password)
460 ('prohibit-password "prohibit-password")))
461 (format port "PermitEmptyPasswords ~a\n"
462 #$(if (openssh-configuration-allow-empty-passwords? config)
464 (format port "PasswordAuthentication ~a\n"
465 #$(if (openssh-configuration-password-authentication? config)
467 (format port "PubkeyAuthentication ~a\n"
468 #$(if (openssh-configuration-public-key-authentication?
471 (format port "X11Forwarding ~a\n"
472 #$(if (openssh-configuration-x11-forwarding? config)
474 (format port "AllowAgentForwarding ~a\n"
475 #$(if (openssh-configuration-allow-agent-forwarding? config)
477 (format port "AllowTcpForwarding ~a\n"
478 #$(if (openssh-configuration-allow-tcp-forwarding? config)
480 (format port "GatewayPorts ~a\n"
481 #$(if (openssh-configuration-gateway-ports? config)
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)
488 (format port "UsePAM ~a\n"
489 #$(if (openssh-configuration-use-pam? config)
491 (format port "PrintLastLog ~a\n"
492 #$(if (openssh-configuration-print-last-log? config)
494 (format port "LogLevel ~a\n"
497 (openssh-configuration-log-level config))))
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")
503 (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
504 '#$(openssh-configuration-accepted-environment config))
508 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
509 '#$(openssh-configuration-subsystems config))
512 #$(openssh-configuration-extra-content config))
515 (define (openssh-shepherd-service config)
516 "Return a <shepherd-service> for openssh with CONFIG."
519 (openssh-configuration-pid-file config))
522 (openssh-configuration-port-number config))
524 (define max-connections
525 (openssh-configuration-max-connections config))
527 (define openssh-command
528 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
529 "-D" "-f" #$(openssh-config-file config)))
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"))))
539 (define ipv6-support?
540 ;; Expression that returns true if IPv6 support is available.
541 #~(catch 'system-error
543 (let ((sock (socket AF_INET6 SOCK_STREAM 0)))
548 (list (shepherd-service
549 (documentation "OpenSSH server.")
550 (requirement '(syslogd loopback))
551 (provision '(ssh-daemon ssh sshd))
553 (start #~(if #$inetd-style?
554 (make-inetd-constructor
555 (append #$openssh-command '("-i"))
557 (make-socket-address AF_INET INADDR_ANY
562 (make-socket-address AF_INET6 IN6ADDR_ANY
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)))))
573 (define (openssh-pam-services config)
574 "Return a list of <pam-services> for sshd with CONFIG."
575 (list (unix-pam-service
578 #:allow-empty-passwords?
579 (openssh-configuration-allow-empty-passwords? config))))
581 (define (extend-openssh-authorized-keys config keys)
582 "Extend CONFIG with the extra authorized keys listed in KEYS."
583 (openssh-configuration
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.
593 ,@(concatenate (vhash-fold* cons '() user user-keys))))
596 (define openssh-service-type
597 (service-type (name 'openssh)
599 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
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
607 (service-extension account-service-type
608 (const %openssh-accounts))
610 ;; Install OpenSSH in the system profile. That way,
611 ;; 'scp' is found when someone tries to copy to or from
613 (service-extension profile-service-type
615 (list (openssh-configuration-openssh
617 (compose concatenate)
618 (extend extend-openssh-authorized-keys)
619 (default-value (openssh-configuration))))
626 (define-record-type* <dropbear-configuration>
627 dropbear-configuration make-dropbear-configuration
628 dropbear-configuration?
629 (dropbear dropbear-configuration-dropbear
631 (port-number dropbear-configuration-port-number
633 (syslog-output? dropbear-configuration-syslog-output?
635 (pid-file dropbear-configuration-pid-file
636 (default "/var/run/dropbear.pid"))
637 (root-login? dropbear-configuration-root-login?
639 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
641 (password-authentication? dropbear-configuration-password-authentication?
644 (define (dropbear-activation config)
645 "Return the activation gexp for CONFIG."
647 (use-modules (guix build utils))
648 (mkdir-p "/etc/dropbear")))
650 (define (dropbear-shepherd-service config)
651 "Return a <shepherd-service> for dropbear with CONFIG."
653 (dropbear-configuration-dropbear config))
656 (dropbear-configuration-pid-file config))
658 (define dropbear-command
659 #~(list (string-append #$dropbear "/sbin/dropbear")
661 ;; '-R' allows host keys to be automatically generated upon first
662 ;; connection, at a time when /dev/urandom is more likely securely
666 "-p" #$(number->string (dropbear-configuration-port-number config))
668 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
669 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
670 #$@(if (dropbear-configuration-password-authentication? config)
673 #$@(if (dropbear-configuration-allow-empty-passwords? config)
678 (if (dropbear-configuration-syslog-output? config)
679 '(networking syslogd) '(networking)))
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)))))
689 (define dropbear-service-type
690 (service-type (name 'dropbear)
692 "Run the Dropbear secure shell (SSH) server.")
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))))
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>}
704 (service dropbear-service-type config))
712 (define-record-type* <autossh-configuration>
713 autossh-configuration make-autossh-configuration
714 autossh-configuration?
715 (user autossh-configuration-user
717 (poll autossh-configuration-poll
719 (first-poll autossh-configuration-first-poll
721 (gate-time autossh-configuration-gate-time
723 (log-level autossh-configuration-log-level
725 (max-start autossh-configuration-max-start
727 (message autossh-configuration-message
729 (port autossh-configuration-port
731 (ssh-options autossh-configuration-ssh-options
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)
741 (define (autossh-shepherd-service config)
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="
759 (autossh-configuration-poll config)))
760 #$(string-append "AUTOSSH_FIRST_POLL="
763 (autossh-configuration-first-poll config)
764 (autossh-configuration-poll config))))
765 #$(string-append "AUTOSSH_GATETIME="
767 (autossh-configuration-gate-time config)))
768 #$(string-append "AUTOSSH_LOGLEVEL="
770 (autossh-configuration-log-level config)))
771 #$(string-append "AUTOSSH_MAXSTART="
773 (or (autossh-configuration-max-start config)
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))))
781 (define (autossh-service-activation config)
782 (with-imported-modules '((guix build utils))
784 (use-modules (guix build utils))
786 (getpw #$(autossh-configuration-user config)))
787 (let* ((directory #$(autossh-file-name config ""))
788 (log (string-append directory "/log")))
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))))))
794 (define autossh-service-type
797 (description "Automatically set up ssh connections (and keep them alive).")
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))))
810 (define-record-type* <webssh-configuration>
811 webssh-configuration make-webssh-configuration
812 webssh-configuration?
813 (package webssh-configuration-package ;file-like
815 (user-name webssh-configuration-user-name ;string
817 (group-name webssh-configuration-group-name ;string
819 (policy webssh-configuration-policy ;symbol
821 (known-hosts webssh-configuration-known-hosts ;list of strings
823 (port webssh-configuration-port ;number
825 (address webssh-configuration-address ;string
827 (log-file webssh-configuration-log-file ;string
828 (default "/var/log/webssh.log"))
829 (log-level webssh-configuration-log-level ;symbol
832 (define %webssh-configuration-nginx
833 (nginx-server-configuration
836 (list (nginx-location-configuration
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;")))))))
847 (define webssh-account
848 ;; Return the user accounts and user groups for CONFIG.
850 (($ <webssh-configuration> _ user-name group-name _ _ _ _ _ _)
856 (comment "webssh privilege separation user")
857 (home-directory (string-append "/var/run/" user-name))
858 (shell #~(string-append #$shadow "/sbin/nologin")))))))
860 (define webssh-activation
861 ;; Return the activation GEXP for CONFIG.
863 (($ <webssh-configuration> _ user-name group-name policy known-hosts _ _
865 (with-imported-modules '((guix build utils))
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))
875 (call-with-output-file known-hosts-file
877 (for-each (lambda (host) (display host port) (newline port))
879 (display-hint (G_ "webssh: reject policy requires `known-hosts'.")))))
880 (for-each (lambda (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)))))))
887 (define webssh-shepherd-service
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)
898 ((debug) '("--logging=debug"))
901 ((reject) '("--policy=reject"))
904 (list (string-append "--port=" (number->string #$port)))
907 (list (string-append "--address=" #$address))
910 #:group #$group-name))
911 (stop #~(make-kill-destructor)))))))
913 (define webssh-service-type
917 (list (service-extension shepherd-root-service-type
918 webssh-shepherd-service)
919 (service-extension account-service-type
921 (service-extension activation-service-type
923 (default-value (webssh-configuration))
927 ;;; ssh.scm ends here