]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - src/initscripts/helper/aws-setup
aws-setup: Remove some excessive whitespace
[people/pmueller/ipfire-2.x.git] / src / initscripts / helper / aws-setup
index 2fae9ea18c7e40584ea378ccb690b25c8678b654..2c3311fc24ba44e0b44fddc3484a4100d25a99c5 100644 (file)
@@ -3,10 +3,13 @@
 . /etc/sysconfig/rc
 . ${rc_functions}
 
+# Set PATH to find our own executables
+export PATH=/usr/local/sbin:/usr/local/bin:${PATH}
+
 get() {
        local file="${1}"
 
-       wget -qO - "http://169.254.169.254/latest/meta-data/${file}"
+       wget -qO - "http://169.254.169.254/latest/${file}"
 }
 
 to_address() {
@@ -48,12 +51,15 @@ prefix2netmask() {
 }
 
 import_aws_configuration() {
-       local instance_id="$(get instance-id)"
+       local instance_id="$(get meta-data/instance-id)"
 
        boot_mesg "Importing AWS configuration for instance ${instance_id}..."
 
+       # Store instance ID
+       echo "${instance_id}" > /var/run/aws-instance-id
+
        # Initialise system settings
-       local hostname=$(get local-hostname)
+       local hostname=$(get meta-data/local-hostname)
 
        # Set hostname
        if ! grep -q "^HOSTNAME=" /var/ipfire/main/settings; then
@@ -65,34 +71,75 @@ import_aws_configuration() {
                echo "DOMAINNAME=${hostname#*.}" >> /var/ipfire/main/settings
        fi
 
-       # Import any DNS server settings
-       eval $(/usr/local/bin/readhash <(grep -E "^DNS([0-9])=" /var/ipfire/ethernet/settings 2>/dev/null))
+       # Create setup user
+       if ! getent passwd setup &>/dev/null; then
+               useradd setup -s /usr/bin/run-setup -g nobody -m
+
+               # Unlock the account
+               usermod -p "x" setup
+       fi
+
+       # Import SSH keys for setup user
+       local line
+       for line in $(get "meta-data/public-keys/"); do
+               local key_no="${line%=*}"
+
+               local key="$(get meta-data/public-keys/${key_no}/openssh-key)"
+               if [ -n "${key}" ] && ! grep -q "^${key}$" "/home/setup/.ssh/authorized_keys" 2>/dev/null; then
+                       mkdir -p "/home/setup/.ssh"
+                       chmod 700 "/home/setup/.ssh"
+                       chown setup.nobody "/home/setup/.ssh"
+
+                       echo "${key}" >> "/home/setup/.ssh/authorized_keys"
+                       chmod 600 "/home/setup/.ssh/authorized_keys"
+                       chown setup.nobody "/home/setup/.ssh/authorized_keys"
+               fi
+       done
+
+       # Download the user-data script only on the first boot
+       if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
+               # Download user-data
+               local user_data="$(get user-data)"
+
+               # Save user-data script to be executed later
+               if [ "${user_data:0:2}" = "#!" ]; then
+                       echo "${user_data}" > /tmp/aws-user-data.script
+                       chmod 700 /tmp/aws-user-data.script
+
+                       # Run the user-data script
+                       local now="$(date -u +"%s")"
+                       /tmp/aws-user-data.script &>/var/log/user-data.log.${now}
+
+                       # Delete the script right away
+                       rm /tmp/aws-user-data.script
+               fi
+       fi
 
        # Import network configuration
-       (
-               # RED + GREEN
-                       echo "CONFIG_TYPE=1"
-       ) > /var/ipfire/ethernet/settings
+       # After this, no network connectivity will be available from this script due to the
+       # renaming of the network interfaces for which they have to be shut down
+       local config_type=1
+       : > /var/ipfire/ethernet/settings
 
        local mac
-       for mac in $(get network/interfaces/macs/); do
+       for mac in $(get meta-data/network/interfaces/macs/); do
                # Remove trailing slash
                mac="${mac//\//}"
 
-               local device_number="$(get "network/interfaces/macs/${mac}/device-number")"
-               local interface_id="$(get "network/interfaces/macs/${mac}/interface-id")"
+               local device_number="$(get "meta-data/network/interfaces/macs/${mac}/device-number")"
+               local interface_id="$(get "meta-data/network/interfaces/macs/${mac}/interface-id")"
 
                # First IPv4 address
-               local ipv4_address="$(get "network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
+               local ipv4_address="$(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | head -n1)"
                local ipv4_address_num="$(to_integer "${ipv4_address}")"
 
                # Get VPC subnet
-               local vpc="$(get "network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
+               local vpc="$(get "meta-data/network/interfaces/macs/${mac}/vpc-ipv4-cidr-block")"
                local vpc_netaddress="${vpc%/*}"
                local vpc_netaddress_num="$(to_integer "${vpc_netaddress}")"
 
                # Get subnet size
-               local subnet="$(get "network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
+               local subnet="$(get "meta-data/network/interfaces/macs/${mac}/subnet-ipv4-cidr-block")"
 
                local prefix="${subnet#*/}"
                local netmask="$(prefix2netmask "${prefix}")"
@@ -106,16 +153,14 @@ import_aws_configuration() {
                case "${device_number}" in
                        # RED
                        0)
+                               local interface_name="red0"
+
                                # The gateway is always the first IP address in the subnet
                                local gateway="$(to_address $(( netaddress_num + 1 )))"
 
-                               # The AWS internal DNS service is available on the second IP address of the VPC
-                               local dns1="$(to_address $(( vpc_netaddress_num + 2 )))"
-                               local dns2=
-
                                (
                                        echo "RED_TYPE=STATIC"
-                                       echo "RED_DEV=red0"
+                                       echo "RED_DEV=${interface_name}"
                                        echo "RED_MACADDR=${mac}"
                                        echo "RED_DESCRIPTION='${interface_id}'"
                                        echo "RED_ADDRESS=${ipv4_address}"
@@ -123,20 +168,20 @@ import_aws_configuration() {
                                        echo "RED_NETADDRESS=${netaddress}"
                                        echo "RED_BROADCAST=${broadcast}"
                                        echo "DEFAULT_GATEWAY=${gateway}"
-                                       echo "DNS1=${DNS1:-${dns1}}"
-                                       echo "DNS2=${DNS2:-${dns2}}"
                                ) >> /var/ipfire/ethernet/settings
 
                                # Import aliases for RED
-                               for alias in $(get "network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
+                               for alias in $(get "meta-data/network/interfaces/macs/${mac}/local-ipv4s" | tail -n +2); do
                                        echo "${alias},on,"
                                done > /var/ipfire/ethernet/aliases
                                ;;
 
                        # GREEN
                        1)
+                               local interface_name="green0"
+
                                (
-                                       echo "GREEN_DEV=green0"
+                                       echo "GREEN_DEV=${interface_name}"
                                        echo "GREEN_MACADDR=${mac}"
                                        echo "GREEN_DESCRIPTION='${interface_id}'"
                                        echo "GREEN_ADDRESS=${ipv4_address}"
@@ -145,35 +190,46 @@ import_aws_configuration() {
                                        echo "GREEN_BROADCAST=${broadcast}"
                                ) >> /var/ipfire/ethernet/settings
                                ;;
-               esac
-       done
-
-       # Import SSH keys
-       local line
-       for line in $(get "public-keys/"); do
-               local key_no="${line%=*}"
 
-               local key="$(get public-keys/${key_no}/openssh-key)"
-               if [ -n "${key}" ] && ! grep -q "^${key}$" /root/.ssh/authorized_keys 2>/dev/null; then
-                       mkdir -p /root/.ssh
-                       chmod 700 /root/.ssh
+                       # ORANGE
+                       2)
+                               local interface_name="orange0"
+                               config_type=2
 
-                       echo "${key}" >> /root/.ssh/authorized_keys
-                       chmod 600 /root/.ssh/authorized_keys
-               fi
+                               (
+                                       echo "ORANGE_DEV=${interface_name}"
+                                       echo "ORANGE_MACADDR=${mac}"
+                                       echo "ORANGE_DESCRIPTION='${interface_id}'"
+                                       echo "ORANGE_ADDRESS=${ipv4_address}"
+                                       echo "ORANGE_NETMASK=${netmask}"
+                                       echo "ORANGE_NETADDRESS=${netaddress}"
+                                       echo "ORANGE_BROADCAST=${broadcast}"
+                               ) >> /var/ipfire/ethernet/settings
+                               ;;
+               esac
        done
 
+       # Save CONFIG_TYPE
+       echo "CONFIG_TYPE=${config_type}" >> /var/ipfire/ethernet/settings
+
        # Actions performed only on the very first start
        if [ ! -e "/var/ipfire/main/firstsetup_ok" ]; then
                # Enable SSH
                sed -e "s/ENABLE_SSH=.*/ENABLE_SSH=on/g" -i /var/ipfire/remote/settings
 
-               touch /var/ipfire/remote/enablessh
-               chown nobody:nobody /var/ipfire/remote/enablessh
+               # Disable SSH password authentication
+               sed -e "s/^ENABLE_SSH_PASSWORDS=.*/ENABLE_SSH_PASSWORDS=off/" -i /var/ipfire/remote/settings
 
                # Enable SSH key authentication
                sed -e "s/^ENABLE_SSH_KEYS=.*/ENABLE_SSH_KEYS=on/" -i /var/ipfire/remote/settings
 
+               # Apply SSH settings
+               /usr/local/bin/sshctrl
+
+               # Mark SSH to start immediately (but not right now)
+               touch /var/ipfire/remote/enablessh
+               chown nobody:nobody /var/ipfire/remote/enablessh
+
                # Firewall rules for SSH and WEBIF
                (
                        echo "1,ACCEPT,INPUTFW,ON,std_net_src,ALL,ipfire,RED1,,TCP,,,ON,,,cust_srv,SSH,,,,,,,,,,,00:00,00:00,,AUTO,,dnat,,,,,second"
@@ -204,13 +260,27 @@ case "${reason}" in
                # Add the default route
                ip route add default via "${new_routers}"
 
+               # Setup DNS
+               for domain_name_server in ${new_domain_name_servers}; do
+                       echo "nameserver ${domain_name_server}"
+               done > /etc/resolv.conf
+
+               # The system is online now
+               touch /var/ipfire/red/active
+
                # Import AWS configuration
                import_aws_configuration
                ;;
 
        EXPIRE|FAIL|RELEASE|STOP)
+               # The system is no longer online
+               rm -f /var/ipfire/red/active
+
                # Remove all IP addresses
                ip addr flush dev "${interface}"
+
+               # Shut down the interface
+               ip link set "${interface}" down
                ;;
 
        *)