projects/guix·blob python-team · refs

291 lines · 11.8 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2018 Mathieu Othacehe <[email protected]>
3;;; Copyright © 2019, 2020 Ludovic Courtès <[email protected]>
4;;; Copyright © 2019 Tobias Geerinckx-Rice <[email protected]>
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 installer newt user)
22 #:use-module (gnu installer user)
23 #:use-module (gnu installer newt page)
24 #:use-module (gnu installer newt utils)
25 #:use-module (gnu installer utils)
26 #:use-module (guix i18n)
27 #:use-module (newt)
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 receive)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-26)
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-35)
34 #:export (run-user-page))
35
36(define* (run-user-add-page #:key (name "") (real-name "")
37 (home-directory ""))
38 "Run a form to enter the user name, home directory, and password. Use NAME,
39REAL-NAME, and HOME-DIRECTORY as the initial values in the form."
40 (define (pad-label label)
41 (string-pad-right label 25))
42
43 (define (root-account? name)
44 (string=? name "root"))
45
46 (let* ((label-name
47 (make-label -1 -1 (pad-label (G_ "Name"))))
48 (label-real-name
49 (make-label -1 -1 (pad-label (G_ "Real name"))))
50 (label-home-directory
51 (make-label -1 -1 (pad-label (G_ "Home directory"))))
52 (label-password
53 (make-label -1 -1 (pad-label (G_ "Password"))))
54 (entry-width 35)
55 (entry-name (make-entry -1 -1 entry-width
56 #:initial-value name))
57 (entry-real-name (make-entry -1 -1 entry-width
58 #:initial-value real-name))
59 (entry-home-directory (make-entry -1 -1 entry-width
60 #:initial-value home-directory))
61 (password-visible-cb
62 (make-checkbox -1 -1 (G_ "Show") #\space "x "))
63 (entry-password (make-entry -1 -1 entry-width
64 #:flags (logior FLAG-PASSWORD
65 FLAG-SCROLL)))
66 (entry-grid (make-grid 3 5))
67 (button-grid (make-grid 1 1))
68 (ok-button (make-button -1 -1 (G_ "OK")))
69 (grid (make-grid 1 2))
70 (title (G_ "User creation"))
71 (set-entry-grid-field
72 (cut set-grid-field entry-grid <> <> GRID-ELEMENT-COMPONENT <>))
73 (form (make-form)))
74
75 (set-entry-grid-field 0 0 label-name)
76 (set-entry-grid-field 1 0 entry-name)
77 (set-entry-grid-field 0 1 label-real-name)
78 (set-entry-grid-field 1 1 entry-real-name)
79 (set-entry-grid-field 0 2 label-home-directory)
80 (set-entry-grid-field 1 2 entry-home-directory)
81 (set-entry-grid-field 0 3 label-password)
82 (set-entry-grid-field 1 3 entry-password)
83
84 (set-grid-field entry-grid
85 2 3
86 GRID-ELEMENT-COMPONENT
87 password-visible-cb
88 #:pad-left 1)
89
90 (set-grid-field button-grid 0 0 GRID-ELEMENT-COMPONENT ok-button)
91
92 (add-component-callback
93 entry-name
94 (lambda ()
95 (set-entry-text entry-home-directory
96 (string-append "/home/" (entry-value entry-name)))
97
98 (when (string-null? (entry-value entry-real-name))
99 (set-entry-text entry-real-name
100 (string-titlecase (entry-value entry-name))))))
101
102 (add-component-callback
103 password-visible-cb
104 (lambda ()
105 (set-entry-flags entry-password
106 FLAG-PASSWORD
107 FLAG-ROLE-TOGGLE)))
108
109 (add-components-to-form form
110 label-name label-real-name
111 label-home-directory label-password
112 entry-name entry-real-name
113 entry-home-directory entry-password
114 password-visible-cb
115 ok-button)
116
117 (make-wrapped-grid-window (vertically-stacked-grid
118 GRID-ELEMENT-SUBGRID entry-grid
119 GRID-ELEMENT-SUBGRID button-grid)
120 title)
121
122 (let ((error-empty-field-page
123 (lambda ()
124 (run-error-page (G_ "Empty inputs are not allowed.")
125 (G_ "Empty input"))))
126 (error-root-page
127 (lambda ()
128 (run-error-page (G_ "Root account is automatically created.")
129 (G_ "Root account")))))
130 (receive (exit-reason argument)
131 (run-form form)
132 (dynamic-wind
133 (const #t)
134 (lambda ()
135 (when (eq? exit-reason 'exit-component)
136 (cond
137 ((components=? argument ok-button)
138 (let ((name (entry-value entry-name))
139 (real-name (entry-value entry-real-name))
140 (home-directory (entry-value entry-home-directory))
141 (password (entry-value entry-password)))
142 (cond
143 ;; Empty field.
144 ((or (string=? name "")
145 (string=? home-directory ""))
146 (error-empty-field-page)
147 (run-user-add-page))
148 ;; Reject root account.
149 ((root-account? name)
150 (error-root-page)
151 (run-user-add-page))
152 (else
153 (let ((password (confirm-password password)))
154 (if password
155 (user
156 (name name)
157 (real-name real-name)
158 (home-directory home-directory)
159 (password (make-secret password)))
160 (run-user-add-page #:name name
161 #:real-name real-name
162 #:home-directory
163 home-directory))))))))))
164 (lambda ()
165 (destroy-form-and-pop form)))))))
166
167(define* (confirm-password password #:optional (try-again (const #f)))
168 "Ask the user to confirm PASSWORD, a possibly empty string. Call TRY-AGAIN,
169a thunk, if the confirmation doesn't match PASSWORD, and return its result."
170 (define confirmation
171 (run-input-page (G_ "Please confirm the password.")
172 (G_ "Password confirmation required")
173 #:allow-empty-input? #t
174 #:input-visibility-checkbox? #t))
175
176 (if (string=? password confirmation)
177 password
178 (begin
179 (run-error-page
180 (G_ "Password mismatch, please try again.")
181 (G_ "Password error"))
182 (try-again))))
183
184(define (run-root-password-page)
185 (define password
186 ;; TRANSLATORS: Leave "root" untranslated: it refers to the name of the
187 ;; system administrator account.
188 (run-input-page (G_ "Please choose a password for the system \
189administrator (\"root\").")
190 (G_ "System administrator password")
191 #:input-visibility-checkbox? #t))
192
193 (confirm-password password run-root-password-page))
194
195(define (run-user-page)
196 (define (run users)
197 (let* ((listbox (make-listbox
198 -1 -1 10
199 (logior FLAG-SCROLL FLAG-BORDER)))
200 (info-textbox
201 (make-reflowed-textbox
202 -1 -1
203 (G_ "Please add at least one user to system\
204 using the 'Add' button.")
205 40 #:flags FLAG-BORDER))
206 (add-button (make-compact-button -1 -1 (G_ "Add")))
207 (del-button (make-compact-button -1 -1 (G_ "Delete")))
208 (listbox-button-grid
209 (apply
210 vertically-stacked-grid
211 GRID-ELEMENT-COMPONENT add-button
212 `(,@(if (null? users)
213 '()
214 (list GRID-ELEMENT-COMPONENT del-button)))))
215 (ok-button (make-button -1 -1 (G_ "OK")))
216 (exit-button (make-button -1 -1 (G_ "Exit")))
217 (title (G_ "User creation"))
218 (grid
219 (vertically-stacked-grid
220 GRID-ELEMENT-COMPONENT info-textbox
221 GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
222 GRID-ELEMENT-COMPONENT listbox
223 GRID-ELEMENT-SUBGRID listbox-button-grid)
224 GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
225 GRID-ELEMENT-COMPONENT ok-button
226 GRID-ELEMENT-COMPONENT exit-button)))
227 (sorted-users (sort users (lambda (a b)
228 (string<= (user-name a)
229 (user-name b)))))
230 (listbox-elements
231 (map
232 (lambda (user)
233 `((key . ,(append-entry-to-listbox listbox
234 (user-name user)))
235 (user . ,user)))
236 sorted-users))
237 (form (make-form)))
238
239
240 (add-form-to-grid grid form #t)
241 (make-wrapped-grid-window grid title)
242 (if (null? users)
243 (set-current-component form add-button)
244 (set-current-component form ok-button))
245
246 (receive (exit-reason argument)
247 (run-form-with-clients form '(add-users))
248 (dynamic-wind
249 (const #t)
250 (lambda ()
251 (match exit-reason
252 ('exit-component
253 (cond
254 ((components=? argument add-button)
255 (run (cons (run-user-add-page) users)))
256 ((components=? argument del-button)
257 (let* ((current-user-key (current-listbox-entry listbox))
258 (users
259 (map (cut assoc-ref <> 'user)
260 (remove (lambda (element)
261 (equal? (assoc-ref element 'key)
262 current-user-key))
263 listbox-elements))))
264 (run users)))
265 ((components=? argument ok-button)
266 (when (null? users)
267 (run-error-page (G_ "Please create at least one user.")
268 (G_ "No user"))
269 (run users))
270 (reverse users))
271 ((components=? argument exit-button)
272 (abort-to-prompt 'installer-step 'abort))))
273 ('exit-fd-ready
274 ;; Read the complete user list at once.
275 (match argument
276 ((('user ('name names) ('real-name real-names)
277 ('home-directory homes) ('password passwords))
278 ..1)
279 (map (lambda (name real-name home password)
280 (user (name name) (real-name real-name)
281 (home-directory home)
282 (password (make-secret password))))
283 names real-names homes passwords))))))
284 (lambda ()
285 (destroy-form-and-pop form))))))
286
287 ;; Add a "root" user simply to convey the root password.
288 (cons (user (name "root")
289 (home-directory "/root")
290 (password (make-secret (run-root-password-page))))
291 (run '())))