aboutsummaryrefslogtreecommitdiffstats
path: root/dhcpcd-run-hooks.in
blob: 2125f9a2857e4a27e5e04e91c27aff59fcdfd280 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/sh
# dhcpcd client configuration script 

# Handy variables and functions for our hooks to use
if [ "$reason" = ROUTERADVERT ]; then
	ifsuffix=":ra"
else
	ifsuffix=
fi
ifname="$interface$ifsuffix"

from=from
signature_base="# Generated by dhcpcd"
signature="$signature_base $from $ifname"
signature_base_end="# End of dhcpcd"
signature_end="$signature_base_end $from $ifname"
state_dir=/var/run/dhcpcd

# Ensure that all arguments are unique
uniqify()
{
	local result= i=
	for i do
		case " $result " in
			*" $i "*);;
			*) result="$result $i";;
		esac
	done
	echo "${result# *}"
}

# List interface config files in a directory.
# If dhcpcd is running as a single instance then it will have a list of
# interfaces in the preferred order.
# Otherwise we just use what we have.
list_interfaces()
{
	local i= x= ifaces=
	for i in $interface_order; do
		[ -e "$1/$i" ] && ifaces="$ifaces${ifaces:+ }$i"
	done
	for x in "$1"/*; do
		[ -e "$x" ] || continue
		for i in $interface_order; do
			if [ $i = "${x##*/}" ]; then
				unset x
				break
			fi
		done
		[ -n "$x" ] && ifaces="$ifaces${ifaces:+ }${x##*/}"
	done
	echo "$ifaces"
}

# We normally use sed to extract values using a key from a list of files
# but sed may not always be available at the time.
key_get_value()
{
	local key="$1" value= x= line=

	shift
	if type sed >/dev/null 2>&1; then
		sed -n "s/^$key//p" $@
	else
		for x do
			while read line; do
				case "$line" in
				"$key"*) echo "${line##$key}";;
				esac
			done < "$x"
		done
	fi
}

# We normally use sed to remove markers from a configuration file
# but sed may not always be available at the time.
remove_markers()
{
	local m1="$1" m2="$2" x= line= in_marker=0

	shift; shift
	if type sed >/dev/null 2>&1; then
		sed "/^$m1/,/^$m2/d" $@
	else
		for x do
			while read line; do
				case "$line" in
				"$m1"*) in_marker=1;;
				"$m2"*) in_marker=0;;
				*) [ $in_marker = 0 ] && echo "$line";;
				esac
			done < "$x"
		done
	fi
}

# Compare two files.
# If different, replace first with second otherwise remove second.
change_file()
{
	if [ -e "$1" ]; then
		if type cmp >/dev/null 2>&1; then
			cmp -s "$1" "$2"
		elif type diff >/dev/null 2>&1; then
			diff -q "$1" "$2" >/dev/null
		else
			# Hopefully we're only working on small text files ...
			[ "$(cat "$1")" = "$(cat "$2")" ]
		fi
		if [ $? -eq 0 ]; then
			rm -f "$2"
			return 1
		fi
	fi
	cat "$2" > "$1"
	rm -f "$2"
	return 0
}

# Save a config file
save_conf()
{
	if [ -f "$1" ]; then
		rm -f "$1-pre.$interface"
		cat "$1" > "$1-pre.$interface"
	fi
}

# Restore a config file
restore_conf()
{
	[ -f "$1-pre.$interface" ] || return 1
	cat "$1-pre.$interface" > "$1"
	rm -f "$1-pre.$interface"
}

# Write a syslog entry
syslog()
{
	local lvl="$1"

	[ -n "$lvl" ] && shift
	if [ -n "$*" ]; then
		if type logger >/dev/null 2>&1; then
			logger -t dhcpcd -p daemon."$lvl" -is "$interface: $*"
		fi
	fi
}

# Check for a valid domain name as per RFC1123 with the exception of
# allowing - and _ as they seem to be widely used.
valid_domainname()
{
	local name="$1" label

	[ -z "$name" -o ${#name} -gt 255 ] && return 1
	
	while [ -n "$name" ]; do
		label="${name%%.*}"
		[ -z "$label" -o ${#label} -gt 63 ] && return 1
		case "$label" in
		-*|_*|*-|*_)		return 1;;
		*[![:alnum:]-_]*)	return 1;;
		esac
		[ "$name" = "${name#*.}" ] && break
		name="${name#*.}"
	done
	return 0	
}

valid_domainname_list()
{
	local name

	for name do
		valid_domainname "$name" || return $?
	done
	return 0
}

# Check for a valid path
valid_path()
{
	case "$@" in
	*[![:alnum:]#%+-_:\.,@~\\/\[\]=\ ]*) return 1;;
	esac
	return 0
}

# Check a system service exists 
service_exists()
{
	@SERVICEEXISTS@
}

# Send a command to a system service
service_cmd()
{
	@SERVICECMD@
}

# Send a command to a system service if it is running
service_status()
{
	@SERVICESTATUS@
}

# Handy macros for our hooks
service_command()
{
	service_exists $1 && service_cmd $1 $2
}
service_condcommand()
{
	service_exists $1 && service_status $1 && service_cmd $1 $2
}

# We source each script into this one so that scripts run earlier can
# remove variables from the environment so later scripts don't see them.
# Thus, the user can create their dhcpcd.enter/exit-hook script to configure
# /etc/resolv.conf how they want and stop the system scripts ever updating it.
for hook in \
	@SYSCONFDIR@/dhcpcd.enter-hook \
	@HOOKDIR@/* \
	@SYSCONFDIR@/dhcpcd.exit-hook
do
	for skip in $skip_hooks; do
		case "$hook" in
			*/"$skip")			continue 2;;
			*/[0-9][0-9]"-$skip")		continue 2;;
			*/[0-9][0-9]"-$skip.sh")	continue 2;;
		esac
	done
	if [ -f "$hook" ]; then
		. "$hook"
	fi
done