Commit | Line | Data |
---|---|---|
4dfe6c58 | 1 | ;;; GNU Guix --- Functional package management for GNU |
82b71ac3 | 2 | ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org> |
78ab0746 | 3 | ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org> |
4dfe6c58 LC |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
8 | ;;; under the terms of the GNU General Public License as published by | |
9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
10 | ;;; your option) any later version. | |
11 | ;;; | |
12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;;; GNU General Public License for more details. | |
16 | ;;; | |
17 | ;;; You should have received a copy of the GNU General Public License | |
18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
548f7a8f | 20 | (define-module (gnu build activation) |
8a9e21d1 | 21 | #:use-module (gnu build linux-boot) |
09e028f4 | 22 | #:use-module (guix build utils) |
4dfe6c58 | 23 | #:use-module (ice-9 ftw) |
ab6a279a LC |
24 | #:use-module (ice-9 match) |
25 | #:use-module (srfi srfi-1) | |
ad896f23 | 26 | #:use-module (srfi srfi-26) |
ab6a279a | 27 | #:export (activate-users+groups |
ae763b5b | 28 | activate-user-home |
ab6a279a | 29 | activate-etc |
b4140694 | 30 | activate-setuid-programs |
387e1754 | 31 | activate-special-files |
d460204f | 32 | activate-modprobe |
f34c56be | 33 | activate-firmware |
b158f1d7 | 34 | activate-ptrace-attach |
b4140694 | 35 | activate-current-system)) |
4dfe6c58 LC |
36 | |
37 | ;;; Commentary: | |
38 | ;;; | |
39 | ;;; This module provides "activation" helpers. Activation is the process that | |
40 | ;;; consists in setting up system-wide files and directories so that an | |
41 | ;;; 'operating-system' configuration becomes active. | |
42 | ;;; | |
43 | ;;; Code: | |
44 | ||
9bea87a5 LC |
45 | (define (enumerate thunk) |
46 | "Return the list of values returned by THUNK until it returned #f." | |
47 | (let loop ((entry (thunk)) | |
48 | (result '())) | |
49 | (if (not entry) | |
50 | (reverse result) | |
51 | (loop (thunk) (cons entry result))))) | |
52 | ||
53 | (define (current-users) | |
54 | "Return the passwd entries for all the currently defined user accounts." | |
55 | (setpw) | |
56 | (enumerate getpwent)) | |
57 | ||
58 | (define (current-groups) | |
59 | "Return the group entries for all the currently defined user groups." | |
60 | (setgr) | |
61 | (enumerate getgrent)) | |
62 | ||
c8fa3426 | 63 | (define* (add-group name #:key gid password system? |
ab6a279a LC |
64 | (log-port (current-error-port))) |
65 | "Add NAME as a user group, with the given numeric GID if specified." | |
66 | ;; Use 'groupadd' from the Shadow package. | |
67 | (format log-port "adding group '~a'...~%" name) | |
68 | (let ((args `(,@(if gid `("-g" ,(number->string gid)) '()) | |
69 | ,@(if password `("-p" ,password) '()) | |
c8fa3426 | 70 | ,@(if system? `("--system") '()) |
ab6a279a LC |
71 | ,name))) |
72 | (zero? (apply system* "groupadd" args)))) | |
73 | ||
45c5b47b LC |
74 | (define %skeleton-directory |
75 | ;; Directory containing skeleton files for new accounts. | |
76 | ;; Note: keep the trailing '/' so that 'scandir' enters it. | |
77 | "/etc/skel/") | |
78 | ||
79 | (define (dot-or-dot-dot? file) | |
80 | (member file '("." ".."))) | |
81 | ||
82 | (define* (copy-account-skeletons home | |
cf98d342 LC |
83 | #:key |
84 | (directory %skeleton-directory) | |
85 | uid gid) | |
86 | "Copy the account skeletons from DIRECTORY to HOME. When UID is an integer, | |
87 | make it the owner of all the files created; likewise for GID." | |
88 | (define (set-owner file) | |
89 | (when (or uid gid) | |
90 | (chown file (or uid -1) (or gid -1)))) | |
91 | ||
45c5b47b LC |
92 | (let ((files (scandir directory (negate dot-or-dot-dot?) |
93 | string<?))) | |
94 | (mkdir-p home) | |
cf98d342 | 95 | (set-owner home) |
45c5b47b | 96 | (for-each (lambda (file) |
356a62b8 | 97 | (let ((target (string-append home "/" file))) |
4e8b7502 | 98 | (copy-recursively (string-append directory "/" file) |
2fa909b2 LC |
99 | target |
100 | #:log (%make-void-port "w")) | |
cf98d342 LC |
101 | (for-each set-owner |
102 | (find-files target (const #t) | |
103 | #:directories? #t)) | |
356a62b8 LC |
104 | (make-file-writable target))) |
105 | files))) | |
106 | ||
107 | (define* (make-skeletons-writable home | |
108 | #:optional (directory %skeleton-directory)) | |
109 | "Make sure that the files that have been copied from DIRECTORY to HOME are | |
110 | owner-writable in HOME." | |
111 | (let ((files (scandir directory (negate dot-or-dot-dot?) | |
112 | string<?))) | |
113 | (for-each (lambda (file) | |
114 | (let ((target (string-append home "/" file))) | |
115 | (when (file-exists? target) | |
116 | (make-file-writable target)))) | |
45c5b47b LC |
117 | files))) |
118 | ||
ab6a279a | 119 | (define* (add-user name group |
eb56ee02 LC |
120 | #:key uid comment home create-home? |
121 | shell password system? | |
ab6a279a LC |
122 | (supplementary-groups '()) |
123 | (log-port (current-error-port))) | |
124 | "Create an account for user NAME part of GROUP, with the specified | |
125 | properties. Return #t on success." | |
126 | (format log-port "adding user '~a'...~%" name) | |
127 | ||
128 | (if (and uid (zero? uid)) | |
129 | ||
130 | ;; 'useradd' fails with "Cannot determine your user name" if the root | |
131 | ;; account doesn't exist. Thus, for bootstrapping purposes, create that | |
132 | ;; one manually. | |
41db5a75 | 133 | (let ((home (or home "/root"))) |
ab6a279a LC |
134 | (call-with-output-file "/etc/shadow" |
135 | (cut format <> "~a::::::::~%" name)) | |
136 | (call-with-output-file "/etc/passwd" | |
137 | (cut format <> "~a:x:~a:~a:~a:~a:~a~%" | |
138 | name "0" "0" comment home shell)) | |
139 | (chmod "/etc/shadow" #o600) | |
41db5a75 LC |
140 | (copy-account-skeletons home) |
141 | (chmod home #o700) | |
ab6a279a LC |
142 | #t) |
143 | ||
144 | ;; Use 'useradd' from the Shadow package. | |
145 | (let ((args `(,@(if uid `("-u" ,(number->string uid)) '()) | |
146 | "-g" ,(if (number? group) (number->string group) group) | |
147 | ,@(if (pair? supplementary-groups) | |
148 | `("-G" ,(string-join supplementary-groups ",")) | |
149 | '()) | |
150 | ,@(if comment `("-c" ,comment) '()) | |
b2979344 LC |
151 | ,@(if home `("-d" ,home) '()) |
152 | ||
153 | ;; Home directories of non-system accounts are created by | |
154 | ;; 'activate-user-home'. | |
155 | ,@(if (and home create-home? system? | |
156 | (not (file-exists? home))) | |
157 | '("--create-home") | |
f3b692ac | 158 | '()) |
b2979344 | 159 | |
ab6a279a LC |
160 | ,@(if shell `("-s" ,shell) '()) |
161 | ,@(if password `("-p" ,password) '()) | |
459dd9ea | 162 | ,@(if system? '("--system") '()) |
ab6a279a | 163 | ,name))) |
356a62b8 LC |
164 | (and (zero? (apply system* "useradd" args)) |
165 | (begin | |
166 | ;; Since /etc/skel is a link to a directory in the store where | |
167 | ;; all files have the writable bit cleared, and since 'useradd' | |
168 | ;; preserves permissions when it copies them, explicitly make | |
169 | ;; them writable. | |
170 | (make-skeletons-writable home) | |
171 | #t))))) | |
ab6a279a | 172 | |
e2b464b7 | 173 | (define* (modify-user name group |
eb56ee02 LC |
174 | #:key uid comment home create-home? |
175 | shell password system? | |
e2b464b7 LC |
176 | (supplementary-groups '()) |
177 | (log-port (current-error-port))) | |
178 | "Modify user account NAME to have all the given settings." | |
179 | ;; Use 'usermod' from the Shadow package. | |
180 | (let ((args `(,@(if uid `("-u" ,(number->string uid)) '()) | |
181 | "-g" ,(if (number? group) (number->string group) group) | |
182 | ,@(if (pair? supplementary-groups) | |
183 | `("-G" ,(string-join supplementary-groups ",")) | |
184 | '()) | |
185 | ,@(if comment `("-c" ,comment) '()) | |
82b71ac3 LC |
186 | ;; Don't use '--move-home'. |
187 | ,@(if home `("-d" ,home) '()) | |
e2b464b7 LC |
188 | ,@(if shell `("-s" ,shell) '()) |
189 | ,name))) | |
190 | (zero? (apply system* "usermod" args)))) | |
191 | ||
9bea87a5 LC |
192 | (define* (delete-user name #:key (log-port (current-error-port))) |
193 | "Remove user account NAME. Return #t on success. This may fail if NAME is | |
194 | logged in." | |
195 | (format log-port "deleting user '~a'...~%" name) | |
196 | (zero? (system* "userdel" name))) | |
197 | ||
198 | (define* (delete-group name #:key (log-port (current-error-port))) | |
199 | "Remove group NAME. Return #t on success." | |
200 | (format log-port "deleting group '~a'...~%" name) | |
201 | (zero? (system* "groupdel" name))) | |
202 | ||
e2b464b7 | 203 | (define* (ensure-user name group |
eb56ee02 LC |
204 | #:key uid comment home create-home? |
205 | shell password system? | |
e2b464b7 LC |
206 | (supplementary-groups '()) |
207 | (log-port (current-error-port)) | |
208 | #:rest rest) | |
209 | "Make sure user NAME exists and has the relevant settings." | |
210 | (if (false-if-exception (getpwnam name)) | |
211 | (apply modify-user name group rest) | |
212 | (apply add-user name group rest))) | |
213 | ||
ab6a279a LC |
214 | (define (activate-users+groups users groups) |
215 | "Make sure the accounts listed in USERS and the user groups listed in GROUPS | |
216 | are all available. | |
217 | ||
218 | Each item in USERS is a list of all the characteristics of a user account; | |
219 | each item in GROUPS is a tuple with the group name, group password or #f, and | |
220 | numeric gid or #f." | |
221 | (define (touch file) | |
f01efec0 | 222 | (close-port (open-file file "a0b"))) |
ab6a279a LC |
223 | |
224 | (define activate-user | |
225 | (match-lambda | |
eb56ee02 LC |
226 | ((name uid group supplementary-groups comment home create-home? |
227 | shell password system?) | |
e2b464b7 LC |
228 | (let ((profile-dir (string-append "/var/guix/profiles/per-user/" |
229 | name))) | |
230 | (ensure-user name group | |
231 | #:uid uid | |
232 | #:system? system? | |
233 | #:supplementary-groups supplementary-groups | |
234 | #:comment comment | |
235 | #:home home | |
b2979344 | 236 | #:create-home? create-home? |
41f76ae0 | 237 | |
e2b464b7 LC |
238 | #:shell shell |
239 | #:password password) | |
240 | ||
241 | (unless system? | |
242 | ;; Create the profile directory for the new account. | |
243 | (let ((pw (getpwnam name))) | |
244 | (mkdir-p profile-dir) | |
245 | (chown profile-dir (passwd:uid pw) (passwd:gid pw)))))))) | |
ab6a279a LC |
246 | |
247 | ;; 'groupadd' aborts if the file doesn't already exist. | |
248 | (touch "/etc/group") | |
249 | ||
6526d43e | 250 | ;; Allow home directories to be created under /var/lib. |
a7199b7d | 251 | (mkdir-p "/var/lib") |
6526d43e | 252 | |
ab6a279a LC |
253 | ;; Create the root account so we can use 'useradd' and 'groupadd'. |
254 | (activate-user (find (match-lambda | |
255 | ((name (? zero?) _ ...) #t) | |
256 | (_ #f)) | |
257 | users)) | |
258 | ||
259 | ;; Then create the groups. | |
260 | (for-each (match-lambda | |
c8fa3426 | 261 | ((name password gid system?) |
e2fcc23a | 262 | (unless (false-if-exception (getgrnam name)) |
c8fa3426 LC |
263 | (add-group name |
264 | #:gid gid #:password password | |
265 | #:system? system?)))) | |
ab6a279a LC |
266 | groups) |
267 | ||
9bea87a5 LC |
268 | ;; Create the other user accounts. |
269 | (for-each activate-user users) | |
270 | ||
271 | ;; Finally, delete extra user accounts and groups. | |
272 | (for-each delete-user | |
273 | (lset-difference string=? | |
274 | (map passwd:name (current-users)) | |
275 | (match users | |
276 | (((names . _) ...) | |
277 | names)))) | |
278 | (for-each delete-group | |
279 | (lset-difference string=? | |
280 | (map group:name (current-groups)) | |
281 | (match groups | |
282 | (((names . _) ...) | |
283 | names))))) | |
ab6a279a | 284 | |
ae763b5b LC |
285 | (define (activate-user-home users) |
286 | "Create and populate the home directory of USERS, a list of tuples, unless | |
287 | they already exist." | |
288 | (define ensure-user-home | |
289 | (match-lambda | |
290 | ((name uid group supplementary-groups comment home create-home? | |
291 | shell password system?) | |
41f76ae0 LC |
292 | ;; The home directories of system accounts are created during |
293 | ;; activation, not here. | |
294 | (unless (or (not home) (not create-home?) system? | |
295 | (directory-exists? home)) | |
cf98d342 LC |
296 | (let* ((pw (getpwnam name)) |
297 | (uid (passwd:uid pw)) | |
298 | (gid (passwd:gid pw))) | |
299 | (mkdir-p home) | |
300 | (chown home uid gid) | |
8bb76f3d LC |
301 | (chmod home #o700) |
302 | (copy-account-skeletons home | |
303 | #:uid uid #:gid gid)))))) | |
ae763b5b LC |
304 | |
305 | (for-each ensure-user-home users)) | |
306 | ||
4dfe6c58 LC |
307 | (define (activate-etc etc) |
308 | "Install ETC, a directory in the store, as the source of static files for | |
309 | /etc." | |
310 | ||
311 | ;; /etc is a mixture of static and dynamic settings. Here is where we | |
312 | ;; initialize it from the static part. | |
313 | ||
ee7bae3b LC |
314 | (define (rm-f file) |
315 | (false-if-exception (delete-file file))) | |
316 | ||
4dfe6c58 | 317 | (format #t "populating /etc from ~a...~%" etc) |
49962b15 | 318 | (mkdir-p "/etc") |
ee7bae3b | 319 | |
78ab0746 MW |
320 | ;; Create the /etc/ssl -> /run/current-system/profile/etc/ssl symlink. This |
321 | ;; symlink, to a target outside of the store, probably doesn't belong in the | |
322 | ;; static 'etc' store directory. However, if it were to be put there, | |
323 | ;; beware that if /run/current-system/profile/etc/ssl doesn't exist at the | |
324 | ;; time of activation (e.g. when installing a fresh system), the call to | |
325 | ;; 'file-is-directory?' below will fail because it uses 'stat', not 'lstat'. | |
326 | (rm-f "/etc/ssl") | |
327 | (symlink "/run/current-system/profile/etc/ssl" "/etc/ssl") | |
328 | ||
ee7bae3b LC |
329 | (rm-f "/etc/static") |
330 | (symlink etc "/etc/static") | |
331 | (for-each (lambda (file) | |
332 | (let ((target (string-append "/etc/" file)) | |
333 | (source (string-append "/etc/static/" file))) | |
334 | (rm-f target) | |
335 | ||
336 | ;; Things such as /etc/sudoers must be regular files, not | |
337 | ;; symlinks; furthermore, they could be modified behind our | |
338 | ;; back---e.g., with 'visudo'. Thus, make a copy instead of | |
339 | ;; symlinking them. | |
340 | (if (file-is-directory? source) | |
341 | (symlink source target) | |
342 | (copy-file source target)) | |
343 | ||
344 | ;; XXX: Dirty hack to meet sudo's expectations. | |
345 | (when (string=? (basename target) "sudoers") | |
346 | (chmod target #o440)))) | |
45c5b47b | 347 | (scandir etc (negate dot-or-dot-dot?) |
ee7bae3b LC |
348 | |
349 | ;; The default is 'string-locale<?', but we don't have | |
350 | ;; it when run from the initrd's statically-linked | |
351 | ;; Guile. | |
6496de9b | 352 | string<?))) |
4dfe6c58 | 353 | |
09e028f4 LC |
354 | (define %setuid-directory |
355 | ;; Place where setuid programs are stored. | |
356 | "/run/setuid-programs") | |
357 | ||
358 | (define (activate-setuid-programs programs) | |
359 | "Turn PROGRAMS, a list of file names, into setuid programs stored under | |
360 | %SETUID-DIRECTORY." | |
361 | (define (make-setuid-program prog) | |
362 | (let ((target (string-append %setuid-directory | |
363 | "/" (basename prog)))) | |
5e66574a | 364 | (copy-file prog target) |
09e028f4 LC |
365 | (chown target 0 0) |
366 | (chmod target #o6555))) | |
367 | ||
368 | (format #t "setting up setuid programs in '~a'...~%" | |
369 | %setuid-directory) | |
370 | (if (file-exists? %setuid-directory) | |
ad896f23 LC |
371 | (for-each (compose delete-file |
372 | (cut string-append %setuid-directory "/" <>)) | |
09e028f4 LC |
373 | (scandir %setuid-directory |
374 | (lambda (file) | |
375 | (not (member file '("." "..")))) | |
376 | string<?)) | |
377 | (mkdir-p %setuid-directory)) | |
378 | ||
379 | (for-each make-setuid-program programs)) | |
380 | ||
387e1754 LC |
381 | (define (activate-special-files special-files) |
382 | "Install the files listed in SPECIAL-FILES. Each element of SPECIAL-FILES | |
383 | is a pair where the first element is the name of the special file and the | |
384 | second element is the name it should appear at, such as: | |
385 | ||
386 | ((\"/bin/sh\" \"/gnu/store/…-bash/bin/sh\") | |
387 | (\"/usr/bin/env\" \"/gnu/store/…-coreutils/bin/env\")) | |
388 | " | |
389 | (define install-special-file | |
390 | (match-lambda | |
391 | ((target file) | |
392 | (let ((pivot (string-append target ".new"))) | |
393 | (mkdir-p (dirname target)) | |
394 | (symlink file pivot) | |
395 | (rename-file pivot target))))) | |
396 | ||
397 | (for-each install-special-file special-files)) | |
ee248b6a | 398 | |
d460204f LC |
399 | (define (activate-modprobe modprobe) |
400 | "Tell the kernel to use MODPROBE to load modules." | |
401 | (call-with-output-file "/proc/sys/kernel/modprobe" | |
402 | (lambda (port) | |
403 | (display modprobe port)))) | |
404 | ||
f34c56be LC |
405 | (define (activate-firmware directory) |
406 | "Tell the kernel to look for device firmware under DIRECTORY. This | |
407 | mechanism bypasses udev: it allows Linux to handle firmware loading directly | |
408 | by itself, without having to resort to a \"user helper\"." | |
409 | (call-with-output-file "/sys/module/firmware_class/parameters/path" | |
410 | (lambda (port) | |
411 | (display directory port)))) | |
b158f1d7 LC |
412 | |
413 | (define (activate-ptrace-attach) | |
414 | "Allow users to PTRACE_ATTACH their own processes. | |
415 | ||
416 | This works around a regression introduced in the default \"security\" policy | |
417 | found in Linux 3.4 onward that prevents users from attaching to their own | |
418 | processes--see Yama.txt in the Linux source tree for the rationale. This | |
419 | sounds like an unacceptable restriction for little or no security | |
420 | improvement." | |
15f0de05 MW |
421 | (let ((file "/proc/sys/kernel/yama/ptrace_scope")) |
422 | (when (file-exists? file) | |
423 | (call-with-output-file file | |
424 | (lambda (port) | |
425 | (display 0 port)))))) | |
f34c56be LC |
426 | |
427 | \f | |
b4140694 LC |
428 | (define %current-system |
429 | ;; The system that is current (a symlink.) This is not necessarily the same | |
484a2b3a LC |
430 | ;; as the system we booted (aka. /run/booted-system) because we can re-build |
431 | ;; a new system configuration and activate it, without rebooting. | |
b4140694 LC |
432 | "/run/current-system") |
433 | ||
434 | (define (boot-time-system) | |
435 | "Return the '--system' argument passed on the kernel command line." | |
436 | (find-long-option "--system" (linux-command-line))) | |
437 | ||
6d49355d LC |
438 | (define* (activate-current-system |
439 | #:optional (system (or (getenv "GUIX_NEW_SYSTEM") | |
440 | (boot-time-system)))) | |
484a2b3a | 441 | "Atomically make SYSTEM the current system." |
6d49355d LC |
442 | ;; The 'GUIX_NEW_SYSTEM' environment variable is used as a way for 'guix |
443 | ;; system reconfigure' to pass the file name of the new system. | |
444 | ||
b4140694 | 445 | (format #t "making '~a' the current system...~%" system) |
b4140694 LC |
446 | |
447 | ;; Atomically make SYSTEM current. | |
448 | (let ((new (string-append %current-system ".new"))) | |
449 | (symlink system new) | |
450 | (rename-file new %current-system))) | |
451 | ||
4dfe6c58 | 452 | ;;; activation.scm ends here |