Commit 545d3474 authored by Jeremy White's avatar Jeremy White

Implement support for the various password options.

parent 37b9f5a0
......@@ -71,6 +71,10 @@ int main(int argc, char *argv[])
if (rc)
goto exit;
rc = options_process_io(&session.options);
if (rc)
goto exit;
/*------------------------------------------------------------------------
** Create the session
**----------------------------------------------------------------------*/
......
......@@ -29,6 +29,14 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <spice/protocol.h>
#include "options.h"
#include "x11spice.h"
......@@ -117,7 +125,8 @@ static void usage(options_t *options, char *argv0)
{
int len = strlen(argv0);
fprintf(stderr, "%s: [--viewonly ] [--timeout=seconds] [--display=DISPLAY]\n", argv0);
fprintf(stderr, "%*.*s [--auto=<listen-spec>] [--generate-passcode]\n", len, len, "");
fprintf(stderr, "%*.*s [--password=<password>] [--password-file={-|<password-file>}]\n", len, len, "");
fprintf(stderr, "%*.*s [--auto=<listen-spec>] [--generate-password[=len]]\n", len, len, "");
fprintf(stderr, "%*.*s [--hide] [--minimize]\n", len, len, "");
fprintf(stderr, "Command line parameters override settings in %s\n", options->user_config_file);
fprintf(stderr, "which overrides settings in %s\n",
......@@ -130,7 +139,8 @@ int options_parse_arguments(int argc, char *argv[], options_t *options)
int longindex = 0;
enum option_types { OPTION_VIEWONLY, OPTION_TIMEOUT, OPTION_AUTO, OPTION_HIDE,
OPTION_GENERATE_PASSCODE, OPTION_DISPLAY, OPTION_MINIMIZE,
OPTION_PASSWORD, OPTION_PASSWORD_FILE,
OPTION_GENERATE_PASSWORD, OPTION_DISPLAY, OPTION_MINIMIZE,
OPTION_HELP
};
......@@ -140,7 +150,9 @@ int options_parse_arguments(int argc, char *argv[], options_t *options)
{"timeout", 1, 0, OPTION_TIMEOUT },
{"auto", 1, 0, OPTION_AUTO },
{"hide", 0, 0, OPTION_HIDE },
{"generate-passcode", 0, 0, OPTION_GENERATE_PASSCODE},
{"password", 1, 0, OPTION_PASSWORD },
{"password-file", 1, 0, OPTION_PASSWORD_FILE },
{"generate-password", 2, 0, OPTION_GENERATE_PASSWORD },
{"display", 1, 0, OPTION_DISPLAY },
{"minimize", 0, 0, OPTION_MINIMIZE },
{"help", 0, 0, OPTION_HELP},
......@@ -173,9 +185,18 @@ int options_parse_arguments(int argc, char *argv[], options_t *options)
options->hide = 1;
break;
case OPTION_GENERATE_PASSCODE:
/* FIXME - implement --generate_passcode */
options->generate_passcode = 1;
case OPTION_PASSWORD:
options->spice_password = strdup(optarg);
break;
case OPTION_PASSWORD_FILE:
options->password_file = strdup(optarg);
break;
case OPTION_GENERATE_PASSWORD:
options->generate_password = DEFAULT_PASSWORD_LENGTH;
if (optarg)
options->generate_password = atol(optarg);
break;
case OPTION_DISPLAY:
......@@ -192,6 +213,19 @@ int options_parse_arguments(int argc, char *argv[], options_t *options)
}
}
/* Make sure conflicting password options are not given */
if (rc == 0) {
int count = 0;
count += options->password_file ? 1 : 0;
count += options->spice_password ? 1 : 0;
count += options->generate_password ? 1 : 0;
if (count > 1) {
fprintf(stderr, "Error: you can specify only one of password, password-file, "
"and generate-password\n");
rc = X11SPICE_ERR_BADARGS;
}
}
return rc;
}
......@@ -217,13 +251,14 @@ void options_from_config(options_t *options)
options->timeout = int_option(userkey, systemkey, "spice", "timeout");
options->minimize = int_option(userkey, systemkey, "spice", "minimize");
options->viewonly = int_option(userkey, systemkey, "spice", "viewonly");
options->generate_passcode = int_option(userkey, systemkey, "spice", "generate-passcode");
options->generate_password = int_option(userkey, systemkey, "spice", "generate-password");
options->hide = int_option(userkey, systemkey, "spice", "hide");
options->display = string_option(userkey, systemkey, "spice", "display");
options->autouri = string_option(userkey, systemkey, "spice", "auto");
options->spice_addr = string_option(userkey, systemkey, "spice", "addr");
options->spice_password = string_option(userkey, systemkey, "spice", "password");
options->password_file = string_option(userkey, systemkey, "spice", "password-file");
options->spice_port = int_option(userkey, systemkey, "spice", "port");
options->disable_ticketing = bool_option(userkey, systemkey, "spice", "disable-ticketing");
options->exit_on_disconnect = bool_option(userkey, systemkey, "spice", "exit-on-disconnect");
......@@ -239,6 +274,88 @@ void options_from_config(options_t *options)
options->disable_ticketing, options->spice_port);
}
static int process_password_file(options_t *options)
{
int rc = 0;
FILE *fp;
char *p;
char buf[SPICE_MAX_PASSWORD_LENGTH + 1];
if (strcmp(options->password_file, "-") == 0) {
printf("Enter password: ");
fflush(stdout);
fp = stdin;
}
else {
fp = fopen(options->password_file, "r");
if (!fp)
return X11SPICE_ERR_OPEN;
}
if (!fgets(buf, sizeof(buf), fp))
rc = X11SPICE_ERR_PARSE;
if (strcmp(options->password_file, "-") != 0)
fclose(fp);
/* Strip a trailing \n */
p = buf + strlen(buf);
if (p > buf && *(p - 1) == '\n')
*(p - 1) = '\0';
options->spice_password = strdup(buf);
return rc;
}
static int generate_password(options_t *options)
{
int fd;
int rc;
char *p;
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
return X11SPICE_ERR_OPEN;
p = options->spice_password = malloc(options->generate_password + 1);
if (!p)
return X11SPICE_ERR_MALLOC;
while (p - options->spice_password < options->generate_password) {
rc = read(fd, p, sizeof(*p));
if (rc == 0 || (rc == -1 && errno != EINTR))
return -1;
if (isalnum(*p))
p++;
}
*p = '\0';
close(fd);
return 0;
}
int options_process_io(options_t *options)
{
int rc;
if (options->password_file) {
rc = process_password_file(options);
if (rc)
return rc;
}
if (options->generate_password) {
rc = generate_password(options);
if (rc)
return rc;
printf("PASSWORD=%s\n", options->spice_password);
fflush(stdout);
}
return 0;
}
#if defined(OPTIONS_MAIN)
int main(int argc, char *argv[])
{
......
......@@ -21,6 +21,11 @@
#ifndef OPTIONS_H_
#define OPTIONS_H_
/*----------------------------------------------------------------------------
** constants
**--------------------------------------------------------------------------*/
#define DEFAULT_PASSWORD_LENGTH 8
/*----------------------------------------------------------------------------
** Structure definitions
**--------------------------------------------------------------------------*/
......@@ -29,7 +34,7 @@ typedef struct {
long timeout;
int minimize;
int viewonly;
int generate_passcode;
int generate_password;
int hide;
char *display;
char *autouri;
......@@ -38,6 +43,7 @@ typedef struct {
char *spice_addr;
int spice_port;
char *spice_password;
char *password_file;
int disable_ticketing;
int exit_on_disconnect;
char *virtio_path;
......@@ -54,6 +60,7 @@ typedef struct {
**--------------------------------------------------------------------------*/
void options_init(options_t *options);
int options_parse_arguments(int argc, char *argv[], options_t *options);
int options_process_io(options_t *options);
void options_free(options_t *options);
void options_from_config(options_t *options);
......
......@@ -41,5 +41,6 @@
#define X11SPICE_ERR_NO_SOCKET 14
#define X11SPICE_ERR_BIND 15
#define X11SPICE_ERR_LISTEN 16
#define X11SPICE_ERR_OPEN 17
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment