1265055Smarcel#!/bin/sh
2284079Smarcel#
3265055Smarcel# Copyright (c) 1993 - 2004 The FreeBSD Project. All rights reserved.
4265055Smarcel#
5265055Smarcel# Redistribution and use in source and binary forms, with or without
6265055Smarcel# modification, are permitted provided that the following conditions
7265055Smarcel# are met:
8265055Smarcel# 1. Redistributions of source code must retain the above copyright
9265055Smarcel#    notice, this list of conditions and the following disclaimer.
10265055Smarcel# 2. Redistributions in binary form must reproduce the above copyright
11265055Smarcel#    notice, this list of conditions and the following disclaimer in the
12265055Smarcel#    documentation and/or other materials provided with the distribution.
13265055Smarcel#
14265055Smarcel# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
15265055Smarcel# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16265055Smarcel# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17265055Smarcel# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
18265055Smarcel# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19265055Smarcel# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20265055Smarcel# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21265055Smarcel# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22265055Smarcel# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23265055Smarcel# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24265055Smarcel# SUCH DAMAGE.
25265055Smarcel#
26265055Smarcel# $FreeBSD$
27265055Smarcel#
28265055Smarcel
29265055Smarcel# PROVIDE: nsswitch
30265055Smarcel# REQUIRE: root
31265055Smarcel# BEFORE:  NETWORK
32265055Smarcel
33265055Smarcel. /etc/rc.subr
34265055Smarcel
35265055Smarcelname="nsswitch"
36265055Smarcelstart_cmd="nsswitch_start"
37265055Smarcelstop_cmd=":"
38265055Smarcel
39265055Smarcelgenerate_host_conf()
40265055Smarcel{
41265055Smarcel    local _cont _sources
42265055Smarcel
43284079Smarcel    nsswitch_conf=$1; shift;
44284079Smarcel    host_conf=$1; shift;
45284079Smarcel
46284079Smarcel    _cont=0
47284079Smarcel    _sources=""
48284144Smarcel    while read line; do
49284144Smarcel	line=${line##[ 	]}
50285069Smarcel	case $line in
51285069Smarcel	hosts:*)
52285069Smarcel		;;
53285074Smarcel	*)
54285074Smarcel		if [ $_cont -ne 1 ]; then
55284079Smarcel			continue
56284079Smarcel		fi
57284079Smarcel		;;
58284079Smarcel	esac
59284079Smarcel	if [ "${line%\\}" = "${line}\\" ]; then
60284079Smarcel		_cont=1
61284079Smarcel	fi
62284079Smarcel	line=${line#hosts:}
63284079Smarcel	line=${line%\\}
64284079Smarcel	line=${line%%#*}
65284079Smarcel	_sources="${_sources}${_sources:+ }$line"
66284079Smarcel    done < $nsswitch_conf
67284144Smarcel
68284144Smarcel    echo "# Auto-generated from nsswitch.conf" > $host_conf
69284144Smarcel    for _s in ${_sources}; do
70285069Smarcel	case $_s in
71285069Smarcel	files)
72284246Smarcel		echo "hosts" >> $host_conf
73284246Smarcel		;;
74284246Smarcel	dns)
75284246Smarcel		echo "dns" >> $host_conf
76285069Smarcel		;;
77285074Smarcel	nis)
78285074Smarcel		echo "nis" >> $host_conf
79285074Smarcel		;;
80285074Smarcel	cache | *=*)
81285074Smarcel		;;
82284079Smarcel	*)
83284079Smarcel		echo "Warning: unrecognized source [$_s]" >&2
84284079Smarcel		;;
85284079Smarcel	esac
86284079Smarcel    done
87284079Smarcel}
88265055Smarcel
89nsswitch_start()
90{
91	# Generate host.conf for compatibility
92	#
93	if [ ! -f "/etc/host.conf" -o \
94		"/etc/host.conf" -ot "/etc/nsswitch.conf" ]
95	then
96		echo 'Generating host.conf.'
97		generate_host_conf /etc/nsswitch.conf /etc/host.conf
98	fi
99
100}
101
102load_rc_config $name
103run_rc_command "$1"
104