]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/helper/aws-setup
nano: Update to 2.9.8
[people/pmueller/ipfire-2.x.git] / src / initscripts / helper / aws-setup
CommitLineData
bd3bcb45
MT
1#!/bin/bash
2
3. /etc/sysconfig/rc
4. ${rc_functions}
5
9ae73c30
MT
6# Set PATH to find our own executables
7export PATH=/usr/local/sbin:/usr/local/bin:${PATH}
8
bd3bcb45
MT
9get() {
10 local file="${1}"
11
4e4c122c 12 wget -qO - "http://169.254.169.254/latest/${file}"
bd3bcb45
MT
13}
14
15to_address() {
16 local n="${1}"
17
18 local o1=$(( (n & 0xff000000) >> 24 ))
19 local o2=$(( (n & 0xff0000) >> 16 ))
20 local o3=$(( (n & 0xff00) >> 8 ))
21 local o4=$(( (n & 0xff) ))
22
23 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
24}
25
26to_integer() {
27 local address="${1}"
28
29 local integer=0
30
31 local i
32 for i in ${address//\./ }; do
33 integer=$(( (integer << 8) + i ))
34 done
35
36 printf "%d\n" "${integer}"
37}
38
39prefix2netmask() {
40 local prefix=${1}
41
42 local zeros=$(( 32 - prefix ))
43 local netmask=0
44
45 local i
46 for (( i=0; i<${zeros}; i++ )); do
47 netmask=$(( (netmask << 1) ^ 1 ))
48 done
49
50 to_address "$(( netmask ^ 0xffffffff ))"
51}
52
c7141f04
MT
53find_interface() {
54 local mac="${1}"
55
56 local path
57 for path in /sys/class/net/*; do
58 local address="$(<${path}/address)"
59
60 if [ "${mac}" = "${address}" ]; then
61 basename "${path}"
62 return 0
63 fi
64 done
65
66 return 1
67}
68
bd3bcb45 69import_aws_configuration() {
4e4c122c 70 local instance_id="$(get meta-data/instance-id)"
bd3bcb45
MT
71
72 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
73
eb7ccf87
MT
74 # Store instance ID
75 echo "${instance_id}" > /var/run/aws-instance-id
76
bd3bcb45 77 # Initialise system settings
4e4c122c 78 local hostname=$(get meta-data/local-hostname)
bd3bcb45 79
3273ff48
MT
80 # Set hostname
81 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
82 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
83 fi
84
85 # Set domainname
86 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
87 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
bd3bcb45
MT
88 fi
89
1413006b
MT
90 # Create setup user
91 if ! getent passwd setup &>/dev/null; then
e2d9fe7e 92 useradd setup -s /usr/bin/run-setup -g nobody -m
87d7fc66
MT
93
94 # Unlock the account
95 usermod -p "x" setup
1413006b 96 fi
2e42a9ea 97
53eb4918
MT
98 # Import SSH keys for setup user
99 local line
4e4c122c 100 for line in $(get "meta-data/public-keys/"); do
53eb4918
MT
101 local key_no="${line%=*}"
102
4e4c122c 103 local key="$(get meta-data/public-keys/${key_no}/openssh-key)"
53eb4918
MT
104 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
105 mkdir -p "/home/setup/.ssh"
106 chmod 700 "/home/setup/.ssh"
6a7e6b44 107 chown setup.nobody "/home/setup/.ssh"
53eb4918
MT
108
109 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
110 chmod 600 "/home/setup/.ssh/authorized_keys"
6a7e6b44 111 chown setup.nobody "/home/setup/.ssh/authorized_keys"
53eb4918 112 fi
2e42a9ea
MT
113 done
114
8defa50e
MT
115 # Download the user-data script only on the first boot
116 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
6cf58643 117 # Initialize pakfire
96422f85 118 /etc/init.d/pakfire start &>/dev/null
6cf58643 119
0ed9b770 120 # Install all available updates
96422f85 121 ( pakfire update && pakfire upgrade -y ) &>/dev/null
0ed9b770 122
281d75c9
MT
123 # If an update requires a reboot, we will do it
124 if [ -e "/var/run/need_reboot" ]; then
125 reboot
126 exit 1
127 fi
128
8defa50e
MT
129 # Download user-data
130 local user_data="$(get user-data)"
131
132 # Save user-data script to be executed later
133 if [ "${user_data:0:2}" = "#!" ]; then
134 echo "${user_data}" > /tmp/aws-user-data.script
135 chmod 700 /tmp/aws-user-data.script
136
137 # Run the user-data script
40436fa1 138 /tmp/aws-user-data.script &>/root/user-data.log
8defa50e
MT
139
140 # Delete the script right away
141 rm /tmp/aws-user-data.script
142 fi
143 fi
4e4c122c 144
bd3bcb45
MT
145 # Import any DNS server settings
146 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
147
148 # Import network configuration
2e42a9ea
MT
149 # After this, no network connectivity will be available from this script due to the
150 # renaming of the network interfaces for which they have to be shut down
0f224ad7
MT
151 local config_type=1
152 : > /var/ipfire/ethernet/settings
bd3bcb45 153
3273ff48 154 local mac
4e4c122c 155 for mac in $(get meta-data/network/interfaces/macs/); do
bd3bcb45
MT
156 # Remove trailing slash
157 mac="${mac//\//}"
158
4e4c122c
MT
159 local device_number="$(get "meta-data/network/interfaces/macs/${mac}/device-number")"
160 local interface_id="$(get "meta-data/network/interfaces/macs/${mac}/interface-id")"
bd3bcb45
MT
161
162 # First IPv4 address
4e4c122c 163 local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
bd3bcb45
MT
164 local ipv4_address_num="$(to_integer "${ipv4_address}")"
165
607240e2 166 # Get VPC subnet
4e4c122c 167 local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
607240e2
MT
168 local vpc_netaddress="${vpc%/*}"
169 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
170
bd3bcb45 171 # Get subnet size
4e4c122c 172 local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
bd3bcb45
MT
173
174 local prefix="${subnet#*/}"
175 local netmask="$(prefix2netmask "${prefix}")"
176 local netmask_num="$(to_integer "${netmask}")"
177
178 # Calculate the network and broadcast addresses
179 local netaddress="${subnet%/*}"
180 local netaddress_num="$(to_integer "${netaddress}")"
181 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
182
bd3bcb45
MT
183 case "${device_number}" in
184 # RED
185 0)
c7141f04
MT
186 local interface_name="red0"
187
c86fd963
MT
188 # The gateway is always the first IP address in the subnet
189 local gateway="$(to_address $(( netaddress_num + 1 )))"
190
191 # The AWS internal DNS service is available on the second IP address of the VPC
192 local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
193 local dns2=
194
bd3bcb45
MT
195 (
196 echo "RED_TYPE=STATIC"
c7141f04 197 echo "RED_DEV=${interface_name}"
bd3bcb45
MT
198 echo "RED_MACADDR=${mac}"
199 echo "RED_DESCRIPTION='${interface_id}'"
200 echo "RED_ADDRESS=${ipv4_address}"
201 echo "RED_NETMASK=${netmask}"
202 echo "RED_NETADDRESS=${netaddress}"
203 echo "RED_BROADCAST=${broadcast}"
204 echo "DEFAULT_GATEWAY=${gateway}"
205 echo "DNS1=${DNS1:-${dns1}}"
206 echo "DNS2=${DNS2:-${dns2}}"
207 ) >> /var/ipfire/ethernet/settings
208
209 # Import aliases for RED
4e4c122c 210 for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
bd3bcb45
MT
211 echo "${alias},on,"
212 done > /var/ipfire/ethernet/aliases
213 ;;
214
215 # GREEN
216 1)
c7141f04
MT
217 local interface_name="green0"
218
bd3bcb45 219 (
c7141f04 220 echo "GREEN_DEV=${interface_name}"
bd3bcb45
MT
221 echo "GREEN_MACADDR=${mac}"
222 echo "GREEN_DESCRIPTION='${interface_id}'"
223 echo "GREEN_ADDRESS=${ipv4_address}"
224 echo "GREEN_NETMASK=${netmask}"
225 echo "GREEN_NETADDRESS=${netaddress}"
226 echo "GREEN_BROADCAST=${broadcast}"
227 ) >> /var/ipfire/ethernet/settings
228 ;;
0f224ad7
MT
229
230 # ORANGE
231 2)
c7141f04 232 local interface_name="orange0"
0f224ad7
MT
233 config_type=2
234
235 (
c7141f04 236 echo "ORANGE_DEV=${interface_name}"
0f224ad7
MT
237 echo "ORANGE_MACADDR=${mac}"
238 echo "ORANGE_DESCRIPTION='${interface_id}'"
239 echo "ORANGE_ADDRESS=${ipv4_address}"
240 echo "ORANGE_NETMASK=${netmask}"
241 echo "ORANGE_NETADDRESS=${netaddress}"
242 echo "ORANGE_BROADCAST=${broadcast}"
243 ) >> /var/ipfire/ethernet/settings
244 ;;
bd3bcb45 245 esac
c7141f04
MT
246
247 # Rename interface
248 local interface="$(find_interface "${mac}")"
249
470e85c3 250 if [ -n "${interface}" ] && [ -n "${interface_name}" ] && [ "${interface}" != "${interface_name}" ]; then
c7141f04
MT
251 ip link set "${interface}" down
252 ip link set "${interface}" name "${interface_name}"
253 fi
bd3bcb45
MT
254 done
255
0f224ad7
MT
256 # Save CONFIG_TYPE
257 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
258
bd3bcb45
MT
259 # Actions performed only on the very first start
260 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
7fa83c2f 261 # Enable SSH
8b59ef08
MT
262 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
263
0cf70cae
MT
264 # Disable SSH password authentication
265 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
266
7fa83c2f
MT
267 # Enable SSH key authentication
268 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
269
8f2c3b49
MT
270 # Apply SSH settings
271 /usr/local/bin/sshctrl
272
693208bf
MT
273 # Mark SSH to start immediately (but not right now)
274 touch /var/ipfire/remote/enablessh
275 chown nobody:nobody /var/ipfire/remote/enablessh
276
bd3bcb45
MT
277 # Firewall rules for SSH and WEBIF
278 (
279 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
280 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
281 ) >> /var/ipfire/firewall/input
282
283 # This script has now completed the first steps of setup
284 touch /var/ipfire/main/firstsetup_ok
285 fi
286
287 # All done
288 echo_ok
289}
290
291case "${reason}" in
292 PREINIT)
293 # Bring up the interface
294 ip link set "${interface}" up
295 ;;
296
297 BOUND|RENEW|REBIND|REBOOT)
298 # Remove any previous IP addresses
299 ip addr flush dev "${interface}"
300
301 # Add (or re-add) the new IP address
302 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
303
304 # Add the default route
305 ip route add default via "${new_routers}"
306
647ca912
MT
307 # Setup DNS
308 for domain_name_server in ${new_domain_name_servers}; do
309 echo "nameserver ${domain_name_server}"
310 done > /etc/resolv.conf
311
312 # The system is online now
313 touch /var/ipfire/red/active
314
bd3bcb45
MT
315 # Import AWS configuration
316 import_aws_configuration
317 ;;
318
319 EXPIRE|FAIL|RELEASE|STOP)
647ca912
MT
320 # The system is no longer online
321 rm -f /var/ipfire/red/active
322
bd3bcb45
MT
323 # Remove all IP addresses
324 ip addr flush dev "${interface}"
325 ;;
326
327 *)
328 echo "Unhandled reason: ${reason}" >&2
329 exit 2
330 ;;
331esac
332
333# Terminate
334exit 0