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.

CConnection.cxx 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <rfb/Exception.h>
  21. #include <rfb/CMsgReaderV3.h>
  22. #include <rfb/CMsgWriterV3.h>
  23. #include <rfb/CSecurity.h>
  24. #include <rfb/Security.h>
  25. #include <rfb/CConnection.h>
  26. #include <rfb/util.h>
  27. #include <rfb/LogWriter.h>
  28. using namespace rfb;
  29. static LogWriter vlog("CConnection");
  30. CConnection::CConnection()
  31. : csecurity(0), is(0), os(0), reader_(0), writer_(0),
  32. shared(false), nSecTypes(0), clientSecTypeOrder(false),
  33. state_(RFBSTATE_UNINITIALISED), useProtocol3_3(false)
  34. {
  35. security = new Security();
  36. }
  37. CConnection::~CConnection()
  38. {
  39. if (csecurity) csecurity->destroy();
  40. deleteReaderAndWriter();
  41. }
  42. void CConnection::deleteReaderAndWriter()
  43. {
  44. delete reader_;
  45. reader_ = 0;
  46. delete writer_;
  47. writer_ = 0;
  48. }
  49. void CConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
  50. {
  51. is = is_;
  52. os = os_;
  53. }
  54. void CConnection::addSecType(rdr::U8 secType)
  55. {
  56. if (nSecTypes == maxSecTypes)
  57. throw Exception("too many security types");
  58. secTypes[nSecTypes++] = secType;
  59. }
  60. void CConnection::setClientSecTypeOrder(bool clientOrder) {
  61. clientSecTypeOrder = clientOrder;
  62. }
  63. void CConnection::initialiseProtocol()
  64. {
  65. state_ = RFBSTATE_PROTOCOL_VERSION;
  66. }
  67. void CConnection::processMsg()
  68. {
  69. switch (state_) {
  70. case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break;
  71. case RFBSTATE_SECURITY_TYPES: processSecurityTypesMsg(); break;
  72. case RFBSTATE_SECURITY: processSecurityMsg(); break;
  73. case RFBSTATE_SECURITY_RESULT: processSecurityResultMsg(); break;
  74. case RFBSTATE_INITIALISATION: processInitMsg(); break;
  75. case RFBSTATE_NORMAL: reader_->readMsg(); break;
  76. case RFBSTATE_UNINITIALISED:
  77. throw Exception("CConnection::processMsg: not initialised yet?");
  78. default:
  79. throw Exception("CConnection::processMsg: invalid state");
  80. }
  81. }
  82. void CConnection::processVersionMsg()
  83. {
  84. vlog.debug("reading protocol version");
  85. bool done;
  86. if (!cp.readVersion(is, &done)) {
  87. state_ = RFBSTATE_INVALID;
  88. throw Exception("reading version failed: not an RFB server?");
  89. }
  90. if (!done) return;
  91. vlog.info("Server supports RFB protocol version %d.%d",
  92. cp.majorVersion, cp.minorVersion);
  93. // The only official RFB protocol versions are currently 3.3, 3.7 and 3.8
  94. if (cp.beforeVersion(3,3)) {
  95. char msg[256];
  96. sprintf(msg,"Server gave unsupported RFB protocol version %d.%d",
  97. cp.majorVersion, cp.minorVersion);
  98. vlog.error(msg);
  99. state_ = RFBSTATE_INVALID;
  100. throw Exception(msg);
  101. } else if (useProtocol3_3 || cp.beforeVersion(3,7)) {
  102. cp.setVersion(3,3);
  103. } else if (cp.afterVersion(3,8)) {
  104. cp.setVersion(3,8);
  105. }
  106. cp.writeVersion(os);
  107. state_ = RFBSTATE_SECURITY_TYPES;
  108. vlog.info("Using RFB protocol version %d.%d",
  109. cp.majorVersion, cp.minorVersion);
  110. }
  111. void CConnection::processSecurityTypesMsg()
  112. {
  113. vlog.debug("processing security types message");
  114. int secType = secTypeInvalid;
  115. if (cp.isVersion(3,3)) {
  116. // legacy 3.3 server may only offer "vnc authentication" or "none"
  117. secType = is->readU32();
  118. if (secType == secTypeInvalid) {
  119. throwConnFailedException();
  120. } else if (secType == secTypeNone || secType == secTypeVncAuth) {
  121. int j;
  122. for (j = 0; j < nSecTypes; j++)
  123. if (secTypes[j] == secType) break;
  124. if (j == nSecTypes)
  125. secType = secTypeInvalid;
  126. } else {
  127. vlog.error("Unknown 3.3 security type %d", secType);
  128. throw Exception("Unknown 3.3 security type");
  129. }
  130. } else {
  131. // >=3.7 server will offer us a list
  132. int nServerSecTypes = is->readU8();
  133. if (nServerSecTypes == 0)
  134. throwConnFailedException();
  135. int secTypePos = nSecTypes;
  136. for (int i = 0; i < nServerSecTypes; i++) {
  137. rdr::U8 serverSecType = is->readU8();
  138. vlog.debug("Server offers security type %s(%d)",
  139. secTypeName(serverSecType),serverSecType);
  140. // If we haven't already chosen a secType, try this one
  141. // If we are using the client's preference for types,
  142. // we keep trying types, to find the one that matches and
  143. // which appears first in the client's list of supported types.
  144. if (secType == secTypeInvalid || clientSecTypeOrder) {
  145. for (int j = 0; j < nSecTypes; j++) {
  146. if (secTypes[j] == serverSecType && j < secTypePos) {
  147. secType = secTypes[j];
  148. secTypePos = j;
  149. break;
  150. }
  151. }
  152. // NB: Continue reading the remaining server secTypes, but ignore them
  153. }
  154. }
  155. // Inform the server of our decision
  156. if (secType != secTypeInvalid) {
  157. os->writeU8(secType);
  158. os->flush();
  159. vlog.debug("Choosing security type %s(%d)",secTypeName(secType),secType);
  160. }
  161. }
  162. if (secType == secTypeInvalid) {
  163. state_ = RFBSTATE_INVALID;
  164. vlog.error("No matching security types");
  165. throw Exception("No matching security types");
  166. }
  167. state_ = RFBSTATE_SECURITY;
  168. csecurity = security->GetCSecurity(secType);
  169. processSecurityMsg();
  170. }
  171. void CConnection::processSecurityMsg()
  172. {
  173. vlog.debug("processing security message");
  174. if (csecurity->processMsg(this)) {
  175. state_ = RFBSTATE_SECURITY_RESULT;
  176. processSecurityResultMsg();
  177. }
  178. }
  179. void CConnection::processSecurityResultMsg()
  180. {
  181. vlog.debug("processing security result message");
  182. int result;
  183. if (cp.beforeVersion(3,8) && csecurity->getType() == secTypeNone) {
  184. result = secResultOK;
  185. } else {
  186. if (!is->checkNoWait(1)) return;
  187. result = is->readU32();
  188. }
  189. switch (result) {
  190. case secResultOK:
  191. securityCompleted();
  192. return;
  193. case secResultFailed:
  194. vlog.debug("auth failed");
  195. break;
  196. case secResultTooMany:
  197. vlog.debug("auth failed - too many tries");
  198. break;
  199. default:
  200. throw Exception("Unknown security result from server");
  201. }
  202. CharArray reason;
  203. if (cp.beforeVersion(3,8))
  204. reason.buf = strDup("Authentication failure");
  205. else
  206. reason.buf = is->readString();
  207. state_ = RFBSTATE_INVALID;
  208. throw AuthFailureException(reason.buf);
  209. }
  210. void CConnection::processInitMsg()
  211. {
  212. vlog.debug("reading server initialisation");
  213. reader_->readServerInit();
  214. }
  215. void CConnection::throwConnFailedException()
  216. {
  217. state_ = RFBSTATE_INVALID;
  218. CharArray reason;
  219. reason.buf = is->readString();
  220. throw ConnFailedException(reason.buf);
  221. }
  222. void CConnection::securityCompleted()
  223. {
  224. state_ = RFBSTATE_INITIALISATION;
  225. reader_ = new CMsgReaderV3(this, is);
  226. writer_ = new CMsgWriterV3(&cp, os);
  227. vlog.debug("Authentication success!");
  228. authSuccess();
  229. writer_->writeClientInit(shared);
  230. }
  231. void CConnection::authSuccess()
  232. {
  233. }
  234. void CConnection::serverInit()
  235. {
  236. state_ = RFBSTATE_NORMAL;
  237. vlog.debug("initialisation done");
  238. }