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