nsswitch revision 157682
133965Sjdp#!/bin/sh
233965Sjdp#
333965Sjdp# Copyright (c) 1993 - 2004 The FreeBSD Project. All rights reserved.
433965Sjdp#
533965Sjdp# Redistribution and use in source and binary forms, with or without
633965Sjdp# modification, are permitted provided that the following conditions
733965Sjdp# are met:
833965Sjdp# 1. Redistributions of source code must retain the above copyright
933965Sjdp#    notice, this list of conditions and the following disclaimer.
1033965Sjdp# 2. Redistributions in binary form must reproduce the above copyright
1133965Sjdp#    notice, this list of conditions and the following disclaimer in the
1233965Sjdp#    documentation and/or other materials provided with the distribution.
1333965Sjdp#
1433965Sjdp# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1533965Sjdp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1633965Sjdp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1733965Sjdp# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
1833965Sjdp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1933965Sjdp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2033965Sjdp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2133965Sjdp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2233965Sjdp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2333965Sjdp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2433965Sjdp# SUCH DAMAGE.
2533965Sjdp#
2633965Sjdp# $FreeBSD: head/etc/rc.d/nsswitch 157682 2006-04-12 12:01:53Z ume $
2733965Sjdp#
2833965Sjdp
2933965Sjdp# PROVIDE: nsswitch
3033965Sjdp# REQUIRE: root
3133965Sjdp# BEFORE:  NETWORK
3233965Sjdp
3333965Sjdp. /etc/rc.subr
3433965Sjdp
3533965Sjdpname="nsswitch"
3633965Sjdpstart_cmd="nsswitch_start"
3733965Sjdpstop_cmd=":"
3833965Sjdp
3933965Sjdpconvert_host_conf()
4033965Sjdp{
4133965Sjdp    host_conf=$1; shift;
4233965Sjdp    nsswitch_conf=$1; shift;
4333965Sjdp
4433965Sjdp    while read line; do
4533965Sjdp	line=${line##[ 	]}
4633965Sjdp	case $line in
4733965Sjdp        hosts|local|file)
4833965Sjdp		_nsswitch="${_nsswitch}${_nsswitch+ }files"
4933965Sjdp		;;
5033965Sjdp	dns|bind)
5133965Sjdp		_nsswitch="${_nsswitch}${_nsswitch+ }dns"
5233965Sjdp		;;
5333965Sjdp	nis)
5433965Sjdp		_nsswitch="${_nsswitch}${_nsswitch+ }nis"
5533965Sjdp		;;
5633965Sjdp	'#'*)
5733965Sjdp		;;
5833965Sjdp	*)
5933965Sjdp        	printf "Warning: unrecognized line [%s]", $line > "/dev/stderr"
6033965Sjdp		;;
61		
62	esac
63    done < $host_conf
64
65    echo "hosts: $_nsswitch" > $nsswitch_conf
66}
67
68generate_nsswitch_conf()
69{
70    nsswitch_conf=$1; shift;
71
72    cat >$nsswitch_conf <<EOF
73group: compat
74group_compat: nis
75hosts: files dns
76networks: files
77passwd: compat
78passwd_compat: nis
79shells: files
80EOF
81}
82
83generate_host_conf()
84{
85    nsswitch_conf=$1; shift;
86    host_conf=$1; shift;
87
88    _cont=0
89    _sources=""
90    while read line; do
91	line=${line##[ 	]}
92	case $line in
93	hosts:*)
94		;;
95	*)
96		if [ $_cont -ne 1 ]; then
97			continue
98		fi
99		;;
100	esac
101	if [ "${line%\\}" = "${line}\\" ]; then
102		_cont=1
103	fi
104	line=${line#hosts:}
105	line=${line%\\}
106	line=${line%%#*}
107	_sources="${_sources}${_sources:+ }$line"
108    done < $nsswitch_conf
109
110    echo "# Auto-generated from nsswitch.conf" > $host_conf
111    for _s in ${_sources}; do
112	case $_s in
113	files)
114		echo "hosts" >> $host_conf
115		;;
116	dns)
117		echo "dns" >> $host_conf
118		;;
119	nis)
120		echo "nis" >> $host_conf
121		;;
122	*=*)
123		;;
124	*)
125		printf "Warning: unrecognized source [%s]", $_s > "/dev/stderr"
126		;;
127	esac
128    done
129}
130
131nsswitch_start()
132{
133	# Convert host.conf to nsswitch.conf if necessary
134	#
135	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
136		echo ''
137		echo 'Warning: /etc/host.conf is no longer used'
138		echo '  /etc/nsswitch.conf will be created for you'
139		convert_host_conf /etc/host.conf /etc/nsswitch.conf
140	fi
141
142	# Generate default nsswitch.conf if none exists
143	#
144	if [ ! -f "/etc/nsswitch.conf" ]; then
145		echo 'Generating nsswitch.conf.'
146		generate_nsswitch_conf /etc/nsswitch.conf
147	fi
148
149	# Generate host.conf for compatibility
150	#
151	if [ ! -f "/etc/host.conf" ]; then
152		echo 'Generating host.conf.'
153		generate_host_conf /etc/nsswitch.conf /etc/host.conf
154	fi
155
156}
157
158load_rc_config $name
159run_rc_command "$1"
160