通过FTP通过TLS / SSL连接到未经认证的主机

时间:2012-03-20 20:35:05

标签: ruby ftp ftps

我从中获取文件的供应商正在通过SSL从FTP更改为FTP。

我正在尝试将代码从net/ftp更新为net/ftptls

我需要连接的新主机未经过认证,我的脚本会报告此错误。

  

主机名与服务器证书不匹配

供应商不会解决此问题。

关注/usr/lib/ruby/1.8/net/ftptls.rb我认为通过猴子修补FTPTLS来忽略不受信任的主机并不会太难。

我尝试将verify_mode更改为OpenSSL::SSL::VERIFY_NONE并注释掉post_connection_check`行。

既没有奏效。

关于如何做到这一点的任何想法?

require 'socket'
require 'openssl'
require 'net/ftp'

module Net
  class FTPTLS < FTP
    def connect(host, port=FTP_PORT)
      @hostname = host
      super
    end

    def login(user = "anonymous", passwd = nil, acct = nil)
       store = OpenSSL::X509::Store.new
       store.set_default_paths
       ctx = OpenSSL::SSL::SSLContext.new('SSLv23')
       ctx.cert_store = store
       ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
       ctx.key = nil
       ctx.cert = nil
       voidcmd("AUTH TLS")
       @sock = OpenSSL::SSL::SSLSocket.new(@sock, ctx)
       @sock.connect
       @sock.post_connection_check(@hostname)
       super(user, passwd, acct)
       voidcmd("PBSZ 0")
    end
  end
end

2 个答案:

答案 0 :(得分:2)

这可能是世界上最慢的答案,但是我遇到了你的问题并帮助我自己解决了这个问题,所以我想发帖给后人。

你非常接近,你只需要注释掉#post_connection_check。

我做了什么,而不是monkeypatching ruby​​本身,将这个副本带到我项目的/ lib中。

module Net

  class FTPTLS < FTP
    def connect(host, port=FTP_PORT)
      @hostname = host
      super
    end

    def login(user = "anonymous", params = {:password => nil, :acct => nil, :ignore_cert => false})
      store = OpenSSL::X509::Store.new
      store.set_default_paths
      ctx = OpenSSL::SSL::SSLContext.new('SSLv23')
      ctx.cert_store = store
      ctx.verify_mode = params[:ignore_cert] ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
      ctx.key = nil
      ctx.cert = nil
      voidcmd("AUTH TLS")
      @sock = OpenSSL::SSL::SSLSocket.new(@sock, ctx)
      @sock.connect
      @sock.post_connection_check(@hostname) unless params[:ignore_cert]
      super(user, params[:password], params[:acct])
      voidcmd("PBSZ 0")
    end
  end
end

我还清理了一点传递。你可以像这样使用它:

  require 'ftptls'  # Use my local version, not net/ftptls
  @ftp_connection = Net::FTPTLS.new()
  @ftp_connection.passive = true
  @ftp_connection.connect(host, 21)
  @ftp_connection.login('user', :password => 'pass', :ignore_cert => true)

HTH

答案 1 :(得分:2)

我知道Poul可能为时已晚,但我发现double-bag-ftps gem足够且易于使用,当我不得不做类似的事情时。