mirror of
https://github.com/D-Modem/gui.git
synced 2026-11-28 14:19:58 -08:00
Add libdmodemssh: an SSH client on libssh for every app
One engine in C on libssh 0.11 (with mbedTLS, both built from their release tarballs where the system has no libssh 0.11): host keys for the caller to trust (and OpenSSH's known_hosts when asked), logins by password, keyboard-interactive, a key, the agent or OpenSSH's defaults, a shell on a pty, commands, direct channels, remote listens and the server's agent channels; keys made, read and re-encrypted. Its tests log in to a real sshd when DMODEM_SSH_TEST names one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRSV65geuGd6zMt2V6r
This commit is contained in:
7 files changed
+1981
No files matched your search
@@ -14,6 +14,7 @@ option(DMODEM_GUI_LINUX "Build the GTK 4 app (linux/)" ON)
|
||||
add_subdirectory(core)
|
||||
enable_testing()
|
||||
add_subdirectory(core/test)
|
||||
add_subdirectory(ssh)
|
||||
|
||||
if(WIN32)
|
||||
add_subdirectory(windows)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# libdmodemssh: D-Modem's SSH client (dmodemssh.h), on libssh 0.11.
|
||||
# The system's libssh when it is 0.11 or later (and
|
||||
# -DDMODEM_SSH_BUNDLED=OFF), else libssh and mbedTLS built here from
|
||||
# their release tarballs: always for the Windows and macOS apps, which
|
||||
# ship no libraries of the system's.
|
||||
if(WIN32 OR APPLE)
|
||||
set(_bundled ON)
|
||||
else()
|
||||
set(_bundled AUTO)
|
||||
endif()
|
||||
set(DMODEM_SSH_BUNDLED ${_bundled} CACHE STRING "Build libssh and mbedTLS from source (ON, OFF, AUTO: when the system's is older than 0.11)")
|
||||
|
||||
if(NOT DMODEM_SSH_BUNDLED STREQUAL "ON")
|
||||
find_package(PkgConfig)
|
||||
if(PkgConfig_FOUND)
|
||||
pkg_check_modules(LIBSSH IMPORTED_TARGET libssh>=0.11)
|
||||
endif()
|
||||
endif()
|
||||
if(LIBSSH_FOUND)
|
||||
add_library(dmodem_libssh INTERFACE)
|
||||
target_link_libraries(dmodem_libssh INTERFACE PkgConfig::LIBSSH)
|
||||
elseif(DMODEM_SSH_BUNDLED STREQUAL "OFF")
|
||||
message(FATAL_ERROR "libssh 0.11 or later not found (or -DDMODEM_SSH_BUNDLED=AUTO)")
|
||||
else()
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/cmake/BundledLibssh.cmake)
|
||||
endif()
|
||||
|
||||
add_library(dmodemssh STATIC src/dmodemssh.c)
|
||||
target_include_directories(dmodemssh PUBLIC include)
|
||||
target_link_libraries(dmodemssh PUBLIC dmodem_libssh)
|
||||
if(WIN32)
|
||||
target_link_libraries(dmodemssh PUBLIC ws2_32)
|
||||
elseif(UNIX)
|
||||
find_package(Threads REQUIRED)
|
||||
target_link_libraries(dmodemssh PUBLIC Threads::Threads)
|
||||
endif()
|
||||
if(MSVC)
|
||||
target_compile_options(dmodemssh PRIVATE /W4 /wd4996)
|
||||
else()
|
||||
target_compile_options(dmodemssh PRIVATE -Wall -Wextra -Wshadow)
|
||||
endif()
|
||||
|
||||
add_executable(dmodemssh_test test/dmodemssh_test.c)
|
||||
target_link_libraries(dmodemssh_test PRIVATE dmodemssh)
|
||||
add_test(NAME dmodemssh COMMAND dmodemssh_test)
|
||||
@@ -0,0 +1,76 @@
|
||||
# libssh on mbedTLS, both static, built from their release tarballs
|
||||
# (checked by hash), as the target dmodem_libssh.
|
||||
include(FetchContent)
|
||||
include(ExternalProject)
|
||||
|
||||
set(DMODEM_MBEDTLS_URL https://github.com/Mbed-TLS/mbedtls/releases/download/mbedtls-3.6.4/mbedtls-3.6.4.tar.bz2)
|
||||
set(DMODEM_MBEDTLS_SHA256 ec35b18a6c593cf98c3e30db8b98ff93e8940a8c4e690e66b41dfc011d678110)
|
||||
set(DMODEM_LIBSSH_URL https://www.libssh.org/files/0.11/libssh-0.11.3.tar.xz)
|
||||
set(DMODEM_LIBSSH_SHA256 7d8a1361bb094ec3f511964e78a5a4dba689b5986e112afabe4f4d0d6c6125c3)
|
||||
|
||||
set(_prefix ${CMAKE_BINARY_DIR}/dmodem-libssh)
|
||||
set(_args
|
||||
-DCMAKE_BUILD_TYPE=Release
|
||||
-DCMAKE_INSTALL_PREFIX=${_prefix}
|
||||
-DCMAKE_INSTALL_LIBDIR=lib
|
||||
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
|
||||
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
|
||||
-DCMAKE_MSVC_RUNTIME_LIBRARY=${CMAKE_MSVC_RUNTIME_LIBRARY}
|
||||
-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
|
||||
-DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}
|
||||
-DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT})
|
||||
if(CMAKE_TOOLCHAIN_FILE)
|
||||
list(APPEND _args -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE})
|
||||
endif()
|
||||
string(REPLACE ";" "|" _archs "${CMAKE_OSX_ARCHITECTURES}")
|
||||
list(TRANSFORM _args REPLACE "^-DCMAKE_OSX_ARCHITECTURES=.*" "-DCMAKE_OSX_ARCHITECTURES=${_archs}")
|
||||
|
||||
if(WIN32)
|
||||
set(_mbed_libs mbedcrypto.lib mbedx509.lib mbedtls.lib everest.lib p256m.lib)
|
||||
set(_ssh_lib ssh.lib)
|
||||
if(MINGW)
|
||||
set(_mbed_libs libmbedcrypto.a libmbedx509.a libmbedtls.a libeverest.a libp256m.a)
|
||||
set(_ssh_lib libssh.a)
|
||||
endif()
|
||||
else()
|
||||
set(_mbed_libs libmbedcrypto.a libmbedx509.a libmbedtls.a libeverest.a libp256m.a)
|
||||
set(_ssh_lib libssh.a)
|
||||
endif()
|
||||
list(TRANSFORM _mbed_libs PREPEND ${_prefix}/lib/ OUTPUT_VARIABLE _mbed_paths)
|
||||
|
||||
ExternalProject_Add(dmodem_mbedtls
|
||||
URL ${DMODEM_MBEDTLS_URL}
|
||||
URL_HASH SHA256=${DMODEM_MBEDTLS_SHA256}
|
||||
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
|
||||
PATCH_COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/PatchMbedtls.cmake
|
||||
LIST_SEPARATOR |
|
||||
CMAKE_ARGS ${_args} -DENABLE_TESTING=OFF -DENABLE_PROGRAMS=OFF
|
||||
-DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DMBEDTLS_FATAL_WARNINGS=OFF
|
||||
BUILD_BYPRODUCTS ${_mbed_paths})
|
||||
|
||||
ExternalProject_Add(dmodem_libssh_build
|
||||
URL ${DMODEM_LIBSSH_URL}
|
||||
URL_HASH SHA256=${DMODEM_LIBSSH_SHA256}
|
||||
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
|
||||
DEPENDS dmodem_mbedtls
|
||||
LIST_SEPARATOR |
|
||||
CMAKE_ARGS ${_args} -DBUILD_SHARED_LIBS=OFF
|
||||
-DWITH_MBEDTLS=ON -DMBEDTLS_ROOT_DIR=${_prefix}
|
||||
-DCMAKE_PREFIX_PATH=${_prefix}
|
||||
-DWITH_ZLIB=OFF -DWITH_GSSAPI=OFF -DWITH_SERVER=OFF
|
||||
-DWITH_EXAMPLES=OFF -DUNIT_TESTING=OFF -DCLIENT_TESTING=OFF
|
||||
-DWITH_NACL=OFF -DWITH_SYMBOL_VERSIONING=OFF
|
||||
BUILD_BYPRODUCTS ${_prefix}/lib/${_ssh_lib})
|
||||
|
||||
file(MAKE_DIRECTORY ${_prefix}/include)
|
||||
add_library(dmodem_libssh INTERFACE)
|
||||
add_dependencies(dmodem_libssh dmodem_libssh_build)
|
||||
target_include_directories(dmodem_libssh INTERFACE ${_prefix}/include)
|
||||
target_compile_definitions(dmodem_libssh INTERFACE LIBSSH_STATIC=1)
|
||||
target_link_libraries(dmodem_libssh INTERFACE ${_prefix}/lib/${_ssh_lib} ${_mbed_paths})
|
||||
if(WIN32)
|
||||
target_link_libraries(dmodem_libssh INTERFACE ws2_32 iphlpapi bcrypt shell32 advapi32)
|
||||
elseif(UNIX)
|
||||
find_package(Threads REQUIRED)
|
||||
target_link_libraries(dmodem_libssh INTERFACE Threads::Threads)
|
||||
endif()
|
||||
@@ -0,0 +1,24 @@
|
||||
# mbedTLS's config with threading on, as libssh wants it: its own
|
||||
# pthread mutexes, or (Windows) libssh's given through threading_alt.h.
|
||||
# Run in mbedTLS's source tree by BundledLibssh.cmake.
|
||||
set(_config include/mbedtls/mbedtls_config.h)
|
||||
file(READ ${_config} _text)
|
||||
if(NOT _text MATCHES "D-Modem: threading")
|
||||
file(APPEND ${_config} "
|
||||
/* D-Modem: threading, for libssh */
|
||||
#define MBEDTLS_THREADING_C
|
||||
#if defined(_WIN32)
|
||||
#define MBEDTLS_THREADING_ALT
|
||||
#else
|
||||
#define MBEDTLS_THREADING_PTHREAD
|
||||
#endif
|
||||
")
|
||||
endif()
|
||||
file(WRITE include/mbedtls/threading_alt.h "/* D-Modem: libssh's mutexes (a pointer each) */
|
||||
#ifndef DMODEM_THREADING_ALT_H
|
||||
#define DMODEM_THREADING_ALT_H
|
||||
typedef struct mbedtls_threading_mutex_t {
|
||||
void *mutex;
|
||||
} mbedtls_threading_mutex_t;
|
||||
#endif
|
||||
")
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* dmodemssh.h - D-Modem's SSH client, on libssh: the one engine every
|
||||
* app has (the desktops', iOS's, and Android's through JNI), so that
|
||||
* each has the same SSH.
|
||||
*
|
||||
* A connection (dmc_ssh) is driven by one thread that calls
|
||||
* dmc_ssh_poll() in a loop and acts on the events it hands back: the
|
||||
* server's key to trust or not, a login method to choose, a
|
||||
* keyboard-interactive round to answer, a channel's data. Every other
|
||||
* call may come from any thread (a lock is taken): what is typed, a
|
||||
* window's new size, the answers.
|
||||
*
|
||||
* What it has: host keys checked by the caller (and in OpenSSH's
|
||||
* known_hosts when asked); logins by password, keyboard-interactive, a
|
||||
* private key (OpenSSH's, PEM), the system's agent, or OpenSSH's own
|
||||
* keys and agent at once (ssh's default); ~/.ssh/config when asked; Ed25519,
|
||||
* ECDSA and RSA keys; a shell on a pty, a command, direct TCP channels
|
||||
* (ssh -L and -D), remote listens (ssh -R) and the server's
|
||||
* agent channels (ssh -A) handed to the caller as channels.
|
||||
*
|
||||
* This file and dmodemssh.c are the same in D-Modem/gui (ssh/) and
|
||||
* D-Modem/app (native-ssh/): change both.
|
||||
*/
|
||||
#ifndef DMODEMSSH_H
|
||||
#define DMODEMSSH_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct dmc_ssh dmc_ssh;
|
||||
|
||||
/* what dmc_ssh_poll() hands back */
|
||||
enum {
|
||||
DMC_SSH_EV_HOSTKEY = 1, /* the server's key: data is its OpenSSH line
|
||||
("ssh-ed25519 AAAA..."), value a
|
||||
DMC_SSH_HOST_*; answer dmc_ssh_trust() */
|
||||
DMC_SSH_EV_AUTH, /* a login method wanted: value is the
|
||||
DMC_SSH_AUTH_* the server takes, data why
|
||||
the last failed ("" the first time);
|
||||
answer one dmc_ssh_auth_*() */
|
||||
DMC_SSH_EV_PROMPTS, /* the server's questions (keyboard-
|
||||
interactive): value how many, data the
|
||||
round's name; read them with
|
||||
dmc_ssh_prompt(), answer each with
|
||||
dmc_ssh_answer(), then dmc_ssh_answered() */
|
||||
DMC_SSH_EV_READY, /* logged in: data the server's banner, if any */
|
||||
DMC_SSH_EV_OPEN, /* a channel opened (value 1) or refused
|
||||
(value 0, data why) */
|
||||
DMC_SSH_EV_DATA, /* a channel's data (value 1: its stderr) */
|
||||
DMC_SSH_EV_EOF, /* a channel's end of data */
|
||||
DMC_SSH_EV_EXIT, /* a command's exit status (value), or the
|
||||
signal that ended it (data) */
|
||||
DMC_SSH_EV_CLOSE, /* a channel closed: its number is free */
|
||||
DMC_SSH_EV_INCOMING, /* a channel the server opened, open now:
|
||||
value DMC_SSH_IN_AGENT, or the remote
|
||||
listen's port (data: who connected) */
|
||||
DMC_SSH_EV_LISTEN, /* a remote listen (channel: its request's
|
||||
number) answered: value the port bound,
|
||||
-1 refused */
|
||||
DMC_SSH_EV_CLOSED /* the connection over: data why (NULL when
|
||||
dmc_ssh_disconnect() asked) */
|
||||
};
|
||||
|
||||
typedef struct dmc_ssh_event {
|
||||
int type;
|
||||
int channel; /* the channel it's about (1 and up), or 0 */
|
||||
int value;
|
||||
const char *data; /* until the next dmc_ssh_poll() */
|
||||
int len;
|
||||
} dmc_ssh_event;
|
||||
|
||||
/* the server's key against OpenSSH's known_hosts (when the
|
||||
"knownhosts" option was given) */
|
||||
enum {
|
||||
DMC_SSH_HOST_UNCHECKED = 0, DMC_SSH_HOST_KNOWN, DMC_SSH_HOST_NEW,
|
||||
DMC_SSH_HOST_CHANGED, DMC_SSH_HOST_OTHER_TYPE
|
||||
};
|
||||
|
||||
/* the login methods the server takes */
|
||||
enum {
|
||||
DMC_SSH_AUTH_PASSWORD = 1, DMC_SSH_AUTH_PUBLICKEY = 2,
|
||||
DMC_SSH_AUTH_INTERACTIVE = 4
|
||||
};
|
||||
|
||||
/* a server-opened channel that is the agent's (ssh -A) */
|
||||
enum { DMC_SSH_IN_AGENT = -1 };
|
||||
|
||||
/* a connection to make to user@host:port (nothing done until polled) */
|
||||
dmc_ssh *dmc_ssh_new(const char *host, int port, const char *user);
|
||||
/* the connection dropped (if it's up) and freed */
|
||||
void dmc_ssh_free(dmc_ssh *s);
|
||||
/* an option, before the first poll: 0, or -1 if it's not one.
|
||||
"config" OpenSSH's config read (a path; "" its own ~/.ssh/config)
|
||||
"knownhosts" the host's key looked up there (a path; "" OpenSSH's)
|
||||
"hostkeys" the host key algorithms, in order (a list with commas)
|
||||
"timeout" seconds to connect in
|
||||
"compression" "yes" or "no" */
|
||||
int dmc_ssh_option(dmc_ssh *s, const char *name, const char *value);
|
||||
/* the next event: 1 (in *ev), 0 when timeout_ms passed with none, -1
|
||||
when the connection is over and its DMC_SSH_EV_CLOSED was handed back */
|
||||
int dmc_ssh_poll(dmc_ssh *s, int timeout_ms, dmc_ssh_event *ev);
|
||||
/* the connection closed by the user: a DMC_SSH_EV_CLOSED with NULL comes */
|
||||
void dmc_ssh_disconnect(dmc_ssh *s);
|
||||
|
||||
/* the server's key: trusted (ok) or not; remember: added to known_hosts */
|
||||
void dmc_ssh_trust(dmc_ssh *s, int ok, int remember);
|
||||
/* a login tried (an answer to DMC_SSH_EV_AUTH) */
|
||||
void dmc_ssh_auth_password(dmc_ssh *s, const char *password);
|
||||
/* by a private key's text (OpenSSH's, PEM): 0, or -1 when it can't be
|
||||
read with that passphrase (nothing is tried: answer again) */
|
||||
int dmc_ssh_auth_key(dmc_ssh *s, const char *key, const char *passphrase);
|
||||
/* by the system's agent (SSH_AUTH_SOCK) */
|
||||
void dmc_ssh_auth_agent(dmc_ssh *s);
|
||||
/* as OpenSSH's ssh does: the agent, then ~/.ssh's keys with no passphrase */
|
||||
void dmc_ssh_auth_auto(dmc_ssh *s);
|
||||
/* by the server's questions (keyboard-interactive) */
|
||||
void dmc_ssh_auth_interactive(dmc_ssh *s);
|
||||
/* no more tries: the connection closes, "not logged in" */
|
||||
void dmc_ssh_auth_cancel(dmc_ssh *s);
|
||||
/* a DMC_SSH_EV_PROMPTS round: its instruction, and question i (*echo:
|
||||
whether the answer may be shown) */
|
||||
const char *dmc_ssh_instruction(dmc_ssh *s);
|
||||
const char *dmc_ssh_prompt(dmc_ssh *s, int i, int *echo);
|
||||
void dmc_ssh_answer(dmc_ssh *s, int i, const char *answer);
|
||||
void dmc_ssh_answered(dmc_ssh *s);
|
||||
|
||||
/* channels, once ready: each a number (1 and up), or -1; a
|
||||
DMC_SSH_EV_OPEN tells whether it opened */
|
||||
/* a shell on a pty of term in the given size; agent: ssh -A asked */
|
||||
int dmc_ssh_shell(dmc_ssh *s, const char *term, int cols, int rows, int agent);
|
||||
/* a command run, with no pty */
|
||||
int dmc_ssh_exec(dmc_ssh *s, const char *command);
|
||||
/* a TCP connection made from the server to host:port (ssh -L, -D), for
|
||||
a connection that came from from_host:from_port */
|
||||
int dmc_ssh_direct(dmc_ssh *s, const char *host, int port,
|
||||
const char *from_host, int from_port);
|
||||
/* the server asked to listen on address:port (ssh -R; NULL: its
|
||||
default, 0: a port it picks): a request's number, answered by a
|
||||
DMC_SSH_EV_LISTEN; its connections come as DMC_SSH_EV_INCOMING */
|
||||
int dmc_ssh_listen(dmc_ssh *s, const char *address, int port);
|
||||
/* that listen stopped */
|
||||
void dmc_ssh_unlisten(dmc_ssh *s, const char *address, int port);
|
||||
/* data to a channel (kept until the server can take it): 0, or -1 if
|
||||
the channel isn't open */
|
||||
int dmc_ssh_write(dmc_ssh *s, int channel, const char *data, int len);
|
||||
/* the channel's end of data sent */
|
||||
void dmc_ssh_eof(dmc_ssh *s, int channel);
|
||||
/* the channel closed (a DMC_SSH_EV_CLOSE comes) */
|
||||
void dmc_ssh_close(dmc_ssh *s, int channel);
|
||||
/* a shell's pty resized */
|
||||
void dmc_ssh_resize(dmc_ssh *s, int channel, int cols, int rows);
|
||||
|
||||
/* keys */
|
||||
/* OpenSSH's SHA256 fingerprint of a key's OpenSSH line
|
||||
("SHA256:..."), into buf: 0, or -1 if it isn't a key */
|
||||
int dmc_ssh_fingerprint(const char *keyline, char *buf, int max);
|
||||
/* a new key ("ed25519", "ecdsa" with bits 256, 384 or 521, "rsa" with
|
||||
bits): its private text (OpenSSH's, encrypted with passphrase unless
|
||||
it's NULL or "") and its public line, both to free with
|
||||
dmc_ssh_free_string(): 0, or -1 */
|
||||
int dmc_ssh_keygen(const char *type, int bits, const char *passphrase,
|
||||
char **private_key, char **public_key);
|
||||
/* a private key's public line: 0, or -1 if it can't be read with that
|
||||
passphrase */
|
||||
int dmc_ssh_key_public(const char *key, const char *passphrase,
|
||||
char **public_key);
|
||||
/* a private key written again (OpenSSH's form) with a new passphrase
|
||||
(NULL or "": none): 0, or -1 */
|
||||
int dmc_ssh_key_export(const char *key, const char *passphrase,
|
||||
const char *new_passphrase, char **out);
|
||||
void dmc_ssh_free_string(char *str);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+1296
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,358 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (C) 2026 D-Modem Authors
|
||||
*
|
||||
* dmodemssh_test.c - libdmodemssh's tests: keys (always), and a login to
|
||||
* a real sshd when DMODEM_SSH_TEST names one as host:port:user:password
|
||||
* (a shell, a command, a direct channel and a remote listen on it).
|
||||
* DMODEM_SSH_TEST_KEY, a private key's file that the user's
|
||||
* authorized_keys has, adds a login by key.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#define closesocket close
|
||||
#endif
|
||||
|
||||
#include "dmodemssh.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(c) do { if (!(c)) { \
|
||||
fprintf(stderr, "%s:%d: failed: %s\n", __FILE__, __LINE__, #c); \
|
||||
failures++; } } while (0)
|
||||
|
||||
/* a key made, read back, its fingerprint, and with a passphrase */
|
||||
static void test_keys(void)
|
||||
{
|
||||
static const char *types[] = { "ed25519", "ecdsa", "rsa" };
|
||||
static const int bits[] = { 0, 384, 2048 };
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
char *priv = NULL, *pub = NULL, *pub2 = NULL, *enc = NULL;
|
||||
char fp[128];
|
||||
|
||||
CHECK(dmc_ssh_keygen(types[i], bits[i], NULL, &priv, &pub) == 0);
|
||||
if (!priv || !pub)
|
||||
continue;
|
||||
CHECK(strstr(priv, "BEGIN OPENSSH PRIVATE KEY") != NULL);
|
||||
CHECK(dmc_ssh_key_public(priv, NULL, &pub2) == 0);
|
||||
CHECK(pub2 && !strcmp(pub, pub2));
|
||||
CHECK(dmc_ssh_fingerprint(pub, fp, sizeof fp) == 0);
|
||||
CHECK(!strncmp(fp, "SHA256:", 7) && strlen(fp) == 50);
|
||||
CHECK(dmc_ssh_key_export(priv, NULL, "sesame", &enc) == 0);
|
||||
dmc_ssh_free_string(pub2);
|
||||
pub2 = NULL;
|
||||
CHECK(enc && dmc_ssh_key_public(enc, "wrong", &pub2) < 0);
|
||||
CHECK(enc && dmc_ssh_key_public(enc, "sesame", &pub2) == 0);
|
||||
CHECK(pub2 && !strcmp(pub, pub2));
|
||||
dmc_ssh_free_string(priv);
|
||||
dmc_ssh_free_string(pub);
|
||||
dmc_ssh_free_string(pub2);
|
||||
dmc_ssh_free_string(enc);
|
||||
}
|
||||
{
|
||||
char fp[128];
|
||||
CHECK(dmc_ssh_fingerprint("ssh-ed25519 notbase64!", fp, sizeof fp) < 0);
|
||||
}
|
||||
}
|
||||
|
||||
static const char *host, *user, *password;
|
||||
static int port;
|
||||
|
||||
/* the next event (10 s at most): its type, or 0 */
|
||||
static int next(dmc_ssh *s, dmc_ssh_event *ev)
|
||||
{
|
||||
int r = dmc_ssh_poll(s, 10000, ev);
|
||||
|
||||
if (r <= 0) {
|
||||
fprintf(stderr, "no event (%d)\n", r);
|
||||
return 0;
|
||||
}
|
||||
return ev->type;
|
||||
}
|
||||
|
||||
/* a connection logged in by password (a wrong one first): or NULL */
|
||||
static dmc_ssh *logged_in(void)
|
||||
{
|
||||
dmc_ssh *s = dmc_ssh_new(host, port, user);
|
||||
dmc_ssh_event ev;
|
||||
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_HOSTKEY);
|
||||
CHECK(!strncmp(ev.data, "ssh-", 4) || !strncmp(ev.data, "ecdsa-", 6));
|
||||
CHECK(ev.value == DMC_SSH_HOST_UNCHECKED);
|
||||
dmc_ssh_trust(s, 1, 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_AUTH);
|
||||
CHECK(ev.value & DMC_SSH_AUTH_PASSWORD);
|
||||
CHECK(!strcmp(ev.data, ""));
|
||||
dmc_ssh_auth_password(s, "not it");
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_AUTH);
|
||||
CHECK(!strcmp(ev.data, "The password was not accepted"));
|
||||
dmc_ssh_auth_password(s, password);
|
||||
if (next(s, &ev) != DMC_SSH_EV_READY) {
|
||||
CHECK(!"logged in");
|
||||
dmc_ssh_free(s);
|
||||
return NULL;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/* a channel's output read until it closes: into out; its exit status */
|
||||
static int drain(dmc_ssh *s, int ch, char *out, int max)
|
||||
{
|
||||
dmc_ssh_event ev;
|
||||
int len = 0, status = -1000;
|
||||
|
||||
for (;;) {
|
||||
int t = next(s, &ev);
|
||||
|
||||
if (!t || t == DMC_SSH_EV_CLOSED)
|
||||
break;
|
||||
if (ev.channel != ch)
|
||||
continue;
|
||||
if (t == DMC_SSH_EV_DATA && len + ev.len < max) {
|
||||
memcpy(out + len, ev.data, (size_t)ev.len);
|
||||
len += ev.len;
|
||||
} else if (t == DMC_SSH_EV_EXIT) {
|
||||
status = ev.value;
|
||||
} else if (t == DMC_SSH_EV_CLOSE) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
out[len] = 0;
|
||||
return status;
|
||||
}
|
||||
|
||||
/* a shell on a pty: its size, a command typed, its exit */
|
||||
static void test_shell(dmc_ssh *s)
|
||||
{
|
||||
dmc_ssh_event ev;
|
||||
char out[8192];
|
||||
int ch = dmc_ssh_shell(s, "vt100", 100, 30, 0);
|
||||
static const char typed[] = "stty size; echo $TERM; exit 3\n";
|
||||
|
||||
CHECK(ch > 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_OPEN && ev.channel == ch && ev.value == 1);
|
||||
CHECK(dmc_ssh_write(s, ch, typed, (int)strlen(typed)) == 0);
|
||||
CHECK(drain(s, ch, out, sizeof out) == 3);
|
||||
CHECK(strstr(out, "30 100") != NULL);
|
||||
CHECK(strstr(out, "vt100") != NULL);
|
||||
CHECK(dmc_ssh_write(s, ch, "x", 1) < 0);
|
||||
}
|
||||
|
||||
/* a command: its output, stdin, and exit status */
|
||||
static void test_exec(dmc_ssh *s)
|
||||
{
|
||||
dmc_ssh_event ev;
|
||||
char out[8192];
|
||||
int ch = dmc_ssh_exec(s, "tr a-z A-Z; exit 7");
|
||||
|
||||
CHECK(ch > 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_OPEN && ev.value == 1);
|
||||
CHECK(dmc_ssh_write(s, ch, "hello\n", 6) == 0);
|
||||
dmc_ssh_eof(s, ch);
|
||||
CHECK(drain(s, ch, out, sizeof out) == 7);
|
||||
CHECK(!strcmp(out, "HELLO\n"));
|
||||
}
|
||||
|
||||
/* a direct channel to the sshd itself: its banner */
|
||||
static void test_direct(dmc_ssh *s)
|
||||
{
|
||||
dmc_ssh_event ev;
|
||||
int ch = dmc_ssh_direct(s, "127.0.0.1", port, "127.0.0.1", 40000);
|
||||
int t;
|
||||
|
||||
CHECK(ch > 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_OPEN && ev.value == 1);
|
||||
do {
|
||||
t = next(s, &ev);
|
||||
} while (t && t != DMC_SSH_EV_DATA);
|
||||
CHECK(t == DMC_SSH_EV_DATA && !strncmp(ev.data, "SSH-2.0-", 8));
|
||||
dmc_ssh_close(s, ch);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_CLOSE && ev.channel == ch);
|
||||
}
|
||||
|
||||
/* a remote listen on a port the server picks, connected to from here */
|
||||
static void test_listen(dmc_ssh *s)
|
||||
{
|
||||
dmc_ssh_event ev;
|
||||
int id = dmc_ssh_listen(s, "127.0.0.1", 0), bound, ch, t;
|
||||
struct sockaddr_in a;
|
||||
int fd;
|
||||
char b[16];
|
||||
|
||||
CHECK(id > 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_LISTEN && ev.channel == id);
|
||||
bound = ev.value;
|
||||
CHECK(bound > 0);
|
||||
if (bound <= 0)
|
||||
return;
|
||||
fd = (int)socket(AF_INET, SOCK_STREAM, 0);
|
||||
memset(&a, 0, sizeof a);
|
||||
a.sin_family = AF_INET;
|
||||
a.sin_port = htons((unsigned short)bound);
|
||||
a.sin_addr.s_addr = htonl(0x7f000001);
|
||||
CHECK(connect(fd, (struct sockaddr *)&a, sizeof a) == 0);
|
||||
send(fd, "ping", 4, 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_INCOMING && ev.value == bound);
|
||||
ch = ev.channel;
|
||||
do {
|
||||
t = next(s, &ev);
|
||||
} while (t && t != DMC_SSH_EV_DATA);
|
||||
CHECK(t == DMC_SSH_EV_DATA && ev.channel == ch && ev.len == 4 &&
|
||||
!memcmp(ev.data, "ping", 4));
|
||||
CHECK(dmc_ssh_write(s, ch, "pong", 4) == 0);
|
||||
{
|
||||
/* (the reply comes as the session is polled) */
|
||||
int got = 0, i;
|
||||
for (i = 0; i < 40 && got < 4; i++) {
|
||||
dmc_ssh_poll(s, 50, &ev);
|
||||
#ifdef _WIN32
|
||||
{
|
||||
u_long n = 0;
|
||||
ioctlsocket(fd, FIONREAD, &n);
|
||||
if (n >= 4)
|
||||
got = recv(fd, b, 4, 0);
|
||||
}
|
||||
#else
|
||||
got = (int)recv(fd, b, 4, MSG_DONTWAIT);
|
||||
if (got < 0)
|
||||
got = 0;
|
||||
#endif
|
||||
}
|
||||
CHECK(got == 4 && !memcmp(b, "pong", 4));
|
||||
}
|
||||
closesocket(fd);
|
||||
dmc_ssh_close(s, ch);
|
||||
dmc_ssh_unlisten(s, "127.0.0.1", bound);
|
||||
}
|
||||
|
||||
/* a login by a key in a file */
|
||||
static void test_key_login(const char *file)
|
||||
{
|
||||
FILE *f = fopen(file, "rb");
|
||||
char key[16384];
|
||||
size_t n;
|
||||
dmc_ssh *s;
|
||||
dmc_ssh_event ev;
|
||||
|
||||
CHECK(f != NULL);
|
||||
if (!f)
|
||||
return;
|
||||
n = fread(key, 1, sizeof key - 1, f);
|
||||
key[n] = 0;
|
||||
fclose(f);
|
||||
s = dmc_ssh_new(host, port, user);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_HOSTKEY);
|
||||
dmc_ssh_trust(s, 1, 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_AUTH && (ev.value & DMC_SSH_AUTH_PUBLICKEY));
|
||||
CHECK(dmc_ssh_auth_key(s, key, NULL) == 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_READY);
|
||||
dmc_ssh_free(s);
|
||||
}
|
||||
|
||||
/* the logins and channels against DMODEM_SSH_TEST's sshd */
|
||||
static void test_live(const char *spec)
|
||||
{
|
||||
static char buf[512];
|
||||
char *p[4];
|
||||
int i;
|
||||
dmc_ssh *s;
|
||||
dmc_ssh_event ev;
|
||||
const char *key = getenv("DMODEM_SSH_TEST_KEY");
|
||||
|
||||
snprintf(buf, sizeof buf, "%s", spec);
|
||||
p[0] = buf;
|
||||
for (i = 1; i < 4; i++) {
|
||||
p[i] = strchr(p[i - 1], ':');
|
||||
if (!p[i]) {
|
||||
fprintf(stderr, "DMODEM_SSH_TEST: host:port:user:password\n");
|
||||
failures++;
|
||||
return;
|
||||
}
|
||||
*p[i]++ = 0;
|
||||
}
|
||||
host = p[0];
|
||||
port = atoi(p[1]);
|
||||
user = p[2];
|
||||
password = p[3];
|
||||
|
||||
s = logged_in();
|
||||
if (!s)
|
||||
return;
|
||||
test_shell(s);
|
||||
test_exec(s);
|
||||
test_direct(s);
|
||||
test_listen(s);
|
||||
dmc_ssh_disconnect(s);
|
||||
while (next(s, &ev) && ev.type != DMC_SSH_EV_CLOSED)
|
||||
;
|
||||
CHECK(ev.type == DMC_SSH_EV_CLOSED && ev.data == NULL);
|
||||
CHECK(dmc_ssh_poll(s, 10, &ev) == -1);
|
||||
dmc_ssh_free(s);
|
||||
|
||||
/* a refused key: the connection ends saying so */
|
||||
s = dmc_ssh_new(host, port, user);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_HOSTKEY);
|
||||
dmc_ssh_trust(s, 0, 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_CLOSED);
|
||||
CHECK(ev.data && !strcmp(ev.data, "the host's key was not accepted"));
|
||||
dmc_ssh_free(s);
|
||||
|
||||
/* keyboard-interactive, when the server has it: the password asked */
|
||||
s = dmc_ssh_new(host, port, user);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_HOSTKEY);
|
||||
dmc_ssh_trust(s, 1, 0);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_AUTH);
|
||||
if (ev.value & DMC_SSH_AUTH_INTERACTIVE) {
|
||||
int echo = 1;
|
||||
|
||||
dmc_ssh_auth_interactive(s);
|
||||
CHECK(next(s, &ev) == DMC_SSH_EV_PROMPTS && ev.value == 1);
|
||||
CHECK(strstr(dmc_ssh_prompt(s, 0, &echo), "assword") != NULL);
|
||||
CHECK(echo == 0);
|
||||
dmc_ssh_answer(s, 0, password);
|
||||
dmc_ssh_answered(s);
|
||||
/* (some servers end with a round of no questions) */
|
||||
while (next(s, &ev) == DMC_SSH_EV_PROMPTS && ev.value == 0)
|
||||
dmc_ssh_answered(s);
|
||||
CHECK(ev.type == DMC_SSH_EV_READY);
|
||||
} else {
|
||||
printf("no keyboard-interactive: not tested\n");
|
||||
}
|
||||
dmc_ssh_free(s);
|
||||
|
||||
if (key)
|
||||
test_key_login(key);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char *spec = getenv("DMODEM_SSH_TEST");
|
||||
|
||||
#ifdef _WIN32
|
||||
WSADATA w;
|
||||
WSAStartup(MAKEWORD(2, 2), &w);
|
||||
#endif
|
||||
test_keys();
|
||||
if (spec)
|
||||
test_live(spec);
|
||||
else
|
||||
printf("DMODEM_SSH_TEST is not set: no login tested\n");
|
||||
if (failures) {
|
||||
fprintf(stderr, "%d failed\n", failures);
|
||||
return 1;
|
||||
}
|
||||
printf("ok\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user