Merge remote-tracking branch 'origin/master' into core-updates
[jackhill/guix/guix.git] / gnu / system / shadow.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Alex Griffin <a@ajgrf.com>
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
20 (define-module (gnu system shadow)
21 #:use-module (guix records)
22 #:use-module (guix gexp)
23 #:use-module (guix store)
24 #:use-module (guix sets)
25 #:use-module (guix ui)
26 #:use-module (gnu services)
27 #:use-module ((gnu system file-systems)
28 #:select (%tty-gid))
29 #:use-module ((gnu packages admin)
30 #:select (shadow))
31 #:use-module (gnu packages bash)
32 #:use-module (gnu packages guile-wm)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
37 #:export (user-account
38 user-account?
39 user-account-name
40 user-account-password
41 user-account-uid
42 user-account-group
43 user-account-supplementary-groups
44 user-account-comment
45 user-account-home-directory
46 user-account-shell
47 user-account-system?
48
49 user-group
50 user-group?
51 user-group-name
52 user-group-password
53 user-group-id
54 user-group-system?
55
56 default-skeletons
57 skeleton-directory
58 %base-groups
59 %base-user-accounts
60
61 account-service-type
62 account-service))
63
64 ;;; Commentary:
65 ;;;
66 ;;; Utilities for configuring the Shadow tool suite ('login', 'passwd', etc.)
67 ;;;
68 ;;; Code:
69
70 (define-record-type* <user-account>
71 user-account make-user-account
72 user-account?
73 (name user-account-name)
74 (password user-account-password (default #f))
75 (uid user-account-uid (default #f))
76 (group user-account-group) ; number | string
77 (supplementary-groups user-account-supplementary-groups
78 (default '())) ; list of strings
79 (comment user-account-comment (default ""))
80 (home-directory user-account-home-directory)
81 (shell user-account-shell ; gexp
82 (default #~(string-append #$bash "/bin/bash")))
83 (system? user-account-system? ; Boolean
84 (default #f)))
85
86 (define-record-type* <user-group>
87 user-group make-user-group
88 user-group?
89 (name user-group-name)
90 (password user-group-password (default #f))
91 (id user-group-id (default #f))
92 (system? user-group-system? ; Boolean
93 (default #f)))
94
95
96 (define %base-groups
97 ;; Default set of groups.
98 (let-syntax ((system-group (syntax-rules ()
99 ((_ args ...)
100 (user-group (system? #t) args ...)))))
101 (list (system-group (name "root") (id 0))
102 (system-group (name "wheel")) ; root-like users
103 (system-group (name "users")) ; normal users
104 (system-group (name "nogroup")) ; for daemons etc.
105
106 ;; The following groups are conventionally used by things like udev to
107 ;; control access to hardware devices.
108 (system-group (name "tty") (id %tty-gid))
109 (system-group (name "dialout"))
110 (system-group (name "kmem"))
111 (system-group (name "input")) ; input devices, from udev
112 (system-group (name "video"))
113 (system-group (name "audio"))
114 (system-group (name "netdev")) ; used in avahi-dbus.conf
115 (system-group (name "lp"))
116 (system-group (name "disk"))
117 (system-group (name "floppy"))
118 (system-group (name "cdrom"))
119 (system-group (name "tape"))
120 (system-group (name "kvm"))))) ; for /dev/kvm
121
122 (define %base-user-accounts
123 ;; List of standard user accounts. Note that "root" is a special case, so
124 ;; it's not listed here.
125 (list (user-account
126 (name "nobody")
127 (uid 65534)
128 (group "nogroup")
129 (shell #~(string-append #$shadow "/sbin/nologin"))
130 (home-directory "/nonexistent")
131 (system? #t))))
132
133 (define (default-skeletons)
134 "Return the default skeleton files for /etc/skel. These files are copied by
135 'useradd' in the home directory of newly created user accounts."
136 (define copy-guile-wm
137 (with-imported-modules '((guix build utils))
138 #~(begin
139 (use-modules (guix build utils))
140 (copy-file (car (find-files #$guile-wm "wm-init-sample.scm"))
141 #$output))))
142
143 (let ((profile (plain-file "bash_profile" "\
144 # Honor per-interactive-shell startup file
145 if [ -f ~/.bashrc ]; then . ~/.bashrc; fi\n"))
146 (bashrc (plain-file "bashrc" "\
147 # Bash initialization for interactive non-login shells and
148 # for remote shells (info \"(bash) Bash Startup Files\").
149
150 # Export 'SHELL' to child processes. Programs such as 'screen'
151 # honor it and otherwise use /bin/sh.
152 export SHELL
153
154 if [ -n \"$SSH_CLIENT\" -a -z \"`type -P cat`\" ]
155 then
156 # We are being invoked from a non-interactive SSH session
157 # (as in \"ssh host command\") but 'cat' cannot be found
158 # in $PATH. Source /etc/profile so we get $PATH and other
159 # essential variables.
160 source /etc/profile
161 fi
162
163 # Adjust the prompt depending on whether we're in 'guix environment'.
164 if [ -n \"$GUIX_ENVIRONMENT\" ]
165 then
166 PS1='\\u@\\h \\w [env]\\$ '
167 else
168 PS1='\\u@\\h \\w\\$ '
169 fi
170 alias ls='ls -p --color'
171 alias ll='ls -l'\n"))
172 (zlogin (plain-file "zlogin" "\
173 # Honor system-wide environment variables
174 source /etc/profile\n"))
175 (guile-wm (computed-file "guile-wm" copy-guile-wm))
176 (xdefaults (plain-file "Xdefaults" "\
177 XTerm*utf8: always
178 XTerm*metaSendsEscape: true\n"))
179 (gdbinit (plain-file "gdbinit" "\
180 # Tell GDB where to look for separate debugging files.
181 set debug-file-directory ~/.guix-profile/lib/debug\n")))
182 `((".bash_profile" ,profile)
183 (".bashrc" ,bashrc)
184 (".zlogin" ,zlogin)
185 (".Xdefaults" ,xdefaults)
186 (".guile-wm" ,guile-wm)
187 (".gdbinit" ,gdbinit))))
188
189 (define (skeleton-directory skeletons)
190 "Return a directory containing SKELETONS, a list of name/derivation tuples."
191 (computed-file "skel"
192 (with-imported-modules '((guix build utils))
193 #~(begin
194 (use-modules (ice-9 match)
195 (guix build utils))
196
197 (mkdir #$output)
198 (chdir #$output)
199
200 ;; Note: copy the skeletons instead of symlinking
201 ;; them like 'file-union' does, because 'useradd'
202 ;; would just copy the symlinks as is.
203 (for-each (match-lambda
204 ((target source)
205 (copy-recursively source target)))
206 '#$skeletons)
207 #t))))
208
209 (define (assert-valid-users/groups users groups)
210 "Raise an error if USERS refer to groups not listed in GROUPS."
211 (let ((groups (list->set (map user-group-name groups))))
212 (define (validate-supplementary-group user group)
213 (unless (set-contains? groups group)
214 (raise (condition
215 (&message
216 (message
217 (format #f (_ "supplementary group '~a' \
218 of user '~a' is undeclared")
219 group
220 (user-account-name user))))))))
221
222 (for-each (lambda (user)
223 (unless (set-contains? groups (user-account-group user))
224 (raise (condition
225 (&message
226 (message
227 (format #f (_ "primary group '~a' \
228 of user '~a' is undeclared")
229 (user-account-group user)
230 (user-account-name user)))))))
231
232 (for-each (cut validate-supplementary-group user <>)
233 (user-account-supplementary-groups user)))
234 users)))
235
236 \f
237 ;;;
238 ;;; Service.
239 ;;;
240
241 (define (user-group->gexp group)
242 "Turn GROUP, a <user-group> object, into a list-valued gexp suitable for
243 'active-groups'."
244 #~(list #$(user-group-name group)
245 #$(user-group-password group)
246 #$(user-group-id group)
247 #$(user-group-system? group)))
248
249 (define (user-account->gexp account)
250 "Turn ACCOUNT, a <user-account> object, into a list-valued gexp suitable for
251 'activate-users'."
252 #~`(#$(user-account-name account)
253 #$(user-account-uid account)
254 #$(user-account-group account)
255 #$(user-account-supplementary-groups account)
256 #$(user-account-comment account)
257 #$(user-account-home-directory account)
258 ,#$(user-account-shell account) ; this one is a gexp
259 #$(user-account-password account)
260 #$(user-account-system? account)))
261
262 (define (account-activation accounts+groups)
263 "Return a gexp that activates ACCOUNTS+GROUPS, a list of <user-account> and
264 <user-group> objects. Raise an error if a user account refers to a undefined
265 group."
266 (define accounts
267 (filter user-account? accounts+groups))
268
269 (define user-specs
270 (map user-account->gexp accounts))
271
272 (define groups
273 (filter user-group? accounts+groups))
274
275 (define group-specs
276 (map user-group->gexp groups))
277
278 (assert-valid-users/groups accounts groups)
279
280 ;; Add users and user groups.
281 #~(begin
282 (setenv "PATH"
283 (string-append #$(@ (gnu packages admin) shadow) "/sbin"))
284 (activate-users+groups (list #$@user-specs)
285 (list #$@group-specs))))
286
287 (define (shells-file shells)
288 "Return a file-like object that builds a shell list for use as /etc/shells
289 based on SHELLS. /etc/shells is used by xterm, polkit, and other programs."
290 (computed-file "shells"
291 #~(begin
292 (use-modules (srfi srfi-1))
293
294 (define shells
295 (delete-duplicates (list #$@shells)))
296
297 (call-with-output-file #$output
298 (lambda (port)
299 (display "\
300 /bin/sh
301 /run/current-system/profile/bin/sh
302 /run/current-system/profile/bin/bash\n" port)
303 (for-each (lambda (shell)
304 (display shell port)
305 (newline port))
306 shells))))))
307 (define (etc-files arguments)
308 "Filter out among ARGUMENTS things corresponding to skeletons, and return
309 the /etc/skel directory for those."
310 (let ((skels (filter pair? arguments))
311 (users (filter user-account? arguments)))
312 `(("skel" ,(skeleton-directory skels))
313 ("shells" ,(shells-file (map user-account-shell users))))))
314
315 (define account-service-type
316 (service-type (name 'account)
317
318 ;; Concatenate <user-account>, <user-group>, and skeleton
319 ;; lists.
320 (compose concatenate)
321 (extend append)
322
323 (extensions
324 (list (service-extension activation-service-type
325 account-activation)
326 (service-extension etc-service-type
327 etc-files)))))
328
329 (define (account-service accounts+groups skeletons)
330 "Return a <service> that takes care of user accounts and user groups, with
331 ACCOUNTS+GROUPS as its initial list of accounts and groups."
332 (service account-service-type
333 (append skeletons accounts+groups)))
334
335 ;;; shadow.scm ends here