telnetConsole.cpp 10 KB

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