nsswitch revision 156917
1#!/bin/sh
2#
3# Copyright (c) 1993 - 2004 The FreeBSD Project. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD: head/etc/rc.d/nsswitch 156917 2006-03-20 18:00:14Z flz $
27#
28
29# PROVIDE: nsswitch
30# REQUIRE: root
31# BEFORE:  NETWORK
32
33. /etc/rc.subr
34
35name="nsswitch"
36start_cmd="nsswitch_start"
37stop_cmd=":"
38
39convert_host_conf()
40{
41    host_conf=$1; shift;
42    nsswitch_conf=$1; shift;
43
44    while read line; do
45	line=${line##[ 	]}
46	case $line in
47        hosts|local|file)
48		_nsswitch="${_nsswitch}${_nsswitch+ }files"
49		;;
50	dns|bind)
51		_nsswitch="${_nsswitch}${_nsswitch+ }dns"
52		;;
53	nis)
54		_nsswitch="${_nsswitch}${_nsswitch+ }nis"
55		;;
56	'#'*)
57		;;
58	*)
59        	printf "Warning: unrecognized line [%s]", $line > "/dev/stderr"
60		;;
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, do not edit" > $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