| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 | #!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Avahi functions
#
# License
# =======
#
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
function create_avahi_service {
    service_name=$1
    service_type=$2
    service_protocol=$3
    service_port=$4
    service_description="$5"
    if [ ! -d /etc/avahi ]; then
        echo $'create_avahi_service: avahi was not installed'
        exit 52925
    fi
    { echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
      echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
      echo '<service-group>';
      echo "  <name replace-wildcards=\"yes\">%h ${service_type}</name>";
      echo '  <service>';
      echo "    <type>_${service_type}._${service_protocol}</type>";
      echo "    <port>${service_port}</port>"; }  > "/etc/avahi/services/${service_name}.service"
    if [ "$service_description" ]; then
        echo "    <txt-record>$service_description</txt-record>" >> "/etc/avahi/services/${service_name}.service"
    fi
    { echo '  </service>';
      echo '</service-group>'; } >> "/etc/avahi/services/${service_name}.service"
}
function mesh_avahi {
    # shellcheck disable=SC2154
    chroot "$rootdir" apt-get -yq install avahi-utils 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"
    sed -i "s|use-ipv4=.*|use-ipv4=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|use-ipv6=.*|use-ipv6=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    #sed -i "s|#allow-interfaces=.*|allow-interfaces=wlan0, wlan1, wlan2, wlan3, wlan4, wlan5|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    #sed -i "s|allow-interfaces=.*|allow-interfaces=wlan0, wlan1, wlan2, wlan3, wlan4, wlan5|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    #sed -i "s|#deny-interfaces=.*|deny-interfaces=eth0, eth1, eth2, eth3, eth4, eth5|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    #sed -i "s|deny-interfaces=.*|deny-interfaces=eth0, eth1, eth2, eth3, eth4, eth5|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|disallow-other-stacks=.*|disallow-other-stacks=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|#publish_addresses=.*|publish_addresses=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|publish_addresses=.*|publish_addresses=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|#publish-hinfo=.*|publish-hinfo=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|publish-hinfo=.*|publish-hinfo=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|#publish-workstation=.*|publish-workstation=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|publish-workstation=.*|publish-workstation=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|#publish-domain=.*|publish-domain=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|publish-domain=.*|publish-domain=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|#publish-a-on-ipv6=.*|publish-a-on-ipv6=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
    sed -i "s|publish-a-on-ipv6=.*|publish-a-on-ipv6=yes|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 a mesh routing protocol service
    { echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
      echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
      echo '<service-group>';
      echo '  <name replace-wildcards="yes">%h routing</name>';
      echo '  <service>';
      echo '    <type>_bmx6._tcp</type>';
      echo '  </service>';
      echo '</service-group>'; } > "$rootdir/etc/avahi/services/routing.service"
    # keep the daemon running
    WATCHDOG_SCRIPT_NAME="keepon"
    { echo '';
      echo '# keep avahi daemon running';
      echo "AVAHI_RUNNING=\$(pgrep avahi-daemon > /dev/null && echo Running)";
      echo "if [ ! \$AVAHI_RUNNING ]; then";
      echo '  systemctl start avahi-daemon';
      echo "  echo -n \$CURRENT_DATE >> \$LOGFILE";
      echo "  echo \" Avahi daemon restarted\" >> \$LOGFILE";
      echo 'fi'; } >> "$rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME"
    chmod +x "$rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME"
}
function install_avahi {
    if [ "$INSTALLING_MESH" ]; then
        mesh_avahi
        return
    fi
    if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
        return
    fi
    # only enable avahi if we're doing mesh networking
    if [[ $ENABLE_BATMAN != "yes" ]]; then
        return
    fi
    if ! "${PROJECT_NAME}-mesh-install" -f avahi; then
        echo $'Failed to install avahi'
        exit 68442
    fi
    if [ "$DEFAULT_DOMAIN_NAME" ]; then
        sed -i "s|#host-name=.*|host-name=$LOCAL_NAME|g" /etc/avahi/avahi-daemon.conf
        sed -i "s|host-name=.*|host-name=$LOCAL_NAME|g" /etc/avahi/avahi-daemon.conf
    else
        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" /etc/avahi/avahi-daemon.conf
        sed -i "s|host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
    fi
    mark_completed "${FUNCNAME[0]}"
}
function configure_firewall_for_avahi {
    if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
        return
    fi
    iptables -A INPUT -p tcp --dport 548 -j ACCEPT
    iptables -A INPUT -p udp --dport 548 -j ACCEPT
    iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
    iptables -A INPUT -p udp --dport 5353 -j ACCEPT
    iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
    iptables -A INPUT -p udp --dport 5354 -j ACCEPT
    function_check save_firewall_settings
    save_firewall_settings
    mark_completed "${FUNCNAME[0]}"
}
# NOTE: deliberately there is no "exit 0"
 |