activation.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2013 Andreas Enge <andreas@enge.fr>
  5. ;;; Copyright © 2015, 2018 Mark H Weaver <mhw@netris.org>
  6. ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
  7. ;;; Copyright © 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
  8. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (gnu build activation)
  25. #:use-module (gnu system accounts)
  26. #:use-module (gnu build accounts)
  27. #:use-module (gnu build linux-boot)
  28. #:use-module (guix build utils)
  29. #:use-module ((guix build syscalls) #:select (with-file-lock))
  30. #:use-module (ice-9 ftw)
  31. #:use-module (ice-9 match)
  32. #:use-module (ice-9 vlist)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-11)
  35. #:use-module (srfi srfi-26)
  36. #:export (activate-users+groups
  37. activate-user-home
  38. activate-etc
  39. activate-setuid-programs
  40. activate-special-files
  41. activate-modprobe
  42. activate-firmware
  43. activate-ptrace-attach
  44. activate-current-system
  45. mkdir-p/perms))
  46. ;;; Commentary:
  47. ;;;
  48. ;;; This module provides "activation" helpers. Activation is the process that
  49. ;;; consists in setting up system-wide files and directories so that an
  50. ;;; 'operating-system' configuration becomes active.
  51. ;;;
  52. ;;; Code:
  53. (define %skeleton-directory
  54. ;; Directory containing skeleton files for new accounts.
  55. ;; Note: keep the trailing '/' so that 'scandir' enters it.
  56. "/etc/skel/")
  57. (define (dot-or-dot-dot? file)
  58. (member file '("." "..")))
  59. ;; Based upon mkdir-p from (guix build utils)
  60. (define (verify-not-symbolic dir)
  61. "Verify DIR or its ancestors aren't symbolic links."
  62. (define absolute?
  63. (string-prefix? "/" dir))
  64. (define not-slash
  65. (char-set-complement (char-set #\/)))
  66. (define (verify-component file)
  67. (unless (eq? 'directory (stat:type (lstat file)))
  68. (error "file name component is not a directory" dir)))
  69. (let loop ((components (string-tokenize dir not-slash))
  70. (root (if absolute?
  71. ""
  72. ".")))
  73. (match components
  74. ((head tail ...)
  75. (let ((file (string-append root "/" head)))
  76. (catch 'system-error
  77. (lambda ()
  78. (verify-component file)
  79. (loop tail file))
  80. (lambda args
  81. (if (= ENOENT (system-error-errno args))
  82. #t
  83. (apply throw args))))))
  84. (() #t))))
  85. ;; TODO: the TOCTTOU race can be addressed once guile has bindings
  86. ;; for fstatat, openat and friends.
  87. (define (mkdir-p/perms directory owner bits)
  88. "Create the directory DIRECTORY and all its ancestors.
  89. Verify no component of DIRECTORY is a symbolic link.
  90. Warning: this is currently suspect to a TOCTTOU race!"
  91. (verify-not-symbolic directory)
  92. (mkdir-p directory)
  93. (chown directory (passwd:uid owner) (passwd:gid owner))
  94. (chmod directory bits))
  95. (define* (copy-account-skeletons home
  96. #:key
  97. (directory %skeleton-directory)
  98. uid gid)
  99. "Copy the account skeletons from DIRECTORY to HOME. When UID is an integer,
  100. make it the owner of all the files created; likewise for GID."
  101. (define (set-owner file)
  102. (when (or uid gid)
  103. (chown file (or uid -1) (or gid -1))))
  104. (let ((files (scandir directory (negate dot-or-dot-dot?)
  105. string<?)))
  106. (mkdir-p home)
  107. (set-owner home)
  108. (for-each (lambda (file)
  109. (let ((target (string-append home "/" file)))
  110. (copy-recursively (string-append directory "/" file)
  111. target
  112. #:log (%make-void-port "w"))
  113. (for-each set-owner
  114. (find-files target (const #t)
  115. #:directories? #t))
  116. (make-file-writable target)))
  117. files)))
  118. (define* (make-skeletons-writable home
  119. #:optional (directory %skeleton-directory))
  120. "Make sure that the files that have been copied from DIRECTORY to HOME are
  121. owner-writable in HOME."
  122. (let ((files (scandir directory (negate dot-or-dot-dot?)
  123. string<?)))
  124. (for-each (lambda (file)
  125. (let ((target (string-append home "/" file)))
  126. (when (file-exists? target)
  127. (make-file-writable target))))
  128. files)))
  129. (define (duplicates lst)
  130. "Return elements from LST present more than once in LST."
  131. (let loop ((lst lst)
  132. (seen vlist-null)
  133. (result '()))
  134. (match lst
  135. (()
  136. (reverse result))
  137. ((head . tail)
  138. (loop tail
  139. (vhash-cons head #t seen)
  140. (if (vhash-assoc head seen)
  141. (cons head result)
  142. result))))))
  143. (define (activate-users+groups users groups)
  144. "Make sure USERS (a list of user account records) and GROUPS (a list of user
  145. group records) are all available."
  146. (define (make-home-directory user)
  147. (let ((home (user-account-home-directory user))
  148. (pwd (getpwnam (user-account-name user))))
  149. (mkdir-p home)
  150. ;; Always set ownership and permissions for home directories of system
  151. ;; accounts. If a service needs looser permissions on its home
  152. ;; directories, it can always chmod it in an activation snippet.
  153. (chown home (passwd:uid pwd) (passwd:gid pwd))
  154. (chmod home #o700)))
  155. (define system-accounts
  156. (filter (lambda (user)
  157. (and (user-account-system? user)
  158. (user-account-create-home-directory? user)))
  159. users))
  160. ;; Allow home directories to be created under /var/lib.
  161. (mkdir-p "/var/lib")
  162. ;; Take same lock as libc's 'lckpwdf' (but without a timeout) while we read
  163. ;; and write the databases. This ensures there's no race condition with
  164. ;; other tools that might be accessing it at the same time.
  165. (with-file-lock %password-lock-file
  166. (let-values (((groups passwd shadow)
  167. (user+group-databases users groups)))
  168. (write-group groups)
  169. (write-passwd passwd)
  170. (write-shadow shadow)))
  171. ;; Home directories of non-system accounts are created by
  172. ;; 'activate-user-home'.
  173. (for-each make-home-directory system-accounts)
  174. ;; Turn shared home directories, such as /var/empty, into root-owned,
  175. ;; read-only places.
  176. (for-each (lambda (directory)
  177. (chown directory 0 0)
  178. (chmod directory #o555))
  179. (duplicates (map user-account-home-directory system-accounts))))
  180. (define (activate-user-home users)
  181. "Create and populate the home directory of USERS, a list of tuples, unless
  182. they already exist."
  183. (define ensure-user-home
  184. (lambda (user)
  185. (let ((name (user-account-name user))
  186. (home (user-account-home-directory user))
  187. (create-home? (user-account-create-home-directory? user))
  188. (system? (user-account-system? user)))
  189. ;; The home directories of system accounts are created during
  190. ;; activation, not here.
  191. (unless (or (not home) (not create-home?) system?
  192. (directory-exists? home))
  193. (let* ((pw (getpwnam name))
  194. (uid (passwd:uid pw))
  195. (gid (passwd:gid pw)))
  196. (mkdir-p home)
  197. (chown home uid gid)
  198. (chmod home #o700)
  199. (copy-account-skeletons home
  200. #:uid uid #:gid gid))))))
  201. (for-each ensure-user-home users))
  202. (define (activate-etc etc)
  203. "Install ETC, a directory in the store, as the source of static files for
  204. /etc."
  205. ;; /etc is a mixture of static and dynamic settings. Here is where we
  206. ;; initialize it from the static part.
  207. (define (rm-f file)
  208. (false-if-exception (delete-file file)))
  209. (format #t "populating /etc from ~a...~%" etc)
  210. (mkdir-p "/etc")
  211. ;; Create the /etc/ssl -> /run/current-system/profile/etc/ssl symlink. This
  212. ;; symlink, to a target outside of the store, probably doesn't belong in the
  213. ;; static 'etc' store directory. However, if it were to be put there,
  214. ;; beware that if /run/current-system/profile/etc/ssl doesn't exist at the
  215. ;; time of activation (e.g. when installing a fresh system), the call to
  216. ;; 'file-is-directory?' below will fail because it uses 'stat', not 'lstat'.
  217. (rm-f "/etc/ssl")
  218. (symlink "/run/current-system/profile/etc/ssl" "/etc/ssl")
  219. (rm-f "/etc/static")
  220. (symlink etc "/etc/static")
  221. (for-each (lambda (file)
  222. (let ((target (string-append "/etc/" file))
  223. (source (string-append "/etc/static/" file)))
  224. (rm-f target)
  225. ;; Things such as /etc/sudoers must be regular files, not
  226. ;; symlinks; furthermore, they could be modified behind our
  227. ;; back---e.g., with 'visudo'. Thus, make a copy instead of
  228. ;; symlinking them.
  229. (if (file-is-directory? source)
  230. (symlink source target)
  231. (copy-file source target))
  232. ;; XXX: Dirty hack to meet sudo's expectations.
  233. (when (string=? (basename target) "sudoers")
  234. (chmod target #o440))))
  235. (scandir etc (negate dot-or-dot-dot?)
  236. ;; The default is 'string-locale<?', but we don't have
  237. ;; it when run from the initrd's statically-linked
  238. ;; Guile.
  239. string<?)))
  240. (define %setuid-directory
  241. ;; Place where setuid programs are stored.
  242. "/run/setuid-programs")
  243. (define (activate-setuid-programs programs)
  244. "Turn PROGRAMS, a list of file names, into setuid programs stored under
  245. %SETUID-DIRECTORY."
  246. (define (make-setuid-program prog)
  247. (let ((target (string-append %setuid-directory
  248. "/" (basename prog))))
  249. (copy-file prog target)
  250. (chown target 0 0)
  251. (chmod target #o4555)))
  252. (format #t "setting up setuid programs in '~a'...~%"
  253. %setuid-directory)
  254. (if (file-exists? %setuid-directory)
  255. (for-each (compose delete-file
  256. (cut string-append %setuid-directory "/" <>))
  257. (scandir %setuid-directory
  258. (lambda (file)
  259. (not (member file '("." ".."))))
  260. string<?))
  261. (mkdir-p %setuid-directory))
  262. (for-each (lambda (program)
  263. (catch 'system-error
  264. (lambda ()
  265. (make-setuid-program program))
  266. (lambda args
  267. ;; If we fail to create a setuid program, better keep going
  268. ;; so that we don't leave %SETUID-DIRECTORY empty or
  269. ;; half-populated. This can happen if PROGRAMS contains
  270. ;; incorrect file names: <https://bugs.gnu.org/38800>.
  271. (format (current-error-port)
  272. "warning: failed to make '~a' setuid-root: ~a~%"
  273. program (strerror (system-error-errno args))))))
  274. programs))
  275. (define (activate-special-files special-files)
  276. "Install the files listed in SPECIAL-FILES. Each element of SPECIAL-FILES
  277. is a pair where the first element is the name of the special file and the
  278. second element is the name it should appear at, such as:
  279. ((\"/bin/sh\" \"/gnu/store/…-bash/bin/sh\")
  280. (\"/usr/bin/env\" \"/gnu/store/…-coreutils/bin/env\"))
  281. "
  282. (define install-special-file
  283. (match-lambda
  284. ((target file)
  285. (let ((pivot (string-append target ".new")))
  286. (mkdir-p (dirname target))
  287. (symlink file pivot)
  288. (rename-file pivot target)))))
  289. (for-each install-special-file special-files))
  290. (define (activate-modprobe modprobe)
  291. "Tell the kernel to use MODPROBE to load modules."
  292. ;; If the kernel was built without loadable module support, this file is
  293. ;; unavailable, so check for its existence first.
  294. (when (file-exists? "/proc/sys/kernel/modprobe")
  295. (call-with-output-file "/proc/sys/kernel/modprobe"
  296. (lambda (port)
  297. (display modprobe port)))))
  298. (define (activate-firmware directory)
  299. "Tell the kernel to look for device firmware under DIRECTORY. This
  300. mechanism bypasses udev: it allows Linux to handle firmware loading directly
  301. by itself, without having to resort to a \"user helper\"."
  302. (call-with-output-file "/sys/module/firmware_class/parameters/path"
  303. (lambda (port)
  304. (display directory port))))
  305. (define (activate-ptrace-attach)
  306. "Allow users to PTRACE_ATTACH their own processes.
  307. This works around a regression introduced in the default \"security\" policy
  308. found in Linux 3.4 onward that prevents users from attaching to their own
  309. processes--see Yama.txt in the Linux source tree for the rationale. This
  310. sounds like an unacceptable restriction for little or no security
  311. improvement."
  312. (let ((file "/proc/sys/kernel/yama/ptrace_scope"))
  313. (when (file-exists? file)
  314. (call-with-output-file file
  315. (lambda (port)
  316. (display 0 port))))))
  317. (define %current-system
  318. ;; The system that is current (a symlink.) This is not necessarily the same
  319. ;; as the system we booted (aka. /run/booted-system) because we can re-build
  320. ;; a new system configuration and activate it, without rebooting.
  321. "/run/current-system")
  322. (define (boot-time-system)
  323. "Return the '--system' argument passed on the kernel command line."
  324. (find-long-option "--system" (if (string-contains %host-type "linux-gnu")
  325. (linux-command-line)
  326. (command-line))))
  327. (define* (activate-current-system
  328. #:optional (system (or (getenv "GUIX_NEW_SYSTEM")
  329. (boot-time-system))))
  330. "Atomically make SYSTEM the current system."
  331. ;; The 'GUIX_NEW_SYSTEM' environment variable is used as a way for 'guix
  332. ;; system reconfigure' to pass the file name of the new system.
  333. (format #t "making '~a' the current system...~%" system)
  334. ;; Atomically make SYSTEM current.
  335. (let ((new (string-append %current-system ".new")))
  336. (symlink system new)
  337. (rename-file new %current-system)))
  338. ;;; activation.scm ends here