projects/guix·blob mpd-fix-groups · refs

982 lines · 39.4 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(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 ; deprecated
46 lsh-service-type
47
48 openssh-configuration
49 openssh-configuration?
50 openssh-configuration-openssh
51 openssh-configuration-pid-file
52 openssh-configuration-port-number
53 openssh-configuration-max-connections
54 openssh-configuration-permit-root-login
55 openssh-configuration-allow-empty-passwords?
56 openssh-configuration-password-authentication?
57 openssh-configuration-public-key-authentication?
58 openssh-configuration-x11-forwarding?
59 openssh-configuration-allow-agent-forwarding?
60 openssh-configuration-allow-tcp-forwarding?
61 openssh-configuration-gateway-ports?
62 openssh-configuration-challenge-response-authentication?
63 openssh-configuration-use-pam?
64 openssh-configuration-print-last-log?
65 openssh-configuration-subsystems
66 openssh-configuration-accepted-environment
67 openssh-configuration-log-level
68 openssh-configuration-extra-content
69 openssh-configuration-authorized-keys
70 openssh-configuration-generate-host-keys?
71 openssh-service-type
72
73 dropbear-configuration
74 dropbear-configuration?
75 dropbear-service-type
76 dropbear-service ; deprecated
77
78 autossh-configuration
79 autossh-configuration?
80 autossh-service-type
81
82 webssh-configuration
83 webssh-configuration?
84 webssh-service-type
85 %webssh-configuration-nginx))
86
87;;; Commentary:
88;;;
89;;; This module implements secure shell (SSH) services.
90;;;
91;;; Code:
92
93(define-record-type* <lsh-configuration>
94 lsh-configuration make-lsh-configuration
95 lsh-configuration?
96 (lsh lsh-configuration-lsh
97 (default lsh))
98 (daemonic? lsh-configuration-daemonic?
99 (default #t))
100 (host-key lsh-configuration-host-key
101 (default "/etc/lsh/host-key"))
102 (interfaces lsh-configuration-interfaces
103 (default '()))
104 (port-number lsh-configuration-port-number
105 (default 22))
106 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?
107 (default #f))
108 (root-login? lsh-configuration-root-login?
109 (default #f))
110 (syslog-output? lsh-configuration-syslog-output?
111 (default #t))
112 (pid-file? lsh-configuration-pid-file?
113 (default #f))
114 (pid-file lsh-configuration-pid-file
115 (default "/var/run/lshd.pid"))
116 (x11-forwarding? lsh-configuration-x11-forwarding?
117 (default #t))
118 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?
119 (default #t))
120 (password-authentication? lsh-configuration-password-authentication?
121 (default #t))
122 (public-key-authentication? lsh-configuration-public-key-authentication?
123 (default #t))
124 (initialize? lsh-configuration-initialize?
125 (default #t)))
126
127(define %yarrow-seed
128 "/var/spool/lsh/yarrow-seed-file")
129
130(define (lsh-initialization lsh host-key)
131 "Return the gexp to initialize the LSH service for HOST-KEY."
132 #~(begin
133 (unless (file-exists? #$%yarrow-seed)
134 (system* (string-append #$lsh "/bin/lsh-make-seed")
135 "--sloppy" "-o" #$%yarrow-seed))
136
137 (unless (file-exists? #$host-key)
138 (mkdir-p (dirname #$host-key))
139 (format #t "creating SSH host key '~a'...~%" #$host-key)
140
141 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
142 ;; used yet because /bin/sh might be dangling; factorize this somehow.
143 (let* ((in+out (pipe))
144 (keygen (primitive-fork)))
145 (case keygen
146 ((0)
147 (close-port (car in+out))
148 (close-fdes 1)
149 (dup2 (fileno (cdr in+out)) 1)
150 (execl (string-append #$lsh "/bin/lsh-keygen")
151 "lsh-keygen" "--server"))
152 (else
153 (let ((write-key (primitive-fork)))
154 (case write-key
155 ((0)
156 (close-port (cdr in+out))
157 (close-fdes 0)
158 (dup2 (fileno (car in+out)) 0)
159 (execl (string-append #$lsh "/bin/lsh-writekey")
160 "lsh-writekey" "--server" "-o" #$host-key))
161 (else
162 (close-port (car in+out))
163 (close-port (cdr in+out))
164 (waitpid keygen)
165 (waitpid write-key))))))))))
166
167(define (lsh-activation config)
168 "Return the activation gexp for CONFIG."
169 #~(begin
170 (use-modules (guix build utils))
171 (mkdir-p "/var/spool/lsh")
172 #$(if (lsh-configuration-initialize? config)
173 (lsh-initialization (lsh-configuration-lsh config)
174 (lsh-configuration-host-key config))
175 #t)))
176
177(define (lsh-shepherd-service config)
178 "Return a <shepherd-service> for lsh with CONFIG."
179 (define lsh (lsh-configuration-lsh config))
180 (define pid-file (lsh-configuration-pid-file config))
181 (define pid-file? (lsh-configuration-pid-file? config))
182 (define daemonic? (lsh-configuration-daemonic? config))
183 (define interfaces (lsh-configuration-interfaces config))
184
185 (define lsh-command
186 (append
187 (cons (file-append lsh "/sbin/lshd")
188 (if daemonic?
189 (let ((syslog (if (lsh-configuration-syslog-output? config)
190 '()
191 (list "--no-syslog"))))
192 (cons "--daemonic"
193 (if pid-file?
194 (cons #~(string-append "--pid-file=" #$pid-file)
195 syslog)
196 (cons "--no-pid-file" syslog))))
197 (if pid-file?
198 (list #~(string-append "--pid-file=" #$pid-file))
199 '())))
200 (cons* #~(string-append "--host-key="
201 #$(lsh-configuration-host-key config))
202 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
203 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
204 "-p" (number->string (lsh-configuration-port-number config))
205 (if (lsh-configuration-password-authentication? config)
206 "--password" "--no-password")
207 (if (lsh-configuration-public-key-authentication? config)
208 "--publickey" "--no-publickey")
209 (if (lsh-configuration-root-login? config)
210 "--root-login" "--no-root-login")
211 (if (lsh-configuration-x11-forwarding? config)
212 "--x11-forward" "--no-x11-forward")
213 (if (lsh-configuration-tcp/ip-forwarding? config)
214 "--tcpip-forward" "--no-tcpip-forward")
215 (if (null? interfaces)
216 '()
217 (map (cut string-append "--interface=" <>)
218 interfaces)))))
219
220 (define requires
221 `(user-processes
222 networking
223 pam
224 ,@(if (and daemonic? (lsh-configuration-syslog-output? config))
225 '(syslogd)
226 '())))
227
228 (list (shepherd-service
229 (documentation "GNU lsh SSH server")
230 (provision '(ssh-daemon ssh sshd))
231 (requirement requires)
232 (start #~(make-forkexec-constructor (list #$@lsh-command)))
233 (stop #~(make-kill-destructor)))))
234
235(define (lsh-pam-services config)
236 "Return a list of <pam-services> for lshd with CONFIG."
237 (list (unix-pam-service
238 "lshd"
239 #:login-uid? #t
240 #:allow-empty-passwords?
241 (lsh-configuration-allow-empty-passwords? config))))
242
243(define lsh-service-type
244 (service-type
245 (name 'lsh)
246 (extensions
247 (list (service-extension shepherd-root-service-type
248 lsh-shepherd-service)
249 (service-extension pam-root-service-type
250 lsh-pam-services)
251 (service-extension activation-service-type
252 lsh-activation)))
253 (description "Run the GNU@tie{}lsh secure shell (SSH) daemon,
254@command{lshd}.")
255 (default-value (lsh-configuration))))
256
257(define-deprecated (lsh-service #:key
258 (lsh lsh)
259 (daemonic? #t)
260 (host-key "/etc/lsh/host-key")
261 (interfaces '())
262 (port-number 22)
263 (allow-empty-passwords? #f)
264 (root-login? #f)
265 (syslog-output? #t)
266 (pid-file? #f)
267 (pid-file "/var/run/lshd.pid")
268 (x11-forwarding? #t)
269 (tcp/ip-forwarding? #t)
270 (password-authentication? #t)
271 (public-key-authentication? #t)
272 (initialize? #t))
273 lsh-service-type
274 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
275@var{host-key} must designate a file containing the host key, and readable
276only by root.
277
278When @var{daemonic?} is true, @command{lshd} will detach from the
279controlling terminal and log its output to syslogd, unless one sets
280@var{syslog-output?} to false. Obviously, it also makes lsh-service
281depend on existence of syslogd service. When @var{pid-file?} is true,
282@command{lshd} writes its PID to the file called @var{pid-file}.
283
284When @var{initialize?} is true, automatically create the seed and host key
285upon service activation if they do not exist yet. This may take long and
286require interaction.
287
288When @var{initialize?} is false, it is up to the user to initialize the
289randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
290a key pair with the private key stored in file @var{host-key} (@pxref{lshd
291basics,,, lsh, LSH Manual}).
292
293When @var{interfaces} is empty, lshd listens for connections on all the
294network interfaces; otherwise, @var{interfaces} must be a list of host names
295or addresses.
296
297@var{allow-empty-passwords?} specifies whether to accept log-ins with empty
298passwords, and @var{root-login?} specifies whether to accept log-ins as
299root.
300
301The other options should be self-descriptive."
302 (service lsh-service-type
303 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
304 (host-key host-key) (interfaces interfaces)
305 (port-number port-number)
306 (allow-empty-passwords? allow-empty-passwords?)
307 (root-login? root-login?)
308 (syslog-output? syslog-output?)
309 (pid-file? pid-file?) (pid-file pid-file)
310 (x11-forwarding? x11-forwarding?)
311 (tcp/ip-forwarding? tcp/ip-forwarding?)
312 (password-authentication?
313 password-authentication?)
314 (public-key-authentication?
315 public-key-authentication?)
316 (initialize? initialize?))))
317
318
319;;;
320;;; OpenSSH.
321;;;
322
323(define-record-type* <openssh-configuration>
324 openssh-configuration make-openssh-configuration
325 openssh-configuration?
326 ;; file-like object
327 (openssh openssh-configuration-openssh
328 (default openssh))
329 ;; string
330 (pid-file openssh-configuration-pid-file
331 (default "/var/run/sshd.pid"))
332 ;; integer
333 (port-number openssh-configuration-port-number
334 (default 22))
335 ;; integer
336 (max-connections openssh-configuration-max-connections
337 (default 200))
338 ;; Boolean | 'prohibit-password
339 (permit-root-login openssh-configuration-permit-root-login
340 (default #f))
341 ;; Boolean
342 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
343 (default #f))
344 ;; Boolean
345 (password-authentication? openssh-configuration-password-authentication?
346 (default #t))
347 ;; Boolean
348 (public-key-authentication? openssh-configuration-public-key-authentication?
349 (default #t))
350 ;; Boolean
351 (x11-forwarding? openssh-configuration-x11-forwarding?
352 (default #f))
353
354 ;; Boolean
355 (allow-agent-forwarding? openssh-configuration-allow-agent-forwarding?
356 (default #t))
357
358 ;; Boolean
359 (allow-tcp-forwarding? openssh-configuration-allow-tcp-forwarding?
360 (default #t))
361
362 ;; Boolean
363 (gateway-ports? openssh-configuration-gateway-ports?
364 (default #f))
365
366 ;; Boolean
367 (challenge-response-authentication?
368 openssh-configuration-challenge-response-authentication?
369 (default #f))
370
371 ;; Boolean
372 (use-pam? openssh-configuration-use-pam?
373 (default #t))
374 ;; Boolean
375 (print-last-log? openssh-configuration-print-last-log?
376 (default #t))
377 ;; list of two-element lists
378 (subsystems openssh-configuration-subsystems
379 (default '(("sftp" "internal-sftp"))))
380
381 ;; list of strings
382 (accepted-environment openssh-configuration-accepted-environment
383 (default '()))
384
385 ;; symbol
386 (log-level openssh-configuration-log-level
387 (default 'info))
388
389 ;; String
390 ;; This is an "escape hatch" to provide configuration that isn't yet
391 ;; supported by this configuration record.
392 (extra-content openssh-configuration-extra-content
393 (default ""))
394
395 ;; list of user-name/file-like tuples
396 (authorized-keys openssh-configuration-authorized-keys
397 (default '()))
398
399 ;; Boolean
400 (generate-host-keys? openssh-configuration-generate-host-keys?
401 (default #t))
402
403 ;; Boolean
404 ;; XXX: This should really be handled in an orthogonal way, for instance as
405 ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
406 ;; for now.
407 (%auto-start? openssh-auto-start?
408 (default #t)))
409
410(define %openssh-accounts
411 (list (user-group (name "sshd") (system? #t))
412 (user-account
413 (name "sshd")
414 (group "sshd")
415 (system? #t)
416 (comment "sshd privilege separation user")
417 (home-directory "/var/run/sshd")
418 (shell (file-append shadow "/sbin/nologin")))))
419
420(define (openssh-activation config)
421 "Return the activation GEXP for CONFIG."
422 (with-imported-modules '((guix build utils))
423 #~(begin
424 (use-modules (guix build utils))
425
426 (define (touch file-name)
427 (call-with-output-file file-name (const #t)))
428
429 ;; Make sure /etc/ssh can be read by the 'sshd' user.
430 (mkdir-p "/etc/ssh")
431 (chmod "/etc/ssh" #o755)
432 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
433
434 ;; 'sshd' complains if the authorized-key directory and its parents
435 ;; are group-writable, which rules out /gnu/store. Thus we copy the
436 ;; authorized-key directory to /etc.
437 (catch 'system-error
438 (lambda ()
439 (delete-file-recursively "/etc/ssh/authorized_keys.d"))
440 (lambda args
441 (unless (= ENOENT (system-error-errno args))
442 (apply throw args))))
443 (copy-recursively #$(authorized-key-directory
444 (openssh-configuration-authorized-keys config))
445 "/etc/ssh/authorized_keys.d")
446
447 (chmod "/etc/ssh/authorized_keys.d" #o555)
448
449 (let ((lastlog "/var/log/lastlog"))
450 (when #$(openssh-configuration-print-last-log? config)
451 (unless (file-exists? lastlog)
452 (touch lastlog))))
453
454 (when #$(openssh-configuration-generate-host-keys? config)
455 ;; Generate missing host keys.
456 (system* (string-append #$(openssh-configuration-openssh config)
457 "/bin/ssh-keygen") "-A")))))
458
459(define (authorized-key-directory keys)
460 "Return a directory containing the authorized keys specified in KEYS, a list
461of user-name/file-like tuples."
462 (define build
463 (with-imported-modules (source-module-closure '((guix build utils)))
464 #~(begin
465 (use-modules (ice-9 match) (srfi srfi-26)
466 (guix build utils))
467
468 (mkdir #$output)
469 (for-each (match-lambda
470 ((user keys ...)
471 (let ((file (string-append #$output "/" user)))
472 (call-with-output-file file
473 (lambda (port)
474 (for-each (lambda (key)
475 (call-with-input-file key
476 (cut dump-port <> port)))
477 keys))))))
478 '#$keys))))
479
480 (computed-file "openssh-authorized-keys" build))
481
482(define (openssh-config-file config)
483 "Return the sshd configuration file corresponding to CONFIG."
484 (computed-file
485 "sshd_config"
486 #~(begin
487 (use-modules (ice-9 match))
488 (call-with-output-file #$output
489 (lambda (port)
490 (display "# Generated by 'openssh-service'.\n" port)
491 (format port "Port ~a\n"
492 #$(number->string
493 (openssh-configuration-port-number config)))
494 (format port "PermitRootLogin ~a\n"
495 #$(match (openssh-configuration-permit-root-login config)
496 (#t "yes")
497 (#f "no")
498 ('without-password (warn-about-deprecation
499 'without-password #f
500 #:replacement 'prohibit-password)
501 "prohibit-password")
502 ('prohibit-password "prohibit-password")))
503 (format port "PermitEmptyPasswords ~a\n"
504 #$(if (openssh-configuration-allow-empty-passwords? config)
505 "yes" "no"))
506 (format port "PasswordAuthentication ~a\n"
507 #$(if (openssh-configuration-password-authentication? config)
508 "yes" "no"))
509 (format port "PubkeyAuthentication ~a\n"
510 #$(if (openssh-configuration-public-key-authentication?
511 config)
512 "yes" "no"))
513 (format port "X11Forwarding ~a\n"
514 #$(if (openssh-configuration-x11-forwarding? config)
515 "yes" "no"))
516 (format port "AllowAgentForwarding ~a\n"
517 #$(if (openssh-configuration-allow-agent-forwarding? config)
518 "yes" "no"))
519 (format port "AllowTcpForwarding ~a\n"
520 #$(if (openssh-configuration-allow-tcp-forwarding? config)
521 "yes" "no"))
522 (format port "GatewayPorts ~a\n"
523 #$(if (openssh-configuration-gateway-ports? config)
524 "yes" "no"))
525 (format port "PidFile ~a\n"
526 #$(openssh-configuration-pid-file config))
527 (format port "ChallengeResponseAuthentication ~a\n"
528 #$(if (openssh-configuration-challenge-response-authentication?
529 config)
530 "yes" "no"))
531 (format port "UsePAM ~a\n"
532 #$(if (openssh-configuration-use-pam? config)
533 "yes" "no"))
534 (format port "PrintLastLog ~a\n"
535 #$(if (openssh-configuration-print-last-log? config)
536 "yes" "no"))
537 (format port "LogLevel ~a\n"
538 #$(string-upcase
539 (symbol->string
540 (openssh-configuration-log-level config))))
541
542 ;; Add '/etc/authorized_keys.d/%u', which we populate.
543 (format port "AuthorizedKeysFile \
544 .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
545
546 (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
547 '#$(openssh-configuration-accepted-environment config))
548
549 (for-each
550 (match-lambda
551 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
552 '#$(openssh-configuration-subsystems config))
553
554 (format port "~a\n"
555 #$(openssh-configuration-extra-content config))
556 #t)))))
557
558(define (openssh-shepherd-service config)
559 "Return a <shepherd-service> for openssh with CONFIG."
560
561 (define pid-file
562 (openssh-configuration-pid-file config))
563
564 (define port-number
565 (openssh-configuration-port-number config))
566
567 (define max-connections
568 (openssh-configuration-max-connections config))
569
570 (define config-file
571 (openssh-config-file config))
572
573 (define openssh-command
574 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
575 "-D" "-f" #$config-file))
576
577 (define inetd-style?
578 ;; Whether to use 'make-inetd-constructor'. That procedure appeared in
579 ;; Shepherd 0.9.0, but in 0.9.0, 'make-inetd-constructor' wouldn't let us
580 ;; pass a list of endpoints, and it wouldn't let us define a service
581 ;; listening on both IPv4 and IPv6, hence the conditional below.
582 #~(and (defined? 'make-inetd-constructor)
583 (not (string=? (@ (shepherd config) Version) "0.9.0"))))
584
585 (define ipv6-support?
586 ;; Expression that returns true if IPv6 support is available.
587 #~(catch 'system-error
588 (lambda ()
589 (let ((sock (socket AF_INET6 SOCK_STREAM 0)))
590 (close-port sock)
591 #t))
592 (const #f)))
593
594 (list (shepherd-service
595 (documentation "OpenSSH server.")
596
597 ;; On the Hurd, this can only be started after pfinet is up, hence
598 ;; the dependency on 'networking'.
599 (requirement '(user-processes pam syslogd loopback networking))
600 (provision '(ssh-daemon ssh sshd))
601
602 (start #~(if #$inetd-style?
603 (make-inetd-constructor
604 (append #$openssh-command '("-i"))
605 (cons (endpoint
606 (make-socket-address AF_INET INADDR_ANY
607 #$port-number))
608 (if #$ipv6-support?
609 (list
610 (endpoint
611 (make-socket-address AF_INET6 IN6ADDR_ANY
612 #$port-number)))
613 '()))
614 #:requirements '#$requirement
615 #:max-connections #$max-connections)
616 (make-forkexec-constructor #$openssh-command
617 #:pid-file #$pid-file)))
618 (stop #~(if #$inetd-style?
619 (make-inetd-destructor)
620 (make-kill-destructor)))
621 (actions (list (shepherd-configuration-action config-file)))
622 (auto-start? (openssh-auto-start? config)))))
623
624(define (openssh-pam-services config)
625 "Return a list of <pam-services> for sshd with CONFIG."
626 (list (unix-pam-service
627 "sshd"
628 #:login-uid? #t
629 #:allow-empty-passwords?
630 (openssh-configuration-allow-empty-passwords? config))))
631
632(define (extend-openssh-authorized-keys config keys)
633 "Extend CONFIG with the extra authorized keys listed in KEYS."
634 (openssh-configuration
635 (inherit config)
636 (authorized-keys
637 (match (append (openssh-configuration-authorized-keys config) keys)
638 ((and alist ((users _ ...) ...))
639 ;; Build a user/key-list mapping.
640 (let ((user-keys (alist->vhash alist)))
641 ;; Coalesce the key lists associated with each user.
642 (map (lambda (user)
643 `(,user
644 ,@(concatenate (vhash-fold* cons '() user user-keys))))
645 users)))))))
646
647(define openssh-service-type
648 (service-type (name 'openssh)
649 (description
650 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
651 (extensions
652 (list (service-extension shepherd-root-service-type
653 openssh-shepherd-service)
654 (service-extension pam-root-service-type
655 openssh-pam-services)
656 (service-extension activation-service-type
657 openssh-activation)
658 (service-extension account-service-type
659 (const %openssh-accounts))
660
661 ;; Install OpenSSH in the system profile. That way,
662 ;; 'scp' is found when someone tries to copy to or from
663 ;; this machine.
664 (service-extension profile-service-type
665 (lambda (config)
666 (list (openssh-configuration-openssh
667 config))))))
668 (compose concatenate)
669 (extend extend-openssh-authorized-keys)
670 (default-value (openssh-configuration))))
671
672
673;;;
674;;; Dropbear.
675;;;
676
677(define-record-type* <dropbear-configuration>
678 dropbear-configuration make-dropbear-configuration
679 dropbear-configuration?
680 (dropbear dropbear-configuration-dropbear
681 (default dropbear))
682 (port-number dropbear-configuration-port-number
683 (default 22))
684 (syslog-output? dropbear-configuration-syslog-output?
685 (default #t))
686 (pid-file dropbear-configuration-pid-file
687 (default "/var/run/dropbear.pid"))
688 (root-login? dropbear-configuration-root-login?
689 (default #f))
690 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
691 (default #f))
692 (password-authentication? dropbear-configuration-password-authentication?
693 (default #t)))
694
695(define (dropbear-activation config)
696 "Return the activation gexp for CONFIG."
697 #~(begin
698 (use-modules (guix build utils))
699 (mkdir-p "/etc/dropbear")))
700
701(define (dropbear-shepherd-service config)
702 "Return a <shepherd-service> for dropbear with CONFIG."
703 (define dropbear
704 (dropbear-configuration-dropbear config))
705
706 (define pid-file
707 (dropbear-configuration-pid-file config))
708
709 (define dropbear-command
710 #~(list (string-append #$dropbear "/sbin/dropbear")
711
712 ;; '-R' allows host keys to be automatically generated upon first
713 ;; connection, at a time when /dev/urandom is more likely securely
714 ;; seeded.
715 "-F" "-R"
716
717 "-p" #$(number->string (dropbear-configuration-port-number config))
718 "-P" #$pid-file
719 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
720 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
721 #$@(if (dropbear-configuration-password-authentication? config)
722 '()
723 '("-s" "-g"))
724 #$@(if (dropbear-configuration-allow-empty-passwords? config)
725 '("-B")
726 '())))
727
728 (define requires
729 (if (dropbear-configuration-syslog-output? config)
730 '(user-processes networking syslogd)
731 '(user-processes networking)))
732
733 (list (shepherd-service
734 (documentation "Dropbear SSH server.")
735 (requirement requires)
736 (provision '(ssh-daemon ssh sshd))
737 (start #~(make-forkexec-constructor #$dropbear-command
738 #:pid-file #$pid-file))
739 (stop #~(make-kill-destructor)))))
740
741(define dropbear-service-type
742 (service-type (name 'dropbear)
743 (description
744 "Run the Dropbear secure shell (SSH) server.")
745 (extensions
746 (list (service-extension shepherd-root-service-type
747 dropbear-shepherd-service)
748 (service-extension activation-service-type
749 dropbear-activation)))
750 (default-value (dropbear-configuration))))
751
752(define-deprecated (dropbear-service #:optional
753 (config (dropbear-configuration)))
754 dropbear-service-type
755 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
756daemon} with the given @var{config}, a @code{<dropbear-configuration>}
757object."
758 (service dropbear-service-type config))
759
760
761;;;
762;;; AutoSSH.
763;;;
764
765
766(define-record-type* <autossh-configuration>
767 autossh-configuration make-autossh-configuration
768 autossh-configuration?
769 (user autossh-configuration-user
770 (default "autossh"))
771 (poll autossh-configuration-poll
772 (default 600))
773 (first-poll autossh-configuration-first-poll
774 (default #f))
775 (gate-time autossh-configuration-gate-time
776 (default 30))
777 (log-level autossh-configuration-log-level
778 (default 1))
779 (max-start autossh-configuration-max-start
780 (default #f))
781 (message autossh-configuration-message
782 (default ""))
783 (port autossh-configuration-port
784 (default "0"))
785 (ssh-options autossh-configuration-ssh-options
786 (default '())))
787
788(define (autossh-file-name config file)
789 "Return a path in /var/run/autossh/ that is writable
790 by @code{user} from @code{config}."
791 (string-append "/var/run/autossh/"
792 (autossh-configuration-user config)
793 "/" file))
794
795(define (autossh-shepherd-service config)
796 (shepherd-service
797 (documentation "Automatically set up ssh connections (and keep them alive).")
798 (provision '(autossh))
799 (requirement '(user-processes))
800 (start #~(make-forkexec-constructor
801 (list #$(file-append autossh "/bin/autossh")
802 #$@(autossh-configuration-ssh-options config))
803 #:user #$(autossh-configuration-user config)
804 #:group (passwd:gid (getpw #$(autossh-configuration-user config)))
805 #:pid-file #$(autossh-file-name config "pid")
806 #:log-file #$(autossh-file-name config "log")
807 #:environment-variables
808 '(#$(string-append "AUTOSSH_PIDFILE="
809 (autossh-file-name config "pid"))
810 #$(string-append "AUTOSSH_LOGFILE="
811 (autossh-file-name config "log"))
812 #$(string-append "AUTOSSH_POLL="
813 (number->string
814 (autossh-configuration-poll config)))
815 #$(string-append "AUTOSSH_FIRST_POLL="
816 (number->string
817 (or
818 (autossh-configuration-first-poll config)
819 (autossh-configuration-poll config))))
820 #$(string-append "AUTOSSH_GATETIME="
821 (number->string
822 (autossh-configuration-gate-time config)))
823 #$(string-append "AUTOSSH_LOGLEVEL="
824 (number->string
825 (autossh-configuration-log-level config)))
826 #$(string-append "AUTOSSH_MAXSTART="
827 (number->string
828 (or (autossh-configuration-max-start config)
829 -1)))
830 #$(string-append "AUTOSSH_MESSAGE="
831 (autossh-configuration-message config))
832 #$(string-append "AUTOSSH_PORT="
833 (autossh-configuration-port config)))))
834 (stop #~(make-kill-destructor))))
835
836(define (autossh-service-activation config)
837 (with-imported-modules '((guix build utils))
838 #~(begin
839 (use-modules (guix build utils))
840 (define %user
841 (getpw #$(autossh-configuration-user config)))
842 (let* ((directory #$(autossh-file-name config ""))
843 (log (string-append directory "/log")))
844 (mkdir-p directory)
845 (chown directory (passwd:uid %user) (passwd:gid %user))
846 (call-with-output-file log (const #t))
847 (chown log (passwd:uid %user) (passwd:gid %user))))))
848
849(define autossh-service-type
850 (service-type
851 (name 'autossh)
852 (description "Automatically set up ssh connections (and keep them alive).")
853 (extensions
854 (list (service-extension shepherd-root-service-type
855 (compose list autossh-shepherd-service))
856 (service-extension activation-service-type
857 autossh-service-activation)))
858 (default-value (autossh-configuration))))
859
860
861;;;
862;;; WebSSH
863;;;
864
865(define-record-type* <webssh-configuration>
866 webssh-configuration make-webssh-configuration
867 webssh-configuration?
868 (package webssh-configuration-package ;file-like
869 (default webssh))
870 (user-name webssh-configuration-user-name ;string
871 (default "webssh"))
872 (group-name webssh-configuration-group-name ;string
873 (default "webssh"))
874 (policy webssh-configuration-policy ;symbol
875 (default #f))
876 (known-hosts webssh-configuration-known-hosts ;list of strings
877 (default #f))
878 (port webssh-configuration-port ;number
879 (default #f))
880 (address webssh-configuration-address ;string
881 (default #f))
882 (log-file webssh-configuration-log-file ;string
883 (default "/var/log/webssh.log"))
884 (log-level webssh-configuration-log-level ;symbol
885 (default #f)))
886
887(define %webssh-configuration-nginx
888 (nginx-server-configuration
889 (listen '("80"))
890 (locations
891 (list (nginx-location-configuration
892 (uri "/")
893 (body '("proxy_pass http://127.0.0.1:8888;"
894 "proxy_http_version 1.1;"
895 "proxy_read_timeout 300;"
896 "proxy_set_header Upgrade $http_upgrade;"
897 "proxy_set_header Connection \"upgrade\";"
898 "proxy_set_header Host $http_host;"
899 "proxy_set_header X-Real-IP $remote_addr;"
900 "proxy_set_header X-Real-PORT $remote_port;")))))))
901
902(define webssh-account
903 ;; Return the user accounts and user groups for CONFIG.
904 (match-lambda
905 (($ <webssh-configuration> _ user-name group-name _ _ _ _ _ _)
906 (list (user-group
907 (name group-name))
908 (user-account
909 (name user-name)
910 (group group-name)
911 (comment "webssh privilege separation user")
912 (home-directory (string-append "/var/run/" user-name))
913 (shell #~(string-append #$shadow "/sbin/nologin")))))))
914
915(define webssh-activation
916 ;; Return the activation GEXP for CONFIG.
917 (match-lambda
918 (($ <webssh-configuration> _ user-name group-name policy known-hosts _ _
919 log-file _)
920 (with-imported-modules '((guix build utils))
921 #~(begin
922 (let* ((home-dir (string-append "/var/run/" #$user-name))
923 (ssh-dir (string-append home-dir "/.ssh"))
924 (known-hosts-file (string-append ssh-dir "/known_hosts")))
925 (call-with-output-file #$log-file (const #t))
926 (mkdir-p ssh-dir)
927 (case '#$policy
928 ((reject)
929 (if '#$known-hosts
930 (call-with-output-file known-hosts-file
931 (lambda (port)
932 (for-each (lambda (host) (display host port) (newline port))
933 '#$known-hosts)))
934 (display-hint (G_ "webssh: reject policy requires `known-hosts'.")))))
935 (for-each (lambda (file)
936 (chown file
937 (passwd:uid (getpw #$user-name))
938 (group:gid (getpw #$group-name))))
939 (list #$log-file ssh-dir known-hosts-file))
940 (chmod ssh-dir #o700)))))))
941
942(define webssh-shepherd-service
943 (match-lambda
944 (($ <webssh-configuration> package user-name group-name policy _ port
945 address log-file log-level)
946 (list (shepherd-service
947 (provision '(webssh))
948 (documentation "Run webssh daemon.")
949 (start #~(make-forkexec-constructor
950 `(,(string-append #$webssh "/bin/wssh")
951 ,(string-append "--log-file-prefix=" #$log-file)
952 ,@(case '#$log-level
953 ((debug) '("--logging=debug"))
954 (else '()))
955 ,@(case '#$policy
956 ((reject) '("--policy=reject"))
957 (else '()))
958 ,@(if #$port
959 (list (string-append "--port=" (number->string #$port)))
960 '())
961 ,@(if #$address
962 (list (string-append "--address=" #$address))
963 '()))
964 #:user #$user-name
965 #:group #$group-name))
966 (stop #~(make-kill-destructor)))))))
967
968(define webssh-service-type
969 (service-type
970 (name 'webssh)
971 (extensions
972 (list (service-extension shepherd-root-service-type
973 webssh-shepherd-service)
974 (service-extension account-service-type
975 webssh-account)
976 (service-extension activation-service-type
977 webssh-activation)))
978 (default-value (webssh-configuration))
979 (description
980 "Run the webssh.")))
981
982;;; ssh.scm ends here