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