~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/ftp/_gssapi.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Support for secure authentication using GSSAPI over FTP.
18
18
 
19
19
See RFC2228 for details.
20
20
"""
21
21
 
22
 
from __future__ import absolute_import
23
 
 
24
 
import base64, ftplib
 
22
import base64, ftplib, getpass, socket
25
23
 
26
24
from bzrlib import (
 
25
    config, 
27
26
    errors,
28
27
    )
29
 
from bzrlib.i18n import gettext
30
 
from bzrlib.trace import (
31
 
    mutter,
32
 
    note,
33
 
    )
 
28
from bzrlib.trace import info, mutter
34
29
from bzrlib.transport.ftp import FtpTransport
 
30
from bzrlib.transport import register_transport_proto, register_transport
35
31
 
36
32
try:
37
33
    import kerberos
40
36
    raise errors.DependencyNotPresent('kerberos', e)
41
37
 
42
38
if getattr(kerberos, "authGSSClientWrap", None) is None:
43
 
    raise errors.DependencyNotPresent('kerberos',
 
39
    raise errors.DependencyNotPresent('kerberos', 
44
40
        "missing encryption function authGSSClientWrap")
45
41
 
46
42
 
48
44
    """Extended version of ftplib.FTP that can authenticate using GSSAPI."""
49
45
 
50
46
    def mic_putcmd(self, line):
51
 
        rc = kerberos.authGSSClientWrap(self.vc, base64.b64encode(line))
 
47
        rc = kerberos.authGSSClientWrap(self.vc, 
 
48
            base64.b64encode(line), kerberos.authGSSClientUserName(self.vc))
52
49
        wrapped = kerberos.authGSSClientResponse(self.vc)
53
50
        ftplib.FTP.putcmd(self, "MIC " + wrapped)
54
51
 
64
61
        # Try GSSAPI login first
65
62
 
66
63
        # Used FTP response codes:
67
 
        # 235 [ADAT=base64data] - indicates that the security data exchange
 
64
        # 235 [ADAT=base64data] - indicates that the security data exchange 
68
65
        #     completed successfully.
69
 
        # 334 [ADAT=base64data] - indicates that the requested security
70
 
        #     mechanism is ok, and includes security data to be used by the
 
66
        # 334 [ADAT=base64data] - indicates that the requested security 
 
67
        #     mechanism is ok, and includes security data to be used by the 
71
68
        #     client to construct the next command.
72
69
        # 335 [ADAT=base64data] - indicates that the security data is
73
 
        #     acceptable, and more is required to complete the security
 
70
        #     acceptable, and more is required to complete the security 
74
71
        #     data exchange.
75
72
 
76
73
        resp = self.sendcmd('AUTH GSSAPI')
82
79
                    resp = self.sendcmd('ADAT ' + authdata)
83
80
                    if resp[:9] in ('235 ADAT=', '335 ADAT='):
84
81
                        rc = kerberos.authGSSClientStep(self.vc, resp[9:])
85
 
                        if not ((resp.startswith('235 ') and rc == 1) or
 
82
                        if not ((resp.startswith('235 ') and rc == 1) or 
86
83
                                (resp.startswith('335 ') and rc == 0)):
87
84
                            raise ftplib.error_reply, resp
88
 
            note(gettext("Authenticated as %s") %
89
 
                 kerberos.authGSSClientUserName(self.vc))
 
85
            info("Authenticated as %s" % kerberos.authGSSClientUserName(
 
86
                    self.vc))
90
87
 
91
88
            # Monkey patch ftplib
92
89
            self.putcmd = self.mic_putcmd
101
98
 
102
99
    """
103
100
 
104
 
    connection_class = GSSAPIFtp
105
 
 
106
 
    def _login(self, connection, auth, user, password):
107
 
        """Login with GSSAPI Authentication.
108
 
 
109
 
        The password is used if GSSAPI Authentication is not available.
110
 
 
111
 
        The username and password can both be None, in which case the
112
 
        credentials specified in the URL or provided by the
 
101
    def _create_connection(self, credentials=None):
 
102
        """Create a new connection with the provided credentials.
 
103
 
 
104
        :param credentials: The credentials needed to establish the connection.
 
105
 
 
106
        :return: The created connection and its associated credentials.
 
107
 
 
108
        The credentials are a tuple with the username and password. The 
 
109
        password is used if GSSAPI Authentication is not available.
 
110
 
 
111
        The username and password can both be None, in which case the 
 
112
        credentials specified in the URL or provided by the 
113
113
        AuthenticationConfig() are used.
114
114
        """
 
115
        if credentials is None:
 
116
            user, password = self._user, self._password
 
117
        else:
 
118
            user, password = credentials
 
119
 
 
120
        auth = config.AuthenticationConfig()
 
121
        if user is None:
 
122
            user = auth.get_user('ftp', self._host, port=self._port)
 
123
            if user is None:
 
124
                # Default to local user
 
125
                user = getpass.getuser()
 
126
 
 
127
        mutter("Constructing FTP instance against %r" %
 
128
               ((self._host, self._port, user, '********',
 
129
                self.is_active),))
115
130
        try:
116
 
            connection.gssapi_login(user=user)
 
131
            connection = GSSAPIFtp()
 
132
            connection.connect(host=self._host, port=self._port)
 
133
            try:
 
134
                connection.gssapi_login(user=user)
 
135
            except ftplib.error_perm, e:
 
136
                if user and user != 'anonymous' and \
 
137
                        password is None: # '' is a valid password
 
138
                    password = auth.get_password('ftp', self._host, user,
 
139
                                                 port=self._port)
 
140
                connection.login(user=user, passwd=password)
 
141
            connection.set_pasv(not self.is_active)
 
142
        except socket.error, e:
 
143
            raise errors.SocketConnectionError(self._host, self._port,
 
144
                                               msg='Unable to connect to',
 
145
                                               orig_error= e)
117
146
        except ftplib.error_perm, e:
118
 
            super(GSSAPIFtpTransport, self)._login(connection, auth,
119
 
                                                   user, password)
 
147
            raise errors.TransportError(msg="Error setting up connection:"
 
148
                                        " %s" % str(e), orig_error=e)
 
149
        return connection, (user, password)
120
150
 
121
151
 
122
152
def get_test_permutations():
123
153
    """Return the permutations to be used in testing."""
124
 
    from bzrlib.tests import ftp_server
125
 
    if ftp_server.FTPServerFeature.available():
126
 
        return [(GSSAPIFtpTransport, ftp_server.FTPTestServer)]
 
154
    from bzrlib import tests
 
155
    if tests.FTPServerFeature.available():
 
156
        from bzrlib.tests import ftp_server
 
157
        return [(GSSAPIFtpTransport, ftp_server.FTPServer)]
127
158
    else:
128
159
        return []