2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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. #
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. #
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/>. #
20 ###############################################################################
22 NETWORK_DHCP_DUID_FILE
="${NETWORK_CONFIG_DIR}/dhcp-duid"
28 assert isset interface
29 assert device_exists
${interface}
31 local service
=$
(dhclient_proto2service
${proto} ${interface})
32 service_start
${service}
39 local service
=$
(dhclient_proto2service
${proto} ${interface})
40 service_stop
${service}
47 local service
=$
(dhclient_proto2service
${proto} ${interface})
48 service_status
${service}
51 dhclient_proto2service
() {
56 assert isset interface
62 service
="dhclient4@${interface}.service"
65 service
="dhclient6@${interface}.service"
78 dhclient_write_config
() {
79 local interface
="${1}"
82 assert isset interface
90 while [ $# -gt 0 ]; do
93 config_file
="$(cli_get_val "${1}")"
96 hostname
="$(cli_get_val "${1}")"
99 leases_file
="$(cli_get_val "${1}")"
102 vendor
="$(cli_get_val "${1}")"
105 log WARNING $
"Unknown configuration option passed: ${1}."
111 assert isset config_file
114 if ! isset hostname
; then
115 hostname
="$(config_hostname)"
118 if ! isset vendor
; then
119 vendor
="$(distro_get_pretty_name)"
122 # Clear configuration file (if any).
123 mkdir
-p $
(dirname ${config_file}) 2>/dev
/null
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!"
135 ) >> "${config_file}"
138 fwrite
"${config_file}" "send vendor-class-identifier \"${vendor}\";"
139 fwrite
"${config_file}" # empty line
143 echo "interface \"${interface}\" {"
145 if isset hostname
; then
146 echo " send host-name \"${hostname}\";"
151 ) >> "${config_file}"
154 if isset leases_file
; then
155 __dhclient_update_leases_file
"${leases_file}" ||
return $?
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})"
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}"
172 # Save the new DUID to file and return it
173 print
"${duid}" > "${NETWORK_DHCP_DUID_FILE}"
180 dhclient_generate_duid
() {
181 # Find lowest MAC/link-local address
182 local address
="$(ports_lowest_address)"
184 # Use a random MAC address if no physical address could
186 if ! isset address
; then
187 log WARNING
"Could not determine the lowest MAC/link-local address"
188 address
="$(mac_generate)"
191 log DEBUG
"Using '${address}' to generate the DHCP DUID"
193 print
"00030001${address//\:/}"
197 __dhclient_update_leases_file
() {
200 local duid
="$(dhclient_get_duid)"
202 if [ -e "${leases_file}" ]; then
203 local old_duid
="$(__dhclient_get_duid_from_leases_file "${leases_file}")"
205 if [ "${duid}" = "${old_duid}" ]; then
206 log DEBUG
"DUID in leases file matches. Nothing to do"
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.
216 printf "default-duid \""
219 while [ ${i} -lt ${#duid} ]; do
221 printf "x${duid:${i}:2}"
231 __dhclient_get_duid_from_leases_file
() {
234 # Do nothing if the leases file cannot be read
235 [ -r "${file}" ] ||
return ${EXIT_OK}
239 if [[ ${line} =~ ^default-duid
]]; then
240 line
="${line/default-duid/}"
241 line
="${line//\\\\x/}"
244 line
="$(strip "${line}")"