From 9bff4cb9b9e2c91a288333abe509fddb7e2c7927 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sat, 8 May 2021 00:26:24 -0700 Subject: [PATCH] examples/ssh_server: Add -u and -P option enable pass username and password from command line Signed-off-by: Xiang Xiao Reviewed-by: Jakub Jelen Change-Id: I6404b90a99253d3240f7a28827635b159ff6a574 --- examples/ssh_server_fork.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/examples/ssh_server_fork.c b/examples/ssh_server_fork.c index c9892d86..e0d0a764 100644 --- a/examples/ssh_server_fork.c +++ b/examples/ssh_server_fork.c @@ -48,8 +48,6 @@ The goal is to show the API in action. #endif #endif -#define USER "myuser" -#define PASS "mypassword" #define BUF_SIZE 1048576 #define SESSION_END (SSH_CLOSED | SSH_CLOSED_ERROR) #define SFTP_SERVER_PATH "/usr/lib/sftp-server" @@ -75,6 +73,8 @@ static void set_default_keys(ssh_bind sshbind, } #define DEF_STR_SIZE 1024 char authorizedkeys[DEF_STR_SIZE] = {0}; +char username[128] = "myuser"; +char password[128] = "mypassword"; #ifdef HAVE_ARGP_H const char *argp_program_version = "libssh server example " SSH_STRINGIFY(LIBSSH_VERSION); @@ -137,6 +137,22 @@ static struct argp_option options[] = { .doc = "Set the authorized keys file.", .group = 0 }, + { + .name = "user", + .key = 'u', + .arg = "USERNAME", + .flags = 0, + .doc = "Set expected username.", + .group = 0 + }, + { + .name = "pass", + .key = 'P', + .arg = "PASSWORD", + .flags = 0, + .doc = "Set expected password.", + .group = 0 + }, { .name = "no-default-keys", .key = 'n', @@ -193,6 +209,12 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) { case 'a': strncpy(authorizedkeys, arg, DEF_STR_SIZE-1); break; + case 'u': + strncpy(username, arg, sizeof(username) - 1); + break; + case 'P': + strncpy(password, arg, sizeof(password) - 1); + break; case 'v': ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY_STR, "3"); @@ -440,7 +462,7 @@ static int auth_password(ssh_session session, const char *user, (void) session; - if (strcmp(user, USER) == 0 && strcmp(pass, PASS) == 0) { + if (strcmp(user, username) == 0 && strcmp(pass, password) == 0) { sdata->authenticated = 1; return SSH_AUTH_SUCCESS; }