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