summaryrefslogtreecommitdiff
blob: 83cb1d9d1470cde8584c90781b4df9e19c2a503b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh
# udhcp setup script

# Ideally this should be the defalt udhcpc script, but I doubt upstream
# will accept it.

PATH=/bin:/usr/bin:/sbin:/usr/sbin

update_dns()
{
	[[ -n "${PEER_DNS}" ]] && [[ "${PEER_DNS}" != "yes" ]] && return
	[[ -z "${domain}" ]] && [[ -z "${dns}" ]] && return

	conf="# Generated by udhcpc for ${interface}\n"
	[[ -n "${domain}" ]] && conf="${conf}search ${domain}\n"
	for i in ${dns} ; do
		conf="${conf}nameserver ${i}\n"
	done
	if [[ -x /sbin/resolvconf ]] ; then
		echo -e "${conf}" | resolvconf -a ${interface}
	else
		echo -e "${conf}" > /etc/resolv.conf
		chmod 644 /etc/resolv.conf
	fi
}

update_ntp() {
	[[ -n "${PEER_NTP}" ]] && [[ "${PEER_NTP}" != "yes" ]] && return
	[[ -z "${ntpsrv}" ]] && return
	
	conf="# Generated by udhcpc for interface ${interface}\n"
	conf="${conf}restrict default noquery notrust nomodify\n"
	conf="${conf}restrict 127.0.0.1\n"
	for i in ${ntpsrv} ; do
		conf="${conf}restrict ${i} nomodify notrap noquery\n"
		conf="${conf}server ${i}\n"
	done
	conf="${conf}driftfile /var/lib/ntp/ntp.drift\n"
	conf="${conf}logfile /var/log/ntp.log\n"
	echo -e "${conf}" > /etc/ntp.conf
	chmod 644 /etc/ntp.conf
}

update_hostname() {
	[[ -n "${PEER_HOSTNAME}" ]] && [[ "${PEER_HOSTNAME}" != "yes" ]] && return
	[[ -z "${hostname}" ]] && return

	myhost="$(hostname)"
	[[ -z ${myhost} ]] || [[ ${myhost} == "(none)" ]] && hostname "${hostname}"
}

update_interface()
{
	[[ -n "${broadcast}" ]] && broadcast="broadcast ${broadcast}"
	[[ -n "${subnet}" ]] && netmask="netmask ${subnet}"
	[[ -n "${mtu}" ]] && mtu="mtu ${mtu}"
	ifconfig "${interface}" ${ip} ${broadcast} ${netmask} ${mtu}
}

update_routes()
{
	while route del default dev "${interface}" 2>/dev/null ; do
		:
	done
        
	[[ -n "${PEER_ROUTERS}" ]] && [[ "${PEER_ROUTERS}" != "yes" ]] && return
	
	if [[ -n "${router}" ]] ; then
		metric=
		[[ -n "${IF_METRIC}" ]] && metric="metric ${IF_METRIC}"
		for i in ${router} ; do
			route add default gw "${i}" ${metric} dev "${interface}"
		done
	fi
}

deconfig()
{
	ifconfig "${interface}" 0.0.0.0
	[[ -x /sbin/resolvconf ]] && resolvconf -d "${interface}"
}

case "$1" in
	bound|renew)
		update_hostname
		update_interface
		update_routes
		update_dns
		update_ntp
		;;
	deconfig|leasefail)
		deconfig
		;;
	nak)
		echo "nak: ${message}"
		;;
	*)
		echo "unknown option $1" >&2
		echo "Usage: $0 {bound|deconfig|leasefail|nak|renew}" >&2
		exit 1
		;;
esac

exit 0

# vim: ts=4 :