projects/guix·blob master · refs

828 lines · 33.2 KiB

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