Skip to content

Commit

Permalink
add PidFile and -t support
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed Jul 25, 2023
1 parent 87caf4a commit 8d9b30d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
33 changes: 31 additions & 2 deletions apps/wolfsshd/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -294,6 +296,7 @@ void wolfSSHD_ConfigFree(WOLFSSHD_CONFIG* conf)
FreeString(&current->authKeysFile, heap);
FreeString(&current->hostKeyFile, heap);
FreeString(&current->hostCertFile, heap);
FreeString(&current->pidFile, heap);

WFREE(current, heap, DYNTYPE_SSHD);
current = next;
Expand Down Expand Up @@ -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] = {
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 */
1 change: 1 addition & 0 deletions apps/wolfsshd/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 19 additions & 3 deletions apps/wolfsshd/wolfsshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,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;
Expand All @@ -1282,7 +1283,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;
Expand Down Expand Up @@ -1334,6 +1335,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;
Expand All @@ -1346,8 +1360,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 */
Expand Down Expand Up @@ -1437,13 +1452,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);
Expand Down

0 comments on commit 8d9b30d

Please sign in to comment.