Преглед изворни кода

telnet: sweep the states on server shutdown

to avoid passwordless login on the future connections
Sergey Alirzaev пре 5 година
родитељ
комит
e907f9d608
1 измењених фајлова са 31 додато и 22 уклоњено
  1. 31 22
      modules/Telnet_Server/telnet_server.c

+ 31 - 22
modules/Telnet_Server/telnet_server.c

@@ -194,6 +194,22 @@ static const int8_t * const pcEndOfCommandOutputString = ( int8_t * ) "\r\n[На
   */
 extern SETTINGS_t sSettings;
 
+static void deconfigure_telnet_state(telnetd_state_t *state)
+{
+	if (state->cli_state) {
+		free_state(state->cli_state);
+		state->cli_state = NULL;
+	}
+	memset(state->buf, 0, cmdMAX_INPUT_SIZE);
+	memset(state->prev_cmd, 0, cmdMAX_INPUT_SIZE);
+	memset(state->optdata, 0, cmdMAX_INPUT_SIZE);
+	state->optlen = 0;
+	state->state = TELNET_STATE_NORMAL;
+	state->num_connect = 0;
+	state->active_conn = false;
+	state->telnet_code_auth = USER;
+}
+
 static void sendopt(telnetd_state_t *s, u8_t code, u8_t option)
 {
 	unsigned char buf[3];
@@ -487,6 +503,10 @@ static void stop_server(void) {
 		 }
 	}
 	DBG printf("Portgw stopped\n");
+
+	for (int i = 0; i < array_len(auth_tlnt_srvr_param); ++i) {
+		deconfigure_telnet_state(auth_tlnt_srvr_param + i);
+	}
 }
 
 /* Start server */
@@ -632,11 +652,11 @@ void vBasicSocketsCommandInterpreterTask( void *pvParameters )
 		 * determine which ones they are.                         */
 		desc_ready = rc;
 
-		for (int i=0; i <= max_sd  &&  desc_ready > 0; ++i) {
+		for (int sock = 0; sock <= max_sd && desc_ready > 0; ++sock) {
 			/*******************************************************/
 			/* Check to see if this descriptor is ready            */
 			/*******************************************************/
-			if (FD_ISSET(i, &read_set)) {
+			if (FD_ISSET(sock, &read_set)) {
 				/* A descriptor was found that was readable - one  \
 				* less has to be looked for.  This is being done   \
 				* so that we can stop looking at the working set   \
@@ -645,7 +665,7 @@ void vBasicSocketsCommandInterpreterTask( void *pvParameters )
 				desc_ready -= 1;
 
 				/* Check to see if this is the listening socket     */
-				if (i == lSocket) {
+				if (sock == lSocket) {
 					DBG printf("  Listening socket is readable\n");
 
 					/* Accept all incoming connections that are      */
@@ -689,7 +709,6 @@ void vBasicSocketsCommandInterpreterTask( void *pvParameters )
 									cli_state->num_connect = new_sd;
 									cli_state->send = cli_send;
 									cli_state->state = STATE_NORMAL;
-									cli_state->input_state = CLI_AUTH;
 									auth_tlnt_srvr_param[k].cli_state = cli_state;
 
 									while(recv( new_sd, &auth_tlnt_srvr_param[k].buf[auth_tlnt_srvr_param[k].bufptr], 1,  MSG_DONTWAIT ) > 0){
@@ -749,10 +768,10 @@ void vBasicSocketsCommandInterpreterTask( void *pvParameters )
 				/* This is not the listening socket, therefore an   */
 				/* existing connection must be readable             */
 				else {
-					DBG printf("  Descriptor %d is readable\n", i);
+					DBG printf("  Descriptor %d is readable\n", sock);
 
 					for(cur_cnt = 0; cur_cnt < NUMBER_TELNET_CONNECT; cur_cnt ++){
-						if(auth_tlnt_srvr_param[cur_cnt].num_connect == i){
+						if(auth_tlnt_srvr_param[cur_cnt].num_connect == sock){
 							const user_level_t telnet_code_auth = auth_tlnt_srvr_param[cur_cnt].telnet_code_auth;
 							switch (telnet_code_auth) {
 							  case ADMIN:
@@ -772,9 +791,8 @@ void vBasicSocketsCommandInterpreterTask( void *pvParameters )
 					/* recv fails with EWOULDBLOCK.  If any other */
 					/* failure occurs, we will close the          */
 					/* connection.                                */
-					if ((lBytes = recv(i, &auth_tlnt_srvr_param[cur_cnt].buf[auth_tlnt_srvr_param[cur_cnt].bufptr], 1, 0 )) > 0) {
-
-							newdata(&auth_tlnt_srvr_param[cur_cnt]);
+					if ((lBytes = recv(sock, &auth_tlnt_srvr_param[cur_cnt].buf[auth_tlnt_srvr_param[cur_cnt].bufptr], 1, 0 )) > 0) {
+						newdata(&auth_tlnt_srvr_param[cur_cnt]);
 					}
 
 					if (lBytes < 0) {
@@ -799,20 +817,11 @@ void vBasicSocketsCommandInterpreterTask( void *pvParameters )
 					/* based on the bits that are still turned on in */
 					/* the master set.                               */
 					if (auth_tlnt_srvr_param[cur_cnt].state == TELNET_STATE_CLOSE || auth_tlnt_srvr_param[cur_cnt].cli_state->state == STATE_CLOSE) {
-						free_state(auth_tlnt_srvr_param[cur_cnt].cli_state);
-						memset(auth_tlnt_srvr_param[cur_cnt].buf, 0, cmdMAX_INPUT_SIZE);
-						memset(auth_tlnt_srvr_param[cur_cnt].prev_cmd, 0, cmdMAX_INPUT_SIZE);
-						memset(auth_tlnt_srvr_param[cur_cnt].optdata, 0, cmdMAX_INPUT_SIZE);
-						auth_tlnt_srvr_param[cur_cnt].optlen = 0;
-						auth_tlnt_srvr_param[cur_cnt].state = TELNET_STATE_NORMAL;
-						auth_tlnt_srvr_param[cur_cnt].num_connect = 0;
-						auth_tlnt_srvr_param[cur_cnt].active_conn = false;
-						auth_tlnt_srvr_param[cur_cnt].telnet_code_auth = USER;
-						auth_tlnt_srvr_param[cur_cnt].cli_state->flag_telnet_ip_option = false;
+						deconfigure_telnet_state(auth_tlnt_srvr_param + cur_cnt);
 						cnt_conn -= 1;
-						closesocket(i);
-						FD_CLR(i, &master_set);
-						if (i == max_sd) {
+						closesocket(sock);
+						FD_CLR(sock, &master_set);
+						if (sock == max_sd) {
 							while (FD_ISSET(max_sd, &master_set) == false) {
 								max_sd -= 1;
 							}