1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 David Craven <david@craven.ch>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services ssh)
21 #:use-module (gnu packages ssh)
22 #:use-module (gnu services)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu system pam)
25 #:use-module (guix gexp)
26 #:use-module (guix records)
27 #:use-module (srfi srfi-26)
28 #:export (lsh-configuration
33 dropbear-configuration
34 dropbear-configuration?
40 ;;; This module implements secure shell (SSH) services.
45 (define-record-type* <lsh-configuration>
46 lsh-configuration make-lsh-configuration
48 (lsh lsh-configuration-lsh
50 (daemonic? lsh-configuration-daemonic?)
51 (host-key lsh-configuration-host-key)
52 (interfaces lsh-configuration-interfaces)
53 (port-number lsh-configuration-port-number)
54 (allow-empty-passwords? lsh-configuration-allow-empty-passwords?)
55 (root-login? lsh-configuration-root-login?)
56 (syslog-output? lsh-configuration-syslog-output?)
57 (pid-file? lsh-configuration-pid-file?)
58 (pid-file lsh-configuration-pid-file)
59 (x11-forwarding? lsh-configuration-x11-forwarding?)
60 (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?)
61 (password-authentication? lsh-configuration-password-authentication?)
62 (public-key-authentication? lsh-configuration-public-key-authentication?)
63 (initialize? lsh-configuration-initialize?))
66 "/var/spool/lsh/yarrow-seed-file")
68 (define (lsh-initialization lsh host-key)
69 "Return the gexp to initialize the LSH service for HOST-KEY."
71 (unless (file-exists? #$%yarrow-seed)
72 (system* (string-append #$lsh "/bin/lsh-make-seed")
73 "--sloppy" "-o" #$%yarrow-seed))
75 (unless (file-exists? #$host-key)
76 (mkdir-p (dirname #$host-key))
77 (format #t "creating SSH host key '~a'...~%" #$host-key)
79 ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be
80 ;; used yet because /bin/sh might be dangling; factorize this somehow.
81 (let* ((in+out (pipe))
82 (keygen (primitive-fork)))
85 (close-port (car in+out))
87 (dup2 (fileno (cdr in+out)) 1)
88 (execl (string-append #$lsh "/bin/lsh-keygen")
89 "lsh-keygen" "--server"))
91 (let ((write-key (primitive-fork)))
94 (close-port (cdr in+out))
96 (dup2 (fileno (car in+out)) 0)
97 (execl (string-append #$lsh "/bin/lsh-writekey")
98 "lsh-writekey" "--server" "-o" #$host-key))
100 (close-port (car in+out))
101 (close-port (cdr in+out))
103 (waitpid write-key))))))))))
105 (define (lsh-activation config)
106 "Return the activation gexp for CONFIG."
108 (use-modules (guix build utils))
109 (mkdir-p "/var/spool/lsh")
110 #$(if (lsh-configuration-initialize? config)
111 (lsh-initialization (lsh-configuration-lsh config)
112 (lsh-configuration-host-key config))
115 (define (lsh-shepherd-service config)
116 "Return a <shepherd-service> for lsh with CONFIG."
117 (define lsh (lsh-configuration-lsh config))
118 (define pid-file (lsh-configuration-pid-file config))
119 (define pid-file? (lsh-configuration-pid-file? config))
120 (define daemonic? (lsh-configuration-daemonic? config))
121 (define interfaces (lsh-configuration-interfaces config))
125 (cons #~(string-append #$lsh "/sbin/lshd")
127 (let ((syslog (if (lsh-configuration-syslog-output? config)
129 (list "--no-syslog"))))
132 (cons #~(string-append "--pid-file=" #$pid-file)
134 (cons "--no-pid-file" syslog))))
136 (list #~(string-append "--pid-file=" #$pid-file))
138 (cons* #~(string-append "--host-key="
139 #$(lsh-configuration-host-key config))
140 #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw")
141 #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server")
142 "-p" (number->string (lsh-configuration-port-number config))
143 (if (lsh-configuration-password-authentication? config)
144 "--password" "--no-password")
145 (if (lsh-configuration-public-key-authentication? config)
146 "--publickey" "--no-publickey")
147 (if (lsh-configuration-root-login? config)
148 "--root-login" "--no-root-login")
149 (if (lsh-configuration-x11-forwarding? config)
150 "--x11-forward" "--no-x11-forward")
151 (if (lsh-configuration-tcp/ip-forwarding? config)
152 "--tcpip-forward" "--no-tcpip-forward")
153 (if (null? interfaces)
155 (map (cut string-append "--interface=" <>)
159 (if (and daemonic? (lsh-configuration-syslog-output? config))
160 '(networking syslogd)
163 (list (shepherd-service
164 (documentation "GNU lsh SSH server")
165 (provision '(ssh-daemon))
166 (requirement requires)
167 (start #~(make-forkexec-constructor (list #$@lsh-command)))
168 (stop #~(make-kill-destructor)))))
170 (define (lsh-pam-services config)
171 "Return a list of <pam-services> for lshd with CONFIG."
172 (list (unix-pam-service
174 #:allow-empty-passwords?
175 (lsh-configuration-allow-empty-passwords? config))))
177 (define lsh-service-type
178 (service-type (name 'lsh)
180 (list (service-extension shepherd-root-service-type
181 lsh-shepherd-service)
182 (service-extension pam-root-service-type
184 (service-extension activation-service-type
187 (define* (lsh-service #:key
190 (host-key "/etc/lsh/host-key")
193 (allow-empty-passwords? #f)
197 (pid-file "/var/run/lshd.pid")
199 (tcp/ip-forwarding? #t)
200 (password-authentication? #t)
201 (public-key-authentication? #t)
203 "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}.
204 @var{host-key} must designate a file containing the host key, and readable
207 When @var{daemonic?} is true, @command{lshd} will detach from the
208 controlling terminal and log its output to syslogd, unless one sets
209 @var{syslog-output?} to false. Obviously, it also makes lsh-service
210 depend on existence of syslogd service. When @var{pid-file?} is true,
211 @command{lshd} writes its PID to the file called @var{pid-file}.
213 When @var{initialize?} is true, automatically create the seed and host key
214 upon service activation if they do not exist yet. This may take long and
217 When @var{initialize?} is false, it is up to the user to initialize the
218 randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create
219 a key pair with the private key stored in file @var{host-key} (@pxref{lshd
220 basics,,, lsh, LSH Manual}).
222 When @var{interfaces} is empty, lshd listens for connections on all the
223 network interfaces; otherwise, @var{interfaces} must be a list of host names
226 @var{allow-empty-passwords?} specifies whether to accept log-ins with empty
227 passwords, and @var{root-login?} specifies whether to accept log-ins as
230 The other options should be self-descriptive."
231 (service lsh-service-type
232 (lsh-configuration (lsh lsh) (daemonic? daemonic?)
233 (host-key host-key) (interfaces interfaces)
234 (port-number port-number)
235 (allow-empty-passwords? allow-empty-passwords?)
236 (root-login? root-login?)
237 (syslog-output? syslog-output?)
238 (pid-file? pid-file?) (pid-file pid-file)
239 (x11-forwarding? x11-forwarding?)
240 (tcp/ip-forwarding? tcp/ip-forwarding?)
241 (password-authentication?
242 password-authentication?)
243 (public-key-authentication?
244 public-key-authentication?)
245 (initialize? initialize?))))
252 (define-record-type* <dropbear-configuration>
253 dropbear-configuration make-dropbear-configuration
254 dropbear-configuration?
255 (dropbear dropbear-configuration-dropbear
257 (port-number dropbear-configuration-port-number
259 (syslog-output? dropbear-configuration-syslog-output?
261 (pid-file dropbear-configuration-pid-file
262 (default "/var/run/dropbear.pid"))
263 (root-login? dropbear-configuration-root-login?
265 (allow-empty-passwords? dropbear-configuration-allow-empty-passwords?
267 (password-authentication? dropbear-configuration-password-authentication?
270 (define (dropbear-activation config)
271 "Return the activation gexp for CONFIG."
273 (mkdir-p "/etc/dropbear")))
275 (define (dropbear-shepherd-service config)
276 "Return a <shepherd-service> for dropbear with CONFIG."
278 (dropbear-configuration-dropbear config))
281 (dropbear-configuration-pid-file config))
283 (define dropbear-command
284 #~(list (string-append #$dropbear "/sbin/dropbear")
286 ;; '-R' allows host keys to be automatically generated upon first
287 ;; connection, at a time when /dev/urandom is more likely securely
291 "-p" #$(number->string (dropbear-configuration-port-number config))
293 #$@(if (dropbear-configuration-syslog-output? config) '() '("-E"))
294 #$@(if (dropbear-configuration-root-login? config) '() '("-w"))
295 #$@(if (dropbear-configuration-password-authentication? config)
298 #$@(if (dropbear-configuration-allow-empty-passwords? config)
303 (if (dropbear-configuration-syslog-output? config)
304 '(networking syslogd) '(networking)))
306 (list (shepherd-service
307 (documentation "Dropbear SSH server.")
308 (requirement requires)
309 (provision '(ssh-daemon))
310 (start #~(make-forkexec-constructor #$dropbear-command
311 #:pid-file #$pid-file))
312 (stop #~(make-kill-destructor)))))
314 (define dropbear-service-type
315 (service-type (name 'dropbear)
317 (list (service-extension shepherd-root-service-type
318 dropbear-shepherd-service)
319 (service-extension activation-service-type
320 dropbear-activation)))))
322 (define* (dropbear-service #:optional (config (dropbear-configuration)))
323 "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH
324 daemon} with the given @var{config}, a @code{<dropbear-configuration>}
326 (service dropbear-service-type config))
328 ;;; ssh.scm ends here