mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-11 10:40:27 +09:00
examples/ssh_server: Add -u and -P option
enable pass username and password from command line Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Reviewed-by: Jakub Jelen <jjelen@redhat.com> Change-Id: I6404b90a99253d3240f7a28827635b159ff6a574
This commit is contained in:
@@ -48,8 +48,6 @@ The goal is to show the API in action.
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define USER "myuser"
|
|
||||||
#define PASS "mypassword"
|
|
||||||
#define BUF_SIZE 1048576
|
#define BUF_SIZE 1048576
|
||||||
#define SESSION_END (SSH_CLOSED | SSH_CLOSED_ERROR)
|
#define SESSION_END (SSH_CLOSED | SSH_CLOSED_ERROR)
|
||||||
#define SFTP_SERVER_PATH "/usr/lib/sftp-server"
|
#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
|
#define DEF_STR_SIZE 1024
|
||||||
char authorizedkeys[DEF_STR_SIZE] = {0};
|
char authorizedkeys[DEF_STR_SIZE] = {0};
|
||||||
|
char username[128] = "myuser";
|
||||||
|
char password[128] = "mypassword";
|
||||||
#ifdef HAVE_ARGP_H
|
#ifdef HAVE_ARGP_H
|
||||||
const char *argp_program_version = "libssh server example "
|
const char *argp_program_version = "libssh server example "
|
||||||
SSH_STRINGIFY(LIBSSH_VERSION);
|
SSH_STRINGIFY(LIBSSH_VERSION);
|
||||||
@@ -137,6 +137,22 @@ static struct argp_option options[] = {
|
|||||||
.doc = "Set the authorized keys file.",
|
.doc = "Set the authorized keys file.",
|
||||||
.group = 0
|
.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",
|
.name = "no-default-keys",
|
||||||
.key = 'n',
|
.key = 'n',
|
||||||
@@ -193,6 +209,12 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) {
|
|||||||
case 'a':
|
case 'a':
|
||||||
strncpy(authorizedkeys, arg, DEF_STR_SIZE-1);
|
strncpy(authorizedkeys, arg, DEF_STR_SIZE-1);
|
||||||
break;
|
break;
|
||||||
|
case 'u':
|
||||||
|
strncpy(username, arg, sizeof(username) - 1);
|
||||||
|
break;
|
||||||
|
case 'P':
|
||||||
|
strncpy(password, arg, sizeof(password) - 1);
|
||||||
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY_STR,
|
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_LOG_VERBOSITY_STR,
|
||||||
"3");
|
"3");
|
||||||
@@ -440,7 +462,7 @@ static int auth_password(ssh_session session, const char *user,
|
|||||||
|
|
||||||
(void) session;
|
(void) session;
|
||||||
|
|
||||||
if (strcmp(user, USER) == 0 && strcmp(pass, PASS) == 0) {
|
if (strcmp(user, username) == 0 && strcmp(pass, password) == 0) {
|
||||||
sdata->authenticated = 1;
|
sdata->authenticated = 1;
|
||||||
return SSH_AUTH_SUCCESS;
|
return SSH_AUTH_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user