1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (gnu build activation)
21 #:use-module (gnu build linux-boot)
22 #:use-module (guix build utils)
23 #:use-module (ice-9 ftw)
24 #:use-module (ice-9 match)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-26)
27 #:export (activate-users+groups
30 activate-setuid-programs
31 activate-special-files
34 activate-ptrace-attach
35 activate-current-system))
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.
45 (define (enumerate thunk)
46 "Return the list of values returned by THUNK until it returned #f."
47 (let loop ((entry (thunk))
51 (loop (thunk) (cons entry result)))))
53 (define (current-users)
54 "Return the passwd entries for all the currently defined user accounts."
58 (define (current-groups)
59 "Return the group entries for all the currently defined user groups."
63 (define* (add-group name #:key gid password system?
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) '())
70 ,@(if system? `("--system") '())
72 (zero? (apply system* "groupadd" args))))
74 (define %skeleton-directory
75 ;; Directory containing skeleton files for new accounts.
76 ;; Note: keep the trailing '/' so that 'scandir' enters it.
79 (define (dot-or-dot-dot? file)
80 (member file '("." "..")))
82 (define* (copy-account-skeletons home
84 (directory %skeleton-directory)
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)
90 (chown file (or uid -1) (or gid -1))))
92 (let ((files (scandir directory (negate dot-or-dot-dot?)
96 (for-each (lambda (file)
97 (let ((target (string-append home "/" file)))
98 (copy-recursively (string-append directory "/" file)
100 #:log (%make-void-port "w"))
102 (find-files target (const #t)
104 (make-file-writable target)))
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?)
113 (for-each (lambda (file)
114 (let ((target (string-append home "/" file)))
115 (when (file-exists? target)
116 (make-file-writable target))))
119 (define* (add-user name group
120 #:key uid comment home create-home?
121 shell password system?
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)
128 (if (and uid (zero? uid))
130 ;; 'useradd' fails with "Cannot determine your user name" if the root
131 ;; account doesn't exist. Thus, for bootstrapping purposes, create that
133 (let ((home (or home "/root")))
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)
140 (copy-account-skeletons home)
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 ","))
150 ,@(if comment `("-c" ,comment) '())
151 ,@(if home `("-d" ,home) '())
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)))
160 ,@(if shell `("-s" ,shell) '())
161 ,@(if password `("-p" ,password) '())
162 ,@(if system? '("--system") '())
164 (and (zero? (apply system* "useradd" args))
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
170 (make-skeletons-writable home)
173 (define* (modify-user name group
174 #:key uid comment home create-home?
175 shell password system?
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 ","))
185 ,@(if comment `("-c" ,comment) '())
186 ;; Don't use '--move-home'.
187 ,@(if home `("-d" ,home) '())
188 ,@(if shell `("-s" ,shell) '())
190 (zero? (apply system* "usermod" args))))
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
195 (format log-port "deleting user '~a'...~%" name)
196 (zero? (system* "userdel" name)))
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)))
203 (define* (ensure-user name group
204 #:key uid comment home create-home?
205 shell password system?
206 (supplementary-groups '())
207 (log-port (current-error-port))
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)))
214 (define (activate-users+groups users groups)
215 "Make sure the accounts listed in USERS and the user groups listed in GROUPS
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
222 (close-port (open-file file "a0b")))
224 (define activate-user
226 ((name uid group supplementary-groups comment home create-home?
227 shell password system?)
228 (let ((profile-dir (string-append "/var/guix/profiles/per-user/"
230 (ensure-user name group
233 #:supplementary-groups supplementary-groups
236 #:create-home? create-home?
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))))))))
247 ;; 'groupadd' aborts if the file doesn't already exist.
250 ;; Allow home directories to be created under /var/lib.
253 ;; Create the root account so we can use 'useradd' and 'groupadd'.
254 (activate-user (find (match-lambda
255 ((name (? zero?) _ ...) #t)
259 ;; Then create the groups.
260 (for-each (match-lambda
261 ((name password gid system?)
262 (unless (false-if-exception (getgrnam name))
264 #:gid gid #:password password
265 #:system? system?))))
268 ;; Create the other user accounts.
269 (for-each activate-user users)
271 ;; Finally, delete extra user accounts and groups.
272 (for-each delete-user
273 (lset-difference string=?
274 (map passwd:name (current-users))
278 (for-each delete-group
279 (lset-difference string=?
280 (map group:name (current-groups))
285 (define (activate-user-home users)
286 "Create and populate the home directory of USERS, a list of tuples, unless
288 (define ensure-user-home
290 ((name uid group supplementary-groups comment home create-home?
291 shell password system?)
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))
296 (let* ((pw (getpwnam name))
297 (uid (passwd:uid pw))
298 (gid (passwd:gid pw)))
302 (copy-account-skeletons home
303 #:uid uid #:gid gid)))))))
305 (for-each ensure-user-home users))
307 (define (activate-etc etc)
308 "Install ETC, a directory in the store, as the source of static files for
311 ;; /etc is a mixture of static and dynamic settings. Here is where we
312 ;; initialize it from the static part.
315 (false-if-exception (delete-file file)))
317 (format #t "populating /etc from ~a...~%" etc)
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'.
327 (symlink "/run/current-system/profile/etc/ssl" "/etc/ssl")
330 (symlink etc "/etc/static")
331 (for-each (lambda (file)
332 (let ((target (string-append "/etc/" file))
333 (source (string-append "/etc/static/" file)))
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
340 (if (file-is-directory? source)
341 (symlink source target)
342 (copy-file source target))
344 ;; XXX: Dirty hack to meet sudo's expectations.
345 (when (string=? (basename target) "sudoers")
346 (chmod target #o440))))
347 (scandir etc (negate dot-or-dot-dot?)
349 ;; The default is 'string-locale<?', but we don't have
350 ;; it when run from the initrd's statically-linked
354 (define %setuid-directory
355 ;; Place where setuid programs are stored.
356 "/run/setuid-programs")
358 (define (activate-setuid-programs programs)
359 "Turn PROGRAMS, a list of file names, into setuid programs stored under
361 (define (make-setuid-program prog)
362 (let ((target (string-append %setuid-directory
363 "/" (basename prog))))
364 (copy-file prog target)
366 (chmod target #o6555)))
368 (format #t "setting up setuid programs in '~a'...~%"
370 (if (file-exists? %setuid-directory)
371 (for-each (compose delete-file
372 (cut string-append %setuid-directory "/" <>))
373 (scandir %setuid-directory
375 (not (member file '("." ".."))))
377 (mkdir-p %setuid-directory))
379 (for-each make-setuid-program programs))
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:
386 ((\"/bin/sh\" \"/gnu/store/…-bash/bin/sh\")
387 (\"/usr/bin/env\" \"/gnu/store/…-coreutils/bin/env\"))
389 (define install-special-file
392 (let ((pivot (string-append target ".new")))
393 (mkdir-p (dirname target))
395 (rename-file pivot target)))))
397 (for-each install-special-file special-files))
399 (define (activate-modprobe modprobe)
400 "Tell the kernel to use MODPROBE to load modules."
401 (call-with-output-file "/proc/sys/kernel/modprobe"
403 (display modprobe port))))
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"
411 (display directory port))))
413 (define (activate-ptrace-attach)
414 "Allow users to PTRACE_ATTACH their own processes.
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
421 (let ((file "/proc/sys/kernel/yama/ptrace_scope"))
422 (when (file-exists? file)
423 (call-with-output-file file
425 (display 0 port))))))
428 (define %current-system
429 ;; The system that is current (a symlink.) This is not necessarily the same
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.
432 "/run/current-system")
434 (define (boot-time-system)
435 "Return the '--system' argument passed on the kernel command line."
436 (find-long-option "--system" (linux-command-line)))
438 (define* (activate-current-system
439 #:optional (system (or (getenv "GUIX_NEW_SYSTEM")
440 (boot-time-system))))
441 "Atomically make SYSTEM the current system."
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.
445 (format #t "making '~a' the current system...~%" system)
447 ;; Atomically make SYSTEM current.
448 (let ((new (string-append %current-system ".new")))
450 (rename-file new %current-system)))
452 ;;; activation.scm ends here