aboutsummaryrefslogtreecommitdiff
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu installer newt user)
  #:use-module (gnu installer user)
  #:use-module (gnu installer newt page)
  #:use-module (gnu installer newt utils)
  #:use-module (gnu installer utils)
  #:use-module (guix i18n)
  #:use-module (newt)
  #:use-module (ice-9 match)
  #:use-module (ice-9 receive)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:export (run-user-page))

(define* (run-user-add-page #:key (name "") (real-name "")
                            (home-directory ""))
  "Run a form to enter the user name, home directory, and password.  Use NAME,
REAL-NAME, and HOME-DIRECTORY as the initial values in the form."
  (define (pad-label label)
    (string-pad-right label 25))

  (define (root-account? name)
    (string=? name "root"))

  (let* ((label-name
          (make-label -1 -1 (pad-label (G_ "Name"))))
         (label-real-name
          (make-label -1 -1 (pad-label (G_ "Real name"))))
         (label-home-directory
          (make-label -1 -1 (pad-label (G_ "Home directory"))))
         (label-password
          (make-label -1 -1 (pad-label (G_ "Password"))))
         (entry-width 35)
         (entry-name (make-entry -1 -1 entry-width
                                 #:initial-value name))
         (entry-real-name (make-entry -1 -1 entry-width
                                      #:initial-value real-name))
         (entry-home-directory (make-entry -1 -1 entry-width
                                           #:initial-value home-directory))
         (password-visible-cb
          (make-checkbox -1 -1 (G_ "Show") #\space "x "))
         (entry-password (make-entry -1 -1 entry-width
                                     #:flags (logior FLAG-PASSWORD
                                                     FLAG-SCROLL)))
         (entry-grid (make-grid 3 5))
         (button-grid (make-grid 1 1))
         (ok-button (make-button -1 -1 (G_ "OK")))
         (grid (make-grid 1 2))
         (title (G_ "User creation"))
         (set-entry-grid-field
          (cut set-grid-field entry-grid <> <> GRID-ELEMENT-COMPONENT <>))
         (form (make-form)))

    (set-entry-grid-field 0 0 label-name)
    (set-entry-grid-field 1 0 entry-name)
    (set-entry-grid-field 0 1 label-real-name)
    (set-entry-grid-field 1 1 entry-real-name)
    (set-entry-grid-field 0 2 label-home-directory)
    (set-entry-grid-field 1 2 entry-home-directory)
    (set-entry-grid-field 0 3 label-password)
    (set-entry-grid-field 1 3 entry-password)

    (set-grid-field entry-grid
                    2 3
                    GRID-ELEMENT-COMPONENT
                    password-visible-cb
                    #:pad-left 1)

    (set-grid-field button-grid 0 0 GRID-ELEMENT-COMPONENT ok-button)

    (add-component-callback
     entry-name
     (lambda ()
       (set-entry-text entry-home-directory
                       (string-append "/home/" (entry-value entry-name)))

       (when (string-null? (entry-value entry-real-name))
         (set-entry-text entry-real-name
                         (string-titlecase (entry-value entry-name))))))

    (add-component-callback
     password-visible-cb
     (lambda ()
       (set-entry-flags entry-password
                        FLAG-PASSWORD
                        FLAG-ROLE-TOGGLE)))

    (add-components-to-form form
                            label-name label-real-name
                            label-home-directory label-password
                            entry-name entry-real-name
                            entry-home-directory entry-password
                            password-visible-cb
                            ok-button)

    (make-wrapped-grid-window (vertically-stacked-grid
                               GRID-ELEMENT-SUBGRID entry-grid
                               GRID-ELEMENT-SUBGRID button-grid)
                              title)

    (let ((error-empty-field-page
           (lambda ()
             (run-error-page (G_ "Empty inputs are not allowed.")
                             (G_ "Empty input"))))
          (error-root-page
           (lambda ()
             (run-error-page (G_ "Root account is automatically created.")
                             (G_ "Root account")))))
      (receive (exit-reason argument)
          (run-form form)
        (dynamic-wind
          (const #t)
          (lambda ()
            (when (eq? exit-reason 'exit-component)
              (cond
               ((components=? argument ok-button)
                (let ((name           (entry-value entry-name))
                      (real-name      (entry-value entry-real-name))
                      (home-directory (entry-value entry-home-directory))
                      (password       (entry-value entry-password)))
                  (cond
                   ;; Empty field.
                   ((or (string=? name "")
                        (string=? home-directory ""))
                    (error-empty-field-page)
                    (run-user-add-page))
                   ;; Reject root account.
                   ((root-account? name)
                    (error-root-page)
                    (run-user-add-page))
                   (else
                    (let ((password (confirm-password password)))
                      (if password
                          (user
                           (name name)
                           (real-name real-name)
                           (home-directory home-directory)
                           (password (make-secret password)))
                          (run-user-add-page #:name name
                                             #:real-name real-name
                                             #:home-directory
                                             home-directory))))))))))
          (lambda ()
            (destroy-form-and-pop form)))))))

(define* (confirm-password password #:optional (try-again (const #f)))
  "Ask the user to confirm PASSWORD, a possibly empty string.  Call TRY-AGAIN,
a thunk, if the confirmation doesn't match PASSWORD, and return its result."
  (define confirmation
    (run-input-page (G_ "Please confirm the password.")
                    (G_ "Password confirmation required")
                    #:allow-empty-input? #t
                    #:input-visibility-checkbox? #t))

  (if (string=? password confirmation)
      password
      (begin
        (run-error-page
         (G_ "Password mismatch, please try again.")
         (G_ "Password error"))
        (try-again))))

(define (run-root-password-page)
  (define password
    ;; TRANSLATORS: Leave "root" untranslated: it refers to the name of the
    ;; system administrator account.
    (run-input-page (G_ "Please choose a password for the system \
administrator (\"root\").")
                    (G_ "System administrator password")
                    #:input-visibility-checkbox? #t))

  (confirm-password password run-root-password-page))

(define (run-user-page)
  (define (run users)
    (let* ((listbox (make-listbox
                     -1 -1 10
                     (logior FLAG-SCROLL FLAG-BORDER)))
           (info-textbox
            (make-reflowed-textbox
             -1 -1
             (G_ "Please add at least one user to system\
 using the 'Add' button.")
             40 #:flags FLAG-BORDER))
           (add-button (make-compact-button -1 -1 (G_ "Add")))
           (del-button (make-compact-button -1 -1 (G_ "Delete")))
           (listbox-button-grid
            (apply
             vertically-stacked-grid
             GRID-ELEMENT-COMPONENT add-button
             `(,@(if (null? users)
                     '()
                     (list GRID-ELEMENT-COMPONENT del-button)))))
           (ok-button (make-button -1 -1 (G_ "OK")))
           (exit-button (make-button -1 -1 (G_ "Exit")))
           (title (G_ "User creation"))
           (grid
            (vertically-stacked-grid
             GRID-ELEMENT-COMPONENT info-textbox
             GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
                                   GRID-ELEMENT-COMPONENT listbox
                                   GRID-ELEMENT-SUBGRID listbox-button-grid)
             GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
                                   GRID-ELEMENT-COMPONENT ok-button
                                   GRID-ELEMENT-COMPONENT exit-button)))
           (sorted-users (sort users (lambda (a b)
                                       (string<= (user-name a)
                                                 (user-name b)))))
           (listbox-elements
            (map
             (lambda (user)
               `((key . ,(append-entry-to-listbox listbox
                                                  (user-name user)))
                 (user . ,user)))
             sorted-users))
           (form (make-form)))


      (add-form-to-grid grid form #t)
      (make-wrapped-grid-window grid title)
      (if (null? users)
          (set-current-component form add-button)
          (set-current-component form ok-button))

      (receive (exit-reason argument)
          (run-form-with-clients form '(add-users))
        (dynamic-wind
          (const #t)
          (lambda ()
            (match exit-reason
              ('exit-component
               (cond
                ((components=? argument add-button)
                 (run (cons (run-user-add-page) users)))
                ((components=? argument del-button)
                 (let* ((current-user-key (current-listbox-entry listbox))
                        (users
                         (map (cut assoc-ref <> 'user)
                              (remove (lambda (element)
                                        (equal? (assoc-ref element 'key)
                                                current-user-key))
                                      listbox-elements))))
                   (run users)))
                ((components=? argument ok-button)
                 (when (null? users)
                   (run-error-page (G_ "Please create at least one user.")
                                   (G_ "No user"))
                   (run users))
                 (reverse users))
                ((components=? argument exit-button)
                 (abort-to-prompt 'installer-step 'abort))))
              ('exit-fd-ready
               ;; Read the complete user list at once.
               (match argument
                 ((('user ('name names) ('real-name real-names)
                          ('home-directory homes) ('password passwords))
                   ..1)
                  (map (lambda (name real-name home password)
                         (user (name name) (real-name real-name)
                               (home-directory home)
                               (password (make-secret password))))
                       names real-names homes passwords))))))
          (lambda ()
            (destroy-form-and-pop form))))))

  ;; Add a "root" user simply to convey the root password.
  (cons (user (name "root")
              (home-directory "/root")
              (password (make-secret (run-root-password-page))))
        (run '())))
e using 'service'. (<bitlbee-configuration>): New record type. (bitlbee-dmd-service): New procedure. (%bitlbee-accounts, %bitlbee-activation, bitlbee-service-type): New variables. (bitlbee-service): Rewrite using 'service'. (%wicd-activation): New variable. (wicd-dmd-service): New procedure. (wicd-service-type): New variable. (wicd-service): Rewrite using 'service'. * gnu/services/ssh.scm (<lsh-configuration>): New record type. (activation): Rename to... (lsh-initialization): ... this. (lsh-activation, lsh-dmd-service, lsh-pam-services): New procedures. (lsh-service-type): New variable. (lsh-service): Rewrite using 'service' and 'lsh-configuration'. * gnu/services/web.scm (<nginx-configuration>): New record type. (%nginx-accounts): New variable. (nginx-activation, nginx-dmd-service): New procedures. (nginx-service-type): New variable. (nginx-service): Rewrite using 'service' and 'nginx-configuration'. * gnu/services/xorg.scm (<slim-configuration>): New record type. (slim-pam-service, slim-dmd-service): New procedures. (slim-service-type): New variable. (slim-service): Rewrite using 'service' and 'slim-configuration'. * gnu/system.scm (file-union): Remove. (other-file-system-services): Adjust to new 'file-system-service' signature. (essential-services): Add #:container? parameter. Add %DMD-ROOT-SERVICE, %ACTIVATION-SERVICE, and calls to 'pam-root-service', 'account-service', 'operating-system-etc-service', and a SETUID-PROGRAM-SERVICE instance. (operating-system-services): Pass #:container? to 'essential-services. (etc-directory): Remove. (operating-system-etc-service): New procedure. Rewrite as a call to 'etc-service'. (operating-system-accounts): Change to not return accounts required by services. (operating-system-etc-directory): Rewrite as a call to 'fold-services' and 'etc-directory'. (user-group->gexp, user-account->gexp, modprobe-wrapper): Remove. (operating-system-activation-script): Rewrite as a call to 'fold-services' and 'activation-service->script'. (operating-system-boot-script): Likewise. (operating-system-derivation): Add call to 'lower-object'. (emacs-site-file, emacs-site-directory, shells-file): Change to use 'computed-file' and 'scheme-file' instead of the monadic procedures. * gnu/system/install.scm (cow-store-service-type): New variable. (cow-store-service): Rewrite using 'service'. (/etc/configuration-files): New procedure. (configuration-template-service-type, %configuration-template-service): New variables. (configuration-template-service): Remove. (installation-services): Adjust accordingly. Adjust argument to 'guix-service'. * gnu/system/linux.scm (/etc-entry, pam-root-service): New procedures. (pam-root-service-type): New variable. * gnu/system/shadow.scm (user-group->gexp, user-account->gexp, account-activation, etc-skel, account-service): New procedures. (account-service-type): New variable. * tests/services.scm: New file. * doc/guix.texi (Base Services, Desktop Services): Adjust accordingly. (Defining Services): Rewrite. * doc/images/service-graph.dot: New file. * doc.am (DOT_FILES): Add it. * po/guix/POTFILES.in: Add gnu/services.scm. Ludovic Courtès 2015-10-10system: Make service procedures non-monadic....* gnu/services/avahi.scm (configuration-file): Use 'plain-file' instead of 'text-file'. (avahi-service): Turn into a regular procedure that returns a <service>. * gnu/services/base.scm (root-file-system-service, file-system-service, user-unmount-service, user-processes-service, host-name-service, console-keymap-service, console-font-service, mingetty-service, nscd.conf-file, nscd-service): Likewise. (%default-syslog.conf): New variable. (syslog-service): Use it. Turn into a regular procedure. (guix-service, udev-rules-union, kvm-udev-rule, udev-service, device-mapping-service, swap-service): Likewise. * gnu/services/databases.scm (%default-postgres-hba, %default-postgres-ident): Use 'plain-file' instead of 'text-file'. (%default-postgres-config): Use 'mixed-text-file' instead of 'text-file*'. (postgresql-service): Use 'program-file' instead of 'gexp->script'. Turn into a regular procedure. * gnu/services/desktop.scm (dbus-configuration-directory): Use 'computed-file' instead of 'gexp->derivation'. (upower-configuration-file, geoclue-configuration-file, elogind-configuration-file): Use 'plain-file' instead of 'text-file'. (dbus-service, upower-service, colord-service, geoclue-service, polkit-service, elogind-service): Turn into regular procedures. (%desktop-services): Remove use of 'mlet' when iterating on %BASE-SERVICES. * gnu/services/lirc.scm (lirc-service): Turn into a regular procedure. * gnu/services/networking.scm (static-networking-service, dhcp-client-service, ntp-service, tor-service, bitlbee-service, wicd-service): Likewise. * gnu/services/ssh.scm (lsh-service): Likewise. * gnu/services/web.scm (nginx-service): Likewise. * gnu/services/xorg.scm (xorg-configuration-file): Use 'mixed-text-file' instead of 'text-file*'. (xorg-start-command, slim-service): Turn into regular procedures. (xinitrc): Use 'program-file' instead of 'gexp->script'. * gnu/system/install.scm (cow-store-service, configuration-template-service): Turn into regular procedures. * gnu/system.scm (other-file-system-services, device-mapping-services, swap-services, essential-services, operating-system-services, user-shells, operating-system-accounts): Remove now unnecessary 'mlet' and turn into regular procedures. (operating-system-etc-directory, operating-system-activation-script, operating-system-boot-script): Adjust accordingly. * doc/guix.texi (Base Services, Networking Services, X Window, Desktop Services, Database Services, Web Services, Various Services, Name Service Switch): Adjust accordingly. Ludovic Courtès 2015-05-08doc: Document 'avahi-service'....* gnu/services/avahi.scm (avahi-service): Add URL in docstring. * doc/guix.texi (Networking Services): Document it. (Name Service Switch): Fix cross-reference. Ludovic Courtès 2015-04-17services: Explicitly refer to Shadow when requiring the 'nologin' shell....* gnu/services/avahi.scm (avahi-service): Change 'shell' to a gexp referring to "nologin" in the SHADOW package. * gnu/services/dbus.scm (dbus-service): Likewise. * gnu/services/networking.scm (ntp-service, tor-service): Likewise. Ludovic Courtès 2015-01-14monads: Move '%store-monad' and related procedures where they belong....This turns (guix monads) into a generic module for monads, and moves the store monad and related monadic procedures in their corresponding module. * guix/monads.scm (store-return, store-bind, %store-monad, store-lift, text-file, interned-file, package-file, package->derivation, package->cross-derivation, origin->derivation, imported-modules, compiled, modules, built-derivations, run-with-store): Move to... * guix/store.scm (store-return, store-bind, %store-monad, store-lift, text-file, interned-file): ... here. (%guile-for-build): New variable. (run-with-store): Moved from monads.scm. Remove default value for #:guile-for-build. * guix/packages.scm (default-guile): Export. (set-guile-for-build): New procedure. (package-file, package->derivation, package->cross-derivation, origin->derivation): Moved from monads.scm. * guix/derivations.scm (%guile-for-build): Remove. (imported-modules): Rename to... (%imported-modules): ... this. (compiled-modules): Rename to... (%compiled-modules): ... this. (built-derivations, imported-modules, compiled-modules): New procedures. * gnu/services/avahi.scm, gnu/services/base.scm, gnu/services/dbus.scm, gnu/services/dmd.scm, gnu/services/networking.scm, gnu/services/ssh.scm, gnu/services/xorg.scm, gnu/system/install.scm, gnu/system/linux-initrd.scm, gnu/system/shadow.scm, guix/download.scm, guix/gexp.scm, guix/git-download.scm, guix/profiles.scm, guix/svn-download.scm, tests/monads.scm: Adjust imports accordingly. * guix/monad-repl.scm (default-guile-derivation): New procedure. (store-monad-language, run-in-store): Use it. * build-aux/hydra/gnu-system.scm (qemu-jobs): Add explicit 'set-guile-for-build' call. * guix/scripts/archive.scm (derivation-from-expression): Likewise. * guix/scripts/build.scm (options/resolve-packages): Likewise. * guix/scripts/environment.scm (guix-environment): Likewise. * guix/scripts/system.scm (guix-system): Likewise. * doc/guix.texi (The Store Monad): Adjust module names accordingly. Ludovic Courtès 2014-07-25services: Use system groups where applicable....* gnu/services/avahi.scm (avahi-service): Add 'system?' field to 'user-group' form. * gnu/services/base.scm (guix-service): Likewise. * gnu/services/dbus.scm (dbus-service): Likewise. * gnu/services/networking.scm (tor-service): Likewise. Ludovic Courtès 2014-07-08services: Update to use the dmd 0.2 API....* gnu/services/avahi.scm (avahi-service)[start]: Wrap command in a list. * gnu/services/dbus.scm (dbus-service)[start]: Likewise. * gnu/services/ssh.scm (lsh-service): Likewise. * gnu/services/base.scm (mingetty-service)[start]: Likewise. (nscd-service)[start]: Likewise. (syslog-service)[start]: Likewise. (guix-service)[start]: Likewise. (udev-service)[start]: Use 'exec-command' instead of 'execl'. * gnu/services/xorg.scm (slim-service)[start]: Likewise, and use #:environment-variables. Ludovic Courtès 2014-06-27system: Add a 'system?' field to user accounts....* gnu/system/shadow.scm (<user-account>)[system?]: New field. * gnu/system.scm (user-account->gexp): Add it. * guix/build/activation.scm (add-user): Add #:system? parameter and honor it. (activate-users+groups): Handle the 'system?' part of user tuples. Pass it to 'add-user'. Don't create PROFILE-DIR when SYSTEM? is true. * gnu/services/dbus.scm (dbus-service): Add 'system?' field for "messagebus" account. * gnu/services/base.scm (guix-build-accounts): Likewise. * gnu/services/avahi.scm (avahi-service): Likewise. Ludovic Courtès 2014-05-26services: Add D-Bus and Avahi....* gnu/services/dbus.scm: New file. * gnu/services/avahi.scm: New file. * gnu-system.am (GNU_SYSTEM_MODULES): Add them. Ludovic Courtès