2015-11-21 00:54:24 +01:00
#!/bin/bash
2015-11-20 17:09:21 +01:00
#
2015-11-21 12:25:32 +01:00
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
2015-11-20 17:09:21 +01:00
# Based on bin/freedombox-customize from freedom-maker
#
2015-11-21 12:25:32 +01:00
# License
# =======
#
2015-11-20 17:09:21 +01:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -e
set -x
2015-11-21 12:56:40 +01:00
PROJECT_NAME='freedombone'
2016-01-14 19:00:25 +01:00
INSTALL_DIR=/root/build
2015-11-27 12:42:16 +01:00
2015-12-03 15:40:38 +01:00
# username created by default within a debian image
GENERIC_IMAGE_USERNAME='fbone'
2015-11-27 17:52:23 +01:00
export TEXTDOMAIN=${PROJECT_NAME}-image-customise
2015-11-27 12:42:16 +01:00
export TEXTDOMAINDIR="/usr/share/locale"
2015-11-21 12:56:40 +01:00
2015-11-30 15:25:09 +01:00
# Whether to minimise the number of decisions during interactive install
MINIMAL_INSTALL="yes"
2015-11-21 11:29:10 +01:00
MY_USERNAME='debian'
2015-12-01 09:32:30 +01:00
MY_PASSWORD="${PROJECT_NAME}"
2015-11-20 22:43:03 +01:00
# IP address of the router (gateway)
ROUTER_IP_ADDRESS="192.168.1.254"
# The fixed IP address of the Beaglebone Black on your local network
BOX_IP_ADDRESS="192.168.1.55"
# DNS
NAMESERVER1='213.73.91.35'
NAMESERVER2='85.214.20.141'
2015-11-21 13:59:19 +01:00
# optional configuration file containing freedombone settings
CONFIG_FILENAME=
2015-11-21 15:33:39 +01:00
# Optional ssh public key to allow
SSH_PUBKEY="no"
2015-11-23 11:33:45 +01:00
# Whether this is a generic image for mass redistribution on the interwebs
GENERIC_IMAGE="no"
2015-12-04 13:09:07 +01:00
# default SSH port
SSH_PORT=2222
2015-12-31 13:00:55 +01:00
# Whether sites are accessible only within a Tor browser
ONION_ONLY="no"
2016-01-15 15:41:59 +01:00
WIFI_INTERFACE='wlan0'
2015-11-20 17:09:21 +01:00
enable_eatmydata_override() {
chroot $rootdir apt-get install --no-install-recommends -y eatmydata
if [ -x $rootdir/usr/bin/eatmydata ] && \
[ ! -f $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata ]; then
2015-11-27 16:29:43 +01:00
echo $"info: Adding apt config to call dpkg via eatmydata"
2015-11-20 17:09:21 +01:00
printf "#!/bin/sh\nexec eatmydata dpkg \"\$@\"\n" \
> $rootdir/var/tmp/dpkg-eatmydata
chmod 755 $rootdir/var/tmp/dpkg-eatmydata
cat > $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata <<EOF
Dir::Bin::dpkg "/var/tmp/dpkg-eatmydata";
EOF
else
2015-11-27 16:29:43 +01:00
echo $"error: unable to find /usr/bin/eatmydata after installing the eatmydata package"
2015-11-20 17:09:21 +01:00
fi
}
disable_eatmydata_override() {
for override in \
/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata \
/var/tmp/dpkg-eatmydata ; do
2015-11-27 16:29:43 +01:00
echo $"info: Removing apt config to call dpkg via eatmydata"
2015-11-20 17:09:21 +01:00
if [ -f $rootdir$override ] ; then
rm -f $rootdir$override
else
2015-11-27 16:29:43 +01:00
echo $"warning: missing $rootdir$override"
2015-11-20 17:09:21 +01:00
fi
done
sync # Flush file buffers before continuing
}
set_apt_sources() {
NEW_MIRROR="$1"
COMPONENTS="main"
cat <<EOF > etc/apt/sources.list
deb $NEW_MIRROR $SUITE $COMPONENTS
deb-src $NEW_MIRROR $SUITE $COMPONENTS
#deb http://security.debian.org/ $SUITE/updates main
#deb-src http://security.debian.org/ $SUITE/updates main
EOF
}
2015-11-22 12:12:38 +01:00
configure_networking() {
2015-11-23 21:43:43 +01:00
if [[ $GENERIC_IMAGE == "no" ]]; then
2015-11-23 17:30:31 +01:00
echo "# This file describes the network interfaces available on your system
2015-11-21 14:12:31 +01:00
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address $BOX_IP_ADDRESS
netmask 255.255.255.0
gateway $ROUTER_IP_ADDRESS
dns-nameservers $NAMESERVER1 $NAMESERVER2
# Example to keep MAC address between reboots
#hwaddress ether B5:A2:BE:3F:1A:FE
# The secondary network interface
#auto eth1
#iface eth1 inet dhcp
# WiFi Example
#auto wlan0
#iface wlan0 inet dhcp
# wpa-ssid \"essid\"
# wpa-psk \"password\"
# Ethernet/RNDIS gadget (g_ether)
# ... or on host side, usbnet and random hwaddr
# Note on some boards, usb0 is automaticly setup with an init script
#iface usb0 inet static
# address 192.168.7.2
# netmask 255.255.255.0
# network 192.168.7.0
# gateway 192.168.7.1" > $rootdir/etc/network/interfaces
2015-11-23 17:30:31 +01:00
hexarray=( 1 2 3 4 5 6 7 8 9 0 a b c d e f )
a=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
b=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
c=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
d=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
e=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
2015-11-23 21:43:43 +01:00
sed -i "s|#hwaddress ether.*|hwaddress ether de:$a:$b:$c:$d:$e|g" \
2015-11-23 17:30:31 +01:00
$rootdir/etc/network/interfaces
2015-11-23 21:43:43 +01:00
fi
2015-11-21 14:12:31 +01:00
sed -i "s/nameserver.*/nameserver $NAMESERVER1/g" $rootdir/etc/resolv.conf
sed -i "/nameserver $NAMESERVER1/a\nameserver $NAMESERVER2" $rootdir/etc/resolv.conf
# change the motd to show further install instructions
2015-11-27 16:29:43 +01:00
echo $"
2015-11-22 14:52:42 +01:00
.---. . .
| | |
|--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
| | (.-' (.-' ( | ( )| | | | )( )| | (.-'
' ' --' --' -' - -' ' ' -' -' -' ' - --'
Initial base install
Your system is not yet installed. To complete the process run the
following commands, then enter your details.
2015-11-21 14:12:31 +01:00
sudo su
2015-12-01 09:32:30 +01:00
${PROJECT_NAME} menuconfig
2015-11-21 14:12:31 +01:00
2015-11-22 14:52:42 +01:00
" > $rootdir/etc/motd
2015-11-21 14:12:31 +01:00
}
2015-11-22 12:12:38 +01:00
configure_ssh() {
2015-12-04 13:09:07 +01:00
sed -i "s/Port .*/Port ${SSH_PORT}/g" $rootdir/etc/ssh/sshd_config
2015-11-21 16:47:44 +01:00
2015-11-21 15:33:39 +01:00
if [[ "$SSH_PUBKEY" != "no" ]]; then
if [ ! -d $rootdir/home/$MY_USERNAME/.ssh ]; then
mkdir $rootdir/home/$MY_USERNAME/.ssh
fi
echo "$SSH_PUBKEY" > $rootdir/home/$MY_USERNAME/.ssh/authorized_keys
chroot $rootdir chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
sed -i 's|PasswordAuthentication.*|PasswordAuthentication no|g' $rootdir/etc/ssh/sshd_config
2015-11-27 16:29:43 +01:00
echo $"Using ssh public key:"
2015-11-21 15:33:39 +01:00
echo $SSH_PUBKEY
2015-11-27 16:29:43 +01:00
echo $'Password ssh authentication turned off'
2015-11-21 15:33:39 +01:00
fi
}
2015-11-22 14:43:23 +01:00
admin_user_sudo() {
echo "$MY_USERNAME ALL=(ALL) ALL" >> $rootdir/etc/sudoers
}
2015-11-23 11:33:45 +01:00
create_generic_image() {
if [[ $GENERIC_IMAGE == "no" ]]; then
2015-11-23 16:04:05 +01:00
return
fi
2015-12-24 20:04:17 +01:00
VARIANT="full"
if [ $CONFIG_FILENAME ]; then
if [[ "$CONFIG_FILENAME" == *"mesh.cfg"* ]]; then
VARIANT="mesh"
fi
fi
2015-11-23 11:33:45 +01:00
# Don't install any configuration. This will be a base system
2015-12-24 20:04:17 +01:00
if [[ $VARIANT != "mesh" ]]; then
CONFIG_FILENAME=
2016-01-11 12:27:08 +01:00
else
touch $rootdir/root/.initial_mesh_setup
2015-12-24 20:04:17 +01:00
fi
2015-11-23 16:04:05 +01:00
# The presence of this file indicates that the initial
# setup has not yet been completed
2015-11-23 11:33:45 +01:00
touch $rootdir/home/$MY_USERNAME/.initial_setup
2015-11-23 22:14:12 +01:00
chroot $rootdir chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.initial_setup
2015-11-23 17:36:03 +01:00
touch $rootdir/root/.initial_setup
2015-11-23 21:43:43 +01:00
2015-11-23 16:04:05 +01:00
cat >> $rootdir/home/$MY_USERNAME/.bashrc <<EOF
2015-11-23 12:26:04 +01:00
# initial setup of the system
2015-11-23 23:03:17 +01:00
if [ -f ~/.initial_setup ]; then
2015-11-23 23:19:17 +01:00
clear
2015-11-23 23:03:17 +01:00
echo "
.---. . .
| | |
|--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
| | (.-' (.-' ( | ( )| | | | )( )| | (.-'
' ' --' --' -' - -' ' ' -' -' -' ' - --'
Initial setup process
Please enter your password a second time.
"
sudo su
fi
EOF
2016-01-05 18:43:02 +01:00
echo '# initial setup of the system' >> $rootdir/root/.bashrc
echo 'if [ -f ~/.initial_setup ]; then' >> $rootdir/root/.bashrc
2015-11-23 23:03:17 +01:00
echo ' if [ -f ~/login.txt ]; then' >> $rootdir/root/.bashrc
2015-11-24 15:14:07 +01:00
echo ' NEW_USER_PASSWORD=$(printf `cat ~/login.txt`)' >> $rootdir/root/.bashrc
2015-11-23 23:03:17 +01:00
echo ' else' >> $rootdir/root/.bashrc
2015-11-24 11:22:55 +01:00
echo ' ENTROPY=$(cat /proc/sys/kernel/random/entropy_avail)' >> $rootdir/root/.bashrc
2015-11-24 15:56:44 +01:00
echo ' if [ $ENTROPY -lt 500 ]; then' >> $rootdir/root/.bashrc
2015-12-01 09:32:30 +01:00
echo ' dialog --backtitle "Initial setup process" --title "Password Generation" --msgbox "WARNING: The entropy available on this system is too low to generate a password.\n\nThe installation process cannot continue." 8 50' >> $rootdir/root/.bashrc
2015-11-24 16:47:24 +01:00
echo ' exit' >> $rootdir/root/.bashrc
2015-11-24 11:22:55 +01:00
echo ' fi' >> $rootdir/root/.bashrc
2015-11-23 23:03:17 +01:00
echo ' NEW_USER_PASSWORD="$(openssl rand -base64 12 | cut -c1-10)"' >> $rootdir/root/.bashrc
echo ' fi' >> $rootdir/root/.bashrc
2015-11-24 13:08:55 +01:00
echo ' echo "${NEW_USER_PASSWORD}" > ~/login.txt' >> $rootdir/root/.bashrc
2015-11-24 17:54:43 +01:00
echo ' clear' >> $rootdir/root/.bashrc
echo ' echo ""' >> $rootdir/root/.bashrc
echo ' echo "Your new login password is:"' >> $rootdir/root/.bashrc
echo ' echo ""' >> $rootdir/root/.bashrc
echo ' toilet "${NEW_USER_PASSWORD}"' >> $rootdir/root/.bashrc
echo ' echo ""' >> $rootdir/root/.bashrc
echo ' echo " ${NEW_USER_PASSWORD}"' >> $rootdir/root/.bashrc
echo ' echo ""' >> $rootdir/root/.bashrc
echo ' echo "Use it whenever you wish to ssh into this system."' >> $rootdir/root/.bashrc
echo ' echo ""' >> $rootdir/root/.bashrc
2015-11-26 22:43:48 +01:00
echo ' echo "IMPORTANT: Please take a moment to enter the above password into a"' >> $rootdir/root/.bashrc
echo ' echo "password manager or write it down somewhere."' >> $rootdir/root/.bashrc
2015-11-24 17:54:43 +01:00
echo ' echo ""' >> $rootdir/root/.bashrc
echo ' read -n1 -r -p "Press any key to continue..." key' >> $rootdir/root/.bashrc
2015-11-25 10:51:35 +01:00
2015-11-23 21:43:43 +01:00
# change the password for the admin user
2015-11-24 16:47:24 +01:00
echo -n " echo \"${MY_USERNAME}:" >> $rootdir/root/.bashrc
2015-11-24 15:56:44 +01:00
echo '$(printf `cat ~/login.txt`)"|chpasswd' >> $rootdir/root/.bashrc
2015-11-23 21:43:43 +01:00
2015-12-01 21:52:27 +01:00
# update before continuing
echo " cd /root/${PROJECT_NAME}" >> $rootdir/root/.bashrc
echo " git stash" >> $rootdir/root/.bashrc
echo " git pull" >> $rootdir/root/.bashrc
echo " make install" >> $rootdir/root/.bashrc
2015-12-24 20:04:17 +01:00
if [[ $VARIANT != "mesh" ]]; then
2015-12-31 14:21:46 +01:00
if [[ $ONION_ONLY == "no" ]]; then
if [[ $MINIMAL_INSTALL == "no" ]]; then
echo " ${PROJECT_NAME} menuconfig-full" >> $rootdir/root/.bashrc
else
echo " ${PROJECT_NAME} menuconfig" >> $rootdir/root/.bashrc
fi
2015-12-24 20:04:17 +01:00
else
2015-12-31 14:21:46 +01:00
echo " ${PROJECT_NAME} menuconfig-onion" >> $rootdir/root/.bashrc
2015-12-24 20:04:17 +01:00
fi
2015-12-01 21:52:27 +01:00
else
2016-01-11 12:28:18 +01:00
echo ' echo ""'
2015-12-01 21:52:27 +01:00
fi
2015-11-24 16:47:24 +01:00
echo ' if [ "$?" = "0" ]; then' >> $rootdir/root/.bashrc
2015-12-01 09:32:30 +01:00
echo " if [ -f ~/${PROJECT_NAME}-completed.txt ]; then" >> $rootdir/root/.bashrc
2015-11-23 21:43:43 +01:00
# Remove the initial setup files
2015-12-02 21:03:52 +01:00
echo ' rm /root/.initial_setup' >> $rootdir/root/.bashrc
2015-11-24 16:47:24 +01:00
echo " rm /home/${MY_USERNAME}/.initial_setup" >> $rootdir/root/.bashrc
2015-12-03 15:40:38 +01:00
echo " touch /root/.remove_${GENERIC_IMAGE_USERNAME}" >> $rootdir/root/.bashrc
2015-12-02 21:03:52 +01:00
echo ' shred -zu ~/login.txt' >> $rootdir/root/.bashrc
2016-01-08 18:32:54 +01:00
END_MESSAGE1=$'Congratulations!'
if [[ $VARIANT != "mesh" ]]; then
END_MESSAGE2=$'\nYour Freedombone system has now installed\n\nThe onion ssh service is at $SSH_ONION_HOSTNAME\n\nPress any key to reboot and begin using it'
echo ' SSH_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_ssh/hostname)' >> $rootdir/root/.bashrc
else
END_MESSAGE2=$'\nYour Freedombone system has now installed\n\nPress any key to reboot and begin using it'
fi
echo " dialog --title '$END_MESSAGE1' --msgbox \"$END_MESSAGE2\" 9 50" >> $rootdir/root/.bashrc
2015-12-03 15:40:38 +01:00
echo ' reboot' >> $rootdir/root/.bashrc
echo ' fi' >> $rootdir/root/.bashrc
2016-01-04 18:33:05 +01:00
echo ' else' >> $rootdir/root/.bashrc
2016-01-05 12:16:11 +01:00
echo ' key=' >> $rootdir/root/.bashrc
echo ' while [[ $key != "x" ]]; do' >> $rootdir/root/.bashrc
echo ' read -n1 -r -p "Press x to continue..." key' >> $rootdir/root/.bashrc
echo ' done' >> $rootdir/root/.bashrc
2015-12-03 15:40:38 +01:00
echo ' fi' >> $rootdir/root/.bashrc
echo ' exit' >> $rootdir/root/.bashrc
echo 'else' >> $rootdir/root/.bashrc
echo ' # Remove default account after install' >> $rootdir/root/.bashrc
echo " if [ -f /root/.remove_${GENERIC_IMAGE_USERNAME} ]; then" >> $rootdir/root/.bashrc
echo " if [ -d /home/${GENERIC_IMAGE_USERNAME} ]; then" >> $rootdir/root/.bashrc
echo " userdel -r ${GENERIC_IMAGE_USERNAME}" >> $rootdir/root/.bashrc
echo " if [ -d /home/${GENERIC_IMAGE_USERNAME} ]; then" >> $rootdir/root/.bashrc
echo " rm -rf /home/${GENERIC_IMAGE_USERNAME}" >> $rootdir/root/.bashrc
echo " rm /root/.remove_${GENERIC_IMAGE_USERNAME}" >> $rootdir/root/.bashrc
echo ' fi' >> $rootdir/root/.bashrc
echo ' fi' >> $rootdir/root/.bashrc
echo ' fi' >> $rootdir/root/.bashrc
echo 'fi' >> $rootdir/root/.bashrc
2015-11-23 11:33:45 +01:00
}
2015-11-22 12:12:38 +01:00
continue_installation() {
2015-11-21 14:12:31 +01:00
# If a configuration file exists then run with it
# otherwise the interactive installer can be used
# This is equivalent to installing freedombox-setup on freedombox
if [ $CONFIG_FILENAME ]; then
if [ ${#CONFIG_FILENAME} -gt 2 ]; then
cp $CONFIG_FILENAME $rootdir/root/$PROJECT_NAME.cfg
2016-01-11 18:06:59 +01:00
cat $rootdir/root/$PROJECT_NAME.cfg
2016-01-12 11:05:23 +01:00
chroot "$rootdir" su -c "$PROJECT_NAME -c /root/$PROJECT_NAME.cfg" - root
2015-11-21 14:12:31 +01:00
fi
fi
}
2015-12-22 12:07:00 +01:00
atheros_wifi() {
firmware_filename="open-ath9k-htc-firmware_1.3-1_all.deb"
firmware_hash='5fea58ffefdf0ef15b504db7fbe3bc078c03e0d927bba64085e4b6f2546102f5'
firmware_url="http://us.archive.trisquel.info/trisquel/pool/main/o/open-ath9k-htc-firmware/$firmware_filename"
firmware_tempfile="/tmp/$firmware_filename"
wget "$firmware_url" -O "$rootdir$firmware_tempfile"
downloaded_firmware_hash=$(sha256sum "$rootdir$firmware_tempfile" | awk -F ' ' '{print $1}')
if [[ "$downloaded_firmware_hash" == "$firmware_hash" ]]; then
chroot "$rootdir" dpkg -i "$firmware_tempfile"
else
echo 'WARNING: Atheros Wifi firmware hash does not match. The driver has not been installed.'
fi
}
2016-01-14 18:45:02 +01:00
mesh_avahi() {
chroot "$rootdir" apt-get -y install avahi-utils avahi-autoipd avahi-dnsconfd
decarray=( 1 2 3 4 5 6 7 8 9 0 )
PEER_ID=${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}
sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" $rootdir/etc/avahi/avahi-daemon.conf
if [ ! -d $rootdir/etc/avahi/services ]; then
mkdir -p $rootdir/etc/avahi/services
fi
# remove an avahi service which isn't used
if [ -f $rootdir/etc/avahi/services/udisks.service ]; then
rm $rootdir/etc/avahi/services/udisks.service
fi
# Add an ssh service
echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->' > $rootdir/etc/avahi/services/ssh.service
echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">' >> $rootdir/etc/avahi/services/ssh.service
echo '<service-group>' >> $rootdir/etc/avahi/services/ssh.service
echo ' <name replace-wildcards="yes">%h SSH</name>' >> $rootdir/etc/avahi/services/ssh.service
echo ' <service>' >> $rootdir/etc/avahi/services/ssh.service
echo ' <type>_ssh._tcp</type>' >> $rootdir/etc/avahi/services/ssh.service
echo " <port>$SSH_PORT</port>" >> $rootdir/etc/avahi/services/ssh.service
echo ' </service>' >> $rootdir/etc/avahi/services/ssh.service
echo '</service-group>' >> $rootdir/etc/avahi/services/ssh.service
# keep the daemon running
2016-01-15 15:41:59 +01:00
WATCHDOG_SCRIPT_NAME="keepon"
2016-01-14 18:45:02 +01:00
echo '' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
echo '# keep avahi daemon running' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
echo 'AVAHI_RUNNING=$(pgrep avahi-daemon > /dev/null && echo Running)' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
echo 'if [ ! $AVAHI_RUNNING ]; then' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
echo ' systemctl start avahi-daemon' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
echo ' echo -n $CURRENT_DATE >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
echo ' echo " Avahi daemon restarted" >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
echo 'fi' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
2016-01-15 15:41:59 +01:00
chmod +x $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
2016-01-14 18:45:02 +01:00
}
2016-01-15 15:41:59 +01:00
mesh_batman() {
2016-01-14 18:45:02 +01:00
chroot "$rootdir" apt-get -y install iproute bridge-utils libnetfilter-conntrack3 batctl
chroot "$rootdir" apt-get -y install python-dev libevent-dev ebtables python-pip git
chroot "$rootdir" apt-get -y install wireless-tools rfkill
if ! grep -q "batman_adv" $rootdir/etc/modules; then
echo 'batman_adv' >> $rootdir/etc/modules
fi
2016-01-15 15:41:59 +01:00
if [ -f /usr/local/bin/${PROJECT_NAME}-mesh-batman ]; then
2016-01-14 18:45:02 +01:00
cp /usr/local/bin/${PROJECT_NAME}-mesh-batman $rootdir/var/lib/batman
2016-01-15 15:41:59 +01:00
else
2016-01-14 18:45:02 +01:00
cp /usr/bin/${PROJECT_NAME}-mesh-batman $rootdir/var/lib/batman
2016-01-15 15:41:59 +01:00
fi
2016-01-14 18:45:02 +01:00
echo '[Unit]' > $rootdir/etc/systemd/system/batman.service
echo 'Description=B.A.T.M.A.N. Advanced' >> $rootdir/etc/systemd/system/batman.service
echo '' >> $rootdir/etc/systemd/system/batman.service
echo '[Service]' >> $rootdir/etc/systemd/system/batman.service
echo 'Type=oneshot' >> $rootdir/etc/systemd/system/batman.service
echo "ExecStart=/var/lib/batman start" >> $rootdir/etc/systemd/system/batman.service
echo "ExecStop=/var/lib/batman stop" >> $rootdir/etc/systemd/system/batman.service
echo 'RemainAfterExit=yes' >> $rootdir/etc/systemd/system/batman.service
echo '' >> $rootdir/etc/systemd/system/batman.service
echo '# Allow time for the server to start/stop' >> $rootdir/etc/systemd/system/batman.service
echo 'TimeoutSec=300' >> $rootdir/etc/systemd/system/batman.service
echo '' >> $rootdir/etc/systemd/system/batman.service
echo '[Install]' >> $rootdir/etc/systemd/system/batman.service
echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/batman.service
chroot "$rootdir" systemctl enable batman
}
2016-01-15 15:41:59 +01:00
mesh_firewall() {
TOX_PORT=33445
ZERONET_PORT=15441
TRACKER_PORT=6969
FIREWALL_FILENAME=$rootdir/etc/systemd/system/meshfirewall.service
MESH_FIREWALL_SCRIPT=/usr/bin/mesh-firewall
echo '#!/bin/bash' > $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -P INPUT ACCEPT' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'ip6tables -P INPUT ACCEPT' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -F' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'ip6tables -F' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -t nat -F' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'ip6tables -t nat -F' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -X' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'ip6tables -X' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -P INPUT DROP' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'ip6tables -P INPUT DROP' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -A INPUT -i lo -j ACCEPT' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo '' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo '# Make sure incoming tcp connections are SYN packets' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo '' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo '# Drop packets with incoming fragments' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -A INPUT -f -j DROP' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo '' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo '# Drop bogons' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo '' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo '# Incoming malformed NULL packets:' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo 'iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo '' >> $rootdir/$MESH_FIREWALL_SCRIPT
echo "iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT" >> $rootdir/$MESH_FIREWALL_SCRIPT
echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $ZERONET_PORT -j ACCEPT" >> $rootdir/$MESH_FIREWALL_SCRIPT
echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $ZERONET_PORT -j ACCEPT" >> $rootdir/$MESH_FIREWALL_SCRIPT
echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $TRACKER_PORT -j ACCEPT" >> $rootdir/$MESH_FIREWALL_SCRIPT
echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $TRACKER_PORT -j ACCEPT" >> $rootdir/$MESH_FIREWALL_SCRIPT
echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport 1900 -j ACCEPT" >> $rootdir/$MESH_FIREWALL_SCRIPT
chmod +x $rootdir/$MESH_FIREWALL_SCRIPT
echo '[Unit]' > $FIREWALL_FILENAME
echo 'Description=Mesh Firewall' >> $FIREWALL_FILENAME
echo '' >> $FIREWALL_FILENAME
echo '[Service]' >> $FIREWALL_FILENAME
echo 'Type=oneshot' >> $FIREWALL_FILENAME
echo "ExecStart=$MESH_FIREWALL_SCRIPT" >> $FIREWALL_FILENAME
echo 'RemainAfterExit=no' >> $FIREWALL_FILENAME
echo '' >> $FIREWALL_FILENAME
echo 'TimeoutSec=30' >> $FIREWALL_FILENAME
echo '' >> $FIREWALL_FILENAME
echo '[Install]' >> $FIREWALL_FILENAME
echo 'WantedBy=multi-user.target' >> $FIREWALL_FILENAME
chroot "$rootdir" systemctl enable meshfirewall
}
mesh_tox_node() {
TOX_REPO='git://github.com/irungentoo/toxcore.git'
TOX_COMMIT='73b2144edcfd1ca617e9054479b66ab0c0361a14'
TOX_BOOTSTRAP_ID_FILE=/var/lib/tox-bootstrapd/pubkey.txt
# These are some default nodes, but you can replace them with trusted nodes
# as you prefer. See https://wiki.tox.im/Nodes
TOX_NODES=
#TOX_NODES=(
# '192.254.75.102,2607:5600:284::2,33445,951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F,Tox RELENG,US'
# '144.76.60.215,2a01:4f8:191:64d6::1,33445,04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F,sonOfRa,DE'
#)
iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT
save_firewall_settings
2016-01-14 19:00:25 +01:00
chroot "$rootdir" apt-get -y install build-essential libtool autotools-dev
chroot "$rootdir" apt-get -y install automake checkinstall check git yasm
chroot "$rootdir" apt-get -y install libsodium13 libsodium-dev libcap2-bin
chroot "$rootdir" apt-get -y install libconfig9 libconfig-dev
git clone $TOX_REPO $rootdir/$INSTALL_DIR/toxcore
chroot "$rootdir" cd $INSTALL_DIR/toxcore
chroot "$rootdir" git checkout $TOX_COMMIT -b $TOX_COMMIT
chroot "$rootdir" autoreconf -i
chroot "$rootdir" ./configure --enable-daemon
chroot "$rootdir" make
chroot "$rootdir" make install
chroot "$rootdir" cp /usr/local/lib/libtoxcore* /usr/lib/
if [ ! -f $rootdir/usr/local/bin/tox-bootstrapd ]; then
echo $"File not found /usr/local/bin/tox-bootstrapd"
fi
chroot "$rootdir" useradd --home-dir /var/lib/tox-bootstrapd --create-home --system --shell /sbin/nologin --comment $"Account to run Tox's DHT bootstrap daemon" --user-group tox-bootstrapd
chroot "$rootdir" chmod 700 /var/lib/tox-bootstrapd
if [ ! -f $rootdir/$INSTALL_DIR/toxcore/other/bootstrap_daemon/tox-bootstrapd.conf ]; then
echo $"File not found $INSTALL_DIR/toxcore/other/bootstrap_daemon/tox-bootstrapd.conf"
fi
# remove Maildir
if [ -d $rootdir/var/lib/tox-bootstrapd/Maildir ]; then
rm -rf $rootdir/var/lib/tox-bootstrapd/Maildir
fi
# create configuration file
echo "port = $TOX_PORT" > $rootdir/etc/tox-bootstrapd.conf
echo 'keys_file_path = "/var/lib/tox-bootstrapd/keys"' >> $rootdir/etc/tox-bootstrapd.conf
echo 'pid_file_path = "/var/run/tox-bootstrapd/tox-bootstrapd.pid"' >> $rootdir/etc/tox-bootstrapd.conf
echo 'enable_ipv6 = true' >> $rootdir/etc/tox-bootstrapd.conf
echo 'enable_ipv4_fallback = true' >> $rootdir/etc/tox-bootstrapd.conf
echo 'enable_lan_discovery = true' >> $rootdir/etc/tox-bootstrapd.conf
echo 'enable_tcp_relay = true' >> $rootdir/etc/tox-bootstrapd.conf
echo "tcp_relay_ports = [443, 3389, $TOX_PORT]" >> $rootdir/etc/tox-bootstrapd.conf
echo 'enable_motd = true' >> $rootdir/etc/tox-bootstrapd.conf
echo 'motd = "tox-bootstrapd"' >> $rootdir/etc/tox-bootstrapd.conf
if [ $TOX_NODES ]; then
echo 'bootstrap_nodes = (' >> $rootdir/etc/tox-bootstrapd.conf
toxcount=0
while [ "x${TOX_NODES[toxcount]}" != "x" ]
do
toxval_ipv4=$(echo $TOX_NODES[toxcount] | awk -F ',' '{print $1}')
toxval_ipv6=$(echo $TOX_NODES[toxcount] | awk -F ',' '{print $2}')
toxval_port=$(echo $TOX_NODES[toxcount] | awk -F ',' '{print $3}')
toxval_pubkey=$(echo $TOX_NODES[toxcount] | awk -F ',' '{print $4}')
toxval_maintainer=$(echo $TOX_NODES[toxcount] | awk -F ',' '{print $5}')
echo "{ // $toxval_maintainer" >> $rootdir/etc/tox-bootstrapd.conf
if [[ $toxval_ipv6 != 'NONE' ]]; then
echo " address = \"$toxval_ipv6\"" >> $rootdir/etc/tox-bootstrapd.conf
else
echo " address = \"$toxval_ipv4\"" >> $rootdir/etc/tox-bootstrapd.conf
fi
echo " port = $toxval_port" >> $rootdir/etc/tox-bootstrapd.conf
echo " public_key = \"$toxval_pubkey\"" >> $rootdir/etc/tox-bootstrapd.conf
toxcount=$(( $toxcount + 1 ))
if [ "x${TOX_NODES[toxcount]}" != "x" ]; then
echo "}," >> $rootdir/etc/tox-bootstrapd.conf
else
echo "}" >> $rootdir/etc/tox-bootstrapd.conf
fi
done
echo ')' >> $rootdir/etc/tox-bootstrapd.conf
fi
if [ ! -f $rootdir/$INSTALL_DIR/toxcore/other/bootstrap_daemon/tox-bootstrapd.service ]; then
echo $"File not found $INSTALL_DIR/toxcore/other/bootstrap_daemon/tox-bootstrapd.service"
fi
cp $rootdir/$INSTALL_DIR/toxcore/other/bootstrap_daemon/tox-bootstrapd.service $rootdir/etc/systemd/system/
chroot "$rootdir" systemctl enable tox-bootstrapd.service
# TODO
TOX_PUBLIC_KEY=$(cat /var/log/syslog | grep tox | grep "Public Key" | awk -F ' ' '{print $8}' | tail -1)
if [ ${#TOX_PUBLIC_KEY} -lt 30 ]; then
echo $'Could not obtain the tox node public key'
exit 6529
fi
# save the public key for later reference
echo "$TOX_PUBLIC_KEY" > $TOX_BOOTSTRAP_ID_FILE
configure_firewall_for_tox
if ! grep -q $"Your Tox node public key is" /home/$MY_USERNAME/README; then
echo '' >> /home/$MY_USERNAME/README
echo '' >> /home/$MY_USERNAME/README
echo 'Tox' >> /home/$MY_USERNAME/README
echo '===' >> /home/$MY_USERNAME/README
echo $"tox onion domain: ${TOX_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
echo $"Your Tox node public key is: $TOX_PUBLIC_KEY" >> /home/$MY_USERNAME/README
echo $'In the Toxic client you can connect to it with:' >> /home/$MY_USERNAME/README
echo " /connect $DEFAULT_DOMAIN_NAME.local $TOX_PORT $TOX_PUBLIC_KEY" >> /home/$MY_USERNAME/README
chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
chmod 600 /home/$MY_USERNAME/README
fi
}
2016-01-11 12:27:08 +01:00
initialise_mesh() {
if [[ $VARIANT != "mesh" ]]; then
return
fi
2016-01-15 15:41:59 +01:00
mesh_firewall
2016-01-14 18:45:02 +01:00
mesh_avahi
2016-01-15 15:41:59 +01:00
mesh_batman
#mesh_tox_node
MESH_SERVICE='mesh-setup.service'
MESH_SETUP_DAEMON=$rootdir/etc/systemd/system/$MESH_SERVICE
echo '[Unit]' > $MESH_SETUP_DAEMON
echo 'Description=Initial mesh router configuration' >> $MESH_SETUP_DAEMON
echo 'After=syslog.target' >> $MESH_SETUP_DAEMON
echo 'After=network.target' >> $MESH_SETUP_DAEMON
echo '[Service]' >> $MESH_SETUP_DAEMON
echo 'Type=simple' >> $MESH_SETUP_DAEMON
echo 'User=root' >> $MESH_SETUP_DAEMON
echo 'Group=root' >> $MESH_SETUP_DAEMON
echo 'WorkingDirectory=/root' >> $MESH_SETUP_DAEMON
echo "ExecStart=/usr/local/bin/${PROJECT_NAME}-image-mesh > /var/log/mesh-setup.log" >> $MESH_SETUP_DAEMON
echo '' >> $MESH_SETUP_DAEMON
echo 'TimeoutSec=99999' >> $MESH_SETUP_DAEMON
echo '' >> $MESH_SETUP_DAEMON
echo '[Install]' >> $MESH_SETUP_DAEMON
echo 'WantedBy=multi-user.target' >> $MESH_SETUP_DAEMON
chroot "$rootdir" systemctl enable $MESH_SERVICE
2016-01-11 12:27:08 +01:00
}
2015-11-20 17:09:21 +01:00
# Set to true/false to control if eatmydata is used during build
use_eatmydata=true
rootdir="$1"
fmdir="$(pwd)"
image="$fmdir"/"$2"
cd "$rootdir"
echo info: building $MACHINE for $ARCHITECTURE
export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
export LC_ALL=C LANGUAGE=C LANG=C
# Override libpam-tmpdir setting during build, as the directories
# are not created yet.
export TMP=/tmp/ TMPDIR=/tmp/
2015-11-20 22:43:03 +01:00
username=$MY_USERNAME
2015-11-27 16:29:43 +01:00
echo $"warning: creating initial user $username with well known password!"
2015-11-20 22:43:03 +01:00
password=$MY_PASSWORD
2015-11-22 14:07:48 +01:00
chroot "$rootdir" adduser --gecos $username --disabled-password $username
2015-11-20 17:09:21 +01:00
echo $username:$password | chroot $rootdir /usr/sbin/chpasswd
2015-11-22 14:07:48 +01:00
chroot "$rootdir" adduser $username sudo
2015-11-20 17:09:21 +01:00
case "$MACHINE" in
virtualbox)
# hide irrelevant console keyboard messages.
echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
>> /etc/init.d/rc.local
;;
2015-11-22 23:52:59 +01:00
qemu)
# hide irrelevant console keyboard messages.
echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
>> /etc/init.d/rc.local
;;
2015-11-20 17:09:21 +01:00
esac
set_apt_sources $BUILD_MIRROR
2015-11-22 14:07:48 +01:00
chroot "$rootdir" apt-get update
2015-11-20 17:09:21 +01:00
cat > $rootdir/usr/sbin/policy-rc.d <<EOF
#!/bin/sh
exit 101
EOF
chmod a+rx $rootdir/usr/sbin/policy-rc.d
if $use_eatmydata ; then
enable_eatmydata_override
fi
if [ -n "$CUSTOM_SETUP" ]; then
cp "$CUSTOM_SETUP" "$rootdir"/tmp
chroot "$rootdir" apt-get install -y gdebi-core
chroot "$rootdir" gdebi -n /tmp/"$(basename $CUSTOM_SETUP)"
fi
2015-11-24 17:54:43 +01:00
chroot "$rootdir" apt-get install -y sudo git dialog toilet build-essential openssh-server
2015-11-25 10:51:35 +01:00
chroot "$rootdir" apt-get install -y avahi-daemon avahi-utils avahi-discover avahi-autoipd
2015-11-26 23:08:44 +01:00
chroot "$rootdir" apt-get install -y iptables dnsutils net-tools network-manager iputils-ping
2015-11-29 16:00:10 +01:00
chroot "$rootdir" apt-get install -y libnss-mdns libnss-myhostname libnss-gw-name nano man ntp
2015-12-03 16:06:12 +01:00
chroot "$rootdir" apt-get install -y locales locales-all debconf
2015-11-25 10:51:35 +01:00
2015-11-29 17:20:20 +01:00
sed -i "s|#host-name=.*|host-name=${PROJECT_NAME}|g" $rootdir/etc/avahi/avahi-daemon.conf
2015-11-22 14:07:48 +01:00
chroot "$rootdir" /bin/bash -x <<EOF
git clone https://github.com/bashrc/freedombone /root/freedombone
cd /root/freedombone
make install
EOF
2015-11-20 17:09:21 +01:00
2015-12-01 09:32:30 +01:00
chroot "$rootdir" ${PROJECT_NAME}-image-hardware-setup 2>&1 | \
tee $rootdir/var/log/${PROJECT_NAME}-image-hardware-setup.log
2015-11-20 17:09:21 +01:00
rm $rootdir/usr/sbin/policy-rc.d
2015-11-29 12:34:57 +01:00
# Set up HRNG for systems known to have one
# Otherwise install haveged
2015-11-22 14:07:48 +01:00
if [[ "$MACHINE" != "beaglebone" ]]; then
chroot $rootdir apt-get -y install haveged
else
chroot $rootdir apt-get -y install rng-tools
sed -i 's|#HRNGDEVICE=/dev/hwrng|HRNGDEVICE=/dev/hwrng|g' $rootdir/etc/default/rng-tools
fi
2015-11-20 17:09:21 +01:00
# copy u-boot to beginning of image
case "$MACHINE" in
beaglebone)
2015-11-20 22:43:03 +01:00
dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/MLO of="$image" \
count=1 seek=1 conv=notrunc bs=128k
dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/u-boot.img of="$image" \
count=2 seek=1 conv=notrunc bs=384k
;;
2015-11-20 17:09:21 +01:00
cubieboard2)
dd if=$rootdir/usr/lib/u-boot/Cubieboard2/u-boot-sunxi-with-spl.bin of="$image" \
seek=8 conv=notrunc bs=1k
;;
esac
if $use_eatmydata ; then
disable_eatmydata_override
fi
set_apt_sources $MIRROR
2015-11-22 14:07:48 +01:00
chroot "$rootdir" apt-get update
2015-11-20 17:09:21 +01:00
2015-11-21 15:33:39 +01:00
configure_ssh
2015-11-21 14:12:31 +01:00
configure_networking
2015-11-22 14:43:23 +01:00
admin_user_sudo
2015-11-23 11:33:45 +01:00
create_generic_image
2015-12-22 12:07:00 +01:00
atheros_wifi
2015-12-22 12:43:07 +01:00
continue_installation
2016-01-11 12:27:08 +01:00
initialise_mesh
2015-11-21 12:56:40 +01:00
2015-11-20 17:09:21 +01:00
cd /
2015-11-27 16:29:43 +01:00
echo $"info: killing leftover processes in chroot"
2015-11-20 17:09:21 +01:00
fuser -mvk $rootdir/. || true