]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.dhclient
Fix creating new configs
[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
125 # Print the header.
126 config_header "dhclient daemon configuration file for ${interface}" \
127 > "${config_file}"
128
129 # Global options.
130 fappend "${config_file}" "send vendor-class-identifier \"${vendor}\";"
131 fappend "${config_file}" # empty line
132
133 # Interface options.
134 (
135 echo "interface \"${interface}\" {"
136
137 if isset hostname; then
138 echo " send host-name \"${hostname}\";"
139 print
140 fi
141
142 echo "}"
143 ) >> "${config_file}"
144
145 # Update leases file
146 if isset leases_file; then
147 __dhclient_update_leases_file "${leases_file}" || return $?
148 fi
149
150 return ${EXIT_OK}
151 }
152
153 dhclient_get_duid() {
154 # If the DUID already exists, we do not do anything at all.
155 if [ -s "${NETWORK_DHCP_DUID_FILE}" ]; then
156 print "$(<${NETWORK_DHCP_DUID_FILE})"
157 return ${EXIT_OK}
158 fi
159
160 # If no DUID exists, we will need to create a new one
161 local duid="$(dhclient_generate_duid)"
162 log DEBUG "Created new DHCP DUID: ${duid}"
163
164 # Save the new DUID to file and return it
165 print "${duid}" > "${NETWORK_DHCP_DUID_FILE}"
166
167 print "${duid}"
168 return ${EXIT_OK}
169 }
170
171
172 dhclient_generate_duid() {
173 # Find lowest MAC/link-local address
174 local address="$(ports_lowest_address)"
175
176 # Use a random MAC address if no physical address could
177 # be found.
178 if ! isset address; then
179 log WARNING "Could not determine the lowest MAC/link-local address"
180 address="$(mac_generate)"
181 fi
182
183 log DEBUG "Using '${address}' to generate the DHCP DUID"
184
185 print "00030001${address//\:/}"
186 return ${EXIT_OK}
187 }
188
189 __dhclient_update_leases_file() {
190 local file="${1}"
191
192 local duid="$(dhclient_get_duid)"
193
194 if [ -e "${leases_file}" ]; then
195 local old_duid="$(__dhclient_get_duid_from_leases_file "${leases_file}")"
196
197 if [ "${duid}" = "${old_duid}" ]; then
198 log DEBUG "DUID in leases file matches. Nothing to do"
199 return ${EXIT_OK}
200 fi
201 fi
202
203 # If the leases file does not exist, yet, or the
204 # DUID in there is different, we will create/overwrite
205 # the leases file with the correct DUID.
206
207 (
208 printf "default-duid \""
209
210 local i=0
211 while [ ${i} -lt ${#duid} ]; do
212 printf "\\\\\\\\"
213 printf "x${duid:${i}:2}"
214 i=$(( ${i} + 2 ))
215 done
216
217 print "\";"
218 ) > "${leases_file}"
219
220 return ${EXIT_OK}
221 }
222
223 __dhclient_get_duid_from_leases_file() {
224 local file="${1}"
225
226 # Do nothing if the leases file cannot be read
227 [ -r "${file}" ] || return ${EXIT_OK}
228
229 local line
230 while read line; do
231 if [[ ${line} =~ ^default-duid ]]; then
232 line="${line/default-duid/}"
233 line="${line//\\\\x/}"
234 line="${line//;/}"
235
236 line="$(strip "${line}")"
237 unquote "${line}"
238 return ${EXIT_OK}
239 fi
240 done < "${file}"
241
242 return ${EXIT_ERROR}
243 }