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