]> git.ipfire.org Git - ipfire-2.x.git/blame - src/initscripts/helper/aws-setup
Merge remote-tracking branch 'origin/next'
[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
68e060cb
MT
9# AWS supports an MTU of up to 9001 bytes
10DEFAULT_MTU=9001
11
bd3bcb45
MT
12get() {
13 local file="${1}"
14
4e4c122c 15 wget -qO - "http://169.254.169.254/latest/${file}"
bd3bcb45
MT
16}
17
18to_address() {
19 local n="${1}"
20
21 local o1=$(( (n & 0xff000000) >> 24 ))
22 local o2=$(( (n & 0xff0000) >> 16 ))
23 local o3=$(( (n & 0xff00) >> 8 ))
24 local o4=$(( (n & 0xff) ))
25
26 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
27}
28
29to_integer() {
30 local address="${1}"
31
32 local integer=0
33
34 local i
35 for i in ${address//\./ }; do
36 integer=$(( (integer << 8) + i ))
37 done
38
39 printf "%d\n" "${integer}"
40}
41
42prefix2netmask() {
43 local prefix=${1}
44
45 local zeros=$(( 32 - prefix ))
46 local netmask=0
47
48 local i
49 for (( i=0; i<${zeros}; i++ )); do
50 netmask=$(( (netmask << 1) ^ 1 ))
51 done
52
53 to_address "$(( netmask ^ 0xffffffff ))"
54}
55
56import_aws_configuration() {
4e4c122c 57 local instance_id="$(get meta-data/instance-id)"
bd3bcb45
MT
58
59 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
60
eb7ccf87
MT
61 # Store instance ID
62 echo "${instance_id}" > /var/run/aws-instance-id
63
bd3bcb45 64 # Initialise system settings
4e4c122c 65 local hostname=$(get meta-data/local-hostname)
bd3bcb45 66
3273ff48
MT
67 # Set hostname
68 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
69 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
70 fi
71
72 # Set domainname
73 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
74 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
bd3bcb45
MT
75 fi
76
1413006b
MT
77 # Create setup user
78 if ! getent passwd setup &>/dev/null; then
e2d9fe7e 79 useradd setup -s /usr/bin/run-setup -g nobody -m
87d7fc66
MT
80
81 # Unlock the account
82 usermod -p "x" setup
1413006b 83 fi
2e42a9ea 84
53eb4918
MT
85 # Import SSH keys for setup user
86 local line
4e4c122c 87 for line in $(get "meta-data/public-keys/"); do
53eb4918
MT
88 local key_no="${line%=*}"
89
4e4c122c 90 local key="$(get meta-data/public-keys/${key_no}/openssh-key)"
53eb4918
MT
91 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
92 mkdir -p "/home/setup/.ssh"
93 chmod 700 "/home/setup/.ssh"
6a7e6b44 94 chown setup.nobody "/home/setup/.ssh"
53eb4918
MT
95
96 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
97 chmod 600 "/home/setup/.ssh/authorized_keys"
6a7e6b44 98 chown setup.nobody "/home/setup/.ssh/authorized_keys"
53eb4918 99 fi
2e42a9ea
MT
100 done
101
8defa50e
MT
102 # Download the user-data script only on the first boot
103 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
104 # Download user-data
105 local user_data="$(get user-data)"
106
107 # Save user-data script to be executed later
108 if [ "${user_data:0:2}" = "#!" ]; then
109 echo "${user_data}" > /tmp/aws-user-data.script
110 chmod 700 /tmp/aws-user-data.script
111
112 # Run the user-data script
93363446
MT
113 local now="$(date -u +"%s")"
114 /tmp/aws-user-data.script &>/var/log/user-data.log.${now}
8defa50e
MT
115
116 # Delete the script right away
117 rm /tmp/aws-user-data.script
118 fi
119 fi
4e4c122c 120
bd3bcb45 121 # Import network configuration
2e42a9ea
MT
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
0f224ad7
MT
124 local config_type=1
125 : > /var/ipfire/ethernet/settings
bd3bcb45 126
3273ff48 127 local mac
4e4c122c 128 for mac in $(get meta-data/network/interfaces/macs/); do
bd3bcb45
MT
129 # Remove trailing slash
130 mac="${mac//\//}"
131
4e4c122c
MT
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")"
bd3bcb45
MT
134
135 # First IPv4 address
4e4c122c 136 local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
bd3bcb45
MT
137 local ipv4_address_num="$(to_integer "${ipv4_address}")"
138
607240e2 139 # Get VPC subnet
4e4c122c 140 local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
607240e2
MT
141 local vpc_netaddress="${vpc%/*}"
142 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
143
bd3bcb45 144 # Get subnet size
4e4c122c 145 local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
bd3bcb45
MT
146
147 local prefix="${subnet#*/}"
148 local netmask="$(prefix2netmask "${prefix}")"
bd3bcb45
MT
149
150 # Calculate the network and broadcast addresses
151 local netaddress="${subnet%/*}"
152 local netaddress_num="$(to_integer "${netaddress}")"
bd3bcb45 153
bd3bcb45
MT
154 case "${device_number}" in
155 # RED
156 0)
c7141f04
MT
157 local interface_name="red0"
158
c86fd963
MT
159 # The gateway is always the first IP address in the subnet
160 local gateway="$(to_address $(( netaddress_num + 1 )))"
161
bd3bcb45
MT
162 (
163 echo "RED_TYPE=STATIC"
c7141f04 164 echo "RED_DEV=${interface_name}"
bd3bcb45
MT
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}"
68e060cb 170 echo "RED_MTU=1500"
bd3bcb45 171 echo "DEFAULT_GATEWAY=${gateway}"
bd3bcb45
MT
172 ) >> /var/ipfire/ethernet/settings
173
174 # Import aliases for RED
4e4c122c 175 for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
bd3bcb45
MT
176 echo "${alias},on,"
177 done > /var/ipfire/ethernet/aliases
178 ;;
179
180 # GREEN
181 1)
c7141f04
MT
182 local interface_name="green0"
183
bd3bcb45 184 (
c7141f04 185 echo "GREEN_DEV=${interface_name}"
bd3bcb45
MT
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}"
68e060cb 191 echo "GREEN_MTU=${DEFAULT_MTU}"
bd3bcb45
MT
192 ) >> /var/ipfire/ethernet/settings
193 ;;
0f224ad7
MT
194
195 # ORANGE
196 2)
c7141f04 197 local interface_name="orange0"
0f224ad7
MT
198 config_type=2
199
200 (
c7141f04 201 echo "ORANGE_DEV=${interface_name}"
0f224ad7
MT
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}"
68e060cb 207 echo "ORANGE_MTU=${DEFAULT_MTU}"
0f224ad7
MT
208 ) >> /var/ipfire/ethernet/settings
209 ;;
bd3bcb45
MT
210 esac
211 done
212
0f224ad7
MT
213 # Save CONFIG_TYPE
214 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
215
bd3bcb45
MT
216 # Actions performed only on the very first start
217 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
88cb5eb1
MT
218 # Disable using ISP nameservers
219 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
220
7fa83c2f 221 # Enable SSH
8b59ef08
MT
222 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
223
0cf70cae
MT
224 # Disable SSH password authentication
225 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
226
7fa83c2f
MT
227 # Enable SSH key authentication
228 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
229
8f2c3b49
MT
230 # Apply SSH settings
231 /usr/local/bin/sshctrl
232
693208bf
MT
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
bd3bcb45
MT
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 # This script has now completed the first steps of setup
244 touch /var/ipfire/main/firstsetup_ok
245 fi
246
247 # All done
248 echo_ok
249}
250
251case "${reason}" in
252 PREINIT)
253 # Bring up the interface
254 ip link set "${interface}" up
255 ;;
256
257 BOUND|RENEW|REBIND|REBOOT)
258 # Remove any previous IP addresses
259 ip addr flush dev "${interface}"
260
261 # Add (or re-add) the new IP address
262 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
263
264 # Add the default route
265 ip route add default via "${new_routers}"
266
647ca912
MT
267 # Setup DNS
268 for domain_name_server in ${new_domain_name_servers}; do
269 echo "nameserver ${domain_name_server}"
270 done > /etc/resolv.conf
271
272 # The system is online now
273 touch /var/ipfire/red/active
274
bd3bcb45
MT
275 # Import AWS configuration
276 import_aws_configuration
277 ;;
278
279 EXPIRE|FAIL|RELEASE|STOP)
647ca912
MT
280 # The system is no longer online
281 rm -f /var/ipfire/red/active
282
bd3bcb45
MT
283 # Remove all IP addresses
284 ip addr flush dev "${interface}"
c5465a94
MT
285
286 # Shut down the interface
287 ip link set "${interface}" down
bd3bcb45
MT
288 ;;
289
290 *)
291 echo "Unhandled reason: ${reason}" >&2
292 exit 2
293 ;;
294esac
295
296# Terminate
297exit 0