]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/initscripts/helper/aws-setup
Merge branch 'next' of ssh://git.ipfire.org/pub/git/ipfire-2.x into next
[people/pmueller/ipfire-2.x.git] / src / initscripts / helper / aws-setup
1 #!/bin/bash
2
3 . /etc/sysconfig/rc
4 . ${rc_functions}
5
6 get() {
7 local file="${1}"
8
9 wget -qO - "http://169.254.169.254/latest/meta-data/${file}"
10 }
11
12 to_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
23 to_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
36 prefix2netmask() {
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
50 find_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
66 import_aws_configuration() {
67 local instance_id="$(get instance-id)"
68
69 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
70
71 # Store instance ID
72 echo "${instance_id}" > /var/run/aws-instance-id
73
74 # Initialise system settings
75 local hostname=$(get local-hostname)
76
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
85 fi
86
87 # Import SSH keys
88 local line
89 for line in $(get "public-keys/"); do
90 local key_no="${line%=*}"
91
92 local key="$(get public-keys/${key_no}/openssh-key)"
93 if [ -n "${key}" ] && ! grep -q "^${key}$" /root/.ssh/authorized_keys 2>/dev/null; then
94 mkdir -p /root/.ssh
95 chmod 700 /root/.ssh
96
97 echo "${key}" >> /root/.ssh/authorized_keys
98 chmod 600 /root/.ssh/authorized_keys
99 fi
100 done
101
102 # Import any DNS server settings
103 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
104
105 # Import network configuration
106 # After this, no network connectivity will be available from this script due to the
107 # renaming of the network interfaces for which they have to be shut down
108 local config_type=1
109 : > /var/ipfire/ethernet/settings
110
111 local mac
112 for mac in $(get network/interfaces/macs/); do
113 # Remove trailing slash
114 mac="${mac//\//}"
115
116 local device_number="$(get "network/interfaces/macs/${mac}/device-number")"
117 local interface_id="$(get "network/interfaces/macs/${mac}/interface-id")"
118
119 # First IPv4 address
120 local ipv4_address="$(get "network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
121 local ipv4_address_num="$(to_integer "${ipv4_address}")"
122
123 # Get VPC subnet
124 local vpc="$(get "network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
125 local vpc_netaddress="${vpc%/*}"
126 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
127
128 # Get subnet size
129 local subnet="$(get "network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
130
131 local prefix="${subnet#*/}"
132 local netmask="$(prefix2netmask "${prefix}")"
133 local netmask_num="$(to_integer "${netmask}")"
134
135 # Calculate the network and broadcast addresses
136 local netaddress="${subnet%/*}"
137 local netaddress_num="$(to_integer "${netaddress}")"
138 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
139
140 case "${device_number}" in
141 # RED
142 0)
143 local interface_name="red0"
144
145 # The gateway is always the first IP address in the subnet
146 local gateway="$(to_address $(( netaddress_num + 1 )))"
147
148 # The AWS internal DNS service is available on the second IP address of the VPC
149 local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
150 local dns2=
151
152 (
153 echo "RED_TYPE=STATIC"
154 echo "RED_DEV=${interface_name}"
155 echo "RED_MACADDR=${mac}"
156 echo "RED_DESCRIPTION='${interface_id}'"
157 echo "RED_ADDRESS=${ipv4_address}"
158 echo "RED_NETMASK=${netmask}"
159 echo "RED_NETADDRESS=${netaddress}"
160 echo "RED_BROADCAST=${broadcast}"
161 echo "DEFAULT_GATEWAY=${gateway}"
162 echo "DNS1=${DNS1:-${dns1}}"
163 echo "DNS2=${DNS2:-${dns2}}"
164 ) >> /var/ipfire/ethernet/settings
165
166 # Import aliases for RED
167 for alias in $(get "network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
168 echo "${alias},on,"
169 done > /var/ipfire/ethernet/aliases
170 ;;
171
172 # GREEN
173 1)
174 local interface_name="green0"
175
176 (
177 echo "GREEN_DEV=${interface_name}"
178 echo "GREEN_MACADDR=${mac}"
179 echo "GREEN_DESCRIPTION='${interface_id}'"
180 echo "GREEN_ADDRESS=${ipv4_address}"
181 echo "GREEN_NETMASK=${netmask}"
182 echo "GREEN_NETADDRESS=${netaddress}"
183 echo "GREEN_BROADCAST=${broadcast}"
184 ) >> /var/ipfire/ethernet/settings
185 ;;
186
187 # ORANGE
188 2)
189 local interface_name="orange0"
190 config_type=2
191
192 (
193 echo "ORANGE_DEV=${interface_name}"
194 echo "ORANGE_MACADDR=${mac}"
195 echo "ORANGE_DESCRIPTION='${interface_id}'"
196 echo "ORANGE_ADDRESS=${ipv4_address}"
197 echo "ORANGE_NETMASK=${netmask}"
198 echo "ORANGE_NETADDRESS=${netaddress}"
199 echo "ORANGE_BROADCAST=${broadcast}"
200 ) >> /var/ipfire/ethernet/settings
201 ;;
202 esac
203
204 # Rename interface
205 local interface="$(find_interface "${mac}")"
206
207 if [ -n "${interface}" ] && [ -n "${interface_name}" ] && [ "${interface}" != "${interface_name}" ]; then
208 ip link set "${interface}" down
209 ip link set "${interface}" name "${interface_name}"
210 fi
211 done
212
213 # Save CONFIG_TYPE
214 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
215
216 # Actions performed only on the very first start
217 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
218 # Enable SSH
219 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
220
221 touch /var/ipfire/remote/enablessh
222 chown nobody:nobody /var/ipfire/remote/enablessh
223
224 # Enable SSH key authentication
225 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
226
227 # Apply SSH settings
228 /usr/local/bin/sshctrl
229
230 # Firewall rules for SSH and WEBIF
231 (
232 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
233 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
234 ) >> /var/ipfire/firewall/input
235
236 # This script has now completed the first steps of setup
237 touch /var/ipfire/main/firstsetup_ok
238 fi
239
240 # All done
241 echo_ok
242 }
243
244 case "${reason}" in
245 PREINIT)
246 # Bring up the interface
247 ip link set "${interface}" up
248 ;;
249
250 BOUND|RENEW|REBIND|REBOOT)
251 # Remove any previous IP addresses
252 ip addr flush dev "${interface}"
253
254 # Add (or re-add) the new IP address
255 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
256
257 # Add the default route
258 ip route add default via "${new_routers}"
259
260 # Import AWS configuration
261 import_aws_configuration
262 ;;
263
264 EXPIRE|FAIL|RELEASE|STOP)
265 # Remove all IP addresses
266 ip addr flush dev "${interface}"
267 ;;
268
269 *)
270 echo "Unhandled reason: ${reason}" >&2
271 exit 2
272 ;;
273 esac
274
275 # Terminate
276 exit 0