summaryrefslogtreecommitdiff
path: root/src/backends/webdav/syncevo-webdav-lookup.sh
blob: a30aa1f0c0284b0985def3d9cf7b0ed62e2aa403 (plain)
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
114
115
#! /bin/bash
#
# Copyright (C) 2011 Intel Corporation
#
# Does DNS SRV and TXT queries to find the full URL, including method
# (HTTP or HTTPS), host name, port and path (currently hard-coded to
# .well-known/<type>, should use TXT, except that none of the current
# services seem to use that either, so couldn't test).
#
# See http://tools.ietf.org/html/draft-daboo-srv-caldav-10
#
# Works with a variety of underlying utilities:
# - adnshost
# - host
# - nslookup
#
# Usage: syncevo-webdav-lookup.sh carddav|caldav <domain>
# Stdout: http[s]://<domain>:<port>/<path>
# Stderr: error messages indicating failure, empty for success
# Return codes:
# 1 general error
# 2 no DNS utility found
# 3 no result for domain
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301  USA
#

set -o pipefail

TYPE=$1
DOMAIN=$2

# Dump diagnostics on exit, if it still exists.
LOG=`mktemp`
trap "[ -s $LOG ] && ( echo $0 failed to find '$TYPE $DOMAIN':; cat $LOG >&2 ); rm -f $LOG" EXIT

# find one of the supported tools for DNS queries
TOOL=
ALTERNATIVES="adnshost host nslookup"
for i in $ALTERNATIVES; do
    if which $i >/dev/null; then
        TOOL=$i
        break
    fi
done
if [ ! "$TOOL" ]; then
    echo "need one of: $ALTERNATIVES" >&2
    exit 2
fi

# redirect tool errors and commands executed into log;
# try HTTPS first
for type in ${TYPE}s ${TYPE}; do
    (
        case $type in
            *s) METHOD=https;;
            *) METHOD=http;;
        esac
        HOSTNAME=
        PORT=
        # should be looked up via TXT, currently hard-coded
        URLPATH=.well-known/$TYPE

        set -xeo pipefail

        case $TOOL in
            adnshost)
                res=`$TOOL -Fi -tsrv _$type._tcp.$DOMAIN | tee -a $LOG | grep ^_$type._tcp.$DOMAIN`
                # _carddavs._tcp.yahoo.com.cn SRV 1 1 443 carddav.address.yahoo.com misconfig 101 prohibitedcname "DNS alias found where canonical name wanted" ( )
                PORT=`echo $res | sed -e 's;.* SRV [^ ]* [^ ]* \([^ ]*\) \([^ ]*\).*;\1;'`
                HOSTNAME=`echo $res | sed -e 's;.* SRV [^ ]* [^ ]* \([^ ]*\) \([^ ]*\).*;\2;'`
                ;;
            nslookup)
                res=`$TOOL -type=srv _$type._tcp.$DOMAIN | tee -a $LOG | grep "service =" | head -1`
                # _caldavs._tcp.yahoo.com	service = 1 1 443 caldav.calendar.yahoo.com.
                PORT=`echo $res | sed -e 's;.*service = [^ ]* [^ ]* \([^ ]*\) \([^ ]*\)\.;\1;'`
                HOSTNAME=`echo $res | sed -e 's;.*service = [^ ]* [^ ]* \([^ ]*\) \([^ ]*\)\.;\2;'`
                ;;
            host)
                res=`$TOOL -t srv _$type._tcp.$DOMAIN | tee -a $LOG | grep "has SRV record" | head -1`
                # _caldavs._tcp.yahoo.com has SRV record 1 1 443 caldav.calendar.yahoo.com.
                PORT=`echo $res | sed -e 's;.* \([^ ]*\) \([^ ]*\)\.;\1;'`
                HOSTNAME=`echo $res | sed -e 's;.* \([^ ]*\) \([^ ]*\)\.;\2;'`
                ;;
            *)
                echo "unsupported tool $TOOL"
                exit 1
                ;;
        esac

        # print result
        echo $METHOD://$HOSTNAME:$PORT/$URLPATH
    ) 2>>$LOG
    if [ $? -eq 0 ]; then
        # success, discard errrors
        rm -f $LOG
        exit 0
    fi
done

# nothing worked
exit 2