mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-21 15:47:06 +01:00
Telnet password setting (#1382)
* Telnet auth depends on setting * use same define suffix as WS_ * add to web * no initial passwd for core
This commit is contained in:
@@ -110,8 +110,8 @@
|
||||
#define TELNET_STA 0 // By default, disallow connections via STA interface
|
||||
#endif
|
||||
|
||||
#ifndef TELNET_PASSWORD
|
||||
#define TELNET_PASSWORD 1 // Request password to start telnet session by default
|
||||
#ifndef TELNET_AUTHENTICATION
|
||||
#define TELNET_AUTHENTICATION 1 // Request password to start telnet session by default
|
||||
#endif
|
||||
|
||||
#define TELNET_PORT 23 // Port to listen to telnet clients
|
||||
|
||||
@@ -15,9 +15,9 @@ Parts of the code have been borrowed from Thomas Sarlandie's NetServer
|
||||
AsyncServer * _telnetServer;
|
||||
AsyncClient * _telnetClients[TELNET_MAX_CLIENTS];
|
||||
bool _telnetFirst = true;
|
||||
#if TELNET_PASSWORD
|
||||
bool _authenticated[TELNET_MAX_CLIENTS];
|
||||
#endif
|
||||
|
||||
bool _telnetAuth = TELNET_AUTHENTICATION;
|
||||
bool _telnetClientsAuth[TELNET_MAX_CLIENTS];
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Private methods
|
||||
@@ -32,6 +32,7 @@ bool _telnetWebSocketOnReceive(const char * key, JsonVariant& value) {
|
||||
void _telnetWebSocketOnSend(JsonObject& root) {
|
||||
root["telnetVisible"] = 1;
|
||||
root["telnetSTA"] = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
|
||||
root["telnetAuth"] = getSetting("telnetAuth", TELNET_AUTHENTICATION).toInt() == 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -54,16 +55,12 @@ bool _telnetWrite(unsigned char clientId, void *data, size_t len) {
|
||||
unsigned char _telnetWrite(void *data, size_t len) {
|
||||
unsigned char count = 0;
|
||||
for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
|
||||
// Do not send broadcast messages to unauthenticated clients
|
||||
if (_telnetAuth && !_telnetClientsAuth[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
#if TELNET_PASSWORD
|
||||
// Do not send broadcast messages to unauthenticated clients
|
||||
if (_authenticated[i]) {
|
||||
if (_telnetWrite(i, data, len)) ++count;
|
||||
}
|
||||
#else
|
||||
if (_telnetWrite(i, data, len)) ++count;
|
||||
#endif
|
||||
|
||||
if (_telnetWrite(i, data, len)) ++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -96,20 +93,24 @@ void _telnetData(unsigned char clientId, void *data, size_t len) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Password
|
||||
#if TELNET_PASSWORD
|
||||
if (!_authenticated[clientId]) {
|
||||
String password = getAdminPass();
|
||||
if (strncmp(p, password.c_str(), password.length()) == 0) {
|
||||
DEBUG_MSG_P(PSTR("[TELNET] Client #%d authenticated\n"), clientId);
|
||||
_telnetWrite(clientId, "Welcome!\n");
|
||||
_authenticated[clientId] = true;
|
||||
} else {
|
||||
_telnetWrite(clientId, "Password: ");
|
||||
}
|
||||
return;
|
||||
// Password prompt (disable on CORE variant)
|
||||
#ifdef ESPURNA_CORE
|
||||
const bool authenticated = true;
|
||||
#else
|
||||
const bool authenticated = _telnetClientsAuth[clientId];
|
||||
#endif
|
||||
|
||||
if (_telnetAuth && !authenticated) {
|
||||
String password = getAdminPass();
|
||||
if (strncmp(p, password.c_str(), password.length()) == 0) {
|
||||
DEBUG_MSG_P(PSTR("[TELNET] Client #%d authenticated\n"), clientId);
|
||||
_telnetWrite(clientId, "Welcome!\n");
|
||||
_telnetClientsAuth[clientId] = true;
|
||||
} else {
|
||||
_telnetWrite(clientId, "Password: ");
|
||||
}
|
||||
#endif // TELNET_PASSWORD
|
||||
return;
|
||||
}
|
||||
|
||||
// Inject command
|
||||
settingsInject(data, len);
|
||||
@@ -175,9 +176,11 @@ void _telnetNewClient(AsyncClient *client) {
|
||||
debugClearCrashInfo();
|
||||
#endif
|
||||
|
||||
#if TELNET_PASSWORD
|
||||
_authenticated[i] = false;
|
||||
_telnetWrite(i, "Password: ");
|
||||
#ifdef ESPURNA_CORE
|
||||
_telnetClientsAuth[i] = true;
|
||||
#else
|
||||
_telnetClientsAuth[i] = !_telnetAuth;
|
||||
if (_telnetAuth) _telnetWrite(i, "Password: ");
|
||||
#endif
|
||||
|
||||
_telnetFirst = true;
|
||||
@@ -214,6 +217,10 @@ unsigned char telnetWrite(unsigned char ch) {
|
||||
return _telnetWrite(data, 1);
|
||||
}
|
||||
|
||||
void _telnetConfigure() {
|
||||
_telnetAuth = getSetting("telnetAuth", TELNET_AUTHENTICATION).toInt() == 1;
|
||||
}
|
||||
|
||||
void telnetSetup() {
|
||||
|
||||
_telnetServer = new AsyncServer(TELNET_PORT);
|
||||
@@ -227,6 +234,9 @@ void telnetSetup() {
|
||||
wsOnReceiveRegister(_telnetWebSocketOnReceive);
|
||||
#endif
|
||||
|
||||
espurnaRegisterReload(_telnetConfigure);
|
||||
_telnetConfigure();
|
||||
|
||||
DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
|
||||
|
||||
}
|
||||
|
||||
@@ -608,6 +608,13 @@
|
||||
<div class="pure-u-1 pure-u-lg-3-4 hint">Turn ON to be able to telnet to your device while connected to your home router.<br />TELNET is always enabled in AP mode.</div>
|
||||
</div>
|
||||
|
||||
<div class="pure-g module module-telnet">
|
||||
<label class="pure-u-1 pure-u-lg-1-4">TELNET Password</label>
|
||||
<div class="pure-u-1 pure-u-lg-1-4"><input type="checkbox" name="telnetAuth" /></div>
|
||||
<div class="pure-u-0 pure-u-lg-1-2"></div>
|
||||
<div class="pure-u-0 pure-u-lg-1-4"></div>
|
||||
<div class="pure-u-1 pure-u-lg-3-4 hint">Request password when starting telnet session</div>
|
||||
</div>
|
||||
|
||||
<div class="pure-g module module-nofuss">
|
||||
<label class="pure-u-1 pure-u-lg-1-4">Automatic remote updates (NoFUSS)</label>
|
||||
|
||||
Reference in New Issue
Block a user