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