]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/helper/aws-setup
6 # Set PATH to find our own executables
7 export PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:${PATH}
12 wget
-qO - "http://169.254.169.254/latest/${file}"
18 local o1
=$
(( (n
& 0xff000000) >> 24 ))
19 local o2
=$
(( (n
& 0xff0000) >> 16 ))
20 local o3
=$
(( (n
& 0xff00) >> 8 ))
21 local o4
=$
(( (n
& 0xff) ))
23 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
32 for i
in ${address//\./ }; do
33 integer
=$
(( (integer
<< 8) + i ))
36 printf "%d\n" "${integer}"
42 local zeros=$(( 32 - prefix ))
46 for (( i=0; i<${zeros}; i++ )); do
47 netmask=$(( (netmask << 1) ^ 1 ))
50 to_address "$(( netmask ^ 0xffffffff ))"
53 import_aws_configuration() {
54 local instance_id="$(get meta-data/instance-id)"
56 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
59 echo "${instance_id}" > /var/run/aws-instance-id
61 # Initialise system settings
62 local hostname=$(get meta-data/local-hostname)
65 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
66 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
70 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
71 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
75 if ! getent passwd setup &>/dev/null; then
76 useradd setup -s /usr/bin/run-setup -g nobody -m
82 # Import SSH keys for setup user
84 for line in $(get "meta-data/public-keys/"); do
85 local key_no="${line%=*}"
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"
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"
99 # Download the user-data script only on the first boot
100 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
102 local user_data="$(get user-data)"
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
109 # Run the user-data script
110 local now="$(date -u +"%s")"
111 /tmp/aws-user-data.script &>/var/log/user-data.log.${now}
113 # Delete the script right away
114 rm /tmp/aws-user-data.script
118 # Import any DNS server settings
119 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
121 # Import network configuration
122 # After this, no network connectivity will be available from this script due to the
123 # renaming of the network interfaces for which they have to be shut down
125 : > /var/ipfire/ethernet/settings
128 for mac in $(get meta-data/network/interfaces/macs/); do
129 # Remove trailing slash
132 local device_number="$(get "meta-data/network/interfaces/macs/${mac}/device-number")"
133 local interface_id="$(get "meta-data/network/interfaces/macs/${mac}/interface-id")"
136 local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
137 local ipv4_address_num="$(to_integer "${ipv4_address}")"
140 local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
141 local vpc_netaddress="${vpc%/*}"
142 local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
145 local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
147 local prefix="${subnet#*/}"
148 local netmask="$(prefix2netmask "${prefix}")"
149 local netmask_num="$(to_integer "${netmask}")"
151 # Calculate the network and broadcast addresses
152 local netaddress="${subnet%/*}"
153 local netaddress_num="$(to_integer "${netaddress}")"
154 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
156 case "${device_number}" in
159 local interface_name="red0"
161 # The gateway is always the first IP address in the subnet
162 local gateway="$(to_address $(( netaddress_num + 1 )))"
164 # The AWS internal DNS service is available on the second IP address of the VPC
165 local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
169 echo "RED_TYPE=STATIC"
170 echo "RED_DEV=${interface_name}"
171 echo "RED_MACADDR=${mac}"
172 echo "RED_DESCRIPTION='${interface_id}'"
173 echo "RED_ADDRESS=${ipv4_address}"
174 echo "RED_NETMASK=${netmask}"
175 echo "RED_NETADDRESS=${netaddress}"
176 echo "RED_BROADCAST=${broadcast}"
177 echo "DEFAULT_GATEWAY=${gateway}"
178 echo "DNS1=${DNS1:-${dns1}}"
179 echo "DNS2=${DNS2:-${dns2}}"
180 ) >> /var/ipfire/ethernet/settings
182 # Import aliases for RED
183 for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
185 done > /var/ipfire/ethernet/aliases
190 local interface_name="green0"
193 echo "GREEN_DEV=${interface_name}"
194 echo "GREEN_MACADDR=${mac}"
195 echo "GREEN_DESCRIPTION='${interface_id}'"
196 echo "GREEN_ADDRESS=${ipv4_address}"
197 echo "GREEN_NETMASK=${netmask}"
198 echo "GREEN_NETADDRESS=${netaddress}"
199 echo "GREEN_BROADCAST=${broadcast}"
200 ) >> /var/ipfire/ethernet/settings
205 local interface_name="orange0"
209 echo "ORANGE_DEV=${interface_name}"
210 echo "ORANGE_MACADDR=${mac}"
211 echo "ORANGE_DESCRIPTION='${interface_id}'"
212 echo "ORANGE_ADDRESS=${ipv4_address}"
213 echo "ORANGE_NETMASK=${netmask}"
214 echo "ORANGE_NETADDRESS=${netaddress}"
215 echo "ORANGE_BROADCAST=${broadcast}"
216 ) >> /var/ipfire/ethernet/settings
222 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
226 # Actions performed only on the very first start
227 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
229 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
231 # Disable SSH password authentication
232 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
234 # Enable SSH key authentication
235 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
238 /usr/local/bin/sshctrl
240 # Mark SSH to start immediately (but not right now)
241 touch /var/ipfire/remote/enablessh
242 chown nobody:nobody /var/ipfire/remote/enablessh
244 # Firewall rules for SSH and WEBIF
246 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
247 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
248 ) >> /var/ipfire/firewall/input
250 # This script has now completed the first steps of setup
251 touch /var/ipfire/main/firstsetup_ok
260 # Bring up the interface
261 ip link set "${interface}" up
264 BOUND|RENEW|REBIND|REBOOT)
265 # Remove any previous IP addresses
266 ip addr flush dev "${interface}"
268 # Add (or re-add) the new IP address
269 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
271 # Add the default route
272 ip route add default via "${new_routers}"
275 for domain_name_server in ${new_domain_name_servers}; do
276 echo "nameserver ${domain_name_server}"
277 done > /etc/resolv.conf
279 # The system is online now
280 touch /var/ipfire/red/active
282 # Import AWS configuration
283 import_aws_configuration
286 EXPIRE|FAIL|RELEASE|STOP)
287 # The system is no longer online
288 rm -f /var/ipfire/red/active
290 # Remove all IP addresses
291 ip addr flush dev "${interface}"
293 # Shut down the interface
294 ip link set "${interface}" down
298 echo "Unhandled reason: ${reason}" >&2