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