]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/helper/azure-setup
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
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 ###############################################################################
25 # Set PATH to find our own executables
26 export PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:${PATH}
31 wget
--timeout=3 --tries=3 -qO - --header="Metadata:true" "http://169.254.169.254/metadata/instance/${file}?api-version=2019-06-01&format=text"
37 echo "${mac:0:2}:${mac:2:2}:${mac:4:2}:${mac:6:2}:${mac:8:2}:${mac:10:2}"
43 local o1
=$
(( (n
& 0xff000000) >> 24 ))
44 local o2
=$
(( (n
& 0xff0000) >> 16 ))
45 local o3
=$
(( (n
& 0xff00) >> 8 ))
46 local o4
=$
(( (n
& 0xff) ))
48 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
57 for i
in ${address//\./ }; do
58 integer
=$
(( (integer
<< 8) + i ))
61 printf "%d\n" "${integer}"
67 local zeros=$(( 32 - prefix ))
71 for (( i=0; i<${zeros}; i++ )); do
72 netmask=$(( (netmask << 1) ^ 1 ))
75 to_address "$(( netmask ^ 0xffffffff ))"
78 import_azure_configuration() {
79 local instance_id="$(get compute/vmId)"
80 if [ -z "${instance_id}" ]; then
84 boot_mesg "Importing Microsoft Azure configuration for instance ${instance_id}..."
87 echo "${instance_id}" > /var/run/azure-instance-id
89 # Initialise system settings
90 local hostname=$(get compute/name)
93 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
94 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
98 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
99 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
102 # Import SSH keys for setup user
104 for line in $(get "compute/publicKeys/"); do
105 # Remove trailing slash
106 local key_no="${line//\//}"
108 # Get the path where this key should be installed
109 local path="$(get "compute/publicKeys/${key_no}/path")"
110 local key="$(get "compute/publicKeys/${key_no}/keyData")"
113 if [[ "${path}" =~ ^/home ]]; then
117 # Cannot process this user
121 # Create user if it does not exist
122 if ! getent passwd "${user}" &>/dev/null; then
123 useradd "${user}" -s /usr/bin/run-setup -g nobody -m
126 usermod -p "x" "${user}"
129 if [ -n "${key}" ] && ! grep -q "^${key}$" "${path}" 2>/dev/null; then
130 local dir="$(dirname "${path}")"
135 chown "${user}.nobody" "${dir}"
138 echo "${key}" >> "${path}"
140 chown "${user}.nobody" "${path}"
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}")"
164 # Get the network address
165 local netaddress="$(get "network/interface/${device_number}/ipv4/subnet/0/address")"
166 local netaddress_num="$(to_integer "${netaddress}")"
168 case "${device_number}" in
171 local interface_name="red0"
173 # The gateway is always the first IP address in the subnet
174 local gateway="$(to_address $(( netaddress_num + 1 )))"
177 echo "RED_TYPE=STATIC"
178 echo "RED_DEV=${interface_name}"
179 echo "RED_MACADDR=${mac}"
180 echo "RED_DESCRIPTION='${interface_id}'"
181 echo "RED_ADDRESS=${ipv4_address}"
182 echo "RED_NETMASK=${netmask}"
183 echo "RED_NETADDRESS=${netaddress}"
184 echo "DEFAULT_GATEWAY=${gateway}"
185 ) >> /var/ipfire/ethernet/settings
187 # Import aliases for RED
189 for address_no in $(get "network/interface/0/ipv4/ipAddress"); do
190 # Remove trailing slash
191 address_no="${address_no//\//}"
193 # Skip the first address
194 [ "${address_no}" = "0" ] && continue
196 # Fetch the IP address
197 local alias="$(get "network/interface/0/ipv4/ipAddress/${address_no}/privateIpAddress")"
199 done > /var/ipfire/ethernet/aliases
204 local interface_name="green0"
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 ) >> /var/ipfire/ethernet/settings
218 local interface_name="orange0"
222 echo "ORANGE_DEV=${interface_name}"
223 echo "ORANGE_MACADDR=${mac}"
224 echo "ORANGE_DESCRIPTION='${interface_id}'"
225 echo "ORANGE_ADDRESS=${ipv4_address}"
226 echo "ORANGE_NETMASK=${netmask}"
227 echo "ORANGE_NETADDRESS=${netaddress}"
228 ) >> /var/ipfire/ethernet/settings
234 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
236 # Actions performed only on the very first start
237 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
238 # Disable using ISP nameservers
239 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
242 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
244 # Disable SSH password authentication
245 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
247 # Enable SSH key authentication
248 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
251 /usr/local/bin/sshctrl
253 # Mark SSH to start immediately (but not right now)
254 touch /var/ipfire/remote/enablessh
255 chown nobody:nobody /var/ipfire/remote/enablessh
257 # Firewall rules for SSH and WEBIF
259 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
260 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
261 ) >> /var/ipfire/firewall/input
264 local user_data="$(get customData)"
266 # Save user-data script to be executed later
267 if [ "${user_data:0:2}" = "#!" ]; then
268 echo "${user_data}" > /tmp/azure-user-data.script
269 chmod 700 /tmp/azure-user-data.script
271 # Run the user-data script
272 local now="$(date -u +"%s")"
273 /tmp/azure-user-data.script &>/var/log/user-data.log.${now}
275 # Delete the script right away
276 rm /tmp/azure-user-data.script
279 # This script has now completed the first steps of setup
280 touch /var/ipfire/main/firstsetup_ok
289 # Bring up the interface
290 ip link set "${interface}" up
293 BOUND|RENEW|REBIND|REBOOT)
294 # Remove any previous IP addresses
295 ip addr flush dev "${interface}"
297 # Add (or re-add) the new IP address
298 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
300 # Add the default route
301 ip route add default via "${new_routers}"
304 for domain_name_server in ${new_domain_name_servers}; do
305 echo "nameserver ${domain_name_server}"
306 done > /etc/resolv.conf
308 # The system is online now
309 touch /var/ipfire/red/active
311 # Import Azure configuration
312 import_azure_configuration
315 EXPIRE|FAIL|RELEASE|STOP)
316 # The system is no longer online
317 rm -f /var/ipfire/red/active
319 # Remove all IP addresses
320 ip addr flush dev "${interface}"
322 # Shut down the interface
323 ip link set "${interface}" down
327 echo "Unhandled reason: ${reason}" >&2