nsswitch revision 158211
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: head/etc/rc.d/nsswitch 158211 2006-05-01 11:02:48Z des $
27129650Sdes#
28129650Sdes
29129650Sdes# PROVIDE: nsswitch
30129650Sdes# REQUIRE: root
31135305Skeramida# BEFORE:  NETWORK
32129650Sdes
33129650Sdes. /etc/rc.subr
34129650Sdes
35129650Sdesname="nsswitch"
36129650Sdesstart_cmd="nsswitch_start"
37129650Sdesstop_cmd=":"
38129650Sdes
39129650Sdesconvert_host_conf()
40129650Sdes{
41129650Sdes    host_conf=$1; shift;
42129650Sdes    nsswitch_conf=$1; shift;
43156917Sflz
44156917Sflz    while read line; do
45156917Sflz	line=${line##[ 	]}
46156917Sflz	case $line in
47156917Sflz        hosts|local|file)
48156917Sflz		_nsswitch="${_nsswitch}${_nsswitch+ }files"
49156917Sflz		;;
50156917Sflz	dns|bind)
51156917Sflz		_nsswitch="${_nsswitch}${_nsswitch+ }dns"
52156917Sflz		;;
53156917Sflz	nis)
54156917Sflz		_nsswitch="${_nsswitch}${_nsswitch+ }nis"
55156917Sflz		;;
56156917Sflz	'#'*)
57156917Sflz		;;
58156917Sflz	*)
59156917Sflz        	printf "Warning: unrecognized line [%s]", $line > "/dev/stderr"
60156917Sflz		;;
61158139Sume
62156917Sflz	esac
63156917Sflz    done < $host_conf
64156917Sflz
65156917Sflz    echo "hosts: $_nsswitch" > $nsswitch_conf
66129650Sdes}
67129650Sdes
68129651Sdesgenerate_nsswitch_conf()
69129651Sdes{
70129651Sdes    nsswitch_conf=$1; shift;
71129651Sdes
72129651Sdes    cat >$nsswitch_conf <<EOF
73129651Sdesgroup: compat
74129651Sdesgroup_compat: nis
75130870Sdeshosts: files dns
76129651Sdesnetworks: files
77129651Sdespasswd: compat
78129651Sdespasswd_compat: nis
79129651Sdesshells: files
80158141Sumeservices: compat
81158139Sumeservices_compat: nis
82158139Sumeprotocols: files
83158139Sumerpc: files
84129651SdesEOF
85129651Sdes}
86129651Sdes
87129650Sdesgenerate_host_conf()
88129650Sdes{
89129650Sdes    nsswitch_conf=$1; shift;
90129650Sdes    host_conf=$1; shift;
91129650Sdes
92156917Sflz    _cont=0
93156917Sflz    _sources=""
94156917Sflz    while read line; do
95156917Sflz	line=${line##[ 	]}
96156917Sflz	case $line in
97156917Sflz	hosts:*)
98156917Sflz		;;
99156917Sflz	*)
100156917Sflz		if [ $_cont -ne 1 ]; then
101156917Sflz			continue
102156917Sflz		fi
103156917Sflz		;;
104156917Sflz	esac
105156917Sflz	if [ "${line%\\}" = "${line}\\" ]; then
106156917Sflz		_cont=1
107156917Sflz	fi
108156917Sflz	line=${line#hosts:}
109156917Sflz	line=${line%\\}
110156917Sflz	line=${line%%#*}
111156917Sflz	_sources="${_sources}${_sources:+ }$line"
112156917Sflz    done < $nsswitch_conf
113156917Sflz
114157682Sume    echo "# Auto-generated from nsswitch.conf" > $host_conf
115156917Sflz    for _s in ${_sources}; do
116156917Sflz	case $_s in
117156917Sflz	files)
118156917Sflz		echo "hosts" >> $host_conf
119156917Sflz		;;
120156917Sflz	dns)
121156917Sflz		echo "dns" >> $host_conf
122156917Sflz		;;
123156917Sflz	nis)
124156917Sflz		echo "nis" >> $host_conf
125156917Sflz		;;
126156917Sflz	*=*)
127156917Sflz		;;
128156917Sflz	*)
129156917Sflz		printf "Warning: unrecognized source [%s]", $_s > "/dev/stderr"
130156917Sflz		;;
131156917Sflz	esac
132156917Sflz    done
133129650Sdes}
134129650Sdes
135129650Sdesnsswitch_start()
136129650Sdes{
137129650Sdes	# Convert host.conf to nsswitch.conf if necessary
138129650Sdes	#
139129650Sdes	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
140129650Sdes		echo ''
141129650Sdes		echo 'Warning: /etc/host.conf is no longer used'
142129650Sdes		echo '  /etc/nsswitch.conf will be created for you'
143129650Sdes		convert_host_conf /etc/host.conf /etc/nsswitch.conf
144129650Sdes	fi
145129651Sdes
146129651Sdes	# Generate default nsswitch.conf if none exists
147129651Sdes	#
148129651Sdes	if [ ! -f "/etc/nsswitch.conf" ]; then
149129651Sdes		echo 'Generating nsswitch.conf.'
150129651Sdes		generate_nsswitch_conf /etc/nsswitch.conf
151129651Sdes	fi
152129651Sdes
153129651Sdes	# Generate host.conf for compatibility
154129651Sdes	#
155158211Sdes	if [ ! -f "/etc/host.conf" -o \
156158211Sdes		"/etc/host.conf" -ot "/etc/nsswitch.conf" ]
157158211Sdes	then
158129651Sdes		echo 'Generating host.conf.'
159129651Sdes		generate_host_conf /etc/nsswitch.conf /etc/host.conf
160129651Sdes	fi
161129651Sdes
162129650Sdes}
163129650Sdes
164129650Sdesload_rc_config $name
165129650Sdesrun_rc_command "$1"
166