]>
git.ipfire.org Git - people/mfischer/ipfire-2.x.git/blob - src/initscripts/helper/aws-setup
9 wget
-qO - "http://169.254.169.254/latest/meta-data/${file}"
15 local o1
=$
(( (n
& 0xff000000) >> 24 ))
16 local o2
=$
(( (n
& 0xff0000) >> 16 ))
17 local o3
=$
(( (n
& 0xff00) >> 8 ))
18 local o4
=$
(( (n
& 0xff) ))
20 printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}"
29 for i
in ${address//\./ }; do
30 integer
=$
(( (integer
<< 8) + i ))
33 printf "%d\n" "${integer}"
39 local zeros=$(( 32 - prefix ))
43 for (( i=0; i<${zeros}; i++ )); do
44 netmask=$(( (netmask << 1) ^ 1 ))
47 to_address "$(( netmask ^ 0xffffffff ))"
50 import_aws_configuration() {
51 local instance_id="$(get instance-id)"
53 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
55 # Initialise system settings
56 local hostname=$(get local-hostname)
59 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
60 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
64 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
65 echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
68 # Import any DNS server settings
69 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
71 # Import network configuration
75 ) > /var/ipfire/ethernet/settings
78 for mac in $(get network/interfaces/macs/); do
79 # Remove trailing slash
82 local device_number="$(get "network/interfaces/macs/${mac}/device-number")"
83 local interface_id="$(get "network/interfaces/macs/${mac}/interface-id")"
86 local ipv4_address="$(get "network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
87 local ipv4_address_num="$(to_integer "${ipv4_address}")"
90 local subnet="$(get "network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
92 local prefix="${subnet#*/}"
93 local netmask="$(prefix2netmask "${prefix}")"
94 local netmask_num="$(to_integer "${netmask}")"
96 # Calculate the network and broadcast addresses
97 local netaddress="${subnet%/*}"
98 local netaddress_num="$(to_integer "${netaddress}")"
99 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
101 # The gateway is always the first IP address in the subnet
102 local gateway="$(to_address $(( netaddress_num + 1 )))"
104 # The AWS internal DNS service is available on the fourth IP address of each subnet
105 local dns1="$(to_address $(( netaddress_num + 4 )))"
108 case "${device_number}" in
112 echo "RED_TYPE=STATIC"
114 echo "RED_MACADDR=${mac}"
115 echo "RED_DESCRIPTION='${interface_id}'"
116 echo "RED_ADDRESS=${ipv4_address}"
117 echo "RED_NETMASK=${netmask}"
118 echo "RED_NETADDRESS=${netaddress}"
119 echo "RED_BROADCAST=${broadcast}"
120 echo "DEFAULT_GATEWAY=${gateway}"
121 echo "DNS1=${DNS1:-${dns1}}"
122 echo "DNS2=${DNS2:-${dns2}}"
123 ) >> /var/ipfire/ethernet/settings
125 # Import aliases for RED
126 for alias in $(get "network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
128 done > /var/ipfire/ethernet/aliases
134 echo "GREEN_DEV=green0"
135 echo "GREEN_MACADDR=${mac}"
136 echo "GREEN_DESCRIPTION='${interface_id}'"
137 echo "GREEN_ADDRESS=${ipv4_address}"
138 echo "GREEN_NETMASK=${netmask}"
139 echo "GREEN_NETADDRESS=${netaddress}"
140 echo "GREEN_BROADCAST=${broadcast}"
141 ) >> /var/ipfire/ethernet/settings
147 cat /var/ipfire/ethernet/settings
151 for line in $(get "public-keys/"); do
152 local key_no="${line%=*}"
154 local key="$(get public-keys/${key_no}/openssh-key)"
155 if [ -n "${key}" ] && ! grep -q "^${key}$" /root/.ssh/authorized_keys 2>/dev/null; then
159 echo "${key}" >> /root/.ssh/authorized_keys
160 chmod 600 /root/.ssh/authorized_keys
164 # Actions performed only on the very first start
165 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
167 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
169 touch /var/ipfire/remote/enablessh
170 chown nobody:nobody /var/ipfire/remote/enablessh
172 # Enable SSH key authentication
173 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
175 # Firewall rules for SSH and WEBIF
177 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
178 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
179 ) >> /var/ipfire/firewall/input
181 # This script has now completed the first steps of setup
182 touch /var/ipfire/main/firstsetup_ok
191 # Bring up the interface
192 ip link set "${interface}" up
195 BOUND|RENEW|REBIND|REBOOT)
196 # Remove any previous IP addresses
197 ip addr flush dev "${interface}"
199 # Add (or re-add) the new IP address
200 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
202 # Add the default route
203 ip route add default via "${new_routers}"
205 # Import AWS configuration
206 import_aws_configuration
209 EXPIRE|FAIL|RELEASE|STOP)
210 # Remove all IP addresses
211 ip addr flush dev "${interface}"
215 echo "Unhandled reason: ${reason}" >&2