]>
Commit | Line | Data |
---|---|---|
89b10e70 MT |
1 | #!/bin/bash |
2 | ||
3 | . /etc/sysconfig/rc | |
4 | . ${rc_functions} | |
5 | ||
6 | # Set PATH to find our own executables | |
7 | export PATH=/usr/local/sbin:/usr/local/bin:${PATH} | |
8 | ||
bf1ae6aa MT |
9 | # GCP only supports an MTU of 1460 |
10 | DEFAULT_MTU=1460 | |
11 | ||
89b10e70 MT |
12 | get() { |
13 | local file="${1}" | |
14 | ||
15 | wget --header="Metadata-Flavor: Google" -qO - "http://169.254.169.254/computeMetadata/v1/${file}" | |
16 | } | |
17 | ||
18 | to_address() { | |
19 | local n="${1}" | |
20 | ||
21 | local o1=$(( (n & 0xff000000) >> 24 )) | |
22 | local o2=$(( (n & 0xff0000) >> 16 )) | |
23 | local o3=$(( (n & 0xff00) >> 8 )) | |
24 | local o4=$(( (n & 0xff) )) | |
25 | ||
26 | printf "%d.%d.%d.%d\n" "${o1}" "${o2}" "${o3}" "${o4}" | |
27 | } | |
28 | ||
29 | to_integer() { | |
30 | local address="${1}" | |
31 | ||
32 | local integer=0 | |
33 | ||
34 | local i | |
35 | for i in ${address//\./ }; do | |
36 | integer=$(( (integer << 8) + i )) | |
37 | done | |
38 | ||
39 | printf "%d\n" "${integer}" | |
40 | } | |
41 | ||
42 | prefix2netmask() { | |
43 | local prefix=${1} | |
44 | ||
45 | local zeros=$(( 32 - prefix )) | |
46 | local netmask=0 | |
47 | ||
48 | local i | |
49 | for (( i=0; i<${zeros}; i++ )); do | |
50 | netmask=$(( (netmask << 1) ^ 1 )) | |
51 | done | |
52 | ||
53 | to_address "$(( netmask ^ 0xffffffff ))" | |
54 | } | |
55 | ||
56 | import_gcp_configuration() { | |
57 | local instance_id="$(get instance/id)" | |
58 | ||
59 | boot_mesg "Importing Google Compute Platform configuration for instance ${instance_id}..." | |
60 | ||
61 | # Store instance ID | |
62 | echo "${instance_id}" > /var/run/gcp-instance-id | |
63 | ||
64 | # Initialise system settings | |
65 | local hostname=$(get instance/hostname) | |
66 | ||
67 | # Set hostname | |
68 | if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then | |
69 | echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings | |
70 | fi | |
71 | ||
72 | # Set domainname | |
73 | if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then | |
74 | echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings | |
75 | fi | |
76 | ||
77 | # Create setup user | |
78 | if ! getent passwd setup &>/dev/null; then | |
79 | useradd setup -s /usr/bin/run-setup -g nobody -m | |
80 | ||
81 | # Unlock the account | |
82 | usermod -p "x" setup | |
83 | fi | |
84 | ||
85 | # Import SSH keys for setup user | |
86 | local line | |
87 | while read -r line; do | |
88 | # Strip the username part from the key | |
89 | local key="${line#*:}" | |
90 | ||
91 | if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then | |
92 | mkdir -p "/home/setup/.ssh" | |
93 | chmod 700 "/home/setup/.ssh" | |
94 | chown setup.nobody "/home/setup/.ssh" | |
95 | ||
96 | echo "${key}" >> "/home/setup/.ssh/authorized_keys" | |
97 | chmod 600 "/home/setup/.ssh/authorized_keys" | |
98 | chown setup.nobody "/home/setup/.ssh/authorized_keys" | |
99 | fi | |
100 | done <<<"$(get instance/attributes/ssh-keys)" | |
101 | ||
102 | # Download the user-data script only on the first boot | |
103 | if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then | |
104 | # Download a startup script | |
105 | local script="$(get instance/attributes/startup-script)" | |
106 | ||
107 | # Execute the script | |
108 | if [ "${script:0:2}" = "#!" ]; then | |
109 | echo "${script}" > /tmp/gcp-startup.script | |
110 | chmod 700 /tmp/gcp-startup.script | |
111 | ||
112 | # Run the script | |
113 | local now="$(date -u +"%s")" | |
114 | /tmp/gcp-startup.script &>/var/log/startup-script.log.${now} | |
115 | ||
116 | # Delete the script right away | |
117 | rm /tmp/gcp-startup.script | |
118 | fi | |
119 | fi | |
120 | ||
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 | |
124 | local config_type=1 | |
125 | : > /var/ipfire/ethernet/settings | |
126 | ||
127 | local device_number | |
128 | for device_number in $(get instance/network-interfaces/); do | |
129 | # Remove trailing slash | |
130 | device_number="${device_number//\//}" | |
131 | ||
132 | local mac="$(get "instance/network-interfaces/${device_number}/mac")" | |
133 | ||
134 | # XXX TODO read the MTU because Google seems to only support 1460 | |
135 | ||
136 | # First IPv4 address | |
137 | local ipv4_address="$(get "instance/network-interfaces/${device_number}/ip")" | |
138 | local ipv4_address_num="$(to_integer "${ipv4_address}")" | |
139 | ||
140 | local netmask="$(get "instance/network-interfaces/${device_number}/subnetmask")" | |
141 | local netmask_num="$(to_integer "${netmask}")" | |
142 | ||
143 | # Calculate the network and broadcast addresses | |
144 | local netaddress="$(to_address $(( ipv4_address_num & netmask_num )))" | |
145 | local broadcast="$(to_address $(( ipv4_address_num | (0xffffffff ^ netmask_num) )))" | |
146 | ||
147 | case "${device_number}" in | |
148 | # RED | |
149 | 0) | |
150 | local interface_name="red0" | |
151 | local gateway="$(get instance/network-interfaces/${device_number}/gateway)" | |
152 | ||
153 | ( | |
154 | echo "RED_TYPE=STATIC" | |
155 | echo "RED_DEV=${interface_name}" | |
156 | echo "RED_MACADDR=${mac}" | |
157 | echo "RED_DESCRIPTION='${interface_id}'" | |
158 | echo "RED_ADDRESS=${ipv4_address}" | |
159 | echo "RED_NETMASK=${netmask}" | |
160 | echo "RED_NETADDRESS=${netaddress}" | |
161 | echo "RED_BROADCAST=${broadcast}" | |
bf1ae6aa | 162 | echo "RED_MTU=${DEFAULT_MTU}" |
89b10e70 MT |
163 | echo "DEFAULT_GATEWAY=${gateway}" |
164 | ) >> /var/ipfire/ethernet/settings | |
165 | ||
166 | # Import aliases for RED | |
167 | for alias in $(get "instance/network-interfaces/${device_number}/ip-aliases"); do | |
168 | echo "${alias},on," | |
169 | done > /var/ipfire/ethernet/aliases | |
170 | ;; | |
171 | ||
172 | # GREEN | |
173 | 1) | |
174 | local interface_name="green0" | |
175 | ||
176 | ( | |
177 | echo "GREEN_DEV=${interface_name}" | |
178 | echo "GREEN_MACADDR=${mac}" | |
179 | echo "GREEN_DESCRIPTION='${interface_id}'" | |
180 | echo "GREEN_ADDRESS=${ipv4_address}" | |
181 | echo "GREEN_NETMASK=${netmask}" | |
182 | echo "GREEN_NETADDRESS=${netaddress}" | |
183 | echo "GREEN_BROADCAST=${broadcast}" | |
bf1ae6aa | 184 | echo "GREEN_MTU=${DEFAULT_MTU}" |
89b10e70 MT |
185 | ) >> /var/ipfire/ethernet/settings |
186 | ;; | |
187 | ||
188 | # ORANGE | |
189 | 2) | |
190 | local interface_name="orange0" | |
191 | config_type=2 | |
192 | ||
193 | ( | |
194 | echo "ORANGE_DEV=${interface_name}" | |
195 | echo "ORANGE_MACADDR=${mac}" | |
196 | echo "ORANGE_DESCRIPTION='${interface_id}'" | |
197 | echo "ORANGE_ADDRESS=${ipv4_address}" | |
198 | echo "ORANGE_NETMASK=${netmask}" | |
199 | echo "ORANGE_NETADDRESS=${netaddress}" | |
200 | echo "ORANGE_BROADCAST=${broadcast}" | |
bf1ae6aa | 201 | echo "ORANGE_MTU=${DEFAULT_MTU}" |
89b10e70 MT |
202 | ) >> /var/ipfire/ethernet/settings |
203 | ;; | |
204 | esac | |
205 | done | |
206 | ||
207 | # Save CONFIG_TYPE | |
208 | echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings | |
209 | ||
210 | # Actions performed only on the very first start | |
211 | if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then | |
212 | # Disable using ISP nameservers | |
213 | sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings | |
214 | ||
215 | # Enable SSH | |
216 | sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings | |
217 | ||
218 | # Disable SSH password authentication | |
219 | sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings | |
220 | ||
221 | # Enable SSH key authentication | |
222 | sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings | |
223 | ||
224 | # Apply SSH settings | |
225 | /usr/local/bin/sshctrl | |
226 | ||
227 | # Mark SSH to start immediately (but not right now) | |
228 | touch /var/ipfire/remote/enablessh | |
229 | chown nobody:nobody /var/ipfire/remote/enablessh | |
230 | ||
231 | # Firewall rules for SSH and WEBIF | |
232 | ( | |
233 | echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second" | |
234 | echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second" | |
235 | ) >> /var/ipfire/firewall/input | |
236 | ||
237 | # This script has now completed the first steps of setup | |
238 | touch /var/ipfire/main/firstsetup_ok | |
239 | fi | |
240 | ||
241 | # All done | |
242 | echo_ok | |
243 | } | |
244 | ||
245 | case "${reason}" in | |
246 | PREINIT) | |
247 | # Bring up the interface | |
248 | ip link set "${interface}" up | |
249 | ;; | |
250 | ||
251 | BOUND|RENEW|REBIND|REBOOT) | |
252 | # Remove any previous IP addresses | |
253 | ip addr flush dev "${interface}" | |
254 | ||
255 | # Add (or re-add) the new IP address | |
256 | ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}" | |
257 | ||
258 | # Add the default route | |
b6a58881 | 259 | ip route add "${new_routers}" dev "${interface}" |
89b10e70 MT |
260 | ip route add default via "${new_routers}" |
261 | ||
262 | # Setup DNS | |
263 | for domain_name_server in ${new_domain_name_servers}; do | |
264 | echo "nameserver ${domain_name_server}" | |
265 | done > /etc/resolv.conf | |
266 | ||
267 | # The system is online now | |
268 | touch /var/ipfire/red/active | |
269 | ||
270 | # Import GCP configuration | |
271 | import_gcp_configuration | |
272 | ;; | |
273 | ||
274 | EXPIRE|FAIL|RELEASE|STOP) | |
275 | # The system is no longer online | |
276 | rm -f /var/ipfire/red/active | |
277 | ||
278 | # Remove all IP addresses | |
279 | ip addr flush dev "${interface}" | |
280 | ||
281 | # Shut down the interface | |
282 | ip link set "${interface}" down | |
283 | ;; | |
284 | ||
285 | *) | |
286 | echo "Unhandled reason: ${reason}" >&2 | |
287 | exit 2 | |
288 | ;; | |
289 | esac | |
290 | ||
291 | # Terminate | |
292 | exit 0 |