telnet_server.c 23 KB

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