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