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