3
# Copyright (C) 2011 Intel Corporation
5
# Does DNS SRV and TXT queries to find the full URL, including method
6
# (HTTP or HTTPS), host name, port and path (currently hard-coded to
7
# .well-known/<type>, should use TXT, except that none of the current
8
# services seem to use that either, so couldn't test).
10
# See http://tools.ietf.org/html/draft-daboo-srv-caldav-10
12
# Works with a variety of underlying utilities:
17
# Usage: syncevo-webdav-lookup.sh carddav|caldav <domain>
18
# Stdout: http[s]://<domain>:<port>/<path>
19
# Stderr: error messages indicating failure, empty for success
22
# 2 no DNS utility found
23
# 3 no result for domain
25
# This program is free software; you can redistribute it and/or
26
# modify it under the terms of the GNU Lesser General Public
27
# License as published by the Free Software Foundation; either
28
# version 2.1 of the License, or (at your option) version 3.
30
# This program is distributed in the hope that it will be useful,
31
# but WITHOUT ANY WARRANTY; without even the implied warranty of
32
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33
# Lesser General Public License for more details.
35
# You should have received a copy of the GNU Lesser General Public
36
# License along with this library; if not, write to the Free Software
37
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
46
# Dump diagnostics on exit, if it still exists.
48
trap "[ -s $LOG ] && ( echo $0 failed to find '$TYPE $DOMAIN':; cat $LOG >&2 ); rm -f $LOG" EXIT
50
# find one of the supported tools for DNS queries
52
ALTERNATIVES="adnshost host nslookup"
53
for i in $ALTERNATIVES; do
54
if which $i >/dev/null; then
59
if [ ! "$TOOL" ]; then
60
echo "need one of: $ALTERNATIVES" >&2
64
# redirect tool errors and commands executed into log;
66
for type in ${TYPE}s ${TYPE}; do
74
# should be looked up via TXT, currently hard-coded
75
URLPATH=.well-known/$TYPE
81
res=`$TOOL -Fi -tsrv _$type._tcp.$DOMAIN | tee -a $LOG | grep ^_$type._tcp.$DOMAIN`
82
# _carddavs._tcp.yahoo.com.cn SRV 1 1 443 carddav.address.yahoo.com misconfig 101 prohibitedcname "DNS alias found where canonical name wanted" ( )
83
PORT=`echo $res | sed -e 's;.* SRV [^ ]* [^ ]* \([^ ]*\) \([^ ]*\).*;\1;'`
84
HOSTNAME=`echo $res | sed -e 's;.* SRV [^ ]* [^ ]* \([^ ]*\) \([^ ]*\).*;\2;'`
87
res=`$TOOL -type=srv _$type._tcp.$DOMAIN | tee -a $LOG | grep "service =" | head -1`
88
# _caldavs._tcp.yahoo.com service = 1 1 443 caldav.calendar.yahoo.com.
89
PORT=`echo $res | sed -e 's;.*service = [^ ]* [^ ]* \([^ ]*\) \([^ ]*\)\.;\1;'`
90
HOSTNAME=`echo $res | sed -e 's;.*service = [^ ]* [^ ]* \([^ ]*\) \([^ ]*\)\.;\2;'`
93
res=`$TOOL -t srv _$type._tcp.$DOMAIN | tee -a $LOG | grep "has SRV record" | head -1`
94
# _caldavs._tcp.yahoo.com has SRV record 1 1 443 caldav.calendar.yahoo.com.
95
PORT=`echo $res | sed -e 's;.* \([^ ]*\) \([^ ]*\)\.;\1;'`
96
HOSTNAME=`echo $res | sed -e 's;.* \([^ ]*\) \([^ ]*\)\.;\2;'`
99
echo "unsupported tool $TOOL"
105
echo $METHOD://$HOSTNAME:$PORT/$URLPATH
107
if [ $? -eq 0 ]; then
108
# success, discard errrors