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