telnet_server.c 20 KB

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