~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/http/ca_bundle.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Auto-detect of CA bundle for SSL connections"""
 
18
 
 
19
import os
 
20
import sys
 
21
from bzrlib.trace import mutter
 
22
 
 
23
 
 
24
_ca_path = None
 
25
 
 
26
 
 
27
def get_ca_path(use_cache=True):
 
28
    """Return location of CA bundle"""
 
29
    global _ca_path
 
30
 
 
31
    if _ca_path is not None and use_cache:
 
32
        return _ca_path
 
33
 
 
34
    # Find CA bundle for SSL
 
35
    # Reimplementation in Python the magic of curl command line tool
 
36
    # from "Details on Server SSL Certificates"
 
37
    # http://curl.haxx.se/docs/sslcerts.html
 
38
    #
 
39
    # 4. If you're using the curl command line tool, you can specify your own CA
 
40
    #    cert path by setting the environment variable CURL_CA_BUNDLE to the path
 
41
    #    of your choice.
 
42
    #
 
43
    #    If you're using the curl command line tool on Windows, curl will search
 
44
    #    for a CA cert file named "curl-ca-bundle.crt" in these directories and in
 
45
    #    this order:
 
46
    #      1. application's directory
 
47
    #      2. current working directory
 
48
    #      3. Windows System directory (e.g. C:\windows\system32)
 
49
    #      4. Windows Directory (e.g. C:\windows)
 
50
    #      5. all directories along %PATH%
 
51
    #
 
52
    # NOTES:
 
53
    #   bialix: Windows directories usually listed in PATH env variable
 
54
    #   j-a-meinel: bzr should not look in current working dir
 
55
 
 
56
    path = os.environ.get('CURL_CA_BUNDLE')
 
57
    if not path and sys.platform == 'win32':
 
58
        dirs = [os.path.realpath(os.path.dirname(sys.argv[0]))]     # app dir
 
59
        paths = os.environ.get('PATH')
 
60
        if paths:
 
61
            # don't include the cwd in the search
 
62
            paths = [i for i in paths.split(os.pathsep) if i not in ('', '.')]
 
63
            dirs.extend(paths)
 
64
        for d in dirs:
 
65
            fname = os.path.join(d, "curl-ca-bundle.crt")
 
66
            if os.path.isfile(fname):
 
67
                path = fname
 
68
                break
 
69
    if path:
 
70
        mutter('using CA bundle: %r', path)
 
71
    else:
 
72
        path = ''
 
73
 
 
74
    if use_cache:
 
75
        _ca_path = path
 
76
 
 
77
    return path