]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/initscripts/helper/exoscale-setup
Early spring clean: Remove trailing whitespaces, and correct licence headers
[people/pmueller/ipfire-2.x.git] / src / initscripts / helper / exoscale-setup
CommitLineData
e06d8de9 1#!/bin/bash
66c36198
PM
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
e06d8de9
MT
21
22. /etc/sysconfig/rc
23. ${rc_functions}
24
25# Set PATH to find our own executables
26export PATH=/usr/local/sbin:/usr/local/bin:${PATH}
27
28# Exoscale only supports a MTU of 1500
29DEFAULT_MTU=1500
30
31get() {
32 local file="${1}"
33
34 wget -qO - "http://metadata.exoscale.com/latest/${file}"
35}
36
37import_exoscale_configuration() {
38 local instance_id="$(get meta-data/instance-id)"
39
40 boot_mesg "Importing Exoscale configuration for instance ${instance_id}..."
41
42 # Store instance ID
43 echo "${instance_id}" > /var/run/exoscale-instance-id
44
45 # Initialise system settings
46 local hostname=$(get meta-data/local-hostname)
47
48 # Set hostname
49 if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
50 echo "HOSTNAME=${hostname%%.*}" >> /var/ipfire/main/settings
51 fi
52
53 # Set domainname
54 if ! grep -q "^DOMAINNAME=" /var/ipfire/main/settings; then
df892010 55 local domainname="localdomain"
e06d8de9 56
df892010
MT
57 # If the hostname contains a dot we strip the last
58 # part and use it as our domain name
59 if [[ ${hostname} =~ "\." ]]; then
60 domainname="${hostname#*.}"
61 fi
e06d8de9
MT
62
63 echo "DOMAINNAME=${domainname}" >> /var/ipfire/main/settings
64 fi
65
66 # Create setup user
67 if ! getent passwd setup &>/dev/null; then
68 useradd setup -s /usr/bin/run-setup -g nobody -m
69
70 # Unlock the account
71 usermod -p "x" setup
72 fi
73
a7d8d352
MT
74 # Import SSH key for setup user
75 local key=$(get "meta-data/public-keys")
76 if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
77 mkdir -p "/home/setup/.ssh"
78 chmod 700 "/home/setup/.ssh"
79 chown setup.nobody "/home/setup/.ssh"
80
81 echo "${key}" >> "/home/setup/.ssh/authorized_keys"
82 chmod 600 "/home/setup/.ssh/authorized_keys"
83 chown setup.nobody "/home/setup/.ssh/authorized_keys"
84 fi
e06d8de9
MT
85
86 # Download the user-data script only on the first boot
87 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
88 # Download user-data
89 local user_data="$(get user-data)"
90
91 # Save user-data script to be executed later
92 if [ "${user_data:0:2}" = "#!" ]; then
93 echo "${user_data}" > /tmp/user-data.script
94 chmod 700 /tmp/user-data.script
95
96 # Run the user-data script
97 local now="$(date -u +"%s")"
98 /tmp/user-data.script &>/var/log/user-data.log.${now}
99
100 # Delete the script right away
101 rm /tmp/user-data.script
102 fi
103 fi
104
105 # Import any previous settings for the local interfaces
106 eval $(/usr/local/bin/readhash <(grep -E "^(GREEN|ORANGE)_.*=" /var/ipfire/ethernet/settings 2>/dev/null))
107
108 # Import network configuration
109 # After this, no network connectivity will be available from this script due to the
110 # renaming of the network interfaces for which they have to be shut down
111 local config_type=1
112 : > /var/ipfire/ethernet/settings
113
114 local device
115 for device in /sys/class/net/*; do
116 # Fetch the interface index
117 local ifindex="$(<${device}/ifindex)"
118 local address="$(<${device}/address)"
119
120 case "${ifindex}" in
121 # loopback
122 1)
123 continue
124 ;;
125
126 # The first interface will always be the public-facing one (i.e. RED)
127 2)
128 (
129 echo "RED_TYPE=DHCP"
130 echo "RED_DEV=red0"
131 echo "RED_MACADDR=${address}"
132 echo "RED_DESCRIPTION='${ifindex}'"
133 echo "RED_MTU=1500"
134 ) >> /var/ipfire/ethernet/settings
135 ;;
136
137 # GREEN
138 3)
139 # Set some sensible defaults for GREEN
140 if [ -z "${GREEN_ADDRESS}" ]; then
141 GREEN_ADDRESS="10.0.0.1"
142 GREEN_NETMASK="255.255.255.0"
143 GREEN_NETADDRESS="10.0.0.0"
e06d8de9
MT
144 fi
145
146 (
147 echo "GREEN_DEV=green0"
148 echo "GREEN_MACADDR=${address}"
149 echo "GREEN_DESCRIPTION='${ifindex}'"
150 echo "GREEN_ADDRESS=${GREEN_ADDRESS}"
151 echo "GREEN_NETMASK=${GREEN_NETMASK}"
152 echo "GREEN_NETADDRESS=${GREEN_NETADDRESS}"
e06d8de9
MT
153 echo "GREEN_MTU=${DEFAULT_MTU}"
154 ) >> /var/ipfire/ethernet/settings
155 ;;
156
157 # ORANGE
158 4)
159 config_type=2
160
161 # Set some sensible defaults for ORANGE
162 if [ -z "${ORANGE_ADDRESS}" ]; then
163 ORANGE_ADDRESS="10.0.1.1"
164 ORANGE_NETMASK="255.255.255.0"
165 ORANGE_NETADDRESS="10.0.1.0"
e06d8de9
MT
166 fi
167
168 (
169 echo "ORANGE_DEV=orange0"
170 echo "ORANGE_MACADDR=${address}"
171 echo "ORANGE_DESCRIPTION='${ifindex}'"
172 echo "ORANGE_ADDRESS=${ORANGE_ADDRESS}"
173 echo "ORANGE_NETMASK=${ORANGE_NETMASK}"
174 echo "ORANGE_NETADDRESS=${ORANGE_NETADDRESS}"
e06d8de9
MT
175 echo "ORANGE_MTU=${DEFAULT_MTU}"
176 ) >> /var/ipfire/ethernet/settings
177 ;;
178 esac
179 done
180
181 # Save CONFIG_TYPE
182 echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
183
184 # Actions performed only on the very first start
185 if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
186 # Disable using ISP nameservers
187 sed -e "s/^USE_ISP_NAMESERVERS=.*/USE_ISP_NAMESERVERS=off/" -i /var/ipfire/dns/settings
188
189 # Enable SSH
190 sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
191
192 # Disable SSH password authentication
193 sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
194
195 # Enable SSH key authentication
196 sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
197
198 # Apply SSH settings
199 /usr/local/bin/sshctrl
200
201 # Mark SSH to start immediately (but not right now)
202 touch /var/ipfire/remote/enablessh
203 chown nobody:nobody /var/ipfire/remote/enablessh
204
205 # Firewall rules for SSH and WEBIF
206 (
207 echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
208 echo "2,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,TGT_PORT,444,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
209 ) >> /var/ipfire/firewall/input
210
211 # This script has now completed the first steps of setup
212 touch /var/ipfire/main/firstsetup_ok
213 fi
214
215 # All done
216 echo_ok
217}
218
219case "${reason}" in
220 PREINIT)
221 # Bring up the interface
222 ip link set "${interface}" up
223 ;;
224
225 BOUND|RENEW|REBIND|REBOOT)
226 # Remove any previous IP addresses
227 ip addr flush dev "${interface}"
228
229 # Add (or re-add) the new IP address
230 ip addr add "${new_ip_address}/${new_subnet_mask}" dev "${interface}"
231
232 # Add the default route
233 ip route add default via "${new_routers}"
234
235 # Setup DNS
236 for domain_name_server in ${new_domain_name_servers}; do
237 echo "nameserver ${domain_name_server}"
238 done > /etc/resolv.conf
239
240 # The system is online now
241 touch /var/ipfire/red/active
242
243 # Import Exoscale configuration
244 import_exoscale_configuration
245 ;;
246
247 EXPIRE|FAIL|RELEASE|STOP)
248 # The system is no longer online
249 rm -f /var/ipfire/red/active
250
251 # Remove all IP addresses
252 ip addr flush dev "${interface}"
253
254 # Shut down the interface
255 ip link set "${interface}" down
256 ;;
257
258 *)
259 echo "Unhandled reason: ${reason}" >&2
260 exit 2
261 ;;
262esac
263
264# Terminate
265exit 0