telnet_server.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * telnet_server.c
  3. *
  4. * Created on: 24.11.2017
  5. * Author: balbekova
  6. */
  7. /* Standard includes. */
  8. #include <string.h>
  9. /* lwIP core includes */
  10. #include "lwip/opt.h"
  11. #include "lwip/sockets.h"
  12. /* FreeRTOS includes. */
  13. #include "FreeRTOS.h"
  14. #include "task.h"
  15. #include "fr_timers.h"
  16. /* Utils includes. */
  17. #include "FreeRTOS_CLI.h"
  18. #include "CLI_Commands.h"
  19. #include "telnet_server.h"
  20. #include "settings_api.h"
  21. #include "parameters.h"
  22. #include "control_symbol.h"
  23. #include "log.h"
  24. #include "web_params_api.h"
  25. /* Dimensions the buffer into which input characters are placed. */
  26. #define cmdMAX_INPUT_SIZE 144
  27. /* Repeat Login timeout, 1 seconds */
  28. #define REPEAT_SENSOR_INFO_TIME configTICK_RATE_HZ*1*1
  29. /* Set option to drop old connection if the new one is accepted */
  30. #define TCP_DROP_PREV_CONNECTION 0
  31. static portBASE_TYPE FreeRTOS_CLIAuthProcess( const int8_t * const pcCommandInput, int8_t * pcWriteBuffer );
  32. static portBASE_TYPE FreeRTOS_ChangePWDProcess( const int8_t * const pcCommandInput, int8_t * pcWriteBuffer );
  33. void SensorInfoTimerCallback(TimerHandle_t pxTimer);
  34. TimerHandle_t RepeatSensorInfoTimer;
  35. state_telnet_server_t telnetState = TELNET_AUTH;
  36. uint8_t id_change_pwd = 0;
  37. user_level_t telnet_code_auth = USER;
  38. static int32_t lClientFd;
  39. static int32_t lSocket;
  40. static int8_t *pcOutputString;
  41. static bool flagTelnetChange = false;
  42. static struct fd_set master_set, read_set, write_set;
  43. static int max_sd;
  44. static struct sockaddr_in sa;
  45. #ifdef HARDWARE_BT6708
  46. extern bool white_list_check(uint32_t check_remote_addr);
  47. static bool flagWhiteListTelnet = false;
  48. #endif
  49. /**
  50. * @brief Общая структура настроек
  51. */
  52. extern SETTINGS_t sSettings;
  53. /*-----------------------------------------------------------*/
  54. /* Stop server */
  55. static void stop_server(void) {
  56. /* Clean up all of the sockets that are open */
  57. for (int i = 0; i <= max_sd; ++i)
  58. {
  59. if (FD_ISSET(i, &master_set)) {
  60. DBG printf("Close sock %d\n", i);
  61. closesocket(i);
  62. FD_CLR(i, &master_set);
  63. }
  64. }
  65. DBG printf("Portgw stopped\n");
  66. }
  67. /* Start server */
  68. static bool start_server(uint16_t port)
  69. {
  70. int res;
  71. lSocket = socket(PF_INET, SOCK_STREAM, 0);
  72. if (lSocket < 0) {
  73. DBG printf("Socket create failed\r\n");
  74. return false;
  75. }
  76. res = fcntl(lSocket, F_SETFL, O_NONBLOCK);
  77. if (res < 0) {
  78. DBG printf("fcntl() failed");
  79. closesocket(lSocket);
  80. return false;
  81. }
  82. memset(&sa, 0, sizeof(struct sockaddr_in));
  83. sa.sin_family = AF_INET;
  84. sa.sin_addr.s_addr = IPADDR_ANY;
  85. sa.sin_port = htons(port);
  86. if (bind(lSocket, (struct sockaddr *)&sa, sizeof(sa)) == -1)
  87. {
  88. DBG printf("Bind to port %d failed\n", port);
  89. closesocket(lSocket);
  90. return false;
  91. }
  92. res = listen(lSocket, 20);
  93. if (res < 0) {
  94. DBG printf("Listen failed failed\r\n");
  95. closesocket(lSocket);
  96. return false;
  97. }
  98. FD_ZERO(&master_set);
  99. max_sd = lSocket;
  100. FD_SET(lSocket, &master_set);
  101. DBG printf("Port %d opened\n", port);
  102. return true;
  103. }
  104. void vBasicSocketsCommandInterpreterTask( void *pvParameters )
  105. {
  106. int32_t lBytes, lAddrLen = sizeof( struct sockaddr_in );
  107. struct sockaddr_in sLocalAddr;
  108. struct sockaddr_in client_addr;
  109. #ifdef HARDWARE_BT6708
  110. const int8_t * const pcWelcomeMessage = ( const int8_t * ) "BT6708 command server - connection accepted.\r\nlogin:";
  111. #else
  112. const int8_t * const pcWelcomeMessage = ( const int8_t * ) "BT6706 command server - connection accepted.\r\nlogin:";
  113. #endif
  114. static const int8_t * const pcEndOfCommandOutputString = ( int8_t * ) "\r\n[Нажмите клавишу ENTER для повторного выполнения предыдущей команды]\r\n>";
  115. int8_t cInChar, cInputIndex;
  116. static int8_t cInputString[ cmdMAX_INPUT_SIZE ] = { 0 }, cLastInputString[ cmdMAX_INPUT_SIZE ] = { 0 };
  117. portBASE_TYPE xReturned;
  118. int new_sd;
  119. int desc_ready, rc;
  120. struct timeval timeout;
  121. bool close_conn;
  122. uint16_t port;
  123. bool enabled;
  124. bool firstrun = true;
  125. #ifdef HARDWARE_BT6708
  126. struct sockaddr_in sa_temp;
  127. socklen_t len;
  128. #endif
  129. static int active_sd = -1;
  130. FD_ZERO(&master_set);
  131. timeout.tv_sec = 5;
  132. timeout.tv_usec = 0;
  133. ( void ) pvParameters;
  134. pcOutputString = FreeRTOS_CLIGetOutputBuffer();
  135. enabled = sSettings.sTelnet.TelnetEnable;
  136. port = ( uint16_t ) sSettings.sTelnet.port;
  137. while (1) {
  138. /* Check if network settings was changed */
  139. if ((sSettings.sTelnet.port != port) ||
  140. (sSettings.sTelnet.TelnetEnable != enabled) ||
  141. (firstrun))
  142. {
  143. if (!firstrun || sSettings.sTelnet.port != port) {
  144. /* Stop server */
  145. stop_server();
  146. }
  147. if (sSettings.sTelnet.TelnetEnable) {
  148. /* (Re)start server */
  149. if (!start_server(sSettings.sTelnet.port)) {
  150. DBG printf("Server start error\n");
  151. firstrun = true;
  152. vTaskDelay(5000);
  153. continue;
  154. }
  155. }
  156. else {
  157. /* Obtain the address of the output buffer. Note there is no mutual
  158. exclusion on this buffer as it is assumed only one command console
  159. interface will be used at any one time. */
  160. firstrun = true;
  161. vTaskDelay(5000);
  162. continue;
  163. }
  164. firstrun = false;
  165. port = sSettings.sTelnet.port;
  166. enabled = sSettings.sTelnet.TelnetEnable;
  167. }
  168. if (!enabled) {
  169. firstrun = true;
  170. vTaskDelay(5000);
  171. continue;
  172. }
  173. memcpy(&read_set, &master_set, sizeof(master_set));
  174. DBG printf("Waiting on select()...\n");
  175. rc = select(max_sd + 1, &read_set, NULL, NULL, &timeout);
  176. DBG printf(" select() returned %d\n", rc);
  177. if (rc < 0) {
  178. DBG printf(" select() failed\n");
  179. }
  180. if (rc == 0) {
  181. DBG printf(" select() timed out.\n");
  182. }
  183. /* One or more descriptors are readable. Need to \
  184. * determine which ones they are. */
  185. desc_ready = rc;
  186. for (int i=0; i <= max_sd && desc_ready > 0; ++i) {
  187. /*******************************************************/
  188. /* Check to see if this descriptor is ready */
  189. /*******************************************************/
  190. if (FD_ISSET(i, &read_set)) {
  191. /* A descriptor was found that was readable - one \
  192. * less has to be looked for. This is being done \
  193. * so that we can stop looking at the working set \
  194. * once we have found all of the descriptors that \
  195. * were ready. */
  196. desc_ready -= 1;
  197. /* Check to see if this is the listening socket */
  198. if (i == lSocket) {
  199. DBG printf(" Listening socket is readable\n");
  200. /* Accept all incoming connections that are */
  201. /* queued up on the listening socket before we */
  202. /* loop back and call select again. */
  203. do {
  204. /* Accept each incoming connection. If */
  205. /* accept fails with EWOULDBLOCK, then we */
  206. /* have accepted all of them. Any other */
  207. /* failure on accept will cause us to end the */
  208. /* server. */
  209. new_sd = accept(lSocket, NULL, NULL);
  210. if (new_sd < 0) {
  211. if (errno != EWOULDBLOCK) {
  212. DBG printf(" accept() failed\n");
  213. }
  214. break;
  215. }
  216. /* Add the new incoming connection to the */
  217. /* master read set */
  218. printf(" New incoming connection - %d\n", new_sd);
  219. FD_SET(new_sd, &master_set);
  220. if (new_sd > max_sd) {
  221. max_sd = new_sd;
  222. }
  223. #ifdef HARDWARE_BT6708
  224. lwip_getpeername(new_sd, &sa_temp, &len);
  225. flagWhiteListTelnet = white_list_check(sa_temp.sin_addr.s_addr);
  226. #endif
  227. recv( new_sd, cInputString, 27, 0 );
  228. telnetState = TELNET_AUTH;
  229. send( new_sd, pcWelcomeMessage, strlen( ( const char * ) pcWelcomeMessage ), 0 );
  230. cInputIndex = 0;
  231. memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
  232. #if TCP_DROP_PREV_CONNECTION
  233. /* Close previous active connection */
  234. if (active_sd != -1 && active_sd != new_sd) {
  235. DBG printf(" Close prev active connection %d\n", active_sd);
  236. close(active_sd);
  237. FD_CLR(active_sd, &master_set);
  238. if (active_sd == max_sd) {
  239. while (FD_ISSET(max_sd, &master_set) == false) {
  240. max_sd -= 1;
  241. }
  242. }
  243. }
  244. /* Mark new connection as active */
  245. active_sd = new_sd;
  246. DBG printf(" New active connection %d\n", active_sd);
  247. #endif
  248. /* Loop back up and accept another incoming */
  249. /* connection */
  250. } while (new_sd != -1);
  251. }
  252. /* This is not the listening socket, therefore an */
  253. /* existing connection must be readable */
  254. else {
  255. DBG printf(" Descriptor %d is readable\n", i);
  256. close_conn = false;
  257. /* Receive data on this connection until the */
  258. /* recv fails with EWOULDBLOCK. If any other */
  259. /* failure occurs, we will close the */
  260. /* connection. */
  261. if ((lBytes = recv(i, &cInChar, sizeof( cInChar ), 0 )) > 0) {
  262. if( lBytes > 0L )
  263. {
  264. if( cInChar == '\n' )
  265. {
  266. switch(telnetState){
  267. case TELNET_AUTH:
  268. if(FreeRTOS_CLIAuthProcess(cInputString, pcOutputString)){
  269. send( i, pcOutputString, strlen( ( const char * ) pcOutputString ), 0 );
  270. cInputIndex = 0;
  271. memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
  272. }
  273. else{
  274. /* Set lBytes to 0 to close the connection. */
  275. lBytes = 0L;
  276. }
  277. break;
  278. case TELNET_CMD:
  279. /* The input string has been terminated. Was the
  280. input a quit command? */
  281. if( strcmp( "quit", ( const char * ) cInputString ) == 0 )
  282. {
  283. /* Set lBytes to 0 to close the connection. */
  284. lBytes = 0L;
  285. }
  286. else
  287. {
  288. /* The input string was not a quit command.
  289. Pass the string to the command interpreter. */
  290. /* See if the command is empty, indicating that the last command is
  291. to be executed again. */
  292. if( cInputIndex == 0 )
  293. {
  294. strcpy( ( char * ) cInputString, ( char * ) cLastInputString );
  295. }
  296. /* Transmit a line separator, just to make the
  297. output easier to read. */
  298. lwip_send( lClientFd, "\r\n", strlen( "\r\n" ), 0 );
  299. do
  300. {
  301. /* Ensure there is not a string lingering in
  302. the output buffer. */
  303. pcOutputString[ 0 ] = 0x00;
  304. xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
  305. send( i, pcOutputString, strlen( ( const char * ) pcOutputString ), 0 );
  306. } while( xReturned != pdFALSE );
  307. if( strcmp( "sensor info", ( const char * ) cInputString ) == 0 ){
  308. strcpy( ( char * ) cLastInputString, ( char * ) cInputString );
  309. cInputIndex = 0;
  310. memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
  311. lClientFd = i;
  312. xTimerStart(RepeatSensorInfoTimer, 0);
  313. }
  314. else{
  315. /* All the strings generated by the input
  316. command have been sent. Clear the input
  317. string ready to receive the next command.
  318. Remember the command that was just processed
  319. first in case it is to be processed again. */
  320. strcpy( ( char * ) cLastInputString, ( char * ) cInputString );
  321. cInputIndex = 0;
  322. memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
  323. if(telnetState != TELNET_CHANGE_PWD)
  324. send( i, pcEndOfCommandOutputString, strlen( ( const char * ) pcEndOfCommandOutputString ), 0 );
  325. }
  326. }
  327. break;
  328. case TELNET_CHANGE_PWD:
  329. FreeRTOS_ChangePWDProcess(cInputString, pcOutputString);
  330. send( i, pcOutputString, strlen( ( const char * ) pcOutputString ), 0 );
  331. cInputIndex = 0;
  332. memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
  333. break;
  334. }
  335. }
  336. else
  337. {
  338. if( cInChar == '\r' )
  339. {
  340. /* Ignore the character. */
  341. }
  342. else if( cInChar == '\b' )
  343. {
  344. /* Backspace was pressed. Erase the last
  345. character in the string - if any. */
  346. if( cInputIndex > 0 )
  347. {
  348. cInputIndex--;
  349. cInputString[ cInputIndex ] = '\0';
  350. }
  351. }
  352. else if( cInChar == '\t' ){
  353. /*FreeRTOS_CLICompleteCMDCommand(cInputString, pcOutputString);
  354. strcpy((char *)cInputString, (char *)pcOutputString);
  355. send( i, pcOutputString, strlen( ( const char * ) pcOutputString ), 0 );*/
  356. }
  357. else
  358. {
  359. /* A character was entered. Add it to the string
  360. entered so far. When a \n is entered the complete
  361. string will be passed to the command interpreter. */
  362. if( cInputIndex < cmdMAX_INPUT_SIZE )
  363. {
  364. cInputString[ cInputIndex ] = cInChar;
  365. cInputIndex++;
  366. }
  367. if( strcmp( "\377\364\377\375\006", ( const char * ) cInputString ) == 0 ){
  368. xTimerStop(RepeatSensorInfoTimer, 0);
  369. cInputIndex = 0;
  370. memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
  371. cInputString[0] = 255;
  372. cInputString[1] = 251;
  373. cInputString[2] = 6;
  374. cInputString[3] = 255;
  375. cInputString[4] = 242;
  376. //if(telnetState != TELNET_CHANGE_PWD)
  377. //lwip_send( lClientFd, pcEndOfCommandOutputString, strlen( ( const char * ) pcEndOfCommandOutputString ), 0 );
  378. send( i, cInputString, strlen( ( const char * ) cInputString ), 0 );
  379. memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
  380. if(telnetState != TELNET_CHANGE_PWD)
  381. send( i, pcEndOfCommandOutputString, strlen( ( const char * ) pcEndOfCommandOutputString ), 0 );
  382. }
  383. }
  384. }
  385. }
  386. }
  387. if (lBytes < 0) {
  388. if (errno != EWOULDBLOCK){
  389. DBG printf(" recv() failed\n");
  390. close_conn = true;
  391. }
  392. }
  393. /* Check to see if the connection has been */
  394. /* closed by the client */
  395. if (lBytes == 0) {
  396. DBG printf(" Connection closed\n");
  397. close_conn = true;
  398. }
  399. /* If the close_conn flag was turned on, we need */
  400. /* to clean up this active connection. This */
  401. /* clean up process includes removing the */
  402. /* descriptor from the master set and */
  403. /* determining the new maximum descriptor value */
  404. /* based on the bits that are still turned on in */
  405. /* the master set. */
  406. if (close_conn) {
  407. closesocket(i);
  408. FD_CLR(i, &master_set);
  409. if (i == max_sd) {
  410. while (FD_ISSET(max_sd, &master_set) == false) {
  411. max_sd -= 1;
  412. }
  413. }
  414. }
  415. }
  416. }
  417. }
  418. }
  419. }
  420. void telnet_server_init(void) {
  421. vRegisterCLICommands();
  422. xTaskCreate(vBasicSocketsCommandInterpreterTask, ( char * ) "vBasicSocketsCommandInterpreterTask", 8*configMINIMAL_STACK_SIZE , NULL, tskIDLE_PRIORITY + 1, NULL);
  423. RepeatSensorInfoTimer = xTimerCreate("SensorInfoTmr", REPEAT_SENSOR_INFO_TIME, pdFALSE, ( void * ) 0, SensorInfoTimerCallback);
  424. }
  425. void SensorInfoTimerCallback(TimerHandle_t pxTimer) {
  426. portBASE_TYPE xReturned = pdTRUE;
  427. do
  428. {
  429. /* Ensure there is not a string lingering in
  430. the output buffer. */
  431. pcOutputString[ 0 ] = 0x00;
  432. xReturned = FreeRTOS_CLIProcessCommand( "sensor info", pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );
  433. lwip_send( lClientFd, pcOutputString, strlen( ( const char * ) pcOutputString ), 0 );
  434. } while( xReturned != pdFALSE );
  435. xTimerStart(RepeatSensorInfoTimer, 0);
  436. }
  437. static portBASE_TYPE FreeRTOS_CLIAuthProcess( const int8_t * const pcCommandInput, int8_t * pcWriteBuffer )
  438. {
  439. portBASE_TYPE xReturn = pdTRUE;
  440. uint32_t len;
  441. uint8_t valueLen, user_id;
  442. char WebPassword[MAX_WEB_PASSWD_LEN];
  443. char WebLogin[MAX_WEB_LOGIN_LEN];
  444. static uint8_t login_err = 0;
  445. static uint8_t telnet_state_auth = 0;
  446. static char login[ cmdMAX_INPUT_SIZE ] = { 0 };
  447. char password[ cmdMAX_INPUT_SIZE ] = { 0 };
  448. const int8_t * const pcPSWHeader = ( int8_t * ) "\r\npassword:";
  449. const int8_t * const pcLoginHeader = ( int8_t * ) "\r\nlogin:";
  450. memset(pcWriteBuffer, 0, configCOMMAND_INT_MAX_OUTPUT_SIZE);
  451. if(telnet_state_auth == 0){
  452. telnet_state_auth = 1;
  453. memset(login, 0, cmdMAX_INPUT_SIZE);
  454. len = strlen((char *)pcCommandInput);
  455. strncpy(login, (char *)pcCommandInput, len);
  456. strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcPSWHeader, strlen( ( char * ) pcPSWHeader ) );
  457. xReturn = pdTRUE;
  458. }
  459. else{
  460. memset(name_login_telnet, 0, 50);
  461. telnet_state_auth = 0;
  462. memset(password, 0, cmdMAX_INPUT_SIZE);
  463. len = strlen((char *)pcCommandInput);
  464. strncpy(password, (char *)pcCommandInput, len);
  465. #ifdef HARDWARE_BT6708
  466. if(!flagWhiteListTelnet){
  467. xReturn = pdFALSE;
  468. }
  469. else
  470. #endif
  471. {
  472. for (user_id = 0; user_id < MAX_WEB_USERS; user_id++) {
  473. GetUserLogin(user_id, WebLogin, &valueLen);
  474. GetUserPassword(user_id, WebPassword, &valueLen);
  475. /* Check login and password */
  476. if ((strncmp(WebLogin, login, MAX_WEB_LOGIN_LEN) == 0) &&
  477. (strncmp(WebPassword, password, MAX_WEB_PASSWD_LEN) == 0)) {
  478. /* Login and pass are valid */
  479. telnet_code_auth = user_id;
  480. login_err = 0;
  481. strcpy( ( char * ) pcWriteBuffer, "\r\nАвторизация успешно пройдена\r\n>" );
  482. telnetState = TELNET_CMD;
  483. xReturn = pdTRUE;
  484. break;
  485. }
  486. else{
  487. xReturn = pdFALSE;
  488. }
  489. }
  490. }
  491. if(xReturn == pdFALSE){
  492. if(login_err < 4){
  493. login_err ++;
  494. #ifdef HARDWARE_BT6708
  495. if(!flagWhiteListTelnet)
  496. strcpy( ( char * ) pcWriteBuffer, "\r\nДоступ запрешен! Ваш IP-адрес находится вне диапазона доверительных хостов\r\n" );
  497. else
  498. #endif
  499. strcpy( ( char * ) pcWriteBuffer, "\r\nОшибка авторизации\r\n" );
  500. strncat( ( char * ) pcWriteBuffer, ( const char * ) pcLoginHeader, strlen( ( char * ) pcLoginHeader ) );
  501. xReturn = pdTRUE;
  502. }
  503. else{
  504. login_err = 0;
  505. xReturn = pdFALSE;
  506. }
  507. }
  508. switch (user_id) {
  509. case 0:
  510. snprintf(name_login_telnet, sizeof(name_login_telnet), "Администратор");
  511. break;
  512. case 1:
  513. snprintf(name_login_telnet, sizeof(name_login_telnet), "Пользователь");
  514. break;
  515. default:
  516. break;
  517. }
  518. log_event_data(LOG_LOGIN_TELNET, name_login_telnet);
  519. }
  520. return xReturn;
  521. }
  522. static portBASE_TYPE FreeRTOS_ChangePWDProcess( const int8_t * const pcCommandInput, int8_t * pcWriteBuffer )
  523. {
  524. portBASE_TYPE xReturn = pdTRUE;
  525. uint32_t len;
  526. static uint8_t telnet_state_change_pwd = 0;
  527. static char password[ MAX_WEB_LOGIN_LEN ] = { 0 };
  528. char password2[ MAX_WEB_LOGIN_LEN ] = { 0 };
  529. const int8_t * const pcNewPSWHeader = ( int8_t * ) "\r\nВведите повторно новый пароль:";
  530. memset(pcWriteBuffer, 0, configCOMMAND_INT_MAX_OUTPUT_SIZE);
  531. if(telnet_state_change_pwd == 0){
  532. telnet_state_change_pwd = 1;
  533. memset(password, 0, MAX_WEB_LOGIN_LEN);
  534. len = strlen((char *)pcCommandInput);
  535. if(len >= MAX_WEB_LOGIN_LEN){
  536. strcpy( ( char * ) pcWriteBuffer, "\r\nОшибка при вводе нового пароля\r\n>" );
  537. telnetState = TELNET_CMD;
  538. }
  539. else{
  540. if(!control_string_en_digit((char *)pcCommandInput, len)){
  541. strcpy( ( char * ) pcWriteBuffer, "\r\nОшибка при вводе нового пароля\r\n>" );
  542. telnetState = TELNET_CMD;
  543. }
  544. else{
  545. strncpy(password, (char *)pcCommandInput, len);
  546. strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcNewPSWHeader, strlen( ( char * ) pcNewPSWHeader ) );
  547. }
  548. }
  549. }
  550. else{
  551. telnet_state_change_pwd = 0;
  552. memset(password2, 0, cmdMAX_INPUT_SIZE);
  553. len = strlen((char *)pcCommandInput);
  554. if(len >= MAX_WEB_LOGIN_LEN){
  555. strcpy( ( char * ) pcWriteBuffer, "\r\nОшибка при вводе нового пароля\r\n>" );
  556. telnetState = TELNET_CMD;
  557. }
  558. else{
  559. strncpy(password2, (char *)pcCommandInput, len);
  560. if (strncmp(password, password2, MAX_WEB_PASSWD_LEN) == 0) {
  561. memcpy(sSettings.sAuth[id_change_pwd].password, password, 11);
  562. telnet_act = true;
  563. HTTP_SaveSettings();
  564. log_event_data(LOG_PSW_CHANGE, name_login_telnet);
  565. strcpy( ( char * ) pcWriteBuffer, "\r\nПароль успешно изменен\r\n>" );
  566. telnetState = TELNET_CMD;
  567. xReturn = pdTRUE;
  568. }
  569. else{
  570. strcpy( ( char * ) pcWriteBuffer, "\r\nОшибка при вводе нового пароля\r\n>" );
  571. telnetState = TELNET_CMD;
  572. }
  573. }
  574. }
  575. return xReturn;
  576. }