]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.dhclient
db23aff6a7c42e3e0e185b694a5e4d7b4cdb6cd4
[people/stevee/network.git] / src / functions / functions.dhclient
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 NETWORK_DHCP_DUID_FILE="${NETWORK_CONFIG_DIR}/dhcp-duid"
23
24 dhclient_start() {
25 local interface=${1}
26 local proto=${2}
27
28 assert isset interface
29 assert device_exists ${interface}
30
31 local service=$(dhclient_proto2service ${proto} ${interface})
32 service_start ${service}
33 }
34
35 dhclient_stop() {
36 local interface=${1}
37 local proto=${2}
38
39 local service=$(dhclient_proto2service ${proto} ${interface})
40 service_stop ${service}
41 }
42
43 dhclient_status() {
44 local interface=${1}
45 local proto=${2}
46
47 local service=$(dhclient_proto2service ${proto} ${interface})
48 service_status ${service}
49 }
50
51 dhclient_proto2service() {
52 local proto=${1}
53 assert isset proto
54
55 local interface=${2}
56 assert isset interface
57
58 local service
59
60 case "${proto}" in
61 ipv4)
62 service="dhclient4@${interface}.service"
63 ;;
64 ipv6)
65 service="dhclient6@${interface}.service"
66 ;;
67 *)
68 return ${EXIT_ERROR}
69 ;;
70 esac
71
72 assert isset service
73
74 echo "${service}"
75 return ${EXIT_OK}
76 }
77
78 dhclient_write_config() {
79 local interface="${1}"
80 shift
81
82 assert isset interface
83
84 local hostname
85 local vendor
86
87 local config_file
88 local leases_file
89
90 while [ $# -gt 0 ]; do
91 case "${1}" in
92 --config-file=*)
93 config_file="$(cli_get_val "${1}")"
94 ;;
95 --hostname=*)
96 hostname="$(cli_get_val "${1}")"
97 ;;
98 --leases-file=*)
99 leases_file="$(cli_get_val "${1}")"
100 ;;
101 --vendor=*)
102 vendor="$(cli_get_val "${1}")"
103 ;;
104 *)
105 log WARNING $"Unknown configuration option passed: ${1}."
106 ;;
107 esac
108 shift
109 done
110
111 assert isset config_file
112
113 # Set default values
114 if ! isset hostname; then
115 hostname="$(config_hostname)"
116 fi
117
118 if ! isset vendor; then
119 vendor="$(distro_get_pretty_name)"
120 fi
121
122 # Clear configuration file (if any).
123 mkdir -p $(dirname ${config_file}) 2>/dev/null
124 : > ${file}
125
126 # Print the header.
127 ( echo "#"
128 echo "# This is a dhclient daemon configuration file for ${interface}."
129 echo "# THIS FILE IS AUTOMATICALLY GENERATED AND WILL OVERWRITE"
130 echo "# ANY CUSTOM CHANGES!"
131 echo "#"
132 echo "# $(date -u)"
133 echo "#"
134 echo
135 ) >> "${config_file}"
136
137 # Global options.
138 fwrite "${config_file}" "send vendor-class-identifier \"${vendor}\";"
139 fwrite "${config_file}" # empty line
140
141 # Interface options.
142 (
143 echo "interface \"${interface}\" {"
144
145 if isset hostname; then
146 echo " send host-name \"${hostname}\";"
147 print
148 fi
149
150 echo "}"
151 ) >> "${config_file}"
152
153 # Update leases file
154 if isset leases_file; then
155 __dhclient_update_leases_file "${leases_file}" || return $?
156 fi
157
158 return ${EXIT_OK}
159 }
160
161 dhclient_get_duid() {
162 # If the DUID already exists, we do not do anything at all.
163 if [ -s "${NETWORK_DHCP_DUID_FILE}" ]; then
164 print "$(<${NETWORK_DHCP_DUID_FILE})"
165 return ${EXIT_OK}
166 fi
167
168 # If no DUID exists, we will need to create a new one
169 local duid="$(dhclient_generate_duid)"
170 log DEBUG "Created new DHCP DUID: ${duid}"
171
172 # Save the new DUID to file and return it
173 print "${duid}" > "${NETWORK_DHCP_DUID_FILE}"
174
175 print "${duid}"
176 return ${EXIT_OK}
177 }
178
179
180 dhclient_generate_duid() {
181 # Find lowest MAC/link-local address
182 local address="$(ports_lowest_address)"
183
184 # Use a random MAC address if no physical address could
185 # be found.
186 if ! isset address; then
187 log WARNING "Could not determine the lowest MAC/link-local address"
188 address="$(mac_generate)"
189 fi
190
191 log DEBUG "Using '${address}' to generate the DHCP DUID"
192
193 print "00030001${address//\:/}"
194 return ${EXIT_OK}
195 }
196
197 __dhclient_update_leases_file() {
198 local file="${1}"
199
200 local duid="$(dhclient_get_duid)"
201
202 if [ -e "${leases_file}" ]; then
203 local old_duid="$(__dhclient_get_duid_from_leases_file "${leases_file}")"
204
205 if [ "${duid}" = "${old_duid}" ]; then
206 log DEBUG "DUID in leases file matches. Nothing to do"
207 return ${EXIT_OK}
208 fi
209 fi
210
211 # If the leases file does not exist, yet, or the
212 # DUID in there is different, we will create/overwrite
213 # the leases file with the correct DUID.
214
215 (
216 printf "default-duid \""
217
218 local i=0
219 while [ ${i} -lt ${#duid} ]; do
220 printf "\\\\\\\\"
221 printf "x${duid:${i}:2}"
222 i=$(( ${i} + 2 ))
223 done
224
225 print "\";"
226 ) > "${leases_file}"
227
228 return ${EXIT_OK}
229 }
230
231 __dhclient_get_duid_from_leases_file() {
232 local file="${1}"
233
234 # Do nothing if the leases file cannot be read
235 [ -r "${file}" ] || return ${EXIT_OK}
236
237 local line
238 while read line; do
239 if [[ ${line} =~ ^default-duid ]]; then
240 line="${line/default-duid/}"
241 line="${line//\\\\x/}"
242 line="${line//;/}"
243
244 line="$(strip "${line}")"
245 unquote "${line}"
246 return ${EXIT_OK}
247 fi
248 done < "${file}"
249
250 return ${EXIT_ERROR}
251 }