telnetConsole.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. DefineConsoleFunction( 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. }
  76. TelnetConsole::~TelnetConsole()
  77. {
  78. Con::removeConsumer(telnetCallback);
  79. if(mAcceptSocket != NetSocket::INVALID)
  80. Net::closeSocket(mAcceptSocket);
  81. TelnetClient *walk = mClientList, *temp;
  82. while(walk)
  83. {
  84. temp = walk->nextClient;
  85. if(walk->socket != NetSocket::INVALID)
  86. Net::closeSocket(walk->socket);
  87. delete walk;
  88. walk = temp;
  89. }
  90. }
  91. void TelnetConsole::setTelnetParameters(S32 port, const char *telnetPassword, const char *listenPassword, bool remoteEcho)
  92. {
  93. if(port == mAcceptPort)
  94. return;
  95. mRemoteEchoEnabled = remoteEcho;
  96. if(mAcceptSocket != NetSocket::INVALID)
  97. {
  98. Net::closeSocket(mAcceptSocket);
  99. mAcceptSocket = NetSocket::INVALID;
  100. }
  101. mAcceptPort = port;
  102. if(mAcceptPort != -1 && mAcceptPort != 0)
  103. {
  104. NetAddress address;
  105. Net::getIdealListenAddress(&address);
  106. address.port = mAcceptPort;
  107. mAcceptSocket = Net::openSocket();
  108. Net::bindAddress(address, mAcceptSocket);
  109. Net::listen(mAcceptSocket, 4);
  110. Net::setBlocking(mAcceptSocket, false);
  111. }
  112. dStrncpy(mTelnetPassword, telnetPassword, PasswordMaxLength);
  113. dStrncpy(mListenPassword, listenPassword, PasswordMaxLength);
  114. }
  115. void TelnetConsole::processConsoleLine(const char *consoleLine)
  116. {
  117. if (mClientList==NULL) return; // just escape early. don't even do another step...
  118. // ok, spew this line out to all our subscribers...
  119. S32 len = dStrlen(consoleLine)+1;
  120. for(TelnetClient *walk = mClientList; walk; walk = walk->nextClient)
  121. {
  122. if(walk->state == FullAccessConnected || walk->state == ReadOnlyConnected)
  123. {
  124. Net::send(walk->socket, (const unsigned char*)consoleLine, len);
  125. Net::send(walk->socket, (const unsigned char*)"\r\n", 2);
  126. }
  127. }
  128. }
  129. void TelnetConsole::process()
  130. {
  131. NetAddress address;
  132. if(mAcceptSocket != NetSocket::INVALID)
  133. {
  134. // ok, see if we have any new connections:
  135. NetSocket newConnection;
  136. newConnection = Net::accept(mAcceptSocket, &address);
  137. if(newConnection != NetSocket::INVALID)
  138. {
  139. char buffer[256];
  140. Net::addressToString(&address, buffer);
  141. Con::printf("Telnet connection from %s", buffer);
  142. TelnetClient *cl = new TelnetClient;
  143. cl->socket = newConnection;
  144. cl->curPos = 0;
  145. #if defined(TORQUE_SHIPPING) && defined(TORQUE_DISABLE_TELNET_CONSOLE_PASSWORD)
  146. // disable the password in a ship build? WTF. lets make an error:
  147. PleaseMakeSureYouKnowWhatYouAreDoingAndCommentOutThisLineIfSo.
  148. #elif !defined(TORQUE_SHIPPING) && defined(TORQUE_DISABLE_TELNET_CONSOLE_PASSWORD)
  149. cl->state = FullAccessConnected;
  150. #else
  151. cl->state = PasswordTryOne;
  152. #endif
  153. Net::setBlocking(newConnection, false);
  154. const char *prompt = Con::getVariable("Con::Prompt");
  155. char connectMessage[1024];
  156. dSprintf(connectMessage, sizeof(connectMessage),
  157. "Torque Telnet Remote Console\r\n\r\n%s",
  158. cl->state == FullAccessConnected ? prompt : "Enter Password:");
  159. Net::send(cl->socket, (const unsigned char*)connectMessage, dStrlen(connectMessage)+1);
  160. cl->nextClient = mClientList;
  161. mClientList = cl;
  162. }
  163. }
  164. char recvBuf[256];
  165. char reply[1024];
  166. // see if we have any input to process...
  167. for(TelnetClient *client = mClientList; client; client = client->nextClient)
  168. {
  169. S32 numBytes;
  170. Net::Error err = Net::recv(client->socket, (unsigned char*)recvBuf, sizeof(recvBuf), &numBytes);
  171. if((err != Net::NoError && err != Net::WouldBlock) || numBytes == 0)
  172. {
  173. Net::closeSocket(client->socket);
  174. client->socket = NetSocket::INVALID;
  175. continue;
  176. }
  177. S32 replyPos = 0;
  178. for(S32 i = 0; i < numBytes;i++)
  179. {
  180. if(recvBuf[i] == '\r')
  181. continue;
  182. // execute the current command
  183. if(recvBuf[i] == '\n')
  184. {
  185. reply[replyPos++] = '\r';
  186. reply[replyPos++] = '\n';
  187. client->curLine[client->curPos] = 0;
  188. client->curPos = 0;
  189. if(client->state == FullAccessConnected)
  190. {
  191. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  192. replyPos = 0;
  193. // Notify console of line to execute.
  194. RawData rd;
  195. rd.size = dStrlen(client->curLine) + 1;
  196. rd.data = ( S8* ) client->curLine;
  197. Con::smConsoleInput.trigger(rd);
  198. // note - send prompt next
  199. const char *prompt = Con::getVariable("Con::Prompt");
  200. Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt));
  201. }
  202. else if(client->state == ReadOnlyConnected)
  203. {
  204. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  205. replyPos = 0;
  206. }
  207. else
  208. {
  209. client->state++;
  210. if(!dStrncmp(client->curLine, mTelnetPassword, PasswordMaxLength))
  211. {
  212. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  213. replyPos = 0;
  214. // send prompt
  215. const char *prompt = Con::getVariable("Con::Prompt");
  216. Net::send(client->socket, (const unsigned char*)prompt, dStrlen(prompt));
  217. client->state = FullAccessConnected;
  218. }
  219. else if(!dStrncmp(client->curLine, mListenPassword, PasswordMaxLength))
  220. {
  221. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  222. replyPos = 0;
  223. // send prompt
  224. const char *listenConnected = "Connected.\r\n";
  225. Net::send(client->socket, (const unsigned char*)listenConnected, dStrlen(listenConnected));
  226. client->state = ReadOnlyConnected;
  227. }
  228. else
  229. {
  230. const char *sendStr;
  231. if(client->state == DisconnectThisDude)
  232. sendStr = "Too many tries... cya.";
  233. else
  234. sendStr = "Nope... try agian.\r\nEnter Password:";
  235. Net::send(client->socket, (const unsigned char*)sendStr, dStrlen(sendStr));
  236. if(client->state == DisconnectThisDude)
  237. {
  238. Net::closeSocket(client->socket);
  239. client->socket = NetSocket::INVALID;
  240. }
  241. }
  242. }
  243. }
  244. else if(recvBuf[i] == '\b')
  245. {
  246. // pull the old backspace manuever...
  247. if(client->curPos > 0)
  248. {
  249. client->curPos--;
  250. if(client->state == FullAccessConnected)
  251. {
  252. reply[replyPos++] = '\b';
  253. reply[replyPos++] = ' ';
  254. reply[replyPos++] = '\b';
  255. }
  256. }
  257. }
  258. else if(client->curPos < Con::MaxLineLength-1)
  259. {
  260. client->curLine[client->curPos++] = recvBuf[i];
  261. // don't echo password chars...
  262. if(client->state == FullAccessConnected)
  263. reply[replyPos++] = recvBuf[i];
  264. }
  265. }
  266. // Echo the character back to the user, unless the remote echo
  267. // is disabled (by default)
  268. if(replyPos && mRemoteEchoEnabled)
  269. Net::send(client->socket, (const unsigned char*)reply, replyPos);
  270. }
  271. TelnetClient ** walk = &mClientList;
  272. TelnetClient *cl;
  273. while((cl = *walk) != NULL)
  274. {
  275. if(cl->socket == NetSocket::INVALID)
  276. {
  277. *walk = cl->nextClient;
  278. delete cl;
  279. }
  280. else
  281. walk = &cl->nextClient;
  282. }
  283. }