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