1. Revise to use the porting functions.
2. Add test cases to check the config wildcards.
3. Generate test files for the wildcard test, and delete them after.
pull/445/head
John Safranek 2022-08-26 12:41:00 -07:00
parent c42f8fc2fe
commit a01d31592c
No known key found for this signature in database
GPG Key ID: 8CE817DE0D3CCB4A
4 changed files with 100 additions and 14 deletions

View File

@ -35,6 +35,7 @@
#include <wolfssh/ssh.h>
#include <wolfssh/internal.h>
#include <wolfssh/log.h>
#include <wolfssh/port.h>
#include <wolfssl/wolfcrypt/wc_port.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
@ -478,7 +479,7 @@ static int HandleInclude(WOLFSSHD_CONFIG *conf, const char *value)
(defined(__APPLE__) && defined(__MACH__))
int ret;
struct dirent *dir;
DIR *d;
WDIR d;
char *path;
char *filepath = (char*)WMALLOC(PATH_MAX, NULL, 0);
@ -504,26 +505,25 @@ static int HandleInclude(WOLFSSHD_CONFIG *conf, const char *value)
prefixLen = (int)(ptr - value);
}
d = opendir(path);
if (d) {
if (!WOPENDIR(NULL, conf->heap, &d, path)) {
word32 fileCount = 0, i, j;
char** fileNames = NULL;
/* Count up the number of files */
while ((dir = readdir(d)) != NULL) {
while ((dir = WREADDIR(&d)) != NULL) {
/* Skip sub-directories */
if (dir->d_type != DT_DIR) {
fileCount++;
}
}
rewinddir(d);
WREWINDDIR(&d);
if (fileCount > 0) {
fileNames = (char**)WMALLOC(fileCount * sizeof(char*), NULL, 0);
}
i = 0;
while ((dir = readdir(d)) != NULL && i < fileCount) {
while ((dir = WREADDIR(&d)) != NULL && i < fileCount) {
/* Skip sub-directories */
if (dir->d_type != DT_DIR) {
/* Insert in string order */
@ -557,7 +557,7 @@ static int HandleInclude(WOLFSSHD_CONFIG *conf, const char *value)
if (WSTRNCMP(fileNames[i] + WSTRLEN(fileNames[i]) -
WSTRLEN(postfix), postfix, WSTRLEN(postfix))
== 0) {
snprintf(filepath, PATH_MAX, "%s/%s", path,
WSNPRINTF(filepath, PATH_MAX, "%s/%s", path,
fileNames[i]);
}
else {
@ -566,19 +566,19 @@ static int HandleInclude(WOLFSSHD_CONFIG *conf, const char *value)
}
}
else {
snprintf(filepath, PATH_MAX, "%s/%s", path,
WSNPRINTF(filepath, PATH_MAX, "%s/%s", path,
fileNames[i]);
}
ret = wolfSSHD_ConfigLoad(conf, filepath);
if (ret != WS_SUCCESS) {
closedir(d);
WCLOSEDIR(&d);
WFREE(fileNames, NULL, 0);
WFREE(filepath, NULL, 0);
return ret;
}
}
WFREE(fileNames, NULL, 0);
closedir(d);
WCLOSEDIR(&d);
}
else {
/* Bad directory */
@ -759,7 +759,7 @@ int wolfSSHD_ConfigLoad(WOLFSSHD_CONFIG* conf, const char* filename)
f = XFOPEN(filename, "rb");
if (f == XBADFILE) {
wolfSSH_Log(WS_LOG_ERROR, "Unable to open SSHD config file %s\n",
wolfSSH_Log(WS_LOG_ERROR, "Unable to open SSHD config file %s",
filename);
return BAD_FUNC_ARG;
}

View File

@ -15,7 +15,7 @@ apps_wolfsshd_test_test_configuration_SOURCES = apps/wolfsshd/test/test_configur
apps/wolfsshd/auth.c
apps_wolfsshd_test_test_configuration_LDADD = src/libwolfssh.la
apps_wolfsshd_test_test_configuration_DEPENDENCIES = src/libwolfssh.la
apps_wolfsshd_test_test_configuration_CPPFLAGS = -DWOLFSSH_SSHD -DWOLFSSHD_UNIT_TEST -Iapps/wolfsshd/
apps_wolfsshd_test_test_configuration_CPPFLAGS = $(AM_CPPFLAGS) -DWOLFSSH_SSHD -DWOLFSSHD_UNIT_TEST -Iapps/wolfsshd/
DISTCLEANFILES+= apps/wolfsshd/.libs/wolfsshd \
apps/wolfsshd/test/.libs/test_configuration

View File

@ -26,6 +26,76 @@ void Log(const char *const fmt, ...)
va_end(vlist);
}
static void CleanupWildcardTest(void)
{
WDIR dir;
struct dirent* d;
char filepath[MAX_PATH*2]; /* d_name is max_path long */
if (!WOPENDIR(NULL, NULL, &dir, "./sshd_config.d/")) {
while ((d = WREADDIR(&dir)) != NULL) {
if (d->d_type != DT_DIR) {
WSNPRINTF(filepath, sizeof filepath, "%s%s",
"./sshd_config.d/", d->d_name);
WREMOVE(0, filepath);
}
}
WCLOSEDIR(&dir);
WRMDIR(0, "./sshd_config.d/");
}
}
static int SetupWildcardTest(void)
{
WFILE* f;
const byte fileIds[] = { 0, 1, 50, 59, 99 };
word32 fileIdsSz = (word32)(sizeof(fileIds) / sizeof(byte));
word32 i;
int ret;
char filepath[MAX_PATH];
ret = WMKDIR(0, "./sshd_config.d/", 0755);
if (ret == 0) {
for (i = 0; i < fileIdsSz; i++) {
if (fileIds[i] != 0) {
WSNPRINTF(filepath, sizeof filepath, "%s%02u-test.conf",
"./sshd_config.d/", fileIds[i]);
}
else {
WSNPRINTF(filepath, sizeof filepath, "%stest.bad",
"./sshd_config.d/");
}
WFOPEN(&f, filepath, "w");
if (f) {
word32 sz, wr;
char contents[20];
WSNPRINTF(contents, sizeof contents, "LoginGraceTime %02u",
fileIds[i]);
sz = (word32)WSTRLEN(contents);
wr = (word32)WFWRITE(contents, sizeof(char), sz, f);
WFCLOSE(f);
if (sz != wr) {
Log("Couldn't write the contents of file %s\n", filepath);
ret = -1;
break;
}
}
else {
Log("Couldn't create the file %s\n", filepath);
ret = -1;
break;
}
}
}
else {
Log("Couldn't make the test config directory\n");
}
return ret;
}
typedef int (*TEST_FUNC)(void);
typedef struct {
const char *name;
@ -110,6 +180,13 @@ static int test_ParseConfigLine(void)
{"Password auth no", "PasswordAuthentication no", 0},
{"Password auth yes", "PasswordAuthentication yes", 0},
{"Password auth invalid", "PasswordAuthentication wolfsshd", 1},
/* Include files tests. */
{"Include file bad", "Include sshd_config.d/test.bad", 1},
{"Include file exists", "Include sshd_config.d/01-test.conf", 0},
{"Include file DNE", "Include sshd_config.d/test-dne.conf", 1},
{"Include wildcard exists", "Include sshd_config.d/*.conf", 0},
{"Include wildcard NDE", "Include sshd_config.d/*.dne", 0},
};
const int numVectors = (int)(sizeof(vectors) / sizeof(*vectors));
@ -153,6 +230,12 @@ int main(int argc, char** argv)
(void)argc;
(void)argv;
CleanupWildcardTest();
ret = SetupWildcardTest();
if (ret != 0) {
return 1;
}
for (i = 0; i < TEST_CASE_CNT; ++i) {
ret = RunTest(&testCases[i]);
if (ret != WS_SUCCESS) {
@ -160,5 +243,7 @@ int main(int argc, char** argv)
}
}
CleanupWildcardTest();
return ret;
}

View File

@ -1217,8 +1217,9 @@ extern "C" {
/* returns 0 on success */
#define WOPENDIR(fs,h,c,d) ((*(c) = opendir((d))) == NULL)
#define WCLOSEDIR(d) closedir(*(d))
#define WREADDIR(d) readdir(*(d))
#define WCLOSEDIR(d) closedir(*(d))
#define WREADDIR(d) readdir(*(d))
#define WREWINDDIR(d) rewinddir(*(d))
#endif /* NO_WOLFSSH_DIR */
#endif
#endif /* WOLFSSH_SFTP or WOLFSSH_SCP */