Date: Sun, 8 Nov 2015 04:15:38 -0000 The command `scp' originally handled user name parsing on its own, including the retrieval of the current user's name when no user name is specified.
The `get_user_name()' function was developed to retrieve the current user's name lazily (on demand), so as not to waste cycles when a user name is *explicitly* specified. Furthermore, there was a warning message (actually, a fatal error message) produced when it was impossible to retrieve the current user's name; this message was eventually stripped out because it was relatively useless and takes up space. Ultimately, though, all of the `[user@]host' parsing machinery was ripped out of `scp', because it is already handled by `dropbear/dbclient'. However, as it turns out, this centralized machinery could also benefit from the same changes that were made for `scp'; hence, this commit re-introduces the `get_user_name()' function (albeit for use by `dropbear/dbclient' instead), including the removal of the relatively useless warning message. This function has been added to `cli-runopts.c', because the current user's name represents part of the invocation context (i.e., part of the runtime options); by including the declaration in `runopts.h', it automatically becomes visible where it is needed. --- cli-main.c | 2 +- cli-runopts.c | 32 ++++++++++++-------------------- common-session.c | 1 + runopts.h | 2 +- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/cli-main.c b/cli-main.c index c7c9035..a811312 100644 --- a/cli-main.c +++ b/cli-main.c @@ -135,7 +135,7 @@ static void exec_proxy_cmd(void *user_data_cmd) { static void cli_proxy_cmd(int *sock_in, int *sock_out) { int ret; - fill_passwd(cli_opts.own_user); + fill_passwd(get_user_name()); ret = spawn_command(exec_proxy_cmd, cli_opts.proxycmd, sock_out, sock_in, NULL, NULL); diff --git a/cli-runopts.c b/cli-runopts.c index c4cd12c..34cad00 100644 --- a/cli-runopts.c +++ b/cli-runopts.c @@ -36,7 +36,6 @@ cli_runopts cli_opts; /* GLOBAL */ static void printhelp(); static void parse_hostname(const char* orighostarg); static void parse_multihop_hostname(const char* orighostarg, const char* argv0); -static void fill_own_user(); #ifdef ENABLE_CLI_PUBKEY_AUTH static void loadidentityfile(const char* filename, int warnfail); #endif @@ -102,6 +101,17 @@ static void printhelp() { } +const char *get_user_name() { + static const char *user_name = NULL; + + if (user_name == NULL) { + struct passwd *pwd = getpwuid(getuid()); + user_name = pwd ? m_strdup(pwd->pw_name) : "unknown"; + } + + return user_name; +} + void cli_getopts(int argc, char ** argv) { unsigned int i, j; char ** next = 0; @@ -175,8 +185,6 @@ void cli_getopts(int argc, char ** argv) { opts.keepalive_secs = DEFAULT_KEEPALIVE; opts.idle_timeout_secs = DEFAULT_IDLE_TIMEOUT; - fill_own_user(); - for (i = 1; i < (unsigned int)argc; i++) { /* Handle non-flag arguments such as hostname or commands for the remote host */ if (argv[i][0] != '-') @@ -640,7 +648,7 @@ static void parse_hostname(const char* orighostarg) { } if (cli_opts.username == NULL) { - cli_opts.username = m_strdup(cli_opts.own_user); + cli_opts.username = m_strdup(get_user_name()); } port = strchr(cli_opts.remotehost, '^'); @@ -695,22 +703,6 @@ fail: } #endif -static void fill_own_user() { - uid_t uid; - struct passwd *pw = NULL; - - uid = getuid(); - - pw = getpwuid(uid); - if (pw && pw->pw_name != NULL) { - cli_opts.own_user = m_strdup(pw->pw_name); - } else { - dropbear_log(LOG_INFO, "Warning: failed to identify current user. Trying anyway."); - cli_opts.own_user = "unknown"; - } - -} - #ifdef ENABLE_CLI_ANYTCPFWD /* Turn a "[listenaddr:]listenport:remoteaddr:remoteport" string into into a forwarding * set, and add it to the forwarding list */ diff --git a/common-session.c b/common-session.c index 874d539..dd818b2 100644 --- a/common-session.c +++ b/common-session.c @@ -581,6 +581,7 @@ const char* get_user_shell() { return ses.authstate.pw_shell; } } + void fill_passwd(const char* username) { struct passwd *pw = NULL; if (ses.authstate.pw_name) diff --git a/runopts.h b/runopts.h index 0a97937..654d6d4 100644 --- a/runopts.h +++ b/runopts.h @@ -127,7 +127,6 @@ typedef struct cli_runopts { char *remotehost; char *remoteport; - const char *own_user; char *username; char *cmd; @@ -165,6 +164,7 @@ typedef struct cli_runopts { extern cli_runopts cli_opts; void cli_getopts(int argc, char ** argv); +const char *get_user_name(); #ifdef ENABLE_USER_ALGO_LIST void parse_ciphers_macs();