Commit | Line | Data |
---|---|---|
4dfe6c58 | 1 | ;;; GNU Guix --- Functional package management for GNU |
ae763b5b | 2 | ;;; Copyright © 2013, 2014, 2015, 2016, 2017 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 |
ee248b6a | 31 | activate-/bin/sh |
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 | ||
356a62b8 LC |
82 | (define (make-file-writable file) |
83 | "Make FILE writable for its owner.." | |
84 | (let ((stat (lstat file))) ;XXX: symlinks | |
85 | (chmod file (logior #o600 (stat:perms stat))))) | |
86 | ||
45c5b47b LC |
87 | (define* (copy-account-skeletons home |
88 | #:optional (directory %skeleton-directory)) | |
89 | "Copy the account skeletons from DIRECTORY to HOME." | |
90 | (let ((files (scandir directory (negate dot-or-dot-dot?) | |
91 | string<?))) | |
92 | (mkdir-p home) | |
93 | (for-each (lambda (file) | |
356a62b8 | 94 | (let ((target (string-append home "/" file))) |
4e8b7502 | 95 | (copy-recursively (string-append directory "/" file) |
2fa909b2 LC |
96 | target |
97 | #:log (%make-void-port "w")) | |
356a62b8 LC |
98 | (make-file-writable target))) |
99 | files))) | |
100 | ||
101 | (define* (make-skeletons-writable home | |
102 | #:optional (directory %skeleton-directory)) | |
103 | "Make sure that the files that have been copied from DIRECTORY to HOME are | |
104 | owner-writable in HOME." | |
105 | (let ((files (scandir directory (negate dot-or-dot-dot?) | |
106 | string<?))) | |
107 | (for-each (lambda (file) | |
108 | (let ((target (string-append home "/" file))) | |
109 | (when (file-exists? target) | |
110 | (make-file-writable target)))) | |
45c5b47b LC |
111 | files))) |
112 | ||
ab6a279a | 113 | (define* (add-user name group |
eb56ee02 LC |
114 | #:key uid comment home create-home? |
115 | shell password system? | |
ab6a279a LC |
116 | (supplementary-groups '()) |
117 | (log-port (current-error-port))) | |
118 | "Create an account for user NAME part of GROUP, with the specified | |
119 | properties. Return #t on success." | |
120 | (format log-port "adding user '~a'...~%" name) | |
121 | ||
122 | (if (and uid (zero? uid)) | |
123 | ||
124 | ;; 'useradd' fails with "Cannot determine your user name" if the root | |
125 | ;; account doesn't exist. Thus, for bootstrapping purposes, create that | |
126 | ;; one manually. | |
127 | (begin | |
128 | (call-with-output-file "/etc/shadow" | |
129 | (cut format <> "~a::::::::~%" name)) | |
130 | (call-with-output-file "/etc/passwd" | |
131 | (cut format <> "~a:x:~a:~a:~a:~a:~a~%" | |
132 | name "0" "0" comment home shell)) | |
133 | (chmod "/etc/shadow" #o600) | |
45c5b47b | 134 | (copy-account-skeletons (or home "/root")) |
ab6a279a LC |
135 | #t) |
136 | ||
137 | ;; Use 'useradd' from the Shadow package. | |
138 | (let ((args `(,@(if uid `("-u" ,(number->string uid)) '()) | |
139 | "-g" ,(if (number? group) (number->string group) group) | |
140 | ,@(if (pair? supplementary-groups) | |
141 | `("-G" ,(string-join supplementary-groups ",")) | |
142 | '()) | |
143 | ,@(if comment `("-c" ,comment) '()) | |
eb56ee02 | 144 | ,@(if (and home create-home?) |
f3b692ac LC |
145 | (if (file-exists? home) |
146 | `("-d" ,home) ; avoid warning from 'useradd' | |
147 | `("-d" ,home "--create-home")) | |
148 | '()) | |
ab6a279a LC |
149 | ,@(if shell `("-s" ,shell) '()) |
150 | ,@(if password `("-p" ,password) '()) | |
459dd9ea | 151 | ,@(if system? '("--system") '()) |
ab6a279a | 152 | ,name))) |
356a62b8 LC |
153 | (and (zero? (apply system* "useradd" args)) |
154 | (begin | |
155 | ;; Since /etc/skel is a link to a directory in the store where | |
156 | ;; all files have the writable bit cleared, and since 'useradd' | |
157 | ;; preserves permissions when it copies them, explicitly make | |
158 | ;; them writable. | |
159 | (make-skeletons-writable home) | |
160 | #t))))) | |
ab6a279a | 161 | |
e2b464b7 | 162 | (define* (modify-user name group |
eb56ee02 LC |
163 | #:key uid comment home create-home? |
164 | shell password system? | |
e2b464b7 LC |
165 | (supplementary-groups '()) |
166 | (log-port (current-error-port))) | |
167 | "Modify user account NAME to have all the given settings." | |
168 | ;; Use 'usermod' from the Shadow package. | |
169 | (let ((args `(,@(if uid `("-u" ,(number->string uid)) '()) | |
170 | "-g" ,(if (number? group) (number->string group) group) | |
171 | ,@(if (pair? supplementary-groups) | |
172 | `("-G" ,(string-join supplementary-groups ",")) | |
173 | '()) | |
174 | ,@(if comment `("-c" ,comment) '()) | |
175 | ;; Don't use '--move-home', so ignore HOME. | |
176 | ,@(if shell `("-s" ,shell) '()) | |
177 | ,name))) | |
178 | (zero? (apply system* "usermod" args)))) | |
179 | ||
9bea87a5 LC |
180 | (define* (delete-user name #:key (log-port (current-error-port))) |
181 | "Remove user account NAME. Return #t on success. This may fail if NAME is | |
182 | logged in." | |
183 | (format log-port "deleting user '~a'...~%" name) | |
184 | (zero? (system* "userdel" name))) | |
185 | ||
186 | (define* (delete-group name #:key (log-port (current-error-port))) | |
187 | "Remove group NAME. Return #t on success." | |
188 | (format log-port "deleting group '~a'...~%" name) | |
189 | (zero? (system* "groupdel" name))) | |
190 | ||
e2b464b7 | 191 | (define* (ensure-user name group |
eb56ee02 LC |
192 | #:key uid comment home create-home? |
193 | shell password system? | |
e2b464b7 LC |
194 | (supplementary-groups '()) |
195 | (log-port (current-error-port)) | |
196 | #:rest rest) | |
197 | "Make sure user NAME exists and has the relevant settings." | |
198 | (if (false-if-exception (getpwnam name)) | |
199 | (apply modify-user name group rest) | |
200 | (apply add-user name group rest))) | |
201 | ||
ab6a279a LC |
202 | (define (activate-users+groups users groups) |
203 | "Make sure the accounts listed in USERS and the user groups listed in GROUPS | |
204 | are all available. | |
205 | ||
206 | Each item in USERS is a list of all the characteristics of a user account; | |
207 | each item in GROUPS is a tuple with the group name, group password or #f, and | |
208 | numeric gid or #f." | |
209 | (define (touch file) | |
f01efec0 | 210 | (close-port (open-file file "a0b"))) |
ab6a279a LC |
211 | |
212 | (define activate-user | |
213 | (match-lambda | |
eb56ee02 LC |
214 | ((name uid group supplementary-groups comment home create-home? |
215 | shell password system?) | |
e2b464b7 LC |
216 | (let ((profile-dir (string-append "/var/guix/profiles/per-user/" |
217 | name))) | |
218 | (ensure-user name group | |
219 | #:uid uid | |
220 | #:system? system? | |
221 | #:supplementary-groups supplementary-groups | |
222 | #:comment comment | |
223 | #:home home | |
ae763b5b | 224 | #:create-home? (and create-home? system?) |
e2b464b7 LC |
225 | #:shell shell |
226 | #:password password) | |
227 | ||
228 | (unless system? | |
229 | ;; Create the profile directory for the new account. | |
230 | (let ((pw (getpwnam name))) | |
231 | (mkdir-p profile-dir) | |
232 | (chown profile-dir (passwd:uid pw) (passwd:gid pw)))))))) | |
ab6a279a LC |
233 | |
234 | ;; 'groupadd' aborts if the file doesn't already exist. | |
235 | (touch "/etc/group") | |
236 | ||
6526d43e | 237 | ;; Allow home directories to be created under /var/lib. |
a7199b7d | 238 | (mkdir-p "/var/lib") |
6526d43e | 239 | |
ab6a279a LC |
240 | ;; Create the root account so we can use 'useradd' and 'groupadd'. |
241 | (activate-user (find (match-lambda | |
242 | ((name (? zero?) _ ...) #t) | |
243 | (_ #f)) | |
244 | users)) | |
245 | ||
246 | ;; Then create the groups. | |
247 | (for-each (match-lambda | |
c8fa3426 | 248 | ((name password gid system?) |
e2fcc23a | 249 | (unless (false-if-exception (getgrnam name)) |
c8fa3426 LC |
250 | (add-group name |
251 | #:gid gid #:password password | |
252 | #:system? system?)))) | |
ab6a279a LC |
253 | groups) |
254 | ||
9bea87a5 LC |
255 | ;; Create the other user accounts. |
256 | (for-each activate-user users) | |
257 | ||
258 | ;; Finally, delete extra user accounts and groups. | |
259 | (for-each delete-user | |
260 | (lset-difference string=? | |
261 | (map passwd:name (current-users)) | |
262 | (match users | |
263 | (((names . _) ...) | |
264 | names)))) | |
265 | (for-each delete-group | |
266 | (lset-difference string=? | |
267 | (map group:name (current-groups)) | |
268 | (match groups | |
269 | (((names . _) ...) | |
270 | names))))) | |
ab6a279a | 271 | |
ae763b5b LC |
272 | (define (activate-user-home users) |
273 | "Create and populate the home directory of USERS, a list of tuples, unless | |
274 | they already exist." | |
275 | (define ensure-user-home | |
276 | (match-lambda | |
277 | ((name uid group supplementary-groups comment home create-home? | |
278 | shell password system?) | |
279 | (unless (or (not home) (directory-exists? home)) | |
280 | (mkdir-p home) | |
281 | (unless system? | |
282 | (copy-account-skeletons home)))))) | |
283 | ||
284 | (for-each ensure-user-home users)) | |
285 | ||
4dfe6c58 LC |
286 | (define (activate-etc etc) |
287 | "Install ETC, a directory in the store, as the source of static files for | |
288 | /etc." | |
289 | ||
290 | ;; /etc is a mixture of static and dynamic settings. Here is where we | |
291 | ;; initialize it from the static part. | |
292 | ||
ee7bae3b LC |
293 | (define (rm-f file) |
294 | (false-if-exception (delete-file file))) | |
295 | ||
4dfe6c58 | 296 | (format #t "populating /etc from ~a...~%" etc) |
ee7bae3b | 297 | |
78ab0746 MW |
298 | ;; Create the /etc/ssl -> /run/current-system/profile/etc/ssl symlink. This |
299 | ;; symlink, to a target outside of the store, probably doesn't belong in the | |
300 | ;; static 'etc' store directory. However, if it were to be put there, | |
301 | ;; beware that if /run/current-system/profile/etc/ssl doesn't exist at the | |
302 | ;; time of activation (e.g. when installing a fresh system), the call to | |
303 | ;; 'file-is-directory?' below will fail because it uses 'stat', not 'lstat'. | |
304 | (rm-f "/etc/ssl") | |
305 | (symlink "/run/current-system/profile/etc/ssl" "/etc/ssl") | |
306 | ||
ee7bae3b LC |
307 | (rm-f "/etc/static") |
308 | (symlink etc "/etc/static") | |
309 | (for-each (lambda (file) | |
310 | (let ((target (string-append "/etc/" file)) | |
311 | (source (string-append "/etc/static/" file))) | |
312 | (rm-f target) | |
313 | ||
314 | ;; Things such as /etc/sudoers must be regular files, not | |
315 | ;; symlinks; furthermore, they could be modified behind our | |
316 | ;; back---e.g., with 'visudo'. Thus, make a copy instead of | |
317 | ;; symlinking them. | |
318 | (if (file-is-directory? source) | |
319 | (symlink source target) | |
320 | (copy-file source target)) | |
321 | ||
322 | ;; XXX: Dirty hack to meet sudo's expectations. | |
323 | (when (string=? (basename target) "sudoers") | |
324 | (chmod target #o440)))) | |
45c5b47b | 325 | (scandir etc (negate dot-or-dot-dot?) |
ee7bae3b LC |
326 | |
327 | ;; The default is 'string-locale<?', but we don't have | |
328 | ;; it when run from the initrd's statically-linked | |
329 | ;; Guile. | |
6496de9b | 330 | string<?))) |
4dfe6c58 | 331 | |
09e028f4 LC |
332 | (define %setuid-directory |
333 | ;; Place where setuid programs are stored. | |
334 | "/run/setuid-programs") | |
335 | ||
095f4deb LC |
336 | (define (link-or-copy source target) |
337 | "Attempt to make TARGET a hard link to SOURCE; if it fails, fall back to | |
338 | copy SOURCE to TARGET." | |
339 | (catch 'system-error | |
340 | (lambda () | |
341 | (link source target)) | |
342 | (lambda args | |
343 | ;; Perhaps SOURCE and TARGET live in a different file system, so copy | |
344 | ;; SOURCE. | |
345 | (copy-file source target)))) | |
346 | ||
09e028f4 LC |
347 | (define (activate-setuid-programs programs) |
348 | "Turn PROGRAMS, a list of file names, into setuid programs stored under | |
349 | %SETUID-DIRECTORY." | |
350 | (define (make-setuid-program prog) | |
351 | (let ((target (string-append %setuid-directory | |
352 | "/" (basename prog)))) | |
095f4deb | 353 | (link-or-copy prog target) |
09e028f4 LC |
354 | (chown target 0 0) |
355 | (chmod target #o6555))) | |
356 | ||
357 | (format #t "setting up setuid programs in '~a'...~%" | |
358 | %setuid-directory) | |
359 | (if (file-exists? %setuid-directory) | |
ad896f23 LC |
360 | (for-each (compose delete-file |
361 | (cut string-append %setuid-directory "/" <>)) | |
09e028f4 LC |
362 | (scandir %setuid-directory |
363 | (lambda (file) | |
364 | (not (member file '("." "..")))) | |
365 | string<?)) | |
366 | (mkdir-p %setuid-directory)) | |
367 | ||
368 | (for-each make-setuid-program programs)) | |
369 | ||
ee248b6a LC |
370 | (define (activate-/bin/sh shell) |
371 | "Change /bin/sh to point to SHELL." | |
372 | (symlink shell "/bin/sh.new") | |
373 | (rename-file "/bin/sh.new" "/bin/sh")) | |
374 | ||
d460204f LC |
375 | (define (activate-modprobe modprobe) |
376 | "Tell the kernel to use MODPROBE to load modules." | |
377 | (call-with-output-file "/proc/sys/kernel/modprobe" | |
378 | (lambda (port) | |
379 | (display modprobe port)))) | |
380 | ||
f34c56be LC |
381 | (define (activate-firmware directory) |
382 | "Tell the kernel to look for device firmware under DIRECTORY. This | |
383 | mechanism bypasses udev: it allows Linux to handle firmware loading directly | |
384 | by itself, without having to resort to a \"user helper\"." | |
385 | (call-with-output-file "/sys/module/firmware_class/parameters/path" | |
386 | (lambda (port) | |
387 | (display directory port)))) | |
b158f1d7 LC |
388 | |
389 | (define (activate-ptrace-attach) | |
390 | "Allow users to PTRACE_ATTACH their own processes. | |
391 | ||
392 | This works around a regression introduced in the default \"security\" policy | |
393 | found in Linux 3.4 onward that prevents users from attaching to their own | |
394 | processes--see Yama.txt in the Linux source tree for the rationale. This | |
395 | sounds like an unacceptable restriction for little or no security | |
396 | improvement." | |
15f0de05 MW |
397 | (let ((file "/proc/sys/kernel/yama/ptrace_scope")) |
398 | (when (file-exists? file) | |
399 | (call-with-output-file file | |
400 | (lambda (port) | |
401 | (display 0 port)))))) | |
f34c56be LC |
402 | |
403 | \f | |
b4140694 LC |
404 | (define %current-system |
405 | ;; The system that is current (a symlink.) This is not necessarily the same | |
484a2b3a LC |
406 | ;; as the system we booted (aka. /run/booted-system) because we can re-build |
407 | ;; a new system configuration and activate it, without rebooting. | |
b4140694 LC |
408 | "/run/current-system") |
409 | ||
410 | (define (boot-time-system) | |
411 | "Return the '--system' argument passed on the kernel command line." | |
412 | (find-long-option "--system" (linux-command-line))) | |
413 | ||
6d49355d LC |
414 | (define* (activate-current-system |
415 | #:optional (system (or (getenv "GUIX_NEW_SYSTEM") | |
416 | (boot-time-system)))) | |
484a2b3a | 417 | "Atomically make SYSTEM the current system." |
6d49355d LC |
418 | ;; The 'GUIX_NEW_SYSTEM' environment variable is used as a way for 'guix |
419 | ;; system reconfigure' to pass the file name of the new system. | |
420 | ||
b4140694 | 421 | (format #t "making '~a' the current system...~%" system) |
b4140694 LC |
422 | |
423 | ;; Atomically make SYSTEM current. | |
424 | (let ((new (string-append %current-system ".new"))) | |
425 | (symlink system new) | |
426 | (rename-file new %current-system))) | |
427 | ||
4dfe6c58 | 428 | ;;; activation.scm ends here |