def _create_connection(self, credentials=None): if credentials is None: user, password = self._parsed_url.user, self._parsed_url.password else: user, password = credentials try: connection = gio.File(self.url) mount = None try: mount = connection.find_enclosing_mount() except gio.Error, e: if (e.code == gio.ERROR_NOT_MOUNTED): self.loop = glib.MainLoop() ui.ui_factory.show_message('Mounting %s using GIO' % \ self.url) op = gio.MountOperation() if user: op.set_username(user) if password: op.set_password(password) op.connect('ask-password', self._auth_cb) m = connection.mount_enclosing_volume( op, self._mount_done_cb) self.loop.run() except gio.Error, e: raise errors.TransportError(msg="Error setting up connection:" " %s" % str(e), orig_error=e)
def _raise_curl_http_error(self, curl, info=None, body=None): """Common curl->bzrlib error translation. Some methods may choose to override this for particular cases. The URL and code are automatically included as appropriate. :param info: Extra information to include in the message. :param body: File-like object from which the body of the page can be read. """ code = curl.getinfo(pycurl.HTTP_CODE) url = curl.getinfo(pycurl.EFFECTIVE_URL) if body is not None: response_body = body.read() plaintext_body = unhtml_roughly(response_body) else: response_body = None plaintext_body = '' if code == 403: raise errors.TransportError( 'Server refuses to fulfill the request (403 Forbidden)' ' for %s: %s' % (url, plaintext_body)) else: if info is None: msg = '' else: msg = ': ' + info raise errors.InvalidHttpResponse( url, 'Unable to handle http code %d%s: %s' % (code, msg, plaintext_body))
def _raise_curl_http_error(self, curl, info=None): code = curl.getinfo(pycurl.HTTP_CODE) url = curl.getinfo(pycurl.EFFECTIVE_URL) # Some error codes can be handled the same way for all # requests if code == 403: raise errors.TransportError( 'Server refuses to fulfill the request (403 Forbidden)' ' for %s' % url) else: if info is None: msg = '' else: msg = ': ' + info raise errors.InvalidHttpResponse( url, 'Unable to handle http code %d%s' % (code, msg))
class ParamikoVendor(SSHVendor): """Vendor that uses paramiko.""" def _hexify(self, s): return hexlify(s).upper() def _connect(self, username, password, host, port): global SYSTEM_HOSTKEYS, BZR_HOSTKEYS load_host_keys() try: t = paramiko.Transport((host, port or 22)) t.set_log_channel('bzr.paramiko') t.start_client() except (paramiko.SSHException, socket.error), e: self._raise_connection_error(host, port=port, orig_error=e) server_key = t.get_remote_server_key() server_key_hex = self._hexify(server_key.get_fingerprint()) keytype = server_key.get_name() if host in SYSTEM_HOSTKEYS and keytype in SYSTEM_HOSTKEYS[host]: our_server_key = SYSTEM_HOSTKEYS[host][keytype] our_server_key_hex = self._hexify(our_server_key.get_fingerprint()) elif host in BZR_HOSTKEYS and keytype in BZR_HOSTKEYS[host]: our_server_key = BZR_HOSTKEYS[host][keytype] our_server_key_hex = self._hexify(our_server_key.get_fingerprint()) else: trace.warning('Adding %s host key for %s: %s' % (keytype, host, server_key_hex)) add = getattr(BZR_HOSTKEYS, 'add', None) if add is not None: # paramiko >= 1.X.X BZR_HOSTKEYS.add(host, keytype, server_key) else: BZR_HOSTKEYS.setdefault(host, {})[keytype] = server_key our_server_key = server_key our_server_key_hex = self._hexify(our_server_key.get_fingerprint()) save_host_keys() if server_key != our_server_key: filename1 = os.path.expanduser('~/.ssh/known_hosts') filename2 = osutils.pathjoin(config.config_dir(), 'ssh_host_keys') raise errors.TransportError( 'Host keys for %s do not match! %s != %s' % (host, our_server_key_hex, server_key_hex), ['Try editing %s or %s' % (filename1, filename2)]) _paramiko_auth(username, password, host, port, t) return t
class FtpTransport(ConnectedTransport): """This is the transport agent for ftp:// access.""" def __init__(self, base, _from_transport=None): """Set the base path where files will be stored.""" if not (base.startswith('ftp://') or base.startswith('aftp://')): raise ValueError(base) super(FtpTransport, self).__init__(base, _from_transport=_from_transport) self._unqualified_scheme = 'ftp' if self._scheme == 'aftp': self.is_active = True else: self.is_active = False def _get_FTP(self): """Return the ftplib.FTP instance for this object.""" # Ensures that a connection is established connection = self._get_connection() if connection is None: # First connection ever connection, credentials = self._create_connection() self._set_connection(connection, credentials) return connection def _create_connection(self, credentials=None): """Create a new connection with the provided credentials. :param credentials: The credentials needed to establish the connection. :return: The created connection and its associated credentials. The credentials are only the password as it may have been entered interactively by the user and may be different from the one provided in base url at transport creation time. """ if credentials is None: user, password = self._user, self._password else: user, password = credentials auth = config.AuthenticationConfig() if user is None: user = auth.get_user('ftp', self._host, port=self._port) if user is None: # Default to local user user = getpass.getuser() mutter("Constructing FTP instance against %r" % ((self._host, self._port, user, '********', self.is_active), )) try: connection = ftplib.FTP() connection.connect(host=self._host, port=self._port) if user and user != 'anonymous' and \ password is None: # '' is a valid password password = auth.get_password('ftp', self._host, user, port=self._port) connection.login(user=user, passwd=password) connection.set_pasv(not self.is_active) except socket.error, e: raise errors.SocketConnectionError(self._host, self._port, msg='Unable to connect to', orig_error=e) except ftplib.error_perm, e: raise errors.TransportError(msg="Error setting up connection:" " %s" % str(e), orig_error=e)
then read from. For now we do this via the magic of StringIO """ # TODO: decode should be deprecated try: mutter("FTP get: %s", self._remote_path(relpath)) f = self._get_FTP() ret = StringIO() f.retrbinary('RETR ' + self._remote_path(relpath), ret.write, 8192) ret.seek(0) return ret except ftplib.error_perm, e: raise errors.NoSuchFile(self.abspath(relpath), extra=str(e)) except ftplib.error_temp, e: if retries > _number_of_retries: raise errors.TransportError( msg="FTP temporary error during GET %s. Aborting." % self.abspath(relpath), orig_error=e) else: warning("FTP temporary error: %s. Retrying.", str(e)) self._reconnect() return self.get(relpath, decode, retries + 1) except EOFError, e: if retries > _number_of_retries: raise errors.TransportError( "FTP control connection closed during GET %s." % self.abspath(relpath), orig_error=e) else: warning("FTP control connection closed. Trying to reopen.") time.sleep(_sleep_between_retries) self._reconnect()