]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/helper/azure-setup
6 # Set PATH to find our own executables
7 export PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:${PATH}
12 wget
-qO - --header="Metadata:true" "http://169.254.169.254/metadata/instance/${file}?api-version=2019-06-01&format=text"
18 echo "${mac:0:2}:${mac:2:2}:${mac:4:2}:${mac:6:2}:${mac:8:2}:${mac:10:2}"
24 local o1
=$
(( (n
& 0xff000000) >> 24 ))
25 local o2
=$
(( (n
& 0xff0000) >> 16 ))
26 local o3
=$
(( (n
& 0xff00) >> 8 ))
27 local o4
=$
(( (n
& 0xff) ))
29 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
38 for i
in ${address//\./ }; do
39 integer
=$
(( (integer
<< 8) + i ))
42 printf "%d\n" "${integer}"
48 local zeros=$(( 32 - prefix ))
52 for (( i=0; i<${zeros}; i++ )); do
53 netmask=$(( (netmask << 1) ^ 1 ))
56 to_address "$(( netmask ^ 0xffffffff ))"
59 import_azure_configuration() {
60 local instance_id="$(get compute/vmId)"
62 boot_mesg "Importing Microsoft Azure configuration for instance ${instance_id}..."
65 echo "${instance_id}" > /var/run/azure-instance-id
67 # Initialise system settings
68 local hostname=$(get compute/name)
71 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
72 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
76 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
77 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
80 # Import SSH keys for setup user
82 for line in $(get "compute/publicKeys/"); do
83 # Remove trailing slash
84 local key_no="${line//\//}"
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")"
91 if [[ "${path}" =~ ^/home ]]; then
95 # Cannot process this user
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
104 usermod -p "x" "${user}"
107 if [ -n "${key}" ] && ! grep -q "^${key}$" "${path}" 2>/dev/null; then
108 local dir="$(dirname "${path}")"
113 chown "${user}.nobody" "${dir}"
116 echo "${key}" >> "${path}"
118 chown "${user}.nobody" "${path}"
122 # Download the user-data script only on the first boot
123 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
125 local user_data="$(get customData)"
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
132 # Run the user-data script
133 local now="$(date -u +"%s")"
134 /tmp/azure-user-data.script &>/var/log/user-data.log.${now}
136 # Delete the script right away
137 rm /tmp/azure-user-data.script
141 # Import any DNS server settings
142 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
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
148 : > /var/ipfire/ethernet/settings
151 for device_number in $(get network/interface); do
152 # Remove trailing slash
153 device_number="${device_number//\//}"
155 local mac="$(get "network/interface/${device_number}/macAddress")"
156 mac="$(format_mac "${mac}")"
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}")"
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) )))"
170 case "${device_number}" in
173 local interface_name="red0"
175 # The gateway is always the first IP address in the subnet
176 local gateway="$(to_address $(( netaddress_num + 1 )))"
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"
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
197 # Import aliases for RED
199 for address_no in $(get "network/interface/0/ipv4/ipAddress"); do
200 # Remove trailing slash
201 address_no="${address_no//\//}"
203 # Skip the first address
204 [ "${address_no}" = "0" ] && continue
206 # Fetch the IP address
207 local alias="$(get "network/interface/0/ipv4/ipAddress/${address_no}/privateIpAddress")"
209 done > /var/ipfire/ethernet/aliases
214 local interface_name="green0"
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
229 local interface_name="orange0"
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
246 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
248 # Actions performed only on the very first start
249 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
251 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
253 # Disable SSH password authentication
254 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
256 # Enable SSH key authentication
257 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
260 /usr/local/bin/sshctrl
262 # Mark SSH to start immediately (but not right now)
263 touch /var/ipfire/remote/enablessh
264 chown nobody:nobody /var/ipfire/remote/enablessh
266 # Firewall rules for SSH and WEBIF
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
272 # This script has now completed the first steps of setup
273 touch /var/ipfire/main/firstsetup_ok
282 # Bring up the interface
283 ip link set "${interface}" up
286 BOUND|RENEW|REBIND|REBOOT)
287 # Remove any previous IP addresses
288 ip addr flush dev "${interface}"
290 # Add (or re-add) the new IP address
291 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
293 # Add the default route
294 ip route add default via "${new_routers}"
297 for domain_name_server in ${new_domain_name_servers}; do
298 echo "nameserver ${domain_name_server}"
299 done > /etc/resolv.conf
301 # The system is online now
302 touch /var/ipfire/red/active
304 # Import Azure configuration
305 import_azure_configuration
308 EXPIRE|FAIL|RELEASE|STOP)
309 # The system is no longer online
310 rm -f /var/ipfire/red/active
312 # Remove all IP addresses
313 ip addr flush dev "${interface}"
315 # Shut down the interface
316 ip link set "${interface}" down
320 echo "Unhandled reason: ${reason}" >&2