freedombone/src/freedombone-utils-avahi

199 lines
8.5 KiB
Plaintext
Raw Permalink Normal View History

2016-07-03 17:13:34 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-07-03 17:13:34 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-07-03 17:13:34 +02:00
#
# Avahi functions
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
2016-07-03 17:13:34 +02:00
#
# 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 {
2016-09-07 23:21:42 +02:00
service_name=$1
service_type=$2
service_protocol=$3
service_port=$4
service_description="$5"
2016-07-03 17:13:34 +02:00
2016-09-07 23:21:42 +02:00
if [ ! -d /etc/avahi ]; then
echo $'create_avahi_service: avahi was not installed'
exit 52925
fi
2016-07-03 17:13:34 +02:00
2018-02-25 13:50:46 +01:00
{ 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"
2016-09-07 23:21:42 +02:00
if [ "$service_description" ]; then
2018-02-25 13:50:46 +01:00
echo " <txt-record>$service_description</txt-record>" >> "/etc/avahi/services/${service_name}.service"
2016-09-07 23:21:42 +02:00
fi
2018-02-25 13:50:46 +01:00
{ echo ' </service>';
echo '</service-group>'; } >> "/etc/avahi/services/${service_name}.service"
2016-07-03 17:13:34 +02:00
}
2016-07-12 09:27:11 +02:00
function mesh_avahi {
2018-02-25 14:49:17 +01:00
# shellcheck disable=SC2154
2018-01-15 13:29:06 +01:00
chroot "$rootdir" apt-get -yq install avahi-utils avahi-dnsconfd
2016-07-12 09:27:11 +02:00
2016-09-07 23:21:42 +02:00
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]}
2018-02-25 13:50:46 +01:00
sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" "$rootdir/etc/avahi/avahi-daemon.conf"
2018-04-08 15:23:26 +02:00
sed -i "s|host-name=.*|host-name=P$PEER_ID|g" "$rootdir/etc/avahi/avahi-daemon.conf"
sed -i 's|#domain-name=.*|domain-name=local|g' "$rootdir/etc/avahi/avahi-daemon.conf"
sed -i 's|domain-name=.*|domain-name=local|g' "$rootdir/etc/avahi/avahi-daemon.conf"
2018-02-25 13:50:46 +01:00
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"
# Note: wlan interfaces must be allowed within avahi, otherwise the
# *.local address will not resolve
sed -i "s|#allow-interfaces|allow-interfaces|g" "$rootdir/etc/avahi/avahi-daemon.conf"
sed -i "s|allow-interfaces=.*|allow-interfaces=wlan0, wlan1, wlan2, wlan3, wlan4, wlan5, eth0, eth1|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"
2018-02-25 13:50:46 +01:00
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"
2016-09-07 23:21:42 +02:00
fi
2016-07-12 09:27:11 +02:00
2016-09-07 23:21:42 +02:00
# remove an avahi service which isn't used
2018-02-25 13:50:46 +01:00
if [ -f "$rootdir/etc/avahi/services/udisks.service" ]; then
rm "$rootdir/etc/avahi/services/udisks.service"
2016-09-07 23:21:42 +02:00
fi
2016-07-12 09:27:11 +02:00
2018-01-16 23:54:03 +01:00
# Add a mesh routing protocol service
2018-02-25 13:50:46 +01:00
{ 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"
2016-07-12 09:27:11 +02:00
2016-09-07 23:21:42 +02:00
# keep the daemon running
WATCHDOG_SCRIPT_NAME="keepon"
2018-02-25 13:50:46 +01:00
{ echo '';
echo '# keep avahi daemon running';
echo "AVAHI_RUNNING=\$(systemctl is-active avahi-daemon >/dev/null 2>&1 && echo Running)";
2018-02-25 13:50:46 +01:00
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"
2016-07-12 09:27:11 +02:00
}
2018-05-27 12:36:17 +02:00
function keep_avahi_running {
WATCHDOG_SCRIPT_NAME="keepon"
2018-05-27 12:39:59 +02:00
if grep -q "avahi daemon" "/usr/bin/$WATCHDOG_SCRIPT_NAME"; then
return
2018-05-27 12:36:17 +02:00
fi
2018-05-27 12:39:59 +02:00
{ echo '';
echo '# keep avahi daemon running';
echo "AVAHI_RUNNING=\$(systemctl is-active avahi-daemon >/dev/null 2>&1 && 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';
echo '# End of avahi'; } >> "/usr/bin/$WATCHDOG_SCRIPT_NAME"
chmod +x "/usr/bin/$WATCHDOG_SCRIPT_NAME"
# This is a bit of a hack to ensure that avahi services get broadcast
# Without this some other systems may not be able to see the server
{ echo '#!/bin/bash';
echo 'systemctl restart avahi-daemon'; } > /usr/bin/restartavahi
chmod +x /usr/bin/restartavahi
cron_add_mins 10 /usr/bin/restartavahi
2018-05-27 12:36:17 +02:00
}
2016-07-12 11:55:18 +02:00
function install_avahi {
2018-02-25 13:50:46 +01:00
if [ "$INSTALLING_MESH" ]; then
2016-09-07 23:21:42 +02:00
mesh_avahi
return
fi
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
2016-09-07 23:21:42 +02:00
return
fi
# only enable avahi if we're doing mesh networking
if [[ $ENABLE_BATMAN != "yes" ]]; then
2016-09-07 23:21:42 +02:00
return
fi
2016-07-03 17:13:34 +02:00
2018-02-25 14:49:17 +01:00
if ! "${PROJECT_NAME}-mesh-install" -f avahi; then
2016-09-07 23:21:42 +02:00
echo $'Failed to install avahi'
exit 68442
fi
2016-07-03 17:13:34 +02:00
2018-02-25 14:49:17 +01:00
if [ "$DEFAULT_DOMAIN_NAME" ]; then
2017-06-22 15:18:15 +02:00
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
2016-09-07 23:21:42 +02:00
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
2018-04-08 15:23:26 +02:00
sed -i 's|#domain-name=.*|domain-name=local|g' /etc/avahi/avahi-daemon.conf
sed -i 's|domain-name=.*|domain-name=local|g' /etc/avahi/avahi-daemon.conf
2016-07-03 17:13:34 +02:00
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
2016-07-03 17:13:34 +02:00
}
function configure_firewall_for_avahi {
2018-02-25 13:50:46 +01:00
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
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
}
2016-07-03 17:13:34 +02:00
# NOTE: deliberately there is no "exit 0"