Skip to content

Commit

Permalink
fix ups for format, spelling, includes, and string compare
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed Aug 30, 2023
1 parent 4fd4d04 commit 034ed7f
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 185 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ client.plist
*.sdf
*.v11.suo
*.vcxproj.filters
*.vcxproj.user
*.opensdf
*.pdb
.vs
Expand Down
33 changes: 20 additions & 13 deletions apps/wolfsshd/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#ifdef WOLFSSL_USER_SETTINGS
#include <wolfssl/wolfcrypt/settings.h>
#else
#include <wolfssl/options.h>
#endif

#ifdef WOLFSSH_SSHD
Expand Down Expand Up @@ -522,8 +524,8 @@ static int SearchForPubKey(const char* path, const WS_UserAuthData_PublicKey* pu
}
}
while (ret == WSSHD_AUTH_SUCCESS &&
(current = XFGETS(lineBuf, MAX_LINE_SZ, f)) != NULL) {
currentSz = (word32)XSTRLEN(current);
(current = WFGETS(lineBuf, MAX_LINE_SZ, f)) != NULL) {
currentSz = (word32)WSTRLEN(current);

/* remove leading spaces */
while (currentSz > 0 && current[0] == ' ') {
Expand Down Expand Up @@ -551,7 +553,7 @@ static int SearchForPubKey(const char* path, const WS_UserAuthData_PublicKey* pu
}
}

if (f != XBADFILE) {
if (f != WBADFILE) {
WFCLOSE(f);
}

Expand Down Expand Up @@ -977,15 +979,15 @@ static int CheckPublicKeyWIN(const char* usr,
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Windows check public key");

ret = SetupUserTokenWin(usr, pubKeyCtx,usrCaKeysFile, authCtx);

/* after successful logon check the public key sent */
if (ret == WSSHD_AUTH_SUCCESS) {
WCHAR h[MAX_PATH];

if (_GetHomeDirectory(authCtx, usr, h, MAX_PATH) == WS_SUCCESS) {
CHAR r[MAX_PATH];
size_t rSz;

if (wcstombs_s(&rSz, r, MAX_PATH, h, MAX_PATH - 1) != 0) {
ret = WSSHD_AUTH_FAILURE;
}
Expand All @@ -1002,8 +1004,8 @@ static int CheckPublicKeyWIN(const char* usr,
}
}
else {
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Windows failed to get home directory for user %s",
usr);
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Windows failed to get home directory for user %s", usr);
ret = WSSHD_AUTH_FAILURE;
}
}
Expand Down Expand Up @@ -1088,7 +1090,7 @@ static int RequestAuthentication(WS_UserAuthData* authData,
}
else {
rc = authCtx->checkPasswordCb(usr, authData->sf.password.password,
authData->sf.password.passwordSz, authCtx);
authData->sf.password.passwordSz, authCtx);
if (rc == WSSHD_AUTH_SUCCESS) {
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Password ok.");
}
Expand Down Expand Up @@ -1195,7 +1197,8 @@ static int RequestAuthentication(WS_UserAuthData* authData,
ret = WOLFSSH_USERAUTH_SUCCESS;
}
else {
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error getting users token.");
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Error getting users token.");
ret = WOLFSSH_USERAUTH_FAILURE;
}
#else
Expand All @@ -1205,17 +1208,20 @@ static int RequestAuthentication(WS_UserAuthData* authData,
else {
/* if not a certificate then parse through authorized key file */
rc = authCtx->checkPublicKeyCb(usr, &authData->sf.publicKey,
wolfSSHD_ConfigGetUserCAKeysFile(authCtx->conf), authCtx);
wolfSSHD_ConfigGetUserCAKeysFile(authCtx->conf),
authCtx);
if (rc == WSSHD_AUTH_SUCCESS) {
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Public key ok.");
ret = WOLFSSH_USERAUTH_SUCCESS;
}
else if (rc == WSSHD_AUTH_FAILURE) {
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Public key not authorized.");
wolfSSH_Log(WS_LOG_INFO,
"[SSHD] Public key not authorized.");
ret = WOLFSSH_USERAUTH_INVALID_PUBLICKEY;
}
else {
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error checking public key.");
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Error checking public key.");
ret = WOLFSSH_USERAUTH_FAILURE;
}
}
Expand Down Expand Up @@ -1403,7 +1409,8 @@ WOLFSSHD_AUTH* wolfSSHD_AuthCreateUser(void* heap, const WOLFSSHD_CONFIG* conf)
/* set the default user checking based on build */
ret = SetDefaultUserCheck(auth);
if (ret != WS_SUCCESS) {
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error setting default user check.");
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Error setting default user check.");
}

/* set the default password checking based on build */
Expand Down
2 changes: 2 additions & 0 deletions apps/wolfsshd/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#ifdef WOLFSSL_USER_SETTINGS
#include <wolfssl/wolfcrypt/settings.h>
#else
#include <wolfssl/options.h>
#endif

#ifdef WOLFSSH_SSHD
Expand Down
75 changes: 47 additions & 28 deletions apps/wolfsshd/wolfsshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#ifdef WOLFSSL_USER_SETTINGS
#include <wolfssl/wolfcrypt/settings.h>
#else
#include <wolfssl/options.h>
#endif

#ifdef WOLFSSH_SSHD
Expand Down Expand Up @@ -152,12 +154,13 @@ static void SyslogCb(enum wolfSSH_LogLevel level, const char *const msgStr)
static void ServiceDebugCb(enum wolfSSH_LogLevel level, const char* const msgStr)
{
WCHAR* wc;
size_t szWord = WSTRLEN(msgStr) + 3; /* + 3 for null terminator and new line */
size_t szWord = WSTRLEN(msgStr) + 3; /* + 3 for null terminator and new
* line */
size_t sz = szWord *sizeof(wchar_t);
wc = (WCHAR*)WMALLOC(sz, NULL, DYNAMIC_TYPE_LOG);
if (wc) {
size_t con;

if (mbstowcs_s(&con, wc, szWord, msgStr, szWord-1) == 0) {
wc[con - 1] = L'\r';
wc[con] = L'\n';
Expand Down Expand Up @@ -499,7 +502,8 @@ static int SCP_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
#ifndef _WIN32
/* temporarily elevate permissions to get users information */
if (wolfSSHD_AuthRaisePermissions(conn->auth) != WS_SUCCESS) {
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Failure to raise permissions for auth");
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Failure to raise permissions for auth");
return WS_FATAL_ERROR;
}

Expand Down Expand Up @@ -592,7 +596,8 @@ static int SFTP_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
#ifndef _WIN32
/* temporarily elevate permissions to get users information */
if (wolfSSHD_AuthRaisePermissions(conn->auth) != WS_SUCCESS) {
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Failure to raise permissions for auth");
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Failure to raise permissions for auth");
return WS_FATAL_ERROR;
}

Expand Down Expand Up @@ -661,7 +666,8 @@ static int SFTP_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,

if (ret == WS_SUCCESS) {
r[rSz] = '\0';
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Using directory %s for SFTP connection", r);
wolfSSH_Log(WS_LOG_INFO,
"[SSHD] Using directory %s for SFTP connection", r);
if (wolfSSH_SFTP_SetDefaultPath(ssh, r) != WS_SUCCESS) {
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Error setting SFTP default home path");
Expand Down Expand Up @@ -816,7 +822,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
/* @TODO check for conpty support LoadLibrary()and GetProcAddress(). */


if (forcedCmd != NULL && XSTRCMP(forcedCmd, "internal-sftp") == 0) {
if (forcedCmd != NULL && WSTRCMP(forcedCmd, "internal-sftp") == 0) {
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Only SFTP connections allowed for user "
"%s", wolfSSH_GetUsername(ssh));
Expand All @@ -841,11 +847,12 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
ret = WS_MEMORY_E;
}
else {
WCHAR* tmp = (WCHAR*)WMALLOC(sizeof(wchar_t) * cmdSz, NULL, DYNTYPE_SSHD);
WCHAR* tmp = (WCHAR*)WMALLOC(sizeof(wchar_t) * cmdSz, NULL,
DYNTYPE_SSHD);
if (tmp == NULL) {
ret = WS_MEMORY_E;
}

if (ret == WS_SUCCESS) {
size_t wr = 0;
if (mbstowcs_s(&wr, tmp, cmdSz, forcedCmd, cmdSz - 1) != 0) {
Expand Down Expand Up @@ -931,7 +938,8 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
if (ret == WS_SUCCESS) {
/* Using HeapAlloc for better support when possibly passing
memory between Windows Modules */
ext.lpAttributeList = (PPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, sz);
ext.lpAttributeList =
(PPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, sz);
if (ext.lpAttributeList == NULL) {
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Issue getting memory for attribute list");
Expand All @@ -940,16 +948,18 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
}

if (ret == WS_SUCCESS) {
if (InitializeProcThreadAttributeList(ext.lpAttributeList, 1, 0, &sz) != TRUE) {
if (InitializeProcThreadAttributeList(ext.lpAttributeList, 1, 0,
&sz) != TRUE) {
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Issue initializing proc thread attribute");
ret = WS_FATAL_ERROR;
}
}

if (ret == WS_SUCCESS) {
if (UpdateProcThreadAttribute(ext.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
pCon, sizeof(HPCON), NULL, NULL) != TRUE) {
if (UpdateProcThreadAttribute(ext.lpAttributeList, 0,
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
pCon, sizeof(HPCON), NULL, NULL) != TRUE) {
wolfSSH_Log(WS_LOG_ERROR,
"[SSHD] Issue updating proc thread attribute");
ret = WS_FATAL_ERROR;
Expand All @@ -969,8 +979,8 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
}
#else
/* Needs enabled when running as non-service, compiled out for now to
* make sure it can not accediently be used since the permissions of the
* created process match the current process. */
* make sure it can not accidentally be used since the permissions of
* the created process match the current process. */
if (CreateProcessW(NULL, cmd, NULL, NULL, FALSE,
EXTENDED_STARTUPINFO_PRESENT, NULL, h, &ext.StartupInfo, &processInfo)
!= TRUE) {
Expand Down Expand Up @@ -1003,8 +1013,8 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
FD_ZERO(&readFds);
FD_SET(sshFd, &readFds);

wolfSSH_Log(WS_LOG_INFO,
"[SSHD] Successfully created process for console, waiting for it to start");
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Successfully created process for "
"console, waiting for it to start");

WaitForInputIdle(processInfo.hProcess, 1000);

Expand All @@ -1018,14 +1028,19 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,

if (readPending == 0) {
/* check if process is still running before waiting to read */
if (GetExitCodeProcess(processInfo.hProcess, &processState) == TRUE) {
if (GetExitCodeProcess(processInfo.hProcess, &processState)
== TRUE) {
if (processState != STILL_ACTIVE) {
wolfSSH_Log(WS_LOG_INFO,
"[SSHD] Process has exited, exit state = %d, close down SSH connection", processState);
Sleep(100); /* give the stdout/stderr of process a little time to write to pipe */
if (PeekNamedPipe(ptyOut, NULL, 0, NULL, &ava, NULL) == TRUE) {
"[SSHD] Process has exited, exit state = %d, "
"close down SSH connection", processState);
Sleep(100); /* give the stdout/stderr of process a
* little time to write to pipe */
if (PeekNamedPipe(ptyOut, NULL, 0, NULL, &ava, NULL)
== TRUE) {
if (ava > 0) {
/* if data still pending then continue sneding it over SSH */
/* if data still pending then continue
* sending it over SSH */
readPending = 1;
continue;
}
Expand All @@ -1041,7 +1056,8 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
break;
}
/* when select times out and no socket is set as ready
Windows overwrites readFds with 0. Reset the fd here for next select call */
Windows overwrites readFds with 0. Reset the fd here
for next select call */
if (rc == 0) {
FD_SET(sshFd, &readFds);
}
Expand All @@ -1065,15 +1081,17 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
rc = wolfSSH_get_error(ssh);
if (rc == WS_CHAN_RXD) {
if (lastChannel == shellChannelId) {
cnt_r = wolfSSH_ChannelIdRead(ssh, shellChannelId,
shellBuffer,
cnt_r = wolfSSH_ChannelIdRead(ssh,
shellChannelId, shellBuffer,
sizeof shellBuffer);
if (cnt_r <= 0)
break;
pending = 0;
if (WriteFile(ptyIn, shellBuffer, cnt_r, &cnt_r, NULL) != TRUE) {
if (WriteFile(ptyIn, shellBuffer, cnt_r, &cnt_r,
NULL) != TRUE) {
wolfSSH_Log(WS_LOG_INFO,
"[SSHD] Error writing to pipe for console");
"[SSHD] Error writing to pipe for "
"console");
break;
}
}
Expand All @@ -1090,7 +1108,8 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
if (readPending) {
WMEMSET(shellBuffer, 0, EXAMPLE_BUFFER_SZ);

if (ReadFile(ptyOut, shellBuffer, EXAMPLE_BUFFER_SZ, &cnt_r, NULL) != TRUE) {
if (ReadFile(ptyOut, shellBuffer, EXAMPLE_BUFFER_SZ, &cnt_r,
NULL) != TRUE) {
wolfSSH_Log(WS_LOG_INFO,
"[SSHD] Error reading from pipe for console");
break;
Expand Down Expand Up @@ -1459,7 +1478,7 @@ static void* HandleConnection(void* arg)
graceTime = wolfSSHD_AuthGetGraceTime(conn->auth);
if (graceTime > 0) {
#ifdef WIN32
//@TODO SetTimer(NULL, NULL, graceTime, alarmCatch);
/* @TODO SetTimer(NULL, NULL, graceTime, alarmCatch); */
#else
signal(SIGALRM, alarmCatch);
alarm((unsigned int)graceTime);
Expand Down
8 changes: 4 additions & 4 deletions examples/sftpclient/sftpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -1339,11 +1339,11 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args)
WFREE(workingDir, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (ret == WS_SUCCESS) {
if (wolfSSH_shutdown(ssh) != WS_SUCCESS) {
int rc;
rc = wolfSSH_get_error(ssh);
int rc;
rc = wolfSSH_get_error(ssh);

if (rc != WS_SOCKET_ERROR_E && rc != WS_EOF)
printf("error with wolfSSH_shutdown()\n");
if (rc != WS_SOCKET_ERROR_E && rc != WS_EOF)
printf("error with wolfSSH_shutdown()\n");
}
}
WCLOSESOCKET(sockFd);
Expand Down
4 changes: 2 additions & 2 deletions ide/winvs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ DLL in that directory.
SSHD Service
-----------

Creating a new servie
Creating a new service
`sc.exe create wolfSSHd binpath="D:\work\wolfssh\ide\winvs\Debug\x64\wolfsshd.exe -f <sshd_config fils> -h <optionally load host key> -p <optional port number>"`

Starting wolfSSHd service run the following command in an adminstrator power shell session:
Expand All @@ -70,4 +70,4 @@ To stop the service run the following in an adminstrator power shell session:
`sc.exe stop wolfSSHd`

To delete the service run
`sc.exe delete wolfSSHd`
`sc.exe delete wolfSSHd`
19 changes: 0 additions & 19 deletions ide/winvs/api-test/api-test.vcxproj.user

This file was deleted.

Loading

0 comments on commit 034ed7f

Please sign in to comment.