diff --git a/apps/wolfsshd/configuration.c b/apps/wolfsshd/configuration.c index 71d2d5de6..b2c4262fe 100644 --- a/apps/wolfsshd/configuration.c +++ b/apps/wolfsshd/configuration.c @@ -64,6 +64,7 @@ struct WOLFSSHD_CONFIG { char* listenAddress; char* authKeysFile; char* forceCmd; + char* pidFile; WOLFSSHD_CONFIG* next; /* next config in list */ long loginTimer; word16 port; @@ -76,6 +77,7 @@ struct WOLFSSHD_CONFIG { }; int CountWhitespace(const char* in, int inSz, byte inv); +int SetFileString(char** dst, const char* src, void* heap); /* convert a string into seconds, handles if 'm' for minutes follows the string * number, i.e. 2m @@ -294,6 +296,7 @@ void wolfSSHD_ConfigFree(WOLFSSHD_CONFIG* conf) FreeString(¤t->authKeysFile, heap); FreeString(¤t->hostKeyFile, heap); FreeString(¤t->hostCertFile, heap); + FreeString(¤t->pidFile, heap); WFREE(current, heap, DYNTYPE_SSHD); current = next; @@ -330,9 +333,10 @@ enum { OPT_FORCE_CMD = 19, OPT_HOST_CERT = 20, OPT_TRUSTED_USER_CA_KEYS = 21, + OPT_PIDFILE = 22, }; enum { - NUM_OPTIONS = 22 + NUM_OPTIONS = 23 }; static const CONFIG_OPTION options[NUM_OPTIONS] = { @@ -358,6 +362,7 @@ static const CONFIG_OPTION options[NUM_OPTIONS] = { {OPT_FORCE_CMD, "ForceCommand"}, {OPT_HOST_CERT, "HostCertificate"}, {OPT_TRUSTED_USER_CA_KEYS, "TrustedUserCAKeys"}, + {OPT_PIDFILE, "PidFile"}, }; /* returns WS_SUCCESS on success */ @@ -999,6 +1004,9 @@ static int HandleConfigOption(WOLFSSHD_CONFIG** conf, int opt, /* TODO: Add logic to check if file exists? */ ret = wolfSSHD_ConfigSetUserCAKeysFile(*conf, value); break; + case OPT_PIDFILE: + ret = SetFileString(&(*conf)->pidFile, value, (*conf)->heap); + break; default: break; } @@ -1070,8 +1078,13 @@ WOLFSSHD_STATIC int ParseConfigLine(WOLFSSHD_CONFIG** conf, const char* l, } } else { + #ifdef WOLFSSH_IGNORE_UNKNOWN_CONFIG + wolfSSH_Log(WS_LOG_DEBUG, "[SSHD] ignoring config line %s.", l); + ret = WS_SUCCESS; + #else wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error parsing config line."); ret = WS_FATAL_ERROR; + #endif } return ret; @@ -1288,7 +1301,7 @@ char* wolfSSHD_ConfigGetUserCAKeysFile(const WOLFSSHD_CONFIG* conf) return ret; } -static int SetFileString(char** dst, const char* src, void* heap) +int SetFileString(char** dst, const char* src, void* heap) { int ret = WS_SUCCESS; @@ -1420,4 +1433,20 @@ long wolfSSHD_ConfigGetGraceTime(const WOLFSSHD_CONFIG* conf) return ret; } + + +/* Used to save out the PID of SSHD to a file */ +void wolfSSHD_ConfigSavePID(const WOLFSSHD_CONFIG* conf) +{ + FILE* f; + char buf[12]; /* large enough to hold 'int' type with null terminator */ + + WMEMSET(buf, 0, sizeof(buf)); + if (WFOPEN(&f, conf->pidFile, "wb") == 0) { + WSNPRINTF(buf, sizeof(buf), "%d", getpid()); + WFWRITE(buf, 1, WSTRLEN(buf), f); + WFCLOSE(f); + } +} + #endif /* WOLFSSH_SSHD */ diff --git a/apps/wolfsshd/configuration.h b/apps/wolfsshd/configuration.h index 9d68d0b31..68807975d 100644 --- a/apps/wolfsshd/configuration.h +++ b/apps/wolfsshd/configuration.h @@ -56,6 +56,7 @@ WOLFSSHD_CONFIG* wolfSSHD_GetUserConf(const WOLFSSHD_CONFIG* conf, const char* usr, const char* grp, const char* host, const char* localAdr, word16* localPort, const char* RDomain, const char* adr); +void wolfSSHD_ConfigSavePID(const WOLFSSHD_CONFIG* conf); #ifdef WOLFSSHD_UNIT_TEST int ParseConfigLine(WOLFSSHD_CONFIG** conf, const char* l, int lSz); diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index f014cc85e..0202c7e9e 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -1276,6 +1276,7 @@ int main(int argc, char** argv) WOLFSSHD_AUTH* auth = NULL; WOLFSSH_CTX* ctx = NULL; byte isDaemon = 1; + byte testMode = 0; const char* configFile = "/etc/ssh/sshd_config"; const char* hostKeyFile = NULL; @@ -1300,7 +1301,7 @@ int main(int argc, char** argv) } } - while ((ch = mygetopt(argc, argv, "?f:p:h:dDE:")) != -1) { + while ((ch = mygetopt(argc, argv, "?f:p:h:dDE:o:t")) != -1) { switch (ch) { case 'f': configFile = myoptarg; @@ -1352,6 +1353,19 @@ int main(int argc, char** argv) } break; + case 'o': + #ifdef WOLFSSH_IGNORE_UNKNOWN_CONFIG + wolfSSH_Log(WS_LOG_DEBUG, "[SSHD] ignoring -o."); + break; + #else + ShowUsage(); + return WS_FATAL_ERROR; + #endif + + case 't': + testMode = 1; + break; + case '?': ShowUsage(); return WS_SUCCESS; @@ -1364,8 +1378,9 @@ int main(int argc, char** argv) if (ret == WS_SUCCESS) { ret = wolfSSHD_ConfigLoad(conf, configFile); - if (ret != WS_SUCCESS) + if (ret != WS_SUCCESS) { fprintf(stderr, "Error reading in configure file %s\n", configFile); + } } /* port was not overridden with argument, read from config file */ @@ -1455,13 +1470,14 @@ int main(int argc, char** argv) } if (ret == WS_SUCCESS) { + wolfSSHD_ConfigSavePID(conf); if (wolfSSHD_AuthReducePermissions(auth) != WS_SUCCESS) { wolfSSH_Log(WS_LOG_INFO, "[SSHD] Error lowering permissions level"); ret = WS_FATAL_ERROR; } } - if (ret == WS_SUCCESS) { + if (ret == WS_SUCCESS && !testMode) { wolfSSH_Log(WS_LOG_INFO, "[SSHD] Starting to listen on port %d", port); tcp_listen(&listenFd, &port, 1); wolfSSH_Log(WS_LOG_INFO, "[SSHD] Listening on port %d", port);