]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/helper/gcp-setup
Early spring clean: Remove trailing whitespaces, and correct licence headers
[people/pmueller/ipfire-2.x.git] / src / initscripts / helper / gcp-setup
CommitLineData
89b10e70 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###############################################################################
89b10e70
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
bf1ae6aa
MT
28# GCP only supports an MTU of 1460
29DEFAULT_MTU=1460
30
89b10e70
MT
31get() {
32 local file="${1}"
33
34 wget --header="Metadata-Flavor: Google" -qO - "http://169.254.169.254/computeMetadata/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
75import_gcp_configuration() {
76 local instance_id="$(get instance/id)"
77
78 boot_mesg "Importing Google Compute Platform configuration for instance ${instance_id}..."
79
80 # Store instance ID
81 echo "${instance_id}" > /var/run/gcp-instance-id
82
83 # Initialise system settings
84 local hostname=$(get instance/hostname)
85
86 # Set hostname
87 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
88 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
89 fi
90
91 # Set domainname
92 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
93 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
94 fi
95
96 # Create setup user
97 if ! getent passwd setup &>/dev/null; then
98 useradd setup -s /usr/bin/run-setup -g nobody -m
99
100 # Unlock the account
101 usermod -p "x" setup
102 fi
103
104 # Import SSH keys for setup user
105 local line
106 while read -r line; do
107 # Strip the username part from the key
108 local key="${line#*:}"
109
110 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
111 mkdir -p "/home/setup/.ssh"
112 chmod 700 "/home/setup/.ssh"
113 chown setup.nobody "/home/setup/.ssh"
114
115 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
116 chmod 600 "/home/setup/.ssh/authorized_keys"
117 chown setup.nobody "/home/setup/.ssh/authorized_keys"
118 fi
119 done <<<"$(get instance/attributes/ssh-keys)"
120
121 # Download the user-data script only on the first boot
122 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
123 # Download a startup script
124 local script="$(get instance/attributes/startup-script)"
125
126 # Execute the script
127 if [ "${script:0:2}" = "#!" ]; then
128 echo "${script}" > /tmp/gcp-startup.script
129 chmod 700 /tmp/gcp-startup.script
130
131 # Run the script
132 local now="$(date -u +"%s")"
133 /tmp/gcp-startup.script &>/var/log/startup-script.log.${now}
134
135 # Delete the script right away
136 rm /tmp/gcp-startup.script
137 fi
138 fi
139
140 # Import network configuration
141 # After this, no network connectivity will be available from this script due to the
142 # renaming of the network interfaces for which they have to be shut down
143 local config_type=1
144 : > /var/ipfire/ethernet/settings
145
146 local device_number
147 for device_number in $(get instance/network-interfaces/); do
148 # Remove trailing slash
149 device_number="${device_number//\//}"
150
151 local mac="$(get "instance/network-interfaces/${device_number}/mac")"
152
153 # XXX TODO read the MTU because Google seems to only support 1460
154
155 # First IPv4 address
156 local ipv4_address="$(get "instance/network-interfaces/${device_number}/ip")"
157 local ipv4_address_num="$(to_integer "${ipv4_address}")"
158
159 local netmask="$(get "instance/network-interfaces/${device_number}/subnetmask")"
160 local netmask_num="$(to_integer "${netmask}")"
161
162 # Calculate the network and broadcast addresses
163 local netaddress="$(to_address $(( ipv4_address_num & netmask_num )))"
89b10e70
MT
164
165 case "${device_number}" in
166 # RED
167 0)
168 local interface_name="red0"
169 local gateway="$(get instance/network-interfaces/${device_number}/gateway)"
170
171 (
172 echo "RED_TYPE=STATIC"
173 echo "RED_DEV=${interface_name}"
174 echo "RED_MACADDR=${mac}"
175 echo "RED_DESCRIPTION='${interface_id}'"
176 echo "RED_ADDRESS=${ipv4_address}"
177 echo "RED_NETMASK=${netmask}"
178 echo "RED_NETADDRESS=${netaddress}"
bf1ae6aa 179 echo "RED_MTU=${DEFAULT_MTU}"
89b10e70
MT
180 echo "DEFAULT_GATEWAY=${gateway}"
181 ) >> /var/ipfire/ethernet/settings
182
183 # Import aliases for RED
184 for alias in $(get "instance/network-interfaces/${device_number}/ip-aliases"); do
185 echo "${alias},on,"
186 done > /var/ipfire/ethernet/aliases
187 ;;
188
189 # GREEN
190 1)
191 local interface_name="green0"
192
193 (
194 echo "GREEN_DEV=${interface_name}"
195 echo "GREEN_MACADDR=${mac}"
196 echo "GREEN_DESCRIPTION='${interface_id}'"
197 echo "GREEN_ADDRESS=${ipv4_address}"
198 echo "GREEN_NETMASK=${netmask}"
199 echo "GREEN_NETADDRESS=${netaddress}"
bf1ae6aa 200 echo "GREEN_MTU=${DEFAULT_MTU}"
89b10e70
MT
201 ) >> /var/ipfire/ethernet/settings
202 ;;
203
204 # ORANGE
205 2)
206 local interface_name="orange0"
207 config_type=2
208
209 (
210 echo "ORANGE_DEV=${interface_name}"
211 echo "ORANGE_MACADDR=${mac}"
212 echo "ORANGE_DESCRIPTION='${interface_id}'"
213 echo "ORANGE_ADDRESS=${ipv4_address}"
214 echo "ORANGE_NETMASK=${netmask}"
215 echo "ORANGE_NETADDRESS=${netaddress}"
bf1ae6aa 216 echo "ORANGE_MTU=${DEFAULT_MTU}"
89b10e70
MT
217 ) >> /var/ipfire/ethernet/settings
218 ;;
219 esac
220 done
221
222 # Save CONFIG_TYPE
223 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
224
225 # Actions performed only on the very first start
226 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
227 # Disable using ISP nameservers
228 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
229
230 # Enable SSH
231 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
232
233 # Disable SSH password authentication
234 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
235
236 # Enable SSH key authentication
237 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
238
239 # Apply SSH settings
240 /usr/local/bin/sshctrl
241
242 # Mark SSH to start immediately (but not right now)
243 touch /var/ipfire/remote/enablessh
244 chown nobody:nobody /var/ipfire/remote/enablessh
245
246 # Firewall rules for SSH and WEBIF
247 (
248 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
249 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
250 ) >> /var/ipfire/firewall/input
251
252 # This script has now completed the first steps of setup
253 touch /var/ipfire/main/firstsetup_ok
254 fi
255
256 # All done
257 echo_ok
258}
259
260case "${reason}" in
261 PREINIT)
262 # Bring up the interface
263 ip link set "${interface}" up
264 ;;
265
266 BOUND|RENEW|REBIND|REBOOT)
267 # Remove any previous IP addresses
268 ip addr flush dev "${interface}"
269
270 # Add (or re-add) the new IP address
271 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
272
273 # Add the default route
b6a58881 274 ip route add "${new_routers}" dev "${interface}"
89b10e70
MT
275 ip route add default via "${new_routers}"
276
277 # Setup DNS
278 for domain_name_server in ${new_domain_name_servers}; do
279 echo "nameserver ${domain_name_server}"
280 done > /etc/resolv.conf
281
282 # The system is online now
283 touch /var/ipfire/red/active
284
285 # Import GCP configuration
286 import_gcp_configuration
287 ;;
288
289 EXPIRE|FAIL|RELEASE|STOP)
290 # The system is no longer online
291 rm -f /var/ipfire/red/active
292
293 # Remove all IP addresses
294 ip addr flush dev "${interface}"
295
296 # Shut down the interface
297 ip link set "${interface}" down
298 ;;
299
300 *)
301 echo "Unhandled reason: ${reason}" >&2
302 exit 2
303 ;;
304esac
305
306# Terminate
307exit 0