]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/helper/aws-setup
d79d1c610b2d670e90bcc9076acf4889dd6b1f7e
[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 # Download user-data
102 local user_data="$(get user-data)"
103
104 # Save user-data script to be executed later
105 if [ "${user_data:0:2}" = "#!" ]; then
106 echo "${user_data}" > /tmp/aws-user-data.script
107 chmod 700 /tmp/aws-user-data.script
108
109 # Run the user-data script
110 /tmp/aws-user-data.script &>/var/log/user-data.log
111
112 # Delete the script right away
113 rm /tmp/aws-user-data.script
114 fi
115 fi
116
117 # Import any DNS server settings
118 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
119
120 # Import network configuration
121 # After this, no network connectivity will be available from this script due to the
122 # renaming of the network interfaces for which they have to be shut down
123 local config_type=1
124 : > /var/ipfire/ethernet/settings
125
126 local mac
127 for mac in $(get meta-data/network/interfaces/macs/); do
128 # Remove trailing slash
129 mac="${mac//\//}"
130
131 local device_number="$(get "meta-data/network/interfaces/macs/${mac}/device-number")"
132 local interface_id="$(get "meta-data/network/interfaces/macs/${mac}/interface-id")"
133
134 # First IPv4 address
135 local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
136 local ipv4_address_num="$(to_integer "${ipv4_address}")"
137
138 # Get VPC subnet
139 local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
140 local vpc_netaddress="${vpc%/*}"
141 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
142
143 # Get subnet size
144 local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
145
146 local prefix="${subnet#*/}"
147 local netmask="$(prefix2netmask "${prefix}")"
148 local netmask_num="$(to_integer "${netmask}")"
149
150 # Calculate the network and broadcast addresses
151 local netaddress="${subnet%/*}"
152 local netaddress_num="$(to_integer "${netaddress}")"
153 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
154
155 case "${device_number}" in
156 # RED
157 0)
158 local interface_name="red0"
159
160 # The gateway is always the first IP address in the subnet
161 local gateway="$(to_address $(( netaddress_num + 1 )))"
162
163 # The AWS internal DNS service is available on the second IP address of the VPC
164 local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
165 local dns2=
166
167 (
168 echo "RED_TYPE=STATIC"
169 echo "RED_DEV=${interface_name}"
170 echo "RED_MACADDR=${mac}"
171 echo "RED_DESCRIPTION='${interface_id}'"
172 echo "RED_ADDRESS=${ipv4_address}"
173 echo "RED_NETMASK=${netmask}"
174 echo "RED_NETADDRESS=${netaddress}"
175 echo "RED_BROADCAST=${broadcast}"
176 echo "DEFAULT_GATEWAY=${gateway}"
177 echo "DNS1=${DNS1:-${dns1}}"
178 echo "DNS2=${DNS2:-${dns2}}"
179 ) >> /var/ipfire/ethernet/settings
180
181 # Import aliases for RED
182 for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
183 echo "${alias},on,"
184 done > /var/ipfire/ethernet/aliases
185 ;;
186
187 # GREEN
188 1)
189 local interface_name="green0"
190
191 (
192 echo "GREEN_DEV=${interface_name}"
193 echo "GREEN_MACADDR=${mac}"
194 echo "GREEN_DESCRIPTION='${interface_id}'"
195 echo "GREEN_ADDRESS=${ipv4_address}"
196 echo "GREEN_NETMASK=${netmask}"
197 echo "GREEN_NETADDRESS=${netaddress}"
198 echo "GREEN_BROADCAST=${broadcast}"
199 ) >> /var/ipfire/ethernet/settings
200 ;;
201
202 # ORANGE
203 2)
204 local interface_name="orange0"
205 config_type=2
206
207 (
208 echo "ORANGE_DEV=${interface_name}"
209 echo "ORANGE_MACADDR=${mac}"
210 echo "ORANGE_DESCRIPTION='${interface_id}'"
211 echo "ORANGE_ADDRESS=${ipv4_address}"
212 echo "ORANGE_NETMASK=${netmask}"
213 echo "ORANGE_NETADDRESS=${netaddress}"
214 echo "ORANGE_BROADCAST=${broadcast}"
215 ) >> /var/ipfire/ethernet/settings
216 ;;
217 esac
218 done
219
220 # Save CONFIG_TYPE
221 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
222
223
224
225 # Actions performed only on the very first start
226 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
227 # Enable SSH
228 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
229
230 # Disable SSH password authentication
231 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
232
233 # Enable SSH key authentication
234 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
235
236 # Apply SSH settings
237 /usr/local/bin/sshctrl
238
239 # Mark SSH to start immediately (but not right now)
240 touch /var/ipfire/remote/enablessh
241 chown nobody:nobody /var/ipfire/remote/enablessh
242
243 # Firewall rules for SSH and WEBIF
244 (
245 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
246 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
247 ) >> /var/ipfire/firewall/input
248
249 # This script has now completed the first steps of setup
250 touch /var/ipfire/main/firstsetup_ok
251 fi
252
253 # All done
254 echo_ok
255 }
256
257 case "${reason}" in
258 PREINIT)
259 # Bring up the interface
260 ip link set "${interface}" up
261 ;;
262
263 BOUND|RENEW|REBIND|REBOOT)
264 # Remove any previous IP addresses
265 ip addr flush dev "${interface}"
266
267 # Add (or re-add) the new IP address
268 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
269
270 # Add the default route
271 ip route add default via "${new_routers}"
272
273 # Setup DNS
274 for domain_name_server in ${new_domain_name_servers}; do
275 echo "nameserver ${domain_name_server}"
276 done > /etc/resolv.conf
277
278 # The system is online now
279 touch /var/ipfire/red/active
280
281 # Import AWS configuration
282 import_aws_configuration
283 ;;
284
285 EXPIRE|FAIL|RELEASE|STOP)
286 # The system is no longer online
287 rm -f /var/ipfire/red/active
288
289 # Remove all IP addresses
290 ip addr flush dev "${interface}"
291
292 # Shut down the interface
293 ip link set "${interface}" down
294 ;;
295
296 *)
297 echo "Unhandled reason: ${reason}" >&2
298 exit 2
299 ;;
300 esac
301
302 # Terminate
303 exit 0