freedombone-utils-avahi 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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=\$(systemctl is-active avahi-daemon >/dev/null 2>&1 && 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 keep_avahi_running {
  109. WATCHDOG_SCRIPT_NAME="keepon"
  110. if grep -q "avahi daemon" "/usr/bin/$WATCHDOG_SCRIPT_NAME"; then
  111. return
  112. fi
  113. { echo '';
  114. echo '# keep avahi daemon running';
  115. echo "AVAHI_RUNNING=\$(systemctl is-active avahi-daemon >/dev/null 2>&1 && echo Running)";
  116. echo "if [ ! \$AVAHI_RUNNING ]; then";
  117. echo ' systemctl start avahi-daemon';
  118. echo " echo -n \$CURRENT_DATE >> \$LOGFILE";
  119. echo " echo \" Avahi daemon restarted\" >> \$LOGFILE";
  120. echo 'fi';
  121. echo '# End of avahi'; } >> "/usr/bin/$WATCHDOG_SCRIPT_NAME"
  122. chmod +x "/usr/bin/$WATCHDOG_SCRIPT_NAME"
  123. # This is a bit of a hack to ensure that avahi services get broadcast
  124. # Without this some other systems may not be able to see the server
  125. { echo '#!/bin/bash';
  126. echo 'systemctl restart avahi-daemon'; } > /usr/bin/restartavahi
  127. chmod +x /usr/bin/restartavahi
  128. cron_add_mins 10 /usr/bin/restartavahi
  129. }
  130. function install_avahi {
  131. if [ "$INSTALLING_MESH" ]; then
  132. mesh_avahi
  133. return
  134. fi
  135. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  136. return
  137. fi
  138. # only enable avahi if we're doing mesh networking
  139. if [[ $ENABLE_BATMAN != "yes" ]]; then
  140. return
  141. fi
  142. if ! "${PROJECT_NAME}-mesh-install" -f avahi; then
  143. echo $'Failed to install avahi'
  144. exit 68442
  145. fi
  146. if [ "$DEFAULT_DOMAIN_NAME" ]; then
  147. sed -i "s|#host-name=.*|host-name=$LOCAL_NAME|g" /etc/avahi/avahi-daemon.conf
  148. sed -i "s|host-name=.*|host-name=$LOCAL_NAME|g" /etc/avahi/avahi-daemon.conf
  149. else
  150. decarray=( 1 2 3 4 5 6 7 8 9 0 )
  151. 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]}
  152. sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
  153. sed -i "s|host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
  154. fi
  155. sed -i 's|#domain-name=.*|domain-name=local|g' /etc/avahi/avahi-daemon.conf
  156. sed -i 's|domain-name=.*|domain-name=local|g' /etc/avahi/avahi-daemon.conf
  157. mark_completed "${FUNCNAME[0]}"
  158. }
  159. function configure_firewall_for_avahi {
  160. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  161. return
  162. fi
  163. iptables -A INPUT -p tcp --dport 548 -j ACCEPT
  164. iptables -A INPUT -p udp --dport 548 -j ACCEPT
  165. iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
  166. iptables -A INPUT -p udp --dport 5353 -j ACCEPT
  167. iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
  168. iptables -A INPUT -p udp --dport 5354 -j ACCEPT
  169. function_check save_firewall_settings
  170. save_firewall_settings
  171. mark_completed "${FUNCNAME[0]}"
  172. }
  173. # NOTE: deliberately there is no "exit 0"