/contrib/openresolv/dnsmasq.in

https://bitbucket.org/freebsd/freebsd-head/ · Autoconf · 127 lines · 85 code · 9 blank · 33 comment · 27 complexity · afaf13977dd7cf4ac5ff7fac31e7d6d6 MD5 · raw file

  1. #!/bin/sh
  2. # Copyright (c) 2007-2009 Roy Marples
  3. # All rights reserved
  4. # dnsmasq subscriber for resolvconf
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following
  12. # disclaimer in the documentation and/or other materials provided
  13. # with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. [ -f "@SYSCONFDIR@"/resolvconf.conf ] || exit 0
  27. . "@SYSCONFDIR@/resolvconf.conf" || exit 1
  28. [ -z "$dnsmasq_conf" -a -z "$dnsmasq_resolv" ] && exit 0
  29. [ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
  30. NL="
  31. "
  32. : ${dnsmasq_pid:=/var/run/dnsmasq.pid}
  33. [ -s "$dnsmasq_pid" ] || dnsmasq_pid=/var/run/dnsmasq/dnsmasq.pid
  34. : ${dnsmasq_service:=dnsmasq}
  35. : ${dnsmasq_restart:=@RESTARTCMD ${dnsmasq_service}@}
  36. newconf="# Generated by resolvconf$NL"
  37. newresolv="$newconf"
  38. # Using dbus means that we never have to restart the daemon
  39. # This is important as it means we should not drop DNS queries
  40. # whilst changing DNS options around. However, dbus support is optional
  41. # so we need to validate a few things first.
  42. # Check for DBus support in the binary
  43. dbus=false
  44. : ${dbus_pid:=/var/run/dbus/dbus.pid}
  45. [ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus.pid
  46. [ -s "$dbus_pid" ] || dbus_pid=/var/run/dbus/pid
  47. if [ -s "$dbus_pid" -a -s "$dnsmasq_pid" ]; then
  48. if dnsmasq --version 2>/dev/null | \
  49. grep -q "^Compile time options.*[[:space:]]DBus[[:space:]]"
  50. then
  51. # Sanity - check that dnsmasq and dbus are running
  52. if kill -0 $(cat "$dbus_pid") 2>/dev/null && \
  53. kill -0 $(cat "$dnsmasq_pid") 2>/dev/null
  54. then
  55. dbus=true
  56. newconf="$newconf$NL# Domain specific servers will"
  57. newconf="$newconf be sent over dbus${NL}enable-dbus$NL"
  58. fi
  59. fi
  60. fi
  61. for n in $NAMESERVERS; do
  62. newresolv="${newresolv}nameserver $n$NL"
  63. done
  64. dbusdest=
  65. for d in $DOMAINS; do
  66. dn="${d%%:*}"
  67. ns="${d#*:}"
  68. while [ -n "$ns" ]; do
  69. if $dbus; then
  70. SIFS=${IFS-y} OIFS=$IFS
  71. IFS=.
  72. set -- ${ns%%,*}
  73. num="0x$(printf %02x $1 $2 $3 $4)"
  74. if [ "$SIFS" = yi ]; then
  75. unset IFS
  76. else
  77. IFS=$OIFS
  78. fi
  79. dbusdest="$dbusdest uint32:$(printf %u $num)"
  80. dbusdest="$dbusdest string:$dn"
  81. else
  82. newconf="${newconf}server=/$dn/${ns%%,*}$NL"
  83. fi
  84. [ "$ns" = "${ns#*,}" ] && break
  85. ns="${ns#*,}"
  86. done
  87. done
  88. changed=false
  89. if [ -n "$dnsmasq_conf" ]; then
  90. if [ ! -f "$dnsmasq_conf" ] || \
  91. [ "$(cat "$dnsmasq_conf")" != "$(printf %s "$newconf")" ]
  92. then
  93. changed=true
  94. printf %s "$newconf" >"$dnsmasq_conf"
  95. fi
  96. fi
  97. if [ -n "$dnsmasq_resolv" ]; then
  98. if [ -f "$dnsmasq_resolv" ]; then
  99. if [ "$(cat "$dnsmasq_resolv")" != "$(printf %s "$newresolv")" ]
  100. then
  101. changed=true
  102. printf %s "$newresolv" >"$dnsmasq_resolv"
  103. fi
  104. else
  105. # dnsmasq polls this file so no need to set changed=true
  106. printf %s "$newresolv" >"$dnsmasq_resolv"
  107. fi
  108. fi
  109. if $changed; then
  110. eval $dnsmasq_restart
  111. fi
  112. if $dbus; then
  113. $changed || kill -HUP $(cat "$dnsmasq_pid")
  114. # Send even if empty so old servers are cleared
  115. dbus-send --system --dest=uk.org.thekelleys.dnsmasq \
  116. /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers \
  117. $dbusdest
  118. fi