nsswitch revision 129651
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 129651 2004-05-24 11:53:26Z 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
57129651Sdesgenerate_nsswitch_conf()
58129651Sdes{
59129651Sdes    nsswitch_conf=$1; shift;
60129651Sdes
61129651Sdes    cat >$nsswitch_conf <<EOF
62129651Sdesgroup: compat
63129651Sdesgroup_compat: nis
64129651Sdeshosts: dns files
65129651Sdesnetworks: files
66129651Sdespasswd: compat
67129651Sdespasswd_compat: nis
68129651Sdesshells: files
69129651SdesEOF
70129651Sdes}
71129651Sdes
72129650Sdesgenerate_host_conf()
73129650Sdes{
74129650Sdes    nsswitch_conf=$1; shift;
75129650Sdes    host_conf=$1; shift;
76129650Sdes
77129650Sdes    awk '
78129650SdesBEGIN {
79129650Sdes    xlat["files"] = "hosts";
80129650Sdes    xlat["dns"] = "bind";
81129650Sdes    xlat["nis"] = "nis";
82129650Sdes    cont = 0;
83129650Sdes}
84129650Sdessub(/^[\t ]*hosts:/, "") || cont {
85129650Sdes    if (!cont)
86129650Sdes        srcs = ""
87129650Sdes    sub(/#.*/, "")
88129650Sdes    gsub(/[][]/, " & ")
89129650Sdes    cont = sub(/\\$/, "")
90129650Sdes    srcs = srcs " " $0
91129650Sdes}
92129650SdesEND {
93129650Sdes    print "# Auto-generated from nsswitch.conf, do not edit"
94129650Sdes    ns = split(srcs, s)
95129650Sdes    for (n = 1; n <= ns; ++n) {
96129650Sdes        if (s[n] in xlat)
97129650Sdes            print xlat[s[n]]
98129650Sdes    }
99129650Sdes}
100129650Sdes' <$nsswitch_conf >$host_conf
101129650Sdes}
102129650Sdes
103129650Sdesnsswitch_start()
104129650Sdes{
105129650Sdes	# Convert host.conf to nsswitch.conf if necessary
106129650Sdes	#
107129650Sdes	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
108129650Sdes		echo ''
109129650Sdes		echo 'Warning: /etc/host.conf is no longer used'
110129650Sdes		echo '  /etc/nsswitch.conf will be created for you'
111129650Sdes		convert_host_conf /etc/host.conf /etc/nsswitch.conf
112129650Sdes	fi
113129651Sdes
114129651Sdes	# Generate default nsswitch.conf if none exists
115129651Sdes	#
116129651Sdes	if [ ! -f "/etc/nsswitch.conf" ]; then
117129651Sdes		echo 'Generating nsswitch.conf.'
118129651Sdes		generate_nsswitch_conf /etc/nsswitch.conf
119129651Sdes	fi
120129651Sdes
121129651Sdes	# Generate host.conf for compatibility
122129651Sdes	#
123129651Sdes	if [ ! -f "/etc/host.conf" ]; then
124129651Sdes		echo 'Generating host.conf.'
125129651Sdes		generate_host_conf /etc/nsswitch.conf /etc/host.conf
126129651Sdes	fi
127129651Sdes
128129650Sdes}
129129650Sdes
130129650Sdesload_rc_config $name
131129650Sdesrun_rc_command "$1"
132