3 Nsscash (a pun on cache) is a simple file-based cache for NSS similar to
4 nsscache [1]. The goal is to distribute users/groups/etc. to multiple systems
5 without having to rely on a (single) stable server. Traditional systems like
6 LDAP or NIS require a stable server or users/groups cannot be resolved. By
7 distributing the data to all systems, temporary outages of the server cause no
8 issues on the clients. In addition the local storage is much faster than
9 remote network access. To update the local caches polling via HTTP is
10 performed, for example every minute, only downloading new data if anything has
13 Nsscash consists of two parts: `nsscash`, written in Go, which downloads files
14 via HTTP or HTTPS, parses them, creates indices and writes the result to a
15 local file. The second part is the NSS module (`libnss_cash.so.2`), written in
16 C, which provides integration via `/etc/nsswitch.conf`. It's specifically
17 designed to be very simple and uses the data prepared by `nsscash` for
18 lookups. To support quick lookups, in O(log n), the files utilize indices.
20 Nsscash is very careful when deploying the changes:
21 - All files are updated using the standard "write to temporary file", "sync",
22 "rename" steps which is atomic on UNIX file systems. The indices are stored
23 in the same file preventing stale data during the update.
24 - All errors cause an immediate abort ("fail fast") with a proper error
25 message and a non-zero exit status. This prevents hiding possibly important
26 errors. In addition all files are fetched first and then deployed to try to
27 prevent inconsistent state if only one file can be downloaded. The state
28 file (containing last file modification and content hash) is only updated
29 when all operations were successful.
30 - To prevent unexpected permissions, `nsscash` does not create new files. The
31 user must create them first and `nsscash` will then re-use the permissions
32 (without the write bits to discourage manual modifications) and owner/group
33 when updating the file (see examples below).
34 - To prevent misconfigurations, empty files (no users/groups) are not
35 permitted and will not be written to disk. This is designed to prevent the
36 accidental loss of all users/groups on a system.
38 The passwd/group files have the following size restrictions:
39 - maximum number of entries: '2^64-1' (uint64_t)
40 - maximum passwd entry size: 65543 bytes (including newline)
41 - maximum group entry size: 65535 bytes (including newline, only one member)
42 - maximum members per group: depends on the user name length,
43 with 9 bytes per user: 5460 users
44 - `nsscash` checks for these restrictions and aborts with an error if they are
47 nsscash is licensed under AGPL version 3 or later.
49 [1] https://github.com/google/nsscache
55 - github.com/pkg/errors
56 - github.com/BurntSushi/toml
57 - C compiler, for `libnss_cash.so.2`
59 Tested on Debian Stretch and Buster, but should work on any GNU/Linux system.
60 With adaptations to the NSS module it should work on any UNIX-like system
66 Install `libnss_cash.so.2` somewhere in your library search path (see
67 `/etc/ld.so.conf`), e.g. `/usr/lib/x86_64-linux-gnu/`.
69 Update `/etc/nsswitch.conf` to include the cash module; `passwd` and `group`
70 are currently supported. For example:
76 Create the cache files with the proper permissions (`nsscash fetch` won't
77 create new files to prevent using incorrect permissions):
79 touch /etc/passwd.nsscash
80 touch /etc/group.nsscash
81 chmod 0644 /etc/passwd.nsscash
82 chmod 0644 /etc/group.nsscash
84 Configure the `nsscash` configuration file `nsscash.toml`, see below.
88 nsscash fetch /path/to/config/nsscash.toml
90 This will fetch the configured files and update the local caches. The files
91 are atomically overwritten (via temporary file, sync, and rename).
93 Verify the users/groups are available, e.g. with `getent`. If everything
94 works, remember to reboot the host as changes to `nsswitch.conf` don't affect
97 Now configure `nsscash` to run regularly, for example via cron or a systemd
100 To monitor `nsscash` for errors one can use the last modification time of the
101 state file (see below). It's written on each successful run and not modified
106 Nsscash is configured through a simple configuration file written in TOML. A
107 typical configuration looks like this:
109 statepath = "/var/lib/nsscash/state.json"
113 url = "https://example.org/passwd"
114 path = "/etc/passwd.nsscash"
118 url = "https://example.org/group"
119 path = "/etc/group.nsscash"
121 # Optional, but useful to deploy files which are not supported by the
122 # nsscash NSS module, but by libc's "files" NSS module. nsscash takes care
123 # of the atomic replacement and updates; an "netgroup: files" entry in
124 # "/etc/nsswitch.conf" makes the netgroups available.
127 url = "https://example.org/netgroup"
128 path = "/etc/netgroup"
130 The following global keys are available:
132 - `statepath`: Path to a JSON file which stores the last modification time and
133 hash of each file; automatically updated by `nsscash`. Used to fetch data
134 only when something has changed to reduce the required traffic, via
135 `If-Modified-Since`. When the hash of a file has changed the download is
138 Each `file` block describes a single file to download/write. The following
141 - `type`: Type of this file; can be either `passwd` (for files in
142 `/etc/passwd` format), `group` (for files in `/etc/group` format), or
143 `plain` (arbitrary format). Only `passwd` and `group` files are supported by
144 the nsscash NSS module. But, as explained above, `plain` can be used to
145 distribute arbitrary files. The type is required as the `.nsscash` files are
146 pre processed for faster lookups and simpler code which requires a known
149 - `url`: URL to fetch the file from; HTTP and HTTPS are supported
151 - `ca`: Path to a custom CA in PEM format. Restricts HTTPS requests to accept
152 only certificates signed by this CA. Defaults to the system's certificate
155 - `path`: Path to store the retrieved file
160 Written by Simon Ruderich <simon@ruderich.org>.
165 This program is licensed under AGPL version 3 or later.
167 Copyright (C) 2019 Simon Ruderich
169 This program is free software: you can redistribute it and/or modify
170 it under the terms of the GNU Affero General Public License as published by
171 the Free Software Foundation, either version 3 of the License, or
172 (at your option) any later version.
174 This program is distributed in the hope that it will be useful,
175 but WITHOUT ANY WARRANTY; without even the implied warranty of
176 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
177 GNU Affero General Public License for more details.
179 You should have received a copy of the GNU Affero General Public License
180 along with this program. If not, see <https://www.gnu.org/licenses/>.