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
295 ;; Boolean | 'prohibit-password
296 (permit-root-login openssh-configuration-permit-root-login
299 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
302 (password-authentication? openssh-configuration-password-authentication?
305 (public-key-authentication? openssh-configuration-public-key-authentication?
308 (x11-forwarding? openssh-configuration-x11-forwarding?
312 (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
316 (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
320 (gateway-ports? openssh-configuration-gateway-ports?
324 (challenge-response-authentication? openssh-challenge-response-authentication?
327 (use-pam? openssh-configuration-use-pam?
330 (print-last-log? openssh-configuration-print-last-log?
332 ;; list of two-element lists
333 (subsystems openssh-configuration-subsystems
334 (default '(("sftp" "internal-sftp"))))
337 (accepted-environment openssh-configuration-accepted-environment
341 (log-level openssh-configuration-log-level
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
350 ;; list of user-name/file-like tuples
351 (authorized-keys openssh-configuration-authorized-keys
355 (generate-host-keys? openssh-configuration-generate-host-keys?
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
362 (%auto-start? openssh-auto-start?
365 (define %openssh-accounts
366 (list (user-group (name "sshd") (system? #t))
371 (comment "sshd privilege separation user")
372 (home-directory "/var/run/sshd")
373 (shell (file-append shadow "/sbin/nologin")))))
375 (define (openssh-activation config)
376 "Return the activation GEXP for CONFIG."
377 (with-imported-modules '((guix build utils))
379 (use-modules (guix build utils))
381 (define (touch file-name)
382 (call-with-output-file file-name (const #t)))
384 ;; Make sure /etc/ssh can be read by the 'sshd' user.
386 (chmod "/etc/ssh" #o755)
387 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
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.
394 (delete-file-recursively "/etc/authorized_keys.d"))
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")
402 (chmod "/etc/ssh/authorized_keys.d" #o555)
404 (let ((lastlog "/var/log/lastlog"))
405 (when #$(openssh-configuration-print-last-log? config)
406 (unless (file-exists? lastlog)
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")))))
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."
418 (with-imported-modules (source-module-closure '((guix build utils)))
420 (use-modules (ice-9 match) (srfi srfi-26)
424 (for-each (match-lambda
426 (let ((file (string-append #$output "/" user)))
427 (call-with-output-file file
429 (for-each (lambda (key)
430 (call-with-input-file key
431 (cut dump-port <> port)))
435 (computed-file "openssh-authorized-keys" build))
437 (define (openssh-config-file config)
438 "Return the sshd configuration file corresponding to CONFIG."
442 (use-modules (ice-9 match))
443 (call-with-output-file #$output
445 (display "# Generated by 'openssh-service'.\n" port)
446 (format port "Port ~a\n"
448 (openssh-configuration-port-number config)))
449 (format port "PermitRootLogin ~a\n"
450 #$(match (openssh-configuration-permit-root-login config)
453 ('without-password (warn-about-deprecation
455 #:replacement 'prohibit-password)
457 ('prohibit-password "prohibit-password")))
458 (format port "PermitEmptyPasswords ~a\n"
459 #$(if (openssh-configuration-allow-empty-passwords? config)
461 (format port "PasswordAuthentication ~a\n"
462 #$(if (openssh-configuration-password-authentication? config)
464 (format port "PubkeyAuthentication ~a\n"
465 #$(if (openssh-configuration-public-key-authentication?
468 (format port "X11Forwarding ~a\n"
469 #$(if (openssh-configuration-x11-forwarding? config)
471 (format port "AllowAgentForwarding ~a\n"
472 #$(if (openssh-configuration-allow-agent-forwarding? config)
474 (format port "AllowTcpForwarding ~a\n"
475 #$(if (openssh-configuration-allow-tcp-forwarding? config)
477 (format port "GatewayPorts ~a\n"
478 #$(if (openssh-configuration-gateway-ports? config)
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)
485 (format port "UsePAM ~a\n"
486 #$(if (openssh-configuration-use-pam? config)
488 (format port "PrintLastLog ~a\n"
489 #$(if (openssh-configuration-print-last-log? config)
491 (format port "LogLevel ~a\n"
494 (openssh-configuration-log-level config))))
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")
500 (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
501 '#$(openssh-configuration-accepted-environment config))
505 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
506 '#$(openssh-configuration-subsystems config))
509 #$(openssh-configuration-extra-content config))
512 (define (openssh-shepherd-service config)
513 "Return a <shepherd-service> for openssh with CONFIG."
516 (openssh-configuration-pid-file config))
518 (define openssh-command
519 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
520 "-D" "-f" #$(openssh-config-file config)))
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)))))
531 (define (openssh-pam-services config)
532 "Return a list of <pam-services> for sshd with CONFIG."
533 (list (unix-pam-service
536 #:allow-empty-passwords?
537 (openssh-configuration-allow-empty-passwords? config))))
539 (define (extend-openssh-authorized-keys config keys)
540 "Extend CONFIG with the extra authorized keys listed in KEYS."
541 (openssh-configuration
544 (match (openssh-configuration-authorized-keys config)
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.
552 ,@(concatenate (vhash-fold* cons '() user user-keys))))
555 (define openssh-service-type
556 (service-type (name 'openssh)
558 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
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
566 (service-extension account-service-type
567 (const %openssh-accounts))
569 ;; Install OpenSSH in the system profile. That way,
570 ;; 'scp' is found when someone tries to copy to or from
572 (service-extension profile-service-type
574 (list (openssh-configuration-openssh
576 (compose concatenate)
577 (extend extend-openssh-authorized-keys)
578 (default-value (openssh-configuration))))
585 (define-record-type* <dropbear-configuration>
586 dropbear-configuration make-dropbear-configuration
587 dropbear-configuration?
588 (dropbear dropbear-configuration-dropbear
590 (port-number dropbear-configuration-port-number
592 (syslog-output? dropbear-configuration-syslog-output?
594 (pid-file dropbear-configuration-pid-file
595 (default "/var/run/dropbear.pid"))
596 (root-login? dropbear-configuration-root-login?
598 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
600 (password-authentication? dropbear-configuration-password-authentication?
603 (define (dropbear-activation config)
604 "Return the activation gexp for CONFIG."
606 (use-modules (guix build utils))
607 (mkdir-p "/etc/dropbear")))
609 (define (dropbear-shepherd-service config)
610 "Return a <shepherd-service> for dropbear with CONFIG."
612 (dropbear-configuration-dropbear config))
615 (dropbear-configuration-pid-file config))
617 (define dropbear-command
618 #~(list (string-append #$dropbear "/sbin/dropbear")
620 ;; '-R' allows host keys to be automatically generated upon first
621 ;; connection, at a time when /dev/urandom is more likely securely
625 "-p" #$(number->string (dropbear-configuration-port-number config))
627 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
628 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
629 #$@(if (dropbear-configuration-password-authentication? config)
632 #$@(if (dropbear-configuration-allow-empty-passwords? config)
637 (if (dropbear-configuration-syslog-output? config)
638 '(networking syslogd) '(networking)))
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)))))
648 (define dropbear-service-type
649 (service-type (name 'dropbear)
651 "Run the Dropbear secure shell (SSH) server.")
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))))
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>}
663 (service dropbear-service-type config))
671 (define-record-type* <autossh-configuration>
672 autossh-configuration make-autossh-configuration
673 autossh-configuration?
674 (user autossh-configuration-user
676 (poll autossh-configuration-poll
678 (first-poll autossh-configuration-first-poll
680 (gate-time autossh-configuration-gate-time
682 (log-level autossh-configuration-log-level
684 (max-start autossh-configuration-max-start
686 (message autossh-configuration-message
688 (port autossh-configuration-port
690 (ssh-options autossh-configuration-ssh-options
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)
700 (define (autossh-shepherd-service config)
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="
718 (autossh-configuration-poll config)))
719 #$(string-append "AUTOSSH_FIRST_POLL="
722 (autossh-configuration-first-poll config)
723 (autossh-configuration-poll config))))
724 #$(string-append "AUTOSSH_GATETIME="
726 (autossh-configuration-gate-time config)))
727 #$(string-append "AUTOSSH_LOGLEVEL="
729 (autossh-configuration-log-level config)))
730 #$(string-append "AUTOSSH_MAXSTART="
732 (or (autossh-configuration-max-start config)
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))))
740 (define (autossh-service-activation config)
741 (with-imported-modules '((guix build utils))
743 (use-modules (guix build utils))
745 (getpw #$(autossh-configuration-user config)))
746 (let* ((directory #$(autossh-file-name config ""))
747 (log (string-append directory "/log")))
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))))))
753 (define autossh-service-type
756 (description "Automatically set up ssh connections (and keep them alive).")
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))))
769 (define-record-type* <webssh-configuration>
770 webssh-configuration make-webssh-configuration
771 webssh-configuration?
772 (package webssh-configuration-package ;file-like
774 (user-name webssh-configuration-user-name ;string
776 (group-name webssh-configuration-group-name ;string
778 (policy webssh-configuration-policy ;symbol
780 (known-hosts webssh-configuration-known-hosts ;list of strings
782 (port webssh-configuration-port ;number
784 (address webssh-configuration-address ;string
786 (log-file webssh-configuration-log-file ;string
787 (default "/var/log/webssh.log"))
788 (log-level webssh-configuration-log-level ;symbol
791 (define %webssh-configuration-nginx
792 (nginx-server-configuration
795 (list (nginx-location-configuration
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;")))))))
806 (define webssh-account
807 ;; Return the user accounts and user groups for CONFIG.
809 (($ <webssh-configuration> _ user-name group-name _ _ _ _ _ _)
815 (comment "webssh privilege separation user")
816 (home-directory (string-append "/var/run/" user-name))
817 (shell #~(string-append #$shadow "/sbin/nologin")))))))
819 (define webssh-activation
820 ;; Return the activation GEXP for CONFIG.
822 (($ <webssh-configuration> _ user-name group-name policy known-hosts _ _
824 (with-imported-modules '((guix build utils))
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))
834 (call-with-output-file known-hosts-file
836 (for-each (lambda (host) (display host port) (newline port))
838 (display-hint (G_ "webssh: reject policy requires `known-hosts'.")))))
839 (for-each (lambda (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)))))))
846 (define webssh-shepherd-service
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)
857 ((debug) '("--logging=debug"))
860 ((reject) '("--policy=reject"))
863 (list (string-append "--port=" (number->string #$port)))
866 (list (string-append "--address=" #$address))
869 #:group #$group-name))
870 (stop #~(make-kill-destructor)))))))
872 (define webssh-service-type
876 (list (service-extension shepherd-root-service-type
877 webssh-shepherd-service)
878 (service-extension account-service-type
880 (service-extension activation-service-type
882 (default-value (webssh-configuration))
886 ;;; ssh.scm ends here