]> git.ipfire.org Git - ipfire-2.x.git/blob - src/initscripts/helper/aws-setup
8097ad9ef7327b5895d4100c809f1072e5fcda9f
[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 import_aws_configuration() {
51 local instance_id="$(get instance-id)"
52
53 boot_mesg "Importing AWS configuration for instance ${instance_id}..."
54
55 # Initialise system settings
56 if [ ! -s "/var/ipfire/main/settings" ]; then
57 local hostname=$(get local-hostname)
58
59 (
60 echo "HOSTNAME=${hostname%%.*}"
61 echo "DOMAINNAME=${hostname#*.}"
62 ) > /var/ipfire/main/settings
63 fi
64
65 # Import any DNS server settings
66 eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
67
68 # Import network configuration
69 (
70 # RED + GREEN
71 echo "CONFIG_TYPE=1"
72 ) > /var/ipfire/ethernet/settings
73
74 for mac in $(get network/interfaces/macs/); do
75 # Remove trailing slash
76 mac="${mac//\//}"
77
78 local device_number="$(get "network/interfaces/macs/${mac}/device-number")"
79 local interface_id="$(get "network/interfaces/macs/${mac}/interface-id")"
80
81 # First IPv4 address
82 local ipv4_address="$(get "network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
83 local ipv4_address_num="$(to_integer "${ipv4_address}")"
84
85 # Get subnet size
86 local subnet="$(get "network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
87
88 local prefix="${subnet#*/}"
89 local netmask="$(prefix2netmask "${prefix}")"
90 local netmask_num="$(to_integer "${netmask}")"
91
92 # Calculate the network and broadcast addresses
93 local netaddress="${subnet%/*}"
94 local netaddress_num="$(to_integer "${netaddress}")"
95 local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))"
96
97 # The gateway is always the first IP address in the subnet
98 local gateway="$(to_address $(( netaddress_num + 1 )))"
99
100 # The AWS internal DNS service is available on the fourth IP address of each subnet
101 local dns1="$(to_address $(( netaddress_num + 4 )))"
102 local dns2=
103
104 case "${device_number}" in
105 # RED
106 0)
107 (
108 echo "RED_TYPE=STATIC"
109 echo "RED_DEV=red0"
110 echo "RED_MACADDR=${mac}"
111 echo "RED_DESCRIPTION='${interface_id}'"
112 echo "RED_ADDRESS=${ipv4_address}"
113 echo "RED_NETMASK=${netmask}"
114 echo "RED_NETADDRESS=${netaddress}"
115 echo "RED_BROADCAST=${broadcast}"
116 echo "DEFAULT_GATEWAY=${gateway}"
117 echo "DNS1=${DNS1:-${dns1}}"
118 echo "DNS2=${DNS2:-${dns2}}"
119 ) >> /var/ipfire/ethernet/settings
120
121 # Import aliases for RED
122 for alias in $(get "network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
123 echo "${alias},on,"
124 done > /var/ipfire/ethernet/aliases
125 ;;
126
127 # GREEN
128 1)
129 (
130 echo "GREEN_DEV=green0"
131 echo "GREEN_MACADDR=${mac}"
132 echo "GREEN_DESCRIPTION='${interface_id}'"
133 echo "GREEN_ADDRESS=${ipv4_address}"
134 echo "GREEN_NETMASK=${netmask}"
135 echo "GREEN_NETADDRESS=${netaddress}"
136 echo "GREEN_BROADCAST=${broadcast}"
137 ) >> /var/ipfire/ethernet/settings
138 ;;
139 esac
140 done
141
142 # DEBUG
143 cat /var/ipfire/ethernet/settings
144
145 # Import SSH keys
146 local line
147 for line in $(get "public-keys/"); do
148 local key_no="${line%=*}"
149
150 local key="$(get public-keys/${key_no}/openssh-key)"
151 if [ -n "${key}" ] && ! grep -q "^${key}$" /root/.ssh/authorized_keys 2>/dev/null; then
152 mkdir -p /root/.ssh
153 chmod 700 /root/.ssh
154
155 echo "${key}" >> /root/.ssh/authorized_keys
156 chmod 600 /root/.ssh/authorized_keys
157 fi
158 done
159
160 # Actions performed only on the very first start
161 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
162 # Enable SSH
163 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
164
165 touch /var/ipfire/remote/enablessh
166 chown nobody:nobody /var/ipfire/remote/enablessh
167
168 # Enable SSH key authentication
169 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
170
171 # Firewall rules for SSH and WEBIF
172 (
173 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
174 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
175 ) >> /var/ipfire/firewall/input
176
177 # This script has now completed the first steps of setup
178 touch /var/ipfire/main/firstsetup_ok
179 fi
180
181 # All done
182 echo_ok
183 }
184
185 case "${reason}" in
186 PREINIT)
187 # Bring up the interface
188 ip link set "${interface}" up
189 ;;
190
191 BOUND|RENEW|REBIND|REBOOT)
192 # Remove any previous IP addresses
193 ip addr flush dev "${interface}"
194
195 # Add (or re-add) the new IP address
196 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
197
198 # Add the default route
199 ip route add default via "${new_routers}"
200
201 # Import AWS configuration
202 import_aws_configuration
203 ;;
204
205 EXPIRE|FAIL|RELEASE|STOP)
206 # Remove all IP addresses
207 ip addr flush dev "${interface}"
208 ;;
209
210 *)
211 echo "Unhandled reason: ${reason}" >&2
212 exit 2
213 ;;
214 esac
215
216 # Terminate
217 exit 0