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.

SConnection.cxx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2019 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <rfb/Exception.h>
  22. #include <rfb/Security.h>
  23. #include <rfb/clipboardTypes.h>
  24. #include <rfb/msgTypes.h>
  25. #include <rfb/fenceTypes.h>
  26. #include <rfb/SMsgReader.h>
  27. #include <rfb/SMsgWriter.h>
  28. #include <rfb/SConnection.h>
  29. #include <rfb/ServerCore.h>
  30. #include <rfb/encodings.h>
  31. #include <rfb/EncodeManager.h>
  32. #include <rfb/SSecurity.h>
  33. #include <rfb/LogWriter.h>
  34. using namespace rfb;
  35. static LogWriter vlog("SConnection");
  36. // AccessRights values
  37. const SConnection::AccessRights SConnection::AccessView = 0x0001;
  38. const SConnection::AccessRights SConnection::AccessKeyEvents = 0x0002;
  39. const SConnection::AccessRights SConnection::AccessPtrEvents = 0x0004;
  40. const SConnection::AccessRights SConnection::AccessCutText = 0x0008;
  41. const SConnection::AccessRights SConnection::AccessSetDesktopSize = 0x0010;
  42. const SConnection::AccessRights SConnection::AccessNonShared = 0x0020;
  43. const SConnection::AccessRights SConnection::AccessDefault = 0x03ff;
  44. const SConnection::AccessRights SConnection::AccessNoQuery = 0x0400;
  45. const SConnection::AccessRights SConnection::AccessFull = 0xffff;
  46. SConnection::SConnection()
  47. : readyForSetColourMapEntries(false),
  48. is(0), os(0), reader_(0), writer_(0),
  49. ssecurity(0), state_(RFBSTATE_UNINITIALISED),
  50. preferredEncoding(encodingRaw),
  51. clientClipboard(NULL), hasLocalClipboard(false)
  52. {
  53. defaultMajorVersion = 3;
  54. defaultMinorVersion = 8;
  55. if (rfb::Server::protocol3_3)
  56. defaultMinorVersion = 3;
  57. client.setVersion(defaultMajorVersion, defaultMinorVersion);
  58. }
  59. SConnection::~SConnection()
  60. {
  61. if (ssecurity)
  62. delete ssecurity;
  63. delete reader_;
  64. reader_ = 0;
  65. delete writer_;
  66. writer_ = 0;
  67. strFree(clientClipboard);
  68. }
  69. void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
  70. {
  71. is = is_;
  72. os = os_;
  73. }
  74. void SConnection::initialiseProtocol()
  75. {
  76. char str[13];
  77. sprintf(str, "RFB %03d.%03d\n", defaultMajorVersion, defaultMinorVersion);
  78. os->writeBytes(str, 12);
  79. os->flush();
  80. state_ = RFBSTATE_PROTOCOL_VERSION;
  81. }
  82. void SConnection::processMsg()
  83. {
  84. switch (state_) {
  85. case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break;
  86. case RFBSTATE_SECURITY_TYPE: processSecurityTypeMsg(); break;
  87. case RFBSTATE_SECURITY: processSecurityMsg(); break;
  88. case RFBSTATE_INITIALISATION: processInitMsg(); break;
  89. case RFBSTATE_NORMAL: reader_->readMsg(); break;
  90. case RFBSTATE_QUERYING:
  91. throw Exception("SConnection::processMsg: bogus data from client while "
  92. "querying");
  93. case RFBSTATE_UNINITIALISED:
  94. throw Exception("SConnection::processMsg: not initialised yet?");
  95. default:
  96. throw Exception("SConnection::processMsg: invalid state");
  97. }
  98. }
  99. void SConnection::processVersionMsg()
  100. {
  101. char verStr[13];
  102. int majorVersion;
  103. int minorVersion;
  104. vlog.debug("reading protocol version");
  105. if (!is->checkNoWait(12))
  106. return;
  107. is->readBytes(verStr, 12);
  108. verStr[12] = '\0';
  109. if (sscanf(verStr, "RFB %03d.%03d\n",
  110. &majorVersion, &minorVersion) != 2) {
  111. state_ = RFBSTATE_INVALID;
  112. throw Exception("reading version failed: not an RFB client?");
  113. }
  114. client.setVersion(majorVersion, minorVersion);
  115. vlog.info("Client needs protocol version %d.%d",
  116. client.majorVersion, client.minorVersion);
  117. if (client.majorVersion != 3) {
  118. // unknown protocol version
  119. throwConnFailedException("Client needs protocol version %d.%d, server has %d.%d",
  120. client.majorVersion, client.minorVersion,
  121. defaultMajorVersion, defaultMinorVersion);
  122. }
  123. if (client.minorVersion != 3 && client.minorVersion != 7 && client.minorVersion != 8) {
  124. vlog.error("Client uses unofficial protocol version %d.%d",
  125. client.majorVersion,client.minorVersion);
  126. if (client.minorVersion >= 8)
  127. client.minorVersion = 8;
  128. else if (client.minorVersion == 7)
  129. client.minorVersion = 7;
  130. else
  131. client.minorVersion = 3;
  132. vlog.error("Assuming compatibility with version %d.%d",
  133. client.majorVersion,client.minorVersion);
  134. }
  135. versionReceived();
  136. std::list<rdr::U8> secTypes;
  137. std::list<rdr::U8>::iterator i;
  138. secTypes = security.GetEnabledSecTypes();
  139. if (client.isVersion(3,3)) {
  140. // cope with legacy 3.3 client only if "no authentication" or "vnc
  141. // authentication" is supported.
  142. for (i=secTypes.begin(); i!=secTypes.end(); i++) {
  143. if (*i == secTypeNone || *i == secTypeVncAuth) break;
  144. }
  145. if (i == secTypes.end()) {
  146. throwConnFailedException("No supported security type for %d.%d client",
  147. client.majorVersion, client.minorVersion);
  148. }
  149. os->writeU32(*i);
  150. if (*i == secTypeNone) os->flush();
  151. state_ = RFBSTATE_SECURITY;
  152. ssecurity = security.GetSSecurity(this, *i);
  153. processSecurityMsg();
  154. return;
  155. }
  156. // list supported security types for >=3.7 clients
  157. if (secTypes.empty())
  158. throwConnFailedException("No supported security types");
  159. os->writeU8(secTypes.size());
  160. for (i=secTypes.begin(); i!=secTypes.end(); i++)
  161. os->writeU8(*i);
  162. os->flush();
  163. state_ = RFBSTATE_SECURITY_TYPE;
  164. }
  165. void SConnection::processSecurityTypeMsg()
  166. {
  167. vlog.debug("processing security type message");
  168. int secType = is->readU8();
  169. processSecurityType(secType);
  170. }
  171. void SConnection::processSecurityType(int secType)
  172. {
  173. // Verify that the requested security type should be offered
  174. std::list<rdr::U8> secTypes;
  175. std::list<rdr::U8>::iterator i;
  176. secTypes = security.GetEnabledSecTypes();
  177. for (i=secTypes.begin(); i!=secTypes.end(); i++)
  178. if (*i == secType) break;
  179. if (i == secTypes.end())
  180. throw Exception("Requested security type not available");
  181. vlog.info("Client requests security type %s(%d)",
  182. secTypeName(secType),secType);
  183. try {
  184. state_ = RFBSTATE_SECURITY;
  185. ssecurity = security.GetSSecurity(this, secType);
  186. } catch (rdr::Exception& e) {
  187. throwConnFailedException("%s", e.str());
  188. }
  189. processSecurityMsg();
  190. }
  191. void SConnection::processSecurityMsg()
  192. {
  193. vlog.debug("processing security message");
  194. try {
  195. bool done = ssecurity->processMsg();
  196. if (done) {
  197. state_ = RFBSTATE_QUERYING;
  198. setAccessRights(ssecurity->getAccessRights());
  199. queryConnection(ssecurity->getUserName());
  200. }
  201. } catch (AuthFailureException& e) {
  202. vlog.error("AuthFailureException: %s", e.str());
  203. state_ = RFBSTATE_SECURITY_FAILURE;
  204. authFailure(e.str());
  205. }
  206. }
  207. void SConnection::processInitMsg()
  208. {
  209. vlog.debug("reading client initialisation");
  210. reader_->readClientInit();
  211. }
  212. void SConnection::throwConnFailedException(const char* format, ...)
  213. {
  214. va_list ap;
  215. char str[256];
  216. va_start(ap, format);
  217. (void) vsnprintf(str, sizeof(str), format, ap);
  218. va_end(ap);
  219. vlog.info("Connection failed: %s", str);
  220. if (state_ == RFBSTATE_PROTOCOL_VERSION) {
  221. if (client.majorVersion == 3 && client.minorVersion == 3) {
  222. os->writeU32(0);
  223. os->writeString(str);
  224. os->flush();
  225. } else {
  226. os->writeU8(0);
  227. os->writeString(str);
  228. os->flush();
  229. }
  230. }
  231. state_ = RFBSTATE_INVALID;
  232. throw ConnFailedException(str);
  233. }
  234. void SConnection::setAccessRights(AccessRights ar)
  235. {
  236. accessRights = ar;
  237. }
  238. bool SConnection::accessCheck(AccessRights ar) const
  239. {
  240. return (accessRights & ar) == ar;
  241. }
  242. void SConnection::setEncodings(int nEncodings, const rdr::S32* encodings)
  243. {
  244. int i;
  245. preferredEncoding = encodingRaw;
  246. for (i = 0;i < nEncodings;i++) {
  247. if (EncodeManager::supported(encodings[i])) {
  248. preferredEncoding = encodings[i];
  249. break;
  250. }
  251. }
  252. SMsgHandler::setEncodings(nEncodings, encodings);
  253. if (client.supportsEncoding(pseudoEncodingExtendedClipboard)) {
  254. rdr::U32 sizes[] = { 0 };
  255. writer()->writeClipboardCaps(rfb::clipboardUTF8 |
  256. rfb::clipboardRequest |
  257. rfb::clipboardPeek |
  258. rfb::clipboardNotify |
  259. rfb::clipboardProvide,
  260. sizes);
  261. }
  262. }
  263. void SConnection::clientCutText(const char* str)
  264. {
  265. strFree(clientClipboard);
  266. clientClipboard = NULL;
  267. clientClipboard = latin1ToUTF8(str);
  268. handleClipboardAnnounce(true);
  269. }
  270. void SConnection::handleClipboardRequest(rdr::U32 flags)
  271. {
  272. if (!(flags & rfb::clipboardUTF8))
  273. return;
  274. if (!hasLocalClipboard)
  275. return;
  276. handleClipboardRequest();
  277. }
  278. void SConnection::handleClipboardPeek(rdr::U32 flags)
  279. {
  280. if (!hasLocalClipboard)
  281. return;
  282. if (client.clipboardFlags() & rfb::clipboardNotify)
  283. writer()->writeClipboardNotify(rfb::clipboardUTF8);
  284. }
  285. void SConnection::handleClipboardNotify(rdr::U32 flags)
  286. {
  287. strFree(clientClipboard);
  288. clientClipboard = NULL;
  289. if (flags & rfb::clipboardUTF8)
  290. handleClipboardAnnounce(true);
  291. else
  292. handleClipboardAnnounce(false);
  293. }
  294. void SConnection::handleClipboardProvide(rdr::U32 flags,
  295. const size_t* lengths,
  296. const rdr::U8* const* data)
  297. {
  298. if (!(flags & rfb::clipboardUTF8))
  299. return;
  300. strFree(clientClipboard);
  301. clientClipboard = NULL;
  302. clientClipboard = convertLF((const char*)data[0], lengths[0]);
  303. handleClipboardData(clientClipboard);
  304. }
  305. void SConnection::supportsQEMUKeyEvent()
  306. {
  307. writer()->writeQEMUKeyEvent();
  308. }
  309. void SConnection::versionReceived()
  310. {
  311. }
  312. void SConnection::authSuccess()
  313. {
  314. }
  315. void SConnection::authFailure(const char* reason)
  316. {
  317. if (state_ != RFBSTATE_SECURITY_FAILURE)
  318. throw Exception("SConnection::authFailure: invalid state");
  319. os->writeU32(secResultFailed);
  320. if (!client.beforeVersion(3,8)) // 3.8 onwards have failure message
  321. os->writeString(reason);
  322. os->flush();
  323. throw AuthFailureException(reason);
  324. }
  325. void SConnection::queryConnection(const char* userName)
  326. {
  327. approveConnection(true);
  328. }
  329. void SConnection::approveConnection(bool accept, const char* reason)
  330. {
  331. if (state_ != RFBSTATE_QUERYING)
  332. throw Exception("SConnection::approveConnection: invalid state");
  333. if (!client.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) {
  334. if (accept) {
  335. os->writeU32(secResultOK);
  336. } else {
  337. os->writeU32(secResultFailed);
  338. if (!client.beforeVersion(3,8)) { // 3.8 onwards have failure message
  339. if (reason)
  340. os->writeString(reason);
  341. else
  342. os->writeString("Authentication failure");
  343. }
  344. }
  345. os->flush();
  346. }
  347. if (accept) {
  348. state_ = RFBSTATE_INITIALISATION;
  349. reader_ = new SMsgReader(this, is);
  350. writer_ = new SMsgWriter(&client, os);
  351. authSuccess();
  352. } else {
  353. state_ = RFBSTATE_INVALID;
  354. if (reason)
  355. throw AuthFailureException(reason);
  356. else
  357. throw AuthFailureException();
  358. }
  359. }
  360. void SConnection::clientInit(bool shared)
  361. {
  362. writer_->writeServerInit(client.width(), client.height(),
  363. client.pf(), client.name());
  364. state_ = RFBSTATE_NORMAL;
  365. }
  366. void SConnection::close(const char* reason)
  367. {
  368. state_ = RFBSTATE_CLOSING;
  369. }
  370. void SConnection::setPixelFormat(const PixelFormat& pf)
  371. {
  372. SMsgHandler::setPixelFormat(pf);
  373. readyForSetColourMapEntries = true;
  374. if (!pf.trueColour)
  375. writeFakeColourMap();
  376. }
  377. void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental)
  378. {
  379. if (!readyForSetColourMapEntries) {
  380. readyForSetColourMapEntries = true;
  381. if (!client.pf().trueColour) {
  382. writeFakeColourMap();
  383. }
  384. }
  385. }
  386. void SConnection::fence(rdr::U32 flags, unsigned len, const char data[])
  387. {
  388. if (!(flags & fenceFlagRequest))
  389. return;
  390. // We cannot guarantee any synchronisation at this level
  391. flags = 0;
  392. writer()->writeFence(flags, len, data);
  393. }
  394. void SConnection::enableContinuousUpdates(bool enable,
  395. int x, int y, int w, int h)
  396. {
  397. }
  398. void SConnection::handleClipboardRequest()
  399. {
  400. }
  401. void SConnection::handleClipboardAnnounce(bool available)
  402. {
  403. }
  404. void SConnection::handleClipboardData(const char* data)
  405. {
  406. }
  407. void SConnection::requestClipboard()
  408. {
  409. if (clientClipboard != NULL) {
  410. handleClipboardData(clientClipboard);
  411. return;
  412. }
  413. if (client.supportsEncoding(pseudoEncodingExtendedClipboard) &&
  414. (client.clipboardFlags() & rfb::clipboardRequest))
  415. writer()->writeClipboardRequest(rfb::clipboardUTF8);
  416. }
  417. void SConnection::announceClipboard(bool available)
  418. {
  419. hasLocalClipboard = available;
  420. if (client.supportsEncoding(pseudoEncodingExtendedClipboard) &&
  421. (client.clipboardFlags() & rfb::clipboardNotify))
  422. writer()->writeClipboardNotify(available ? rfb::clipboardUTF8 : 0);
  423. else {
  424. if (available)
  425. handleClipboardRequest();
  426. }
  427. }
  428. void SConnection::sendClipboardData(const char* data)
  429. {
  430. if (client.supportsEncoding(pseudoEncodingExtendedClipboard) &&
  431. (client.clipboardFlags() & rfb::clipboardProvide)) {
  432. CharArray filtered(convertCRLF(data));
  433. size_t sizes[1] = { strlen(filtered.buf) + 1 };
  434. const rdr::U8* data[1] = { (const rdr::U8*)filtered.buf };
  435. writer()->writeClipboardProvide(rfb::clipboardUTF8, sizes, data);
  436. } else {
  437. CharArray latin1(utf8ToLatin1(data));
  438. writer()->writeServerCutText(latin1.buf);
  439. }
  440. }
  441. void SConnection::writeFakeColourMap(void)
  442. {
  443. int i;
  444. rdr::U16 red[256], green[256], blue[256];
  445. for (i = 0;i < 256;i++)
  446. client.pf().rgbFromPixel(i, &red[i], &green[i], &blue[i]);
  447. writer()->writeSetColourMapEntries(0, 256, red, green, blue);
  448. }