freedombone-utils-avahi 7.1KB

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