]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/helper/azure-setup
Merge remote-tracking branch 'origin/next'
[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
141 # Import any DNS server settings
142 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
143
144 # Import network configuration
145 # After this, no network connectivity will be available from this script due to the
146 # renaming of the network interfaces for which they have to be shut down
147 local config_type=1
148 : > /var/ipfire/ethernet/settings
149
150 local device_number
151 for device_number in $(get network/interface); do
152 # Remove trailing slash
153 device_number="${device_number//\//}"
154
155 local mac="$(get "network/interface/${device_number}/macAddress")"
156 mac="$(format_mac "${mac}")"
157
158 # First IPv4 address
159 local ipv4_address="$(get "network/interface/${device_number}/ipv4/ipAddress/0/privateIpAddress")"
160 local ipv4_address_num="$(to_integer "${ipv4_address}")"
161 local prefix="$(get "network/interface/${device_number}/ipv4/subnet/0/prefix")"
162 local netmask="$(prefix2netmask "${prefix}")"
163 local netmask_num="$(to_integer "${netmask}")"
164
165 # Calculate the network and broadcast addresses
166 local netaddress="$(get "network/interface/${device_number}/ipv4/subnet/0/address")"
167 local netaddress_num="$(to_integer "${netaddress}")"
168 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
169
170 case "${device_number}" in
171 # RED
172 0)
173 local interface_name="red0"
174
175 # The gateway is always the first IP address in the subnet
176 local gateway="$(to_address $(( netaddress_num + 1 )))"
177
178 # Microsoft uses a special IP address for DNS
179 # https://blogs.msdn.microsoft.com/mast/2015/05/18/what-is-the-ip-address-168-63-129-16/
180 local dns1="168.63.129.16"
181 local dns2=
182
183 (
184 echo "RED_TYPE=STATIC"
185 echo "RED_DEV=${interface_name}"
186 echo "RED_MACADDR=${mac}"
187 echo "RED_DESCRIPTION='${interface_id}'"
188 echo "RED_ADDRESS=${ipv4_address}"
189 echo "RED_NETMASK=${netmask}"
190 echo "RED_NETADDRESS=${netaddress}"
191 echo "RED_BROADCAST=${broadcast}"
192 echo "DEFAULT_GATEWAY=${gateway}"
193 echo "DNS1=${DNS1:-${dns1}}"
194 echo "DNS2=${DNS2:-${dns2}}"
195 ) >> /var/ipfire/ethernet/settings
196
197 # Import aliases for RED
198 local address_no
199 for address_no in $(get "network/interface/0/ipv4/ipAddress"); do
200 # Remove trailing slash
201 address_no="${address_no//\//}"
202
203 # Skip the first address
204 [ "${address_no}" = "0" ] && continue
205
206 # Fetch the IP address
207 local alias="$(get "network/interface/0/ipv4/ipAddress/${address_no}/privateIpAddress")"
208 echo "${alias},on,"
209 done > /var/ipfire/ethernet/aliases
210 ;;
211
212 # GREEN
213 1)
214 local interface_name="green0"
215
216 (
217 echo "GREEN_DEV=${interface_name}"
218 echo "GREEN_MACADDR=${mac}"
219 echo "GREEN_DESCRIPTION='${interface_id}'"
220 echo "GREEN_ADDRESS=${ipv4_address}"
221 echo "GREEN_NETMASK=${netmask}"
222 echo "GREEN_NETADDRESS=${netaddress}"
223 echo "GREEN_BROADCAST=${broadcast}"
224 ) >> /var/ipfire/ethernet/settings
225 ;;
226
227 # ORANGE
228 2)
229 local interface_name="orange0"
230 config_type=2
231
232 (
233 echo "ORANGE_DEV=${interface_name}"
234 echo "ORANGE_MACADDR=${mac}"
235 echo "ORANGE_DESCRIPTION='${interface_id}'"
236 echo "ORANGE_ADDRESS=${ipv4_address}"
237 echo "ORANGE_NETMASK=${netmask}"
238 echo "ORANGE_NETADDRESS=${netaddress}"
239 echo "ORANGE_BROADCAST=${broadcast}"
240 ) >> /var/ipfire/ethernet/settings
241 ;;
242 esac
243 done
244
245 # Save CONFIG_TYPE
246 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
247
248 # Actions performed only on the very first start
249 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
250 # Enable SSH
251 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
252
253 # Disable SSH password authentication
254 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
255
256 # Enable SSH key authentication
257 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
258
259 # Apply SSH settings
260 /usr/local/bin/sshctrl
261
262 # Mark SSH to start immediately (but not right now)
263 touch /var/ipfire/remote/enablessh
264 chown nobody:nobody /var/ipfire/remote/enablessh
265
266 # Firewall rules for SSH and WEBIF
267 (
268 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
269 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
270 ) >> /var/ipfire/firewall/input
271
272 # This script has now completed the first steps of setup
273 touch /var/ipfire/main/firstsetup_ok
274 fi
275
276 # All done
277 echo_ok
278}
279
280case "${reason}" in
281 PREINIT)
282 # Bring up the interface
283 ip link set "${interface}" up
284 ;;
285
286 BOUND|RENEW|REBIND|REBOOT)
287 # Remove any previous IP addresses
288 ip addr flush dev "${interface}"
289
290 # Add (or re-add) the new IP address
291 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
292
293 # Add the default route
294 ip route add default via "${new_routers}"
295
296 # Setup DNS
297 for domain_name_server in ${new_domain_name_servers}; do
298 echo "nameserver ${domain_name_server}"
299 done > /etc/resolv.conf
300
301 # The system is online now
302 touch /var/ipfire/red/active
303
304 # Import Azure configuration
305 import_azure_configuration
306 ;;
307
308 EXPIRE|FAIL|RELEASE|STOP)
309 # The system is no longer online
310 rm -f /var/ipfire/red/active
311
312 # Remove all IP addresses
313 ip addr flush dev "${interface}"
314
315 # Shut down the interface
316 ip link set "${interface}" down
317 ;;
318
319 *)
320 echo "Unhandled reason: ${reason}" >&2
321 exit 2
322 ;;
323esac
324
325# Terminate
326exit 0