#!/bin/bash # # .---. . . # | | | # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-' # ' ' --' --' -' - -' ' ' -' -' -' ' - --' # # Freedom in the Cloud # # Based on bin/freedombox-customize from freedom-maker # # License # ======= # # 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 . set -e set -x PROJECT_NAME='freedombone' # username created by default within a debian image GENERIC_IMAGE_USERNAME='fbone' export TEXTDOMAIN=${PROJECT_NAME}-image-customise export TEXTDOMAINDIR="/usr/share/locale" # Whether to minimise the number of decisions during interactive install MINIMAL_INSTALL="yes" MY_USERNAME='debian' MY_PASSWORD="${PROJECT_NAME}" # 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' # optional configuration file containing freedombone settings CONFIG_FILENAME= # Optional ssh public key to allow SSH_PUBKEY="no" # Whether this is a generic image for mass redistribution on the interwebs GENERIC_IMAGE="no" # default SSH port SSH_PORT=2222 # Whether sites are accessible only within a Tor browser ONION_ONLY="no" 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 echo $"info: Adding apt config to call dpkg via eatmydata" 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 < 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 } configure_networking() { if [[ $GENERIC_IMAGE == "no" ]]; then echo "# This file describes the network interfaces available on your system # 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 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]} sed -i "s|#hwaddress ether.*|hwaddress ether de:$a:$b:$c:$d:$e|g" \ $rootdir/etc/network/interfaces fi 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 echo $" .---. . . | | | |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. | | (.-' (.-' ( | ( )| | | | )( )| | (.-' ' ' --' --' -' - -' ' ' -' -' -' ' - --' Initial base install Your system is not yet installed. To complete the process run the following commands, then enter your details. sudo su ${PROJECT_NAME} menuconfig " > $rootdir/etc/motd } configure_ssh() { sed -i "s/Port .*/Port ${SSH_PORT}/g" $rootdir/etc/ssh/sshd_config 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 echo $"Using ssh public key:" echo $SSH_PUBKEY echo $'Password ssh authentication turned off' fi } admin_user_sudo() { echo "$MY_USERNAME ALL=(ALL) ALL" >> $rootdir/etc/sudoers } create_generic_image() { if [[ $GENERIC_IMAGE == "no" ]]; then return fi VARIANT="full" if [ $CONFIG_FILENAME ]; then if [[ "$CONFIG_FILENAME" == *"mesh.cfg"* ]]; then VARIANT="mesh" fi fi # Don't install any configuration. This will be a base system if [[ $VARIANT != "mesh" ]]; then CONFIG_FILENAME= else touch $rootdir/root/.initial_mesh_setup fi # The presence of this file indicates that the initial # setup has not yet been completed touch $rootdir/home/$MY_USERNAME/.initial_setup chroot $rootdir chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.initial_setup touch $rootdir/root/.initial_setup cat >> $rootdir/home/$MY_USERNAME/.bashrc <> $rootdir/root/.bashrc echo 'if [ -f ~/.initial_setup ]; then' >> $rootdir/root/.bashrc echo ' if [ -f ~/login.txt ]; then' >> $rootdir/root/.bashrc echo ' NEW_USER_PASSWORD=$(printf `cat ~/login.txt`)' >> $rootdir/root/.bashrc echo ' else' >> $rootdir/root/.bashrc echo ' ENTROPY=$(cat /proc/sys/kernel/random/entropy_avail)' >> $rootdir/root/.bashrc echo ' if [ $ENTROPY -lt 500 ]; then' >> $rootdir/root/.bashrc 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 echo ' exit' >> $rootdir/root/.bashrc echo ' fi' >> $rootdir/root/.bashrc echo ' NEW_USER_PASSWORD="$(openssl rand -base64 12 | cut -c1-10)"' >> $rootdir/root/.bashrc echo ' fi' >> $rootdir/root/.bashrc echo ' echo "${NEW_USER_PASSWORD}" > ~/login.txt' >> $rootdir/root/.bashrc 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 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 echo ' echo ""' >> $rootdir/root/.bashrc echo ' read -n1 -r -p "Press any key to continue..." key' >> $rootdir/root/.bashrc # change the password for the admin user echo -n " echo \"${MY_USERNAME}:" >> $rootdir/root/.bashrc echo '$(printf `cat ~/login.txt`)"|chpasswd' >> $rootdir/root/.bashrc # 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 if [[ $VARIANT != "mesh" ]]; then 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 else echo " ${PROJECT_NAME} menuconfig-onion" >> $rootdir/root/.bashrc fi else echo ' echo ""' fi echo ' if [ "$?" = "0" ]; then' >> $rootdir/root/.bashrc echo " if [ -f ~/${PROJECT_NAME}-completed.txt ]; then" >> $rootdir/root/.bashrc # Remove the initial setup files echo ' rm /root/.initial_setup' >> $rootdir/root/.bashrc echo " rm /home/${MY_USERNAME}/.initial_setup" >> $rootdir/root/.bashrc echo " touch /root/.remove_${GENERIC_IMAGE_USERNAME}" >> $rootdir/root/.bashrc echo ' shred -zu ~/login.txt' >> $rootdir/root/.bashrc 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 echo ' reboot' >> $rootdir/root/.bashrc echo ' fi' >> $rootdir/root/.bashrc echo ' else' >> $rootdir/root/.bashrc 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 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 } continue_installation() { # 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 cat $rootdir/root/$PROJECT_NAME.cfg chroot "$rootdir" su -c "$PROJECT_NAME -c /root/$PROJECT_NAME.cfg" - root fi fi } 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 } 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 '' > $rootdir/etc/avahi/services/ssh.service echo '' >> $rootdir/etc/avahi/services/ssh.service echo '' >> $rootdir/etc/avahi/services/ssh.service echo ' %h SSH' >> $rootdir/etc/avahi/services/ssh.service echo ' ' >> $rootdir/etc/avahi/services/ssh.service echo ' _ssh._tcp' >> $rootdir/etc/avahi/services/ssh.service echo " $SSH_PORT" >> $rootdir/etc/avahi/services/ssh.service echo ' ' >> $rootdir/etc/avahi/services/ssh.service echo '' >> $rootdir/etc/avahi/services/ssh.service # keep the daemon running WATCHDOG_SCRIPT_NAME="keepon" 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 chmod +x $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME } function mesh_batman { 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 if [ -f /usr/local/bin/${PROJECT_NAME}-mesh-batman ]; then cp /usr/local/bin/${PROJECT_NAME}-mesh-batman $rootdir/var/lib/batman else cp /usr/bin/${PROJECT_NAME}-mesh-batman $rootdir/var/lib/batman fi 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 } initialise_mesh() { if [[ $VARIANT != "mesh" ]]; then return fi mesh_avahi mesh_batman #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 } # 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/ username=$MY_USERNAME echo $"warning: creating initial user $username with well known password!" password=$MY_PASSWORD chroot "$rootdir" adduser --gecos $username --disabled-password $username echo $username:$password | chroot $rootdir /usr/sbin/chpasswd chroot "$rootdir" adduser $username sudo case "$MACHINE" in virtualbox) # hide irrelevant console keyboard messages. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \ >> /etc/init.d/rc.local ;; qemu) # hide irrelevant console keyboard messages. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \ >> /etc/init.d/rc.local ;; esac set_apt_sources $BUILD_MIRROR chroot "$rootdir" apt-get update cat > $rootdir/usr/sbin/policy-rc.d <&1 | \ tee $rootdir/var/log/${PROJECT_NAME}-image-hardware-setup.log rm $rootdir/usr/sbin/policy-rc.d # Set up HRNG for systems known to have one # Otherwise install haveged 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 # copy u-boot to beginning of image case "$MACHINE" in beaglebone) 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 ;; 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 chroot "$rootdir" apt-get update configure_ssh configure_networking admin_user_sudo create_generic_image atheros_wifi continue_installation initialise_mesh cd / echo $"info: killing leftover processes in chroot" fuser -mvk $rootdir/. || true