1# $NetBSD: 50-ypbind,v 1.7 2015/03/26 10:26:37 roy Exp $
2
3# Sample dhcpcd hook for ypbind
4# This script is only suitable for the BSD versions.
5
6: ${ypbind_restart_cmd:=service_command ypbind restart}
7: ${ypbind_stop_cmd:=service_condcommand ypbind stop}
8ypbind_dir="$state_dir/ypbind"
9: ${ypdomain_dir:=/var/yp}
10: ${ypdomain_suffix:=.ypservers}
11
12
13best_domain()
14{
15	local i=
16
17	for i in "$ypbind_dir/$interface_order".*; do
18		if [ -f "$i" ]; then
19			cat "$i"
20			return 0
21		fi
22	done
23	return 1
24}
25
26make_yp_binding()
27{
28	[ -d "$ypbind_dir" ] || mkdir -p "$ypbind_dir"
29	echo "$new_nis_domain" >"$ypbind_dir/$ifname"
30
31	if [ -z "$ypdomain_dir" ]; then
32		false
33	else
34		local cf="$ypdomain_dir/$new_nis_domain$ypdomain_suffix"
35		if [ -n "$new_nis_servers" ]; then
36			local ncf="$cf.$ifname" x=
37			rm -f "$ncf"
38			for x in $new_nis_servers; do
39				echo "$x" >>"$ncf"
40			done
41			change_file "$cf" "$ncf"
42		else
43			[ -e "$cf" ] && rm "$cf"
44		fi
45	fi
46
47	local nd="$(best_domain)"
48	if [ $? = 0 -a "$nd" != "$(domainname)" ]; then
49		domainname "$nd"
50		if [ -n "$ypbind_restart_cmd" ]; then
51			eval $ypbind_restart_cmd
52		fi
53	fi
54}
55
56restore_yp_binding()
57{
58
59	rm -f "$ypbind_dir/$ifname"
60	local nd="$(best_domain)"
61	# We need to stop ypbind if there is no best domain
62	# otherwise it will just stall as we cannot set domainname
63	# to blank :/
64	if [ -z "$nd" ]; then
65		if [ -n "$ypbind_stop_cmd" ]; then
66			eval $ypbind_stop_cmd
67		fi
68	elif [ "$nd" != "$(domainname)" ]; then
69		domainname "$nd"
70		if [ -n "$ypbind_restart_cmd" ]; then
71			eval $ypbind_restart_cmd
72		fi
73	fi
74}
75
76if [ "$reason" = PREINIT ]; then
77	rm -f "$ypbind_dir/$interface".*
78elif $if_up || $if_down; then
79	if [ -n "$new_nis_domain" ]; then
80		if valid_domainname "$new_nis_domain"; then
81			make_yp_binding
82		else
83			syslog err "Invalid NIS domain name: $new_nis_domain"
84		fi
85	elif [ -n "$old_nis_domain" ]; then
86		restore_yp_binding
87	fi
88fi
89