1129650Sdes#!/bin/sh
2129650Sdes#
3129650Sdes# Copyright (c) 1993 - 2004 The FreeBSD Project. All rights reserved.
4129650Sdes#
5129650Sdes# Redistribution and use in source and binary forms, with or without
6129650Sdes# modification, are permitted provided that the following conditions
7129650Sdes# are met:
8129650Sdes# 1. Redistributions of source code must retain the above copyright
9129650Sdes#    notice, this list of conditions and the following disclaimer.
10129650Sdes# 2. Redistributions in binary form must reproduce the above copyright
11129650Sdes#    notice, this list of conditions and the following disclaimer in the
12129650Sdes#    documentation and/or other materials provided with the distribution.
13129650Sdes#
14129650Sdes# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
15129650Sdes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16129650Sdes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17129650Sdes# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
18129650Sdes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19129650Sdes# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20129650Sdes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21129650Sdes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22129650Sdes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23129650Sdes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24129650Sdes# SUCH DAMAGE.
25129650Sdes#
26129650Sdes# $FreeBSD: releng/11.0/etc/rc.d/nsswitch 298514 2016-04-23 16:10:54Z lme $
27129650Sdes#
28129650Sdes
29129650Sdes# PROVIDE: nsswitch
30129650Sdes# REQUIRE: root
31135305Skeramida# BEFORE:  NETWORK
32129650Sdes
33129650Sdes. /etc/rc.subr
34129650Sdes
35129650Sdesname="nsswitch"
36298514Slmedesc="Name-service switch"
37129650Sdesstart_cmd="nsswitch_start"
38129650Sdesstop_cmd=":"
39129650Sdes
40129650Sdesgenerate_host_conf()
41129650Sdes{
42165664Syar    local _cont _sources
43165664Syar
44129650Sdes    nsswitch_conf=$1; shift;
45129650Sdes    host_conf=$1; shift;
46129650Sdes
47156917Sflz    _cont=0
48156917Sflz    _sources=""
49156917Sflz    while read line; do
50156917Sflz	line=${line##[ 	]}
51156917Sflz	case $line in
52156917Sflz	hosts:*)
53156917Sflz		;;
54156917Sflz	*)
55156917Sflz		if [ $_cont -ne 1 ]; then
56156917Sflz			continue
57156917Sflz		fi
58156917Sflz		;;
59156917Sflz	esac
60156917Sflz	if [ "${line%\\}" = "${line}\\" ]; then
61156917Sflz		_cont=1
62156917Sflz	fi
63156917Sflz	line=${line#hosts:}
64156917Sflz	line=${line%\\}
65156917Sflz	line=${line%%#*}
66156917Sflz	_sources="${_sources}${_sources:+ }$line"
67156917Sflz    done < $nsswitch_conf
68156917Sflz
69157682Sume    echo "# Auto-generated from nsswitch.conf" > $host_conf
70156917Sflz    for _s in ${_sources}; do
71156917Sflz	case $_s in
72156917Sflz	files)
73156917Sflz		echo "hosts" >> $host_conf
74156917Sflz		;;
75156917Sflz	dns)
76156917Sflz		echo "dns" >> $host_conf
77156917Sflz		;;
78156917Sflz	nis)
79156917Sflz		echo "nis" >> $host_conf
80156917Sflz		;;
81201440Sgavin	cache | *=*)
82156917Sflz		;;
83156917Sflz	*)
84213202Simp		echo "Warning: unrecognized source [$_s]" >&2
85156917Sflz		;;
86156917Sflz	esac
87156917Sflz    done
88129650Sdes}
89129650Sdes
90129650Sdesnsswitch_start()
91129650Sdes{
92129651Sdes	# Generate host.conf for compatibility
93129651Sdes	#
94158211Sdes	if [ ! -f "/etc/host.conf" -o \
95158211Sdes		"/etc/host.conf" -ot "/etc/nsswitch.conf" ]
96158211Sdes	then
97129651Sdes		echo 'Generating host.conf.'
98129651Sdes		generate_host_conf /etc/nsswitch.conf /etc/host.conf
99129651Sdes	fi
100129651Sdes
101129650Sdes}
102129650Sdes
103129650Sdesload_rc_config $name
104129650Sdesrun_rc_command "$1"
105