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