]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/helper/aws-setup
aws: Re-enable check if we are actually running on EC2
[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
6get() {
7 local file="${1}"
8
9 wget -qO - "http://169.254.169.254/latest/meta-data/${file}"
10}
11
12to_address() {
13 local n="${1}"
14
15 local o1=$(( (n & 0xff000000) >> 24 ))
16 local o2=$(( (n & 0xff0000) >> 16 ))
17 local o3=$(( (n & 0xff00) >> 8 ))
18 local o4=$(( (n & 0xff) ))
19
20 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
21}
22
23to_integer() {
24 local address="${1}"
25
26 local integer=0
27
28 local i
29 for i in ${address//\./ }; do
30 integer=$(( (integer << 8) + i ))
31 done
32
33 printf "%d\n" "${integer}"
34}
35
36prefix2netmask() {
37 local prefix=${1}
38
39 local zeros=$(( 32 - prefix ))
40 local netmask=0
41
42 local i
43 for (( i=0; i<${zeros}; i++ )); do
44 netmask=$(( (netmask << 1) ^ 1 ))
45 done
46
47 to_address "$(( netmask ^ 0xffffffff ))"
48}
49
c7141f04
MT
50find_interface() {
51 local mac="${1}"
52
53 local path
54 for path in /sys/class/net/*; do
55 local address="$(<${path}/address)"
56
57 if [ "${mac}" = "${address}" ]; then
58 basename "${path}"
59 return 0
60 fi
61 done
62
63 return 1
64}
65
bd3bcb45
MT
66import_aws_configuration() {
67 local instance_id="$(get instance-id)"
68
69 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
70
71 # Initialise system settings
3273ff48 72 local hostname=$(get local-hostname)
bd3bcb45 73
3273ff48
MT
74 # Set hostname
75 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
76 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
77 fi
78
79 # Set domainname
80 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
81 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
bd3bcb45
MT
82 fi
83
2e42a9ea
MT
84 # Import SSH keys
85 local line
86 for line in $(get "public-keys/"); do
87 local key_no="${line%=*}"
88
89 local key="$(get public-keys/${key_no}/openssh-key)"
90 if [ -n "${key}" ] && ! grep -q "^${key}$" /root/.ssh/authorized_keys 2>/dev/null; then
91 mkdir -p /root/.ssh
92 chmod 700 /root/.ssh
93
94 echo "${key}" >> /root/.ssh/authorized_keys
95 chmod 600 /root/.ssh/authorized_keys
96 fi
97 done
98
bd3bcb45
MT
99 # Import any DNS server settings
100 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
101
102 # Import network configuration
2e42a9ea
MT
103 # After this, no network connectivity will be available from this script due to the
104 # renaming of the network interfaces for which they have to be shut down
0f224ad7
MT
105 local config_type=1
106 : > /var/ipfire/ethernet/settings
bd3bcb45 107
3273ff48 108 local mac
bd3bcb45
MT
109 for mac in $(get network/interfaces/macs/); do
110 # Remove trailing slash
111 mac="${mac//\//}"
112
113 local device_number="$(get "network/interfaces/macs/${mac}/device-number")"
114 local interface_id="$(get "network/interfaces/macs/${mac}/interface-id")"
115
116 # First IPv4 address
117 local ipv4_address="$(get "network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
118 local ipv4_address_num="$(to_integer "${ipv4_address}")"
119
607240e2
MT
120 # Get VPC subnet
121 local vpc="$(get "network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
122 local vpc_netaddress="${vpc%/*}"
123 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
124
bd3bcb45
MT
125 # Get subnet size
126 local subnet="$(get "network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
127
128 local prefix="${subnet#*/}"
129 local netmask="$(prefix2netmask "${prefix}")"
130 local netmask_num="$(to_integer "${netmask}")"
131
132 # Calculate the network and broadcast addresses
133 local netaddress="${subnet%/*}"
134 local netaddress_num="$(to_integer "${netaddress}")"
135 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
136
bd3bcb45
MT
137 case "${device_number}" in
138 # RED
139 0)
c7141f04
MT
140 local interface_name="red0"
141
c86fd963
MT
142 # The gateway is always the first IP address in the subnet
143 local gateway="$(to_address $(( netaddress_num + 1 )))"
144
145 # The AWS internal DNS service is available on the second IP address of the VPC
146 local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
147 local dns2=
148
bd3bcb45
MT
149 (
150 echo "RED_TYPE=STATIC"
c7141f04 151 echo "RED_DEV=${interface_name}"
bd3bcb45
MT
152 echo "RED_MACADDR=${mac}"
153 echo "RED_DESCRIPTION='${interface_id}'"
154 echo "RED_ADDRESS=${ipv4_address}"
155 echo "RED_NETMASK=${netmask}"
156 echo "RED_NETADDRESS=${netaddress}"
157 echo "RED_BROADCAST=${broadcast}"
158 echo "DEFAULT_GATEWAY=${gateway}"
159 echo "DNS1=${DNS1:-${dns1}}"
160 echo "DNS2=${DNS2:-${dns2}}"
161 ) >> /var/ipfire/ethernet/settings
162
163 # Import aliases for RED
164 for alias in $(get "network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
165 echo "${alias},on,"
166 done > /var/ipfire/ethernet/aliases
167 ;;
168
169 # GREEN
170 1)
c7141f04
MT
171 local interface_name="green0"
172
bd3bcb45 173 (
c7141f04 174 echo "GREEN_DEV=${interface_name}"
bd3bcb45
MT
175 echo "GREEN_MACADDR=${mac}"
176 echo "GREEN_DESCRIPTION='${interface_id}'"
177 echo "GREEN_ADDRESS=${ipv4_address}"
178 echo "GREEN_NETMASK=${netmask}"
179 echo "GREEN_NETADDRESS=${netaddress}"
180 echo "GREEN_BROADCAST=${broadcast}"
181 ) >> /var/ipfire/ethernet/settings
182 ;;
0f224ad7
MT
183
184 # ORANGE
185 2)
c7141f04 186 local interface_name="orange0"
0f224ad7
MT
187 config_type=2
188
189 (
c7141f04 190 echo "ORANGE_DEV=${interface_name}"
0f224ad7
MT
191 echo "ORANGE_MACADDR=${mac}"
192 echo "ORANGE_DESCRIPTION='${interface_id}'"
193 echo "ORANGE_ADDRESS=${ipv4_address}"
194 echo "ORANGE_NETMASK=${netmask}"
195 echo "ORANGE_NETADDRESS=${netaddress}"
196 echo "ORANGE_BROADCAST=${broadcast}"
197 ) >> /var/ipfire/ethernet/settings
198 ;;
bd3bcb45 199 esac
c7141f04
MT
200
201 # Rename interface
202 local interface="$(find_interface "${mac}")"
203
470e85c3 204 if [ -n "${interface}" ] && [ -n "${interface_name}" ] && [ "${interface}" != "${interface_name}" ]; then
c7141f04
MT
205 ip link set "${interface}" down
206 ip link set "${interface}" name "${interface_name}"
207 fi
bd3bcb45
MT
208 done
209
0f224ad7
MT
210 # Save CONFIG_TYPE
211 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
212
bd3bcb45
MT
213 # Actions performed only on the very first start
214 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
7fa83c2f 215 # Enable SSH
8b59ef08
MT
216 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
217
7fa83c2f
MT
218 touch /var/ipfire/remote/enablessh
219 chown nobody:nobody /var/ipfire/remote/enablessh
220
221 # Enable SSH key authentication
222 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
223
8f2c3b49
MT
224 # Apply SSH settings
225 /usr/local/bin/sshctrl
226
bd3bcb45
MT
227 # Firewall rules for SSH and WEBIF
228 (
229 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
230 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
231 ) >> /var/ipfire/firewall/input
232
233 # This script has now completed the first steps of setup
234 touch /var/ipfire/main/firstsetup_ok
235 fi
236
237 # All done
238 echo_ok
239}
240
241case "${reason}" in
242 PREINIT)
243 # Bring up the interface
244 ip link set "${interface}" up
245 ;;
246
247 BOUND|RENEW|REBIND|REBOOT)
248 # Remove any previous IP addresses
249 ip addr flush dev "${interface}"
250
251 # Add (or re-add) the new IP address
252 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
253
254 # Add the default route
255 ip route add default via "${new_routers}"
256
257 # Import AWS configuration
258 import_aws_configuration
259 ;;
260
261 EXPIRE|FAIL|RELEASE|STOP)
262 # Remove all IP addresses
263 ip addr flush dev "${interface}"
264 ;;
265
266 *)
267 echo "Unhandled reason: ${reason}" >&2
268 exit 2
269 ;;
270esac
271
272# Terminate
273exit 0