]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/helper/aws-setup
9b67019d625823f5c3c4f943b857bd7641ca24cd
[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 # AWS supports an MTU of up to 9001 bytes
10 DEFAULT_MTU=9001
11
12 get() {
13 local file="${1}"
14
15 wget -qO - "http://169.254.169.254/latest/${file}"
16 }
17
18 to_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
29 to_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
42 prefix2netmask() {
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
56 import_aws_configuration() {
57 local instance_id="$(get meta-data/instance-id)"
58
59 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
60
61 # Store instance ID
62 echo "${instance_id}" > /var/run/aws-instance-id
63
64 # Initialise system settings
65 local hostname=$(get meta-data/local-hostname)
66
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
75 fi
76
77 # Create setup user
78 if ! getent passwd setup &>/dev/null; then
79 useradd setup -s /usr/bin/run-setup -g nobody -m
80
81 # Unlock the account
82 usermod -p "x" setup
83 fi
84
85 # Import SSH keys for setup user
86 local line
87 for line in $(get "meta-data/public-keys/"); do
88 local key_no="${line%=*}"
89
90 local key="$(get meta-data/public-keys/${key_no}/openssh-key)"
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"
94 chown setup.nobody "/home/setup/.ssh"
95
96 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
97 chmod 600 "/home/setup/.ssh/authorized_keys"
98 chown setup.nobody "/home/setup/.ssh/authorized_keys"
99 fi
100 done
101
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
113 local now="$(date -u +"%s")"
114 /tmp/aws-user-data.script &>/var/log/user-data.log.${now}
115
116 # Delete the script right away
117 rm /tmp/aws-user-data.script
118 fi
119 fi
120
121 # Import network configuration
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
124 local config_type=1
125 : > /var/ipfire/ethernet/settings
126
127 local mac
128 for mac in $(get meta-data/network/interfaces/macs/); do
129 # Remove trailing slash
130 mac="${mac//\//}"
131
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")"
134
135 # First IPv4 address
136 local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
137 local ipv4_address_num="$(to_integer "${ipv4_address}")"
138
139 # Get VPC subnet
140 local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
141 local vpc_netaddress="${vpc%/*}"
142 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
143
144 # Get subnet size
145 local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
146
147 local prefix="${subnet#*/}"
148 local netmask="$(prefix2netmask "${prefix}")"
149 local netmask_num="$(to_integer "${netmask}")"
150
151 # Calculate the network and broadcast addresses
152 local netaddress="${subnet%/*}"
153 local netaddress_num="$(to_integer "${netaddress}")"
154 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
155
156 case "${device_number}" in
157 # RED
158 0)
159 local interface_name="red0"
160
161 # The gateway is always the first IP address in the subnet
162 local gateway="$(to_address $(( netaddress_num + 1 )))"
163
164 (
165 echo "RED_TYPE=STATIC"
166 echo "RED_DEV=${interface_name}"
167 echo "RED_MACADDR=${mac}"
168 echo "RED_DESCRIPTION='${interface_id}'"
169 echo "RED_ADDRESS=${ipv4_address}"
170 echo "RED_NETMASK=${netmask}"
171 echo "RED_NETADDRESS=${netaddress}"
172 echo "RED_BROADCAST=${broadcast}"
173 echo "RED_MTU=1500"
174 echo "DEFAULT_GATEWAY=${gateway}"
175 ) >> /var/ipfire/ethernet/settings
176
177 # Import aliases for RED
178 for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
179 echo "${alias},on,"
180 done > /var/ipfire/ethernet/aliases
181 ;;
182
183 # GREEN
184 1)
185 local interface_name="green0"
186
187 (
188 echo "GREEN_DEV=${interface_name}"
189 echo "GREEN_MACADDR=${mac}"
190 echo "GREEN_DESCRIPTION='${interface_id}'"
191 echo "GREEN_ADDRESS=${ipv4_address}"
192 echo "GREEN_NETMASK=${netmask}"
193 echo "GREEN_NETADDRESS=${netaddress}"
194 echo "GREEN_BROADCAST=${broadcast}"
195 echo "GREEN_MTU=${DEFAULT_MTU}"
196 ) >> /var/ipfire/ethernet/settings
197 ;;
198
199 # ORANGE
200 2)
201 local interface_name="orange0"
202 config_type=2
203
204 (
205 echo "ORANGE_DEV=${interface_name}"
206 echo "ORANGE_MACADDR=${mac}"
207 echo "ORANGE_DESCRIPTION='${interface_id}'"
208 echo "ORANGE_ADDRESS=${ipv4_address}"
209 echo "ORANGE_NETMASK=${netmask}"
210 echo "ORANGE_NETADDRESS=${netaddress}"
211 echo "ORANGE_BROADCAST=${broadcast}"
212 echo "ORANGE_MTU=${DEFAULT_MTU}"
213 ) >> /var/ipfire/ethernet/settings
214 ;;
215 esac
216 done
217
218 # Save CONFIG_TYPE
219 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
220
221 # Actions performed only on the very first start
222 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
223 # Disable using ISP nameservers
224 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
225
226 # Enable SSH
227 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
228
229 # Disable SSH password authentication
230 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
231
232 # Enable SSH key authentication
233 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
234
235 # Apply SSH settings
236 /usr/local/bin/sshctrl
237
238 # Mark SSH to start immediately (but not right now)
239 touch /var/ipfire/remote/enablessh
240 chown nobody:nobody /var/ipfire/remote/enablessh
241
242 # Firewall rules for SSH and WEBIF
243 (
244 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
245 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
246 ) >> /var/ipfire/firewall/input
247
248 # This script has now completed the first steps of setup
249 touch /var/ipfire/main/firstsetup_ok
250 fi
251
252 # All done
253 echo_ok
254 }
255
256 case "${reason}" in
257 PREINIT)
258 # Bring up the interface
259 ip link set "${interface}" up
260 ;;
261
262 BOUND|RENEW|REBIND|REBOOT)
263 # Remove any previous IP addresses
264 ip addr flush dev "${interface}"
265
266 # Add (or re-add) the new IP address
267 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
268
269 # Add the default route
270 ip route add default via "${new_routers}"
271
272 # Setup DNS
273 for domain_name_server in ${new_domain_name_servers}; do
274 echo "nameserver ${domain_name_server}"
275 done > /etc/resolv.conf
276
277 # The system is online now
278 touch /var/ipfire/red/active
279
280 # Import AWS configuration
281 import_aws_configuration
282 ;;
283
284 EXPIRE|FAIL|RELEASE|STOP)
285 # The system is no longer online
286 rm -f /var/ipfire/red/active
287
288 # Remove all IP addresses
289 ip addr flush dev "${interface}"
290
291 # Shut down the interface
292 ip link set "${interface}" down
293 ;;
294
295 *)
296 echo "Unhandled reason: ${reason}" >&2
297 exit 2
298 ;;
299 esac
300
301 # Terminate
302 exit 0