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