~ubuntu-branches/ubuntu/trusty/syncevolution/trusty-proposed

« back to all changes in this revision

Viewing changes to src/backends/webdav/syncevo-webdav-lookup.sh

  • Committer: Bazaar Package Importer
  • Author(s): Tino Keitel
  • Date: 2011-07-20 16:02:02 UTC
  • mfrom: (3.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110720160202-e8uf7ogw4vh0q0f3
Tags: 1.1.99.5a-1
* New upstream version 1.1.99.5a, first release candiate for 1.2
* Added python-openssl dependency, the HTTP server needs it for HTTPS support
* Added versioned dependency on libsynthesis0 to get required features
* Fixed .orig.tar.gz generation in get-orig-source target
* Added myself to Uploaders:, thanks to David for sponsoring
* Use updated upstream tag for source package generation
* Removed 0001-Replace-with-in-call-to-PKG_CHECK_MODULES.patch, fixed upstream
* Renamed NEWS.Debian to NEWS so that it is actually used
* Updated NEWS for 1.1.99.5a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/bash
 
2
#
 
3
# Copyright (C) 2011 Intel Corporation
 
4
#
 
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).
 
9
#
 
10
# See http://tools.ietf.org/html/draft-daboo-srv-caldav-10
 
11
#
 
12
# Works with a variety of underlying utilities:
 
13
# - adnshost
 
14
# - host
 
15
# - nslookup
 
16
#
 
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
 
20
# Return codes:
 
21
# 1 general error
 
22
# 2 no DNS utility found
 
23
# 3 no result for domain
 
24
#
 
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.
 
29
#
 
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.
 
34
#
 
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
 
38
# 02110-1301  USA
 
39
#
 
40
 
 
41
set -o pipefail
 
42
 
 
43
TYPE=$1
 
44
DOMAIN=$2
 
45
 
 
46
# Dump diagnostics on exit, if it still exists.
 
47
LOG=`mktemp`
 
48
trap "[ -s $LOG ] && ( echo $0 failed to find '$TYPE $DOMAIN':; cat $LOG >&2 ); rm -f $LOG" EXIT
 
49
 
 
50
# find one of the supported tools for DNS queries
 
51
TOOL=
 
52
ALTERNATIVES="adnshost host nslookup"
 
53
for i in $ALTERNATIVES; do
 
54
    if which $i >/dev/null; then
 
55
        TOOL=$i
 
56
        break
 
57
    fi
 
58
done
 
59
if [ ! "$TOOL" ]; then
 
60
    echo "need one of: $ALTERNATIVES" >&2
 
61
    exit 2
 
62
fi
 
63
 
 
64
# redirect tool errors and commands executed into log;
 
65
# try HTTPS first
 
66
for type in ${TYPE}s ${TYPE}; do
 
67
    (
 
68
        case $type in
 
69
            *s) METHOD=https;;
 
70
            *) METHOD=http;;
 
71
        esac
 
72
        HOSTNAME=
 
73
        PORT=
 
74
        # should be looked up via TXT, currently hard-coded
 
75
        URLPATH=.well-known/$TYPE
 
76
 
 
77
        set -xeo pipefail
 
78
 
 
79
        case $TOOL in
 
80
            adnshost)
 
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;'`
 
85
                ;;
 
86
            nslookup)
 
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;'`
 
91
                ;;
 
92
            host)
 
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;'`
 
97
                ;;
 
98
            *)
 
99
                echo "unsupported tool $TOOL"
 
100
                exit 1
 
101
                ;;
 
102
        esac
 
103
 
 
104
        # print result
 
105
        echo $METHOD://$HOSTNAME:$PORT/$URLPATH
 
106
    ) 2>>$LOG
 
107
    if [ $? -eq 0 ]; then
 
108
        # success, discard errrors
 
109
        rm -f $LOG
 
110
        exit 0
 
111
    fi
 
112
done
 
113
 
 
114
# nothing worked
 
115
exit 2