telnetConsole.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "console/simBase.h"
  24. #include "console/engineAPI.h"
  25. #include "console/telnetConsole.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include "core/util/journal/process.h"
  28. #include "core/module.h"
  29. MODULE_BEGIN( TelnetConsole )
  30. MODULE_INIT
  31. {
  32. TelnetConsole::create();
  33. }
  34. MODULE_SHUTDOWN
  35. {
  36. TelnetConsole::destroy();
  37. }
  38. MODULE_END;
  39. TelnetConsole *TelConsole = NULL;
  40. void TelnetConsole::create()
  41. {
  42. TelConsole = new TelnetConsole;
  43. Process::notify(TelConsole, &TelnetConsole::process, PROCESS_FIRST_ORDER);
  44. }
  45. void TelnetConsole::destroy()
  46. {
  47. Process::remove(TelConsole, &TelnetConsole::process);
  48. delete TelConsole;
  49. TelConsole = NULL;
  50. }
  51. DefineEngineFunction( telnetSetParameters, void, ( int port, const char* consolePass, const char* listenPass, bool remoteEcho ), ( false ),
  52. "@brief Initializes and open the telnet console.\n\n"
  53. "@param port Port to listen on for console connections (0 will shut down listening).\n"
  54. "@param consolePass Password for read/write access to console.\n"
  55. "@param listenPass Password for read access to console.\n"
  56. "@param remoteEcho [optional] Enable echoing back to the client, off by default.\n\n"
  57. "@ingroup Debugging")
  58. {
  59. if (TelConsole)
  60. TelConsole->setTelnetParameters(port, consolePass, listenPass, remoteEcho);
  61. }
  62. static void telnetCallback(U32 level, const char *consoleLine)
  63. {
  64. TORQUE_UNUSED(level);
  65. if (TelConsole)
  66. TelConsole->processConsoleLine(consoleLine);
  67. }
  68. TelnetConsole::TelnetConsole()
  69. {
  70. Con::addConsumer(telnetCallback);
  71. mAcceptSocket = NetSocket::INVALID;
  72. mAcceptPort = -1;
  73. mClientList = NULL;
  74. mRemoteEchoEnabled = false;
  75. dStrncpy(mTelnetPassword, "", PasswordMaxLength);
  76. dStrncpy(mListenPassword, "", PasswordMaxLength);
  77. }
  78. TelnetConsole::~TelnetConsole()
  79. {
  80. Con::removeConsumer(telnetCallback);
  81. if(mAcceptSocket != NetSocket::INVALID)
  82. Net::closeSocket(mAcceptSocket);
  83. TelnetClient *walk = mClientList, *temp;
  84. while(walk)
  85. {
  86. temp = walk->nextClient;
  87. if(walk->socket != NetSocket::INVALID)
  88. Net::closeSocket(walk->socket);
  89. delete walk;
  90. walk = temp;
  91. }
  92. }
  93. void TelnetConsole::setTelnetParameters(S32 port, const char *telnetPassword, const char *listenPassword, bool remoteEcho)
  94. {
  95. if(port == mAcceptPort)
  96. return;
  97. mRemoteEchoEnabled = remoteEcho;
  98. if(mAcceptSocket != NetSocket::INVALID)
  99. {
  100. Net::closeSocket(mAcceptSocket);
  101. mAcceptSocket = NetSocket::INVALID;
  102. }
  103. mAcceptPort = port;
  104. if(mAcceptPort != -1 && mAcceptPort != 0)
  105. {
  106. NetAddress address;
  107. Net::getIdealListenAddress(&address);
  108. address.port = mAcceptPort;
  109. mAcceptSocket = Net::openSocket();
  110. Net::bindAddress(address, mAcceptSocket);
  111. Net::listen(mAcceptSocket, 4);
  112. Net::setBlocking(mAcceptSocket, false);
  113. }
  114. dStrncpy(mTelnetPassword, telnetPassword, PasswordMaxLength);
  115. dStrncpy(mListenPassword, listenPassword, PasswordMaxLength);
  116. }
  117. void TelnetConsole::processConsoleLine(const char *consoleLine)
  118. {
  119. if (mClientList==NULL) return; // just escape early. don't even do another step...
  120. // ok, spew this line out to all our subscribers...
  121. S32 len = dStrlen(consoleLine)+1;
  122. for(TelnetClient *walk = mClientList; walk; walk = walk->nextClient)
  123. {
  124. if(walk->state == FullAccessConnected || walk->state == ReadOnlyConnected)
  125. {
  126. Net::send(walk->socket, (const unsigned char*)consoleLine, len);
  127. Net::send(walk->socket, (const unsigned char*)"\r\n", 2);
  128. }
  129. }
  130. }
  131. void TelnetConsole::process()
  132. {
  133. NetAddress address;
  134. if(mAcceptSocket != NetSocket::INVALID)
  135. {
  136. // ok, see if we have any new connections:
  137. NetSocket newConnection;
  138. newConnection = Net::accept(mAcceptSocket, &address);
  139. if(newConnection != NetSocket::INVALID)
  140. {
  141. char buffer[256];
  142. Net::addressToString(&address, buffer);
  143. Con::printf("Telnet connection from %s", buffer);
  144. TelnetClient *cl = new TelnetClient;
  145. cl->socket = newConnection;
  146. cl->curPos = 0;
  147. #if defined(TORQUE_SHIPPING) && defined(TORQUE_DISABLE_TELNET_CONSOLE_PASSWORD)
  148. // disable the password in a ship build? WTF. lets make an error:
  149. PleaseMakeSureYouKnowWhatYouAreDoingAndCommentOutThisLineIfSo.
  150. #elif !defined(TORQUE_SHIPPING) && defined(TORQUE_DISABLE_TELNET_CONSOLE_PASSWORD)
  151. cl->state = FullAccessConnected;
  152. #else
  153. cl->state = PasswordTryOne;
  154. #endif
  155. Net::setBlocking(newConnection, false);
  156. const char *prompt = Con::getVariable("Con::Prompt");
  157. char connectMessage[1024];
  158. dSprintf(connectMessage, sizeof(connectMessage),
  159. "Torque Telnet Remote Console\r\n\r\n%s",
  160. cl->state == FullAccessConnected ? prompt : "Enter Password:");
  161. Net::send(cl->socket, (const unsigned char*)connectMessage, dStrlen(connectMessage)+1);
  162. cl->nextClient = mClientList;
  163. mClientList = cl;
  164. }
  165. }
  166. char recvBuf[256];
  167. char reply[1024];
  168. // see if we have any input to process...
  169. for(TelnetClient *client = mClientList; client; client = client->nextClient)
  170. {
  171. S32 numBytes;
  172. Net::Error err = Net::recv(client->socket, (unsigned char*)recvBuf, sizeof(recvBuf), &numBytes);
  173. if((err != Net::NoError && err != Net::WouldBlock) || numBytes == 0)
  174. {
  175. Net::closeSocket(client->socket);
  176. client->socket = NetSocket::INVALID;
  177. continue;
  178. }
  179. S32 replyPos = 0;
  180. for(S32 i = 0; i < numBytes;i++)
  181. {
  182. if(recvBuf[i] == '\r')
  183. continue;
  184. // execute the current command
  185. if(recvBuf[i] == '\n')
  186. {
  187. reply[replyPos++] = '\r';
  188. reply[replyPos++] = '\n';
  189. client->curLine[client->curPos] = 0;
  190. client->curPos = 0;
  191. if(client->state == FullAccessConnected)
  192. {
  193. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  194. replyPos = 0;
  195. // Notify console of line to execute.
  196. RawData rd;
  197. rd.size = dStrlen(client->curLine) + 1;
  198. rd.data = ( S8* ) client->curLine;
  199. Con::smConsoleInput.trigger(rd);
  200. // note - send prompt next
  201. const char *prompt = Con::getVariable("Con::Prompt");
  202. Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt));
  203. }
  204. else if(client->state == ReadOnlyConnected)
  205. {
  206. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  207. replyPos = 0;
  208. }
  209. else
  210. {
  211. client->state++;
  212. if(!dStrncmp(client->curLine, mTelnetPassword, PasswordMaxLength))
  213. {
  214. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  215. replyPos = 0;
  216. // send prompt
  217. const char *prompt = Con::getVariable("Con::Prompt");
  218. Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt));
  219. client->state = FullAccessConnected;
  220. }
  221. else if(!dStrncmp(client->curLine, mListenPassword, PasswordMaxLength))
  222. {
  223. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  224. replyPos = 0;
  225. // send prompt
  226. const char *listenConnected = "Connected.\r\n";
  227. Net::send(client->socket, (const unsigned char*)listenConnected, dStrlen(listenConnected));
  228. client->state = ReadOnlyConnected;
  229. }
  230. else
  231. {
  232. const char *sendStr;
  233. if(client->state == DisconnectThisDude)
  234. sendStr = "Too many tries... cya.";
  235. else
  236. sendStr = "Nope... try agian.\r\nEnter Password:";
  237. Net::send(client->socket, (const unsigned char*)sendStr, dStrlen(sendStr));
  238. if(client->state == DisconnectThisDude)
  239. {
  240. Net::closeSocket(client->socket);
  241. client->socket = NetSocket::INVALID;
  242. }
  243. }
  244. }
  245. }
  246. else if(recvBuf[i] == '\b')
  247. {
  248. // pull the old backspace manuever...
  249. if(client->curPos > 0)
  250. {
  251. client->curPos--;
  252. if(client->state == FullAccessConnected)
  253. {
  254. reply[replyPos++] = '\b';
  255. reply[replyPos++] = ' ';
  256. reply[replyPos++] = '\b';
  257. }
  258. }
  259. }
  260. else if(client->curPos < Con::MaxLineLength-1)
  261. {
  262. client->curLine[client->curPos++] = recvBuf[i];
  263. // don't echo password chars...
  264. if(client->state == FullAccessConnected)
  265. reply[replyPos++] = recvBuf[i];
  266. }
  267. }
  268. // Echo the character back to the user, unless the remote echo
  269. // is disabled (by default)
  270. if(replyPos && mRemoteEchoEnabled)
  271. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  272. }
  273. TelnetClient ** walk = &mClientList;
  274. TelnetClient *cl;
  275. while((cl = *walk) != NULL)
  276. {
  277. if(cl->socket == NetSocket::INVALID)
  278. {
  279. *walk = cl->nextClient;
  280. delete cl;
  281. }
  282. else
  283. walk = &cl->nextClient;
  284. }
  285. }