telnetConsole.cpp 10 KB

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