network.subr revision 100282
1208561Sraj#!/bin/sh -x
2208561Sraj#
3208561Sraj# $FreeBSD: head/etc/network.subr 100282 2002-07-18 05:00:19Z dougb $
4208561Sraj#
5208561Sraj
6208561Sraj# PROVIDE: network1
7208561Sraj# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty
8208561Sraj# KEYWORD: FreeBSD
9208561Sraj
10208561Sraj. /etc/rc.subr
11208561Sraj
12208561Srajname="network1"
13208561Srajstart_cmd="network_start"
14208561Srajstop_cmd="network_stop"
15208561Sraj
16208561Srajconvert_host_conf()
17208561Sraj{
18208561Sraj    host_conf=$1; shift;
19208561Sraj    nsswitch_conf=$1; shift;
20208561Sraj    awk '                                                                   \
21208561Sraj        /^[:blank:]*#/       { next }                                       \
22208561Sraj        /(hosts|local|file)/ { nsswitch[c] = "files"; c++; next }           \
23208561Sraj        /(dns|bind)/         { nsswitch[c] = "dns";   c++; next }           \
24208561Sraj        /nis/                { nsswitch[c] = "nis";   c++; next }           \
25208561Sraj        { printf "Warning: unrecognized line [%s]", $0 > "/dev/stderr" }    \
26208561Sraj        END {                                                               \
27208561Sraj                printf "hosts: ";                                           \
28208561Sraj                for (i in nsswitch) printf "%s ", nsswitch[i];              \
29208561Sraj                printf "\n";                                                \
30208561Sraj        }' < $host_conf > $nsswitch_conf
31208561Sraj}
32208561Sraj
33208561Srajgenerate_host_conf()
34208561Sraj{
35208561Sraj    nsswitch_conf=$1; shift;
36208561Sraj    host_conf=$1; shift;
37208561Sraj
38208561Sraj    awk '
39208561SrajBEGIN {
40208561Sraj    xlat["files"] = "hosts";
41208561Sraj    xlat["dns"] = "bind";
42208561Sraj    xlat["nis"] = "nis";
43208561Sraj    cont = 0;
44208561Sraj}
45208561Srajsub(/^[\t ]*hosts:/, "") || cont {
46208561Sraj    if (!cont)
47208561Sraj        srcs = ""
48208561Sraj    sub(/#.*/, "")
49208561Sraj    gsub(/[][]/, " & ")
50208561Sraj    cont = sub(/\\$/, "")
51208561Sraj    srcs = srcs " " $0
52208561Sraj}
53208561SrajEND {
54208561Sraj    print "# Auto-generated from nsswitch.conf, do not edit"
55208561Sraj    ns = split(srcs, s)
56208561Sraj    for (n = 1; n <= ns; ++n) {
57208561Sraj        if (s[n] in xlat)
58208561Sraj            print xlat[s[n]]
59208561Sraj    }
60208561Sraj}
61208561Sraj' <$nsswitch_conf >$host_conf
62208561Sraj}
63208561Sraj
64208561Srajnetwork_gif_setup() {
65208561Sraj	case ${gif_interfaces} in
66208561Sraj	[Nn][Oo] | '')
67208561Sraj		;;
68208561Sraj	*)
69208561Sraj		for i in ${gif_interfaces}; do
70208561Sraj			eval peers=\$gifconfig_$i
71208561Sraj			case ${peers} in
72208561Sraj			'')
73208561Sraj				continue
74208561Sraj				;;
75208561Sraj			*)
76208561Sraj				ifconfig $i create >/dev/null 2>&1
77208561Sraj				ifconfig $i tunnel ${peers}
78208561Sraj				;;
79208561Sraj			esac
80208561Sraj		done
81208561Sraj		;;
82208561Sraj	esac
83208561Sraj}
84208561Sraj
85266105Sloosnetwork_start()
86208561Sraj{
87208561Sraj	# set hostname, turn on network
88208561Sraj	#
89208561Sraj	echo -n "Doing initial network setup:"
90208561Sraj
91208561Sraj	# Generate host.conf for compatibility
92208561Sraj	#
93208561Sraj	if [ -f "/etc/nsswitch.conf" ]; then
94208561Sraj		echo -n ' host.conf'
95208561Sraj		generate_host_conf /etc/nsswitch.conf /etc/host.conf
96208561Sraj	fi
97208561Sraj
98208561Sraj	# Convert host.conf to nsswitch.conf if necessary
99208561Sraj	#
100208561Sraj	if [ -f "/etc/host.conf" -a ! -f "/etc/nsswitch.conf" ]; then
101208561Sraj		echo ''
102		echo 'Warning: /etc/host.conf is no longer used'
103		echo '  /etc/nsswitch.conf will be created for you'
104		convert_host_conf /etc/host.conf /etc/nsswitch.conf
105	fi
106
107	# Set the host name if it is not already set
108	#
109	if [ -z "`hostname -s`" ]; then
110		hostname ${hostname}
111		echo -n ' hostname'
112	fi
113
114	# Set the domainname if we're using NIS
115	#
116	if checkyesno nisdomainname ; then
117		domainname ${nisdomainname}
118		echo -n ' domain'
119	fi
120
121	echo '.'
122
123	# Attempt to create cloned interfaces.
124	for ifn in ${cloned_interfaces}; do
125		ifconfig ${ifn} create
126	done
127
128	# gifconfig
129	network_gif_setup
130
131	# Set up all the network interfaces, calling startup scripts if needed
132	#
133	case ${network_interfaces} in
134	[Aa][Uu][Tt][Oo])
135		network_interfaces="`ifconfig -l`"
136		;;
137	*)
138		network_interfaces="${network_interfaces} ${cloned_interfaces}"
139		;;
140	esac
141
142	dhcp_interfaces=""
143	for ifn in ${network_interfaces}; do
144		if [ -r /etc/start_if.${ifn} ]; then
145			. /etc/start_if.${ifn}
146			eval showstat_$ifn=1
147		fi
148
149		# Do the primary ifconfig if specified
150		#
151		eval ifconfig_args=\$ifconfig_${ifn}
152
153		case ${ifconfig_args} in
154		'')
155			;;
156		[Dd][Hh][Cc][Pp])
157			# DHCP inits are done all in one go below
158			dhcp_interfaces="$dhcp_interfaces $ifn"
159			eval showstat_$ifn=1
160			;;
161		*)
162			ifconfig ${ifn} ${ifconfig_args}
163			eval showstat_$ifn=1
164			;;
165		esac
166	done
167
168	if [ ! -z "${dhcp_interfaces}" ]; then
169		${dhcp_program:-/sbin/dhclient} ${dhcp_flags} ${dhcp_interfaces}
170	fi
171
172	for ifn in ${network_interfaces}; do
173		# Check to see if aliases need to be added
174		#
175		alias=0
176		while : ; do
177			eval ifconfig_args=\$ifconfig_${ifn}_alias${alias}
178			if [ -n "${ifconfig_args}" ]; then
179				ifconfig ${ifn} ${ifconfig_args} alias
180				eval showstat_$ifn=1
181				alias=$((${alias} + 1))
182			else
183				break;
184			fi
185		done
186
187		# Do ipx address if specified
188		#
189		eval ifconfig_args=\$ifconfig_${ifn}_ipx
190		if [ -n "${ifconfig_args}" ]; then
191			ifconfig ${ifn} ${ifconfig_args}
192			eval showstat_$ifn=1
193		fi
194	done
195
196	# catch-all for interfaces that haven't been 'ifconfig'ed so far
197	for ifn in ${network_interfaces}; do
198		eval showstat=\$showstat_${ifn}
199		if [ ! -z ${showstat} ]; then
200			ifconfig ${ifn}
201		fi
202	done
203
204	echo '.'
205
206	# Resync ipfilter
207	/etc/rc.d/ipfilter resync
208}
209
210network_stop()
211{
212	echo -n "Stopping network:"
213
214	# flush routes
215	#
216	echo -n " flush routes"
217	route -n flush
218
219	echo '.'
220}
221
222load_rc_config $name
223run_rc_command "$1"
224