nsswitch revision 129650
1100280Sgordon#!/bin/sh
2100280Sgordon#
3118638Sfjoe# Copyright (c) 1993 - 2004 The FreeBSD Project. All rights reserved.
466830Sobrien#
566830Sobrien# Redistribution and use in source and binary forms, with or without
666830Sobrien# modification, are permitted provided that the following conditions
766830Sobrien# are met:
866830Sobrien# 1. Redistributions of source code must retain the above copyright
966830Sobrien#    notice, this list of conditions and the following disclaimer.
1066830Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1166830Sobrien#    notice, this list of conditions and the following disclaimer in the
1266830Sobrien#    documentation and/or other materials provided with the distribution.
1366830Sobrien#
1466830Sobrien# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1566830Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1666830Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1766830Sobrien# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
1866830Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1966830Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2066830Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2166830Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2266830Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2366830Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2466830Sobrien# SUCH DAMAGE.
2566830Sobrien#
2666830Sobrien# $FreeBSD: head/etc/rc.d/nsswitch 129650 2004-05-24 11:48:58Z des $
2751231Ssheldonh#
28100280Sgordon
29108191Sdillon# PROVIDE: nsswitch
30127663Sluigi# REQUIRE: root
31108191Sdillon# BEFORE:  network
32108191Sdillon# KEYWORD: FreeBSD
33127663Sluigi
34127663Sluigi. /etc/rc.subr
35127663Sluigi
36127663Sluiginame="nsswitch"
37108191Sdillonstart_cmd="nsswitch_start"
38127663Sluigistop_cmd=":"
39127663Sluigi
40127663Sluigiconvert_host_conf()
41108191Sdillon{
42127663Sluigi    host_conf=$1; shift;
43127663Sluigi    nsswitch_conf=$1; shift;
44127663Sluigi    awk '                                                                   \
45127663Sluigi        /^[:blank:]*#/       { next }                                       \
46149170Sbrooks        /(hosts|local|file)/ { nsswitch[c] = "files"; c++; next }           \
47127663Sluigi        /(dns|bind)/         { nsswitch[c] = "dns";   c++; next }           \
48127663Sluigi        /nis/                { nsswitch[c] = "nis";   c++; next }           \
49127663Sluigi        { printf "Warning: unrecognized line [%s]", $0 > "/dev/stderr" }    \
50127663Sluigi        END {                                                               \
51127663Sluigi                printf "hosts: ";                                           \
52127663Sluigi                for (i in nsswitch) printf "%s ", nsswitch[i];              \
53149170Sbrooks                printf "\n";                                                \
54130151Sschweikh        }' < $host_conf > $nsswitch_conf
55127663Sluigi}
56127663Sluigi
57149170Sbrooksgenerate_host_conf()
58149170Sbrooks{
59108191Sdillon    nsswitch_conf=$1; shift;
60127663Sluigi    host_conf=$1; shift;
61127663Sluigi
62126787Sphk    awk '
63127663SluigiBEGIN {
64108191Sdillon    xlat["files"] = "hosts";
65127663Sluigi    xlat["dns"] = "bind";
66127663Sluigi    xlat["nis"] = "nis";
67127663Sluigi    cont = 0;
68127663Sluigi}
69127663Sluigisub(/^[\t ]*hosts:/, "") || cont {
70127663Sluigi    if (!cont)
71108191Sdillon        srcs = ""
72127663Sluigi    sub(/#.*/, "")
73127663Sluigi    gsub(/[][]/, " & ")
74127663Sluigi    cont = sub(/\\$/, "")
75127663Sluigi    srcs = srcs " " $0
76127663Sluigi}
77127663SluigiEND {
78149556Sbmah    print "# Auto-generated from nsswitch.conf, do not edit"
79108191Sdillon    ns = split(srcs, s)
80127663Sluigi    for (n = 1; n <= ns; ++n) {
81127663Sluigi        if (s[n] in xlat)
82127663Sluigi            print xlat[s[n]]
83127663Sluigi    }
84127663Sluigi}
85127663Sluigi' <$nsswitch_conf >$host_conf
86127663Sluigi}
87127663Sluigi
88127663Sluiginsswitch_start()
89127663Sluigi{
90108191Sdillon	# Generate host.conf for compatibility
91127663Sluigi	#
92127663Sluigi	if [ -f "/etc/nsswitch.conf" ]; then
93127663Sluigi		echo 'Generating host.conf.'
94127663Sluigi		generate_host_conf /etc/nsswitch.conf /etc/host.conf
95108191Sdillon	fi
96127663Sluigi
97127663Sluigi	# Convert host.conf to nsswitch.conf if necessary
98127663Sluigi	#
99127663Sluigi	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
100127663Sluigi		echo ''
101127663Sluigi		echo 'Warning: /etc/host.conf is no longer used'
102127663Sluigi		echo '  /etc/nsswitch.conf will be created for you'
103127663Sluigi		convert_host_conf /etc/host.conf /etc/nsswitch.conf
104127663Sluigi	fi
105127663Sluigi}
106127663Sluigi
107127663Sluigiload_rc_config $name
108127663Sluigirun_rc_command "$1"
109127663Sluigi