services: Service types can now specify a default value for instances.
[jackhill/guix/guix.git] / gnu / services / ssh.scm
CommitLineData
f33e2d78 1;;; GNU Guix --- Functional package management for GNU
71b0601a
DC
2;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3;;; Copyright © 2016 David Craven <david@craven.ch>
86d8f6d3 4;;; Copyright © 2016 Julien Lepiller <julien@lepiller.eu>
e57bd0be 5;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
f33e2d78
LC
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)
71b0601a 23 #:use-module (gnu packages ssh)
86d8f6d3 24 #:use-module (gnu packages admin)
f33e2d78 25 #:use-module (gnu services)
0190c1c0 26 #:use-module (gnu services shepherd)
6e828634 27 #:use-module (gnu system pam)
86d8f6d3 28 #:use-module (gnu system shadow)
71b0601a
DC
29 #:use-module (guix gexp)
30 #:use-module (guix records)
fde40c98 31 #:use-module (srfi srfi-26)
86d8f6d3 32 #:use-module (ice-9 match)
24e96431
33 #:export (lsh-configuration
34 lsh-configuration?
35 lsh-service
36 lsh-service-type
71b0601a 37
86d8f6d3
JL
38 openssh-configuration
39 openssh-configuration?
40 openssh-service-type
86d8f6d3 41
71b0601a
DC
42 dropbear-configuration
43 dropbear-configuration?
44 dropbear-service-type
45 dropbear-service))
f33e2d78
LC
46
47;;; Commentary:
48;;;
49;;; This module implements secure shell (SSH) services.
50;;;
51;;; Code:
52
0adfe95a
LC
53(define-record-type* <lsh-configuration>
54 lsh-configuration make-lsh-configuration
55 lsh-configuration?
56 (lsh lsh-configuration-lsh
57 (default lsh))
58 (daemonic? lsh-configuration-daemonic?)
59 (host-key lsh-configuration-host-key)
60 (interfaces lsh-configuration-interfaces)
61 (port-number lsh-configuration-port-number)
62 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
63 (root-login? lsh-configuration-root-login?)
64 (syslog-output? lsh-configuration-syslog-output?)
65 (pid-file? lsh-configuration-pid-file?)
66 (pid-file lsh-configuration-pid-file)
67 (x11-forwarding? lsh-configuration-x11-forwarding?)
68 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
69 (password-authentication? lsh-configuration-password-authentication?)
70 (public-key-authentication? lsh-configuration-public-key-authentication?)
71 (initialize? lsh-configuration-initialize?))
72
f33e2d78
LC
73(define %yarrow-seed
74 "/var/spool/lsh/yarrow-seed-file")
75
0adfe95a
LC
76(define (lsh-initialization lsh host-key)
77 "Return the gexp to initialize the LSH service for HOST-KEY."
f33e2d78
LC
78 #~(begin
79 (unless (file-exists? #$%yarrow-seed)
80 (system* (string-append #$lsh "/bin/lsh-make-seed")
81 "--sloppy" "-o" #$%yarrow-seed))
82
83 (unless (file-exists? #$host-key)
84 (mkdir-p (dirname #$host-key))
85 (format #t "creating SSH host key '~a'...~%" #$host-key)
86
87 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
88 ;; used yet because /bin/sh might be dangling; factorize this somehow.
89 (let* ((in+out (pipe))
90 (keygen (primitive-fork)))
91 (case keygen
92 ((0)
93 (close-port (car in+out))
94 (close-fdes 1)
95 (dup2 (fileno (cdr in+out)) 1)
96 (execl (string-append #$lsh "/bin/lsh-keygen")
97 "lsh-keygen" "--server"))
98 (else
99 (let ((write-key (primitive-fork)))
100 (case write-key
101 ((0)
102 (close-port (cdr in+out))
103 (close-fdes 0)
104 (dup2 (fileno (car in+out)) 0)
105 (execl (string-append #$lsh "/bin/lsh-writekey")
106 "lsh-writekey" "--server" "-o" #$host-key))
107 (else
108 (close-port (car in+out))
109 (close-port (cdr in+out))
110 (waitpid keygen)
111 (waitpid write-key))))))))))
112
0adfe95a
LC
113(define (lsh-activation config)
114 "Return the activation gexp for CONFIG."
115 #~(begin
116 (use-modules (guix build utils))
117 (mkdir-p "/var/spool/lsh")
118 #$(if (lsh-configuration-initialize? config)
119 (lsh-initialization (lsh-configuration-lsh config)
120 (lsh-configuration-host-key config))
121 #t)))
122
d4053c71
AK
123(define (lsh-shepherd-service config)
124 "Return a <shepherd-service> for lsh with CONFIG."
0adfe95a
LC
125 (define lsh (lsh-configuration-lsh config))
126 (define pid-file (lsh-configuration-pid-file config))
127 (define pid-file? (lsh-configuration-pid-file? config))
128 (define daemonic? (lsh-configuration-daemonic? config))
129 (define interfaces (lsh-configuration-interfaces config))
130
131 (define lsh-command
132 (append
9e41130b 133 (cons (file-append lsh "/sbin/lshd")
0adfe95a
LC
134 (if daemonic?
135 (let ((syslog (if (lsh-configuration-syslog-output? config)
136 '()
137 (list "--no-syslog"))))
138 (cons "--daemonic"
139 (if pid-file?
140 (cons #~(string-append "--pid-file=" #$pid-file)
141 syslog)
142 (cons "--no-pid-file" syslog))))
143 (if pid-file?
144 (list #~(string-append "--pid-file=" #$pid-file))
145 '())))
146 (cons* #~(string-append "--host-key="
147 #$(lsh-configuration-host-key config))
148 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
149 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
150 "-p" (number->string (lsh-configuration-port-number config))
151 (if (lsh-configuration-password-authentication? config)
152 "--password" "--no-password")
153 (if (lsh-configuration-public-key-authentication? config)
154 "--publickey" "--no-publickey")
155 (if (lsh-configuration-root-login? config)
156 "--root-login" "--no-root-login")
157 (if (lsh-configuration-x11-forwarding? config)
158 "--x11-forward" "--no-x11-forward")
159 (if (lsh-configuration-tcp/ip-forwarding? config)
160 "--tcpip-forward" "--no-tcpip-forward")
161 (if (null? interfaces)
162 '()
fde40c98
LC
163 (map (cut string-append "--interface=" <>)
164 interfaces)))))
0adfe95a
LC
165
166 (define requires
167 (if (and daemonic? (lsh-configuration-syslog-output? config))
168 '(networking syslogd)
169 '(networking)))
170
d4053c71 171 (list (shepherd-service
0adfe95a
LC
172 (documentation "GNU lsh SSH server")
173 (provision '(ssh-daemon))
174 (requirement requires)
175 (start #~(make-forkexec-constructor (list #$@lsh-command)))
176 (stop #~(make-kill-destructor)))))
177
178(define (lsh-pam-services config)
179 "Return a list of <pam-services> for lshd with CONFIG."
180 (list (unix-pam-service
181 "lshd"
182 #:allow-empty-passwords?
183 (lsh-configuration-allow-empty-passwords? config))))
184
185(define lsh-service-type
186 (service-type (name 'lsh)
187 (extensions
d4053c71
AK
188 (list (service-extension shepherd-root-service-type
189 lsh-shepherd-service)
0adfe95a
LC
190 (service-extension pam-root-service-type
191 lsh-pam-services)
192 (service-extension activation-service-type
193 lsh-activation)))))
194
f33e2d78
LC
195(define* (lsh-service #:key
196 (lsh lsh)
5833bf33 197 (daemonic? #t)
f33e2d78
LC
198 (host-key "/etc/lsh/host-key")
199 (interfaces '())
200 (port-number 22)
201 (allow-empty-passwords? #f)
202 (root-login? #f)
203 (syslog-output? #t)
5833bf33
DP
204 (pid-file? #f)
205 (pid-file "/var/run/lshd.pid")
f33e2d78
LC
206 (x11-forwarding? #t)
207 (tcp/ip-forwarding? #t)
208 (password-authentication? #t)
209 (public-key-authentication? #t)
21cc905a 210 (initialize? #t))
f33e2d78
LC
211 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
212@var{host-key} must designate a file containing the host key, and readable
213only by root.
214
5833bf33
DP
215When @var{daemonic?} is true, @command{lshd} will detach from the
216controlling terminal and log its output to syslogd, unless one sets
217@var{syslog-output?} to false. Obviously, it also makes lsh-service
218depend on existence of syslogd service. When @var{pid-file?} is true,
219@command{lshd} writes its PID to the file called @var{pid-file}.
220
f33e2d78
LC
221When @var{initialize?} is true, automatically create the seed and host key
222upon service activation if they do not exist yet. This may take long and
223require interaction.
224
20dd519c
LC
225When @var{initialize?} is false, it is up to the user to initialize the
226randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
227a key pair with the private key stored in file @var{host-key} (@pxref{lshd
228basics,,, lsh, LSH Manual}).
229
f33e2d78
LC
230When @var{interfaces} is empty, lshd listens for connections on all the
231network interfaces; otherwise, @var{interfaces} must be a list of host names
232or addresses.
233
20dd519c
LC
234@var{allow-empty-passwords?} specifies whether to accept log-ins with empty
235passwords, and @var{root-login?} specifies whether to accept log-ins as
f33e2d78
LC
236root.
237
238The other options should be self-descriptive."
0adfe95a
LC
239 (service lsh-service-type
240 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
241 (host-key host-key) (interfaces interfaces)
242 (port-number port-number)
243 (allow-empty-passwords? allow-empty-passwords?)
244 (root-login? root-login?)
245 (syslog-output? syslog-output?)
246 (pid-file? pid-file?) (pid-file pid-file)
247 (x11-forwarding? x11-forwarding?)
248 (tcp/ip-forwarding? tcp/ip-forwarding?)
249 (password-authentication?
250 password-authentication?)
251 (public-key-authentication?
252 public-key-authentication?)
253 (initialize? initialize?))))
f33e2d78 254
71b0601a 255\f
86d8f6d3
JL
256;;;
257;;; OpenSSH.
258;;;
259
260(define-record-type* <openssh-configuration>
261 openssh-configuration make-openssh-configuration
262 openssh-configuration?
4ca3e9b7
CL
263 ;; <package>
264 (openssh openssh-configuration-openssh
23f22ba8 265 (default openssh))
4ca3e9b7 266 ;; string
d8f31281
LC
267 (pid-file openssh-configuration-pid-file
268 (default "/var/run/sshd.pid"))
4ca3e9b7
CL
269 ;; integer
270 (port-number openssh-configuration-port-number
d8f31281 271 (default 22))
4ca3e9b7
CL
272 ;; Boolean | 'without-password
273 (permit-root-login openssh-configuration-permit-root-login
d8f31281 274 (default #f))
4ca3e9b7
CL
275 ;; Boolean
276 (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
d8f31281 277 (default #f))
4ca3e9b7
CL
278 ;; Boolean
279 (password-authentication? openssh-configuration-password-authentication?
d8f31281 280 (default #t))
4ca3e9b7 281 ;; Boolean
d8f31281 282 (public-key-authentication? openssh-configuration-public-key-authentication?
4ca3e9b7
CL
283 (default #t))
284 ;; Boolean
285 (x11-forwarding? openssh-configuration-x11-forwarding?
d8f31281 286 (default #f))
4ca3e9b7 287 ;; Boolean
563c5d42 288 (challenge-response-authentication? openssh-challenge-response-authentication?
4ca3e9b7
CL
289 (default #f))
290 ;; Boolean
563c5d42 291 (use-pam? openssh-configuration-use-pam?
4ca3e9b7
CL
292 (default #t))
293 ;; Boolean
f895dce4 294 (print-last-log? openssh-configuration-print-last-log?
12723370
CL
295 (default #t))
296 ;; list of two-element lists
297 (subsystems openssh-configuration-subsystems
298 (default '(("sftp" "internal-sftp")))))
86d8f6d3
JL
299
300(define %openssh-accounts
301 (list (user-group (name "sshd") (system? #t))
302 (user-account
303 (name "sshd")
304 (group "sshd")
305 (system? #t)
306 (comment "sshd privilege separation user")
307 (home-directory "/var/run/sshd")
308 (shell #~(string-append #$shadow "/sbin/nologin")))))
309
310(define (openssh-activation config)
311 "Return the activation GEXP for CONFIG."
312 #~(begin
e57bd0be 313 (use-modules (guix build utils))
86d8f6d3
JL
314 (mkdir-p "/etc/ssh")
315 (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
316
f895dce4
CL
317 (define (touch file-name)
318 (call-with-output-file file-name (const #t)))
319
320 (let ((lastlog "/var/log/lastlog"))
321 (when #$(openssh-configuration-print-last-log? config)
322 (unless (file-exists? lastlog)
323 (touch lastlog))))
324
86d8f6d3 325 ;; Generate missing host keys.
23f22ba8
LF
326 (system* (string-append #$(openssh-configuration-openssh config)
327 "/bin/ssh-keygen") "-A")))
86d8f6d3
JL
328
329(define (openssh-config-file config)
330 "Return the sshd configuration file corresponding to CONFIG."
331 (computed-file
332 "sshd_config"
12723370
CL
333 #~(begin
334 (use-modules (ice-9 match))
335 (call-with-output-file #$output
336 (lambda (port)
337 (display "# Generated by 'openssh-service'.\n" port)
338 (format port "Port ~a\n"
339 #$(number->string
340 (openssh-configuration-port-number config)))
341 (format port "PermitRootLogin ~a\n"
342 #$(match (openssh-configuration-permit-root-login config)
343 (#t "yes")
344 (#f "no")
345 ('without-password "without-password")))
346 (format port "PermitEmptyPasswords ~a\n"
347 #$(if (openssh-configuration-allow-empty-passwords? config)
348 "yes" "no"))
349 (format port "PasswordAuthentication ~a\n"
350 #$(if (openssh-configuration-password-authentication? config)
351 "yes" "no"))
352 (format port "PubkeyAuthentication ~a\n"
353 #$(if (openssh-configuration-public-key-authentication?
354 config)
355 "yes" "no"))
356 (format port "X11Forwarding ~a\n"
357 #$(if (openssh-configuration-x11-forwarding? config)
358 "yes" "no"))
359 (format port "PidFile ~a\n"
360 #$(openssh-configuration-pid-file config))
361 (format port "ChallengeResponseAuthentication ~a\n"
362 #$(if (openssh-challenge-response-authentication? config)
363 "yes" "no"))
364 (format port "UsePAM ~a\n"
365 #$(if (openssh-configuration-use-pam? config)
366 "yes" "no"))
367 (format port "PrintLastLog ~a\n"
368 #$(if (openssh-configuration-print-last-log? config)
369 "yes" "no"))
370 (for-each
371 (match-lambda
372 ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
373 '#$(openssh-configuration-subsystems config))
374 #t)))))
86d8f6d3
JL
375
376(define (openssh-shepherd-service config)
377 "Return a <shepherd-service> for openssh with CONFIG."
378
379 (define pid-file
380 (openssh-configuration-pid-file config))
381
382 (define openssh-command
23f22ba8 383 #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
86d8f6d3
JL
384 "-D" "-f" #$(openssh-config-file config)))
385
386 (list (shepherd-service
387 (documentation "OpenSSH server.")
388 (requirement '(networking syslogd))
389 (provision '(ssh-daemon))
390 (start #~(make-forkexec-constructor #$openssh-command
391 #:pid-file #$pid-file))
392 (stop #~(make-kill-destructor)))))
393
563c5d42
CL
394(define (openssh-pam-services config)
395 "Return a list of <pam-services> for sshd with CONFIG."
396 (list (unix-pam-service
397 "sshd"
398 #:allow-empty-passwords?
399 (openssh-configuration-allow-empty-passwords? config))))
400
86d8f6d3
JL
401(define openssh-service-type
402 (service-type (name 'openssh)
403 (extensions
404 (list (service-extension shepherd-root-service-type
405 openssh-shepherd-service)
563c5d42
CL
406 (service-extension pam-root-service-type
407 openssh-pam-services)
86d8f6d3
JL
408 (service-extension activation-service-type
409 openssh-activation)
410 (service-extension account-service-type
411 (const %openssh-accounts))))))
412
86d8f6d3 413\f
71b0601a
DC
414;;;
415;;; Dropbear.
416;;;
417
418(define-record-type* <dropbear-configuration>
419 dropbear-configuration make-dropbear-configuration
420 dropbear-configuration?
421 (dropbear dropbear-configuration-dropbear
422 (default dropbear))
423 (port-number dropbear-configuration-port-number
424 (default 22))
425 (syslog-output? dropbear-configuration-syslog-output?
426 (default #t))
427 (pid-file dropbear-configuration-pid-file
428 (default "/var/run/dropbear.pid"))
429 (root-login? dropbear-configuration-root-login?
430 (default #f))
431 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
432 (default #f))
433 (password-authentication? dropbear-configuration-password-authentication?
434 (default #t)))
435
436(define (dropbear-activation config)
437 "Return the activation gexp for CONFIG."
438 #~(begin
e57bd0be 439 (use-modules (guix build utils))
71b0601a
DC
440 (mkdir-p "/etc/dropbear")))
441
442(define (dropbear-shepherd-service config)
443 "Return a <shepherd-service> for dropbear with CONFIG."
444 (define dropbear
445 (dropbear-configuration-dropbear config))
446
447 (define pid-file
448 (dropbear-configuration-pid-file config))
449
450 (define dropbear-command
451 #~(list (string-append #$dropbear "/sbin/dropbear")
452
453 ;; '-R' allows host keys to be automatically generated upon first
454 ;; connection, at a time when /dev/urandom is more likely securely
455 ;; seeded.
456 "-F" "-R"
457
458 "-p" #$(number->string (dropbear-configuration-port-number config))
459 "-P" #$pid-file
460 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
461 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
462 #$@(if (dropbear-configuration-password-authentication? config)
463 '()
464 '("-s" "-g"))
465 #$@(if (dropbear-configuration-allow-empty-passwords? config)
466 '("-B")
467 '())))
468
469 (define requires
470 (if (dropbear-configuration-syslog-output? config)
471 '(networking syslogd) '(networking)))
472
473 (list (shepherd-service
474 (documentation "Dropbear SSH server.")
475 (requirement requires)
476 (provision '(ssh-daemon))
477 (start #~(make-forkexec-constructor #$dropbear-command
478 #:pid-file #$pid-file))
479 (stop #~(make-kill-destructor)))))
480
481(define dropbear-service-type
482 (service-type (name 'dropbear)
483 (extensions
484 (list (service-extension shepherd-root-service-type
485 dropbear-shepherd-service)
486 (service-extension activation-service-type
487 dropbear-activation)))))
488
489(define* (dropbear-service #:optional (config (dropbear-configuration)))
490 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
491daemon} with the given @var{config}, a @code{<dropbear-configuration>}
492object."
493 (service dropbear-service-type config))
494
f33e2d78 495;;; ssh.scm ends here