freedombone-utils-avahi 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Avahi functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. function create_avahi_service {
  29. service_name=$1
  30. service_type=$2
  31. service_protocol=$3
  32. service_port=$4
  33. service_description="$5"
  34. if [ ! -d /etc/avahi ]; then
  35. echo $'create_avahi_service: avahi was not installed'
  36. exit 52925
  37. fi
  38. { echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
  39. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
  40. echo '<service-group>';
  41. echo " <name replace-wildcards=\"yes\">%h ${service_type}</name>";
  42. echo ' <service>';
  43. echo " <type>_${service_type}._${service_protocol}</type>";
  44. echo " <port>${service_port}</port>"; } > "/etc/avahi/services/${service_name}.service"
  45. if [ "$service_description" ]; then
  46. echo " <txt-record>$service_description</txt-record>" >> "/etc/avahi/services/${service_name}.service"
  47. fi
  48. { echo ' </service>';
  49. echo '</service-group>'; } >> "/etc/avahi/services/${service_name}.service"
  50. }
  51. function mesh_avahi {
  52. # shellcheck disable=SC2154
  53. chroot "$rootdir" apt-get -yq install avahi-utils avahi-dnsconfd
  54. decarray=( 1 2 3 4 5 6 7 8 9 0 )
  55. 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]}
  56. sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  57. sed -i "s|host-name=.*|host-name=P$PEER_ID|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  58. sed -i 's|#domain-name=.*|domain-name=local|g' "$rootdir/etc/avahi/avahi-daemon.conf"
  59. sed -i 's|domain-name=.*|domain-name=local|g' "$rootdir/etc/avahi/avahi-daemon.conf"
  60. sed -i "s|use-ipv4=.*|use-ipv4=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  61. sed -i "s|use-ipv6=.*|use-ipv6=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  62. # Note: wlan interfaces must be allowed within avahi, otherwise the
  63. # *.local address will not resolve
  64. sed -i "s|#allow-interfaces|allow-interfaces|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  65. sed -i "s|allow-interfaces=.*|allow-interfaces=wlan0, wlan1, wlan2, wlan3, wlan4, wlan5, eth0, eth1|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  66. #sed -i "s|#deny-interfaces=.*|deny-interfaces=eth0, eth1, eth2, eth3, eth4, eth5|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  67. #sed -i "s|deny-interfaces=.*|deny-interfaces=eth0, eth1, eth2, eth3, eth4, eth5|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  68. sed -i "s|#disallow-other-stacks=.*|disallow-other-stacks=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  69. sed -i "s|disallow-other-stacks=.*|disallow-other-stacks=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  70. sed -i "s|#publish_addresses=.*|publish_addresses=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  71. sed -i "s|publish_addresses=.*|publish_addresses=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  72. sed -i "s|#publish-hinfo=.*|publish-hinfo=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  73. sed -i "s|publish-hinfo=.*|publish-hinfo=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  74. sed -i "s|#publish-workstation=.*|publish-workstation=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  75. sed -i "s|publish-workstation=.*|publish-workstation=no|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  76. sed -i "s|#publish-domain=.*|publish-domain=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  77. sed -i "s|publish-domain=.*|publish-domain=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  78. sed -i "s|#publish-a-on-ipv6=.*|publish-a-on-ipv6=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  79. sed -i "s|publish-a-on-ipv6=.*|publish-a-on-ipv6=yes|g" "$rootdir/etc/avahi/avahi-daemon.conf"
  80. if [ ! -d "$rootdir/etc/avahi/services" ]; then
  81. mkdir -p "$rootdir/etc/avahi/services"
  82. fi
  83. # remove an avahi service which isn't used
  84. if [ -f "$rootdir/etc/avahi/services/udisks.service" ]; then
  85. rm "$rootdir/etc/avahi/services/udisks.service"
  86. fi
  87. # Add a mesh routing protocol service
  88. { echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
  89. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
  90. echo '<service-group>';
  91. echo ' <name replace-wildcards="yes">%h routing</name>';
  92. echo ' <service>';
  93. echo ' <type>_bmx6._tcp</type>';
  94. echo ' </service>';
  95. echo '</service-group>'; } > "$rootdir/etc/avahi/services/routing.service"
  96. # keep the daemon running
  97. WATCHDOG_SCRIPT_NAME="keepon"
  98. { echo '';
  99. echo '# keep avahi daemon running';
  100. echo "AVAHI_RUNNING=\$(pgrep avahi-daemon > /dev/null && echo Running)";
  101. echo "if [ ! \$AVAHI_RUNNING ]; then";
  102. echo ' systemctl start avahi-daemon';
  103. echo " echo -n \$CURRENT_DATE >> \$LOGFILE";
  104. echo " echo \" Avahi daemon restarted\" >> \$LOGFILE";
  105. echo 'fi'; } >> "$rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME"
  106. chmod +x "$rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME"
  107. }
  108. function install_avahi {
  109. if [ "$INSTALLING_MESH" ]; then
  110. mesh_avahi
  111. return
  112. fi
  113. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  114. return
  115. fi
  116. # only enable avahi if we're doing mesh networking
  117. if [[ $ENABLE_BATMAN != "yes" ]]; then
  118. return
  119. fi
  120. if ! "${PROJECT_NAME}-mesh-install" -f avahi; then
  121. echo $'Failed to install avahi'
  122. exit 68442
  123. fi
  124. if [ "$DEFAULT_DOMAIN_NAME" ]; then
  125. sed -i "s|#host-name=.*|host-name=$LOCAL_NAME|g" /etc/avahi/avahi-daemon.conf
  126. sed -i "s|host-name=.*|host-name=$LOCAL_NAME|g" /etc/avahi/avahi-daemon.conf
  127. else
  128. decarray=( 1 2 3 4 5 6 7 8 9 0 )
  129. 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]}
  130. sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
  131. sed -i "s|host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
  132. fi
  133. sed -i 's|#domain-name=.*|domain-name=local|g' /etc/avahi/avahi-daemon.conf
  134. sed -i 's|domain-name=.*|domain-name=local|g' /etc/avahi/avahi-daemon.conf
  135. mark_completed "${FUNCNAME[0]}"
  136. }
  137. function configure_firewall_for_avahi {
  138. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  139. return
  140. fi
  141. iptables -A INPUT -p tcp --dport 548 -j ACCEPT
  142. iptables -A INPUT -p udp --dport 548 -j ACCEPT
  143. iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
  144. iptables -A INPUT -p udp --dport 5353 -j ACCEPT
  145. iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
  146. iptables -A INPUT -p udp --dport 5354 -j ACCEPT
  147. function_check save_firewall_settings
  148. save_firewall_settings
  149. mark_completed "${FUNCNAME[0]}"
  150. }
  151. # NOTE: deliberately there is no "exit 0"