Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / gnu / services / ssh.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017 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 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
13 ;;;
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22 (define-module (gnu services ssh)
23 #:use-module (gnu packages ssh)
24 #:use-module (gnu packages admin)
25 #:use-module (gnu services)
26 #:use-module (gnu services shepherd)
27 #:use-module (gnu system pam)
28 #:use-module (gnu system shadow)
29 #:use-module (guix gexp)
30 #:use-module (guix records)
31 #:use-module (guix modules)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-26)
34 #:use-module (ice-9 match)
35 #:export (lsh-configuration
36 lsh-configuration?
37 lsh-service
38 lsh-service-type
39
40 openssh-configuration
41 openssh-configuration?
42 openssh-service-type
43
44 dropbear-configuration
45 dropbear-configuration?
46 dropbear-service-type
47 dropbear-service))
48
49 ;;; Commentary:
50 ;;;
51 ;;; This module implements secure shell (SSH) services.
52 ;;;
53 ;;; Code:
54
55 (define-record-type* <lsh-configuration>
56 lsh-configuration make-lsh-configuration
57 lsh-configuration?
58 (lsh lsh-configuration-lsh
59 (default lsh))
60 (daemonic? lsh-configuration-daemonic?)
61 (host-key lsh-configuration-host-key)
62 (interfaces lsh-configuration-interfaces)
63 (port-number lsh-configuration-port-number)
64 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
65 (root-login? lsh-configuration-root-login?)
66 (syslog-output? lsh-configuration-syslog-output?)
67 (pid-file? lsh-configuration-pid-file?)
68 (pid-file lsh-configuration-pid-file)
69 (x11-forwarding? lsh-configuration-x11-forwarding?)
70 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
71 (password-authentication? lsh-configuration-password-authentication?)
72 (public-key-authentication? lsh-configuration-public-key-authentication?)
73 (initialize? lsh-configuration-initialize?))
74
75 (define %yarrow-seed
76 "/var/spool/lsh/yarrow-seed-file")
77
78 (define (lsh-initialization lsh host-key)
79 "Return the gexp to initialize the LSH service for HOST-KEY."
80 #~(begin
81 (unless (file-exists? #$%yarrow-seed)
82 (system* (string-append #$lsh "/bin/lsh-make-seed")
83 "--sloppy" "-o" #$%yarrow-seed))
84
85 (unless (file-exists? #$host-key)
86 (mkdir-p (dirname #$host-key))
87 (format #t "creating SSH host key '~a'...~%" #$host-key)
88
89 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
90 ;; used yet because /bin/sh might be dangling; factorize this somehow.
91 (let* ((in+out (pipe))
92 (keygen (primitive-fork)))
93 (case keygen
94 ((0)
95 (close-port (car in+out))
96 (close-fdes 1)
97 (dup2 (fileno (cdr in+out)) 1)
98 (execl (string-append #$lsh "/bin/lsh-keygen")
99 "lsh-keygen" "--server"))
100 (else
101 (let ((write-key (primitive-fork)))
102 (case write-key
103 ((0)
104 (close-port (cdr in+out))
105 (close-fdes 0)
106 (dup2 (fileno (car in+out)) 0)
107 (execl (string-append #$lsh "/bin/lsh-writekey")
108 "lsh-writekey" "--server" "-o" #$host-key))
109 (else
110 (close-port (car in+out))
111 (close-port (cdr in+out))
112 (waitpid keygen)
113 (waitpid write-key))))))))))
114
115 (define (lsh-activation config)
116 "Return the activation gexp for CONFIG."
117 #~(begin
118 (use-modules (guix build utils))
119 (mkdir-p "/var/spool/lsh")
120 #$(if (lsh-configuration-initialize? config)
121 (lsh-initialization (lsh-configuration-lsh config)
122 (lsh-configuration-host-key config))
123 #t)))
124
125 (define (lsh-shepherd-service config)
126 "Return a <shepherd-service> for lsh with CONFIG."
127 (define lsh (lsh-configuration-lsh config))
128 (define pid-file (lsh-configuration-pid-file config))
129 (define pid-file? (lsh-configuration-pid-file? config))
130 (define daemonic? (lsh-configuration-daemonic? config))
131 (define interfaces (lsh-configuration-interfaces config))
132
133 (define lsh-command
134 (append
135 (cons (file-append lsh "/sbin/lshd")
136 (if daemonic?
137 (let ((syslog (if (lsh-configuration-syslog-output? config)
138 '()
139 (list "--no-syslog"))))
140 (cons "--daemonic"
141 (if pid-file?
142 (cons #~(string-append "--pid-file=" #$pid-file)
143 syslog)
144 (cons "--no-pid-file" syslog))))
145 (if pid-file?
146 (list #~(string-append "--pid-file=" #$pid-file))
147 '())))
148 (cons* #~(string-append "--host-key="
149 #$(lsh-configuration-host-key config))
150 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
151 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
152 "-p" (number->string (lsh-configuration-port-number config))
153 (if (lsh-configuration-password-authentication? config)
154 "--password" "--no-password")
155 (if (lsh-configuration-public-key-authentication? config)
156 "--publickey" "--no-publickey")
157 (if (lsh-configuration-root-login? config)
158 "--root-login" "--no-root-login")
159 (if (lsh-configuration-x11-forwarding? config)
160 "--x11-forward" "--no-x11-forward")
161 (if (lsh-configuration-tcp/ip-forwarding? config)
162 "--tcpip-forward" "--no-tcpip-forward")
163 (if (null? interfaces)
164 '()
165 (map (cut string-append "--interface=" <>)
166 interfaces)))))
167
168 (define requires
169 (if (and daemonic? (lsh-configuration-syslog-output? config))
170 '(networking syslogd)
171 '(networking)))
172
173 (list (shepherd-service
174 (documentation "GNU lsh SSH server")
175 (provision '(ssh-daemon))
176 (requirement requires)
177 (start #~(make-forkexec-constructor (list #$@lsh-command)))
178 (stop #~(make-kill-destructor)))))
179
180 (define (lsh-pam-services config)
181 "Return a list of <pam-services> for lshd with CONFIG."
182 (list (unix-pam-service
183 "lshd"
184 #:allow-empty-passwords?
185 (lsh-configuration-allow-empty-passwords? config))))
186
187 (define lsh-service-type
188 (service-type (name 'lsh)
189 (description
190 "Run the GNU@tie{}lsh secure shell (SSH) daemon,
191 @command{lshd}.")
192 (extensions
193 (list (service-extension shepherd-root-service-type
194 lsh-shepherd-service)
195 (service-extension pam-root-service-type
196 lsh-pam-services)
197 (service-extension activation-service-type
198 lsh-activation)))))
199
200 (define* (lsh-service #:key
201 (lsh lsh)
202 (daemonic? #t)
203 (host-key "/etc/lsh/host-key")
204 (interfaces '())
205 (port-number 22)
206 (allow-empty-passwords? #f)
207 (root-login? #f)
208 (syslog-output? #t)
209 (pid-file? #f)
210 (pid-file "/var/run/lshd.pid")
211 (x11-forwarding? #t)
212 (tcp/ip-forwarding? #t)
213 (password-authentication? #t)
214 (public-key-authentication? #t)
215 (initialize? #t))
216 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
217 @var{host-key} must designate a file containing the host key, and readable
218 only by root.
219
220 When @var{daemonic?} is true, @command{lshd} will detach from the
221 controlling terminal and log its output to syslogd, unless one sets
222 @var{syslog-output?} to false. Obviously, it also makes lsh-service
223 depend on existence of syslogd service. When @var{pid-file?} is true,
224 @command{lshd} writes its PID to the file called @var{pid-file}.
225
226 When @var{initialize?} is true, automatically create the seed and host key
227 upon service activation if they do not exist yet. This may take long and
228 require interaction.
229
230 When @var{initialize?} is false, it is up to the user to initialize the
231 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
232 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
233 basics,,, lsh, LSH Manual}).
234
235 When @var{interfaces} is empty, lshd listens for connections on all the
236 network interfaces; otherwise, @var{interfaces} must be a list of host names
237 or addresses.
238
239 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
240 passwords, and @var{root-login?} specifies whether to accept log-ins as
241 root.
242
243 The other options should be self-descriptive."
244 (service lsh-service-type
245 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
246 (host-key host-key) (interfaces interfaces)
247 (port-number port-number)
248 (allow-empty-passwords? allow-empty-passwords?)
249 (root-login? root-login?)
250 (syslog-output? syslog-output?)
251 (pid-file? pid-file?) (pid-file pid-file)
252 (x11-forwarding? x11-forwarding?)
253 (tcp/ip-forwarding? tcp/ip-forwarding?)
254 (password-authentication?
255 password-authentication?)
256 (public-key-authentication?
257 public-key-authentication?)
258 (initialize? initialize?))))
259
260 \f
261 ;;;
262 ;;; OpenSSH.
263 ;;;
264
265 (define-record-type* <openssh-configuration>
266 openssh-configuration make-openssh-configuration
267 openssh-configuration?
268 ;; <package>
269 (openssh openssh-configuration-openssh
270 (default openssh))
271 ;; string
272 (pid-file openssh-configuration-pid-file
273 (default "/var/run/sshd.pid"))
274 ;; integer
275 (port-number openssh-configuration-port-number
276 (default 22))
277 ;; Boolean | 'without-password
278 (permit-root-login openssh-configuration-permit-root-login
279 (default #f))
280 ;; Boolean
281 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
282 (default #f))
283 ;; Boolean
284 (password-authentication? openssh-configuration-password-authentication?
285 (default #t))
286 ;; Boolean
287 (public-key-authentication? openssh-configuration-public-key-authentication?
288 (default #t))
289 ;; Boolean
290 (x11-forwarding? openssh-configuration-x11-forwarding?
291 (default #f))
292 ;; Boolean
293 (challenge-response-authentication? openssh-challenge-response-authentication?
294 (default #f))
295 ;; Boolean
296 (use-pam? openssh-configuration-use-pam?
297 (default #t))
298 ;; Boolean
299 (print-last-log? openssh-configuration-print-last-log?
300 (default #t))
301 ;; list of two-element lists
302 (subsystems openssh-configuration-subsystems
303 (default '(("sftp" "internal-sftp"))))
304
305 ;; list of strings
306 (accepted-environment openssh-configuration-accepted-environment
307 (default '()))
308
309 ;; list of user-name/file-like tuples
310 (authorized-keys openssh-authorized-keys
311 (default '()))
312
313 ;; Boolean
314 ;; XXX: This should really be handled in an orthogonal way, for instance as
315 ;; proposed in <https://bugs.gnu.org/27155>. Keep it internal/undocumented
316 ;; for now.
317 (%auto-start? openssh-auto-start?
318 (default #t)))
319
320 (define %openssh-accounts
321 (list (user-group (name "sshd") (system? #t))
322 (user-account
323 (name "sshd")
324 (group "sshd")
325 (system? #t)
326 (comment "sshd privilege separation user")
327 (home-directory "/var/run/sshd")
328 (shell #~(string-append #$shadow "/sbin/nologin")))))
329
330 (define (openssh-activation config)
331 "Return the activation GEXP for CONFIG."
332 (with-imported-modules '((guix build utils))
333 #~(begin
334 (use-modules (guix build utils))
335
336 (define (touch file-name)
337 (call-with-output-file file-name (const #t)))
338
339 ;; Make sure /etc/ssh can be read by the 'sshd' user.
340 (mkdir-p "/etc/ssh")
341 (chmod "/etc/ssh" #o755)
342 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
343
344 ;; 'sshd' complains if the authorized-key directory and its parents
345 ;; are group-writable, which rules out /gnu/store. Thus we copy the
346 ;; authorized-key directory to /etc.
347 (catch 'system-error
348 (lambda ()
349 (delete-file-recursively "/etc/authorized_keys.d"))
350 (lambda args
351 (unless (= ENOENT (system-error-errno args))
352 (apply throw args))))
353 (copy-recursively #$(authorized-key-directory
354 (openssh-authorized-keys config))
355 "/etc/ssh/authorized_keys.d")
356
357 (chmod "/etc/ssh/authorized_keys.d" #o555)
358
359 (let ((lastlog "/var/log/lastlog"))
360 (when #$(openssh-configuration-print-last-log? config)
361 (unless (file-exists? lastlog)
362 (touch lastlog))))
363
364 ;; Generate missing host keys.
365 (system* (string-append #$(openssh-configuration-openssh config)
366 "/bin/ssh-keygen") "-A"))))
367
368 (define (authorized-key-directory keys)
369 "Return a directory containing the authorized keys specified in KEYS, a list
370 of user-name/file-like tuples."
371 (define build
372 (with-imported-modules (source-module-closure '((guix build utils)))
373 #~(begin
374 (use-modules (ice-9 match) (srfi srfi-26)
375 (guix build utils))
376
377 (mkdir #$output)
378 (for-each (match-lambda
379 ((user keys ...)
380 (let ((file (string-append #$output "/" user)))
381 (call-with-output-file file
382 (lambda (port)
383 (for-each (lambda (key)
384 (call-with-input-file key
385 (cut dump-port <> port)))
386 keys))))))
387 '#$keys))))
388
389 (computed-file "openssh-authorized-keys" build))
390
391 (define (openssh-config-file config)
392 "Return the sshd configuration file corresponding to CONFIG."
393 (computed-file
394 "sshd_config"
395 #~(begin
396 (use-modules (ice-9 match))
397 (call-with-output-file #$output
398 (lambda (port)
399 (display "# Generated by 'openssh-service'.\n" port)
400 (format port "Port ~a\n"
401 #$(number->string
402 (openssh-configuration-port-number config)))
403 (format port "PermitRootLogin ~a\n"
404 #$(match (openssh-configuration-permit-root-login config)
405 (#t "yes")
406 (#f "no")
407 ('without-password "without-password")))
408 (format port "PermitEmptyPasswords ~a\n"
409 #$(if (openssh-configuration-allow-empty-passwords? config)
410 "yes" "no"))
411 (format port "PasswordAuthentication ~a\n"
412 #$(if (openssh-configuration-password-authentication? config)
413 "yes" "no"))
414 (format port "PubkeyAuthentication ~a\n"
415 #$(if (openssh-configuration-public-key-authentication?
416 config)
417 "yes" "no"))
418 (format port "X11Forwarding ~a\n"
419 #$(if (openssh-configuration-x11-forwarding? config)
420 "yes" "no"))
421 (format port "PidFile ~a\n"
422 #$(openssh-configuration-pid-file config))
423 (format port "ChallengeResponseAuthentication ~a\n"
424 #$(if (openssh-challenge-response-authentication? config)
425 "yes" "no"))
426 (format port "UsePAM ~a\n"
427 #$(if (openssh-configuration-use-pam? config)
428 "yes" "no"))
429 (format port "PrintLastLog ~a\n"
430 #$(if (openssh-configuration-print-last-log? config)
431 "yes" "no"))
432
433 ;; Add '/etc/authorized_keys.d/%u', which we populate.
434 (format port "AuthorizedKeysFile \
435 .ssh/authorized_keys .ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u\n")
436
437 (for-each (lambda (s) (format port "AcceptEnv ~a\n" s))
438 '#$(openssh-configuration-accepted-environment config))
439
440 (for-each
441 (match-lambda
442 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
443 '#$(openssh-configuration-subsystems config))
444 #t)))))
445
446 (define (openssh-shepherd-service config)
447 "Return a <shepherd-service> for openssh with CONFIG."
448
449 (define pid-file
450 (openssh-configuration-pid-file config))
451
452 (define openssh-command
453 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
454 "-D" "-f" #$(openssh-config-file config)))
455
456 (list (shepherd-service
457 (documentation "OpenSSH server.")
458 (requirement '(syslogd loopback))
459 (provision '(ssh-daemon))
460 (start #~(make-forkexec-constructor #$openssh-command
461 #:pid-file #$pid-file))
462 (stop #~(make-kill-destructor))
463 (auto-start? (openssh-auto-start? config)))))
464
465 (define (openssh-pam-services config)
466 "Return a list of <pam-services> for sshd with CONFIG."
467 (list (unix-pam-service
468 "sshd"
469 #:allow-empty-passwords?
470 (openssh-configuration-allow-empty-passwords? config))))
471
472 (define (extend-openssh-authorized-keys config keys)
473 "Extend CONFIG with the extra authorized keys listed in KEYS."
474 (openssh-configuration
475 (inherit config)
476 (authorized-keys
477 (append (openssh-authorized-keys config) keys))))
478
479 (define openssh-service-type
480 (service-type (name 'openssh)
481 (description
482 "Run the OpenSSH secure shell (SSH) server, @command{sshd}.")
483 (extensions
484 (list (service-extension shepherd-root-service-type
485 openssh-shepherd-service)
486 (service-extension pam-root-service-type
487 openssh-pam-services)
488 (service-extension activation-service-type
489 openssh-activation)
490 (service-extension account-service-type
491 (const %openssh-accounts))))
492 (compose concatenate)
493 (extend extend-openssh-authorized-keys)
494 (default-value (openssh-configuration))))
495
496 \f
497 ;;;
498 ;;; Dropbear.
499 ;;;
500
501 (define-record-type* <dropbear-configuration>
502 dropbear-configuration make-dropbear-configuration
503 dropbear-configuration?
504 (dropbear dropbear-configuration-dropbear
505 (default dropbear))
506 (port-number dropbear-configuration-port-number
507 (default 22))
508 (syslog-output? dropbear-configuration-syslog-output?
509 (default #t))
510 (pid-file dropbear-configuration-pid-file
511 (default "/var/run/dropbear.pid"))
512 (root-login? dropbear-configuration-root-login?
513 (default #f))
514 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
515 (default #f))
516 (password-authentication? dropbear-configuration-password-authentication?
517 (default #t)))
518
519 (define (dropbear-activation config)
520 "Return the activation gexp for CONFIG."
521 #~(begin
522 (use-modules (guix build utils))
523 (mkdir-p "/etc/dropbear")))
524
525 (define (dropbear-shepherd-service config)
526 "Return a <shepherd-service> for dropbear with CONFIG."
527 (define dropbear
528 (dropbear-configuration-dropbear config))
529
530 (define pid-file
531 (dropbear-configuration-pid-file config))
532
533 (define dropbear-command
534 #~(list (string-append #$dropbear "/sbin/dropbear")
535
536 ;; '-R' allows host keys to be automatically generated upon first
537 ;; connection, at a time when /dev/urandom is more likely securely
538 ;; seeded.
539 "-F" "-R"
540
541 "-p" #$(number->string (dropbear-configuration-port-number config))
542 "-P" #$pid-file
543 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
544 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
545 #$@(if (dropbear-configuration-password-authentication? config)
546 '()
547 '("-s" "-g"))
548 #$@(if (dropbear-configuration-allow-empty-passwords? config)
549 '("-B")
550 '())))
551
552 (define requires
553 (if (dropbear-configuration-syslog-output? config)
554 '(networking syslogd) '(networking)))
555
556 (list (shepherd-service
557 (documentation "Dropbear SSH server.")
558 (requirement requires)
559 (provision '(ssh-daemon))
560 (start #~(make-forkexec-constructor #$dropbear-command
561 #:pid-file #$pid-file))
562 (stop #~(make-kill-destructor)))))
563
564 (define dropbear-service-type
565 (service-type (name 'dropbear)
566 (description
567 "Run the Dropbear secure shell (SSH) server.")
568 (extensions
569 (list (service-extension shepherd-root-service-type
570 dropbear-shepherd-service)
571 (service-extension activation-service-type
572 dropbear-activation)))))
573
574 (define* (dropbear-service #:optional (config (dropbear-configuration)))
575 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
576 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
577 object."
578 (service dropbear-service-type config))
579
580 ;;; ssh.scm ends here