nsswitch revision 129650
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 129650 2004-05-24 11:48:58Z des $
27129650Sdes#
28129650Sdes
29129650Sdes# PROVIDE: nsswitch
30129650Sdes# REQUIRE: root
31129650Sdes# BEFORE:  network
32129650Sdes# KEYWORD: FreeBSD
33129650Sdes
34129650Sdes. /etc/rc.subr
35129650Sdes
36129650Sdesname="nsswitch"
37129650Sdesstart_cmd="nsswitch_start"
38129650Sdesstop_cmd=":"
39129650Sdes
40129650Sdesconvert_host_conf()
41129650Sdes{
42129650Sdes    host_conf=$1; shift;
43129650Sdes    nsswitch_conf=$1; shift;
44129650Sdes    awk '                                                                   \
45129650Sdes        /^[:blank:]*#/       { next }                                       \
46129650Sdes        /(hosts|local|file)/ { nsswitch[c] = "files"; c++; next }           \
47129650Sdes        /(dns|bind)/         { nsswitch[c] = "dns";   c++; next }           \
48129650Sdes        /nis/                { nsswitch[c] = "nis";   c++; next }           \
49129650Sdes        { printf "Warning: unrecognized line [%s]", $0 > "/dev/stderr" }    \
50129650Sdes        END {                                                               \
51129650Sdes                printf "hosts: ";                                           \
52129650Sdes                for (i in nsswitch) printf "%s ", nsswitch[i];              \
53129650Sdes                printf "\n";                                                \
54129650Sdes        }' < $host_conf > $nsswitch_conf
55129650Sdes}
56129650Sdes
57129650Sdesgenerate_host_conf()
58129650Sdes{
59129650Sdes    nsswitch_conf=$1; shift;
60129650Sdes    host_conf=$1; shift;
61129650Sdes
62129650Sdes    awk '
63129650SdesBEGIN {
64129650Sdes    xlat["files"] = "hosts";
65129650Sdes    xlat["dns"] = "bind";
66129650Sdes    xlat["nis"] = "nis";
67129650Sdes    cont = 0;
68129650Sdes}
69129650Sdessub(/^[\t ]*hosts:/, "") || cont {
70129650Sdes    if (!cont)
71129650Sdes        srcs = ""
72129650Sdes    sub(/#.*/, "")
73129650Sdes    gsub(/[][]/, " & ")
74129650Sdes    cont = sub(/\\$/, "")
75129650Sdes    srcs = srcs " " $0
76129650Sdes}
77129650SdesEND {
78129650Sdes    print "# Auto-generated from nsswitch.conf, do not edit"
79129650Sdes    ns = split(srcs, s)
80129650Sdes    for (n = 1; n <= ns; ++n) {
81129650Sdes        if (s[n] in xlat)
82129650Sdes            print xlat[s[n]]
83129650Sdes    }
84129650Sdes}
85129650Sdes' <$nsswitch_conf >$host_conf
86129650Sdes}
87129650Sdes
88129650Sdesnsswitch_start()
89129650Sdes{
90129650Sdes	# Generate host.conf for compatibility
91129650Sdes	#
92129650Sdes	if [ -f "/etc/nsswitch.conf" ]; then
93129650Sdes		echo 'Generating host.conf.'
94129650Sdes		generate_host_conf /etc/nsswitch.conf /etc/host.conf
95129650Sdes	fi
96129650Sdes
97129650Sdes	# Convert host.conf to nsswitch.conf if necessary
98129650Sdes	#
99129650Sdes	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
100129650Sdes		echo ''
101129650Sdes		echo 'Warning: /etc/host.conf is no longer used'
102129650Sdes		echo '  /etc/nsswitch.conf will be created for you'
103129650Sdes		convert_host_conf /etc/host.conf /etc/nsswitch.conf
104129650Sdes	fi
105129650Sdes}
106129650Sdes
107129650Sdesload_rc_config $name
108129650Sdesrun_rc_command "$1"
109