You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

440 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "xmppclient.h"
  28. #include "xmpptask.h"
  29. #include "talk/base/logging.h"
  30. #include "talk/base/sigslot.h"
  31. #include "talk/base/scoped_ptr.h"
  32. #include "talk/base/stringutils.h"
  33. #include "talk/xmpp/constants.h"
  34. #include "talk/xmpp/saslplainmechanism.h"
  35. #include "talk/xmpp/prexmppauth.h"
  36. #include "talk/xmpp/plainsaslhandler.h"
  37. namespace buzz {
  38. class XmppClient::Private :
  39. public sigslot::has_slots<>,
  40. public XmppSessionHandler,
  41. public XmppOutputHandler {
  42. public:
  43. explicit Private(XmppClient* client) :
  44. client_(client),
  45. socket_(NULL),
  46. engine_(NULL),
  47. proxy_port_(0),
  48. pre_engine_error_(XmppEngine::ERROR_NONE),
  49. pre_engine_subcode_(0),
  50. signal_closed_(false),
  51. allow_plain_(false) {}
  52. virtual ~Private() {
  53. // We need to disconnect from socket_ before engine_ is destructed (by
  54. // the auto-generated destructor code).
  55. ResetSocket();
  56. }
  57. // the owner
  58. XmppClient* const client_;
  59. // the two main objects
  60. talk_base::scoped_ptr<AsyncSocket> socket_;
  61. talk_base::scoped_ptr<XmppEngine> engine_;
  62. talk_base::scoped_ptr<PreXmppAuth> pre_auth_;
  63. talk_base::CryptString pass_;
  64. std::string auth_mechanism_;
  65. std::string auth_token_;
  66. talk_base::SocketAddress server_;
  67. std::string proxy_host_;
  68. int proxy_port_;
  69. XmppEngine::Error pre_engine_error_;
  70. int pre_engine_subcode_;
  71. CaptchaChallenge captcha_challenge_;
  72. bool signal_closed_;
  73. bool allow_plain_;
  74. void ResetSocket() {
  75. if (socket_) {
  76. socket_->SignalConnected.disconnect(this);
  77. socket_->SignalRead.disconnect(this);
  78. socket_->SignalClosed.disconnect(this);
  79. socket_.reset(NULL);
  80. }
  81. }
  82. // implementations of interfaces
  83. void OnStateChange(int state);
  84. void WriteOutput(const char* bytes, size_t len);
  85. void StartTls(const std::string& domainname);
  86. void CloseConnection();
  87. // slots for socket signals
  88. void OnSocketConnected();
  89. void OnSocketRead();
  90. void OnSocketClosed();
  91. };
  92. bool IsTestServer(const std::string& server_name,
  93. const std::string& test_server_domain) {
  94. return (!test_server_domain.empty() &&
  95. talk_base::ends_with(server_name.c_str(),
  96. test_server_domain.c_str()));
  97. }
  98. XmppReturnStatus XmppClient::Connect(
  99. const XmppClientSettings& settings,
  100. const std::string& lang, AsyncSocket* socket, PreXmppAuth* pre_auth) {
  101. if (socket == NULL)
  102. return XMPP_RETURN_BADARGUMENT;
  103. if (d_->socket_)
  104. return XMPP_RETURN_BADSTATE;
  105. d_->socket_.reset(socket);
  106. d_->socket_->SignalConnected.connect(d_.get(), &Private::OnSocketConnected);
  107. d_->socket_->SignalRead.connect(d_.get(), &Private::OnSocketRead);
  108. d_->socket_->SignalClosed.connect(d_.get(), &Private::OnSocketClosed);
  109. d_->engine_.reset(XmppEngine::Create());
  110. d_->engine_->SetSessionHandler(d_.get());
  111. d_->engine_->SetOutputHandler(d_.get());
  112. if (!settings.resource().empty()) {
  113. d_->engine_->SetRequestedResource(settings.resource());
  114. }
  115. d_->engine_->SetTls(settings.use_tls());
  116. // The talk.google.com server returns a certificate with common-name:
  117. // CN="gmail.com" for @gmail.com accounts,
  118. // CN="googlemail.com" for @googlemail.com accounts,
  119. // CN="talk.google.com" for other accounts (such as @example.com),
  120. // so we tweak the tls server setting for those other accounts to match the
  121. // returned certificate CN of "talk.google.com".
  122. // For other servers, we leave the strings empty, which causes the jid's
  123. // domain to be used. We do the same for gmail.com and googlemail.com as the
  124. // returned CN matches the account domain in those cases.
  125. std::string server_name = settings.server().HostAsURIString();
  126. if (server_name == buzz::STR_TALK_GOOGLE_COM ||
  127. server_name == buzz::STR_TALKX_L_GOOGLE_COM ||
  128. server_name == buzz::STR_XMPP_GOOGLE_COM ||
  129. server_name == buzz::STR_XMPPX_L_GOOGLE_COM ||
  130. IsTestServer(server_name, settings.test_server_domain())) {
  131. if (settings.host() != STR_GMAIL_COM &&
  132. settings.host() != STR_GOOGLEMAIL_COM) {
  133. d_->engine_->SetTlsServer("", STR_TALK_GOOGLE_COM);
  134. }
  135. }
  136. // Set language
  137. d_->engine_->SetLanguage(lang);
  138. d_->engine_->SetUser(buzz::Jid(settings.user(), settings.host(), STR_EMPTY));
  139. d_->pass_ = settings.pass();
  140. d_->auth_mechanism_ = settings.auth_mechanism();
  141. d_->auth_token_ = settings.auth_token();
  142. d_->server_ = settings.server();
  143. d_->proxy_host_ = settings.proxy_host();
  144. d_->proxy_port_ = settings.proxy_port();
  145. d_->allow_plain_ = settings.allow_plain();
  146. d_->pre_auth_.reset(pre_auth);
  147. return XMPP_RETURN_OK;
  148. }
  149. XmppEngine::State XmppClient::GetState() const {
  150. if (!d_->engine_)
  151. return XmppEngine::STATE_NONE;
  152. return d_->engine_->GetState();
  153. }
  154. XmppEngine::Error XmppClient::GetError(int* subcode) {
  155. if (subcode) {
  156. *subcode = 0;
  157. }
  158. if (!d_->engine_)
  159. return XmppEngine::ERROR_NONE;
  160. if (d_->pre_engine_error_ != XmppEngine::ERROR_NONE) {
  161. if (subcode) {
  162. *subcode = d_->pre_engine_subcode_;
  163. }
  164. return d_->pre_engine_error_;
  165. }
  166. return d_->engine_->GetError(subcode);
  167. }
  168. const XmlElement* XmppClient::GetStreamError() {
  169. if (!d_->engine_) {
  170. return NULL;
  171. }
  172. return d_->engine_->GetStreamError();
  173. }
  174. CaptchaChallenge XmppClient::GetCaptchaChallenge() {
  175. if (!d_->engine_)
  176. return CaptchaChallenge();
  177. return d_->captcha_challenge_;
  178. }
  179. std::string XmppClient::GetAuthMechanism() {
  180. if (!d_->engine_)
  181. return "";
  182. return d_->auth_mechanism_;
  183. }
  184. std::string XmppClient::GetAuthToken() {
  185. if (!d_->engine_)
  186. return "";
  187. return d_->auth_token_;
  188. }
  189. int XmppClient::ProcessStart() {
  190. // Should not happen, but was observed in crash reports
  191. if (!d_->socket_) {
  192. LOG(LS_ERROR) << "socket_ already reset";
  193. return STATE_DONE;
  194. }
  195. if (d_->pre_auth_) {
  196. d_->pre_auth_->SignalAuthDone.connect(this, &XmppClient::OnAuthDone);
  197. d_->pre_auth_->StartPreXmppAuth(
  198. d_->engine_->GetUser(), d_->server_, d_->pass_,
  199. d_->auth_mechanism_, d_->auth_token_);
  200. d_->pass_.Clear(); // done with this;
  201. return STATE_PRE_XMPP_LOGIN;
  202. }
  203. else {
  204. d_->engine_->SetSaslHandler(new PlainSaslHandler(
  205. d_->engine_->GetUser(), d_->pass_, d_->allow_plain_));
  206. d_->pass_.Clear(); // done with this;
  207. return STATE_START_XMPP_LOGIN;
  208. }
  209. }
  210. void XmppClient::OnAuthDone() {
  211. Wake();
  212. }
  213. int XmppClient::ProcessTokenLogin() {
  214. // Should not happen, but was observed in crash reports
  215. if (!d_->socket_) {
  216. LOG(LS_ERROR) << "socket_ already reset";
  217. return STATE_DONE;
  218. }
  219. // Don't know how this could happen, but crash reports show it as NULL
  220. if (!d_->pre_auth_) {
  221. d_->pre_engine_error_ = XmppEngine::ERROR_AUTH;
  222. EnsureClosed();
  223. return STATE_ERROR;
  224. }
  225. // Wait until pre authentication is done is done
  226. if (!d_->pre_auth_->IsAuthDone())
  227. return STATE_BLOCKED;
  228. if (!d_->pre_auth_->IsAuthorized()) {
  229. // maybe split out a case when gaia is down?
  230. if (d_->pre_auth_->HadError()) {
  231. d_->pre_engine_error_ = XmppEngine::ERROR_AUTH;
  232. d_->pre_engine_subcode_ = d_->pre_auth_->GetError();
  233. }
  234. else {
  235. d_->pre_engine_error_ = XmppEngine::ERROR_UNAUTHORIZED;
  236. d_->pre_engine_subcode_ = 0;
  237. d_->captcha_challenge_ = d_->pre_auth_->GetCaptchaChallenge();
  238. }
  239. d_->pre_auth_.reset(NULL); // done with this
  240. EnsureClosed();
  241. return STATE_ERROR;
  242. }
  243. // Save auth token as a result
  244. d_->auth_mechanism_ = d_->pre_auth_->GetAuthMechanism();
  245. d_->auth_token_ = d_->pre_auth_->GetAuthToken();
  246. // transfer ownership of pre_auth_ to engine
  247. d_->engine_->SetSaslHandler(d_->pre_auth_.release());
  248. return STATE_START_XMPP_LOGIN;
  249. }
  250. int XmppClient::ProcessStartXmppLogin() {
  251. // Should not happen, but was observed in crash reports
  252. if (!d_->socket_) {
  253. LOG(LS_ERROR) << "socket_ already reset";
  254. return STATE_DONE;
  255. }
  256. // Done with pre-connect tasks - connect!
  257. if (!d_->socket_->Connect(d_->server_)) {
  258. EnsureClosed();
  259. return STATE_ERROR;
  260. }
  261. return STATE_RESPONSE;
  262. }
  263. int XmppClient::ProcessResponse() {
  264. // Hang around while we are connected.
  265. if (!delivering_signal_ &&
  266. (!d_->engine_ || d_->engine_->GetState() == XmppEngine::STATE_CLOSED))
  267. return STATE_DONE;
  268. return STATE_BLOCKED;
  269. }
  270. XmppReturnStatus XmppClient::Disconnect() {
  271. if (!d_->socket_)
  272. return XMPP_RETURN_BADSTATE;
  273. Abort();
  274. d_->engine_->Disconnect();
  275. d_->ResetSocket();
  276. return XMPP_RETURN_OK;
  277. }
  278. XmppClient::XmppClient(TaskParent* parent)
  279. : XmppTaskParentInterface(parent),
  280. delivering_signal_(false),
  281. valid_(false) {
  282. d_.reset(new Private(this));
  283. valid_ = true;
  284. }
  285. XmppClient::~XmppClient() {
  286. valid_ = false;
  287. }
  288. const Jid& XmppClient::jid() const {
  289. return d_->engine_->FullJid();
  290. }
  291. std::string XmppClient::NextId() {
  292. return d_->engine_->NextId();
  293. }
  294. XmppReturnStatus XmppClient::SendStanza(const XmlElement* stanza) {
  295. return d_->engine_->SendStanza(stanza);
  296. }
  297. XmppReturnStatus XmppClient::SendStanzaError(
  298. const XmlElement* old_stanza, XmppStanzaError xse,
  299. const std::string& message) {
  300. return d_->engine_->SendStanzaError(old_stanza, xse, message);
  301. }
  302. XmppReturnStatus XmppClient::SendRaw(const std::string& text) {
  303. return d_->engine_->SendRaw(text);
  304. }
  305. XmppEngine* XmppClient::engine() {
  306. return d_->engine_.get();
  307. }
  308. void XmppClient::Private::OnSocketConnected() {
  309. engine_->Connect();
  310. }
  311. void XmppClient::Private::OnSocketRead() {
  312. char bytes[4096];
  313. size_t bytes_read;
  314. for (;;) {
  315. // Should not happen, but was observed in crash reports
  316. if (!socket_) {
  317. LOG(LS_ERROR) << "socket_ already reset";
  318. return;
  319. }
  320. if (!socket_->Read(bytes, sizeof(bytes), &bytes_read)) {
  321. // TODO: deal with error information
  322. return;
  323. }
  324. if (bytes_read == 0)
  325. return;
  326. //#ifdef _DEBUG
  327. client_->SignalLogInput(bytes, bytes_read);
  328. //#endif
  329. engine_->HandleInput(bytes, bytes_read);
  330. }
  331. }
  332. void XmppClient::Private::OnSocketClosed() {
  333. int code = socket_->GetError();
  334. engine_->ConnectionClosed(code);
  335. }
  336. void XmppClient::Private::OnStateChange(int state) {
  337. if (state == XmppEngine::STATE_CLOSED) {
  338. client_->EnsureClosed();
  339. }
  340. else {
  341. client_->SignalStateChange((XmppEngine::State)state);
  342. }
  343. client_->Wake();
  344. }
  345. void XmppClient::Private::WriteOutput(const char* bytes, size_t len) {
  346. //#ifdef _DEBUG
  347. client_->SignalLogOutput(bytes, len);
  348. //#endif
  349. socket_->Write(bytes, len);
  350. // TODO: deal with error information
  351. }
  352. void XmppClient::Private::StartTls(const std::string& domain) {
  353. #if defined(FEATURE_ENABLE_SSL)
  354. socket_->StartTls(domain);
  355. #endif
  356. }
  357. void XmppClient::Private::CloseConnection() {
  358. socket_->Close();
  359. }
  360. void XmppClient::AddXmppTask(XmppTask* task, XmppEngine::HandlerLevel level) {
  361. d_->engine_->AddStanzaHandler(task, level);
  362. }
  363. void XmppClient::RemoveXmppTask(XmppTask* task) {
  364. d_->engine_->RemoveStanzaHandler(task);
  365. }
  366. void XmppClient::EnsureClosed() {
  367. if (!d_->signal_closed_) {
  368. d_->signal_closed_ = true;
  369. delivering_signal_ = true;
  370. SignalStateChange(XmppEngine::STATE_CLOSED);
  371. delivering_signal_ = false;
  372. }
  373. }
  374. } // namespace buzz