Commit | Line | Data |
---|---|---|
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> | |
f33e2d78 LC |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
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. | |
11 | ;;; | |
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. | |
16 | ;;; | |
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/>. | |
19 | ||
20 | (define-module (gnu services ssh) | |
71b0601a | 21 | #:use-module (gnu packages ssh) |
f33e2d78 | 22 | #:use-module (gnu services) |
0190c1c0 | 23 | #:use-module (gnu services shepherd) |
6e828634 | 24 | #:use-module (gnu system pam) |
71b0601a DC |
25 | #:use-module (guix gexp) |
26 | #:use-module (guix records) | |
fde40c98 | 27 | #:use-module (srfi srfi-26) |
71b0601a DC |
28 | #:export (lsh-service |
29 | ||
30 | dropbear-configuration | |
31 | dropbear-configuration? | |
32 | dropbear-service-type | |
33 | dropbear-service)) | |
f33e2d78 LC |
34 | |
35 | ;;; Commentary: | |
36 | ;;; | |
37 | ;;; This module implements secure shell (SSH) services. | |
38 | ;;; | |
39 | ;;; Code: | |
40 | ||
0adfe95a LC |
41 | ;; TODO: Export. |
42 | (define-record-type* <lsh-configuration> | |
43 | lsh-configuration make-lsh-configuration | |
44 | lsh-configuration? | |
45 | (lsh lsh-configuration-lsh | |
46 | (default lsh)) | |
47 | (daemonic? lsh-configuration-daemonic?) | |
48 | (host-key lsh-configuration-host-key) | |
49 | (interfaces lsh-configuration-interfaces) | |
50 | (port-number lsh-configuration-port-number) | |
51 | (allow-empty-passwords? lsh-configuration-allow-empty-passwords?) | |
52 | (root-login? lsh-configuration-root-login?) | |
53 | (syslog-output? lsh-configuration-syslog-output?) | |
54 | (pid-file? lsh-configuration-pid-file?) | |
55 | (pid-file lsh-configuration-pid-file) | |
56 | (x11-forwarding? lsh-configuration-x11-forwarding?) | |
57 | (tcp/ip-forwarding? lsh-configuration-tcp/ip-forwarding?) | |
58 | (password-authentication? lsh-configuration-password-authentication?) | |
59 | (public-key-authentication? lsh-configuration-public-key-authentication?) | |
60 | (initialize? lsh-configuration-initialize?)) | |
61 | ||
f33e2d78 LC |
62 | (define %yarrow-seed |
63 | "/var/spool/lsh/yarrow-seed-file") | |
64 | ||
0adfe95a LC |
65 | (define (lsh-initialization lsh host-key) |
66 | "Return the gexp to initialize the LSH service for HOST-KEY." | |
f33e2d78 LC |
67 | #~(begin |
68 | (unless (file-exists? #$%yarrow-seed) | |
69 | (system* (string-append #$lsh "/bin/lsh-make-seed") | |
70 | "--sloppy" "-o" #$%yarrow-seed)) | |
71 | ||
72 | (unless (file-exists? #$host-key) | |
73 | (mkdir-p (dirname #$host-key)) | |
74 | (format #t "creating SSH host key '~a'...~%" #$host-key) | |
75 | ||
76 | ;; FIXME: We're just doing a simple pipeline, but 'system' cannot be | |
77 | ;; used yet because /bin/sh might be dangling; factorize this somehow. | |
78 | (let* ((in+out (pipe)) | |
79 | (keygen (primitive-fork))) | |
80 | (case keygen | |
81 | ((0) | |
82 | (close-port (car in+out)) | |
83 | (close-fdes 1) | |
84 | (dup2 (fileno (cdr in+out)) 1) | |
85 | (execl (string-append #$lsh "/bin/lsh-keygen") | |
86 | "lsh-keygen" "--server")) | |
87 | (else | |
88 | (let ((write-key (primitive-fork))) | |
89 | (case write-key | |
90 | ((0) | |
91 | (close-port (cdr in+out)) | |
92 | (close-fdes 0) | |
93 | (dup2 (fileno (car in+out)) 0) | |
94 | (execl (string-append #$lsh "/bin/lsh-writekey") | |
95 | "lsh-writekey" "--server" "-o" #$host-key)) | |
96 | (else | |
97 | (close-port (car in+out)) | |
98 | (close-port (cdr in+out)) | |
99 | (waitpid keygen) | |
100 | (waitpid write-key)))))))))) | |
101 | ||
0adfe95a LC |
102 | (define (lsh-activation config) |
103 | "Return the activation gexp for CONFIG." | |
104 | #~(begin | |
105 | (use-modules (guix build utils)) | |
106 | (mkdir-p "/var/spool/lsh") | |
107 | #$(if (lsh-configuration-initialize? config) | |
108 | (lsh-initialization (lsh-configuration-lsh config) | |
109 | (lsh-configuration-host-key config)) | |
110 | #t))) | |
111 | ||
d4053c71 AK |
112 | (define (lsh-shepherd-service config) |
113 | "Return a <shepherd-service> for lsh with CONFIG." | |
0adfe95a LC |
114 | (define lsh (lsh-configuration-lsh config)) |
115 | (define pid-file (lsh-configuration-pid-file config)) | |
116 | (define pid-file? (lsh-configuration-pid-file? config)) | |
117 | (define daemonic? (lsh-configuration-daemonic? config)) | |
118 | (define interfaces (lsh-configuration-interfaces config)) | |
119 | ||
120 | (define lsh-command | |
121 | (append | |
122 | (cons #~(string-append #$lsh "/sbin/lshd") | |
123 | (if daemonic? | |
124 | (let ((syslog (if (lsh-configuration-syslog-output? config) | |
125 | '() | |
126 | (list "--no-syslog")))) | |
127 | (cons "--daemonic" | |
128 | (if pid-file? | |
129 | (cons #~(string-append "--pid-file=" #$pid-file) | |
130 | syslog) | |
131 | (cons "--no-pid-file" syslog)))) | |
132 | (if pid-file? | |
133 | (list #~(string-append "--pid-file=" #$pid-file)) | |
134 | '()))) | |
135 | (cons* #~(string-append "--host-key=" | |
136 | #$(lsh-configuration-host-key config)) | |
137 | #~(string-append "--password-helper=" #$lsh "/sbin/lsh-pam-checkpw") | |
138 | #~(string-append "--subsystems=sftp=" #$lsh "/sbin/sftp-server") | |
139 | "-p" (number->string (lsh-configuration-port-number config)) | |
140 | (if (lsh-configuration-password-authentication? config) | |
141 | "--password" "--no-password") | |
142 | (if (lsh-configuration-public-key-authentication? config) | |
143 | "--publickey" "--no-publickey") | |
144 | (if (lsh-configuration-root-login? config) | |
145 | "--root-login" "--no-root-login") | |
146 | (if (lsh-configuration-x11-forwarding? config) | |
147 | "--x11-forward" "--no-x11-forward") | |
148 | (if (lsh-configuration-tcp/ip-forwarding? config) | |
149 | "--tcpip-forward" "--no-tcpip-forward") | |
150 | (if (null? interfaces) | |
151 | '() | |
fde40c98 LC |
152 | (map (cut string-append "--interface=" <>) |
153 | interfaces))))) | |
0adfe95a LC |
154 | |
155 | (define requires | |
156 | (if (and daemonic? (lsh-configuration-syslog-output? config)) | |
157 | '(networking syslogd) | |
158 | '(networking))) | |
159 | ||
d4053c71 | 160 | (list (shepherd-service |
0adfe95a LC |
161 | (documentation "GNU lsh SSH server") |
162 | (provision '(ssh-daemon)) | |
163 | (requirement requires) | |
164 | (start #~(make-forkexec-constructor (list #$@lsh-command))) | |
165 | (stop #~(make-kill-destructor))))) | |
166 | ||
167 | (define (lsh-pam-services config) | |
168 | "Return a list of <pam-services> for lshd with CONFIG." | |
169 | (list (unix-pam-service | |
170 | "lshd" | |
171 | #:allow-empty-passwords? | |
172 | (lsh-configuration-allow-empty-passwords? config)))) | |
173 | ||
174 | (define lsh-service-type | |
175 | (service-type (name 'lsh) | |
176 | (extensions | |
d4053c71 AK |
177 | (list (service-extension shepherd-root-service-type |
178 | lsh-shepherd-service) | |
0adfe95a LC |
179 | (service-extension pam-root-service-type |
180 | lsh-pam-services) | |
181 | (service-extension activation-service-type | |
182 | lsh-activation))))) | |
183 | ||
f33e2d78 LC |
184 | (define* (lsh-service #:key |
185 | (lsh lsh) | |
5833bf33 | 186 | (daemonic? #t) |
f33e2d78 LC |
187 | (host-key "/etc/lsh/host-key") |
188 | (interfaces '()) | |
189 | (port-number 22) | |
190 | (allow-empty-passwords? #f) | |
191 | (root-login? #f) | |
192 | (syslog-output? #t) | |
5833bf33 DP |
193 | (pid-file? #f) |
194 | (pid-file "/var/run/lshd.pid") | |
f33e2d78 LC |
195 | (x11-forwarding? #t) |
196 | (tcp/ip-forwarding? #t) | |
197 | (password-authentication? #t) | |
198 | (public-key-authentication? #t) | |
21cc905a | 199 | (initialize? #t)) |
f33e2d78 LC |
200 | "Run the @command{lshd} program from @var{lsh} to listen on port @var{port-number}. |
201 | @var{host-key} must designate a file containing the host key, and readable | |
202 | only by root. | |
203 | ||
5833bf33 DP |
204 | When @var{daemonic?} is true, @command{lshd} will detach from the |
205 | controlling terminal and log its output to syslogd, unless one sets | |
206 | @var{syslog-output?} to false. Obviously, it also makes lsh-service | |
207 | depend on existence of syslogd service. When @var{pid-file?} is true, | |
208 | @command{lshd} writes its PID to the file called @var{pid-file}. | |
209 | ||
f33e2d78 LC |
210 | When @var{initialize?} is true, automatically create the seed and host key |
211 | upon service activation if they do not exist yet. This may take long and | |
212 | require interaction. | |
213 | ||
20dd519c LC |
214 | When @var{initialize?} is false, it is up to the user to initialize the |
215 | randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create | |
216 | a key pair with the private key stored in file @var{host-key} (@pxref{lshd | |
217 | basics,,, lsh, LSH Manual}). | |
218 | ||
f33e2d78 LC |
219 | When @var{interfaces} is empty, lshd listens for connections on all the |
220 | network interfaces; otherwise, @var{interfaces} must be a list of host names | |
221 | or addresses. | |
222 | ||
20dd519c LC |
223 | @var{allow-empty-passwords?} specifies whether to accept log-ins with empty |
224 | passwords, and @var{root-login?} specifies whether to accept log-ins as | |
f33e2d78 LC |
225 | root. |
226 | ||
227 | The other options should be self-descriptive." | |
0adfe95a LC |
228 | (service lsh-service-type |
229 | (lsh-configuration (lsh lsh) (daemonic? daemonic?) | |
230 | (host-key host-key) (interfaces interfaces) | |
231 | (port-number port-number) | |
232 | (allow-empty-passwords? allow-empty-passwords?) | |
233 | (root-login? root-login?) | |
234 | (syslog-output? syslog-output?) | |
235 | (pid-file? pid-file?) (pid-file pid-file) | |
236 | (x11-forwarding? x11-forwarding?) | |
237 | (tcp/ip-forwarding? tcp/ip-forwarding?) | |
238 | (password-authentication? | |
239 | password-authentication?) | |
240 | (public-key-authentication? | |
241 | public-key-authentication?) | |
242 | (initialize? initialize?)))) | |
f33e2d78 | 243 | |
71b0601a DC |
244 | \f |
245 | ;;; | |
246 | ;;; Dropbear. | |
247 | ;;; | |
248 | ||
249 | (define-record-type* <dropbear-configuration> | |
250 | dropbear-configuration make-dropbear-configuration | |
251 | dropbear-configuration? | |
252 | (dropbear dropbear-configuration-dropbear | |
253 | (default dropbear)) | |
254 | (port-number dropbear-configuration-port-number | |
255 | (default 22)) | |
256 | (syslog-output? dropbear-configuration-syslog-output? | |
257 | (default #t)) | |
258 | (pid-file dropbear-configuration-pid-file | |
259 | (default "/var/run/dropbear.pid")) | |
260 | (root-login? dropbear-configuration-root-login? | |
261 | (default #f)) | |
262 | (allow-empty-passwords? dropbear-configuration-allow-empty-passwords? | |
263 | (default #f)) | |
264 | (password-authentication? dropbear-configuration-password-authentication? | |
265 | (default #t))) | |
266 | ||
267 | (define (dropbear-activation config) | |
268 | "Return the activation gexp for CONFIG." | |
269 | #~(begin | |
270 | (mkdir-p "/etc/dropbear"))) | |
271 | ||
272 | (define (dropbear-shepherd-service config) | |
273 | "Return a <shepherd-service> for dropbear with CONFIG." | |
274 | (define dropbear | |
275 | (dropbear-configuration-dropbear config)) | |
276 | ||
277 | (define pid-file | |
278 | (dropbear-configuration-pid-file config)) | |
279 | ||
280 | (define dropbear-command | |
281 | #~(list (string-append #$dropbear "/sbin/dropbear") | |
282 | ||
283 | ;; '-R' allows host keys to be automatically generated upon first | |
284 | ;; connection, at a time when /dev/urandom is more likely securely | |
285 | ;; seeded. | |
286 | "-F" "-R" | |
287 | ||
288 | "-p" #$(number->string (dropbear-configuration-port-number config)) | |
289 | "-P" #$pid-file | |
290 | #$@(if (dropbear-configuration-syslog-output? config) '() '("-E")) | |
291 | #$@(if (dropbear-configuration-root-login? config) '() '("-w")) | |
292 | #$@(if (dropbear-configuration-password-authentication? config) | |
293 | '() | |
294 | '("-s" "-g")) | |
295 | #$@(if (dropbear-configuration-allow-empty-passwords? config) | |
296 | '("-B") | |
297 | '()))) | |
298 | ||
299 | (define requires | |
300 | (if (dropbear-configuration-syslog-output? config) | |
301 | '(networking syslogd) '(networking))) | |
302 | ||
303 | (list (shepherd-service | |
304 | (documentation "Dropbear SSH server.") | |
305 | (requirement requires) | |
306 | (provision '(ssh-daemon)) | |
307 | (start #~(make-forkexec-constructor #$dropbear-command | |
308 | #:pid-file #$pid-file)) | |
309 | (stop #~(make-kill-destructor))))) | |
310 | ||
311 | (define dropbear-service-type | |
312 | (service-type (name 'dropbear) | |
313 | (extensions | |
314 | (list (service-extension shepherd-root-service-type | |
315 | dropbear-shepherd-service) | |
316 | (service-extension activation-service-type | |
317 | dropbear-activation))))) | |
318 | ||
319 | (define* (dropbear-service #:optional (config (dropbear-configuration))) | |
320 | "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH | |
321 | daemon} with the given @var{config}, a @code{<dropbear-configuration>} | |
322 | object." | |
323 | (service dropbear-service-type config)) | |
324 | ||
f33e2d78 | 325 | ;;; ssh.scm ends here |