blob: 2125f9a2857e4a27e5e04e91c27aff59fcdfd280 [file] [log] [blame]
The Android Open Source Projectf7c54212009-03-03 19:29:22 -08001#!/bin/sh
2# dhcpcd client configuration script
3
4# Handy variables and functions for our hooks to use
Dmitry Shmidta3a22602012-07-23 16:45:46 -07005if [ "$reason" = ROUTERADVERT ]; then
6 ifsuffix=":ra"
7else
8 ifsuffix=
9fi
10ifname="$interface$ifsuffix"
11
Dmitry Shmidte86eee12011-01-24 16:27:51 -080012from=from
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080013signature_base="# Generated by dhcpcd"
Dmitry Shmidta3a22602012-07-23 16:45:46 -070014signature="$signature_base $from $ifname"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080015signature_base_end="# End of dhcpcd"
Dmitry Shmidta3a22602012-07-23 16:45:46 -070016signature_end="$signature_base_end $from $ifname"
Dmitry Shmidte86eee12011-01-24 16:27:51 -080017state_dir=/var/run/dhcpcd
18
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080019# Ensure that all arguments are unique
20uniqify()
21{
Dmitry Shmidte86eee12011-01-24 16:27:51 -080022 local result= i=
Dmitry Shmidta3a22602012-07-23 16:45:46 -070023 for i do
Dmitry Shmidte86eee12011-01-24 16:27:51 -080024 case " $result " in
25 *" $i "*);;
26 *) result="$result $i";;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080027 esac
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080028 done
Dmitry Shmidte86eee12011-01-24 16:27:51 -080029 echo "${result# *}"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080030}
31
Dmitry Shmidte86eee12011-01-24 16:27:51 -080032# List interface config files in a directory.
33# If dhcpcd is running as a single instance then it will have a list of
34# interfaces in the preferred order.
35# Otherwise we just use what we have.
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080036list_interfaces()
37{
Dmitry Shmidte86eee12011-01-24 16:27:51 -080038 local i= x= ifaces=
39 for i in $interface_order; do
40 [ -e "$1/$i" ] && ifaces="$ifaces${ifaces:+ }$i"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080041 done
Dmitry Shmidte86eee12011-01-24 16:27:51 -080042 for x in "$1"/*; do
43 [ -e "$x" ] || continue
44 for i in $interface_order; do
45 if [ $i = "${x##*/}" ]; then
46 unset x
47 break
48 fi
49 done
50 [ -n "$x" ] && ifaces="$ifaces${ifaces:+ }${x##*/}"
51 done
52 echo "$ifaces"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080053}
54
55# We normally use sed to extract values using a key from a list of files
56# but sed may not always be available at the time.
57key_get_value()
58{
59 local key="$1" value= x= line=
60
61 shift
62 if type sed >/dev/null 2>&1; then
Dmitry Shmidte86eee12011-01-24 16:27:51 -080063 sed -n "s/^$key//p" $@
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080064 else
Dmitry Shmidta3a22602012-07-23 16:45:46 -070065 for x do
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080066 while read line; do
Dmitry Shmidte86eee12011-01-24 16:27:51 -080067 case "$line" in
68 "$key"*) echo "${line##$key}";;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080069 esac
Dmitry Shmidte86eee12011-01-24 16:27:51 -080070 done < "$x"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080071 done
72 fi
73}
74
75# We normally use sed to remove markers from a configuration file
76# but sed may not always be available at the time.
77remove_markers()
78{
79 local m1="$1" m2="$2" x= line= in_marker=0
80
81 shift; shift
82 if type sed >/dev/null 2>&1; then
Dmitry Shmidte86eee12011-01-24 16:27:51 -080083 sed "/^$m1/,/^$m2/d" $@
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080084 else
Dmitry Shmidta3a22602012-07-23 16:45:46 -070085 for x do
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080086 while read line; do
Dmitry Shmidte86eee12011-01-24 16:27:51 -080087 case "$line" in
88 "$m1"*) in_marker=1;;
89 "$m2"*) in_marker=0;;
90 *) [ $in_marker = 0 ] && echo "$line";;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080091 esac
Dmitry Shmidte86eee12011-01-24 16:27:51 -080092 done < "$x"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080093 done
94 fi
95}
96
Dmitry Shmidte86eee12011-01-24 16:27:51 -080097# Compare two files.
98# If different, replace first with second otherwise remove second.
The Android Open Source Projectf7c54212009-03-03 19:29:22 -080099change_file()
100{
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800101 if [ -e "$1" ]; then
102 if type cmp >/dev/null 2>&1; then
103 cmp -s "$1" "$2"
104 elif type diff >/dev/null 2>&1; then
105 diff -q "$1" "$2" >/dev/null
106 else
107 # Hopefully we're only working on small text files ...
108 [ "$(cat "$1")" = "$(cat "$2")" ]
109 fi
110 if [ $? -eq 0 ]; then
111 rm -f "$2"
112 return 1
113 fi
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800114 fi
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800115 cat "$2" > "$1"
116 rm -f "$2"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800117 return 0
118}
119
120# Save a config file
121save_conf()
122{
123 if [ -f "$1" ]; then
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800124 rm -f "$1-pre.$interface"
125 cat "$1" > "$1-pre.$interface"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800126 fi
127}
128
129# Restore a config file
130restore_conf()
131{
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800132 [ -f "$1-pre.$interface" ] || return 1
133 cat "$1-pre.$interface" > "$1"
134 rm -f "$1-pre.$interface"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800135}
136
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800137# Write a syslog entry
138syslog()
139{
140 local lvl="$1"
141
142 [ -n "$lvl" ] && shift
143 if [ -n "$*" ]; then
144 if type logger >/dev/null 2>&1; then
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700145 logger -t dhcpcd -p daemon."$lvl" -is "$interface: $*"
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800146 fi
147 fi
148}
149
Dmitry Shmidta3a22602012-07-23 16:45:46 -0700150# Check for a valid domain name as per RFC1123 with the exception of
151# allowing - and _ as they seem to be widely used.
152valid_domainname()
153{
154 local name="$1" label
155
156 [ -z "$name" -o ${#name} -gt 255 ] && return 1
157
158 while [ -n "$name" ]; do
159 label="${name%%.*}"
160 [ -z "$label" -o ${#label} -gt 63 ] && return 1
161 case "$label" in
162 -*|_*|*-|*_) return 1;;
163 *[![:alnum:]-_]*) return 1;;
164 esac
165 [ "$name" = "${name#*.}" ] && break
166 name="${name#*.}"
167 done
168 return 0
169}
170
171valid_domainname_list()
172{
173 local name
174
175 for name do
176 valid_domainname "$name" || return $?
177 done
178 return 0
179}
180
181# Check for a valid path
182valid_path()
183{
184 case "$@" in
185 *[![:alnum:]#%+-_:\.,@~\\/\[\]=\ ]*) return 1;;
186 esac
187 return 0
188}
189
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800190# Check a system service exists
191service_exists()
192{
193 @SERVICEEXISTS@
194}
195
196# Send a command to a system service
197service_cmd()
198{
199 @SERVICECMD@
200}
201
202# Send a command to a system service if it is running
203service_status()
204{
205 @SERVICESTATUS@
206}
207
208# Handy macros for our hooks
209service_command()
210{
211 service_exists $1 && service_cmd $1 $2
212}
213service_condcommand()
214{
215 service_exists $1 && service_status $1 && service_cmd $1 $2
216}
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800217
218# We source each script into this one so that scripts run earlier can
219# remove variables from the environment so later scripts don't see them.
220# Thus, the user can create their dhcpcd.enter/exit-hook script to configure
221# /etc/resolv.conf how they want and stop the system scripts ever updating it.
222for hook in \
223 @SYSCONFDIR@/dhcpcd.enter-hook \
224 @HOOKDIR@/* \
225 @SYSCONFDIR@/dhcpcd.exit-hook
226do
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800227 for skip in $skip_hooks; do
228 case "$hook" in
229 */"$skip") continue 2;;
230 */[0-9][0-9]"-$skip") continue 2;;
231 */[0-9][0-9]"-$skip.sh") continue 2;;
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800232 esac
233 done
Dmitry Shmidte86eee12011-01-24 16:27:51 -0800234 if [ -f "$hook" ]; then
235 . "$hook"
The Android Open Source Projectf7c54212009-03-03 19:29:22 -0800236 fi
237done