]>
Commit | Line | Data |
---|---|---|
bd3bcb45 MT |
1 | #!/bin/bash |
2 | ||
3 | . /etc/sysconfig/rc | |
4 | . ${rc_functions} | |
5 | ||
6 | get() { | |
7 | local file="${1}" | |
8 | ||
4e4c122c | 9 | wget -qO - "http://169.254.169.254/latest/${file}" |
bd3bcb45 MT |
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 | ||
c7141f04 MT |
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 | ||
bd3bcb45 | 66 | import_aws_configuration() { |
4e4c122c | 67 | local instance_id="$(get meta-data/instance-id)" |
bd3bcb45 MT |
68 | |
69 | boot_mesg "Importing AWS configuration for instance ${instance_id}..." | |
70 | ||
eb7ccf87 MT |
71 | # Store instance ID |
72 | echo "${instance_id}" > /var/run/aws-instance-id | |
73 | ||
bd3bcb45 | 74 | # Initialise system settings |
4e4c122c | 75 | local hostname=$(get meta-data/local-hostname) |
bd3bcb45 | 76 | |
3273ff48 MT |
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 | |
bd3bcb45 MT |
85 | fi |
86 | ||
1413006b MT |
87 | # Create setup user |
88 | if ! getent passwd setup &>/dev/null; then | |
e2d9fe7e | 89 | useradd setup -s /usr/bin/run-setup -g nobody -m |
87d7fc66 MT |
90 | |
91 | # Unlock the account | |
92 | usermod -p "x" setup | |
1413006b | 93 | fi |
2e42a9ea | 94 | |
53eb4918 MT |
95 | # Import SSH keys for setup user |
96 | local line | |
4e4c122c | 97 | for line in $(get "meta-data/public-keys/"); do |
53eb4918 MT |
98 | local key_no="${line%=*}" |
99 | ||
4e4c122c | 100 | local key="$(get meta-data/public-keys/${key_no}/openssh-key)" |
53eb4918 MT |
101 | if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then |
102 | mkdir -p "/home/setup/.ssh" | |
103 | chmod 700 "/home/setup/.ssh" | |
6a7e6b44 | 104 | chown setup.nobody "/home/setup/.ssh" |
53eb4918 MT |
105 | |
106 | echo "${key}" >> "/home/setup/.ssh/authorized_keys" | |
107 | chmod 600 "/home/setup/.ssh/authorized_keys" | |
6a7e6b44 | 108 | chown setup.nobody "/home/setup/.ssh/authorized_keys" |
53eb4918 | 109 | fi |
2e42a9ea MT |
110 | done |
111 | ||
8defa50e MT |
112 | # Download the user-data script only on the first boot |
113 | if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then | |
0ed9b770 MT |
114 | # Install all available updates |
115 | pakfire update && pakfire upgrade -y | |
116 | ||
8defa50e MT |
117 | # Download user-data |
118 | local user_data="$(get user-data)" | |
119 | ||
120 | # Save user-data script to be executed later | |
121 | if [ "${user_data:0:2}" = "#!" ]; then | |
122 | echo "${user_data}" > /tmp/aws-user-data.script | |
123 | chmod 700 /tmp/aws-user-data.script | |
124 | ||
125 | # Run the user-data script | |
126 | /tmp/aws-user-data.script | |
127 | ||
128 | # Delete the script right away | |
129 | rm /tmp/aws-user-data.script | |
130 | fi | |
131 | fi | |
4e4c122c | 132 | |
bd3bcb45 MT |
133 | # Import any DNS server settings |
134 | eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null)) | |
135 | ||
136 | # Import network configuration | |
2e42a9ea MT |
137 | # After this, no network connectivity will be available from this script due to the |
138 | # renaming of the network interfaces for which they have to be shut down | |
0f224ad7 MT |
139 | local config_type=1 |
140 | : > /var/ipfire/ethernet/settings | |
bd3bcb45 | 141 | |
3273ff48 | 142 | local mac |
4e4c122c | 143 | for mac in $(get meta-data/network/interfaces/macs/); do |
bd3bcb45 MT |
144 | # Remove trailing slash |
145 | mac="${mac//\//}" | |
146 | ||
4e4c122c MT |
147 | local device_number="$(get "meta-data/network/interfaces/macs/${mac}/device-number")" |
148 | local interface_id="$(get "meta-data/network/interfaces/macs/${mac}/interface-id")" | |
bd3bcb45 MT |
149 | |
150 | # First IPv4 address | |
4e4c122c | 151 | local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)" |
bd3bcb45 MT |
152 | local ipv4_address_num="$(to_integer "${ipv4_address}")" |
153 | ||
607240e2 | 154 | # Get VPC subnet |
4e4c122c | 155 | local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")" |
607240e2 MT |
156 | local vpc_netaddress="${vpc%/*}" |
157 | local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")" | |
158 | ||
bd3bcb45 | 159 | # Get subnet size |
4e4c122c | 160 | local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")" |
bd3bcb45 MT |
161 | |
162 | local prefix="${subnet#*/}" | |
163 | local netmask="$(prefix2netmask "${prefix}")" | |
164 | local netmask_num="$(to_integer "${netmask}")" | |
165 | ||
166 | # Calculate the network and broadcast addresses | |
167 | local netaddress="${subnet%/*}" | |
168 | local netaddress_num="$(to_integer "${netaddress}")" | |
169 | local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))" | |
170 | ||
bd3bcb45 MT |
171 | case "${device_number}" in |
172 | # RED | |
173 | 0) | |
c7141f04 MT |
174 | local interface_name="red0" |
175 | ||
c86fd963 MT |
176 | # The gateway is always the first IP address in the subnet |
177 | local gateway="$(to_address $(( netaddress_num + 1 )))" | |
178 | ||
179 | # The AWS internal DNS service is available on the second IP address of the VPC | |
180 | local dns1="$(to_address $(( vpc_netaddress_num + 2 )))" | |
181 | local dns2= | |
182 | ||
bd3bcb45 MT |
183 | ( |
184 | echo "RED_TYPE=STATIC" | |
c7141f04 | 185 | echo "RED_DEV=${interface_name}" |
bd3bcb45 MT |
186 | echo "RED_MACADDR=${mac}" |
187 | echo "RED_DESCRIPTION='${interface_id}'" | |
188 | echo "RED_ADDRESS=${ipv4_address}" | |
189 | echo "RED_NETMASK=${netmask}" | |
190 | echo "RED_NETADDRESS=${netaddress}" | |
191 | echo "RED_BROADCAST=${broadcast}" | |
192 | echo "DEFAULT_GATEWAY=${gateway}" | |
193 | echo "DNS1=${DNS1:-${dns1}}" | |
194 | echo "DNS2=${DNS2:-${dns2}}" | |
195 | ) >> /var/ipfire/ethernet/settings | |
196 | ||
197 | # Import aliases for RED | |
4e4c122c | 198 | for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do |
bd3bcb45 MT |
199 | echo "${alias},on," |
200 | done > /var/ipfire/ethernet/aliases | |
201 | ;; | |
202 | ||
203 | # GREEN | |
204 | 1) | |
c7141f04 MT |
205 | local interface_name="green0" |
206 | ||
bd3bcb45 | 207 | ( |
c7141f04 | 208 | echo "GREEN_DEV=${interface_name}" |
bd3bcb45 MT |
209 | echo "GREEN_MACADDR=${mac}" |
210 | echo "GREEN_DESCRIPTION='${interface_id}'" | |
211 | echo "GREEN_ADDRESS=${ipv4_address}" | |
212 | echo "GREEN_NETMASK=${netmask}" | |
213 | echo "GREEN_NETADDRESS=${netaddress}" | |
214 | echo "GREEN_BROADCAST=${broadcast}" | |
215 | ) >> /var/ipfire/ethernet/settings | |
216 | ;; | |
0f224ad7 MT |
217 | |
218 | # ORANGE | |
219 | 2) | |
c7141f04 | 220 | local interface_name="orange0" |
0f224ad7 MT |
221 | config_type=2 |
222 | ||
223 | ( | |
c7141f04 | 224 | echo "ORANGE_DEV=${interface_name}" |
0f224ad7 MT |
225 | echo "ORANGE_MACADDR=${mac}" |
226 | echo "ORANGE_DESCRIPTION='${interface_id}'" | |
227 | echo "ORANGE_ADDRESS=${ipv4_address}" | |
228 | echo "ORANGE_NETMASK=${netmask}" | |
229 | echo "ORANGE_NETADDRESS=${netaddress}" | |
230 | echo "ORANGE_BROADCAST=${broadcast}" | |
231 | ) >> /var/ipfire/ethernet/settings | |
232 | ;; | |
bd3bcb45 | 233 | esac |
c7141f04 MT |
234 | |
235 | # Rename interface | |
236 | local interface="$(find_interface "${mac}")" | |
237 | ||
470e85c3 | 238 | if [ -n "${interface}" ] && [ -n "${interface_name}" ] && [ "${interface}" != "${interface_name}" ]; then |
c7141f04 MT |
239 | ip link set "${interface}" down |
240 | ip link set "${interface}" name "${interface_name}" | |
241 | fi | |
bd3bcb45 MT |
242 | done |
243 | ||
0f224ad7 MT |
244 | # Save CONFIG_TYPE |
245 | echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings | |
246 | ||
bd3bcb45 MT |
247 | # Actions performed only on the very first start |
248 | if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then | |
7fa83c2f | 249 | # Enable SSH |
8b59ef08 MT |
250 | sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings |
251 | ||
0cf70cae MT |
252 | # Disable SSH password authentication |
253 | sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings | |
254 | ||
7fa83c2f MT |
255 | # Enable SSH key authentication |
256 | sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings | |
257 | ||
8f2c3b49 MT |
258 | # Apply SSH settings |
259 | /usr/local/bin/sshctrl | |
260 | ||
693208bf MT |
261 | # Mark SSH to start immediately (but not right now) |
262 | touch /var/ipfire/remote/enablessh | |
263 | chown nobody:nobody /var/ipfire/remote/enablessh | |
264 | ||
bd3bcb45 MT |
265 | # Firewall rules for SSH and WEBIF |
266 | ( | |
267 | echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second" | |
268 | echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second" | |
269 | ) >> /var/ipfire/firewall/input | |
270 | ||
271 | # This script has now completed the first steps of setup | |
272 | touch /var/ipfire/main/firstsetup_ok | |
273 | fi | |
274 | ||
275 | # All done | |
276 | echo_ok | |
277 | } | |
278 | ||
279 | case "${reason}" in | |
280 | PREINIT) | |
281 | # Bring up the interface | |
282 | ip link set "${interface}" up | |
283 | ;; | |
284 | ||
285 | BOUND|RENEW|REBIND|REBOOT) | |
286 | # Remove any previous IP addresses | |
287 | ip addr flush dev "${interface}" | |
288 | ||
289 | # Add (or re-add) the new IP address | |
290 | ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}" | |
291 | ||
292 | # Add the default route | |
293 | ip route add default via "${new_routers}" | |
294 | ||
647ca912 MT |
295 | # Setup DNS |
296 | for domain_name_server in ${new_domain_name_servers}; do | |
297 | echo "nameserver ${domain_name_server}" | |
298 | done > /etc/resolv.conf | |
299 | ||
300 | # The system is online now | |
301 | touch /var/ipfire/red/active | |
302 | ||
bd3bcb45 MT |
303 | # Import AWS configuration |
304 | import_aws_configuration | |
305 | ;; | |
306 | ||
307 | EXPIRE|FAIL|RELEASE|STOP) | |
647ca912 MT |
308 | # The system is no longer online |
309 | rm -f /var/ipfire/red/active | |
310 | ||
bd3bcb45 MT |
311 | # Remove all IP addresses |
312 | ip addr flush dev "${interface}" | |
313 | ;; | |
314 | ||
315 | *) | |
316 | echo "Unhandled reason: ${reason}" >&2 | |
317 | exit 2 | |
318 | ;; | |
319 | esac | |
320 | ||
321 | # Terminate | |
322 | exit 0 |