summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2015-02-04 21:58:15 +0100
committerLudovic Courtès <ludo@gnu.org>2015-02-04 22:49:35 +0100
commit996ed73948e92eb2005a2a282856753d707f452c (patch)
tree3dd45afdbbb22c01652cd2df9b7b61742440c6ab
parentffd74de0c6de7435fcdc8e340efade032c365081 (diff)
system: Add bindings to configure libc's NSS.
* gnu/system/nss.scm: New file. * gnu-system.am (GNU_SYSTEM_MODULES): Add it. * gnu.scm (%public-modules): Add it. * gnu/system.scm (<operating-system>)[name-service-switch]: New field. (etc-directory): Add #:nss parameter and honor it. (operating-system-etc-directory): Adjust call accordingly. * doc/guix.texi (operating-system Reference): Document 'name-service-switch'. (Name Service Switch): New section.
-rw-r--r--doc/guix.texi116
-rw-r--r--gnu-system.am1
-rw-r--r--gnu.scm3
-rw-r--r--gnu/system.scm10
-rw-r--r--gnu/system/nss.scm213
5 files changed, 338 insertions, 5 deletions
diff --git a/doc/guix.texi b/doc/guix.texi
index fa3aa6d66d9..e489d414bca 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -142,6 +142,7 @@ System Configuration
142* Locales:: Language and cultural convention settings. 142* Locales:: Language and cultural convention settings.
143* Services:: Specifying system services. 143* Services:: Specifying system services.
144* Setuid Programs:: Programs running with root privileges. 144* Setuid Programs:: Programs running with root privileges.
145* Name Service Switch:: Configuring libc's name service switch.
145* Initial RAM Disk:: Linux-Libre bootstrapping. 146* Initial RAM Disk:: Linux-Libre bootstrapping.
146* GRUB Configuration:: Configuring the boot loader. 147* GRUB Configuration:: Configuring the boot loader.
147* Invoking guix system:: Instantiating a system configuration. 148* Invoking guix system:: Instantiating a system configuration.
@@ -3642,6 +3643,7 @@ instance to support new system services.
3642* Locales:: Language and cultural convention settings. 3643* Locales:: Language and cultural convention settings.
3643* Services:: Specifying system services. 3644* Services:: Specifying system services.
3644* Setuid Programs:: Programs running with root privileges. 3645* Setuid Programs:: Programs running with root privileges.
3646* Name Service Switch:: Configuring libc's name service switch.
3645* Initial RAM Disk:: Linux-Libre bootstrapping. 3647* Initial RAM Disk:: Linux-Libre bootstrapping.
3646* GRUB Configuration:: Configuring the boot loader. 3648* GRUB Configuration:: Configuring the boot loader.
3647* Invoking guix system:: Instantiating a system configuration. 3649* Invoking guix system:: Instantiating a system configuration.
@@ -3827,6 +3829,11 @@ Library Reference Manual}). @xref{Locales}, for more information.
3827The list of locale definitions to be compiled and that may be used at 3829The list of locale definitions to be compiled and that may be used at
3828run time. @xref{Locales}. 3830run time. @xref{Locales}.
3829 3831
3832@item @code{name-service-switch} (default: @var{%default-nss})
3833Configuration of libc's name service switch (NSS)---a
3834@code{<name-service-switch>} object. @xref{Name Service Switch}, for
3835details.
3836
3830@item @code{services} (default: @var{%base-services}) 3837@item @code{services} (default: @var{%base-services})
3831A list of monadic values denoting system services. @xref{Services}. 3838A list of monadic values denoting system services. @xref{Services}.
3832 3839
@@ -4648,6 +4655,115 @@ Under the hood, the actual setuid programs are created in the
4648files in this directory refer to the ``real'' binaries, which are in the 4655files in this directory refer to the ``real'' binaries, which are in the
4649store. 4656store.
4650 4657
4658@node Name Service Switch
4659@subsection Name Service Switch
4660
4661@cindex name service switch
4662@cindex NSS
4663The @code{(gnu system nss)} module provides bindings to the
4664configuration file of libc's @dfn{name service switch} or @dfn{NSS}
4665(@pxref{NSS Configuration File,,, libc, The GNU C Library Reference
4666Manual}). In a nutshell, the NSS is a mechanism that allows libc to be
4667extended with new ``name'' lookup methods for system databases, which
4668includes host names, service names, user accounts, and more (@pxref{Name
4669Service Switch, System Databases and Name Service Switch,, libc, The GNU
4670C Library Reference Manual}).
4671
4672The NSS configuration specifies, for each system database, which lookup
4673method is to be used, and how the various methods are chained
4674together---for instance, under which circumstances NSS should try the
4675next method in the list. The NSS configuration is given in the
4676@code{name-service-switch} field of @code{operating-system} declarations
4677(@pxref{operating-system Reference, @code{name-service-switch}}).
4678
4679@c See <http://0pointer.de/lennart/projects/nss-mdns/>.
4680As an example, the declaration below configures the NSS to use the
4681@code{nss-mdns} back-end for host name lookups:
4682
4683@example
4684(name-service-switch
4685 (hosts (list %files ;first, check /etc/hosts
4686
4687 ;; If the above did not succeed, try
4688 ;; with 'mdns_minimal'.
4689 (name-service
4690 (name "mdns_minimal")
4691
4692 ;; 'mdns_minimal' is authoritative for
4693 ;; '.local'. When it returns "not found",
4694 ;; no need to try the next methods.
4695 (reaction (lookup-specification
4696 (not-found => return))))
4697
4698 ;; Then fall back to DNS.
4699 (name-service
4700 (name "dns"))
4701
4702 ;; Finally, try with the "full" 'mdns'.
4703 (name-service
4704 (name "mdns")))))
4705@end example
4706
4707The reference for name service switch configuration is given below. It
4708is a direct mapping of the C library's configuration file format, so
4709please refer to the C library manual for more information (@pxref{NSS
4710Configuration File,,, libc, The GNU C Library Reference Manual}).
4711Compared to libc's NSS configuration file format, it has the advantage
4712not only of adding this warm parenthetic feel that we like, but also
4713static checks: you'll know about syntax errors and typos as soon as you
4714run @command{guix system}.
4715
4716@defvr {Scheme Variable} %default-nss
4717This is the default name service switch configuration, a
4718@code{name-service-switch} object.
4719@end defvr
4720
4721@deftp {Data Type} name-service-switch
4722
4723This is the data type representation the configuration of libc's name
4724service switch (NSS). Each field below represents one of the supported
4725system databases.
4726
4727@table @code
4728@item aliases
4729@itemx ethers
4730@itemx group
4731@itemx gshadow
4732@itemx hosts
4733@itemx initgroups
4734@itemx netgroup
4735@itemx networks
4736@itemx password
4737@itemx public-key
4738@itemx rpc
4739@itemx services
4740@itemx shadow
4741The system databases handled by the NSS. Each of these fields must be a
4742list of @code{<name-service>} objects (see below.)
4743@end table
4744@end deftp
4745
4746@deftp {Data Type} name-service
4747
4748This is the data type representing an actual name service and the
4749associated lookup action.
4750
4751@table @code
4752@item name
4753A string denoting the name service (@pxref{Services in the NSS
4754configuration,,, libc, The GNU C Library Reference Manual}).
4755
4756@item reaction
4757An action specified using the @code{lookup-specification} macro
4758(@pxref{Actions in the NSS configuration,,, libc, The GNU C Library
4759Reference Manual}). For example:
4760
4761@example
4762(lookup-specification (unavailable => continue)
4763 (success => return))
4764@end example
4765@end table
4766@end deftp
4651 4767
4652@node Initial RAM Disk 4768@node Initial RAM Disk
4653@subsection Initial RAM Disk 4769@subsection Initial RAM Disk
diff --git a/gnu-system.am b/gnu-system.am
index f9abc97fb12..ae4799379e2 100644
--- a/gnu-system.am
+++ b/gnu-system.am
@@ -316,6 +316,7 @@ GNU_SYSTEM_MODULES = \
316 gnu/system/linux.scm \ 316 gnu/system/linux.scm \
317 gnu/system/linux-initrd.scm \ 317 gnu/system/linux-initrd.scm \
318 gnu/system/locale.scm \ 318 gnu/system/locale.scm \
319 gnu/system/nss.scm \
319 gnu/system/shadow.scm \ 320 gnu/system/shadow.scm \
320 gnu/system/vm.scm \ 321 gnu/system/vm.scm \
321 \ 322 \
diff --git a/gnu.scm b/gnu.scm
index d9b9117b8ad..e3147b39e38 100644
--- a/gnu.scm
+++ b/gnu.scm
@@ -1,5 +1,5 @@
1;;; GNU Guix --- Functional package management for GNU 1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014 Ludovic Courtès <ludo@gnu.org> 2;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
3;;; Copyright © 2015 Joshua S. Grant <jgrant@parenthetical.io> 3;;; Copyright © 2015 Joshua S. Grant <jgrant@parenthetical.io>
4;;; 4;;;
5;;; This file is part of GNU Guix. 5;;; This file is part of GNU Guix.
@@ -37,6 +37,7 @@
37 (gnu system linux) ; 'base-pam-services' 37 (gnu system linux) ; 'base-pam-services'
38 (gnu system shadow) ; 'user-account' 38 (gnu system shadow) ; 'user-account'
39 (gnu system linux-initrd) 39 (gnu system linux-initrd)
40 (gnu system nss)
40 (gnu services) 41 (gnu services)
41 (gnu services base) 42 (gnu services base)
42 (gnu packages) 43 (gnu packages)
diff --git a/gnu/system.scm b/gnu/system.scm
index b3c5cd80385..3fe78339b70 100644
--- a/gnu/system.scm
+++ b/gnu/system.scm
@@ -47,6 +47,7 @@
47 #:use-module (gnu services base) 47 #:use-module (gnu services base)
48 #:use-module (gnu system grub) 48 #:use-module (gnu system grub)
49 #:use-module (gnu system shadow) 49 #:use-module (gnu system shadow)
50 #:use-module (gnu system nss)
50 #:use-module (gnu system locale) 51 #:use-module (gnu system locale)
51 #:use-module (gnu system linux) 52 #:use-module (gnu system linux)
52 #:use-module (gnu system linux-initrd) 53 #:use-module (gnu system linux-initrd)
@@ -137,6 +138,8 @@
137 (default "en_US.utf8")) 138 (default "en_US.utf8"))
138 (locale-definitions operating-system-locale-definitions ; list of <locale-definition> 139 (locale-definitions operating-system-locale-definitions ; list of <locale-definition>
139 (default %default-locale-definitions)) 140 (default %default-locale-definitions))
141 (name-service-switch operating-system-name-service-switch ; <name-service-switch>
142 (default %default-nss))
140 143
141 (services operating-system-user-services ; list of monadic services 144 (services operating-system-user-services ; list of monadic services
142 (default %base-services)) 145 (default %base-services))
@@ -408,7 +411,7 @@ settings for 'guix.el' to work out-of-the-box."
408 (skeletons '()) 411 (skeletons '())
409 (pam-services '()) 412 (pam-services '())
410 (profile "/run/current-system/profile") 413 (profile "/run/current-system/profile")
411 hosts-file 414 hosts-file nss
412 (sudoers "")) 415 (sudoers ""))
413 "Return a derivation that builds the static part of the /etc directory." 416 "Return a derivation that builds the static part of the /etc directory."
414 (mlet* %store-monad 417 (mlet* %store-monad
@@ -422,10 +425,8 @@ settings for 'guix.el' to work out-of-the-box."
422/run/current-system/profile/bin/bash\n")) 425/run/current-system/profile/bin/bash\n"))
423 (emacs (emacs-site-directory)) 426 (emacs (emacs-site-directory))
424 (issue (text-file "issue" issue)) 427 (issue (text-file "issue" issue))
425
426 ;; For now, generate a basic config so that /etc/hosts is honored.
427 (nsswitch (text-file "nsswitch.conf" 428 (nsswitch (text-file "nsswitch.conf"
428 "hosts: files dns\n")) 429 (name-service-switch->string nss)))
429 430
430 ;; Startup file for POSIX-compliant login shells, which set system-wide 431 ;; Startup file for POSIX-compliant login shells, which set system-wide
431 ;; environment variables. 432 ;; environment variables.
@@ -518,6 +519,7 @@ export ASPELL_CONF=\"dict-dir $HOME/.guix-profile/lib/aspell\"
518 #:skeletons skeletons 519 #:skeletons skeletons
519 #:issue (operating-system-issue os) 520 #:issue (operating-system-issue os)
520 #:locale (operating-system-locale os) 521 #:locale (operating-system-locale os)
522 #:nss (operating-system-name-service-switch os)
521 #:timezone (operating-system-timezone os) 523 #:timezone (operating-system-timezone os)
522 #:hosts-file /etc/hosts 524 #:hosts-file /etc/hosts
523 #:sudoers (operating-system-sudoers os) 525 #:sudoers (operating-system-sudoers os)
diff --git a/gnu/system/nss.scm b/gnu/system/nss.scm
new file mode 100644
index 00000000000..ec2d2517e78
--- /dev/null
+++ b/gnu/system/nss.scm
@@ -0,0 +1,213 @@
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu system nss)
20 #:use-module (rnrs enums)
21 #:use-module (guix records)
22 #:use-module (srfi srfi-9)
23 #:use-module (ice-9 match)
24 #:export (name-service-switch?
25 name-service-switch
26 name-service?
27 name-service
28
29 lookup-specification
30
31 %default-nss
32 %files
33 %compat
34 %dns
35
36 name-service-switch->string))
37
38;;; Commentary:
39;;;
40;;; Bindings for libc's name service switch (NSS) configuration.
41;;;
42;;; Code:
43
44(define-record-type* <name-service> name-service
45 make-name-service
46 name-service?
47 (name name-service-name)
48 (reaction name-service-reaction
49 (default (lookup-specification))))
50
51;; Lookup specification (info "(libc) Actions in the NSS Configuration").
52
53(define-enumeration lookup-action
54 (return continue)
55 make-lookup-action)
56
57(define-enumeration lookup-status
58 (success
59 not-found
60 unavailable
61 try-again)
62 make-lookup-status)
63
64(define-record-type <lookup-status-negation>
65 (lookup-status-negation status)
66 lookup-status-negation?
67 (status lookup-status-negation-status))
68
69(define-record-type <lookup-reaction>
70 (make-lookup-reaction status action)
71 lookup-reaction?
72 (status lookup-reaction-status)
73 (action lookup-reaction-action))
74
75(define-syntax lookup-reaction
76 (syntax-rules (not =>)
77 ((_ ((not status) => action))
78 (make-lookup-reaction (lookup-status-negation (lookup-status status))
79 (lookup-action action)))
80 ((_ (status => action))
81 (make-lookup-reaction (lookup-status status)
82 (lookup-action action)))))
83
84(define-syntax-rule (lookup-specification reaction ...)
85 "Return an NSS lookup specification."
86 (list (lookup-reaction reaction) ...))
87
88
89;;;
90;;; Common name services and default NSS configuration.
91;;;
92
93(define %compat
94 (name-service
95 (name "compat")
96 (reaction (lookup-specification (not-found => return)))))
97
98(define %files
99 (name-service (name "files")))
100
101(define %dns
102 ;; DNS is supposed to be authoritative, so unless it's unavailable, return
103 ;; what it finds.
104 (name-service
105 (name "dns")
106 (reaction (lookup-specification ((not unavailable) => return)))))
107
108;; The NSS. We list all the databases here because that allows us to
109;; statically ensure that the user's configuration refers to existing
110;; databases. See libc/nss/databases.def for the list of databases. Default
111;; values obtained by looking for "DEFAULT_CONFIG" in libc/nss/*.c.
112;;
113;; Although libc places 'dns' before 'files' in the default configurations of
114;; the 'hosts' and 'networks' databases, we choose to put 'files' before 'dns'
115;; by default, so that users can override host/address mappings in /etc/hosts
116;; and bypass DNS to improve their privacy and escape NSA's MORECOWBELL.
117(define-record-type* <name-service-switch> name-service-switch
118 make-name-service-switch
119 name-service-switch?
120 (aliases name-service-switch-aliases
121 (default '()))
122 (ethers name-service-switch-ethers
123 (default '()))
124 (group name-service-switch-group
125 (default (list %compat %files)))
126 (gshadow name-service-switch-gshadow
127 (default '()))
128 (hosts name-service-switch-hosts
129 (default (list %files %dns)))
130 (initgroups name-service-switch-initgroups
131 (default '()))
132 (netgroup name-service-switch-netgroup
133 (default '()))
134 (networks name-service-switch-networks
135 (default (list %files %dns)))
136 (password name-service-switch-password
137 (default (list %compat %files)))
138 (public-key name-service-switch-public-key
139 (default '()))
140 (rpc name-service-switch-rpc
141 (default '()))
142 (services name-service-switch-services
143 (default '()))
144 (shadow name-service-switch-shadow
145 (default (list %compat %files))))
146
147(define %default-nss
148 ;; Default NSS configuration.
149 (name-service-switch))
150
151
152;;;
153;;; Serialization.
154;;;
155
156(define (lookup-status->string status)
157 (match status
158 ('success "SUCCESS")
159 ('not-found "NOTFOUND")
160 ('unavailable "UNAVAIL")
161 ('try-again "TRYAGAIN")
162 (($ <lookup-status-negation> status)
163 (string-append "!" (lookup-status->string status)))))
164
165(define lookup-reaction->string
166 (match-lambda
167 (($ <lookup-reaction> status action)
168 (string-append (lookup-status->string status) "="
169 (symbol->string action)))))
170
171(define name-service->string
172 (match-lambda
173 (($ <name-service> name ())
174 name)
175 (($ <name-service> name reactions)
176 (string-append name " ["
177 (string-join (map lookup-reaction->string reactions))
178 "]"))))
179
180(define (name-service-switch->string nss)
181 "Return the 'nsswitch.conf' contents for NSS as a string. See \"NSS
182Configuration File\" in the libc manual."
183 (let-syntax ((->string
184 (syntax-rules ()
185 ((_ name field)
186 (match (field nss)
187 (() ;keep the default config
188 "")
189 ((services (... ...))
190 (string-append name ":\t"
191 (string-join
192 (map name-service->string services))
193 "\n")))))))
194 (string-append (->string "aliases" name-service-switch-aliases)
195 (->string "ethers" name-service-switch-ethers)
196 (->string "group" name-service-switch-group)
197 (->string "gshadow" name-service-switch-gshadow)
198 (->string "hosts" name-service-switch-hosts)
199 (->string "initgroups" name-service-switch-initgroups)
200 (->string "netgroup" name-service-switch-netgroup)
201 (->string "networks" name-service-switch-networks)
202 (->string "passwd" name-service-switch-password)
203 (->string "publickey" name-service-switch-public-key)
204 (->string "rpc" name-service-switch-rpc)
205 (->string "services" name-service-switch-services)
206 (->string "shadow" name-service-switch-shadow))))
207
208;;; Local Variables:
209;;; eval: (put 'name-service 'scheme-indent-function 0)
210;;; eval: (put 'name-service-switch 'scheme-indent-function 0)
211;;; End:
212
213;;; nss.scm ends here