mirror of https://github.com/wolfSSL/wolfssh.git
Merge pull request #435 from JacobBarthelmeh/sshd
Alpha version of SSHD support with wolfSSHpull/439/head
commit
511a022833
|
|
@ -23,7 +23,9 @@
|
|||
aclocal.m4
|
||||
lt*.m4
|
||||
*.cache
|
||||
config.*
|
||||
config.h*
|
||||
configure~
|
||||
config.status
|
||||
configure
|
||||
libtool
|
||||
libtool.m4
|
||||
|
|
@ -54,6 +56,9 @@ examples/portfwd/portfwd
|
|||
examples/sftpclient/wolfsftp
|
||||
examples/scpclient/wolfscp
|
||||
|
||||
# applications
|
||||
apps/wolfsshd/wolfsshd
|
||||
|
||||
# test output
|
||||
tests/*.test
|
||||
*.trs
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ EXTRA_DIST+= LICENSING README.md ChangeLog.md
|
|||
|
||||
include src/include.am
|
||||
include wolfssh/include.am
|
||||
include apps/wolfsshd/include.am
|
||||
include examples/include.am
|
||||
include tests/include.am
|
||||
include keys/include.am
|
||||
|
|
|
|||
|
|
@ -53,6 +53,9 @@ to help with large file transfers. By default channels are set to handle
|
|||
window size for new channels would be as follows
|
||||
`./configure CPPFLAGS=-DDEFAULT_WINDOW_SZ=16384`
|
||||
|
||||
For 32bit Linux platforms you can add support for files > 2GB by compling
|
||||
with `CFLAGS=-D_FILE_OFFSET_BITS=64`.
|
||||
|
||||
examples
|
||||
--------
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,920 @@
|
|||
/* auth.c
|
||||
*
|
||||
* Copyright (C) 2014-2022 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSH.
|
||||
*
|
||||
* wolfSSH is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSH is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSH_SSHD
|
||||
|
||||
#ifdef __linux__
|
||||
#define _XOPEN_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
|
||||
#include <wolfssh/ssh.h>
|
||||
#include <wolfssh/internal.h>
|
||||
#include <wolfssh/log.h>
|
||||
#include <wolfssl/wolfcrypt/wc_port.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
#include <wolfssl/wolfcrypt/coding.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssh/misc.h>
|
||||
#else
|
||||
#define WOLFSSH_MISC_INCLUDED
|
||||
#include "src/misc.c"
|
||||
#endif
|
||||
|
||||
#include "configuration.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <sys/types.h>
|
||||
#include <pwd.h>
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32) && !(defined(__OSX__) || defined(__APPLE__))
|
||||
#include <shadow.h>
|
||||
#define HAVE_SHADOW
|
||||
#endif
|
||||
|
||||
struct WOLFSSHD_AUTH {
|
||||
CallbackCheckUser CheckUserCb;
|
||||
CallbackCheckPassword CheckPasswordCb;
|
||||
CallbackCheckPublicKey CheckPublicKeyCb;
|
||||
const WOLFSSHD_CONFIG* conf;
|
||||
int gid;
|
||||
int uid;
|
||||
int attempts;
|
||||
void* heap;
|
||||
};
|
||||
|
||||
#ifndef WOLFSSHD_MAX_PASSWORD_ATTEMPTS
|
||||
#define WOLFSSHD_MAX_PASSWORD_ATTEMPTS 3
|
||||
#endif
|
||||
|
||||
#ifndef MAX_LEN_SZ
|
||||
#define MAX_LINE_SZ 500
|
||||
#endif
|
||||
#ifndef MAX_PATH_SZ
|
||||
#define MAX_PATH_SZ 80
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
/* this could potentially be useful in a deeply embeded future port */
|
||||
|
||||
/* Map user names to passwords */
|
||||
/* Use arrays for username and p. The password or public key can
|
||||
* be hashed and the hash stored here. Then I won't need the type. */
|
||||
struct USER_NODE {
|
||||
byte type;
|
||||
byte username[32];
|
||||
word32 usernameSz;
|
||||
byte fingerprint[WC_SHA256_DIGEST_SIZE];
|
||||
struct USER_NODE* next;
|
||||
};
|
||||
|
||||
|
||||
/* Takes a users input and adds it to the list of accepted users
|
||||
* 'value' can be a users password / public key / or certificate
|
||||
* returns an updated list on success (i.e. 'new' -> 'list' -> ...) or NULL
|
||||
* on failure
|
||||
*/
|
||||
USER_NODE* AddNewUser(USER_NODE* list, byte type, const byte* username,
|
||||
word32 usernameSz, const byte* value, word32 valueSz)
|
||||
{
|
||||
USER_NODE* map;
|
||||
|
||||
map = (USER_NODE*)WMALLOC(sizeof(USER_NODE), NULL, 0);
|
||||
if (map != NULL) {
|
||||
map->type = type;
|
||||
if (usernameSz >= sizeof(map->username))
|
||||
usernameSz = sizeof(map->username) - 1;
|
||||
WMEMCPY(map->username, username, usernameSz + 1);
|
||||
map->username[usernameSz] = 0;
|
||||
map->usernameSz = usernameSz;
|
||||
|
||||
if (type != WOLFSSH_USERAUTH_NONE) {
|
||||
wc_Sha256Hash(value, valueSz, map->fingerprint);
|
||||
}
|
||||
|
||||
map->next = list;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
#endif
|
||||
|
||||
enum {
|
||||
WSSHD_AUTH_FAILURE = 0,
|
||||
WSSHD_AUTH_SUCCESS = 1
|
||||
};
|
||||
|
||||
static int CheckAuthKeysLine(char* line, word32 lineSz, const byte* key,
|
||||
word32 keySz)
|
||||
{
|
||||
int ret = WSSHD_AUTH_SUCCESS;
|
||||
char* type;
|
||||
char* keyCandBase64; /* cand == candidate */
|
||||
word32 keyCandBase64Sz;
|
||||
byte* keyCand = NULL;
|
||||
word32 keyCandSz;
|
||||
char* last;
|
||||
enum {
|
||||
NUM_ALLOWED_TYPES = 5
|
||||
};
|
||||
static const char* allowedTypes[NUM_ALLOWED_TYPES] = {
|
||||
"ssh-rsa",
|
||||
"ssh-ed25519",
|
||||
"ecdsa-sha2-nistp256",
|
||||
"ecdsa-sha2-nistp384",
|
||||
"ecdsa-sha2-nistp521"
|
||||
};
|
||||
int typeOk = 0;
|
||||
int i;
|
||||
|
||||
if (line == NULL || lineSz == 0 || key == NULL || keySz == 0) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WSSHD_AUTH_SUCCESS) {
|
||||
if ((type = WSTRTOK(line, " ", &last)) == NULL) {
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
else if ((keyCandBase64 = WSTRTOK(NULL, " ", &last)) == NULL) {
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
if (ret == WSSHD_AUTH_SUCCESS) {
|
||||
for (i = 0; i < NUM_ALLOWED_TYPES; ++i) {
|
||||
if (WSTRCMP(type, allowedTypes[i]) == 0) {
|
||||
typeOk = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!typeOk) {
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
if (ret == WSSHD_AUTH_SUCCESS) {
|
||||
keyCandBase64Sz = (word32)XSTRLEN(keyCandBase64);
|
||||
keyCandSz = (keyCandBase64Sz * 3 + 3) / 4;
|
||||
keyCand = (byte*)WMALLOC(keyCandSz, NULL, DYNTYPE_BUFFER);
|
||||
if (keyCand == NULL) {
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
else {
|
||||
if (Base64_Decode((byte*)keyCandBase64, keyCandBase64Sz, keyCand,
|
||||
&keyCandSz) != 0) {
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret == WSSHD_AUTH_SUCCESS) {
|
||||
if (keyCandSz != keySz || WMEMCMP(key, keyCand, keySz) != 0) {
|
||||
ret = WSSHD_AUTH_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (keyCand != NULL) {
|
||||
WFREE(keyCand, NULL, DYNTYPE_BUFFER);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
#ifdef WOLFSSH_USE_PAM
|
||||
static int CheckPasswordPAM(const char* usr, const byte* pw, word32 pwSz)
|
||||
{
|
||||
(void)usr;
|
||||
(void)pw;
|
||||
(void)pwSz;
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
|
||||
#if 0
|
||||
static int ExtractSalt(char* hash, char** salt, int saltSz)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
int idx = 0;
|
||||
char* p;
|
||||
|
||||
if (hash == NULL || salt == NULL || *salt == NULL || saltSz <= 0) {
|
||||
ret = WS_SUCCESS;
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
if (hash[idx] != '$') {
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
else {
|
||||
++idx;
|
||||
if (idx >= saltSz) {
|
||||
ret = WS_BUFFER_E;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret == 0) {
|
||||
p = strstr(hash + idx, "$");
|
||||
if (p == NULL) {
|
||||
ret = -1;
|
||||
}
|
||||
else {
|
||||
idx += (p - hash);
|
||||
if (idx >= saltSz) {
|
||||
ret = WS_BUFFER_E;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret == 0) {
|
||||
p = strstr(p + 1, "$");
|
||||
if (p == NULL) {
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
else {
|
||||
idx += (p - (hash + idx) + 1);
|
||||
if (idx >= saltSz) {
|
||||
ret = WS_BUFFER_E;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ret == 0) {
|
||||
memcpy(*salt, hash, idx);
|
||||
(*salt)[idx] = 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(WOLFSSH_HAVE_LIBCRYPT) || defined(WOLFSSH_HAVE_LIBLOGIN)
|
||||
static int CheckPasswordHashUnix(const char* input, char* stored)
|
||||
{
|
||||
int ret = WSSHD_AUTH_SUCCESS;
|
||||
char* hashedInput;
|
||||
|
||||
if (input == NULL || stored == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WSSHD_AUTH_SUCCESS) {
|
||||
hashedInput = crypt(input, stored);
|
||||
if (hashedInput == NULL) {
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
else {
|
||||
if (WMEMCMP(hashedInput, stored, WSTRLEN(stored)) != 0) {
|
||||
ret = WSSHD_AUTH_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* WOLFSSH_HAVE_LIBCRYPT || WOLFSSH_HAVE_LIBLOGIN */
|
||||
|
||||
static int CheckPasswordUnix(const char* usr, const byte* pw, word32 pwSz)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
char* pwStr = NULL;
|
||||
struct passwd* pwInfo;
|
||||
#ifdef HAVE_SHADOW
|
||||
struct spwd* shadowInfo;
|
||||
#endif
|
||||
/* The hash of the user's password stored on the system. */
|
||||
char* storedHash;
|
||||
char* storedHashCpy = NULL;
|
||||
|
||||
if (usr == NULL || pw == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
pwStr = (char*)WMALLOC(pwSz + 1, NULL, DYNTYPE_STRING);
|
||||
if (pwStr == NULL) {
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
else {
|
||||
XMEMCPY(pwStr, pw, pwSz);
|
||||
pwStr[pwSz] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
pwInfo = getpwnam((const char*)usr);
|
||||
if (pwInfo == NULL) {
|
||||
/* user name not found on system */
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
#ifdef HAVE_SHADOW
|
||||
if (pwInfo->pw_passwd[0] == 'x') {
|
||||
#ifdef WOLFSSH_HAVE_LIBCRYPT
|
||||
shadowInfo = getspnam((const char*)usr);
|
||||
#else
|
||||
shadowInfo = getspnam((char*)usr);
|
||||
#endif
|
||||
if (shadowInfo == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Error getting user password info");
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Possibly permisions level error?"
|
||||
" i.e SSHD not ran as sudo");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
else {
|
||||
storedHash = shadowInfo->sp_pwdp;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
storedHash = pwInfo->pw_passwd;
|
||||
}
|
||||
}
|
||||
if (ret == WS_SUCCESS) {
|
||||
storedHashCpy = WSTRDUP(storedHash, NULL, DYNTYPE_STRING);
|
||||
if (storedHash == NULL) {
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
#if defined(WOLFSSH_HAVE_LIBCRYPT) || defined(WOLFSSH_HAVE_LIBLOGIN)
|
||||
ret = CheckPasswordHashUnix(pwStr, storedHashCpy);
|
||||
#else
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] No compiled in password check");
|
||||
ret = WS_NOT_COMPILED;
|
||||
#endif
|
||||
}
|
||||
|
||||
if (pwStr != NULL) {
|
||||
WFREE(pwStr, NULL, DYNTYPE_STRING);
|
||||
}
|
||||
if (storedHashCpy != NULL) {
|
||||
WFREE(storedHashCpy, NULL, DYNTYPE_STRING);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* WOLFSSH_USE_PAM */
|
||||
#endif /* !_WIN32 */
|
||||
|
||||
#ifndef _WIN32
|
||||
static int CheckUserUnix(const char* name) {
|
||||
int ret = WSSHD_AUTH_FAILURE;
|
||||
struct passwd* pwInfo;
|
||||
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Unix check user");
|
||||
errno = 0;
|
||||
pwInfo = getpwnam(name);
|
||||
if (pwInfo == NULL) {
|
||||
if (errno != 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error calling getpwnam for user "
|
||||
"%s.", name);
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret = WSSHD_AUTH_SUCCESS;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const char authKeysDefault[] = ".ssh/authorized_keys";
|
||||
static char authKeysPattern[MAX_PATH_SZ] = {0};
|
||||
|
||||
void SetAuthKeysPattern(const char* pattern)
|
||||
{
|
||||
if (pattern != NULL) {
|
||||
WMEMSET(authKeysPattern, 0, sizeof(authKeysPattern));
|
||||
WSTRNCPY(authKeysPattern, pattern, sizeof(authKeysPattern) - 1);
|
||||
}
|
||||
}
|
||||
|
||||
static int ResolveAuthKeysPath(const char* homeDir, char* resolved)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
char* idx;
|
||||
int homeDirSz;
|
||||
const char* suffix = authKeysDefault;
|
||||
|
||||
if (homeDir == NULL || resolved == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (*authKeysPattern != 0) {
|
||||
/* TODO: token substitutions (e.g. %h) */
|
||||
if (*authKeysPattern == '/') {
|
||||
WSTRNCPY(resolved, authKeysPattern, MAX_PATH_SZ);
|
||||
return WS_SUCCESS;
|
||||
}
|
||||
else {
|
||||
suffix = authKeysPattern;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
idx = resolved;
|
||||
homeDirSz = (int)XSTRLEN(homeDir);
|
||||
if (homeDirSz + 1 + WSTRLEN(suffix) >= MAX_PATH_SZ) {
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Path for key file larger than max allowed");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
XMEMCPY(idx, homeDir, homeDirSz);
|
||||
idx += homeDirSz;
|
||||
*(idx++) = '/';
|
||||
/* Intentionally copying the null term from suffix. */
|
||||
XMEMCPY(idx, suffix, WSTRLEN(suffix));
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int CheckPublicKeyUnix(const char* name, const byte* key, word32 keySz)
|
||||
{
|
||||
int ret = WSSHD_AUTH_SUCCESS;
|
||||
int rc;
|
||||
struct passwd* pwInfo;
|
||||
char* authKeysFile = NULL;
|
||||
XFILE f = NULL;
|
||||
char* lineBuf = NULL;
|
||||
char* current;
|
||||
word32 currentSz;
|
||||
int foundKey = 0;
|
||||
char authKeysPath[MAX_PATH_SZ];
|
||||
|
||||
errno = 0;
|
||||
pwInfo = getpwnam((const char*)name);
|
||||
if (pwInfo == NULL) {
|
||||
if (errno != 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error calling getpwnam for user "
|
||||
"%s.", name);
|
||||
}
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
if (ret == WSSHD_AUTH_SUCCESS) {
|
||||
WMEMSET(authKeysPath, 0, sizeof(authKeysPath));
|
||||
rc = ResolveAuthKeysPath(pwInfo->pw_dir, authKeysPath);
|
||||
if (rc != WS_SUCCESS) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Failed to resolve authorized keys"
|
||||
" file path.");
|
||||
ret = rc;
|
||||
}
|
||||
}
|
||||
if (ret == WSSHD_AUTH_SUCCESS) {
|
||||
f = XFOPEN(authKeysPath, "rb");
|
||||
if (f == XBADFILE) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Unable to open %s",
|
||||
authKeysPath);
|
||||
ret = WS_BAD_FILE_E;
|
||||
}
|
||||
}
|
||||
if (ret == WSSHD_AUTH_SUCCESS) {
|
||||
lineBuf = (char*)WMALLOC(MAX_LINE_SZ, NULL, DYNTYPE_BUFFER);
|
||||
if (lineBuf == NULL) {
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
}
|
||||
while (ret == WSSHD_AUTH_SUCCESS &&
|
||||
(current = XFGETS(lineBuf, MAX_LINE_SZ, f)) != NULL) {
|
||||
currentSz = (word32)XSTRLEN(current);
|
||||
|
||||
/* remove leading spaces */
|
||||
while (currentSz > 0 && current[0] == ' ') {
|
||||
currentSz = currentSz - 1;
|
||||
current = current + 1;
|
||||
}
|
||||
|
||||
if (currentSz <= 1) {
|
||||
continue; /* empty line */
|
||||
}
|
||||
|
||||
if (current[0] == '#') {
|
||||
continue; /* commented out line */
|
||||
}
|
||||
|
||||
rc = CheckAuthKeysLine(current, currentSz, key, keySz);
|
||||
if (rc == WSSHD_AUTH_SUCCESS) {
|
||||
foundKey = 1;
|
||||
break;
|
||||
}
|
||||
else if (rc < 0) {
|
||||
ret = rc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (f != XBADFILE) {
|
||||
XFCLOSE(f);
|
||||
}
|
||||
|
||||
if (ret == WSSHD_AUTH_SUCCESS && !foundKey) {
|
||||
ret = WSSHD_AUTH_FAILURE;
|
||||
}
|
||||
|
||||
if (lineBuf != NULL) {
|
||||
WFREE(lineBuf, NULL, DYNTYPE_BUFFER);
|
||||
}
|
||||
if (authKeysFile != NULL) {
|
||||
WFREE(authKeysFile, NULL, DYNTYPE_STRING);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* !_WIN32*/
|
||||
|
||||
|
||||
/* return WOLFSSH_USERAUTH_SUCCESS on success */
|
||||
static int DoCheckUser(const char* usr, WOLFSSHD_AUTH* auth)
|
||||
{
|
||||
int ret = WOLFSSH_USERAUTH_FAILURE;
|
||||
int rc;
|
||||
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Checking user name %s", usr);
|
||||
rc = auth->CheckUserCb(usr);
|
||||
if (rc == WSSHD_AUTH_SUCCESS) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] User ok.");
|
||||
ret = WOLFSSH_USERAUTH_SUCCESS;
|
||||
}
|
||||
else if (ret == WSSHD_AUTH_FAILURE) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] User %s doesn't exist.", usr);
|
||||
ret = WOLFSSH_USERAUTH_INVALID_USER;
|
||||
}
|
||||
else {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error looking up user %s.", usr);
|
||||
ret = WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* @TODO this will take in a pipe or equivalent to talk to a privileged thread
|
||||
* rathar than having WOLFSSHD_AUTH directly with privilege separation */
|
||||
static int RequestAuthentication(const char* usr, int type, const byte* data,
|
||||
int dataSz, WOLFSSHD_AUTH* auth)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (auth == NULL)
|
||||
return WOLFSSH_USERAUTH_FAILURE;
|
||||
|
||||
if (wolfSSHD_ConfigGetPermitRoot(auth->conf) == 0) {
|
||||
if (XSTRCMP(usr, "root") == 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Login as root not permited");
|
||||
return WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
ret = DoCheckUser(usr, auth);
|
||||
/* temporarily elevate permissions */
|
||||
if (ret == WOLFSSH_USERAUTH_SUCCESS &&
|
||||
wolfSSHD_AuthRaisePermissions(auth) != WS_SUCCESS) {
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Failure to raise permissions for auth");
|
||||
ret = WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
|
||||
if (ret == WOLFSSH_USERAUTH_SUCCESS && type == WOLFSSH_USERAUTH_PASSWORD) {
|
||||
int rc;
|
||||
|
||||
if (wolfSSHD_ConfigGetPwAuth(auth->conf) != 1) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Password authentication not "
|
||||
"allowed by configuration!");
|
||||
ret = WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
/* Check if password is valid for this user. */
|
||||
/* first handle empty password cases */
|
||||
else if (dataSz == 0 && wolfSSHD_ConfigGetPermitEmptyPw(auth->conf)
|
||||
!= 1) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Empty passwords not allowed by "
|
||||
"configuration!");
|
||||
ret = WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
else {
|
||||
rc = auth->CheckPasswordCb(usr, data, dataSz);
|
||||
if (rc == WSSHD_AUTH_SUCCESS) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Password ok.");
|
||||
}
|
||||
else if (rc == WSSHD_AUTH_FAILURE) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Password incorrect.");
|
||||
ret = WOLFSSH_USERAUTH_INVALID_PASSWORD;
|
||||
|
||||
auth->attempts--;
|
||||
if (auth->attempts == 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Too many bad password attempts!");
|
||||
ret = WOLFSSH_USERAUTH_REJECTED;
|
||||
}
|
||||
}
|
||||
else {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error checking password.");
|
||||
ret = WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (ret == WOLFSSH_USERAUTH_SUCCESS && type == WOLFSSH_USERAUTH_PUBLICKEY) {
|
||||
int rc;
|
||||
|
||||
rc = auth->CheckPublicKeyCb(usr, data, dataSz);
|
||||
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.");
|
||||
ret = WOLFSSH_USERAUTH_INVALID_PUBLICKEY;
|
||||
}
|
||||
else {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error checking public key.");
|
||||
ret = WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (wolfSSHD_AuthReducePermissions(auth) != WS_SUCCESS) {
|
||||
/* stop everything if not able to reduce permissions level */
|
||||
exit(1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* return WOLFSSH_USERAUTH_SUCCESS on success */
|
||||
int DefaultUserAuth(byte authType, WS_UserAuthData* authData, void* ctx)
|
||||
{
|
||||
int ret = WOLFSSH_USERAUTH_SUCCESS;
|
||||
WOLFSSHD_AUTH* auth;
|
||||
|
||||
if (ctx == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] No auth callbacks passed in");
|
||||
return WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
else {
|
||||
auth = (WOLFSSHD_AUTH*)ctx;
|
||||
if (auth->CheckUserCb == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] No way to check the user is set");
|
||||
return WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (authData == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] No authData passed in");
|
||||
return WOLFSSH_USERAUTH_FAILURE;
|
||||
}
|
||||
|
||||
if (authType != WOLFSSH_USERAUTH_PASSWORD &&
|
||||
#ifdef WOLFSSH_ALLOW_USERAUTH_NONE
|
||||
authType != WOLFSSH_USERAUTH_NONE &&
|
||||
#endif
|
||||
authType != WOLFSSH_USERAUTH_PUBLICKEY) {
|
||||
|
||||
ret = WOLFSSH_USERAUTH_INVALID_AUTHTYPE;
|
||||
}
|
||||
|
||||
/* call to possibly privilaged authentecator for password check */
|
||||
if (ret == WOLFSSH_USERAUTH_SUCCESS &&
|
||||
authData->type == WOLFSSH_USERAUTH_PASSWORD) {
|
||||
ret = RequestAuthentication((const char*)authData->username,
|
||||
authData->type,
|
||||
authData->sf.password.password,
|
||||
authData->sf.password.passwordSz, auth);
|
||||
}
|
||||
|
||||
/* call to possibly privilaged authentecator for public key check */
|
||||
if (ret == WOLFSSH_USERAUTH_SUCCESS &&
|
||||
authData->type == WOLFSSH_USERAUTH_PUBLICKEY) {
|
||||
ret = RequestAuthentication((const char*)authData->username,
|
||||
authData->type,
|
||||
authData->sf.publicKey.publicKey,
|
||||
authData->sf.publicKey.publicKeySz, auth);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int SetDefaultUserCheck(WOLFSSHD_AUTH* auth)
|
||||
{
|
||||
int ret = WS_NOT_COMPILED;
|
||||
|
||||
#ifdef _WIN32
|
||||
/* TODO: Implement for Windows. */
|
||||
#else
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Setting default Unix user name check");
|
||||
auth->CheckUserCb = CheckUserUnix;
|
||||
ret = WS_SUCCESS;
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int SetDefaultPasswordCheck(WOLFSSHD_AUTH* auth)
|
||||
{
|
||||
int ret = WS_NOT_COMPILED;
|
||||
|
||||
#ifdef _WIN32
|
||||
/* TODO: Add CheckPasswordWin. */
|
||||
#elif defined(WOLFSSH_USE_PAM)
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Setting PAM password check");
|
||||
auth->CheckPasswordCb = CheckPasswordPAM;
|
||||
ret = WS_SUCCESS;
|
||||
#else
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Setting Unix password check");
|
||||
auth->CheckPasswordCb = CheckPasswordUnix;
|
||||
ret = WS_SUCCESS;
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int SetDefaultPublicKeyCheck(WOLFSSHD_AUTH* auth)
|
||||
{
|
||||
int ret = WS_NOT_COMPILED;
|
||||
|
||||
#ifdef _WIN32
|
||||
/* TODO: Implement for Windows. */
|
||||
#else
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Setting Unix public key check");
|
||||
auth->CheckPublicKeyCb = CheckPublicKeyUnix;
|
||||
ret = WS_SUCCESS;
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* Sets the default functions to be used for authentication of peer.
|
||||
* Later the default functions could be overriden if needed.
|
||||
* returns a newly created WOLFSSHD_AUTH struct success */
|
||||
WOLFSSHD_AUTH* wolfSSHD_AuthCreateUser(void* heap, const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
WOLFSSHD_AUTH* auth;
|
||||
|
||||
auth = (WOLFSSHD_AUTH*)WMALLOC(sizeof(WOLFSSHD_AUTH), heap, DYNTYPE_SSHD);
|
||||
if (auth != NULL) {
|
||||
int ret;
|
||||
struct passwd* pwInfo;
|
||||
const char* usr = "sshd";
|
||||
|
||||
auth->heap = heap;
|
||||
auth->conf = conf;
|
||||
auth->attempts = WOLFSSHD_MAX_PASSWORD_ATTEMPTS;
|
||||
|
||||
/* 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.");
|
||||
}
|
||||
|
||||
/* set the default password checking based on build */
|
||||
if (ret == WS_SUCCESS) {
|
||||
ret = SetDefaultPasswordCheck(auth);
|
||||
if (ret != WS_SUCCESS) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error setting default "
|
||||
"password check.");
|
||||
}
|
||||
}
|
||||
|
||||
/* set the default public key checking based on build */
|
||||
if (ret == WS_SUCCESS) {
|
||||
ret = SetDefaultPublicKeyCheck(auth);
|
||||
if (ret != WS_SUCCESS) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error setting default "
|
||||
"public key check.");
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
pwInfo = getpwnam(usr);
|
||||
if (pwInfo == NULL) {
|
||||
/* user name not found on system */
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] No sshd user found to use");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
auth->gid = pwInfo->pw_gid;
|
||||
auth->uid = pwInfo->pw_uid;
|
||||
}
|
||||
|
||||
/* error case in setting one of the default callbacks */
|
||||
if (ret != WS_SUCCESS) {
|
||||
(void)wolfSSHD_AuthFreeUser(auth);
|
||||
auth = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return auth;
|
||||
}
|
||||
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
int wolfSSHD_AuthFreeUser(WOLFSSHD_AUTH* auth)
|
||||
{
|
||||
if (auth != NULL) {
|
||||
WFREE(auth, auth->heap, DYNTYPE_SSHD);
|
||||
}
|
||||
return WS_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/* return WS_SUCCESS on success */
|
||||
int wolfSSHD_AuthRaisePermissions(WOLFSSHD_AUTH* auth)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Attempting to raise permissions level");
|
||||
if (auth) {
|
||||
if (setegid(0) != 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error rasing gid");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
if (seteuid(0) != 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error rasing uid");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* return WS_SUCCESS on success */
|
||||
int wolfSSHD_AuthReducePermissions(WOLFSSHD_AUTH* auth)
|
||||
{
|
||||
byte flag = 0;
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
flag = wolfSSHD_ConfigGetPrivilegeSeparation(auth->conf);
|
||||
if (flag == WOLFSSHD_PRIV_SEPARAT || flag == WOLFSSHD_PRIV_SANDBOX) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Lowering permissions level");
|
||||
if (auth) {
|
||||
if (setegid(auth->gid) != 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error setting sshd gid");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
if (seteuid(auth->uid) != 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error setting sshd uid");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* return the time in seconds for grace timeout period */
|
||||
long wolfSSHD_AuthGetGraceTime(const WOLFSSHD_AUTH* auth)
|
||||
{
|
||||
long ret = WS_BAD_ARGUMENT;
|
||||
|
||||
if (auth != NULL && auth->conf != NULL) {
|
||||
ret = wolfSSHD_ConfigGetGraceTime(auth->conf);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* WOLFSSH_SSHD */
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/* auth.h
|
||||
*
|
||||
* Copyright (C) 2014-2022 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSH.
|
||||
*
|
||||
* wolfSSH is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSH is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef WOLFAUTH_H
|
||||
#define WOLFAUTH_H
|
||||
|
||||
#if 0
|
||||
typedef struct USER_NODE USER_NODE;
|
||||
|
||||
USER_NODE* AddNewUser(USER_NODE* list, byte type, const byte* username,
|
||||
word32 usernameSz, const byte* value, word32 valueSz);
|
||||
#endif
|
||||
|
||||
void SetAuthKeysPattern(const char* pattern);
|
||||
int DefaultUserAuth(byte authType, WS_UserAuthData* authData, void* ctx);
|
||||
|
||||
typedef struct WOLFSSHD_AUTH WOLFSSHD_AUTH;
|
||||
|
||||
/*
|
||||
* Returns WSSHD_AUTH_SUCCESS if user found, WSSHD_AUTH_FAILURE if user not
|
||||
* found, and negative values if an error occurs during checking.
|
||||
*/
|
||||
typedef int (*CallbackCheckUser)(const char* usr);
|
||||
|
||||
|
||||
/*
|
||||
* Returns WSSHD_AUTH_SUCCESS if user found, WSSHD_AUTH_FAILURE if user not
|
||||
* found, and negative values if an error occurs during checking.
|
||||
*/
|
||||
typedef int (*CallbackCheckPassword)(const char* usr, const byte* psw,
|
||||
word32 pswSz);
|
||||
|
||||
/*
|
||||
* Returns WSSHD_AUTH_SUCCESS if public key ok, WSSHD_AUTH_FAILURE if key not
|
||||
* ok, and negative values if an error occurs during checking.
|
||||
*/
|
||||
typedef int (*CallbackCheckPublicKey)(const char* usr, const byte* key,
|
||||
word32 keySz);
|
||||
|
||||
WOLFSSHD_AUTH* wolfSSHD_AuthCreateUser(void* heap, const WOLFSSHD_CONFIG* conf);
|
||||
int wolfSSHD_AuthFreeUser(WOLFSSHD_AUTH* auth);
|
||||
int wolfSSHD_AuthReducePermissions(WOLFSSHD_AUTH* auth);
|
||||
int wolfSSHD_AuthRaisePermissions(WOLFSSHD_AUTH* auth);
|
||||
long wolfSSHD_AuthGetGraceTime(const WOLFSSHD_AUTH* auth);
|
||||
#endif /* WOLFAUTH_H */
|
||||
|
|
@ -0,0 +1,909 @@
|
|||
/* configuration.c
|
||||
*
|
||||
* Copyright (C) 2014-2022 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSH.
|
||||
*
|
||||
* wolfSSH is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSH is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSH_SSHD
|
||||
/* functions for parsing out options from a config file and for handling loading
|
||||
* key/certs using the env. filesystem */
|
||||
|
||||
#ifdef WOLFSSHD_UNIT_TEST
|
||||
#define WOLFSSHD_STATIC
|
||||
#else
|
||||
#define WOLFSSHD_STATIC static
|
||||
#endif
|
||||
|
||||
#include <wolfssh/ssh.h>
|
||||
#include <wolfssh/internal.h>
|
||||
#include <wolfssh/log.h>
|
||||
#include <wolfssl/wolfcrypt/wc_port.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssh/misc.h>
|
||||
#else
|
||||
#define WOLFSSH_MISC_INCLUDED
|
||||
#include "src/misc.c"
|
||||
#endif
|
||||
|
||||
#include "configuration.h"
|
||||
|
||||
struct WOLFSSHD_CONFIG {
|
||||
void* heap;
|
||||
char* banner;
|
||||
char* chrootDir;
|
||||
char* ciphers;
|
||||
char* hostKeyFile;
|
||||
char* hostKeyAlgos;
|
||||
char* kekAlgos;
|
||||
char* listenAddress;
|
||||
char* authKeysFile;
|
||||
long loginTimer;
|
||||
word16 port;
|
||||
byte usePrivilegeSeparation;
|
||||
byte passwordAuth:1;
|
||||
byte pubKeyAuth:1;
|
||||
byte permitRootLogin:1;
|
||||
byte permitEmptyPasswords:1;
|
||||
};
|
||||
|
||||
|
||||
/* convert a string into seconds, handles if 'm' for minutes follows the string
|
||||
* number, i.e. 2m
|
||||
* Returns the value on success and negative value on failure */
|
||||
static long GetConfigInt(const char* in, int inSz, int isTime, void* heap)
|
||||
{
|
||||
long ret = 0;
|
||||
int mult = 1; /* multiplier */
|
||||
int sz = inSz;
|
||||
|
||||
/* check for multipliers */
|
||||
if (isTime) {
|
||||
if (in[sz - 1] == 'm') {
|
||||
sz--;
|
||||
mult = 60;
|
||||
}
|
||||
if (in[sz - 1] == 'h') {
|
||||
sz--;
|
||||
mult = 60*60;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
char* num = (char*)WMALLOC(sz + 1, heap, DYNTYPE_SSHD);
|
||||
if (num == NULL) {
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
else {
|
||||
WMEMCPY(num, in, sz);
|
||||
num[sz] = '\0';
|
||||
ret = atol(num);
|
||||
if (ret == 0 && WSTRCMP(in, "0") != 0) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
else if (ret > 0) {
|
||||
ret = ret * mult;
|
||||
}
|
||||
WFREE(num, heap, DYNTYPE_SSHD);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int CreateString(char** out, const char* in, int inSz, void* heap)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
int idx = 0;
|
||||
|
||||
/* remove leading white spaces */
|
||||
while (idx < inSz && in[idx] == ' ') idx++;
|
||||
|
||||
if (idx == inSz) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
/* malloc new string and set it */
|
||||
if (ret == WS_SUCCESS) {
|
||||
*out = (char*)WMALLOC((inSz - idx) + 1, heap, DYNTYPE_SSHD);
|
||||
if (*out == NULL) {
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
else {
|
||||
XMEMCPY(*out, in + idx, inSz - idx);
|
||||
*(*out + (inSz - idx)) = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void FreeString(char** in, void* heap)
|
||||
{
|
||||
if (*in != NULL) {
|
||||
WFREE(*in, heap, DYNTYPE_SSHD);
|
||||
*in = NULL;
|
||||
}
|
||||
(void)heap;
|
||||
}
|
||||
|
||||
|
||||
/* returns a new WOLFSSHD_CONFIG on success and NULL on failure */
|
||||
WOLFSSHD_CONFIG* wolfSSHD_ConfigNew(void* heap)
|
||||
{
|
||||
WOLFSSHD_CONFIG* ret;
|
||||
|
||||
ret = (WOLFSSHD_CONFIG*)WMALLOC(sizeof(WOLFSSHD_CONFIG), heap,
|
||||
DYNTYPE_SSHD);
|
||||
if (ret == NULL) {
|
||||
fprintf(stderr, "Issue malloc'ing config structure for sshd\n");
|
||||
}
|
||||
else {
|
||||
WMEMSET(ret, 0, sizeof(WOLFSSHD_CONFIG));
|
||||
|
||||
/* default values */
|
||||
ret->port = 22;
|
||||
ret->passwordAuth = 1;
|
||||
}
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
void wolfSSHD_ConfigFree(WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
void* heap;
|
||||
|
||||
if (conf != NULL) {
|
||||
heap = conf->heap;
|
||||
|
||||
FreeString(&conf->authKeysFile, heap);
|
||||
FreeString(&conf->hostKeyFile, heap);
|
||||
|
||||
WFREE(conf, heap, DYNTYPE_SSHD);
|
||||
}
|
||||
}
|
||||
|
||||
#define MAX_LINE_SIZE 160
|
||||
|
||||
typedef struct {
|
||||
int tag;
|
||||
const char* name;
|
||||
} CONFIG_OPTION;
|
||||
|
||||
enum {
|
||||
OPT_AUTH_KEYS_FILE = 0,
|
||||
OPT_PRIV_SEP = 1,
|
||||
OPT_PERMIT_EMPTY_PW = 2,
|
||||
OPT_SUBSYSTEM = 3,
|
||||
OPT_CHALLENGE_RESPONSE_AUTH = 4,
|
||||
OPT_USE_PAM = 5,
|
||||
OPT_X11_FORWARDING = 6,
|
||||
OPT_PRINT_MOTD = 7,
|
||||
OPT_ACCEPT_ENV = 8,
|
||||
OPT_PROTOCOL = 9,
|
||||
OPT_LOGIN_GRACE_TIME = 10,
|
||||
OPT_HOST_KEY = 11,
|
||||
OPT_PASSWORD_AUTH = 12,
|
||||
OPT_PORT = 13,
|
||||
OPT_PERMIT_ROOT = 14,
|
||||
OPT_USE_DNS = 15,
|
||||
OPT_INCLUDE = 16
|
||||
};
|
||||
enum {
|
||||
NUM_OPTIONS = 17
|
||||
};
|
||||
|
||||
static const CONFIG_OPTION options[NUM_OPTIONS] = {
|
||||
{OPT_AUTH_KEYS_FILE, "AuthorizedKeysFile"},
|
||||
{OPT_PRIV_SEP, "UsePrivilegeSeparation"},
|
||||
{OPT_PERMIT_EMPTY_PW, "PermitEmptyPasswords"},
|
||||
{OPT_SUBSYSTEM, "Subsystem"},
|
||||
{OPT_CHALLENGE_RESPONSE_AUTH, "ChallengeResponseAuthentication"},
|
||||
{OPT_USE_PAM, "UsePAM"},
|
||||
{OPT_X11_FORWARDING, "X11Forwarding"},
|
||||
{OPT_PRINT_MOTD, "PrintMotd"},
|
||||
{OPT_ACCEPT_ENV, "AcceptEnv"},
|
||||
{OPT_PROTOCOL, "Protocol"},
|
||||
{OPT_LOGIN_GRACE_TIME, "LoginGraceTime"},
|
||||
{OPT_HOST_KEY, "HostKey"},
|
||||
{OPT_PASSWORD_AUTH, "PasswordAuthentication"},
|
||||
{OPT_PORT, "Port"},
|
||||
{OPT_PERMIT_ROOT, "PermitRootLogin"},
|
||||
{OPT_USE_DNS, "UseDNS"},
|
||||
{OPT_INCLUDE, "Include"}
|
||||
};
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int HandlePrivSep(WOLFSSHD_CONFIG* conf, const char* value)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
if (conf == NULL || value == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (WSTRCMP(value, "sandbox") == 0) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Sandbox privilege separation");
|
||||
conf->usePrivilegeSeparation = WOLFSSHD_PRIV_SANDBOX;
|
||||
}
|
||||
else if (WSTRCMP(value, "yes") == 0) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Privilege separation enabled");
|
||||
conf->usePrivilegeSeparation = WOLFSSHD_PRIV_SEPARAT;
|
||||
}
|
||||
else if (WSTRCMP(value, "no") == 0) {
|
||||
wolfSSH_Log(WS_LOG_INFO,
|
||||
"[SSHD] Turning off privilege separation!");
|
||||
conf->usePrivilegeSeparation = WOLFSSHD_PRIV_OFF;
|
||||
}
|
||||
else {
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Unknown/supported privilege separation!");
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int HandleLoginGraceTime(WOLFSSHD_CONFIG* conf, const char* value)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
long num;
|
||||
|
||||
if (conf == NULL || value == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
num = GetConfigInt(value, (int)XSTRLEN(value), 1, conf->heap);
|
||||
if (num < 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Issue getting login grace "
|
||||
"time");
|
||||
ret = (int)num;
|
||||
}
|
||||
else {
|
||||
conf->loginTimer = num;
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Setting login grace time to "
|
||||
"%ld", num);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int HandlePermitEmptyPw(WOLFSSHD_CONFIG* conf, const char* value)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
if (conf == NULL || value == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (WSTRCMP(value, "no") == 0) {
|
||||
conf->permitEmptyPasswords = 0;
|
||||
}
|
||||
else if (WSTRCMP(value, "yes") == 0) {
|
||||
conf->permitEmptyPasswords = 1;
|
||||
}
|
||||
else {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int HandlePermitRoot(WOLFSSHD_CONFIG* conf, const char* value)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
if (conf == NULL || value == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (WSTRCMP(value, "no") == 0) {
|
||||
conf->permitRootLogin = 0;
|
||||
}
|
||||
else if (WSTRCMP(value, "yes") == 0) {
|
||||
conf->permitRootLogin = 1;
|
||||
}
|
||||
else {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int HandlePwAuth(WOLFSSHD_CONFIG* conf, const char* value)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
if (conf == NULL || value == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (WSTRCMP(value, "no") == 0) {
|
||||
conf->passwordAuth = 0;
|
||||
}
|
||||
else if (WSTRCMP(value, "yes") == 0) {
|
||||
conf->passwordAuth = 1;
|
||||
}
|
||||
else {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define WOLFSSH_PROTOCOL_VERSION 2
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int HandleProtocol(WOLFSSHD_CONFIG* conf, const char* value)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
long portInt;
|
||||
|
||||
if (conf == NULL || value == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
portInt = GetConfigInt(value, (int)WSTRLEN(value), 0, conf->heap);
|
||||
if (portInt <= 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Invalid protocol number: %s.",
|
||||
value);
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
else {
|
||||
if (portInt != WOLFSSH_PROTOCOL_VERSION) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Protocol number %ld not "
|
||||
"supported.", portInt);
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int HandlePort(WOLFSSHD_CONFIG* conf, const char* value)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
long portInt;
|
||||
|
||||
if (conf == NULL || value == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
portInt = GetConfigInt(value, (int)WSTRLEN(value), 0, conf->heap);
|
||||
if (portInt <= 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Invalid port number: %s.",
|
||||
value);
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
else {
|
||||
if (portInt <= (word16)-1) {
|
||||
conf->port = (word16)portInt;
|
||||
}
|
||||
else {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Port number %ld too big.",
|
||||
portInt);
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int HandleInclude(WOLFSSHD_CONFIG *conf, const char *value)
|
||||
{
|
||||
const char *ptr;
|
||||
const char *ptr2;
|
||||
const char *postfix = NULL;
|
||||
const char *prefix = NULL;
|
||||
int prefixLen = 0;
|
||||
int found = 0;
|
||||
|
||||
/* No value, nothing to do */
|
||||
if (!value || value[0] == '\0') {
|
||||
return WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
/* Ignore trailing whitespace */
|
||||
ptr = value + WSTRLEN(value) - 1;
|
||||
while (ptr != value) {
|
||||
if (!WISSPACE(*ptr)) {
|
||||
ptr--;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Find wildcards */
|
||||
ptr2 = ptr;
|
||||
while (ptr2 != value) {
|
||||
if (*ptr2 == '*') {
|
||||
/* Wildcard found */
|
||||
found = 1;
|
||||
if (ptr != ptr2) {
|
||||
postfix = ptr2 + 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (*ptr2 == '/') {
|
||||
/* Found slash before wildcard directory-wildcards not supported */
|
||||
break;
|
||||
}
|
||||
ptr2--;
|
||||
}
|
||||
ptr = ptr2;
|
||||
|
||||
/* Use wildcard */
|
||||
if (found) {
|
||||
#ifdef __unix__
|
||||
int ret;
|
||||
struct dirent *dir;
|
||||
DIR *d;
|
||||
char *path;
|
||||
char *filepath = WMALLOC(PATH_MAX, NULL, 0);
|
||||
|
||||
/* Back find the full path */
|
||||
while (ptr2 != value) {
|
||||
if (*ptr2 == '/') {
|
||||
break;
|
||||
}
|
||||
ptr2--;
|
||||
}
|
||||
|
||||
if (ptr2 != value) {
|
||||
path = WMALLOC(ptr2 - value + 1, NULL, 0);
|
||||
memcpy(path, value, ptr2 - value);
|
||||
path[ptr2 - value] = '\0';
|
||||
prefix = ptr2 + 1;
|
||||
prefixLen = (int)(ptr - ptr2 - 1);
|
||||
} else {
|
||||
path = WMALLOC(2, NULL, 0);
|
||||
memcpy(path, ".", 1);
|
||||
path[1] = '\0';
|
||||
prefix = value;
|
||||
prefixLen = (int)(ptr - value);
|
||||
}
|
||||
|
||||
d = opendir(path);
|
||||
if (d) {
|
||||
while ((dir = readdir(d)) != NULL) {
|
||||
if (dir->d_type == DT_DIR) {
|
||||
/* Skip sub-directories */
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
/* Check if filename prefix matches */
|
||||
if (prefixLen > 0) {
|
||||
if ((int)WSTRLEN(dir->d_name) <= prefixLen) {
|
||||
continue;
|
||||
}
|
||||
if (WSTRNCMP(dir->d_name, prefix, prefixLen) != 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (postfix) {
|
||||
/* Skip if file is too short */
|
||||
if (WSTRLEN(dir->d_name) <= WSTRLEN(postfix)) {
|
||||
continue;
|
||||
}
|
||||
if (WSTRNCMP(dir->d_name + WSTRLEN(dir->d_name) -
|
||||
WSTRLEN(postfix), postfix, WSTRLEN(postfix))
|
||||
== 0) {
|
||||
snprintf(filepath, PATH_MAX, "%s/%s", path,
|
||||
dir->d_name);
|
||||
}
|
||||
else {
|
||||
/* Not a match */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
snprintf(filepath, PATH_MAX, "%s/%s", path,
|
||||
dir->d_name);
|
||||
}
|
||||
ret = wolfSSHD_ConfigLoad(conf, filepath);
|
||||
if (ret != WS_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Bad directory */
|
||||
WFREE(filepath, NULL, 0);
|
||||
return WS_BAD_ARGUMENT;
|
||||
}
|
||||
WFREE(filepath, NULL, 0);
|
||||
#else
|
||||
(void)postfix;
|
||||
(void)prefixLen;
|
||||
(void)prefix;
|
||||
/* Don't support wildcards here */
|
||||
return WS_BAD_ARGUMENT;
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
return wolfSSHD_ConfigLoad(conf, value);
|
||||
}
|
||||
return WS_SUCCESS;
|
||||
}
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int HandleConfigOption(WOLFSSHD_CONFIG* conf, int opt, const char* value)
|
||||
{
|
||||
int ret = WS_BAD_ARGUMENT;
|
||||
|
||||
switch (opt) {
|
||||
case OPT_AUTH_KEYS_FILE:
|
||||
ret = wolfSSHD_ConfigSetAuthKeysFile(conf, value);
|
||||
break;
|
||||
case OPT_PRIV_SEP:
|
||||
ret = HandlePrivSep(conf, value);
|
||||
break;
|
||||
case OPT_PERMIT_EMPTY_PW:
|
||||
ret = HandlePermitEmptyPw(conf, value);
|
||||
break;
|
||||
case OPT_SUBSYSTEM:
|
||||
/* TODO */
|
||||
ret = WS_SUCCESS;
|
||||
break;
|
||||
case OPT_CHALLENGE_RESPONSE_AUTH:
|
||||
/* TODO */
|
||||
ret = WS_SUCCESS;
|
||||
break;
|
||||
case OPT_USE_PAM:
|
||||
/* TODO */
|
||||
ret = WS_SUCCESS;
|
||||
break;
|
||||
case OPT_X11_FORWARDING:
|
||||
/* TODO */
|
||||
ret = WS_SUCCESS;
|
||||
break;
|
||||
case OPT_PRINT_MOTD:
|
||||
/* TODO */
|
||||
ret = WS_SUCCESS;
|
||||
break;
|
||||
case OPT_ACCEPT_ENV:
|
||||
/* TODO */
|
||||
ret = WS_SUCCESS;
|
||||
break;
|
||||
case OPT_PROTOCOL:
|
||||
/* TODO */
|
||||
ret = HandleProtocol(conf, value);
|
||||
break;
|
||||
case OPT_LOGIN_GRACE_TIME:
|
||||
ret = HandleLoginGraceTime(conf, value);
|
||||
break;
|
||||
case OPT_HOST_KEY:
|
||||
/* TODO: Add logic to check if file exists? */
|
||||
ret = wolfSSHD_ConfigSetHostKeyFile(conf, value);
|
||||
break;
|
||||
case OPT_PASSWORD_AUTH:
|
||||
ret = HandlePwAuth(conf, value);
|
||||
break;
|
||||
case OPT_PORT:
|
||||
ret = HandlePort(conf, value);
|
||||
break;
|
||||
case OPT_PERMIT_ROOT:
|
||||
ret = HandlePermitRoot(conf, value);
|
||||
break;
|
||||
case OPT_USE_DNS:
|
||||
/* TODO */
|
||||
ret = WS_SUCCESS;
|
||||
break;
|
||||
case OPT_INCLUDE:
|
||||
ret = HandleInclude(conf, value);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* helper function to count white spaces, returns the number of white spaces on
|
||||
* success */
|
||||
static int CountWhitespace(const char* in, int inSz, byte inv)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
if (in != NULL) {
|
||||
for (; i < inSz; ++i) {
|
||||
if (inv) {
|
||||
if (WISSPACE(in[i])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!WISSPACE(in[i])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/* returns WS_SUCCESS on success
|
||||
* Fails if any option is found that is unknown/unsupported
|
||||
*/
|
||||
WOLFSSHD_STATIC int ParseConfigLine(WOLFSSHD_CONFIG* conf, const char* l,
|
||||
int lSz)
|
||||
{
|
||||
int ret = WS_BAD_ARGUMENT;
|
||||
int sz = 0;
|
||||
char tmp[MAX_FILENAME_SZ];
|
||||
int idx;
|
||||
const CONFIG_OPTION* found = NULL;
|
||||
|
||||
for (idx = 0; idx < NUM_OPTIONS; ++idx) {
|
||||
sz = (int)WSTRLEN(options[idx].name);
|
||||
if (lSz >= sz && WSTRNCMP(l, options[idx].name, sz) == 0) {
|
||||
found = &options[idx];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found != NULL) {
|
||||
/*
|
||||
* Count leading and trailing whitespace. Use that information to cut
|
||||
* out just the string itself when creating tmp.
|
||||
*/
|
||||
idx = sz;
|
||||
idx += CountWhitespace(l + idx, lSz - sz, 0);
|
||||
sz = CountWhitespace(l + idx, lSz - sz, 1);
|
||||
if (sz >= MAX_FILENAME_SZ) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Filename too long.");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
else {
|
||||
WMEMCPY(tmp, l + idx, sz);
|
||||
tmp[sz] = 0;
|
||||
ret = HandleConfigOption(conf, found->tag, tmp);
|
||||
}
|
||||
}
|
||||
else {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error parsing config line.");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* parses and loads in the given configuration file 'filename'
|
||||
* returns WS_SUCCESS on success
|
||||
*/
|
||||
int wolfSSHD_ConfigLoad(WOLFSSHD_CONFIG* conf, const char* filename)
|
||||
{
|
||||
XFILE f;
|
||||
int ret = WS_SUCCESS;
|
||||
char buf[MAX_LINE_SIZE];
|
||||
const char* current;
|
||||
|
||||
if (conf == NULL || filename == NULL)
|
||||
return BAD_FUNC_ARG;
|
||||
|
||||
f = XFOPEN(filename, "rb");
|
||||
if (f == XBADFILE) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "Unable to open SSHD config file %s\n",
|
||||
filename);
|
||||
return BAD_FUNC_ARG;
|
||||
}
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] parsing config file %s", filename);
|
||||
|
||||
while ((current = XFGETS(buf, MAX_LINE_SIZE, f)) != NULL) {
|
||||
int currentSz = (int)XSTRLEN(current);
|
||||
|
||||
/* remove leading spaces */
|
||||
while (currentSz > 0 && current[0] == ' ') {
|
||||
currentSz = currentSz - 1;
|
||||
current = current + 1;
|
||||
}
|
||||
|
||||
if (currentSz <= 1) {
|
||||
continue; /* empty line */
|
||||
}
|
||||
|
||||
if (current[0] == '#') {
|
||||
continue; /* commented out line */
|
||||
}
|
||||
|
||||
ret = ParseConfigLine(conf, current, currentSz);
|
||||
if (ret != WS_SUCCESS) {
|
||||
fprintf(stderr, "Unable to parse config line : %s\n", current);
|
||||
break;
|
||||
}
|
||||
}
|
||||
XFCLOSE(f);
|
||||
|
||||
SetAuthKeysPattern(conf->authKeysFile);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char* wolfSSHD_ConfigGetAuthKeysFile(const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
char* ret = NULL;
|
||||
|
||||
if (conf != NULL) {
|
||||
ret = conf->authKeysFile;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int wolfSSHD_ConfigSetAuthKeysFile(WOLFSSHD_CONFIG* conf, const char* file)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
if (conf == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (conf->authKeysFile != NULL) {
|
||||
FreeString(&conf->authKeysFile, conf->heap);
|
||||
conf->authKeysFile = NULL;
|
||||
}
|
||||
|
||||
if (file != NULL) {
|
||||
ret = CreateString(&conf->authKeysFile, file,
|
||||
(int)WSTRLEN(file), conf->heap);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char* wolfSSHD_ConfigGetBanner(const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
char* ret = NULL;
|
||||
|
||||
if (conf != NULL) {
|
||||
ret = conf->banner;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char* wolfSSHD_ConfigGetHostKeyFile(const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
char* ret = NULL;
|
||||
|
||||
if (conf != NULL) {
|
||||
ret = conf->hostKeyFile;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int wolfSSHD_ConfigSetHostKeyFile(WOLFSSHD_CONFIG* conf, const char* file)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
if (conf == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (conf->hostKeyFile != NULL) {
|
||||
FreeString(&conf->hostKeyFile, conf->heap);
|
||||
conf->hostKeyFile = NULL;
|
||||
}
|
||||
|
||||
if (file != NULL) {
|
||||
ret = CreateString(&conf->hostKeyFile, file,
|
||||
(int)WSTRLEN(file), conf->heap);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
word16 wolfSSHD_ConfigGetPort(const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
word16 ret = 0;
|
||||
|
||||
if (conf != NULL) {
|
||||
ret = conf->port;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
byte wolfSSHD_ConfigGetPermitEmptyPw(const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
byte ret = 0;
|
||||
|
||||
if (conf != NULL) {
|
||||
ret = conf->permitEmptyPasswords;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
byte wolfSSHD_ConfigGetPrivilegeSeparation(const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
byte ret = 0;
|
||||
|
||||
if (conf != NULL) {
|
||||
ret = conf->usePrivilegeSeparation;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
byte wolfSSHD_ConfigGetPwAuth(const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
byte ret = 0;
|
||||
|
||||
if (conf != NULL) {
|
||||
ret = conf->passwordAuth;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
byte wolfSSHD_ConfigGetPermitRoot(const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
byte ret = 0;
|
||||
|
||||
if (conf != NULL) {
|
||||
ret = conf->permitRootLogin;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
long wolfSSHD_ConfigGetGraceTime(const WOLFSSHD_CONFIG* conf)
|
||||
{
|
||||
long ret = WS_BAD_ARGUMENT;
|
||||
|
||||
if (conf != NULL) {
|
||||
ret = conf->loginTimer;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* WOLFSSH_SSHD */
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/* configuration.h
|
||||
*
|
||||
* Copyright (C) 2014-2022 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSH.
|
||||
*
|
||||
* wolfSSH is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSH is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef WOLFSSHD_H
|
||||
#define WOLFSSHD_H
|
||||
|
||||
typedef struct WOLFSSHD_CONFIG WOLFSSHD_CONFIG;
|
||||
|
||||
#include "auth.h"
|
||||
|
||||
/* 0 so that privilage seperation is default on after struct memset'd on init */
|
||||
#define WOLFSSHD_PRIV_SEPARAT 0
|
||||
#define WOLFSSHD_PRIV_SANDBOX 1
|
||||
#define WOLFSSHD_PRIV_OFF 2
|
||||
|
||||
WOLFSSHD_CONFIG* wolfSSHD_ConfigNew(void* heap);
|
||||
void wolfSSHD_ConfigFree(WOLFSSHD_CONFIG* conf);
|
||||
int wolfSSHD_ConfigLoad(WOLFSSHD_CONFIG* conf, const char* filename);
|
||||
|
||||
char* wolfSSHD_ConfigGetBanner(const WOLFSSHD_CONFIG* conf);
|
||||
char* wolfSSHD_ConfigGetHostKeyFile(const WOLFSSHD_CONFIG* conf);
|
||||
int wolfSSHD_ConfigSetHostKeyFile(WOLFSSHD_CONFIG* conf, const char* file);
|
||||
word16 wolfSSHD_ConfigGetPort(const WOLFSSHD_CONFIG* conf);
|
||||
char* wolfSSHD_ConfigGetAuthKeysFile(const WOLFSSHD_CONFIG* conf);
|
||||
int wolfSSHD_ConfigSetAuthKeysFile(WOLFSSHD_CONFIG* conf, const char* file);
|
||||
byte wolfSSHD_ConfigGetPermitEmptyPw(const WOLFSSHD_CONFIG* conf);
|
||||
byte wolfSSHD_ConfigGetPermitRoot(const WOLFSSHD_CONFIG* conf);
|
||||
byte wolfSSHD_ConfigGetPrivilegeSeparation(const WOLFSSHD_CONFIG* conf);
|
||||
long wolfSSHD_ConfigGetGraceTime(const WOLFSSHD_CONFIG* conf);
|
||||
byte wolfSSHD_ConfigGetPwAuth(const WOLFSSHD_CONFIG* conf);
|
||||
|
||||
#ifdef WOLFSSHD_UNIT_TEST
|
||||
int ParseConfigLine(WOLFSSHD_CONFIG* conf, const char* l, int lSz);
|
||||
#endif
|
||||
|
||||
#endif /* WOLFSSHD_H */
|
||||
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
if BUILD_SSHD
|
||||
|
||||
bin_PROGRAMS += apps/wolfsshd/wolfsshd
|
||||
noinst_HEADERS += apps/wolfsshd/configuration.h \
|
||||
apps/wolfsshd/auth.h
|
||||
apps_wolfsshd_wolfsshd_SOURCES = apps/wolfsshd/wolfsshd.c \
|
||||
apps/wolfsshd/configuration.c \
|
||||
apps/wolfsshd/auth.c
|
||||
apps_wolfsshd_wolfsshd_LDADD = src/libwolfssh.la
|
||||
apps_wolfsshd_wolfsshd_DEPENDENCIES = src/libwolfssh.la
|
||||
|
||||
noinst_PROGRAMS += apps/wolfsshd/test/test_configuration
|
||||
apps_wolfsshd_test_test_configuration_SOURCES = apps/wolfsshd/test/test_configuration.c \
|
||||
apps/wolfsshd/configuration.c \
|
||||
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/
|
||||
|
||||
DISTCLEANFILES+= apps/wolfsshd/.libs/wolfsshd \
|
||||
apps/wolfsshd/test/.libs/test_configuration
|
||||
|
||||
endif BUILD_SSHD
|
||||
|
|
@ -0,0 +1,164 @@
|
|||
#include <stdarg.h>
|
||||
|
||||
#include <wolfssh/ssh.h>
|
||||
#include <configuration.h>
|
||||
|
||||
#ifndef WOLFSSH_DEFAULT_LOG_WIDTH
|
||||
#define WOLFSSH_DEFAULT_LOG_WIDTH 120
|
||||
#endif
|
||||
|
||||
#undef FMTCHECK
|
||||
#ifdef __GNUC__
|
||||
#define FMTCHECK __attribute__((format(printf,1,2)))
|
||||
#else
|
||||
#define FMTCHECK
|
||||
#endif /* __GNUC__ */
|
||||
|
||||
|
||||
void Log(const char *const, ...) FMTCHECK;
|
||||
void Log(const char *const fmt, ...)
|
||||
{
|
||||
va_list vlist;
|
||||
char msgStr[WOLFSSH_DEFAULT_LOG_WIDTH];
|
||||
|
||||
va_start(vlist, fmt);
|
||||
WVSNPRINTF(msgStr, sizeof(msgStr), fmt, vlist);
|
||||
va_end(vlist);
|
||||
}
|
||||
|
||||
typedef int (*TEST_FUNC)(void);
|
||||
typedef struct {
|
||||
const char *name;
|
||||
TEST_FUNC func;
|
||||
} TEST_CASE;
|
||||
|
||||
#define TEST_DECL(func) { #func, func }
|
||||
|
||||
#define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
|
||||
|
||||
static void TestSetup(const TEST_CASE* tc)
|
||||
{
|
||||
Log("Running %s.\n", tc->name);
|
||||
}
|
||||
|
||||
static void TestCleanup(void)
|
||||
{
|
||||
}
|
||||
|
||||
static int RunTest(const TEST_CASE* tc)
|
||||
{
|
||||
int ret;
|
||||
|
||||
TestSetup(tc);
|
||||
|
||||
ret = tc->func();
|
||||
if (ret != 0) {
|
||||
Log("%s FAILED.\n", tc->name);
|
||||
}
|
||||
else {
|
||||
Log("%s PASSED.\n", tc->name);
|
||||
}
|
||||
|
||||
TestCleanup();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
const char* desc;
|
||||
const char* line;
|
||||
int shouldFail;
|
||||
} CONFIG_LINE_VECTOR;
|
||||
|
||||
static int test_ParseConfigLine(void)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
int i;
|
||||
WOLFSSHD_CONFIG* conf;
|
||||
|
||||
static CONFIG_LINE_VECTOR vectors[] = {
|
||||
/* Port tests. */
|
||||
{"Valid port", "Port 22", 0},
|
||||
{"Port too big", "Port 65536", 1},
|
||||
{"Negative port", "Port -99", 1},
|
||||
{"Port 0", "Port 0", 1},
|
||||
{"Port NaN", "Port wolfsshd", 1},
|
||||
{"Port no value", "Port \n", 1},
|
||||
|
||||
/* Whitespace tests. */
|
||||
{"Extra leading whitespace", "Port 22", 0},
|
||||
{"Extra trailing whitespace", "Port 22 \n", 0},
|
||||
|
||||
/* Privilege separation tests. */
|
||||
{"Privilege separation yes", "UsePrivilegeSeparation yes", 0},
|
||||
{"Privilege separation no", "UsePrivilegeSeparation no", 0},
|
||||
{"Privilege separation sandbox", "UsePrivilegeSeparation sandbox", 0},
|
||||
{"Privilege separation invalid", "UsePrivilegeSeparation wolfsshd", 1},
|
||||
|
||||
/* Login grace time tests. */
|
||||
{"Valid login grace time seconds", "LoginGraceTime 60", 0},
|
||||
{"Valid login grace time minutes", "LoginGraceTime 1m", 0},
|
||||
{"Valid login grace time hours", "LoginGraceTime 1h", 0},
|
||||
{"Invalid login grace time", "LoginGraceTime wolfsshd", 1},
|
||||
|
||||
/* Permit empty password tests. */
|
||||
{"Permit empty password no", "PermitEmptyPasswords no", 0},
|
||||
{"Permit empty password yes", "PermitEmptyPasswords yes", 0},
|
||||
{"Permit empty password invalid", "PermitEmptyPasswords wolfsshd", 1},
|
||||
|
||||
/* Password auth tests. */
|
||||
{"Password auth no", "PasswordAuthentication no", 0},
|
||||
{"Password auth yes", "PasswordAuthentication yes", 0},
|
||||
{"Password auth invalid", "PasswordAuthentication wolfsshd", 1},
|
||||
};
|
||||
const int numVectors = (int)(sizeof(vectors) / sizeof(*vectors));
|
||||
|
||||
conf = wolfSSHD_ConfigNew(NULL);
|
||||
if (conf == NULL) {
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
for (i = 0; i < numVectors; ++i) {
|
||||
Log(" Testing scenario: %s.", vectors[i].desc);
|
||||
|
||||
ret = ParseConfigLine(conf, vectors[i].line,
|
||||
(int)WSTRLEN(vectors[i].line));
|
||||
|
||||
if ((ret == WS_SUCCESS && !vectors[i].shouldFail) ||
|
||||
(ret != WS_SUCCESS && vectors[i].shouldFail)) {
|
||||
Log(" PASSED.\n");
|
||||
ret = WS_SUCCESS;
|
||||
}
|
||||
else {
|
||||
Log(" FAILED.\n");
|
||||
ret = WS_FATAL_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const TEST_CASE testCases[] = {
|
||||
TEST_DECL(test_ParseConfigLine)
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int i;
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
for (i = 0; i < TEST_CASE_CNT; ++i) {
|
||||
ret = RunTest(&testCases[i]);
|
||||
if (ret != WS_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -0,0 +1,964 @@
|
|||
/* wolfsshd.c
|
||||
*
|
||||
* Copyright (C) 2014-2022 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSH.
|
||||
*
|
||||
* wolfSSH is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSH is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSH_SSHD
|
||||
|
||||
#include <wolfssh/ssh.h>
|
||||
#include <wolfssh/internal.h>
|
||||
#include <wolfssh/log.h>
|
||||
#include <wolfssl/wolfcrypt/wc_port.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
#include <wolfssl/wolfcrypt/logging.h>
|
||||
#include <wolfssl/wolfcrypt/asn_public.h>
|
||||
|
||||
#define WOLFSSH_TEST_SERVER
|
||||
#include <wolfssh/test.h>
|
||||
|
||||
#include "configuration.h"
|
||||
#include "auth.h"
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#ifdef NO_INLINE
|
||||
#include <wolfssh/misc.h>
|
||||
#else
|
||||
#define WOLFSSH_MISC_INCLUDED
|
||||
#include "src/misc.c"
|
||||
#endif
|
||||
|
||||
#ifndef WOLFSSHD_TIMEOUT
|
||||
#define WOLFSSHD_TIMEOUT 1
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSH_SHELL
|
||||
#ifdef HAVE_PTY_H
|
||||
#include <pty.h>
|
||||
#endif
|
||||
#ifdef HAVE_UTIL_H
|
||||
#include <util.h>
|
||||
#endif
|
||||
#ifdef HAVE_TERMIOS_H
|
||||
#include <termios.h>
|
||||
#endif
|
||||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
#if defined(__QNX__) || defined(__QNXNTO__)
|
||||
#include <errno.h>
|
||||
#include <unix.h>
|
||||
#else
|
||||
#include <sys/errno.h>
|
||||
#endif
|
||||
|
||||
static volatile int ChildRunning = 0;
|
||||
static void ChildSig(int sig)
|
||||
{
|
||||
(void)sig;
|
||||
ChildRunning = 0;
|
||||
}
|
||||
|
||||
static void ConnClose(int sig)
|
||||
{
|
||||
pid_t p;
|
||||
int ret;
|
||||
p = wait(&ret);
|
||||
if (p == 0 || p == -1)
|
||||
return; /* parent or error state*/
|
||||
(void)ret;
|
||||
(void)sig;
|
||||
}
|
||||
#endif /* WOLFSSH_SHELL */
|
||||
|
||||
static volatile byte debugMode = 0; /* default to off */
|
||||
static FILE* logFile = NULL;
|
||||
|
||||
/* catch interrupts and close down gracefully */
|
||||
static volatile byte quit = 0;
|
||||
static const char defaultBanner[] = "wolfSSHD\n";
|
||||
|
||||
/* Initial connection information to pass on to threads/forks */
|
||||
typedef struct WOLFSSHD_CONNECTION {
|
||||
WOLFSSH_CTX* ctx;
|
||||
WOLFSSHD_AUTH* auth;
|
||||
int fd;
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
} WOLFSSHD_CONNECTION;
|
||||
|
||||
#ifdef __unix__
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
static void SyslogCb(enum wolfSSH_LogLevel level, const char *const msgStr)
|
||||
{
|
||||
int priority;
|
||||
|
||||
switch (level) {
|
||||
case WS_LOG_WARN:
|
||||
priority = LOG_WARNING;
|
||||
break;
|
||||
case WS_LOG_ERROR:
|
||||
priority = LOG_ERR;
|
||||
break;
|
||||
case WS_LOG_DEBUG:
|
||||
priority = LOG_DEBUG;
|
||||
break;
|
||||
case WS_LOG_INFO:
|
||||
case WS_LOG_USER:
|
||||
case WS_LOG_SFTP:
|
||||
case WS_LOG_SCP:
|
||||
case WS_LOG_AGENT:
|
||||
default:
|
||||
priority = LOG_INFO;
|
||||
break;
|
||||
}
|
||||
openlog("sshd", LOG_PID, LOG_DAEMON);
|
||||
syslog(priority, "%s", msgStr);
|
||||
closelog();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static void ShowUsage(void)
|
||||
{
|
||||
printf("wolfsshd %s\n", LIBWOLFSSH_VERSION_STRING);
|
||||
printf(" -? display this help and exit\n");
|
||||
printf(" -f <file name> Configuration file to use, default is "
|
||||
"/etc/ssh/sshd_config\n");
|
||||
printf(" -p <int> Port number to listen on\n");
|
||||
printf(" -d Turn on debug mode\n");
|
||||
printf(" -D Run in foreground (do not detach)\n");
|
||||
printf(" -h <file name> host private key file to use\n");
|
||||
printf(" -E <file name> append to log file\n");
|
||||
}
|
||||
|
||||
|
||||
/* catch if interupted */
|
||||
static void interruptCatch(int in)
|
||||
{
|
||||
(void)in;
|
||||
if (logFile)
|
||||
fprintf(logFile, "Closing down wolfSSHD\n");
|
||||
quit = 1;
|
||||
}
|
||||
|
||||
|
||||
/* redirect logging to a specific file and add the PID value */
|
||||
static void wolfSSHDLoggingCb(enum wolfSSH_LogLevel lvl, const char *const str)
|
||||
{
|
||||
/* always log errors and optionally log other info/debug level messages */
|
||||
if (lvl == WS_LOG_ERROR) {
|
||||
fprintf(logFile, "[PID %d]: %s\n", getpid(), str);
|
||||
}
|
||||
else if (debugMode) {
|
||||
fprintf(logFile, "[PID %d]: %s\n", getpid(), str);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Frees up the WOLFSSH_CTX struct */
|
||||
static void CleanupCTX(WOLFSSHD_CONFIG* conf, WOLFSSH_CTX** ctx)
|
||||
{
|
||||
if (ctx != NULL && *ctx != NULL) {
|
||||
wolfSSH_CTX_free(*ctx);
|
||||
*ctx = NULL;
|
||||
}
|
||||
(void)conf;
|
||||
}
|
||||
|
||||
|
||||
/* Initializes and sets up the WOLFSSH_CTX struct based on the configure options
|
||||
* return WS_SUCCESS on success
|
||||
*/
|
||||
static int SetupCTX(WOLFSSHD_CONFIG* conf, WOLFSSH_CTX** ctx)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
const char* banner;
|
||||
DerBuffer* der = NULL;
|
||||
byte* privBuf;
|
||||
word32 privBufSz;
|
||||
|
||||
/* create a new WOLFSSH_CTX */
|
||||
*ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL);
|
||||
if (ctx == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Couldn't allocate SSH CTX data.");
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
|
||||
/* setup authority callback for checking peer connections */
|
||||
if (ret == WS_SUCCESS) {
|
||||
wolfSSH_SetUserAuth(*ctx, DefaultUserAuth);
|
||||
}
|
||||
|
||||
/* set banner to display on connection */
|
||||
if (ret == WS_SUCCESS) {
|
||||
banner = wolfSSHD_ConfigGetBanner(conf);
|
||||
if (banner == NULL) {
|
||||
banner = defaultBanner;
|
||||
}
|
||||
wolfSSH_CTX_SetBanner(*ctx, banner);
|
||||
}
|
||||
|
||||
/* Load in host private key */
|
||||
if (ret == WS_SUCCESS) {
|
||||
|
||||
char* hostKey = wolfSSHD_ConfigGetHostKeyFile(conf);
|
||||
|
||||
if (hostKey == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] No host private key set");
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
else {
|
||||
FILE* f;
|
||||
|
||||
f = XFOPEN(hostKey, "rb");
|
||||
if (f == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Unable to open host private key");
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
else {
|
||||
byte* data;
|
||||
int dataSz = 4096;
|
||||
|
||||
data = (byte*)WMALLOC(dataSz, NULL, 0);
|
||||
|
||||
dataSz = (int)XFREAD(data, 1, dataSz, f);
|
||||
XFCLOSE(f);
|
||||
|
||||
if (wc_PemToDer(data, dataSz, PRIVATEKEY_TYPE, &der, NULL,
|
||||
NULL, NULL) != 0) {
|
||||
wolfSSH_Log(WS_LOG_DEBUG, "[SSHD] Failed to convert host "
|
||||
"private key from PEM. Assuming key in DER "
|
||||
"format.");
|
||||
privBuf = data;
|
||||
privBufSz = dataSz;
|
||||
}
|
||||
else {
|
||||
privBuf = der->buffer;
|
||||
privBufSz = der->length;
|
||||
}
|
||||
|
||||
if (wolfSSH_CTX_UsePrivateKey_buffer(*ctx, privBuf, privBufSz,
|
||||
WOLFSSH_FORMAT_ASN1) < 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Failed to use host private key.");
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
WFREE(data, NULL, 0);
|
||||
wc_FreeDer(&der);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* @TODO Load in host public key */
|
||||
|
||||
/* Set allowed connection type, i.e. public key / password */
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
#ifdef WOLFSSH_SFTP
|
||||
#define TEST_SFTP_TIMEOUT 1
|
||||
|
||||
/* handle SFTP operations
|
||||
* returns 0 on success
|
||||
*/
|
||||
static int SFTP_Subsystem(WOLFSSH* ssh, WOLFSSHD_CONNECTION* conn)
|
||||
{
|
||||
byte tmp[1];
|
||||
int ret = WS_SUCCESS;
|
||||
int error = WS_SUCCESS;
|
||||
WS_SOCKET_T sockfd;
|
||||
int select_ret = 0;
|
||||
|
||||
sockfd = (WS_SOCKET_T)wolfSSH_get_fd(ssh);
|
||||
do {
|
||||
if (wolfSSH_stream_peek(ssh, tmp, 1) > 0) {
|
||||
select_ret = WS_SELECT_RECV_READY;
|
||||
}
|
||||
else {
|
||||
select_ret = tcp_select(sockfd, TEST_SFTP_TIMEOUT);
|
||||
}
|
||||
|
||||
if (select_ret == WS_SELECT_RECV_READY ||
|
||||
select_ret == WS_SELECT_ERROR_READY ||
|
||||
error == WS_WANT_WRITE)
|
||||
{
|
||||
ret = wolfSSH_SFTP_read(ssh);
|
||||
error = wolfSSH_get_error(ssh);
|
||||
}
|
||||
else if (select_ret == WS_SELECT_TIMEOUT)
|
||||
error = WS_WANT_READ;
|
||||
else
|
||||
error = WS_FATAL_ERROR;
|
||||
|
||||
if (error == WS_WANT_READ || error == WS_WANT_WRITE ||
|
||||
error == WS_CHAN_RXD || error == WS_REKEYING ||
|
||||
error == WS_WINDOW_FULL)
|
||||
ret = error;
|
||||
|
||||
if (ret == WS_FATAL_ERROR && error == 0) {
|
||||
WOLFSSH_CHANNEL* channel =
|
||||
wolfSSH_ChannelNext(ssh, NULL);
|
||||
if (channel && wolfSSH_ChannelGetEof(channel)) {
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (ret != WS_FATAL_ERROR);
|
||||
|
||||
(void)conn;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef WOLFSSH_SHELL
|
||||
|
||||
#ifndef MAX_COMMAND_SZ
|
||||
#define MAX_COMMAND_SZ 80
|
||||
#endif
|
||||
|
||||
static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh)
|
||||
{
|
||||
WS_SOCKET_T sshFd = 0;
|
||||
int rc;
|
||||
const char *userName;
|
||||
struct passwd *p_passwd;
|
||||
WS_SOCKET_T childFd = 0;
|
||||
pid_t childPid;
|
||||
#ifndef EXAMPLE_BUFFER_SZ
|
||||
#define EXAMPLE_BUFFER_SZ 4096
|
||||
#endif
|
||||
byte shellBuffer[EXAMPLE_BUFFER_SZ];
|
||||
byte channelBuffer[EXAMPLE_BUFFER_SZ];
|
||||
|
||||
userName = wolfSSH_GetUsername(ssh);
|
||||
if (userName == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Failure get user name");
|
||||
return WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
/* 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");
|
||||
return WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
p_passwd = getpwnam((const char *)userName);
|
||||
if (p_passwd == NULL) {
|
||||
/* Not actually a user on the system. */
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Invalid user name found");
|
||||
if (wolfSSHD_AuthReducePermissions(conn->auth) != WS_SUCCESS) {
|
||||
/* stop everything if not able to reduce permissions level */
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
ChildRunning = 1;
|
||||
childPid = forkpty(&childFd, NULL, NULL, NULL);
|
||||
if (childPid < 0) {
|
||||
/* forkpty failed, so return */
|
||||
ChildRunning = 0;
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Issue creating new forkpty");
|
||||
return WS_FATAL_ERROR;
|
||||
}
|
||||
else if (childPid == 0) {
|
||||
/* Child process */
|
||||
char cmd[MAX_COMMAND_SZ];
|
||||
int ret;
|
||||
|
||||
signal(SIGINT, SIG_DFL);
|
||||
signal(SIGCHLD, SIG_DFL);
|
||||
|
||||
setenv("HOME", p_passwd->pw_dir, 1);
|
||||
setenv("LOGNAME", p_passwd->pw_name, 1);
|
||||
rc = chdir(p_passwd->pw_dir);
|
||||
if (rc != 0) {
|
||||
return WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
/* default to /bin/sh if user shell is not set */
|
||||
WMEMSET(cmd, 0, sizeof(cmd));
|
||||
if (XSTRLEN(p_passwd->pw_shell) == 0) {
|
||||
#if defined(__QNX__) || defined(__QNXNTO__)
|
||||
XSNPRINTF(cmd, sizeof(cmd), "su - %s -c %s", userName,
|
||||
"/bin/sh");
|
||||
#else
|
||||
XSNPRINTF(cmd, sizeof(cmd), "runuser -u %s -- %s", userName,
|
||||
"/bin/sh");
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
#if defined(__QNX__) || defined(__QNXNTO__)
|
||||
XSNPRINTF(cmd, sizeof(cmd), "su - %s -c %s", userName,
|
||||
p_passwd->pw_shell);
|
||||
#else
|
||||
XSNPRINTF(cmd, sizeof(cmd), "runuser -u %s -- %s", userName,
|
||||
p_passwd->pw_shell);
|
||||
#endif
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
ret = system(cmd);
|
||||
if (ret && errno) {
|
||||
perror("error executing shell command ");
|
||||
exit(1);
|
||||
}
|
||||
exit(0); /* exit child process and close down SSH connection */
|
||||
}
|
||||
|
||||
if (wolfSSHD_AuthReducePermissions(conn->auth) != WS_SUCCESS) {
|
||||
/* stop everything if not able to reduce permissions level */
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Issue reducing permissions level,"
|
||||
" exiting now");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sshFd = wolfSSH_get_fd(ssh);
|
||||
|
||||
struct termios tios;
|
||||
word32 shellChannelId = 0;
|
||||
signal(SIGCHLD, ChildSig);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
|
||||
rc = tcgetattr(childFd, &tios);
|
||||
if (rc != 0) {
|
||||
return WS_FATAL_ERROR;
|
||||
}
|
||||
rc = tcsetattr(childFd, TCSAFLUSH, &tios);
|
||||
if (rc != 0) {
|
||||
return WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
while (ChildRunning) {
|
||||
byte tmp[2];
|
||||
fd_set readFds;
|
||||
WS_SOCKET_T maxFd;
|
||||
int cnt_r;
|
||||
int cnt_w;
|
||||
int pending = 0;
|
||||
|
||||
FD_ZERO(&readFds);
|
||||
FD_SET(sshFd, &readFds);
|
||||
maxFd = sshFd;
|
||||
|
||||
FD_SET(childFd, &readFds);
|
||||
if (childFd > maxFd)
|
||||
maxFd = childFd;
|
||||
|
||||
if (wolfSSH_stream_peek(ssh, tmp, 1) <= 0) {
|
||||
rc = select((int)maxFd + 1, &readFds, NULL, NULL, NULL);
|
||||
if (rc == -1)
|
||||
break;
|
||||
}
|
||||
else {
|
||||
pending = 1; /* found some pending SSH data */
|
||||
}
|
||||
|
||||
if (pending || FD_ISSET(sshFd, &readFds)) {
|
||||
word32 lastChannel = 0;
|
||||
|
||||
/* The following tries to read from the first channel inside
|
||||
the stream. If the pending data in the socket is for
|
||||
another channel, this will return an error with id
|
||||
WS_CHAN_RXD. That means the agent has pending data in its
|
||||
channel. The additional channel is only used with the
|
||||
agent. */
|
||||
cnt_r = wolfSSH_worker(ssh, &lastChannel);
|
||||
if (cnt_r < 0) {
|
||||
rc = wolfSSH_get_error(ssh);
|
||||
if (rc == WS_CHAN_RXD) {
|
||||
if (lastChannel == shellChannelId) {
|
||||
cnt_r = wolfSSH_ChannelIdRead(ssh, shellChannelId,
|
||||
channelBuffer,
|
||||
sizeof channelBuffer);
|
||||
if (cnt_r <= 0)
|
||||
break;
|
||||
cnt_w = (int)write(childFd,
|
||||
channelBuffer, cnt_r);
|
||||
if (cnt_w <= 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (rc == WS_CHANNEL_CLOSED) {
|
||||
continue;
|
||||
}
|
||||
else if (rc != WS_WANT_READ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (FD_ISSET(childFd, &readFds)) {
|
||||
cnt_r = (int)read(childFd, shellBuffer, sizeof shellBuffer);
|
||||
/* This read will return 0 on EOF */
|
||||
if (cnt_r <= 0) {
|
||||
int err = errno;
|
||||
if (err != EAGAIN) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (cnt_r > 0) {
|
||||
cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId,
|
||||
shellBuffer, cnt_r);
|
||||
if (cnt_w < 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(void)conn;
|
||||
return WS_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
static __thread int timeOut = 0;
|
||||
static void alarmCatch(int signum)
|
||||
{
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Failed login within grace period");
|
||||
timeOut = 1;
|
||||
(void)signum;
|
||||
}
|
||||
|
||||
/* handle wolfSSH accept and directing to correct subsystem */
|
||||
static void* HandleConnection(void* arg)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
WOLFSSHD_CONNECTION* conn = NULL;
|
||||
WOLFSSH* ssh = NULL;
|
||||
|
||||
if (arg == NULL) {
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
conn = (WOLFSSHD_CONNECTION*)arg;
|
||||
ssh = wolfSSH_new(conn->ctx);
|
||||
if (ssh == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Failed to create new WOLFSSH struct");
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
int error;
|
||||
int select_ret = 0;
|
||||
long graceTime;
|
||||
|
||||
wolfSSH_set_fd(ssh, conn->fd);
|
||||
wolfSSH_SetUserAuthCtx(ssh, conn->auth);
|
||||
|
||||
/* set alarm for login grace time */
|
||||
graceTime = wolfSSHD_AuthGetGraceTime(conn->auth);
|
||||
if (graceTime > 0) {
|
||||
signal(SIGALRM, alarmCatch);
|
||||
alarm((unsigned int)graceTime);
|
||||
}
|
||||
|
||||
ret = wolfSSH_accept(ssh);
|
||||
error = wolfSSH_get_error(ssh);
|
||||
while (timeOut == 0 && (ret != WS_SUCCESS
|
||||
&& ret != WS_SCP_COMPLETE && ret != WS_SFTP_COMPLETE)
|
||||
&& (error == WS_WANT_READ || error == WS_WANT_WRITE)) {
|
||||
|
||||
select_ret = tcp_select(conn->fd, 1);
|
||||
if (select_ret == WS_SELECT_RECV_READY ||
|
||||
select_ret == WS_SELECT_ERROR_READY ||
|
||||
error == WS_WANT_WRITE)
|
||||
{
|
||||
ret = wolfSSH_accept(ssh);
|
||||
error = wolfSSH_get_error(ssh);
|
||||
}
|
||||
else if (select_ret == WS_SELECT_TIMEOUT)
|
||||
error = WS_WANT_READ;
|
||||
else
|
||||
error = WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
if (ret != WS_SUCCESS && ret != WS_SFTP_COMPLETE) {
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Failed to accept WOLFSSH connection from %s",
|
||||
conn->ip);
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS || ret == WS_SFTP_COMPLETE) {
|
||||
switch (wolfSSH_GetSessionType(ssh)) {
|
||||
case WOLFSSH_SESSION_SHELL:
|
||||
#ifdef WOLFSSH_SHELL
|
||||
if (ret == WS_SUCCESS) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Entering new shell");
|
||||
SHELL_Subsystem(conn, ssh);
|
||||
}
|
||||
#else
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Shell support is disabled");
|
||||
ret = WS_NOT_COMPILED;
|
||||
#endif
|
||||
break;
|
||||
|
||||
case WOLFSSH_SESSION_SUBSYSTEM:
|
||||
/* test for known subsystems */
|
||||
switch (ret) {
|
||||
case WS_SFTP_COMPLETE:
|
||||
#ifdef WOLFSSH_SFTP
|
||||
ret = SFTP_Subsystem(ssh, conn);
|
||||
#else
|
||||
err_sys("SFTP not compiled in. Please use --enable-sftp");
|
||||
#endif
|
||||
break;
|
||||
|
||||
default:
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Unknown or build not supporting subsystem"
|
||||
" found [%s]", wolfSSH_GetSessionCommand(ssh));
|
||||
ret = WS_NOT_COMPILED;
|
||||
}
|
||||
break;
|
||||
|
||||
case WOLFSSH_SESSION_UNKNOWN:
|
||||
case WOLFSSH_SESSION_EXEC:
|
||||
case WOLFSSH_SESSION_TERMINAL:
|
||||
default:
|
||||
wolfSSH_Log(WS_LOG_ERROR,
|
||||
"[SSHD] Unknown or build not supporting session type found");
|
||||
ret = WS_NOT_COMPILED;
|
||||
}
|
||||
}
|
||||
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Attempting to close down connection");
|
||||
wolfSSH_shutdown(ssh);
|
||||
wolfSSH_free(ssh);
|
||||
if (conn != NULL) {
|
||||
WCLOSESOCKET(conn->fd);
|
||||
}
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Return from closing connection = %d", ret);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* returns WS_SUCCESS on success */
|
||||
static int NewConnection(WOLFSSHD_CONNECTION* conn)
|
||||
{
|
||||
int pd;
|
||||
int ret = WS_SUCCESS;
|
||||
|
||||
pd = fork();
|
||||
if (pd < 0) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Issue spawning new process");
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (pd == 0) {
|
||||
/* child process */
|
||||
signal(SIGINT, SIG_DFL);
|
||||
(void)HandleConnection((void*)conn);
|
||||
exit(0);
|
||||
}
|
||||
else {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Spawned new process %d\n", pd);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* return non zero value for a pending connection */
|
||||
static int PendingConnection(WS_SOCKET_T fd)
|
||||
{
|
||||
int ret;
|
||||
struct timeval t;
|
||||
fd_set r, w, e;
|
||||
WS_SOCKET_T nfds = fd + 1;
|
||||
|
||||
t.tv_usec = 0;
|
||||
t.tv_sec = WOLFSSHD_TIMEOUT;
|
||||
|
||||
FD_ZERO(&r);
|
||||
FD_ZERO(&w);
|
||||
FD_ZERO(&e);
|
||||
|
||||
FD_SET(fd, &r);
|
||||
errno = 0;
|
||||
ret = select(nfds, &r, &w, &e, &t);
|
||||
if (ret < 0) {
|
||||
/* a socket level issue happend, could just be a system call int. */
|
||||
if (errno != EINTR) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] TCP socket error on select()");
|
||||
quit = 1;
|
||||
}
|
||||
ret = 0;
|
||||
}
|
||||
else if (ret > 0) {
|
||||
if (FD_ISSET(fd, &r)) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Incoming TCP data found");
|
||||
}
|
||||
else {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Found TCP write or error data");
|
||||
ret = 0; /* nothing to read */
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int myoptind = 0;
|
||||
char* myoptarg = NULL;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int ret = WS_SUCCESS;
|
||||
word16 port = 0;
|
||||
WS_SOCKET_T listenFd = 0;
|
||||
int ch;
|
||||
WOLFSSHD_CONFIG* conf = NULL;
|
||||
WOLFSSHD_AUTH* auth = NULL;
|
||||
WOLFSSH_CTX* ctx = NULL;
|
||||
byte isDaemon = 1;
|
||||
|
||||
const char* configFile = "/etc/ssh/sshd_config";
|
||||
const char* hostKeyFile = NULL;
|
||||
|
||||
signal(SIGINT, interruptCatch);
|
||||
|
||||
wolfSSH_SetLoggingCb(wolfSSHDLoggingCb);
|
||||
wolfSSH_Debugging_ON();
|
||||
#ifdef DEBUG_WOLFSSL
|
||||
wolfSSL_Debugging_ON();
|
||||
#endif
|
||||
|
||||
logFile = stderr;
|
||||
if (ret == WS_SUCCESS) {
|
||||
wolfSSH_Init();
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
conf = wolfSSHD_ConfigNew(NULL);
|
||||
if (conf == NULL) {
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
}
|
||||
|
||||
while ((ch = mygetopt(argc, argv, "?f:p:h:dDE:")) != -1) {
|
||||
switch (ch) {
|
||||
case 'f':
|
||||
configFile = myoptarg;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
if (ret == WS_SUCCESS) {
|
||||
ret = XATOI(myoptarg);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, "Issue parsing port number %s\n",
|
||||
myoptarg);
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
else {
|
||||
if (ret <= (word16)-1) {
|
||||
port = (word16)ret;
|
||||
ret = WS_SUCCESS;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "Port number %d too big.\n", ret);
|
||||
ret = WS_BAD_ARGUMENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
hostKeyFile = myoptarg;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
debugMode = 1; /* turn on debug mode */
|
||||
break;
|
||||
|
||||
case 'D':
|
||||
isDaemon = 0;
|
||||
break;
|
||||
|
||||
case 'E':
|
||||
logFile = fopen(myoptarg, "ab");
|
||||
if (logFile == NULL) {
|
||||
fprintf(stderr, "Unable to open log file %s\n", myoptarg);
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
case '?':
|
||||
ShowUsage();
|
||||
return WS_SUCCESS;
|
||||
|
||||
default:
|
||||
ShowUsage();
|
||||
return WS_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
ret = wolfSSHD_ConfigLoad(conf, configFile);
|
||||
if (ret != WS_SUCCESS)
|
||||
fprintf(stderr, "Error reading in configure file %s\n", configFile);
|
||||
}
|
||||
|
||||
/* port was not overridden with argument, read from config file */
|
||||
if (ret == WS_SUCCESS && port == 0) {
|
||||
port = wolfSSHD_ConfigGetPort(conf);
|
||||
}
|
||||
|
||||
/* check if host key file was passed in */
|
||||
if (hostKeyFile != NULL) {
|
||||
wolfSSHD_ConfigSetHostKeyFile(conf, hostKeyFile);
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Starting wolfSSH SSHD application");
|
||||
ret = SetupCTX(conf, &ctx);
|
||||
}
|
||||
else {
|
||||
/* TODO: handle error. */
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
auth = wolfSSHD_AuthCreateUser(NULL, conf);
|
||||
if (auth == NULL) {
|
||||
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Issue creating auth struct");
|
||||
ret = WS_MEMORY_E;
|
||||
}
|
||||
}
|
||||
|
||||
/* seperate privlage permisions */
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (wolfSSHD_AuthReducePermissions(auth) != WS_SUCCESS) {
|
||||
wolfSSH_Log(WS_LOG_INFO, "[SSHD] Error lowering permissions level");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if (logFile == NULL) {
|
||||
logFile = stderr;
|
||||
}
|
||||
|
||||
/* run as a daemon */
|
||||
if (ret == WS_SUCCESS && isDaemon) {
|
||||
#ifdef __unix__
|
||||
/* Daemonizing in POSIX, so set a syslog based log */
|
||||
wolfSSH_SetLoggingCb(SyslogCb);
|
||||
#endif
|
||||
pid_t p;
|
||||
|
||||
p = fork();
|
||||
if (p < 0) {
|
||||
fprintf(stderr, "Failed to fork process\n");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
|
||||
if (ret == WS_SUCCESS) {
|
||||
if (p > 0) {
|
||||
exit(0); /* stop parent process */
|
||||
}
|
||||
|
||||
if (setsid() < 0) {
|
||||
fprintf(stderr, "Failed to set a new session");
|
||||
ret = WS_FATAL_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
signal(SIGCHLD, ConnClose);
|
||||
if (ret == WS_SUCCESS) {
|
||||
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);
|
||||
|
||||
/* wait for incoming connections and fork them off */
|
||||
while (ret == WS_SUCCESS && quit == 0) {
|
||||
WOLFSSHD_CONNECTION conn;
|
||||
#ifdef WOLFSSL_NUCLEUS
|
||||
struct addr_struct clientAddr;
|
||||
#else
|
||||
SOCKADDR_IN_T clientAddr;
|
||||
socklen_t clientAddrSz = sizeof(clientAddr);
|
||||
#endif
|
||||
conn.auth = auth;
|
||||
|
||||
/* wait for a connection */
|
||||
if (PendingConnection(listenFd)) {
|
||||
conn.ctx = ctx;
|
||||
#ifdef WOLFSSL_NUCLEUS
|
||||
conn.fd = NU_Accept(listenFd, &clientAddr, 0);
|
||||
#else
|
||||
conn.fd = accept(listenFd, (struct sockaddr*)&clientAddr,
|
||||
&clientAddrSz);
|
||||
if (conn.fd >= 0) {
|
||||
inet_ntop(AF_INET, &clientAddr.sin_addr, conn.ip,
|
||||
INET_ADDRSTRLEN);
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
#ifdef USE_WINDOWS_API
|
||||
unsigned long blocking = 1;
|
||||
int ret = ioctlsocket(conn.fd, FIONBIO, &blocking);
|
||||
if (ret == SOCKET_ERROR)
|
||||
err_sys("ioctlsocket failed");
|
||||
#elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \
|
||||
|| defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS) || \
|
||||
defined(WOLFSSL_NUCLEUS)
|
||||
/* non blocking not supported, for now */
|
||||
#else
|
||||
int flags = fcntl(conn.fd, F_GETFL, 0);
|
||||
if (flags < 0)
|
||||
err_sys("fcntl get failed");
|
||||
flags = fcntl(conn.fd, F_SETFL, flags | O_NONBLOCK);
|
||||
if (flags < 0)
|
||||
err_sys("fcntl set failed");
|
||||
#endif
|
||||
}
|
||||
ret = NewConnection(&conn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CleanupCTX(conf, &ctx);
|
||||
wolfSSHD_ConfigFree(conf);
|
||||
wolfSSHD_AuthFreeUser(auth);
|
||||
wolfSSH_Cleanup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* WOLFSSH_SSHD */
|
||||
62
configure.ac
62
configure.ac
|
|
@ -113,8 +113,8 @@ AC_ARG_WITH(wolfssl,
|
|||
if test -d "${withval}/lib" && test -d "${withval}/include"; then
|
||||
wcpath=${withval}
|
||||
|
||||
LDFLAGS="$LDFLAGS -L${wcpath}/lib"
|
||||
CPPFLAGS="$CPPFLAGS -I${wcpath}/include"
|
||||
LDFLAGS="-L${wcpath}/lib $LDFLAGS"
|
||||
CPPFLAGS="-I${wcpath}/include $CPPFLAGS"
|
||||
else
|
||||
AC_MSG_ERROR([wolfSSL path error (${withval}): missing lib and include])
|
||||
fi
|
||||
|
|
@ -171,6 +171,11 @@ AC_ARG_ENABLE([sftp],
|
|||
[AS_HELP_STRING([--enable-sftp],[Enable SFTP support (default: disabled)])],
|
||||
[ENABLED_SFTP=$enableval],[ENABLED_SFTP=no])
|
||||
|
||||
# SSHD
|
||||
AC_ARG_ENABLE([sshd],
|
||||
[AS_HELP_STRING([--enable-sshd],[Enable SSHD support (default: disabled)])],
|
||||
[ENABLED_SSHD=$enableval],[ENABLED_SSHD=no])
|
||||
|
||||
# TCP/IP Forwarding
|
||||
AC_ARG_ENABLE([fwd],
|
||||
[AS_HELP_STRING([--enable-fwd],[Enable TCP/IP Forwarding support (default: disabled)])],
|
||||
|
|
@ -196,6 +201,12 @@ AC_ARG_ENABLE([smallstack],
|
|||
[AS_HELP_STRING([--enable-smallstack],[Enable small stack (default: disabled)])],
|
||||
[ENABLED_SMALLSTACK=$enableval],[ENABLED_SMALLSTACK=no])
|
||||
|
||||
# use PAM lib
|
||||
AC_ARG_WITH([pam],
|
||||
[AS_HELP_STRING([--with-pam=PATH],[PATH to directory with the PAM library])],
|
||||
[PAM_LIB=$withval],
|
||||
[PAM_LIB=""])
|
||||
|
||||
# Enable All
|
||||
AC_ARG_ENABLE([all],
|
||||
[AS_HELP_STRING([--enable-all],[Enable all wolfSSH features (default: disabled)])],
|
||||
|
|
@ -210,7 +221,9 @@ AC_ARG_ENABLE([distro],
|
|||
AS_IF([test "x$ENABLED_DISTRO" = "xyes"],
|
||||
[ENABLED_ALL=yes; enable_shared=yes; enable_static=yes])
|
||||
AS_IF([test "x$ENABLED_ALL" = "xyes"],
|
||||
[ENABLED_KEYGEN=yes; ENABLED_SCP=yes; ENABLED_SFTP=yes; ENABLED_FWD=yes; ENABLED_SHELL=yes; ENABLED_AGENT=yes])
|
||||
[ENABLED_KEYGEN=yes; ENABLED_SCP=yes; ENABLED_SFTP=yes; ENABLED_FWD=yes; ENABLED_SHELL=yes; ENABLED_AGENT=yes; ENABLED_SSHD=yes])
|
||||
AS_IF([test "x$ENABLED_SSHD" = "xyes"],
|
||||
[ENABLED_SHELL=yes])
|
||||
|
||||
# Set the defined flags for the code.
|
||||
AS_IF([test "x$ENABLED_INLINE" = "xno"],
|
||||
|
|
@ -230,6 +243,47 @@ AS_IF([test "x$ENABLED_SHELL" = "xyes"],
|
|||
AS_IF([test "x$ENABLED_AGENT" = "xyes"],[AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFSSH_AGENT"])
|
||||
AS_IF([test "x$ENABLED_SMALLSTACK" = "xyes"],
|
||||
[AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFSSH_SMALL_STACK"])
|
||||
AS_IF([test "x$ENABLED_SSHD" = "xyes"],
|
||||
[AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFSSH_SSHD"])
|
||||
|
||||
if test "$ENABLED_SSHD" = "yes"; then
|
||||
if test -n "$PAM_LIB"
|
||||
then
|
||||
AC_MSG_CHECKING([for directory $PAM_LIB])
|
||||
if ! test -d "$PAM_LIB"
|
||||
then
|
||||
AC_MSG_ERROR([PAM lib dir $PAM_LIB not found.])
|
||||
fi
|
||||
AC_MSG_RESULT([yes])
|
||||
AM_LDFLAGS="-L$PAM_LIB $AM_LDFLAGS"
|
||||
|
||||
LIBS="$LIBS -lpam"
|
||||
AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFSSH_USE_PAM"
|
||||
#TODO check on link to lib
|
||||
#AC_CHECK_LIB([pam], [pam],
|
||||
# [AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFSSH_USE_PAM"; LIBS="$LIBS -lpam"],
|
||||
# [AC_MSG_ERROR(libpam not found)])
|
||||
else
|
||||
case $host in
|
||||
*qnx*)
|
||||
# QNX uses liblogin for crypt operation
|
||||
AC_CHECK_LIB([login], [login],
|
||||
[AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFSSH_HAVE_LIBLOGIN";
|
||||
LIBS="$LIBS -llogin"],
|
||||
[AC_MSG_ERROR(liblogin not found)])
|
||||
;;
|
||||
*darwin*)
|
||||
AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFSSH_HAVE_LIBCRYPT"
|
||||
;;
|
||||
*)
|
||||
AC_CHECK_LIB([crypt], [crypt],
|
||||
[AM_CPPFLAGS="$AM_CPPFLAGS -DWOLFSSH_HAVE_LIBCRYPT";
|
||||
LIBS="$LIBS -lcrypt"],
|
||||
[AC_MSG_ERROR(libcrypt not found)])
|
||||
esac
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# Set the automake conditionals.
|
||||
AM_CONDITIONAL([BUILD_EXAMPLE_SERVERS],[test "x$ENABLED_EXAMPLES" = "xyes"])
|
||||
|
|
@ -242,6 +296,7 @@ AM_CONDITIONAL([BUILD_FWD],[test "x$ENABLED_FWD" = "xyes"])
|
|||
AM_CONDITIONAL([BUILD_TERM],[test "x$ENABLED_TERM" = "xyes"])
|
||||
AM_CONDITIONAL([BUILD_SHELL],[test "x$ENABLED_SHELL" = "xyes"])
|
||||
AM_CONDITIONAL([BUILD_AGENT],[test "x$ENABLED_AGENT" = "xyes"])
|
||||
AM_CONDITIONAL([BUILD_SSHD],[test "x$ENABLED_SSHD" = "xyes"])
|
||||
|
||||
AX_HARDEN_CC_COMPILER_FLAGS
|
||||
|
||||
|
|
@ -282,6 +337,7 @@ AS_ECHO([" * psuedo-terminal: $ENABLED_PTERM"])
|
|||
AS_ECHO([" * echoserver shell support: $ENABLED_SHELL"])
|
||||
AS_ECHO([" * scp: $ENABLED_SCP"])
|
||||
AS_ECHO([" * sftp: $ENABLED_SFTP"])
|
||||
AS_ECHO([" * sshd: $ENABLED_SSHD"])
|
||||
AS_ECHO([" * agent: $ENABLED_AGENT"])
|
||||
AS_ECHO([" * TCP/IP Forwarding: $ENABLED_FWD"])
|
||||
AS_ECHO([" * Examples: $ENABLED_EXAMPLES"])
|
||||
|
|
|
|||
|
|
@ -69,6 +69,12 @@
|
|||
#endif
|
||||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#if defined(__QNX__) || defined(__QNXNTO__)
|
||||
#include <errno.h>
|
||||
#include <unix.h>
|
||||
#else
|
||||
#include <sys/errno.h>
|
||||
#endif
|
||||
#endif /* WOLFSSH_SHELL */
|
||||
|
||||
#ifdef WOLFSSH_AGENT
|
||||
|
|
|
|||
10
src/log.c
10
src/log.c
|
|
@ -57,7 +57,7 @@
|
|||
#endif /* WOLFSSH_NO_DEFAULT_LOGGING_CB */
|
||||
|
||||
|
||||
#ifdef DEBUG_WOLFSSH
|
||||
#if defined(DEBUG_WOLFSSH) || defined(WOLFSSH_SSHD)
|
||||
static enum wolfSSH_LogLevel logLevel = WS_LOG_DEFAULT;
|
||||
static int logEnable = 0;
|
||||
#endif
|
||||
|
|
@ -66,7 +66,7 @@
|
|||
/* turn debugging on if supported */
|
||||
void wolfSSH_Debugging_ON(void)
|
||||
{
|
||||
#ifdef DEBUG_WOLFSSH
|
||||
#if defined(DEBUG_WOLFSSH) || defined(WOLFSSH_SSHD)
|
||||
logEnable = 1;
|
||||
#endif
|
||||
}
|
||||
|
|
@ -75,7 +75,7 @@ void wolfSSH_Debugging_ON(void)
|
|||
/* turn debugging off */
|
||||
void wolfSSH_Debugging_OFF(void)
|
||||
{
|
||||
#ifdef DEBUG_WOLFSSH
|
||||
#if defined(DEBUG_WOLFSSH) || defined(WOLFSSH_SSHD)
|
||||
logEnable = 0;
|
||||
#endif
|
||||
}
|
||||
|
|
@ -91,7 +91,7 @@ void wolfSSH_SetLoggingCb(wolfSSH_LoggingCb logF)
|
|||
|
||||
int wolfSSH_LogEnabled(void)
|
||||
{
|
||||
#ifdef DEBUG_WOLFSSH
|
||||
#if defined(DEBUG_WOLFSSH) || defined(WOLFSSH_SSHD)
|
||||
return logEnable;
|
||||
#else
|
||||
return 0;
|
||||
|
|
@ -99,7 +99,7 @@ int wolfSSH_LogEnabled(void)
|
|||
}
|
||||
|
||||
|
||||
#ifdef DEBUG_WOLFSSH
|
||||
#if defined(DEBUG_WOLFSSH) || defined(WOLFSSH_SSHD)
|
||||
#ifndef WOLFSSH_NO_DEFAULT_LOGGING_CB
|
||||
/* log level string */
|
||||
static const char* GetLogStr(enum wolfSSH_LogLevel level)
|
||||
|
|
|
|||
|
|
@ -978,7 +978,8 @@ enum WS_DynamicTypes {
|
|||
DYNTYPE_AGENT_BUFFER,
|
||||
DYNTYPE_FILE,
|
||||
DYNTYPE_TEMP,
|
||||
DYNTYPE_PATH
|
||||
DYNTYPE_PATH,
|
||||
DYNTYPE_SSHD
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,15 @@ extern "C" {
|
|||
/* This value needs to stay in sync with the actual value of DYNTYPE_STRING
|
||||
* from internal.h. */
|
||||
|
||||
#ifdef WOLFSSH_SSHD
|
||||
/* uses isspace (not always available from wolfSSL) */
|
||||
#ifdef XISSPACE
|
||||
#define WISSPACE XISSPACE
|
||||
#else
|
||||
#define WISSPACE isspace
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* setup memory handling */
|
||||
#ifndef WMALLOC_USER
|
||||
#ifdef WOLFSSL_USER_SETTINGS
|
||||
|
|
|
|||
Loading…
Reference in New Issue