Soft-AP server side variables check

Proper check of Soft-AP setting’s variables values on server side.
Only alphanumeric values and underscore are allowed in SSID and
Password. Now you can set Soft-AP as OPEN AP with custom SSID.
pull/84/head
KatAst 9 years ago
parent d6dae6888a
commit e268db7e84
  1. 149
      esp-link/cgiwifi.c

@ -311,9 +311,9 @@ static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
if(m!=2){
if( x == STATION_GOT_IP ){
#ifdef CHANGE_TO_STA
#ifdef CHANGE_TO_STA
wifi_set_opmode(1);
#endif
#endif
log_uart(false);
}else{
log_uart(true);
@ -513,6 +513,7 @@ int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
// No changes for Soft-AP in STA mode
int mode = wifi_get_opmode();
if ( mode == 1 ){
@ -525,6 +526,8 @@ int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) {
char buff[96];
int len;
// Do we need a password?
int pass_need=1;
// Check extra security measure
len=httpdFindArg(connData->getArgs, "100", buff, sizeof(buff));
@ -536,26 +539,51 @@ int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) {
}
// Get the new SSID and set
len=httpdFindArg(connData->getArgs, "ap_ssid", buff, sizeof(buff));
if(len>7 && len<30){
if(checkString(buff) && len>7 && len<32){
// STRING PREPROCESSING DONE IN CLIENT SIDE
memset(apconf.ssid, 0, 32);
os_memcpy(apconf.ssid, buff, len);
apconf.ssid_len = len;
pass_need = 1;
}else{
pass_need = 0;
jsonHeader(connData, 400);
httpdSend(connData, "SSID name out of range", -1);
httpdSend(connData, "SSID not valid or out of range", -1);
return HTTPD_CGI_DONE;
}
// Set new PASSWORD
if( pass_need ){
len=httpdFindArg(connData->getArgs, "ap_password", buff, sizeof(buff));
if(len>7 && len<62){
if(checkString(buff) && len>7 && len<62){
// STRING PREPROCESSING DONE IN CLIENT SIDE
memset(apconf.password, 0, 64);
os_memcpy(apconf.password, buff, len);
pass_need = 1;
}else if (len == 0){
pass_need = 0;
memset(apconf.password, 0, 64);
}else{
jsonHeader(connData, 400);
httpdSend(connData, "PASSWORD out of range", -1);
httpdSend(connData, "PASSWORD not valid or out of range", -1);
return HTTPD_CGI_DONE;
}
}
// Set auth mode
if(pass_need){
// Set authentication mode, before password to check open settings
len=httpdFindArg(connData->getArgs, "ap_authmode", buff, sizeof(buff));
if(len>0){
int value = atoi(buff);
if(value >= 0 && value <= 4){
apconf.authmode = value;
}else{
// If out of range set by default
apconf.authmode = 4;
}
}
}else{
apconf.authmode = 0;
}
// Set max connection number
len=httpdFindArg(connData->getArgs, "ap_maxconn", buff, sizeof(buff));
if(len>0){
@ -579,17 +607,6 @@ int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) {
apconf.beacon_interval = 100;
}
}
// Set authentication mode
len=httpdFindArg(connData->getArgs, "ap_authmode", buff, sizeof(buff));
if(len>0){
int value = atoi(buff);
if(value >= 0 && value <= 4){
apconf.authmode = value;
}else{
// If out of range set by default
apconf.authmode = 4;
}
}
// Set ssid to be hidden or not
len=httpdFindArg(connData->getArgs, "ap_hidden", buff, sizeof(buff));
if(len>0){
@ -809,6 +826,29 @@ int ICACHE_FLASH_ATTR cgiWifiInfo(HttpdConnData *connData) {
return HTTPD_CGI_DONE;
}
// Check string againt invalid characters
int ICACHE_FLASH_ATTR checkString(char *str){
unsigned char strStripped[50];
int i = 0, c = 0;
for(; i < os_strlen(str); i++)
{
// Alphanumeric and underscore allowed
if (isalnum((unsigned char)str[i]) || str[i] == '_')
{
strStripped[c] = str[i];
c++;
}
}
strStripped[c] = '\0';
if (os_strcmp((char *)str,(char *)&strStripped) == 0){
return 1;
}else{
os_printf("Error: String has non alphanumeric chars\n");
return 0;
}
}
/* Init the wireless
*
* Call both Soft-AP and Station default config
@ -828,67 +868,82 @@ void ICACHE_FLASH_ATTR wifiInit() {
wifi_station_get_config_default(&stconf);
wifi_softap_get_config_default(&apconf);
#ifdef CGIWIFI_DBG
#ifdef CGIWIFI_DBG
os_printf("Wifi init, mode=%s\n",wifiMode[x]);
#endif
#endif
// STATION parameters only on a full flash, because default opmode is 2
#if defined(STA_SSID) && defined(STA_PASS)
#if defined(STA_SSID) && defined(STA_PASS)
if( x == 2 ){
// Set parameters
if (os_strlen((char*)stconf.ssid) == 0 && os_strlen((char*)stconf.password) == 0) {
os_strncpy((char*)stconf.ssid, VERS_STR(STA_SSID), 32);
os_strncpy((char*)stconf.password, VERS_STR(STA_PASS), 64);
#ifdef CGIWIFI_DBG
#ifdef CGIWIFI_DBG
os_printf("Wifi pre-config trying to connect to AP %s pw %s\n",(char*)stconf.ssid, (char*)stconf.password);
#endif
#endif
// wifi_set_phy_mode(2); // limit to 802.11b/g 'cause n is flaky
stconf.bssid_set = 0;
wifi_station_set_config(&stconf);
}
}
#endif
#endif
// Change SOFT_AP parameters if defined
#if defined(AP_SSID) && defined(AP_PASS)
// Change SOFT_AP settings if defined
#if defined(AP_SSID) && defined(AP_PASS)
// Check if both ssid and pass are alphanumeric values
if(checkString(VERS_STR(AP_SSID)) && checkString(VERS_STR(AP_PASS))){
// Clean memory and set the value of SSID
memset(apconf.ssid, 0, 32);
os_memcpy(apconf.ssid, VERS_STR(AP_SSID), strlen(VERS_STR(AP_SSID)));
os_memcpy(apconf.ssid, VERS_STR(AP_SSID), os_strlen(VERS_STR(AP_SSID)));
// Specify the length of pass
apconf.ssid_len= os_strlen((char*)VERS_STR(AP_SSID));
// If pass is at least 8 and less than 64
int passlen = os_strlen(VERS_STR(AP_PASS));
if( passlen > 7 && passlen < 64 ){
// Clean memory and set the value of PASS
memset(apconf.password, 0, 64);
os_memcpy(apconf.password, VERS_STR(AP_PASS), strlen(VERS_STR(AP_PASS)));
os_memcpy(apconf.password, VERS_STR(AP_PASS), passlen);
// Specify the length of pass
apconf.ssid_len= os_strlen((char*)VERS_STR(AP_PASS));
#ifdef AP_AUTH_MODE
// Can't choose auth mode without a valid ssid and password
#ifdef AP_AUTH_MODE
// If set, use specified auth mode
if(AP_AUTH_MODE >= 0 && AP_AUTH_MODE <=4)
apconf.authmode = AP_AUTH_MODE;
#else
// If not, use wpa wpa2 psk
apconf.authmode = AUTH_WPA_WPA2_PSK;
#endif
#ifdef AP_SSID_HIDDEN
#else
// If not, use OPEN
apconf.authmode = AUTH_OPEN;
#endif
}else if ( passlen == 0){
// If ssid is ok and no pass set auth open
apconf.authmode = AUTH_OPEN;
// Remove stored password
memset(apconf.password, 0, 64);
}
}// end of ssid and pass check
#ifdef AP_SSID_HIDDEN
// If set, use specified ssid hidden parameter
if(AP_SSID_HIDDEN == 0 || AP_SSID_HIDDEN ==1)
apconf.ssid_hidden = AP_SSID_HIDDEN;
#endif
#ifdef AP_MAX_CONN
#endif
#ifdef AP_MAX_CONN
// If set, use specified max conn number
if(AP_MAX_CONN > 0 && AP_MAX_CONN <5)
apconf.max_connection = AP_MAX_CONN;
#endif
#ifdef AP_BEACON_INTERVAL
#endif
#ifdef AP_BEACON_INTERVAL
// If set use specified beacon interval
if(AP_BEACON_INTERVAL >= 100 && AP_BEACON_INTERVAL <= 60000)
apconf.beacon_interval = AP_BEACON_INTERVAL;
#endif
#endif
// Check save softap config
bool softap_set_conf = wifi_softap_set_config(&apconf);
#ifdef CGIWIFI_DBG
#ifdef CGIWIFI_DBG
// Debug info
os_printf("Wifi AP parameters: %s pw %s\n",(char*)apconf.ssid, (char*)apconf.password);
os_printf("Wifi Soft-AP parameters set: %s\n",softap_set_conf? "success":"fail");
#endif
#endif // AP_SSID && AP_PASS
//os_printf("Wifi AP parameters: %s pw %s\n",(char*)apconf.ssid, (char*)apconf.password);
os_printf("Wifi Soft-AP parameters change: %s\n",softap_set_conf? "success":"fail");
#endif
#endif // AP_SSID && AP_PASS
configWifiIP();

Loading…
Cancel
Save