浏览代码

2009-10-27 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	If user name is embedded but password is missing in URI, first
	resolve password using .netrc. If password is found in .netrc,
	then use it as password. If not, use the password specified in
	--ftp-passwd.
	* src/AuthConfigFactory.cc
	* src/Request.cc
	* src/Request.h
	* test/AuthConfigFactoryTest.cc
Tatsuhiro Tsujikawa 16 年之前
父节点
当前提交
14a47f43f8
共有 5 个文件被更改,包括 67 次插入1 次删除
  1. 11 0
      ChangeLog
  2. 20 1
      src/AuthConfigFactory.cc
  3. 5 0
      src/Request.cc
  4. 8 0
      src/Request.h
  5. 23 0
      test/AuthConfigFactoryTest.cc

+ 11 - 0
ChangeLog

@@ -1,3 +1,14 @@
+2009-10-27  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	If user name is embedded but password is missing in URI, first
+	resolve password using .netrc. If password is found in .netrc,
+	then use it as password. If not, use the password specified in
+	--ftp-passwd.
+	* src/AuthConfigFactory.cc
+	* src/Request.cc
+	* src/Request.h
+	* test/AuthConfigFactoryTest.cc
+
 2009-10-25  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Fixed typo

+ 20 - 1
src/AuthConfigFactory.cc

@@ -86,7 +86,26 @@ AuthConfigFactory::createAuthConfig
     }
   } else if(request->getProtocol() == Request::PROTO_FTP) {
     if(!request->getUsername().empty()) {
-      return createAuthConfig(request->getUsername(), request->getPassword());
+      if(request->hasPassword()) {
+	return createAuthConfig(request->getUsername(), request->getPassword());
+      } else {
+	if(!op->getAsBool(PREF_NO_NETRC)) {
+	  // First, check we have password corresponding to host and
+	  // username
+	  NetrcAuthResolver authResolver;
+	  authResolver.setNetrc(_netrc);
+
+	  SharedHandle<AuthConfig> ac =
+	    authResolver.resolveAuthConfig(request->getHost());
+	  if(!ac.isNull() && ac->getUser() == request->getUsername()) {
+	    return ac;
+	  }
+	}
+	// We don't have password for host and username. Return
+	// password specified by --ftp-passwd
+	return
+	  createAuthConfig(request->getUsername(), op->get(PREF_FTP_PASSWD));
+      }
     } else {
       return
 	createFtpAuthResolver(op)->resolveAuthConfig(request->getHost());

+ 5 - 0
src/Request.cc

@@ -72,6 +72,7 @@ Request::Request():
   _pipeliningHint(false),
   _maxPipelinedRequest(1),
   method(METHOD_GET),
+  _hasPassword(false),
   _ipv6LiteralAddress(false)
 {}
 
@@ -161,6 +162,7 @@ bool Request::parseUrl(const std::string& url) {
   _query = A2STR::NIL;
   _username = A2STR::NIL;
   _password = A2STR::NIL;
+  _hasPassword = false;
   _ipv6LiteralAddress = false;
   // find query part
   std::string queryTemp;
@@ -193,6 +195,9 @@ bool Request::parseUrl(const std::string& url) {
       util::split(authPart, A2STR::COLON_C);
     _username = util::urldecode(userPass.first);
     _password = util::urldecode(userPass.second);
+    if(authPart.find(A2STR::COLON_C) != std::string::npos) {
+      _hasPassword = true;
+    }
     hostPart.erase(0, atmarkp+1);
   }
   {

+ 8 - 0
src/Request.h

@@ -83,6 +83,8 @@ private:
 
   std::string _password;
 
+  bool _hasPassword;
+
   bool _ipv6LiteralAddress;
 
   SharedHandle<PeerStat> _peerStat;
@@ -191,6 +193,12 @@ public:
     return _password;
   }
 
+  // Returns true if current URI has embedded password.
+  bool hasPassword() const
+  {
+    return _hasPassword;
+  }
+
   const std::string& getMethod() const {
     return method;
   }

+ 23 - 0
test/AuthConfigFactoryTest.cc

@@ -170,6 +170,29 @@ void AuthConfigFactoryTest::testCreateAuthConfig_ftp()
   req->setUrl("ftp://aria2user:aria2password@localhost/download/aria2-1.0.0.tar.bz2");
   CPPUNIT_ASSERT_EQUAL(std::string("aria2user:aria2password"),
  		       factory.createAuthConfig(req, &option)->getAuthText());
+
+  // username in URI, but no password. We have DefaultAuthenticator
+  // but username is not aria2user
+  req->setUrl("ftp://aria2user@localhost/download/aria2-1.0.0.tar.bz2");
+  CPPUNIT_ASSERT_EQUAL(std::string("aria2user:userDefinedPassword"),
+		       factory.createAuthConfig(req, &option)->getAuthText());
+
+  // Recreate netrc with entry for user aria2user
+  netrc.reset(new Netrc());
+  netrc->addAuthenticator
+    (SharedHandle<Authenticator>(new Authenticator("localhost",
+						   "aria2user",
+						   "netrcpass",
+						   "netrcacct")));
+  factory.setNetrc(netrc);
+  // This time, we can find same username "aria2user" in netrc, so the
+  // password "netrcpass" is used, instead of "userDefinedPassword"
+  CPPUNIT_ASSERT_EQUAL(std::string("aria2user:netrcpass"),
+		       factory.createAuthConfig(req, &option)->getAuthText());
+  // No netrc entry for host mirror, so "userDefinedPassword" is used.
+  req->setUrl("ftp://aria2user@mirror/download/aria2-1.0.0.tar.bz2");
+  CPPUNIT_ASSERT_EQUAL(std::string("aria2user:userDefinedPassword"),
+		       factory.createAuthConfig(req, &option)->getAuthText());
 }
 
 void AuthConfigFactoryTest::testUpdateBasicCred()