xfreerdp: Adds switch --from-stdin.

This switch will prompt username, password, domain and connection
host if not specified at the command line.

Signed-off-by: Rex Tsai <chihchun@kalug.linux.org.tw>
This commit is contained in:
Mike Carifio 2012-06-06 17:53:30 +08:00 committed by Rex Tsai
parent 88f2e84e5e
commit a31f0c62e6
7 changed files with 98 additions and 32 deletions

View File

@ -292,7 +292,19 @@
<term>--authonly</term>
<listitem>
<para>
Only authenticates. This is useful to test your credentials (username and password). Returns status code 0 if the client can connect, 1 otherwise. Requires a username, password and connection host at the command line.
Only authenticates. This is useful to test your credentials (username and password).
Returns status code 0 if the client can connect, 1 otherwise. Requires a username,
password and connection host at the command line.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>--from-stdin</term>
<listitem>
<para>
Prompts for unspecified arguments -u username, -p password, -d domain and connection host.
This is useful to hide arguments from ps. Also useful for scripts that will feed these arguments
to the client via (what else?) stdin.
</para>
</listitem>
</varlistentry>

View File

@ -521,7 +521,6 @@ boolean xf_pre_connect(freerdp* instance)
freerdp_channels_pre_connect(xfi->_context->channels, instance);
if (settings->authentication_only) {
/* Check --authonly has a username and password. */
if (settings->username == NULL ) {
@ -791,7 +790,7 @@ boolean xf_authenticate(freerdp* instance, char** username, char** password, cha
// But it doesn't do anything to fix it...
*password = xmalloc(password_size * sizeof(char));
if (freerdp_passphrase_read("Password: ", *password, password_size) == NULL)
if (freerdp_passphrase_read("Password: ", *password, password_size, instance->settings->from_stdin) == NULL)
return false;
return true;

View File

@ -294,7 +294,8 @@ struct rdp_settings
ALIGN64 char* tsg_password; /* 67 */
ALIGN64 boolean local; /* 68 */
ALIGN64 boolean authentication_only; /* 69 */
ALIGN64 uint64 paddingC[80 - 70]; /* 70 */
ALIGN64 boolean from_stdin; /* 70 */
ALIGN64 uint64 paddingC[80 - 71]; /* 71 */
/* User Interface Parameters */
ALIGN64 boolean sw_gdi; /* 80 */

View File

@ -23,6 +23,6 @@
#include <stdlib.h>
#include <freerdp/api.h>
FREERDP_API char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz);
FREERDP_API char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz, int from_stdin);
#endif /* __UTILS_PASSPHRASE_H */

View File

@ -74,6 +74,7 @@ rdpSettings* settings_new(void* instance)
settings->authentication = true;
settings->authentication_only = false;
settings->from_stdin = false;
settings->received_caps = xzalloc(32);
settings->order_support = xzalloc(32);

View File

@ -27,6 +27,30 @@
#include <freerdp/utils/memory.h>
#include <freerdp/utils/args.h>
void freerdp_parse_hostname(rdpSettings* settings, char* hostname) {
char* p;
if (hostname[0] == '[' && (p = strchr(hostname, ']'))
&& (p[1] == 0 || (p[1] == ':' && !strchr(p + 2, ':')))) {
/* Either "[...]" or "[...]:..." with at most one : after the brackets */
settings->hostname = xstrdup(hostname + 1);
if ((p = strchr((char*)settings->hostname, ']'))) {
*p = 0;
if (p[1] == ':')
settings->port = atoi(p + 2);
}
} else {
/* Port number is cut off and used if exactly one : in the string */
settings->hostname = xstrdup(hostname);
if ((p = strchr((char*)settings->hostname, ':')) && !strchr(p + 1, ':')) {
*p = 0;
settings->port = atoi(p + 1);
}
}
}
/**
* Parse command-line arguments and update rdpSettings members accordingly.
* @param settings pointer to rdpSettings struct to be updated.
@ -82,6 +106,7 @@ int freerdp_parse_args(rdpSettings* settings, int argc, char** argv,
" --ext: load an extension\n"
" --no-auth: disable authentication\n"
" --authonly: authentication only, no UI\n"
" --from-stdin: unspecified username, password, domain and hostname params are prompted\n"
" --no-fastpath: disable fast-path\n"
" --no-motion: don't send mouse motion events\n"
" --gdi: graphics rendering (hw, sw)\n"
@ -311,6 +336,10 @@ int freerdp_parse_args(rdpSettings* settings, int argc, char** argv,
{
settings->authentication_only = true;
}
else if (strcmp("--from-stdin", argv[index]) == 0)
{
settings->from_stdin = true;
}
else if (strcmp("--ignore-certificate", argv[index]) == 0)
{
settings->ignore_certificate = true;
@ -676,28 +705,8 @@ int freerdp_parse_args(rdpSettings* settings, int argc, char** argv,
}
else if (argv[index][0] != '-')
{
if (argv[index][0] == '[' && (p = strchr(argv[index], ']'))
&& (p[1] == 0 || (p[1] == ':' && !strchr(p + 2, ':'))))
{
/* Either "[...]" or "[...]:..." with at most one : after the brackets */
settings->hostname = xstrdup(argv[index] + 1);
if ((p = strchr((char*)settings->hostname, ']')))
{
*p = 0;
if (p[1] == ':')
settings->port = atoi(p + 2);
}
}
else
{
/* Port number is cut off and used if exactly one : in the string */
settings->hostname = xstrdup(argv[index]);
if ((p = strchr((char*)settings->hostname, ':')) && !strchr(p + 1, ':'))
{
*p = 0;
settings->port = atoi(p + 1);
}
}
freerdp_parse_hostname(settings, argv[index]);
/* server is the last argument for the current session. arguments
followed will be parsed for the next session. */
index++;
@ -720,7 +729,8 @@ int freerdp_parse_args(rdpSettings* settings, int argc, char** argv,
if (settings->disable_theming)
settings->performance_flags |= PERF_DISABLE_THEMING;
return index;
break; /* post process missing arguments */
}
else
{
@ -738,6 +748,49 @@ int freerdp_parse_args(rdpSettings* settings, int argc, char** argv,
}
index++;
}
printf("missing server name\n");
return FREERDP_ARGS_PARSE_FAILURE;
/* --from-stdin will prompt for missing arguments only.
You can prompt for username, password, domain and hostname to avoid disclosing
these settings to ps. */
if (settings->from_stdin) {
/* username */
if (NULL == settings->username) {
char input[512];
printf("username: ");
scanf("%511s", input);
settings->username = xstrdup(input);
}
/* password */
if (NULL == settings->password) {
char input[512];
printf("password: ");
scanf("%511s", input);
settings->password = xstrdup(input);
}
/* domain */
if (NULL == settings->domain) {
char input[512];
printf("domain (control-D to skip): ");
scanf("%511s", input);
settings->domain = xstrdup(input);
}
/* hostname */
if (NULL == settings->hostname) {
char input[512];
printf("hostname: ");
scanf("%511s", input);
freerdp_parse_hostname(settings, input);
}
}
/* Must have a hostname. Do you? */
if (NULL == settings->hostname) {
printf("missing server name\n");
return FREERDP_ARGS_PARSE_FAILURE;
} else {
return index;
}
}

View File

@ -34,7 +34,7 @@ char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz)
#include <unistd.h>
#include <freerdp/utils/signal.h>
char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz)
char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz, int from_stdin)
{
char read_char;
char* buf_iter;
@ -50,7 +50,7 @@ char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz)
}
ctermid(term_name);
if(strcmp(term_name, "") == 0
if(from_stdin || strcmp(term_name, "") == 0
|| (term_file = open(term_name, O_RDWR)) == -1)
{
write_file = STDERR_FILENO;