]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/helper/oci-setup
Early spring clean: Remove trailing whitespaces, and correct licence headers
[people/pmueller/ipfire-2.x.git] / src / initscripts / helper / oci-setup
CommitLineData
138c94a9 1#!/bin/bash
66c36198
PM
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
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###############################################################################
138c94a9
MT
21
22. /etc/sysconfig/rc
23. ${rc_functions}
24
25# Set PATH to find our own executables
26export PATH=/usr/local/sbin:/usr/local/bin:${PATH}
27
28# GCP only supports an MTU of 1460
29DEFAULT_MTU=1460
30
31get() {
32 local file="${1}"
33
34 wget -qO - "http://169.254.169.254/opc/v1/${file}"
35}
36
37to_address() {
38 local n="${1}"
39
40 local o1=$(( (n & 0xff000000) >> 24 ))
41 local o2=$(( (n & 0xff0000) >> 16 ))
42 local o3=$(( (n & 0xff00) >> 8 ))
43 local o4=$(( (n & 0xff) ))
44
45 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
46}
47
48to_integer() {
49 local address="${1}"
50
51 local integer=0
52
53 local i
54 for i in ${address//\./ }; do
55 integer=$(( (integer << 8) + i ))
56 done
57
58 printf "%d\n" "${integer}"
59}
60
61prefix2netmask() {
62 local prefix=${1}
63
64 local zeros=$(( 32 - prefix ))
65 local netmask=0
66
67 local i
68 for (( i=0; i<${zeros}; i++ )); do
69 netmask=$(( (netmask << 1) ^ 1 ))
70 done
71
72 to_address "$(( netmask ^ 0xffffffff ))"
73}
74
75oci_list_interfaces() {
76 get "vnics/" | python3 -c "import json, sys; print(\"\n\".join([vnic[\"vnicId\"] for vnic in json.load(sys.stdin)]))"
77}
78
79oci_get_interface_param() {
80 local id="${1}"
81 local param="${2}"
82
83 get "vnics/" | python3 -c "import json, sys; print(\"\n\".join(vnic.get(\"${param}\", \"\") for vnic in json.load(sys.stdin) if vnic[\"vnicId\"] == \"${id}\"))"
84}
85
86import_oci_configuration() {
87 local instance_id="$(get instance/id)"
88
89 boot_mesg "Importing Oracle Cloud Infrastructure configuration for instance ${instance_id}..."
90
91 # Store instance ID
92 echo "${instance_id}" > /var/run/oci-instance-id
93
94 # Initialise system settings
95 local hostname=$(get instance/hostname)
96
97 # Set hostname
98 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
99 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
100 fi
101
102 # Set domainname
103 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
104 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
105 fi
106
107 # Create setup user
108 if ! getent passwd setup &>/dev/null; then
109 useradd setup -s /usr/bin/run-setup -g nobody -m
110
111 # Unlock the account
112 usermod -p "x" setup
113 fi
114
115 # Import SSH keys for setup user
116 local line
117 while read -r line; do
118 # Strip the username part from the key
119 local key="${line#*:}"
120
121 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
122 mkdir -p "/home/setup/.ssh"
123 chmod 700 "/home/setup/.ssh"
124 chown setup.nobody "/home/setup/.ssh"
125
126 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
127 chmod 600 "/home/setup/.ssh/authorized_keys"
128 chown setup.nobody "/home/setup/.ssh/authorized_keys"
129 fi
130 done <<<"$(get instance/metadata/ssh_authorized_keys)"
131
132 # Download the user-data script only on the first boot
133 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
134 # Download a startup script
135 local script="$(get instance/metadata/user_data)"
136
137 # Execute the script
138 if [ "${script:0:2}" = "#!" ]; then
139 echo "${script}" > /tmp/user-data.script
140 chmod 700 /tmp/user-data.script
141
142 # Run the script
143 local now="$(date -u +"%s")"
144 /tmp/user-data.script &>/var/log/user-data.log.${now}
145
146 # Delete the script right away
147 rm /tmp/user-data.script
148 fi
149 fi
150
151 # Import network configuration
152 # After this, no network connectivity will be available from this script due to the
153 # renaming of the network interfaces for which they have to be shut down
154 local config_type=1
155 : > /var/ipfire/ethernet/settings
156
157 local id
158 for id in $(oci_list_interfaces); do
159 local mac="$(oci_get_interface_param "${id}" "macAddr")"
160
161 # First IPv4 address
162 local ipv4_address="$(oci_get_interface_param "${id}" "privateIp")"
163 local ipv4_address_num="$(to_integer "${ipv4_address}")"
164
165 local subnet="$(oci_get_interface_param "${id}" "subnetCidrBlock")"
166 local prefix="${subnet#*/}"
167
168 local netmask="$(prefix2netmask "${prefix}")"
138c94a9
MT
169
170 # Calculate the network and broadcast addresses
171 local netaddress="${subnet%/*}"
138c94a9
MT
172
173 local index="$(oci_get_interface_param "${id}" "nicIndex")"
174
175 # Set index to zero if it was empty
176 if [ -z "${index}" ]; then
177 index=0
178 fi
179
180 case "${index}" in
181 # RED
182 0)
183 local interface_name="red0"
184 local gateway="$(oci_get_interface_param "${id}" "virtualRouterIp")"
185
186 (
187 echo "RED_TYPE=STATIC"
188 echo "RED_DEV=${interface_name}"
189 echo "RED_MACADDR=${mac}"
190 echo "RED_DESCRIPTION='${id}'"
191 echo "RED_ADDRESS=${ipv4_address}"
192 echo "RED_NETMASK=${netmask}"
193 echo "RED_NETADDRESS=${netaddress}"
138c94a9
MT
194 echo "RED_MTU=1500"
195 echo "DEFAULT_GATEWAY=${gateway}"
196 ) >> /var/ipfire/ethernet/settings
197
198 # Import aliases for RED
199 #for alias in $(get "instance/network-interfaces/${device_number}/ip-aliases"); do
200 # echo "${alias},on,"
201 #done > /var/ipfire/ethernet/aliases
202 ;;
203
204 # GREEN
205 1)
206 local interface_name="green0"
207
208 (
209 echo "GREEN_DEV=${interface_name}"
210 echo "GREEN_MACADDR=${mac}"
211 echo "GREEN_DESCRIPTION='${id}'"
212 echo "GREEN_ADDRESS=${ipv4_address}"
213 echo "GREEN_NETMASK=${netmask}"
214 echo "GREEN_NETADDRESS=${netaddress}"
138c94a9
MT
215 echo "GREEN_MTU=${DEFAULT_MTU}"
216 ) >> /var/ipfire/ethernet/settings
217 ;;
218
219 # ORANGE
220 2)
221 local interface_name="orange0"
222 config_type=2
223
224 (
225 echo "ORANGE_DEV=${interface_name}"
226 echo "ORANGE_MACADDR=${mac}"
227 echo "ORANGE_DESCRIPTION='${id}'"
228 echo "ORANGE_ADDRESS=${ipv4_address}"
229 echo "ORANGE_NETMASK=${netmask}"
230 echo "ORANGE_NETADDRESS=${netaddress}"
138c94a9
MT
231 echo "ORANGE_MTU=${DEFAULT_MTU}"
232 ) >> /var/ipfire/ethernet/settings
233 ;;
234 esac
235 done
236
237 # Save CONFIG_TYPE
238 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
239
240 # Actions performed only on the very first start
241 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
242 # Disable using ISP nameservers
243 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
244
245 # Enable SSH
246 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
247
248 # Disable SSH password authentication
249 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
250
251 # Enable SSH key authentication
252 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
253
254 # Apply SSH settings
255 /usr/local/bin/sshctrl
256
257 # Mark SSH to start immediately (but not right now)
258 touch /var/ipfire/remote/enablessh
259 chown nobody:nobody /var/ipfire/remote/enablessh
260
261 # Firewall rules for SSH and WEBIF
262 (
263 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
264 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
265 ) >> /var/ipfire/firewall/input
266
267 # This script has now completed the first steps of setup
268 touch /var/ipfire/main/firstsetup_ok
269 fi
270
271 # All done
272 echo_ok
273}
274
275case "${reason}" in
276 PREINIT)
277 # Bring up the interface
278 ip link set "${interface}" up
279 ;;
280
281 BOUND|RENEW|REBIND|REBOOT)
282 # Remove any previous IP addresses
283 ip addr flush dev "${interface}"
284
285 # Add (or re-add) the new IP address
286 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
287
288 # Add the default route
289 ip route add "${new_routers}" dev "${interface}"
290 ip route add default via "${new_routers}"
291
292 # Setup DNS
293 for domain_name_server in ${new_domain_name_servers}; do
294 echo "nameserver ${domain_name_server}"
295 done > /etc/resolv.conf
296
297 # The system is online now
298 touch /var/ipfire/red/active
299
300 # Import OCI configuration
301 import_oci_configuration
302 ;;
303
304 EXPIRE|FAIL|RELEASE|STOP)
305 # The system is no longer online
306 rm -f /var/ipfire/red/active
307
308 # Remove all IP addresses
309 ip addr flush dev "${interface}"
310
311 # Shut down the interface
312 ip link set "${interface}" down
313 ;;
314
315 *)
316 echo "Unhandled reason: ${reason}" >&2
317 exit 2
318 ;;
319esac
320
321# Terminate
322exit 0