Browse Source

[bt6708]add white list in telnet server

balbekova 6 years ago
parent
commit
d57c17b189

+ 169 - 1
modules/Telnet_Server/CLI_Commands.c

@@ -87,6 +87,12 @@ const char* snmp_args_list[] =
     "community",
 };
 
+const char* whitelist_args_list[] =
+{
+	"info",
+	"range",
+};
+
 const char* ntp_args_list[] =
 {
 	"ENA",
@@ -161,6 +167,13 @@ static portBASE_TYPE prvTaskNetworkCommand( int8_t *pcWriteBuffer, size_t xWrite
  */
 static portBASE_TYPE prvTaskSNMPCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
 
+#ifdef HARDWARE_BT6708
+/*
+ * Implements the whitelist command.
+ */
+static portBASE_TYPE prvTaskWhiteListCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString );
+#endif
+
 /*
  * Implements the change password command.
  */
@@ -272,6 +285,19 @@ static const CLI_Command_Definition_t prvSNMPCommandDefinition =
 	-1 /* The user can enter any number of commands. */
 };
 
+#ifdef HARDWARE_BT6708
+/* Structure that defines the "whitelist" command line command.   This
+generates a table that shows how much run time each task has */
+static const CLI_Command_Definition_t prvWhiteListCommandDefinition =
+{
+	( const int8_t * const ) "whitelist", /* The command string to type. */
+	( const int8_t * const ) "\twhitelist info: вывод информации о текущем белом списке IP адресов контроллера\r\n"
+	 	 	 	 	 	 	 "\twhitelist range <NUM> <A.B.C.D/E>: установка диапазона IP адресов\r\n",
+	prvTaskWhiteListCommand, /* The function to run. */
+	-1 /* The user can enter any number of commands. */
+};
+#endif
+
 /* Structure that defines the "user" command line command.   This
 generates a table that shows how much run time each task has */
 static const CLI_Command_Definition_t prvUserCommandDefinition =
@@ -375,6 +401,9 @@ void vRegisterCLICommands( void )
 	FreeRTOS_CLIRegisterCommand( &prvNTPCommandDefinition );
 	FreeRTOS_CLIRegisterCommand( &prvNetworkCommandDefinition );
 	FreeRTOS_CLIRegisterCommand( &prvSNMPCommandDefinition );
+#ifdef HARDWARE_BT6708
+	FreeRTOS_CLIRegisterCommand( &prvWhiteListCommandDefinition );
+#endif
 	FreeRTOS_CLIRegisterCommand( &prvUserCommandDefinition );
 	FreeRTOS_CLIRegisterCommand( &prvConfigCommandDefinition );
 	FreeRTOS_CLIRegisterCommand( &prvNetConfigCommandDefinition );
@@ -1210,6 +1239,140 @@ static portBASE_TYPE prvTaskSNMPCommand( int8_t *pcWriteBuffer, size_t xWriteBuf
 	return xReturn;
 }
 
+#ifdef HARDWARE_BT6708
+/*
+ * Implements the whitelist command.
+ */
+static portBASE_TYPE prvTaskWhiteListCommand( int8_t *pcWriteBuffer, size_t xWriteBufferLen, const int8_t *pcCommandString ){
+	int8_t *pcParameterString;
+	signed portBASE_TYPE xParameterStringLength, xReturn;
+	portBASE_TYPE xParameterNumber = 1;
+	char str[20];
+	char temp_str[20];
+	uint8_t i;
+	char *beginValue;
+	uint8_t len;
+
+	( void ) pcCommandString;
+	( void ) xWriteBufferLen;
+	configASSERT( pcWriteBuffer );
+
+	memset(pcWriteBuffer, 0, configCOMMAND_INT_MAX_OUTPUT_SIZE);
+	/* Obtain the parameter string. */
+	pcParameterString = ( int8_t * ) FreeRTOS_CLIGetParameter
+															(
+																pcCommandString,		/* The command string itself. */
+																xParameterNumber,		/* Return the next parameter. */
+																&xParameterStringLength	/* Store the parameter string length. */
+															);
+	if(pcParameterString == NULL){
+		strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcInvalidCommand, strlen( ( char * ) pcInvalidCommand ) );
+		return pdFALSE;
+	}
+
+	for(i = 0; i < ARG_WHITELIST_ALL; i ++){
+		if( strncmp( ( const char * ) pcParameterString, whitelist_args_list[i], strlen(whitelist_args_list[i]) ) == 0 ){
+			break;
+		}
+	}
+
+	if(telnet_code_auth != ADMIN && i !=ARG_WHITELIST_INFO){
+		strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcPermissionDenied, strlen( ( char * ) pcPermissionDenied ) );
+		return pdFALSE;
+	}
+	xParameterNumber ++;
+	/* Obtain the parameter string. */
+	pcParameterString = ( int8_t * ) FreeRTOS_CLIGetParameter
+															(
+																pcCommandString,		/* The command string itself. */
+																xParameterNumber,		/* Return the next parameter. */
+																&xParameterStringLength	/* Store the parameter string length. */
+															);
+	xParameterNumber ++;
+	if(pcParameterString == NULL){
+		if(i == ARG_WHITELIST_INFO){
+			whitelist_config_param(pcWriteBuffer);
+		}
+		else{
+			strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcInvalidCommand, strlen( ( char * ) pcInvalidCommand ) );
+		}
+		return pdFALSE;
+	}
+	memset(str, 0, sizeof(str));
+	if(xParameterStringLength > (int32_t)sizeof(str))
+		xParameterStringLength = sizeof(str) - 1;
+	strncat(str, ( const char * ) pcParameterString, xParameterStringLength);
+	switch(i){
+	case ARG_WHITELIST_RANGE:
+		if(xParameterStringLength == 1 && isdigit_int(str[0])){
+			int32_t temp = atoi(str);
+			if(temp > 0 && temp < 6){
+				pcParameterString = ( int8_t * ) FreeRTOS_CLIGetParameter
+																		(
+																			pcCommandString,		/* The command string itself. */
+																			xParameterNumber,		/* Return the next parameter. */
+																			&xParameterStringLength	/* Store the parameter string length. */
+																		);
+				if(pcParameterString != NULL){
+					memset(str, 0, sizeof(str));
+					xParameterStringLength = strlen(( const char * ) pcParameterString);
+					if(xParameterStringLength > (int32_t)sizeof(str))
+						xParameterStringLength = sizeof(str) - 1;
+					strncat(str, ( const char * ) pcParameterString, strlen(( const char * ) pcParameterString));
+					if(xParameterStringLength <= 19){
+						beginValue = strpbrk(str,"/");
+						if(beginValue == NULL){
+							strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcInvalidCommand, strlen( ( char * ) pcInvalidCommand ) );
+							return pdFALSE;
+						}
+						len = beginValue - str;
+						memset(temp_str, 0, sizeof(temp_str));
+						strncpy(temp_str, str, len);
+						for(uint8_t j = (len + 1); j < xParameterStringLength; j++){
+							if(!isdigit_int(str[j])){
+								strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcInvalidCommand, strlen( ( char * ) pcInvalidCommand ) );
+								return pdFALSE;
+							}
+						}
+						if(ipaddr_addr(temp_str) != IPADDR_NONE){
+							SetWhiteListSTR(str, (temp - 1));
+						}
+						else{
+							strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcInvalidCommand, strlen( ( char * ) pcInvalidCommand ) );
+						}
+					}
+					else{
+						strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcInvalidCommand, strlen( ( char * ) pcInvalidCommand ) );
+					}
+				}
+				else{
+					memset(str, 0, sizeof(str));
+					SetWhiteListSTR(str, temp);
+					if(temp > 0 && temp < 6){
+						SETTINGS_Save();
+						log_event_data(LOG_SETTING_SAVE, name_login_telnet);
+					}
+				}
+			}
+			else{
+				strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcInvalidCommand, strlen( ( char * ) pcInvalidCommand ) );
+			}
+		}
+		else{
+			strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcInvalidCommand, strlen( ( char * ) pcInvalidCommand ) );
+		}
+		break;
+	default:
+		strncpy( ( char * ) pcWriteBuffer, ( const char * ) pcInvalidCommand, strlen( ( char * ) pcInvalidCommand ) );
+		break;
+	}
+	xReturn = pdFALSE;
+
+
+	return xReturn;
+}
+#endif
+
 /*
  * Implements the change password command.
  */
@@ -1360,6 +1523,11 @@ static portBASE_TYPE prvTaskConfigCommand( int8_t *pcWriteBuffer, size_t xWriteB
 			case PARAM_CONFIG_INOUTS:
 				inouts_config_param(pcWriteBuffer);
 				break;
+#endif
+#ifdef HARDWARE_BT6708
+			case PARAM_CONFIG_WHITELIST:
+				whitelist_config_param(pcWriteBuffer);
+				break;
 #endif
 			case PARAM_CONFIG_NET:
 				net_config_param(pcWriteBuffer);
@@ -1459,7 +1627,7 @@ static portBASE_TYPE prvTaskNetConfigCommand( int8_t *pcWriteBuffer, size_t xWri
 		  if (GetStateWebReinit() == true)
 		  {
 			  start = 1;
-		  telnet_act = true;
+			  telnet_act = true;
 			SetWebReinitFlag(true);
 			HTTP_SaveSettings();
 			strcpy( ( char * ) pcWriteBuffer, "\t\tНастройки сохранены! Контроллер будет перезагружен\r\n\tПосле перезагрузки подтвердите изменения сетевых настроек\r\n");

+ 9 - 0
modules/Telnet_Server/CLI_Commands.h

@@ -44,6 +44,12 @@ typedef enum{
 	ARG_SNMP_ALL
 }snmp_args_t;
 
+typedef enum{
+	ARG_WHITELIST_INFO = 0,
+	ARG_WHITELIST_RANGE,
+	ARG_WHITELIST_ALL
+}whitelist_args_t;
+
 typedef enum{
 	ARG_NTP_ENABLE = 0,
 	ARG_NTP_DISABLE,
@@ -62,6 +68,9 @@ typedef enum{
 	PARAM_CONFIG_SNMP = 0,
 #ifdef HARDWARE_BT6706
 	PARAM_CONFIG_INOUTS,
+#endif
+#ifdef HARDWARE_BT6708
+	PARAM_CONFIG_WHITELIST,
 #endif
 	PARAM_CONFIG_NET,
 	PARAM_CONFIG_TIME,

+ 31 - 0
modules/Telnet_Server/CLI_Parameters.c

@@ -210,6 +210,37 @@ void time_config_param(int8_t *buf)
 	strcat(( char * ) buf, "\r\n");
 }
 
+#ifdef HARDWARE_BT6708
+//Белый список IP адресов контроллера
+void whitelist_config_param(int8_t *buf)
+{
+	char str[20];
+	uint8_t len = 0;
+	uint8_t i;
+
+	const char *name_range_ip[] = {
+			"IP-адрес 1:\t\t\t",
+			"\r\nIP-адрес 2:\t\t\t",
+			"\r\nIP-адрес 3:\t\t\t",
+			"\r\nIP-адрес 4:\t\t\t",
+			"\r\nIP-адрес 5:\t\t\t",
+	};
+
+	const int8_t * const pcInfoTableHeader = ( int8_t * ) "\r\n*******Белый список IP адресов Контроллера*******\r\n";
+	/* Return the next command help string, before moving the pointer on to
+	the next command in the list. */
+	strncpy( ( char * ) buf, ( const char * ) pcInfoTableHeader, strlen( ( char * ) pcInfoTableHeader ) );
+
+	for(uint8_t i = 0; i < MAX_WHITE_LIST; i++){
+		GetWhiteListSTR(str, &len, i);
+		strcat(( char * ) buf, name_range_ip[i]);
+		strncat(( char * ) buf, str, len);
+	}
+
+	strcat(( char * ) buf, "\r\n");
+}
+#endif
+
 //Параметры ИБП
 void ups_sensor_param(int8_t *buf)
 {

+ 5 - 0
modules/Telnet_Server/CLI_Parameters.h

@@ -23,6 +23,11 @@ void net_config_param(int8_t *buf);
 //Настройки времени
 void time_config_param(int8_t *buf);
 
+#ifdef HARDWARE_BT6708
+//Белый список IP адресов контроллера
+void whitelist_config_param(int8_t *buf);
+#endif
+
 //Параметры ИБП
 void ups_sensor_param(int8_t *buf);