freedombone-utils-avahi 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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-autoipd 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. if [ ! -d $rootdir/etc/avahi/services ]; then
  59. mkdir -p $rootdir/etc/avahi/services
  60. fi
  61. # remove an avahi service which isn't used
  62. if [ -f $rootdir/etc/avahi/services/udisks.service ]; then
  63. rm $rootdir/etc/avahi/services/udisks.service
  64. fi
  65. # Add an ssh service
  66. echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->' > $rootdir/etc/avahi/services/ssh.service
  67. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">' >> $rootdir/etc/avahi/services/ssh.service
  68. echo '<service-group>' >> $rootdir/etc/avahi/services/ssh.service
  69. echo ' <name replace-wildcards="yes">%h SSH</name>' >> $rootdir/etc/avahi/services/ssh.service
  70. echo ' <service>' >> $rootdir/etc/avahi/services/ssh.service
  71. echo ' <type>_ssh._tcp</type>' >> $rootdir/etc/avahi/services/ssh.service
  72. echo " <port>$SSH_PORT</port>" >> $rootdir/etc/avahi/services/ssh.service
  73. echo ' </service>' >> $rootdir/etc/avahi/services/ssh.service
  74. echo '</service-group>' >> $rootdir/etc/avahi/services/ssh.service
  75. # keep the daemon running
  76. WATCHDOG_SCRIPT_NAME="keepon"
  77. echo '' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  78. echo '# keep avahi daemon running' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  79. echo 'AVAHI_RUNNING=$(pgrep avahi-daemon > /dev/null && echo Running)' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  80. echo 'if [ ! $AVAHI_RUNNING ]; then' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  81. echo ' systemctl start avahi-daemon' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  82. echo ' echo -n $CURRENT_DATE >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  83. echo ' echo " Avahi daemon restarted" >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  84. echo 'fi' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  85. chmod +x $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  86. }
  87. function install_avahi {
  88. if [ $INSTALLING_MESH ]; then
  89. mesh_avahi
  90. return
  91. fi
  92. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  93. return
  94. fi
  95. # only enable avahi if we're doing mesh networking
  96. if [[ $ENABLE_BATMAN != "yes" ]]; then
  97. return
  98. fi
  99. ${PROJECT_NAME}-mesh-install -f avahi
  100. if [ ! "$?" = "0" ]; then
  101. echo $'Failed to install avahi'
  102. exit 68442
  103. fi
  104. if [ $DEFAULT_DOMAIN_NAME ]; then
  105. sed -i "s|#host-name=.*|host-name=$LOCAL_NAME|g" /etc/avahi/avahi-daemon.conf
  106. sed -i "s|host-name=.*|host-name=$LOCAL_NAME|g" /etc/avahi/avahi-daemon.conf
  107. else
  108. decarray=( 1 2 3 4 5 6 7 8 9 0 )
  109. 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]}
  110. sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
  111. sed -i "s|host-name=.*|host-name=P$PEER_ID|g" /etc/avahi/avahi-daemon.conf
  112. fi
  113. mark_completed $FUNCNAME
  114. }
  115. function configure_firewall_for_avahi {
  116. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  117. return
  118. fi
  119. iptables -A INPUT -p tcp --dport 548 -j ACCEPT
  120. iptables -A INPUT -p udp --dport 548 -j ACCEPT
  121. iptables -A INPUT -p tcp --dport 5353 -j ACCEPT
  122. iptables -A INPUT -p udp --dport 5353 -j ACCEPT
  123. iptables -A INPUT -p tcp --dport 5354 -j ACCEPT
  124. iptables -A INPUT -p udp --dport 5354 -j ACCEPT
  125. function_check save_firewall_settings
  126. save_firewall_settings
  127. mark_completed $FUNCNAME
  128. }
  129. # NOTE: deliberately there is no "exit 0"