]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/helper/azure-setup
cloud-init: Remove importing DNS settings
[ipfire-2.x.git] / src / initscripts / helper / azure-setup
CommitLineData
acf47bfa
MT
1#!/bin/bash
2
3. /etc/sysconfig/rc
4. ${rc_functions}
5
6# Set PATH to find our own executables
7export PATH=/usr/local/sbin:/usr/local/bin:${PATH}
8
9get() {
10 local file="${1}"
11
12 wget -qO - --header="Metadata:true" "http://169.254.169.254/metadata/instance/${file}?api-version=2019-06-01&format=text"
13}
14
15format_mac() {
16 local mac="${1,,}"
17
abccd997 18 echo "${mac:0:2}:${mac:2:2}:${mac:4:2}:${mac:6:2}:${mac:8:2}:${mac:10:2}"
acf47bfa
MT
19}
20
21to_address() {
22 local n="${1}"
23
24 local o1=$(( (n & 0xff000000) >> 24 ))
25 local o2=$(( (n & 0xff0000) >> 16 ))
26 local o3=$(( (n & 0xff00) >> 8 ))
27 local o4=$(( (n & 0xff) ))
28
29 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
30}
31
32to_integer() {
33 local address="${1}"
34
35 local integer=0
36
37 local i
38 for i in ${address//\./ }; do
39 integer=$(( (integer << 8) + i ))
40 done
41
42 printf "%d\n" "${integer}"
43}
44
45prefix2netmask() {
46 local prefix=${1}
47
48 local zeros=$(( 32 - prefix ))
49 local netmask=0
50
51 local i
52 for (( i=0; i<${zeros}; i++ )); do
53 netmask=$(( (netmask << 1) ^ 1 ))
54 done
55
56 to_address "$(( netmask ^ 0xffffffff ))"
57}
58
59import_azure_configuration() {
60 local instance_id="$(get compute/vmId)"
61
62 boot_mesg "Importing Microsoft Azure configuration for instance ${instance_id}..."
63
64 # Store instance ID
65 echo "${instance_id}" > /var/run/azure-instance-id
66
67 # Initialise system settings
68 local hostname=$(get compute/name)
69
70 # Set hostname
71 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
72 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
73 fi
74
75 # Set domainname
76 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
77 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
78 fi
79
80 # Import SSH keys for setup user
81 local line
82 for line in $(get "compute/publicKeys/"); do
83 # Remove trailing slash
84 local key_no="${line//\//}"
85
86 # Get the path where this key should be installed
87 local path="$(get "compute/publicKeys/${key_no}/path")"
88 local key="$(get "compute/publicKeys/${key_no}/keyData")"
89
90 local user
91 if [[ "${path}" =~ ^/home ]]; then
92 user="${path:6}"
93 user="${user%%/*}"
94 else
95 # Cannot process this user
96 continue
97 fi
98
99 # Create user if it does not exist
100 if ! getent passwd "${user}" &>/dev/null; then
101 useradd "${user}" -s /usr/bin/run-setup -g nobody -m
102
103 # Unlock the account
104 usermod -p "x" "${user}"
105 fi
106
107 if [ -n "${key}" ] && ! grep -q "^${key}$" "${path}" 2>/dev/null; then
108 local dir="$(dirname "${path}")"
109
110 # Install directory
111 mkdir -p "${dir}"
112 chmod 700 "${dir}"
113 chown "${user}.nobody" "${dir}"
114
115 # Install the key
116 echo "${key}" >> "${path}"
117 chmod 600 "${path}"
118 chown "${user}.nobody" "${path}"
119 fi
120 done
121
122 # Download the user-data script only on the first boot
123 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
124 # Download user-data
125 local user_data="$(get customData)"
126
127 # Save user-data script to be executed later
128 if [ "${user_data:0:2}" = "#!" ]; then
129 echo "${user_data}" > /tmp/azure-user-data.script
130 chmod 700 /tmp/azure-user-data.script
131
132 # Run the user-data script
133 local now="$(date -u +"%s")"
134 /tmp/azure-user-data.script &>/var/log/user-data.log.${now}
135
136 # Delete the script right away
137 rm /tmp/azure-user-data.script
138 fi
139 fi
140
acf47bfa
MT
141 # Import network configuration
142 # After this, no network connectivity will be available from this script due to the
143 # renaming of the network interfaces for which they have to be shut down
144 local config_type=1
145 : > /var/ipfire/ethernet/settings
146
147 local device_number
148 for device_number in $(get network/interface); do
149 # Remove trailing slash
150 device_number="${device_number//\//}"
151
152 local mac="$(get "network/interface/${device_number}/macAddress")"
153 mac="$(format_mac "${mac}")"
154
155 # First IPv4 address
156 local ipv4_address="$(get "network/interface/${device_number}/ipv4/ipAddress/0/privateIpAddress")"
157 local ipv4_address_num="$(to_integer "${ipv4_address}")"
158 local prefix="$(get "network/interface/${device_number}/ipv4/subnet/0/prefix")"
159 local netmask="$(prefix2netmask "${prefix}")"
160 local netmask_num="$(to_integer "${netmask}")"
161
162 # Calculate the network and broadcast addresses
163 local netaddress="$(get "network/interface/${device_number}/ipv4/subnet/0/address")"
164 local netaddress_num="$(to_integer "${netaddress}")"
165 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
166
167 case "${device_number}" in
168 # RED
169 0)
170 local interface_name="red0"
171
172 # The gateway is always the first IP address in the subnet
173 local gateway="$(to_address $(( netaddress_num + 1 )))"
174
acf47bfa
MT
175 (
176 echo "RED_TYPE=STATIC"
177 echo "RED_DEV=${interface_name}"
178 echo "RED_MACADDR=${mac}"
179 echo "RED_DESCRIPTION='${interface_id}'"
180 echo "RED_ADDRESS=${ipv4_address}"
181 echo "RED_NETMASK=${netmask}"
182 echo "RED_NETADDRESS=${netaddress}"
183 echo "RED_BROADCAST=${broadcast}"
184 echo "DEFAULT_GATEWAY=${gateway}"
acf47bfa
MT
185 ) >> /var/ipfire/ethernet/settings
186
187 # Import aliases for RED
188 local address_no
189 for address_no in $(get "network/interface/0/ipv4/ipAddress"); do
190 # Remove trailing slash
191 address_no="${address_no//\//}"
192
193 # Skip the first address
194 [ "${address_no}" = "0" ] && continue
195
196 # Fetch the IP address
197 local alias="$(get "network/interface/0/ipv4/ipAddress/${address_no}/privateIpAddress")"
198 echo "${alias},on,"
199 done > /var/ipfire/ethernet/aliases
200 ;;
201
202 # GREEN
203 1)
204 local interface_name="green0"
205
206 (
207 echo "GREEN_DEV=${interface_name}"
208 echo "GREEN_MACADDR=${mac}"
209 echo "GREEN_DESCRIPTION='${interface_id}'"
210 echo "GREEN_ADDRESS=${ipv4_address}"
211 echo "GREEN_NETMASK=${netmask}"
212 echo "GREEN_NETADDRESS=${netaddress}"
213 echo "GREEN_BROADCAST=${broadcast}"
214 ) >> /var/ipfire/ethernet/settings
215 ;;
216
217 # ORANGE
218 2)
219 local interface_name="orange0"
220 config_type=2
221
222 (
223 echo "ORANGE_DEV=${interface_name}"
224 echo "ORANGE_MACADDR=${mac}"
225 echo "ORANGE_DESCRIPTION='${interface_id}'"
226 echo "ORANGE_ADDRESS=${ipv4_address}"
227 echo "ORANGE_NETMASK=${netmask}"
228 echo "ORANGE_NETADDRESS=${netaddress}"
229 echo "ORANGE_BROADCAST=${broadcast}"
230 ) >> /var/ipfire/ethernet/settings
231 ;;
232 esac
233 done
234
235 # Save CONFIG_TYPE
236 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
237
238 # Actions performed only on the very first start
239 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
240 # Enable SSH
241 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
242
243 # Disable SSH password authentication
244 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
245
246 # Enable SSH key authentication
247 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
248
249 # Apply SSH settings
250 /usr/local/bin/sshctrl
251
252 # Mark SSH to start immediately (but not right now)
253 touch /var/ipfire/remote/enablessh
254 chown nobody:nobody /var/ipfire/remote/enablessh
255
256 # Firewall rules for SSH and WEBIF
257 (
258 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
259 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
260 ) >> /var/ipfire/firewall/input
261
262 # This script has now completed the first steps of setup
263 touch /var/ipfire/main/firstsetup_ok
264 fi
265
266 # All done
267 echo_ok
268}
269
270case "${reason}" in
271 PREINIT)
272 # Bring up the interface
273 ip link set "${interface}" up
274 ;;
275
276 BOUND|RENEW|REBIND|REBOOT)
277 # Remove any previous IP addresses
278 ip addr flush dev "${interface}"
279
280 # Add (or re-add) the new IP address
281 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
282
283 # Add the default route
284 ip route add default via "${new_routers}"
285
286 # Setup DNS
287 for domain_name_server in ${new_domain_name_servers}; do
288 echo "nameserver ${domain_name_server}"
289 done > /etc/resolv.conf
290
291 # The system is online now
292 touch /var/ipfire/red/active
293
294 # Import Azure configuration
295 import_azure_configuration
296 ;;
297
298 EXPIRE|FAIL|RELEASE|STOP)
299 # The system is no longer online
300 rm -f /var/ipfire/red/active
301
302 # Remove all IP addresses
303 ip addr flush dev "${interface}"
304
305 # Shut down the interface
306 ip link set "${interface}" down
307 ;;
308
309 *)
310 echo "Unhandled reason: ${reason}" >&2
311 exit 2
312 ;;
313esac
314
315# Terminate
316exit 0