services: openssh: Add 'subsystems' option.
[jackhill/guix/guix.git] / gnu / services / ssh.scm
index 6da612d..b7f9887 100644 (file)
@@ -2,6 +2,7 @@
 ;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2016 David Craven <david@craven.ch>
 ;;; Copyright © 2016 Julien Lepiller <julien@lepiller.eu>
+;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -37,7 +38,6 @@
             openssh-configuration
             openssh-configuration?
             openssh-service-type
-            openssh-service
 
             dropbear-configuration
             dropbear-configuration?
@@ -260,24 +260,42 @@ The other options should be self-descriptive."
 (define-record-type* <openssh-configuration>
   openssh-configuration make-openssh-configuration
   openssh-configuration?
+  ;; <package>
+  (openssh               openssh-configuration-openssh
+                         (default openssh))
+  ;; string
   (pid-file              openssh-configuration-pid-file
                          (default "/var/run/sshd.pid"))
-  (port-number           openssh-configuration-port-number ;integer
+  ;; integer
+  (port-number           openssh-configuration-port-number
                          (default 22))
-  (permit-root-login     openssh-configuration-permit-root-login ;Boolean | 'without-password
+  ;; Boolean | 'without-password
+  (permit-root-login     openssh-configuration-permit-root-login
                          (default #f))
-  (allow-empty-passwords? openssh-configuration-allow-empty-passwords? ;Boolean
+  ;; Boolean
+  (allow-empty-passwords? openssh-configuration-allow-empty-passwords?
                           (default #f))
-  (password-authentication? openssh-configuration-password-authentication? ;Boolean
+  ;; Boolean
+  (password-authentication? openssh-configuration-password-authentication?
                             (default #t))
+  ;; Boolean
   (public-key-authentication? openssh-configuration-public-key-authentication?
-                              (default #t))                         ;Boolean
-  (rsa-authentication?   openssh-configuration-rsa-authentication?  ;Boolean
-                         (default #t))
-  (x11-forwarding?       openssh-configuration-x11-forwarding? ;Boolean
+                              (default #t))
+  ;; Boolean
+  (x11-forwarding?       openssh-configuration-x11-forwarding?
                          (default #f))
-  (protocol-number       openssh-configuration-protocol-number ;integer
-                         (default 2)))
+  ;; Boolean
+  (challenge-response-authentication? openssh-challenge-response-authentication?
+                                      (default #f))
+  ;; Boolean
+  (use-pam?              openssh-configuration-use-pam?
+                         (default #t))
+  ;; Boolean
+  (print-last-log?       openssh-configuration-print-last-log?
+                         (default #t))
+  ;; list of two-element lists
+  (subsystems            openssh-configuration-subsystems
+                         (default '(("sftp" "internal-sftp")))))
 
 (define %openssh-accounts
   (list (user-group (name "sshd") (system? #t))
@@ -292,47 +310,68 @@ The other options should be self-descriptive."
 (define (openssh-activation config)
   "Return the activation GEXP for CONFIG."
   #~(begin
+      (use-modules (guix build utils))
       (mkdir-p "/etc/ssh")
       (mkdir-p (dirname #$(openssh-configuration-pid-file config)))
 
+      (define (touch file-name)
+        (call-with-output-file file-name (const #t)))
+
+      (let ((lastlog "/var/log/lastlog"))
+        (when #$(openssh-configuration-print-last-log? config)
+          (unless (file-exists? lastlog)
+            (touch lastlog))))
+
       ;; Generate missing host keys.
-      (system* (string-append #$openssh "/bin/ssh-keygen") "-A")))
+      (system* (string-append #$(openssh-configuration-openssh config)
+                              "/bin/ssh-keygen") "-A")))
 
 (define (openssh-config-file config)
   "Return the sshd configuration file corresponding to CONFIG."
   (computed-file
    "sshd_config"
-   #~(call-with-output-file #$output
-       (lambda (port)
-         (display "# Generated by 'openssh-service'.\n" port)
-         (format port "Protocol ~a\n"
-                 #$(if (eq? (openssh-configuration-protocol-number config) 1)
-                       "1" "2"))
-         (format port "Port ~a\n"
-                 #$(number->string (openssh-configuration-port-number config)))
-         (format port "PermitRootLogin ~a\n"
-                 #$(match (openssh-configuration-permit-root-login config)
-                     (#t "yes")
-                     (#f "no")
-                     ('without-password "without-password")))
-         (format port "PermitEmptyPasswords ~a\n"
-                 #$(if (openssh-configuration-allow-empty-passwords? config)
-                       "yes" "no"))
-         (format port "PasswordAuthentication ~a\n"
-                 #$(if (openssh-configuration-password-authentication? config)
-                       "yes" "no"))
-         (format port "PubkeyAuthentication ~a\n"
-                 #$(if (openssh-configuration-public-key-authentication? config)
-                       "yes" "no"))
-         (format port "RSAAuthentication ~a\n"
-                 #$(if (openssh-configuration-rsa-authentication? config)
-                       "yes" "no"))
-         (format port "X11Forwarding ~a\n"
-                 #$(if (openssh-configuration-x11-forwarding? config)
-                       "yes" "no"))
-         (format port "PidFile ~a\n"
-                 #$(openssh-configuration-pid-file config))
-         #t))))
+   #~(begin
+       (use-modules (ice-9 match))
+       (call-with-output-file #$output
+         (lambda (port)
+           (display "# Generated by 'openssh-service'.\n" port)
+           (format port "Port ~a\n"
+                   #$(number->string
+                      (openssh-configuration-port-number config)))
+           (format port "PermitRootLogin ~a\n"
+                   #$(match (openssh-configuration-permit-root-login config)
+                       (#t "yes")
+                       (#f "no")
+                       ('without-password "without-password")))
+           (format port "PermitEmptyPasswords ~a\n"
+                   #$(if (openssh-configuration-allow-empty-passwords? config)
+                         "yes" "no"))
+           (format port "PasswordAuthentication ~a\n"
+                   #$(if (openssh-configuration-password-authentication? config)
+                         "yes" "no"))
+           (format port "PubkeyAuthentication ~a\n"
+                   #$(if (openssh-configuration-public-key-authentication?
+                          config)
+                         "yes" "no"))
+           (format port "X11Forwarding ~a\n"
+                   #$(if (openssh-configuration-x11-forwarding? config)
+                         "yes" "no"))
+           (format port "PidFile ~a\n"
+                   #$(openssh-configuration-pid-file config))
+           (format port "ChallengeResponseAuthentication ~a\n"
+                   #$(if (openssh-challenge-response-authentication? config)
+                         "yes" "no"))
+           (format port "UsePAM ~a\n"
+                   #$(if (openssh-configuration-use-pam? config)
+                         "yes" "no"))
+           (format port "PrintLastLog ~a\n"
+                   #$(if (openssh-configuration-print-last-log? config)
+                         "yes" "no"))
+           (for-each
+            (match-lambda
+              ((name command) (format port "Subsystem\t~a\t~a\n" name command)))
+            '#$(openssh-configuration-subsystems config))
+           #t)))))
 
 (define (openssh-shepherd-service config)
   "Return a <shepherd-service> for openssh with CONFIG."
@@ -341,7 +380,7 @@ The other options should be self-descriptive."
     (openssh-configuration-pid-file config))
 
   (define openssh-command
-    #~(list (string-append #$openssh "/sbin/sshd")
+    #~(list (string-append #$(openssh-configuration-openssh config) "/sbin/sshd")
             "-D" "-f" #$(openssh-config-file config)))
 
   (list (shepherd-service
@@ -352,11 +391,20 @@ The other options should be self-descriptive."
                                              #:pid-file #$pid-file))
          (stop #~(make-kill-destructor)))))
 
+(define (openssh-pam-services config)
+  "Return a list of <pam-services> for sshd with CONFIG."
+  (list (unix-pam-service
+         "sshd"
+         #:allow-empty-passwords?
+         (openssh-configuration-allow-empty-passwords? config))))
+
 (define openssh-service-type
   (service-type (name 'openssh)
                 (extensions
                  (list (service-extension shepherd-root-service-type
                                           openssh-shepherd-service)
+                       (service-extension pam-root-service-type
+                                          openssh-pam-services)
                        (service-extension activation-service-type
                                           openssh-activation)
                        (service-extension account-service-type
@@ -388,6 +436,7 @@ The other options should be self-descriptive."
 (define (dropbear-activation config)
   "Return the activation gexp for CONFIG."
   #~(begin
+      (use-modules (guix build utils))
       (mkdir-p "/etc/dropbear")))
 
 (define (dropbear-shepherd-service config)