-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
118640b
commit 43a2580
Showing
4 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/bin/sh -e | ||
# | ||
# 2023 Dennis Camera (dennis.camera at riiengineering.ch) | ||
# | ||
# This file is part of skonfig-base. | ||
# | ||
# skonfig-base is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# skonfig-base is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with skonfig-base. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
# Prints the sources currently configured for the database. | ||
# | ||
|
||
# NOTE: don’t check test -f, because /etc/nsswitch.conf can be a symbolic link | ||
# (hello, Fedora) | ||
test -e /etc/nsswitch.conf || exit 0 | ||
|
||
awk -v db_name="${__object_id:?}" ' | ||
{ | ||
sub(/[ \\t]*#.*$/, "") | ||
while (/\\$/) { | ||
sub(/\\$/, (0 < (getline _nl) ? _nl : "")) | ||
} | ||
} | ||
$1 == db_name ":" { | ||
# store found line, because in nsswitch.conf(5) the last match wins | ||
line = $0 | ||
} | ||
END { | ||
if (line) { | ||
$0 = line | ||
for (i = 2; i <= NF; ++i) { | ||
printf "%s%s", (i>2 ? " " : ""), $i | ||
} | ||
printf ORS | ||
} | ||
} | ||
' /etc/nsswitch.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#!/bin/sh -e | ||
# | ||
# 2023 Dennis Camera (dennis.camera at riiengineering.ch) | ||
# | ||
# This file is part of skonfig-base. | ||
# | ||
# skonfig-base is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# skonfig-base is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with skonfig-base. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
join_lines(){ | ||
awk -v sep="$1" 'NR>1{printf "%s",sep}{printf "%s",$0}END{printf ORS}' | ||
} | ||
|
||
if | ||
join_lines ' ' <"${__object:?}/parameter/source" \ | ||
| cmp -s "${__object:?}/explorer/sources" - | ||
then | ||
# no changes needed | ||
exit 0 | ||
fi | ||
|
||
|
||
if ! test -s "${__object:?}/explorer/sources" | ||
then | ||
# possibly the file does not exist | ||
cat <<'EOF' | ||
test -f /etc/nsswitch.conf || ( | ||
umask 0022 | ||
:>/etc/nsswitch.conf | ||
chown 0:0 /etc/nsswitch.conf | ||
) | ||
EOF | ||
fi | ||
|
||
# update nsswitch.conf(5) | ||
|
||
# NOTE: the updated content is cat(1) into /etc/nsswitch.conf to achieve the | ||
# correct behaviour if /etc/nsswitch.conf is a symbolic link. | ||
cat <<'EOF' | ||
awk -v db_name="${__object_id:?}" -v source_file="${__object:?}/parameter/source" ' | ||
function get_indent( i) { | ||
# FIXME: handle tab stops? | ||
i = index($0, $1) - 1 | ||
i += length($1) | ||
i += index(substr($0, i+1), $2) - 1 | ||
return i | ||
} | ||
function guess_indent(key, cnt, il, i) { | ||
# set initial cnt = 2 to ignore indentations which only occur once | ||
il = (length(key) + 2); cnt = 2 | ||
for (i in indent) { | ||
if (indent[i] > cnt) { il = i } | ||
} | ||
return il | ||
} | ||
BEGIN { | ||
while (0 < (getline src < source_file)) { | ||
sources = sources (sources ? " " : "") src | ||
} | ||
close(source_file) | ||
} | ||
$1 ~ /^[^#].*:/ { | ||
# looks like an entry | ||
db = substr($1, 1, index($1, ":") - 1) | ||
i = get_indent() | ||
if (i > (index($0, $1) + length($1))) { | ||
# only remember if there is more than one space between db and sources | ||
indent[i]++ | ||
} | ||
} | ||
db == db_name { | ||
if (!f) { | ||
# only replace the first occurrence | ||
if (sources) { | ||
match($0, /:[ \t]*/) | ||
printf "%s%s" ORS, substr($0, 1, RSTART + RLENGTH - 1), sources | ||
} | ||
f = 1 | ||
} | ||
while (/\\$/) { getline } | ||
next | ||
} | ||
{ print } | ||
END { | ||
if (!f && sources) { | ||
# append new entry | ||
printf ("%-" guess_indent(db_name) "s%s" ORS), db_name ":", sources | ||
} | ||
} | ||
' /etc/nsswitch.conf >/etc/nsswitch.conf.tmp \ | ||
&& cat /etc/nsswitch.conf.tmp >/etc/nsswitch.conf | ||
rm -f /etc/nsswitch.conf.tmp | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
cdist-type__nsswitch(7) | ||
======================= | ||
|
||
NAME | ||
---- | ||
cdist-type__nsswitch - manage Name Service Switch database configuration | ||
|
||
|
||
DESCRIPTION | ||
----------- | ||
This type can be used to manage database configurations in the | ||
:strong:`nsswitch.conf`\ (5) file. | ||
|
||
This type does no checking whether your operating system/libc respects the | ||
nsswitch.conf file, this job is up to you. | ||
|
||
|
||
OPTIONAL PARAMETERS | ||
------------------- | ||
source | ||
Specify the name of a source for this database (with optional criteria | ||
appended to the source name in square brackets ``[]``). | ||
See :strong:`nsswitch.conf`\ (5) for the options supported by your operating | ||
system. | ||
|
||
Can be used multiple times so specify multiple sources which will be queried | ||
in the order specified. | ||
|
||
If no sources are given, the database entry will be removed from | ||
:strong:`nsswitch.conf`\ (5). | ||
|
||
|
||
EXAMPLES | ||
-------- | ||
|
||
.. code-block:: sh | ||
# Configure NSS to query LDAP for users/groups (Linux) | ||
__nsswitch passwd \ | ||
--source ldap \ | ||
--source files | ||
__nsswitch group \ | ||
--source ldap \ | ||
--source files | ||
__nsswitch shadow \ | ||
--source ldap \ | ||
--source files | ||
# Configure NSS host lookup to include mDNS (using libnss-mdns on Linux) | ||
# Other OSs may use different source names, e.g. mdns_minimal is called | ||
# multicast_dns on NetBSD | ||
__nsswitch hosts \ | ||
--source files \ | ||
--source 'mdns_minimal [NOTFOUND=return]' \ | ||
--source dns | ||
SEE ALSO | ||
-------- | ||
:strong:`nsswitch.conf`\ (5) | ||
|
||
|
||
AUTHORS | ||
------- | ||
| Dennis Camera <[email protected]> | ||
|
||
COPYING | ||
------- | ||
Copyright \(C) 2023 Dennis Camera. | ||
You can redistribute it and/or modify it under the terms of the GNU General | ||
Public License as published by the Free Software Foundation, either version 3 of | ||
the License, or (at your option) any later version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
source |