telnetConsole.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platform/event.h"
  24. #include "network/telnetConsole.h"
  25. #include "game/gameInterface.h"
  26. #include "telnetConsole_ScriptBinding.h"
  27. TelnetConsole *TelConsole = NULL;
  28. void TelnetConsole::create()
  29. {
  30. TelConsole = new TelnetConsole;
  31. }
  32. void TelnetConsole::destroy()
  33. {
  34. delete TelConsole;
  35. TelConsole = NULL;
  36. }
  37. static void telnetCallback(ConsoleLogEntry::Level level, const char *consoleLine)
  38. {
  39. if (TelConsole)
  40. TelConsole->processConsoleLine(consoleLine);
  41. }
  42. TelnetConsole::TelnetConsole()
  43. {
  44. Con::addConsumer(telnetCallback);
  45. mAcceptSocket = NetSocket::INVALID;
  46. mAcceptPort = -1;
  47. mClientList = NULL;
  48. mRemoteEchoEnabled = false;
  49. }
  50. TelnetConsole::~TelnetConsole()
  51. {
  52. Con::removeConsumer(telnetCallback);
  53. if(mAcceptSocket != NetSocket::INVALID)
  54. Net::closeSocket(mAcceptSocket);
  55. TelnetClient *walk = mClientList, *temp;
  56. while(walk)
  57. {
  58. temp = walk->nextClient;
  59. if(walk->socket != NetSocket::INVALID)
  60. Net::closeSocket(walk->socket);
  61. delete walk;
  62. walk = temp;
  63. }
  64. }
  65. void TelnetConsole::setTelnetParameters(S32 port, const char *telnetPassword, const char *listenPassword, bool remoteEcho)
  66. {
  67. if(port == mAcceptPort)
  68. return;
  69. mRemoteEchoEnabled = remoteEcho;
  70. if(mAcceptSocket != NetSocket::INVALID)
  71. {
  72. Net::closeSocket(mAcceptSocket);
  73. mAcceptSocket = NetSocket::INVALID;
  74. }
  75. mAcceptPort = port;
  76. if(mAcceptPort != -1 && mAcceptPort != 0)
  77. {
  78. NetAddress address;
  79. Net::getIdealListenAddress(&address);
  80. address.port = mAcceptPort;
  81. mAcceptSocket = Net::openSocket();
  82. Net::bindAddress(address, mAcceptSocket);
  83. Net::listen(mAcceptSocket, 4);
  84. Net::setBlocking(mAcceptSocket, false);
  85. }
  86. dStrncpy(mTelnetPassword, telnetPassword, PasswordMaxLength);
  87. dStrncpy(mListenPassword, listenPassword, PasswordMaxLength);
  88. }
  89. void TelnetConsole::processConsoleLine(const char *consoleLine)
  90. {
  91. if (mClientList==NULL) return; // just escape early. don't even do another step...
  92. // ok, spew this line out to all our subscribers...
  93. S32 len = dStrlen(consoleLine)+1;
  94. for(TelnetClient *walk = mClientList; walk; walk = walk->nextClient)
  95. {
  96. if(walk->state == FullAccessConnected || walk->state == ReadOnlyConnected)
  97. {
  98. Net::send(walk->socket, (const unsigned char*)consoleLine, len);
  99. Net::send(walk->socket, (const unsigned char*)"\r\n", 2);
  100. }
  101. }
  102. }
  103. void TelnetConsole::process()
  104. {
  105. NetAddress address;
  106. if(mAcceptSocket != NetSocket::INVALID)
  107. {
  108. // ok, see if we have any new connections:
  109. NetSocket newConnection;
  110. newConnection = Net::accept(mAcceptSocket, &address);
  111. if(newConnection != NetSocket::INVALID)
  112. {
  113. char buffer[256];
  114. Net::addressToString(&address, buffer);
  115. Con::printf("Telnet connection from %s", buffer);
  116. TelnetClient *cl = new TelnetClient;
  117. cl->socket = newConnection;
  118. cl->curPos = 0;
  119. #if defined(TORQUE_SHIPPING) && defined(TORQUE_DISABLE_TELNET_CONSOLE_PASSWORD)
  120. // disable the password in a ship build? WTF. lets make an error:
  121. PleaseMakeSureYouKnowWhatYouAreDoingAndCommentOutThisLineIfSo.
  122. #elif !defined(TORQUE_SHIPPING) && defined(TORQUE_DISABLE_TELNET_CONSOLE_PASSWORD)
  123. cl->state = FullAccessConnected;
  124. #else
  125. cl->state = PasswordTryOne;
  126. #endif
  127. Net::setBlocking(newConnection, false);
  128. const char *prompt = Con::getVariable("Con::Prompt");
  129. char connectMessage[1024];
  130. dSprintf(connectMessage, sizeof(connectMessage),
  131. "Torque Telnet Remote Console\r\n\r\n%s",
  132. cl->state == FullAccessConnected ? prompt : "Enter Password:");
  133. Net::send(cl->socket, (const unsigned char*)connectMessage, dStrlen(connectMessage)+1);
  134. cl->nextClient = mClientList;
  135. mClientList = cl;
  136. }
  137. }
  138. char recvBuf[256];
  139. char reply[1024];
  140. // see if we have any input to process...
  141. for(TelnetClient *client = mClientList; client; client = client->nextClient)
  142. {
  143. S32 numBytes;
  144. Net::Error err = Net::recv(client->socket, (unsigned char*)recvBuf, sizeof(recvBuf), &numBytes);
  145. if((err != Net::NoError && err != Net::WouldBlock) || numBytes == 0)
  146. {
  147. Net::closeSocket(client->socket);
  148. client->socket = NetSocket::INVALID;
  149. continue;
  150. }
  151. S32 replyPos = 0;
  152. for(S32 i = 0; i < numBytes;i++)
  153. {
  154. if(recvBuf[i] == '\r')
  155. continue;
  156. // execute the current command
  157. if(recvBuf[i] == '\n')
  158. {
  159. reply[replyPos++] = '\r';
  160. reply[replyPos++] = '\n';
  161. client->curLine[client->curPos] = 0;
  162. client->curPos = 0;
  163. if(client->state == FullAccessConnected)
  164. {
  165. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  166. replyPos = 0;
  167. dStrcpy(mPostEvent.data, client->curLine);
  168. mPostEvent.size = ConsoleEventHeaderSize + dStrlen(client->curLine) + 1;
  169. Game->postEvent(mPostEvent);
  170. // note - send prompt next
  171. const char *prompt = Con::getVariable("Con::Prompt");
  172. Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt));
  173. }
  174. else if(client->state == ReadOnlyConnected)
  175. {
  176. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  177. replyPos = 0;
  178. }
  179. else
  180. {
  181. client->state++;
  182. if(!dStrncmp(client->curLine, mTelnetPassword, PasswordMaxLength))
  183. {
  184. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  185. replyPos = 0;
  186. // send prompt
  187. const char *prompt = Con::getVariable("Con::Prompt");
  188. Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt));
  189. client->state = FullAccessConnected;
  190. }
  191. else if(!dStrncmp(client->curLine, mListenPassword, PasswordMaxLength))
  192. {
  193. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  194. replyPos = 0;
  195. // send prompt
  196. const char *listenConnected = "Connected.\r\n";
  197. Net::send(client->socket, (const unsigned char*)listenConnected, dStrlen(listenConnected));
  198. client->state = ReadOnlyConnected;
  199. }
  200. else
  201. {
  202. const char *sendStr;
  203. if(client->state == DisconnectThisDude)
  204. sendStr = "Too many tries... cya.";
  205. else
  206. sendStr = "Nope... try agian.\r\nEnter Password:";
  207. Net::send(client->socket, (const unsigned char*)sendStr, dStrlen(sendStr));
  208. if(client->state == DisconnectThisDude)
  209. {
  210. Net::closeSocket(client->socket);
  211. client->socket = NetSocket::INVALID;
  212. }
  213. }
  214. }
  215. }
  216. else if(recvBuf[i] == '\b')
  217. {
  218. // pull the old backspace manuever...
  219. if(client->curPos > 0)
  220. {
  221. client->curPos--;
  222. if(client->state == FullAccessConnected)
  223. {
  224. reply[replyPos++] = '\b';
  225. reply[replyPos++] = ' ';
  226. reply[replyPos++] = '\b';
  227. }
  228. }
  229. }
  230. else if(client->curPos < Con::MaxLineLength-1)
  231. {
  232. client->curLine[client->curPos++] = recvBuf[i];
  233. // don't echo password chars...
  234. if(client->state == FullAccessConnected)
  235. reply[replyPos++] = recvBuf[i];
  236. }
  237. }
  238. // Echo the character back to the user, unless the remote echo
  239. // is disabled (by default)
  240. if(replyPos && mRemoteEchoEnabled)
  241. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  242. }
  243. TelnetClient ** walk = &mClientList;
  244. TelnetClient *cl;
  245. while((cl = *walk) != NULL)
  246. {
  247. if(cl->socket == NetSocket::INVALID)
  248. {
  249. *walk = cl->nextClient;
  250. delete cl;
  251. }
  252. else
  253. walk = &cl->nextClient;
  254. }
  255. }