shadow.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Alex Griffin <a@ajgrf.com>
  4. ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  5. ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (gnu system shadow)
  22. #:use-module ((guix diagnostics) #:select (formatted-message))
  23. #:use-module (guix records)
  24. #:use-module (guix gexp)
  25. #:use-module (guix store)
  26. #:use-module (guix modules)
  27. #:use-module (guix sets)
  28. #:use-module (guix ui)
  29. #:use-module (gnu system accounts)
  30. #:use-module (gnu services)
  31. #:use-module (gnu services shepherd)
  32. #:use-module ((gnu system file-systems)
  33. #:select (%tty-gid))
  34. #:use-module ((gnu packages admin)
  35. #:select (shadow))
  36. #:use-module (gnu packages bash)
  37. #:use-module (ice-9 match)
  38. #:use-module (srfi srfi-1)
  39. #:use-module (srfi srfi-26)
  40. #:use-module (srfi srfi-34)
  41. #:use-module (srfi srfi-35)
  42. ;; Re-export these bindings for backward compatibility.
  43. #:re-export (user-account
  44. user-account?
  45. user-account-name
  46. user-account-password
  47. user-account-uid
  48. user-account-group
  49. user-account-supplementary-groups
  50. user-account-comment
  51. user-account-home-directory
  52. user-account-create-home-directory?
  53. user-account-shell
  54. user-account-system?
  55. user-group
  56. user-group?
  57. user-group-name
  58. user-group-password
  59. user-group-id
  60. user-group-system?)
  61. #:export (default-skeletons
  62. skeleton-directory
  63. %base-groups
  64. %base-user-accounts
  65. account-service-type
  66. account-service))
  67. ;;; Commentary:
  68. ;;;
  69. ;;; Utilities for configuring the Shadow tool suite ('login', 'passwd', etc.)
  70. ;;;
  71. ;;; Code:
  72. ;; Change the default shell used by new <user-account> records.
  73. (default-shell (file-append bash "/bin/bash"))
  74. (define %base-groups
  75. ;; Default set of groups.
  76. (let-syntax ((system-group (syntax-rules ()
  77. ((_ args ...)
  78. (user-group (system? #t) args ...)))))
  79. (list (system-group (name "root") (id 0))
  80. (system-group (name "wheel")) ; root-like users
  81. (system-group (name "users")) ; normal users
  82. (system-group (name "nogroup")) ; for daemons etc.
  83. ;; The following groups are conventionally used by things like udev to
  84. ;; control access to hardware devices.
  85. (system-group (name "tty") (id %tty-gid))
  86. (system-group (name "dialout"))
  87. (system-group (name "kmem"))
  88. (system-group (name "input")) ; input devices, from udev
  89. (system-group (name "video"))
  90. (system-group (name "audio"))
  91. (system-group (name "netdev")) ; used in avahi-dbus.conf
  92. (system-group (name "lp"))
  93. (system-group (name "disk"))
  94. (system-group (name "floppy"))
  95. (system-group (name "cdrom"))
  96. (system-group (name "tape"))
  97. (system-group (name "kvm"))))) ; for /dev/kvm
  98. (define %base-user-accounts
  99. ;; List of standard user accounts. Note that "root" is a special case, so
  100. ;; it's not listed here.
  101. (list (user-account
  102. (name "nobody")
  103. (uid 65534)
  104. (group "nogroup")
  105. (shell (file-append shadow "/sbin/nologin"))
  106. (home-directory "/nonexistent")
  107. (create-home-directory? #f)
  108. (system? #t))))
  109. (define (default-skeletons)
  110. "Return the default skeleton files for /etc/skel. These files are copied by
  111. 'useradd' in the home directory of newly created user accounts."
  112. (let ((profile (plain-file "bash_profile" "\
  113. # Honor per-interactive-shell startup file
  114. if [ -f ~/.bashrc ]; then . ~/.bashrc; fi\n"))
  115. (bashrc (plain-file "bashrc" "\
  116. # Bash initialization for interactive non-login shells and
  117. # for remote shells (info \"(bash) Bash Startup Files\").
  118. # Export 'SHELL' to child processes. Programs such as 'screen'
  119. # honor it and otherwise use /bin/sh.
  120. export SHELL
  121. if [[ $- != *i* ]]
  122. then
  123. # We are being invoked from a non-interactive shell. If this
  124. # is an SSH session (as in \"ssh host command\"), source
  125. # /etc/profile so we get PATH and other essential variables.
  126. [[ -n \"$SSH_CLIENT\" ]] && source /etc/profile
  127. # Don't do anything else.
  128. return
  129. fi
  130. # Source the system-wide file.
  131. source /etc/bashrc
  132. # Adjust the prompt depending on whether we're in 'guix environment'.
  133. if [ -n \"$GUIX_ENVIRONMENT\" ]
  134. then
  135. PS1='\\u@\\h \\w [env]\\$ '
  136. else
  137. PS1='\\u@\\h \\w\\$ '
  138. fi
  139. alias ls='ls -p --color=auto'
  140. alias ll='ls -l'
  141. alias grep='grep --color=auto'\n"))
  142. (zprofile (plain-file "zprofile" "\
  143. # Honor system-wide environment variables
  144. source /etc/profile\n"))
  145. (xdefaults (plain-file "Xdefaults" "\
  146. XTerm*utf8: always
  147. XTerm*metaSendsEscape: true\n"))
  148. (gdbinit (plain-file "gdbinit" "\
  149. # Tell GDB where to look for separate debugging files.
  150. set debug-file-directory ~/.guix-profile/lib/debug
  151. # Authorize extensions found in the store, such as the
  152. # pretty-printers of libstdc++.
  153. set auto-load safe-path /gnu/store/*/lib\n")))
  154. `((".bash_profile" ,profile)
  155. (".bashrc" ,bashrc)
  156. ;; Zsh sources ~/.zprofile before ~/.zshrc, and it sources ~/.zlogin
  157. ;; after ~/.zshrc. To avoid interfering with any customizations a user
  158. ;; may have made in their ~/.zshrc, put this in .zprofile, not .zlogin.
  159. (".zprofile" ,zprofile)
  160. (".nanorc" ,(plain-file "nanorc" "\
  161. # Include all the syntax highlighting modules.
  162. include /run/current-system/profile/share/nano/*.nanorc\n"))
  163. (".Xdefaults" ,xdefaults)
  164. (".guile" ,(plain-file "dot-guile"
  165. "(cond ((false-if-exception (resolve-interface '(ice-9 readline)))
  166. =>
  167. (lambda (module)
  168. ;; Enable completion and input history at the REPL.
  169. ((module-ref module 'activate-readline))))
  170. (else
  171. (display \"Consider installing the 'guile-readline' package for
  172. convenient interactive line editing and input history.\\n\\n\")))
  173. (unless (getenv \"INSIDE_EMACS\")
  174. (cond ((false-if-exception (resolve-interface '(ice-9 colorized)))
  175. =>
  176. (lambda (module)
  177. ;; Enable completion and input history at the REPL.
  178. ((module-ref module 'activate-colorized))))
  179. (else
  180. (display \"Consider installing the 'guile-colorized' package
  181. for a colorful Guile experience.\\n\\n\"))))\n"))
  182. (".gdbinit" ,gdbinit))))
  183. (define (skeleton-directory skeletons)
  184. "Return a directory containing SKELETONS, a list of name/derivation tuples."
  185. (computed-file "skel"
  186. (with-imported-modules '((guix build utils))
  187. #~(begin
  188. (use-modules (ice-9 match)
  189. (guix build utils))
  190. (mkdir #$output)
  191. (chdir #$output)
  192. ;; Note: copy the skeletons instead of symlinking
  193. ;; them like 'file-union' does, because 'useradd'
  194. ;; would just copy the symlinks as is.
  195. (for-each (match-lambda
  196. ((target source)
  197. (copy-recursively source target)))
  198. '#$skeletons)
  199. ;; Make nanorc respect XDG_CONFIG_HOME.
  200. (when (file-exists? ".nanorc")
  201. (mkdir-p ".config/nano")
  202. (rename-file ".nanorc" ".config/nano/nanorc"))
  203. #t))))
  204. (define (find-duplicates list)
  205. "Find duplicate entries in @var{list}.
  206. Two entries are considered duplicates, if they are @code{equal?} to each other.
  207. This implementation is made asymptotically faster than @code{delete-duplicates}
  208. through the internal use of hash tables."
  209. (let loop ((list list)
  210. ;; We actually modify table in-place, but still allocate it here
  211. ;; so that we only need one level of indentation.
  212. (table (make-hash-table)))
  213. (match list
  214. (()
  215. (hash-fold (lambda (key value seed)
  216. (if (> value 1)
  217. (cons key seed)
  218. seed))
  219. '()
  220. table))
  221. ((first . rest)
  222. (hash-set! table first
  223. (1+ (hash-ref table first 0)))
  224. (loop rest table)))))
  225. (define (assert-unique-account-names users)
  226. (match (find-duplicates (map user-account-name users))
  227. (() *unspecified*)
  228. (duplicates
  229. (warning
  230. (G_ "the following accounts appear more than once:~{ ~a~}~%")
  231. duplicates))))
  232. (define (assert-unique-group-names groups)
  233. (match (find-duplicates (map user-group-name groups))
  234. (() *unspecified*)
  235. (duplicates
  236. (warning
  237. (G_ "the following groups appear more than once:~{ ~a~}~%")
  238. duplicates))))
  239. (define (assert-valid-users/groups users groups)
  240. "Raise an error if USERS refer to groups not listed in GROUPS."
  241. (let ((groups (list->set (map user-group-name groups))))
  242. (define (validate-supplementary-group user group)
  243. (unless (set-contains? groups group)
  244. (raise (condition
  245. (&message
  246. (message
  247. (format #f (G_ "supplementary group '~a' \
  248. of user '~a' is undeclared")
  249. group
  250. (user-account-name user))))))))
  251. (for-each (lambda (user)
  252. (unless (set-contains? groups (user-account-group user))
  253. (raise (condition
  254. (&message
  255. (message
  256. (format #f (G_ "primary group '~a' \
  257. of user '~a' is undeclared")
  258. (user-account-group user)
  259. (user-account-name user)))))))
  260. (for-each (cut validate-supplementary-group user <>)
  261. (user-account-supplementary-groups user)))
  262. users)))
  263. ;;;
  264. ;;; Service.
  265. ;;;
  266. (define (user-group->gexp group)
  267. "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
  268. 'active-groups'."
  269. #~(list #$(user-group-name group)
  270. #$(user-group-password group)
  271. #$(user-group-id group)
  272. #$(user-group-system? group)))
  273. (define (user-account->gexp account)
  274. "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
  275. 'activate-users'."
  276. #~`(#$(user-account-name account)
  277. #$(user-account-uid account)
  278. #$(user-account-group account)
  279. #$(user-account-supplementary-groups account)
  280. #$(user-account-comment account)
  281. #$(user-account-home-directory account)
  282. #$(user-account-create-home-directory? account)
  283. ,#$(user-account-shell account) ; this one is a gexp
  284. #$(user-account-password account)
  285. #$(user-account-system? account)))
  286. (define (account-activation accounts+groups)
  287. "Return a gexp that activates ACCOUNTS+GROUPS, a list of <user-account> and
  288. <user-group> objects. Raise an error if a user account refers to a undefined
  289. group."
  290. (define accounts
  291. (delete-duplicates (filter user-account? accounts+groups) eq?))
  292. (define user-specs
  293. (map user-account->gexp accounts))
  294. (define groups
  295. (delete-duplicates (filter user-group? accounts+groups) eq?))
  296. (define group-specs
  297. (map user-group->gexp groups))
  298. (assert-unique-account-names accounts)
  299. (assert-unique-group-names groups)
  300. (assert-valid-users/groups accounts groups)
  301. ;; Add users and user groups.
  302. (with-imported-modules (source-module-closure '((gnu system accounts)))
  303. #~(begin
  304. (use-modules (gnu system accounts))
  305. (activate-users+groups (map sexp->user-account (list #$@user-specs))
  306. (map sexp->user-group (list #$@group-specs))))))
  307. (define (account-shepherd-service accounts+groups)
  308. "Return a Shepherd service that creates the home directories for the user
  309. accounts among ACCOUNTS+GROUPS."
  310. (define accounts
  311. (filter user-account? accounts+groups))
  312. ;; Create home directories only once 'file-systems' is up. This makes sure
  313. ;; they are created in the right place if /home lives on a separate
  314. ;; partition.
  315. ;;
  316. ;; XXX: We arrange for this service to stop right after it's done its job so
  317. ;; that 'guix system reconfigure' knows that it can reload it fearlessly
  318. ;; (and thus create new home directories).
  319. (list (shepherd-service
  320. (requirement '(file-systems))
  321. (provision '(user-homes))
  322. (one-shot? #t)
  323. (modules '((gnu build activation)
  324. (gnu system accounts)))
  325. (start (with-imported-modules (source-module-closure
  326. '((gnu build activation)
  327. (gnu system accounts)))
  328. #~(lambda ()
  329. (activate-user-home
  330. (map sexp->user-account
  331. (list #$@(map user-account->gexp accounts))))
  332. #t))) ;success
  333. (documentation "Create user home directories."))))
  334. (define (shells-file shells)
  335. "Return a file-like object that builds a shell list for use as /etc/shells
  336. based on SHELLS. /etc/shells is used by xterm, polkit, and other programs."
  337. (computed-file "shells"
  338. #~(begin
  339. (use-modules (srfi srfi-1))
  340. (define shells
  341. (delete-duplicates (list #$@shells)))
  342. (call-with-output-file #$output
  343. (lambda (port)
  344. (display "\
  345. /bin/sh
  346. /run/current-system/profile/bin/sh
  347. /run/current-system/profile/bin/bash\n" port)
  348. (for-each (lambda (shell)
  349. (display shell port)
  350. (newline port))
  351. shells))))))
  352. (define (etc-files arguments)
  353. "Filter out among ARGUMENTS things corresponding to skeletons, and return
  354. the /etc/skel directory for those."
  355. (let ((skels (filter pair? arguments))
  356. (users (filter user-account? arguments)))
  357. `(("skel" ,(skeleton-directory skels))
  358. ("shells" ,(shells-file (map user-account-shell users))))))
  359. (define account-service-type
  360. (service-type (name 'account)
  361. ;; Concatenate <user-account>, <user-group>, and skeleton
  362. ;; lists.
  363. (compose concatenate)
  364. (extend append)
  365. (extensions
  366. (list (service-extension activation-service-type
  367. account-activation)
  368. (service-extension shepherd-root-service-type
  369. account-shepherd-service)
  370. ;; Have 'user-processes' depend on 'user-homes' so that
  371. ;; daemons start after their home directory has been
  372. ;; created.
  373. (service-extension user-processes-service-type
  374. (const '(user-homes)))
  375. (service-extension etc-service-type
  376. etc-files)))
  377. (description
  378. "Ensure the specified user accounts and groups exist, as well
  379. as each account home directory.")))
  380. (define (account-service accounts+groups skeletons)
  381. "Return a <service> that takes care of user accounts and user groups, with
  382. ACCOUNTS+GROUPS as its initial list of accounts and groups."
  383. (service account-service-type
  384. (append skeletons accounts+groups)))
  385. ;;; shadow.scm ends here