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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* Copyright (C) 2002-2004 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/secTypes.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. : is(0), os(0), reader_(0), writer_(0),
  32. shared(false), security(0), nSecTypes(0), clientSecTypeOrder(false),
  33. state_(RFBSTATE_UNINITIALISED), useProtocol3_3(false)
  34. {
  35. }
  36. CConnection::~CConnection()
  37. {
  38. if (security) security->destroy();
  39. deleteReaderAndWriter();
  40. }
  41. void CConnection::setServerName(const char* serverName_) {
  42. serverName.buf = strDup(serverName_);
  43. }
  44. void CConnection::deleteReaderAndWriter()
  45. {
  46. delete reader_;
  47. reader_ = 0;
  48. delete writer_;
  49. writer_ = 0;
  50. }
  51. void CConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
  52. {
  53. is = is_;
  54. os = os_;
  55. }
  56. void CConnection::addSecType(rdr::U8 secType)
  57. {
  58. if (nSecTypes == maxSecTypes)
  59. throw Exception("too many security types");
  60. secTypes[nSecTypes++] = secType;
  61. }
  62. void CConnection::setClientSecTypeOrder(bool clientOrder) {
  63. clientSecTypeOrder = clientOrder;
  64. }
  65. void CConnection::initialiseProtocol()
  66. {
  67. state_ = RFBSTATE_PROTOCOL_VERSION;
  68. }
  69. void CConnection::processMsg()
  70. {
  71. switch (state_) {
  72. case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break;
  73. case RFBSTATE_SECURITY_TYPES: processSecurityTypesMsg(); break;
  74. case RFBSTATE_SECURITY: processSecurityMsg(); break;
  75. case RFBSTATE_SECURITY_RESULT: processSecurityResultMsg(); break;
  76. case RFBSTATE_INITIALISATION: processInitMsg(); break;
  77. case RFBSTATE_NORMAL: reader_->readMsg(); break;
  78. case RFBSTATE_UNINITIALISED:
  79. throw Exception("CConnection::processMsg: not initialised yet?");
  80. default:
  81. throw Exception("CConnection::processMsg: invalid state");
  82. }
  83. }
  84. void CConnection::processVersionMsg()
  85. {
  86. vlog.debug("reading protocol version");
  87. bool done;
  88. if (!cp.readVersion(is, &done)) {
  89. state_ = RFBSTATE_INVALID;
  90. throw Exception("reading version failed: not an RFB server?");
  91. }
  92. if (!done) return;
  93. vlog.info("Server supports RFB protocol version %d.%d",
  94. cp.majorVersion, cp.minorVersion);
  95. // The only official RFB protocol versions are currently 3.3, 3.7 and 3.8
  96. if (cp.beforeVersion(3,3)) {
  97. char msg[256];
  98. sprintf(msg,"Server gave unsupported RFB protocol version %d.%d",
  99. cp.majorVersion, cp.minorVersion);
  100. vlog.error(msg);
  101. state_ = RFBSTATE_INVALID;
  102. throw Exception(msg);
  103. } else if (useProtocol3_3 || cp.beforeVersion(3,7)) {
  104. cp.setVersion(3,3);
  105. } else if (cp.afterVersion(3,8)) {
  106. cp.setVersion(3,8);
  107. }
  108. cp.writeVersion(os);
  109. state_ = RFBSTATE_SECURITY_TYPES;
  110. vlog.info("Using RFB protocol version %d.%d",
  111. cp.majorVersion, cp.minorVersion);
  112. }
  113. void CConnection::processSecurityTypesMsg()
  114. {
  115. vlog.debug("processing security types message");
  116. int secType = secTypeInvalid;
  117. if (cp.isVersion(3,3)) {
  118. // legacy 3.3 server may only offer "vnc authentication" or "none"
  119. secType = is->readU32();
  120. if (secType == secTypeInvalid) {
  121. throwConnFailedException();
  122. } else if (secType == secTypeNone || secType == secTypeVncAuth) {
  123. int j;
  124. for (j = 0; j < nSecTypes; j++)
  125. if (secTypes[j] == secType) break;
  126. if (j == nSecTypes)
  127. secType = secTypeInvalid;
  128. } else {
  129. vlog.error("Unknown 3.3 security type %d", secType);
  130. throw Exception("Unknown 3.3 security type");
  131. }
  132. } else {
  133. // >=3.7 server will offer us a list
  134. int nServerSecTypes = is->readU8();
  135. if (nServerSecTypes == 0)
  136. throwConnFailedException();
  137. int secTypePos = nSecTypes;
  138. for (int i = 0; i < nServerSecTypes; i++) {
  139. rdr::U8 serverSecType = is->readU8();
  140. vlog.debug("Server offers security type %s(%d)",
  141. secTypeName(serverSecType),serverSecType);
  142. // If we haven't already chosen a secType, try this one
  143. // If we are using the client's preference for types,
  144. // we keep trying types, to find the one that matches and
  145. // which appears first in the client's list of supported types.
  146. if (secType == secTypeInvalid || clientSecTypeOrder) {
  147. for (int j = 0; j < nSecTypes; j++) {
  148. if (secTypes[j] == serverSecType && j < secTypePos) {
  149. secType = secTypes[j];
  150. secTypePos = j;
  151. break;
  152. }
  153. }
  154. // NB: Continue reading the remaining server secTypes, but ignore them
  155. }
  156. }
  157. // Inform the server of our decision
  158. if (secType != secTypeInvalid) {
  159. os->writeU8(secType);
  160. os->flush();
  161. vlog.debug("Choosing security type %s(%d)",secTypeName(secType),secType);
  162. }
  163. }
  164. if (secType == secTypeInvalid) {
  165. state_ = RFBSTATE_INVALID;
  166. vlog.error("No matching security types");
  167. throw Exception("No matching security types");
  168. }
  169. state_ = RFBSTATE_SECURITY;
  170. security = getCSecurity(secType);
  171. processSecurityMsg();
  172. }
  173. void CConnection::processSecurityMsg()
  174. {
  175. vlog.debug("processing security message");
  176. bool done;
  177. if (!security->processMsg(this, &done))
  178. throwAuthFailureException();
  179. if (done) {
  180. state_ = RFBSTATE_SECURITY_RESULT;
  181. processSecurityResultMsg();
  182. }
  183. }
  184. void CConnection::processSecurityResultMsg()
  185. {
  186. vlog.debug("processing security result message");
  187. int result;
  188. if (cp.beforeVersion(3,8) && security->getType() == secTypeNone) {
  189. result = secResultOK;
  190. } else {
  191. if (!is->checkNoWait(1)) return;
  192. result = is->readU32();
  193. }
  194. switch (result) {
  195. case secResultOK:
  196. securityCompleted();
  197. break;
  198. case secResultFailed:
  199. vlog.debug("auth failed");
  200. throwAuthFailureException();
  201. case secResultTooMany:
  202. vlog.debug("auth failed - too many tries");
  203. throwAuthFailureException();
  204. default:
  205. vlog.error("unknown security result");
  206. throwAuthFailureException();
  207. };
  208. }
  209. void CConnection::processInitMsg()
  210. {
  211. vlog.debug("reading server initialisation");
  212. reader_->readServerInit();
  213. }
  214. void CConnection::throwAuthFailureException()
  215. {
  216. CharArray reason;
  217. vlog.debug("state=%d, ver=%d.%d", state(), cp.majorVersion, cp.minorVersion);
  218. if (state()==RFBSTATE_SECURITY_RESULT && !cp.beforeVersion(3,8)) {
  219. reason.buf = is->readString();
  220. } else {
  221. reason.buf = strDup("Authentication failure");
  222. }
  223. state_ = RFBSTATE_INVALID;
  224. vlog.error(reason.buf);
  225. throw AuthFailureException(reason.buf);
  226. }
  227. void CConnection::throwConnFailedException()
  228. {
  229. state_ = RFBSTATE_INVALID;
  230. CharArray reason;
  231. reason.buf = is->readString();
  232. throw ConnFailedException(reason.buf);
  233. }
  234. void CConnection::securityCompleted()
  235. {
  236. state_ = RFBSTATE_INITIALISATION;
  237. reader_ = new CMsgReaderV3(this, is);
  238. writer_ = new CMsgWriterV3(&cp, os);
  239. vlog.debug("Authentication success!");
  240. authSuccess();
  241. writer_->writeClientInit(shared);
  242. }
  243. void CConnection::authSuccess()
  244. {
  245. }
  246. void CConnection::serverInit()
  247. {
  248. state_ = RFBSTATE_NORMAL;
  249. vlog.debug("initialisation done");
  250. }