freedombone-utils-avahi 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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-2016 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-*-->' > /etc/avahi/services/${service_name}.service
  41. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">' >> /etc/avahi/services/${service_name}.service
  42. echo '<service-group>' >> /etc/avahi/services/${service_name}.service
  43. echo " <name replace-wildcards=\"yes\">%h ${service_type}</name>" >> /etc/avahi/services/${service_name}.service
  44. echo ' <service>' >> /etc/avahi/services/${service_name}.service
  45. echo " <type>_${service_type}._${service_protocol}</type>" >> /etc/avahi/services/${service_name}.service
  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>' >> /etc/avahi/services/${service_name}.service
  51. echo '</service-group>' >> /etc/avahi/services/${service_name}.service
  52. }
  53. function mesh_avahi {
  54. chroot "$rootdir" apt-get -yq install avahi-utils avahi-dnsconfd
  55. decarray=( 1 2 3 4 5 6 7 8 9 0 )
  56. 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]}
  57. sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" $rootdir/etc/avahi/avahi-daemon.conf
  58. sed -i "s|use-ipv4=.*|use-ipv4=no|g" $rootdir/etc/avahi/avahi-daemon.conf
  59. sed -i "s|use-ipv6=.*|use-ipv6=yes|g" $rootdir/etc/avahi/avahi-daemon.conf
  60. sed -i "s|#allow-interfaces=.*|allow-interfaces=wlan0, wlan1, wlan2, wlan3, wlan4, wlan5|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|#deny-interfaces=.*|deny-interfaces=eth0, eth1, eth2, eth3, eth4, eth5|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|#disallow-other-stacks=.*|disallow-other-stacks=yes|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|#publish_addresses=.*|publish_addresses=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-hinfo=.*|publish-hinfo=no|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-workstation=.*|publish-workstation=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-domain=.*|publish-domain=yes|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-a-on-ipv6=.*|publish-a-on-ipv6=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. if [ ! -d $rootdir/etc/avahi/services ]; then
  77. mkdir -p $rootdir/etc/avahi/services
  78. fi
  79. # remove an avahi service which isn't used
  80. if [ -f $rootdir/etc/avahi/services/udisks.service ]; then
  81. rm $rootdir/etc/avahi/services/udisks.service
  82. fi
  83. # Add a mesh routing protocol service
  84. echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->' > $rootdir/etc/avahi/services/routing.service
  85. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">' >> $rootdir/etc/avahi/services/routing.service
  86. echo '<service-group>' >> $rootdir/etc/avahi/services/routing.service
  87. echo ' <name replace-wildcards="yes">%h routing</name>' >> $rootdir/etc/avahi/services/routing.service
  88. echo ' <service>' >> $rootdir/etc/avahi/services/routing.service
  89. echo ' <type>_bmx6._tcp</type>' >> $rootdir/etc/avahi/services/routing.service
  90. echo ' </service>' >> $rootdir/etc/avahi/services/routing.service
  91. echo '</service-group>' >> $rootdir/etc/avahi/services/routing.service
  92. # keep the daemon running
  93. WATCHDOG_SCRIPT_NAME="keepon"
  94. echo '' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  95. echo '# keep avahi daemon running' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  96. echo 'AVAHI_RUNNING=$(pgrep avahi-daemon > /dev/null && echo Running)' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  97. echo 'if [ ! $AVAHI_RUNNING ]; then' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  98. echo ' systemctl start avahi-daemon' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  99. echo ' echo -n $CURRENT_DATE >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  100. echo ' echo " Avahi daemon restarted" >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  101. echo 'fi' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  102. chmod +x $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  103. }
  104. function install_avahi {
  105. if [ $INSTALLING_MESH ]; then
  106. mesh_avahi
  107. return
  108. fi
  109. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  110. return
  111. fi
  112. # only enable avahi if we're doing mesh networking
  113. if [[ $ENABLE_BATMAN != "yes" ]]; then
  114. return
  115. fi
  116. ${PROJECT_NAME}-mesh-install -f avahi
  117. if [ ! "$?" = "0" ]; 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
  131. }
  132. function configure_firewall_for_avahi {
  133. if [[ $(is_completed $FUNCNAME) == "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
  145. }
  146. # NOTE: deliberately there is no "exit 0"