-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdnssd-service
executable file
·113 lines (103 loc) · 4.76 KB
/
dnssd-service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
#
# Sends DNS-DS updates for dns-ds service registration and browsing records
#------------------------------------------------------------------------------
#
# load helpful functions
for i in functions/*.sh
do
. ${i}
[[ -n ${DEBUG} ]] && echo "Sourced ${PWD}/functions/$i ..."
done
set_vars $*
#------------------------------------------------------------------------------
# test
NEW_SUBZONE=${NEW_SUBZONE:-""}
# select zone or subdomain within zone
if [[ -n ${NEW_SUBZONE} ]]; then
DNSSD_DOMAIN="${NEW_SUBZONE}.${ZONE}"
else
DNSSD_DOMAIN="${ZONE}"
fi
DNSSD_SERVICES=${DNSSD_SERVICES:-""}
if [[ ! -n ${DNSSD_SERVICES} ]]; then
echo "Error: DNSSD SERVICES services \$DNSSD_SERVICES environment variable is not set. No services have been defined to browse in domain ${DNSSD_SERVICES}"
exit 1
fi
NSUPDATE_AUTH_SIG0_KEY_FQDN=${NSUPDATE_AUTH_SIG0_KEY_FQDN:-${DNSSD_DOMAIN}}
# define default update add & ttl
NSUPDATE_ACTION=${NSUPDATE_ACTION:-"add"}
NSUPDATE_TTL=${NSUPDATE_TTL:-"60"}
for service in ${DNSSD_SERVICES}
do
case ${NSUPDATE_ACTION} in
add)
NSUPDATE_PRECONDITION_SET="nxrrset"
NSUPDATE_ITEMS="${NSUPDATE_ITEMS}check-names off\n"
# service type browsing instance pointer
NSUPDATE_ITEMS="${NSUPDATE_ITEMS}update ${NSUPDATE_ACTION} _services._dns-sd._udp.${DNSSD_DOMAIN} ${NSUPDATE_TTL} PTR ${service}.${DNSSD_DOMAIN}.\n"
# service instance of service to browse
NSUPDATE_ITEMS="${NSUPDATE_ITEMS}update ${NSUPDATE_ACTION} ${service}.${DNSSD_DOMAIN} ${NSUPDATE_TTL} PTR ${NEW_SUBZONE}.${service}.${DNSSD_DOMAIN}.\n"
# service SRV record example
NSUPDATE_ITEMS="${NSUPDATE_ITEMS}update ${NSUPDATE_ACTION} ${NEW_SUBZONE}.${service}.${DNSSD_DOMAIN} ${NSUPDATE_TTL} SRV 0 0 80 ${DNSSD_DOMAIN}.\n"
# service TXT record example
NSUPDATE_ITEMS="${NSUPDATE_ITEMS}update ${NSUPDATE_ACTION} ${NEW_SUBZONE}.${service}.${DNSSD_DOMAIN} ${NSUPDATE_TTL} TXT comment=Hello_this_is_a_${service}_service_instance\n"
send_nsupdate "${DNSSD_DOMAIN}" "$(echo ${NSUPDATE_PRECONDITION};echo -e ${NSUPDATE_ITEMS})" "${NSUPDATE_AUTH_SIG0_KEY_FQDN}"
;;
delete)
NSUPDATE_PRECONDITION_SET="yxrrset"
# service type browsing instance pointer
NSUPDATE_ITEMS="${NSUPDATE_ITEMS}update ${NSUPDATE_ACTION} _services._dns-sd._udp.${DNSSD_DOMAIN} ${NSUPDATE_TTL} PTR ${service}.${DNSSD_DOMAIN}.\n"
# service instance of service to browse
NSUPDATE_ITEMS="${NSUPDATE_ITEMS}update ${NSUPDATE_ACTION} ${service}.${DNSSD_DOMAIN} ${NSUPDATE_TTL} PTR ${NEW_SUBZONE}.${service}.${DNSSD_DOMAIN}.\n"
# service SRV record example
NSUPDATE_ITEMS="${NSUPDATE_ITEMS}update ${NSUPDATE_ACTION} ${NEW_SUBZONE}.${service}.${DNSSD_DOMAIN} ${NSUPDATE_TTL} SRV 0 0 80 ${DNSSD_DOMAIN}.\n"
# service TXT record example
NSUPDATE_ITEMS="${NSUPDATE_ITEMS}update ${NSUPDATE_ACTION} ${NEW_SUBZONE}.${service}.${DNSSD_DOMAIN} ${NSUPDATE_TTL} TXT comment=Hello_this_is_a_${service}_service_instance\n"
send_nsupdate "${DNSSD_DOMAIN}" "$(echo ${NSUPDATE_PRECONDITION};echo -e ${NSUPDATE_ITEMS})" "${NSUPDATE_AUTH_SIG0_KEY_FQDN}"
;;
*)
# NSUPDATE_ACTION should default to "add" - should never get here
echo "Error: NSUPDATE_ACTION is set to '${NSUPDATE_ACTION}', but must be set to 'add' or 'delete'."
exit 1
;;
esac
done
if [[ -n ${DEBUG} ]]; then
echo "---DEBUG"
echo "SCRIPT_NAME = ${SCRIPT_NAME}"
echo "ZONE = ${ZONE}"
echo "DIG_QUERY_PARAM = ${DIG_QUERY_PARAM}"
echo "DNSSD_DOMAIN = ${DNSSD_DOMAIN}"
echo "ZONE_SOA_MASTER = ${ZONE_SOA_MASTER}"
echo "NSUPDATE_SIG0_KEYID = ${NSUPDATE_SIG0_KEYID}"
echo
echo "DEBUG: nsupdate settings"
echo " NSUPDATE_PARAM = ${NSUPDATE_PARAM}"
echo " NSUPDATE_SET_SERVER = ${NSUPDATE_SET_SERVER}"
echo " NSUPDATE_ACTION = ${NSUPDATE_ACTION}"
echo " NSUPDATE_PRECONDITION_SET = ${NSUPDATE_PRECONDITION_SET} (not implemented for ${SCRIPT_NAME})"
echo
echo "DEBUG: nsupdate commands to send"
echo
echo "NSUPDATE_SET_SERVER = ${NSUPDATE_SET_SERVER}"
echo "-- NSUPDATE_ITEMS"
echo -e "${NSUPDATE_ITEMS}"
echo "--"
fi
if [[ -n ${DEBUG} ]]; then
echo
echo "Browsable services via dig ${DIG_QUERY_PARAM} +short _services._dns-sd._udp.${DNSSD_DOMAIN} PTR"
dig ${DIG_QUERY_PARAM} +short _services._dns-sd._udp.${DNSSD_DOMAIN} PTR
echo
echo "Browsable services type instances via dig"
for SERVICE in ${DNSSD_SERVICES}
do
echo "* ${SERVICE}: dig ${DIG_QUERY_PARAM} +short ${SERVICE}.${DNSSD_DOMAIN} PTR"
dig ${DIG_QUERY_PARAM} +short ${SERVICE}.${DNSSD_DOMAIN} PTR
done
echo
AVAHI_BROWSE=${AVAHI_BROWSE:-"avahi-browse"}
echo "Browsable services via ${AVAHI_BROWSE} ${AVAHI_BROWSE_PARAM} -d ${DNSSD_DOMAIN}"
${AVAHI_BROWSE} ${AVAHI_BROWSE_PARAM} -d ${DNSSD_DOMAIN}
fi