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