| 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> |
| 4 | ;;; Copyright © 2016 Julien Lepiller <julien@lepiller.eu> |
| 5 | ;;; |
| 6 | ;;; This file is part of GNU Guix. |
| 7 | ;;; |
| 8 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
| 9 | ;;; under the terms of the GNU General Public License as published by |
| 10 | ;;; the Free Software Foundation; either version 3 of the License, or (at |
| 11 | ;;; your option) any later version. |
| 12 | ;;; |
| 13 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
| 14 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | ;;; GNU General Public License for more details. |
| 17 | ;;; |
| 18 | ;;; You should have received a copy of the GNU General Public License |
| 19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | (define-module (gnu services ssh) |
| 22 | #:use-module (gnu packages ssh) |
| 23 | #:use-module (gnu packages admin) |
| 24 | #:use-module (gnu services) |
| 25 | #:use-module (gnu services shepherd) |
| 26 | #:use-module (gnu system pam) |
| 27 | #:use-module (gnu system shadow) |
| 28 | #:use-module (guix gexp) |
| 29 | #:use-module (guix records) |
| 30 | #:use-module (srfi srfi-26) |
| 31 | #:use-module (ice-9 match) |
| 32 | #:export (lsh-configuration |
| 33 | lsh-configuration? |
| 34 | lsh-service |
| 35 | lsh-service-type |
| 36 | |
| 37 | openssh-configuration |
| 38 | openssh-configuration? |
| 39 | openssh-service-type |
| 40 | openssh-service |
| 41 | |
| 42 | dropbear-configuration |
| 43 | dropbear-configuration? |
| 44 | dropbear-service-type |
| 45 | dropbear-service)) |
| 46 | |
| 47 | ;;; Commentary: |
| 48 | ;;; |
| 49 | ;;; This module implements secure shell (SSH) services. |
| 50 | ;;; |
| 51 | ;;; Code: |
| 52 | |
| 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 | |
| 73 | (define %yarrow-seed |
| 74 | "/var/spool/lsh/yarrow-seed-file") |
| 75 | |
| 76 | (define (lsh-initialization lsh host-key) |
| 77 | "Return the gexp to initialize the LSH service for HOST-KEY." |
| 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 | |
| 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 | |
| 123 | (define (lsh-shepherd-service config) |
| 124 | "Return a <shepherd-service> for lsh with CONFIG." |
| 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 |
| 133 | (cons (file-append lsh "/sbin/lshd") |
| 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 | '() |
| 163 | (map (cut string-append "--interface=" <>) |
| 164 | interfaces))))) |
| 165 | |
| 166 | (define requires |
| 167 | (if (and daemonic? (lsh-configuration-syslog-output? config)) |
| 168 | '(networking syslogd) |
| 169 | '(networking))) |
| 170 | |
| 171 | (list (shepherd-service |
| 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 |
| 188 | (list (service-extension shepherd-root-service-type |
| 189 | lsh-shepherd-service) |
| 190 | (service-extension pam-root-service-type |
| 191 | lsh-pam-services) |
| 192 | (service-extension activation-service-type |
| 193 | lsh-activation))))) |
| 194 | |
| 195 | (define* (lsh-service #:key |
| 196 | (lsh lsh) |
| 197 | (daemonic? #t) |
| 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) |
| 204 | (pid-file? #f) |
| 205 | (pid-file "/var/run/lshd.pid") |
| 206 | (x11-forwarding? #t) |
| 207 | (tcp/ip-forwarding? #t) |
| 208 | (password-authentication? #t) |
| 209 | (public-key-authentication? #t) |
| 210 | (initialize? #t)) |
| 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 |
| 213 | only by root. |
| 214 | |
| 215 | When @var{daemonic?} is true, @command{lshd} will detach from the |
| 216 | controlling terminal and log its output to syslogd, unless one sets |
| 217 | @var{syslog-output?} to false. Obviously, it also makes lsh-service |
| 218 | depend 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 | |
| 221 | When @var{initialize?} is true, automatically create the seed and host key |
| 222 | upon service activation if they do not exist yet. This may take long and |
| 223 | require interaction. |
| 224 | |
| 225 | When @var{initialize?} is false, it is up to the user to initialize the |
| 226 | randomness generator (@pxref{lsh-make-seed,,, lsh, LSH Manual}), and to create |
| 227 | a key pair with the private key stored in file @var{host-key} (@pxref{lshd |
| 228 | basics,,, lsh, LSH Manual}). |
| 229 | |
| 230 | When @var{interfaces} is empty, lshd listens for connections on all the |
| 231 | network interfaces; otherwise, @var{interfaces} must be a list of host names |
| 232 | or addresses. |
| 233 | |
| 234 | @var{allow-empty-passwords?} specifies whether to accept log-ins with empty |
| 235 | passwords, and @var{root-login?} specifies whether to accept log-ins as |
| 236 | root. |
| 237 | |
| 238 | The other options should be self-descriptive." |
| 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?)))) |
| 254 | |
| 255 | \f |
| 256 | ;;; |
| 257 | ;;; OpenSSH. |
| 258 | ;;; |
| 259 | |
| 260 | (define-record-type* <openssh-configuration> |
| 261 | openssh-configuration make-openssh-configuration |
| 262 | openssh-configuration? |
| 263 | (pid-file openssh-configuration-pid-file |
| 264 | (default "/var/run/sshd.pid")) |
| 265 | (port-number openssh-configuration-port-number ;integer |
| 266 | (default 22)) |
| 267 | (permit-root-login openssh-configuration-permit-root-login ;Boolean | 'without-password |
| 268 | (default #f)) |
| 269 | (allow-empty-passwords? openssh-configuration-allow-empty-passwords? ;Boolean |
| 270 | (default #f)) |
| 271 | (password-authentication? openssh-configuration-password-authentication? ;Boolean |
| 272 | (default #t)) |
| 273 | (public-key-authentication? openssh-configuration-public-key-authentication? |
| 274 | (default #t)) ;Boolean |
| 275 | (rsa-authentication? openssh-configuration-rsa-authentication? ;Boolean |
| 276 | (default #t)) |
| 277 | (x11-forwarding? openssh-configuration-x11-forwarding? ;Boolean |
| 278 | (default #f)) |
| 279 | (protocol-number openssh-configuration-protocol-number ;integer |
| 280 | (default 2))) |
| 281 | |
| 282 | (define %openssh-accounts |
| 283 | (list (user-group (name "sshd") (system? #t)) |
| 284 | (user-account |
| 285 | (name "sshd") |
| 286 | (group "sshd") |
| 287 | (system? #t) |
| 288 | (comment "sshd privilege separation user") |
| 289 | (home-directory "/var/run/sshd") |
| 290 | (shell #~(string-append #$shadow "/sbin/nologin"))))) |
| 291 | |
| 292 | (define (openssh-activation config) |
| 293 | "Return the activation GEXP for CONFIG." |
| 294 | #~(begin |
| 295 | (mkdir-p "/etc/ssh") |
| 296 | (mkdir-p (dirname #$(openssh-configuration-pid-file config))) |
| 297 | |
| 298 | ;; Generate missing host keys. |
| 299 | (system* (string-append #$openssh "/bin/ssh-keygen") "-A"))) |
| 300 | |
| 301 | (define (openssh-config-file config) |
| 302 | "Return the sshd configuration file corresponding to CONFIG." |
| 303 | (computed-file |
| 304 | "sshd_config" |
| 305 | #~(call-with-output-file #$output |
| 306 | (lambda (port) |
| 307 | (display "# Generated by 'openssh-service'.\n" port) |
| 308 | (format port "Protocol ~a\n" |
| 309 | #$(if (eq? (openssh-configuration-protocol-number config) 1) |
| 310 | "1" "2")) |
| 311 | (format port "Port ~a\n" |
| 312 | #$(number->string (openssh-configuration-port-number config))) |
| 313 | (format port "PermitRootLogin ~a\n" |
| 314 | #$(match (openssh-configuration-permit-root-login config) |
| 315 | (#t "yes") |
| 316 | (#f "no") |
| 317 | ('without-password "without-password"))) |
| 318 | (format port "PermitEmptyPasswords ~a\n" |
| 319 | #$(if (openssh-configuration-allow-empty-passwords? config) |
| 320 | "yes" "no")) |
| 321 | (format port "PasswordAuthentication ~a\n" |
| 322 | #$(if (openssh-configuration-password-authentication? config) |
| 323 | "yes" "no")) |
| 324 | (format port "PubkeyAuthentication ~a\n" |
| 325 | #$(if (openssh-configuration-public-key-authentication? config) |
| 326 | "yes" "no")) |
| 327 | (format port "RSAAuthentication ~a\n" |
| 328 | #$(if (openssh-configuration-rsa-authentication? config) |
| 329 | "yes" "no")) |
| 330 | (format port "X11Forwarding ~a\n" |
| 331 | #$(if (openssh-configuration-x11-forwarding? config) |
| 332 | "yes" "no")) |
| 333 | (format port "PidFile ~a\n" |
| 334 | #$(openssh-configuration-pid-file config)) |
| 335 | #t)))) |
| 336 | |
| 337 | (define (openssh-shepherd-service config) |
| 338 | "Return a <shepherd-service> for openssh with CONFIG." |
| 339 | |
| 340 | (define pid-file |
| 341 | (openssh-configuration-pid-file config)) |
| 342 | |
| 343 | (define openssh-command |
| 344 | #~(list (string-append #$openssh "/sbin/sshd") |
| 345 | "-D" "-f" #$(openssh-config-file config))) |
| 346 | |
| 347 | (list (shepherd-service |
| 348 | (documentation "OpenSSH server.") |
| 349 | (requirement '(networking syslogd)) |
| 350 | (provision '(ssh-daemon)) |
| 351 | (start #~(make-forkexec-constructor #$openssh-command |
| 352 | #:pid-file #$pid-file)) |
| 353 | (stop #~(make-kill-destructor))))) |
| 354 | |
| 355 | (define openssh-service-type |
| 356 | (service-type (name 'openssh) |
| 357 | (extensions |
| 358 | (list (service-extension shepherd-root-service-type |
| 359 | openssh-shepherd-service) |
| 360 | (service-extension activation-service-type |
| 361 | openssh-activation) |
| 362 | (service-extension account-service-type |
| 363 | (const %openssh-accounts)))))) |
| 364 | |
| 365 | \f |
| 366 | ;;; |
| 367 | ;;; Dropbear. |
| 368 | ;;; |
| 369 | |
| 370 | (define-record-type* <dropbear-configuration> |
| 371 | dropbear-configuration make-dropbear-configuration |
| 372 | dropbear-configuration? |
| 373 | (dropbear dropbear-configuration-dropbear |
| 374 | (default dropbear)) |
| 375 | (port-number dropbear-configuration-port-number |
| 376 | (default 22)) |
| 377 | (syslog-output? dropbear-configuration-syslog-output? |
| 378 | (default #t)) |
| 379 | (pid-file dropbear-configuration-pid-file |
| 380 | (default "/var/run/dropbear.pid")) |
| 381 | (root-login? dropbear-configuration-root-login? |
| 382 | (default #f)) |
| 383 | (allow-empty-passwords? dropbear-configuration-allow-empty-passwords? |
| 384 | (default #f)) |
| 385 | (password-authentication? dropbear-configuration-password-authentication? |
| 386 | (default #t))) |
| 387 | |
| 388 | (define (dropbear-activation config) |
| 389 | "Return the activation gexp for CONFIG." |
| 390 | #~(begin |
| 391 | (mkdir-p "/etc/dropbear"))) |
| 392 | |
| 393 | (define (dropbear-shepherd-service config) |
| 394 | "Return a <shepherd-service> for dropbear with CONFIG." |
| 395 | (define dropbear |
| 396 | (dropbear-configuration-dropbear config)) |
| 397 | |
| 398 | (define pid-file |
| 399 | (dropbear-configuration-pid-file config)) |
| 400 | |
| 401 | (define dropbear-command |
| 402 | #~(list (string-append #$dropbear "/sbin/dropbear") |
| 403 | |
| 404 | ;; '-R' allows host keys to be automatically generated upon first |
| 405 | ;; connection, at a time when /dev/urandom is more likely securely |
| 406 | ;; seeded. |
| 407 | "-F" "-R" |
| 408 | |
| 409 | "-p" #$(number->string (dropbear-configuration-port-number config)) |
| 410 | "-P" #$pid-file |
| 411 | #$@(if (dropbear-configuration-syslog-output? config) '() '("-E")) |
| 412 | #$@(if (dropbear-configuration-root-login? config) '() '("-w")) |
| 413 | #$@(if (dropbear-configuration-password-authentication? config) |
| 414 | '() |
| 415 | '("-s" "-g")) |
| 416 | #$@(if (dropbear-configuration-allow-empty-passwords? config) |
| 417 | '("-B") |
| 418 | '()))) |
| 419 | |
| 420 | (define requires |
| 421 | (if (dropbear-configuration-syslog-output? config) |
| 422 | '(networking syslogd) '(networking))) |
| 423 | |
| 424 | (list (shepherd-service |
| 425 | (documentation "Dropbear SSH server.") |
| 426 | (requirement requires) |
| 427 | (provision '(ssh-daemon)) |
| 428 | (start #~(make-forkexec-constructor #$dropbear-command |
| 429 | #:pid-file #$pid-file)) |
| 430 | (stop #~(make-kill-destructor))))) |
| 431 | |
| 432 | (define dropbear-service-type |
| 433 | (service-type (name 'dropbear) |
| 434 | (extensions |
| 435 | (list (service-extension shepherd-root-service-type |
| 436 | dropbear-shepherd-service) |
| 437 | (service-extension activation-service-type |
| 438 | dropbear-activation))))) |
| 439 | |
| 440 | (define* (dropbear-service #:optional (config (dropbear-configuration))) |
| 441 | "Run the @uref{https://matt.ucc.asn.au/dropbear/dropbear.html,Dropbear SSH |
| 442 | daemon} with the given @var{config}, a @code{<dropbear-configuration>} |
| 443 | object." |
| 444 | (service dropbear-service-type config)) |
| 445 | |
| 446 | ;;; ssh.scm ends here |