]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/helper/aws-setup
Early spring clean: Remove trailing whitespaces, and correct licence headers
[people/pmueller/ipfire-2.x.git] / src / initscripts / helper / aws-setup
CommitLineData
bd3bcb45 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###############################################################################
bd3bcb45
MT
21
22. /etc/sysconfig/rc
23. ${rc_functions}
24
9ae73c30
MT
25# Set PATH to find our own executables
26export PATH=/usr/local/sbin:/usr/local/bin:${PATH}
27
68e060cb
MT
28# AWS supports an MTU of up to 9001 bytes
29DEFAULT_MTU=9001
30
bd3bcb45
MT
31get() {
32 local file="${1}"
33
4e4c122c 34 wget -qO - "http://169.254.169.254/latest/${file}"
bd3bcb45
MT
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_aws_configuration() {
4e4c122c 76 local instance_id="$(get meta-data/instance-id)"
bd3bcb45
MT
77
78 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
79
eb7ccf87
MT
80 # Store instance ID
81 echo "${instance_id}" > /var/run/aws-instance-id
82
bd3bcb45 83 # Initialise system settings
4e4c122c 84 local hostname=$(get meta-data/local-hostname)
bd3bcb45 85
3273ff48
MT
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
bd3bcb45
MT
94 fi
95
1413006b
MT
96 # Create setup user
97 if ! getent passwd setup &>/dev/null; then
e2d9fe7e 98 useradd setup -s /usr/bin/run-setup -g nobody -m
87d7fc66
MT
99
100 # Unlock the account
101 usermod -p "x" setup
1413006b 102 fi
2e42a9ea 103
53eb4918
MT
104 # Import SSH keys for setup user
105 local line
4e4c122c 106 for line in $(get "meta-data/public-keys/"); do
53eb4918
MT
107 local key_no="${line%=*}"
108
4e4c122c 109 local key="$(get meta-data/public-keys/${key_no}/openssh-key)"
53eb4918
MT
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"
6a7e6b44 113 chown setup.nobody "/home/setup/.ssh"
53eb4918
MT
114
115 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
116 chmod 600 "/home/setup/.ssh/authorized_keys"
6a7e6b44 117 chown setup.nobody "/home/setup/.ssh/authorized_keys"
53eb4918 118 fi
2e42a9ea
MT
119 done
120
8defa50e
MT
121 # Download the user-data script only on the first boot
122 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
123 # Download user-data
124 local user_data="$(get user-data)"
125
126 # Save user-data script to be executed later
127 if [ "${user_data:0:2}" = "#!" ]; then
128 echo "${user_data}" > /tmp/aws-user-data.script
129 chmod 700 /tmp/aws-user-data.script
130
131 # Run the user-data script
93363446
MT
132 local now="$(date -u +"%s")"
133 /tmp/aws-user-data.script &>/var/log/user-data.log.${now}
8defa50e
MT
134
135 # Delete the script right away
136 rm /tmp/aws-user-data.script
137 fi
138 fi
4e4c122c 139
bd3bcb45 140 # Import network configuration
2e42a9ea
MT
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
0f224ad7
MT
143 local config_type=1
144 : > /var/ipfire/ethernet/settings
bd3bcb45 145
3273ff48 146 local mac
4e4c122c 147 for mac in $(get meta-data/network/interfaces/macs/); do
bd3bcb45
MT
148 # Remove trailing slash
149 mac="${mac//\//}"
150
4e4c122c
MT
151 local device_number="$(get "meta-data/network/interfaces/macs/${mac}/device-number")"
152 local interface_id="$(get "meta-data/network/interfaces/macs/${mac}/interface-id")"
bd3bcb45
MT
153
154 # First IPv4 address
4e4c122c 155 local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
bd3bcb45
MT
156 local ipv4_address_num="$(to_integer "${ipv4_address}")"
157
607240e2 158 # Get VPC subnet
4e4c122c 159 local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
607240e2
MT
160 local vpc_netaddress="${vpc%/*}"
161 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
162
bd3bcb45 163 # Get subnet size
4e4c122c 164 local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
bd3bcb45
MT
165
166 local prefix="${subnet#*/}"
167 local netmask="$(prefix2netmask "${prefix}")"
bd3bcb45
MT
168
169 # Calculate the network and broadcast addresses
170 local netaddress="${subnet%/*}"
171 local netaddress_num="$(to_integer "${netaddress}")"
bd3bcb45 172
bd3bcb45
MT
173 case "${device_number}" in
174 # RED
175 0)
c7141f04
MT
176 local interface_name="red0"
177
c86fd963
MT
178 # The gateway is always the first IP address in the subnet
179 local gateway="$(to_address $(( netaddress_num + 1 )))"
180
bd3bcb45
MT
181 (
182 echo "RED_TYPE=STATIC"
c7141f04 183 echo "RED_DEV=${interface_name}"
bd3bcb45
MT
184 echo "RED_MACADDR=${mac}"
185 echo "RED_DESCRIPTION='${interface_id}'"
186 echo "RED_ADDRESS=${ipv4_address}"
187 echo "RED_NETMASK=${netmask}"
188 echo "RED_NETADDRESS=${netaddress}"
68e060cb 189 echo "RED_MTU=1500"
bd3bcb45 190 echo "DEFAULT_GATEWAY=${gateway}"
bd3bcb45
MT
191 ) >> /var/ipfire/ethernet/settings
192
193 # Import aliases for RED
4e4c122c 194 for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
bd3bcb45
MT
195 echo "${alias},on,"
196 done > /var/ipfire/ethernet/aliases
197 ;;
198
199 # GREEN
200 1)
c7141f04
MT
201 local interface_name="green0"
202
bd3bcb45 203 (
c7141f04 204 echo "GREEN_DEV=${interface_name}"
bd3bcb45
MT
205 echo "GREEN_MACADDR=${mac}"
206 echo "GREEN_DESCRIPTION='${interface_id}'"
207 echo "GREEN_ADDRESS=${ipv4_address}"
208 echo "GREEN_NETMASK=${netmask}"
209 echo "GREEN_NETADDRESS=${netaddress}"
68e060cb 210 echo "GREEN_MTU=${DEFAULT_MTU}"
bd3bcb45
MT
211 ) >> /var/ipfire/ethernet/settings
212 ;;
0f224ad7
MT
213
214 # ORANGE
215 2)
c7141f04 216 local interface_name="orange0"
0f224ad7
MT
217 config_type=2
218
219 (
c7141f04 220 echo "ORANGE_DEV=${interface_name}"
0f224ad7
MT
221 echo "ORANGE_MACADDR=${mac}"
222 echo "ORANGE_DESCRIPTION='${interface_id}'"
223 echo "ORANGE_ADDRESS=${ipv4_address}"
224 echo "ORANGE_NETMASK=${netmask}"
225 echo "ORANGE_NETADDRESS=${netaddress}"
68e060cb 226 echo "ORANGE_MTU=${DEFAULT_MTU}"
0f224ad7
MT
227 ) >> /var/ipfire/ethernet/settings
228 ;;
bd3bcb45
MT
229 esac
230 done
231
0f224ad7
MT
232 # Save CONFIG_TYPE
233 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
234
bd3bcb45
MT
235 # Actions performed only on the very first start
236 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
88cb5eb1
MT
237 # Disable using ISP nameservers
238 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
239
7fa83c2f 240 # Enable SSH
8b59ef08
MT
241 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
242
0cf70cae
MT
243 # Disable SSH password authentication
244 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
245
7fa83c2f
MT
246 # Enable SSH key authentication
247 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
248
8f2c3b49
MT
249 # Apply SSH settings
250 /usr/local/bin/sshctrl
251
693208bf
MT
252 # Mark SSH to start immediately (but not right now)
253 touch /var/ipfire/remote/enablessh
254 chown nobody:nobody /var/ipfire/remote/enablessh
255
bd3bcb45
MT
256 # Firewall rules for SSH and WEBIF
257 (
258 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
259 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
260 ) >> /var/ipfire/firewall/input
261
262 # This script has now completed the first steps of setup
263 touch /var/ipfire/main/firstsetup_ok
264 fi
265
266 # All done
267 echo_ok
268}
269
270case "${reason}" in
271 PREINIT)
272 # Bring up the interface
273 ip link set "${interface}" up
274 ;;
275
276 BOUND|RENEW|REBIND|REBOOT)
277 # Remove any previous IP addresses
278 ip addr flush dev "${interface}"
279
280 # Add (or re-add) the new IP address
281 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
282
283 # Add the default route
284 ip route add default via "${new_routers}"
285
647ca912
MT
286 # Setup DNS
287 for domain_name_server in ${new_domain_name_servers}; do
288 echo "nameserver ${domain_name_server}"
289 done > /etc/resolv.conf
290
291 # The system is online now
292 touch /var/ipfire/red/active
293
bd3bcb45
MT
294 # Import AWS configuration
295 import_aws_configuration
296 ;;
297
298 EXPIRE|FAIL|RELEASE|STOP)
647ca912
MT
299 # The system is no longer online
300 rm -f /var/ipfire/red/active
301
bd3bcb45
MT
302 # Remove all IP addresses
303 ip addr flush dev "${interface}"
c5465a94
MT
304
305 # Shut down the interface
306 ip link set "${interface}" down
bd3bcb45
MT
307 ;;
308
309 *)
310 echo "Unhandled reason: ${reason}" >&2
311 exit 2
312 ;;
313esac
314
315# Terminate
316exit 0