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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011 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/msgTypes.h>
  24. #include <rfb/fenceTypes.h>
  25. #include <rfb/SMsgReader.h>
  26. #include <rfb/SMsgWriter.h>
  27. #include <rfb/SConnection.h>
  28. #include <rfb/ServerCore.h>
  29. #include <rfb/encodings.h>
  30. #include <rfb/EncodeManager.h>
  31. #include <rfb/SSecurity.h>
  32. #include <rfb/LogWriter.h>
  33. using namespace rfb;
  34. static LogWriter vlog("SConnection");
  35. // AccessRights values
  36. const SConnection::AccessRights SConnection::AccessView = 0x0001;
  37. const SConnection::AccessRights SConnection::AccessKeyEvents = 0x0002;
  38. const SConnection::AccessRights SConnection::AccessPtrEvents = 0x0004;
  39. const SConnection::AccessRights SConnection::AccessCutText = 0x0008;
  40. const SConnection::AccessRights SConnection::AccessSetDesktopSize = 0x0010;
  41. const SConnection::AccessRights SConnection::AccessNonShared = 0x0020;
  42. const SConnection::AccessRights SConnection::AccessDefault = 0x03ff;
  43. const SConnection::AccessRights SConnection::AccessNoQuery = 0x0400;
  44. const SConnection::AccessRights SConnection::AccessFull = 0xffff;
  45. SConnection::SConnection()
  46. : readyForSetColourMapEntries(false),
  47. is(0), os(0), reader_(0), writer_(0),
  48. ssecurity(0), state_(RFBSTATE_UNINITIALISED),
  49. preferredEncoding(encodingRaw)
  50. {
  51. defaultMajorVersion = 3;
  52. defaultMinorVersion = 8;
  53. if (rfb::Server::protocol3_3)
  54. defaultMinorVersion = 3;
  55. client.setVersion(defaultMajorVersion, defaultMinorVersion);
  56. }
  57. SConnection::~SConnection()
  58. {
  59. if (ssecurity)
  60. delete ssecurity;
  61. delete reader_;
  62. reader_ = 0;
  63. delete writer_;
  64. writer_ = 0;
  65. }
  66. void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
  67. {
  68. is = is_;
  69. os = os_;
  70. }
  71. void SConnection::initialiseProtocol()
  72. {
  73. char str[13];
  74. sprintf(str, "RFB %03d.%03d\n", defaultMajorVersion, defaultMinorVersion);
  75. os->writeBytes(str, 12);
  76. os->flush();
  77. state_ = RFBSTATE_PROTOCOL_VERSION;
  78. }
  79. void SConnection::processMsg()
  80. {
  81. switch (state_) {
  82. case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break;
  83. case RFBSTATE_SECURITY_TYPE: processSecurityTypeMsg(); break;
  84. case RFBSTATE_SECURITY: processSecurityMsg(); break;
  85. case RFBSTATE_INITIALISATION: processInitMsg(); break;
  86. case RFBSTATE_NORMAL: reader_->readMsg(); break;
  87. case RFBSTATE_QUERYING:
  88. throw Exception("SConnection::processMsg: bogus data from client while "
  89. "querying");
  90. case RFBSTATE_UNINITIALISED:
  91. throw Exception("SConnection::processMsg: not initialised yet?");
  92. default:
  93. throw Exception("SConnection::processMsg: invalid state");
  94. }
  95. }
  96. void SConnection::processVersionMsg()
  97. {
  98. char verStr[13];
  99. int majorVersion;
  100. int minorVersion;
  101. vlog.debug("reading protocol version");
  102. if (!is->checkNoWait(12))
  103. return;
  104. is->readBytes(verStr, 12);
  105. verStr[12] = '\0';
  106. if (sscanf(verStr, "RFB %03d.%03d\n",
  107. &majorVersion, &minorVersion) != 2) {
  108. state_ = RFBSTATE_INVALID;
  109. throw Exception("reading version failed: not an RFB client?");
  110. }
  111. client.setVersion(majorVersion, minorVersion);
  112. vlog.info("Client needs protocol version %d.%d",
  113. client.majorVersion, client.minorVersion);
  114. if (client.majorVersion != 3) {
  115. // unknown protocol version
  116. throwConnFailedException("Client needs protocol version %d.%d, server has %d.%d",
  117. client.majorVersion, client.minorVersion,
  118. defaultMajorVersion, defaultMinorVersion);
  119. }
  120. if (client.minorVersion != 3 && client.minorVersion != 7 && client.minorVersion != 8) {
  121. vlog.error("Client uses unofficial protocol version %d.%d",
  122. client.majorVersion,client.minorVersion);
  123. if (client.minorVersion >= 8)
  124. client.minorVersion = 8;
  125. else if (client.minorVersion == 7)
  126. client.minorVersion = 7;
  127. else
  128. client.minorVersion = 3;
  129. vlog.error("Assuming compatibility with version %d.%d",
  130. client.majorVersion,client.minorVersion);
  131. }
  132. versionReceived();
  133. std::list<rdr::U8> secTypes;
  134. std::list<rdr::U8>::iterator i;
  135. secTypes = security.GetEnabledSecTypes();
  136. if (client.isVersion(3,3)) {
  137. // cope with legacy 3.3 client only if "no authentication" or "vnc
  138. // authentication" is supported.
  139. for (i=secTypes.begin(); i!=secTypes.end(); i++) {
  140. if (*i == secTypeNone || *i == secTypeVncAuth) break;
  141. }
  142. if (i == secTypes.end()) {
  143. throwConnFailedException("No supported security type for %d.%d client",
  144. client.majorVersion, client.minorVersion);
  145. }
  146. os->writeU32(*i);
  147. if (*i == secTypeNone) os->flush();
  148. state_ = RFBSTATE_SECURITY;
  149. ssecurity = security.GetSSecurity(this, *i);
  150. processSecurityMsg();
  151. return;
  152. }
  153. // list supported security types for >=3.7 clients
  154. if (secTypes.empty())
  155. throwConnFailedException("No supported security types");
  156. os->writeU8(secTypes.size());
  157. for (i=secTypes.begin(); i!=secTypes.end(); i++)
  158. os->writeU8(*i);
  159. os->flush();
  160. state_ = RFBSTATE_SECURITY_TYPE;
  161. }
  162. void SConnection::processSecurityTypeMsg()
  163. {
  164. vlog.debug("processing security type message");
  165. int secType = is->readU8();
  166. processSecurityType(secType);
  167. }
  168. void SConnection::processSecurityType(int secType)
  169. {
  170. // Verify that the requested security type should be offered
  171. std::list<rdr::U8> secTypes;
  172. std::list<rdr::U8>::iterator i;
  173. secTypes = security.GetEnabledSecTypes();
  174. for (i=secTypes.begin(); i!=secTypes.end(); i++)
  175. if (*i == secType) break;
  176. if (i == secTypes.end())
  177. throw Exception("Requested security type not available");
  178. vlog.info("Client requests security type %s(%d)",
  179. secTypeName(secType),secType);
  180. try {
  181. state_ = RFBSTATE_SECURITY;
  182. ssecurity = security.GetSSecurity(this, secType);
  183. } catch (rdr::Exception& e) {
  184. throwConnFailedException("%s", e.str());
  185. }
  186. processSecurityMsg();
  187. }
  188. void SConnection::processSecurityMsg()
  189. {
  190. vlog.debug("processing security message");
  191. try {
  192. bool done = ssecurity->processMsg();
  193. if (done) {
  194. state_ = RFBSTATE_QUERYING;
  195. setAccessRights(ssecurity->getAccessRights());
  196. queryConnection(ssecurity->getUserName());
  197. }
  198. } catch (AuthFailureException& e) {
  199. vlog.error("AuthFailureException: %s", e.str());
  200. os->writeU32(secResultFailed);
  201. if (!client.beforeVersion(3,8)) // 3.8 onwards have failure message
  202. os->writeString(e.str());
  203. os->flush();
  204. throw;
  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. }
  254. void SConnection::supportsQEMUKeyEvent()
  255. {
  256. writer()->writeQEMUKeyEvent();
  257. }
  258. void SConnection::versionReceived()
  259. {
  260. }
  261. void SConnection::authSuccess()
  262. {
  263. }
  264. void SConnection::queryConnection(const char* userName)
  265. {
  266. approveConnection(true);
  267. }
  268. void SConnection::approveConnection(bool accept, const char* reason)
  269. {
  270. if (state_ != RFBSTATE_QUERYING)
  271. throw Exception("SConnection::approveConnection: invalid state");
  272. if (!client.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) {
  273. if (accept) {
  274. os->writeU32(secResultOK);
  275. } else {
  276. os->writeU32(secResultFailed);
  277. if (!client.beforeVersion(3,8)) { // 3.8 onwards have failure message
  278. if (reason)
  279. os->writeString(reason);
  280. else
  281. os->writeString("Authentication failure");
  282. }
  283. }
  284. os->flush();
  285. }
  286. if (accept) {
  287. state_ = RFBSTATE_INITIALISATION;
  288. reader_ = new SMsgReader(this, is);
  289. writer_ = new SMsgWriter(&client, os);
  290. authSuccess();
  291. } else {
  292. state_ = RFBSTATE_INVALID;
  293. if (reason)
  294. throw AuthFailureException(reason);
  295. else
  296. throw AuthFailureException();
  297. }
  298. }
  299. void SConnection::clientInit(bool shared)
  300. {
  301. writer_->writeServerInit(client.width(), client.height(),
  302. client.pf(), client.name());
  303. state_ = RFBSTATE_NORMAL;
  304. }
  305. void SConnection::close(const char* reason)
  306. {
  307. state_ = RFBSTATE_CLOSING;
  308. }
  309. void SConnection::setPixelFormat(const PixelFormat& pf)
  310. {
  311. SMsgHandler::setPixelFormat(pf);
  312. readyForSetColourMapEntries = true;
  313. if (!pf.trueColour)
  314. writeFakeColourMap();
  315. }
  316. void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental)
  317. {
  318. if (!readyForSetColourMapEntries) {
  319. readyForSetColourMapEntries = true;
  320. if (!client.pf().trueColour) {
  321. writeFakeColourMap();
  322. }
  323. }
  324. }
  325. void SConnection::fence(rdr::U32 flags, unsigned len, const char data[])
  326. {
  327. if (!(flags & fenceFlagRequest))
  328. return;
  329. // We cannot guarantee any synchronisation at this level
  330. flags = 0;
  331. writer()->writeFence(flags, len, data);
  332. }
  333. void SConnection::enableContinuousUpdates(bool enable,
  334. int x, int y, int w, int h)
  335. {
  336. }
  337. void SConnection::writeFakeColourMap(void)
  338. {
  339. int i;
  340. rdr::U16 red[256], green[256], blue[256];
  341. for (i = 0;i < 256;i++)
  342. client.pf().rgbFromPixel(i, &red[i], &green[i], &blue[i]);
  343. writer()->writeSetColourMapEntries(0, 256, red, green, blue);
  344. }