nsswitch revision 156917
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 156917 2006-03-20 18:00:14Z flz $
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		;;
61156917Sflz		
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
80129651SdesEOF
81129651Sdes}
82129651Sdes
83129650Sdesgenerate_host_conf()
84129650Sdes{
85129650Sdes    nsswitch_conf=$1; shift;
86129650Sdes    host_conf=$1; shift;
87129650Sdes
88156917Sflz    _cont=0
89156917Sflz    _sources=""
90156917Sflz    while read line; do
91156917Sflz	line=${line##[ 	]}
92156917Sflz	case $line in
93156917Sflz	hosts:*)
94156917Sflz		;;
95156917Sflz	*)
96156917Sflz		if [ $_cont -ne 1 ]; then
97156917Sflz			continue
98156917Sflz		fi
99156917Sflz		;;
100156917Sflz	esac
101156917Sflz	if [ "${line%\\}" = "${line}\\" ]; then
102156917Sflz		_cont=1
103156917Sflz	fi
104156917Sflz	line=${line#hosts:}
105156917Sflz	line=${line%\\}
106156917Sflz	line=${line%%#*}
107156917Sflz	_sources="${_sources}${_sources:+ }$line"
108156917Sflz    done < $nsswitch_conf
109156917Sflz
110156917Sflz    echo "# Auto-generated from nsswitch.conf, do not edit" > $host_conf
111156917Sflz    for _s in ${_sources}; do
112156917Sflz	case $_s in
113156917Sflz	files)
114156917Sflz		echo "hosts" >> $host_conf
115156917Sflz		;;
116156917Sflz	dns)
117156917Sflz		echo "dns" >> $host_conf
118156917Sflz		;;
119156917Sflz	nis)
120156917Sflz		echo "nis" >> $host_conf
121156917Sflz		;;
122156917Sflz	*=*)
123156917Sflz		;;
124156917Sflz	*)
125156917Sflz		printf "Warning: unrecognized source [%s]", $_s > "/dev/stderr"
126156917Sflz		;;
127156917Sflz	esac
128156917Sflz    done
129129650Sdes}
130129650Sdes
131129650Sdesnsswitch_start()
132129650Sdes{
133129650Sdes	# Convert host.conf to nsswitch.conf if necessary
134129650Sdes	#
135129650Sdes	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
136129650Sdes		echo ''
137129650Sdes		echo 'Warning: /etc/host.conf is no longer used'
138129650Sdes		echo '  /etc/nsswitch.conf will be created for you'
139129650Sdes		convert_host_conf /etc/host.conf /etc/nsswitch.conf
140129650Sdes	fi
141129651Sdes
142129651Sdes	# Generate default nsswitch.conf if none exists
143129651Sdes	#
144129651Sdes	if [ ! -f "/etc/nsswitch.conf" ]; then
145129651Sdes		echo 'Generating nsswitch.conf.'
146129651Sdes		generate_nsswitch_conf /etc/nsswitch.conf
147129651Sdes	fi
148129651Sdes
149129651Sdes	# Generate host.conf for compatibility
150129651Sdes	#
151129651Sdes	if [ ! -f "/etc/host.conf" ]; then
152129651Sdes		echo 'Generating host.conf.'
153129651Sdes		generate_host_conf /etc/nsswitch.conf /etc/host.conf
154129651Sdes	fi
155129651Sdes
156129650Sdes}
157129650Sdes
158129650Sdesload_rc_config $name
159129650Sdesrun_rc_command "$1"
160