options.c 15.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
    Copyright (C) 2016  Jeremy White <jwhite@codeweavers.com>
    All rights reserved.

    This file is part of x11spice

    x11spice is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    x11spice is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with x11spice.  If not, see <http://www.gnu.org/licenses/>.
*/

21 22 23 24 25
/*----------------------------------------------------------------------------
**  options.c
**      Code to handle options.  This includes command line arguments
**  as well as options that can be set in configuration files.
**--------------------------------------------------------------------------*/
26

27
#include <glib.h>
28 29 30 31
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
32 33 34 35 36 37 38 39
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>

#include <spice/protocol.h>
40 41 42 43

#include "options.h"
#include "x11spice.h"

44 45 46 47
#if defined(HAVE_LIBAUDIT_H)
#include <libaudit.h>
#endif

48 49 50 51 52 53 54
void options_init(options_t *options)
{
    memset(options, 0, sizeof(*options));
}

void options_free(options_t *options)
{
55
    if (options->display) {
56 57 58
        free(options->display);
        options->display = NULL;
    }
59 60 61

    g_free(options->spice_password);
    options->spice_password = NULL;
62

Jeremy White's avatar
Jeremy White committed
63 64 65 66
    g_free(options->virtio_path);
    options->virtio_path = NULL;
    g_free(options->uinput_path);
    options->uinput_path = NULL;
67 68 69 70
    g_free(options->on_connect);
    options->on_connect = NULL;
    g_free(options->on_disconnect);
    options->on_disconnect = NULL;
Jeremy White's avatar
Jeremy White committed
71

72 73 74
    if (options->listen)
        free(options->listen);
    options->listen = NULL;
Jeremy White's avatar
Jeremy White committed
75 76 77 78 79 80

    g_free(options->user_config_file);
    options->user_config_file = NULL;

    g_free(options->system_config_file);
    options->system_config_file = NULL;
81 82 83
}


84
static gchar *string_option(GKeyFile *u, GKeyFile *s, const gchar *section, const gchar *key)
85 86 87 88 89 90
{
    gchar *ret = NULL;
    GError *error = NULL;

    if (u)
        ret = g_key_file_get_string(u, section, key, &error);
91
    if ((!u || error) && s)
92
        ret = g_key_file_get_string(s, section, key, NULL);
93 94
    if (error)
        g_error_free(error);
95 96

    return ret;
97 98
}

99 100 101 102 103 104 105
static gint int_option(GKeyFile *u, GKeyFile *s, const gchar *section, const gchar *key)
{
    gint ret = 0;
    GError *error = NULL;

    if (u)
        ret = g_key_file_get_integer(u, section, key, &error);
106
    if ((!u || error) && s)
107
        ret = g_key_file_get_integer(s, section, key, NULL);
108 109
    if (error)
        g_error_free(error);
110 111 112 113 114 115 116 117 118 119 120

    return ret;
}

static gboolean bool_option(GKeyFile *u, GKeyFile *s, const gchar *section, const gchar *key)
{
    gboolean ret = FALSE;
    GError *error = NULL;

    if (u)
        ret = g_key_file_get_boolean(u, section, key, &error);
121
    if ((!u || error) && s)
122
        ret = g_key_file_get_boolean(s, section, key, NULL);
123 124
    if (error)
        g_error_free(error);
125 126 127

    return ret;
}
128

Jeremy White's avatar
Jeremy White committed
129
static void usage(options_t *options, char *argv0)
130
{
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    char indent[256];

    snprintf(indent, sizeof(indent), "%*.*s ", (int) strlen(argv0), (int) strlen(argv0), "");
    printf("%s: [OPTIONS] [<listen-specification>]\n", argv0);
    printf("\n");
    printf("Starts a Spice server and connects it to an X11 display.\n");
    printf("\n");
    printf("The <listen-specification> is of the form:\n");
    printf("  [[host]:[port][-end-port]\n");
    printf("where host specifies the address to listen on.  Defaults to localhost\n");
    printf("      port specifies the port to listen to.  Defaults to 5900.\n");
    printf("      end-port, if given, will cause x11spice to scan from port to end-port\n");
    printf("      checking for an open port, and using the first one available.\n");
    printf("\n");
    printf("Options:\n");
146
    printf("%s [--allow-control]\n", indent);
147
    printf("%s [--no-allow-control]\n", indent);
148 149 150 151 152
    printf("%s [--timeout=<seconds>]\n", indent);
    printf("%s [--display=<DISPLAY>]\n", indent);
    printf("%s [--generate-password[=<len>]\n", indent);
    printf("%s [--password=<password>]\n", indent);
    printf("%s [--password-file={-|<password-file}]\n", indent);
153
    printf("%s [--config=<config-file>]\n", indent);
Jeremy White's avatar
Jeremy White committed
154
    printf("%s [--ssl[=<ssl-spec>]]\n", indent);
155 156
    printf("%s [--hide]\n", indent);
    printf("%s [--minimize]\n", indent);
157 158
}

Jeremy White's avatar
Jeremy White committed
159 160
int options_handle_ssl(options_t *options, const char *spec)
{
161
    char *save = NULL;
Jeremy White's avatar
Jeremy White committed
162 163 164 165 166
    char *in = strdup(spec);
    char *p;
    int i = 0;
    int rc = 0;

167 168 169
    if (!in)
        return X11SPICE_ERR_MALLOC;

Jeremy White's avatar
Jeremy White committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    for (p = strtok_r(in, ",", &save); p; p = strtok_r(NULL, ",", &save), i++) {
        if (strlen(p) == 0)
            continue;

        switch(i) {
            case 0:
                options->ssl.ca_cert_file = strdup(p);
                break;
            case 1:
                options->ssl.certs_file = strdup(p);
                break;
            case 2:
                options->ssl.private_key_file = strdup(p);
                break;
            case 3:
                options->ssl.key_password = strdup(p);
                break;
            case 4:
                options->ssl.dh_key_file = strdup(p);
                break;
            case 5:
                options->ssl.ciphersuite = strdup(p);
                break;
            default:
                fprintf(stderr, "Error: invalid ssl specification.");
                rc = X11SPICE_ERR_BADARGS;
                break;
        }
    }

    free(in);
    return rc;
}

void options_handle_ssl_file_options(options_t *options,
                                     GKeyFile *userkey, GKeyFile *systemkey)
{
    options->ssl.enabled = bool_option(userkey, systemkey, "ssl", "enabled");
    options->ssl.ca_cert_file = string_option(userkey, systemkey, "ssl", "ca-cert-file");
    options->ssl.certs_file = string_option(userkey, systemkey, "ssl", "certs-file");
    options->ssl.private_key_file = string_option(userkey, systemkey, "ssl", "private-key-file");
    options->ssl.key_password = string_option(userkey, systemkey, "ssl", "key-password-file");
    options->ssl.dh_key_file = string_option(userkey, systemkey, "ssl", "dh-key-file");
    options->ssl.ciphersuite = string_option(userkey, systemkey, "ssl", "ciphersuite");
}

216 217 218 219 220 221 222 223 224 225
void options_handle_user_config(int argc, char *argv[], options_t *options)
{
    int i;
    for (i = 1; i < argc - 1; i++)
        if (strcmp(argv[i], "--config") == 0 || strcmp(argv[i], "-config") == 0) {
            options->user_config_file = strdup(argv[i + 1]);
            i++;
        }
}

226 227 228 229 230
int options_parse_arguments(int argc, char *argv[], options_t *options)
{
    int rc;
    int longindex = 0;

231 232
    enum option_types {  OPTION_ALLOW_CONTROL, OPTION_DISALLOW_CONTROL,
                         OPTION_TIMEOUT, OPTION_AUTO, OPTION_HIDE,
Jeremy White's avatar
Jeremy White committed
233
                         OPTION_PASSWORD, OPTION_PASSWORD_FILE, OPTION_CONFIG, OPTION_SSL,
234
                         OPTION_GENERATE_PASSWORD, OPTION_DISPLAY, OPTION_MINIMIZE,
235
                         OPTION_HELP
236 237 238 239
    };

    static struct option long_options[] =
    {
240
        {"allow-control",            0, 0,       OPTION_ALLOW_CONTROL },
241
        {"no-allow-control",         0, 0,       OPTION_DISALLOW_CONTROL },
242
        {"timeout",                  1, 0,       OPTION_TIMEOUT  },
243
        {"auto",                     1, 0,       OPTION_AUTO },
244
        {"hide",                     0, 0,       OPTION_HIDE },
245 246
        {"password",                 1, 0,       OPTION_PASSWORD },
        {"password-file",            1, 0,       OPTION_PASSWORD_FILE },
247
        {"config",                   1, 0,       OPTION_CONFIG },
Jeremy White's avatar
Jeremy White committed
248
        {"ssl",                      2, 0,       OPTION_SSL},
249
        {"generate-password",        2, 0,       OPTION_GENERATE_PASSWORD },
250
        {"display",                  1, 0,       OPTION_DISPLAY },
251
        {"minimize",                 0, 0,       OPTION_MINIMIZE },
252 253 254 255
        {"help",                     0, 0,       OPTION_HELP},
        {0, 0, 0, 0}
    };

256
    while (1) {
257
        rc = getopt_long_only(argc, argv, "", long_options, &longindex);
258
        if (rc == -1) {
259 260 261 262
            rc = 0;
            break;
        }

263
        switch (rc) {
264 265
            case OPTION_ALLOW_CONTROL:
                options->allow_control = 1;
Jeremy White's avatar
Jeremy White committed
266 267
                break;

268 269 270 271
            case OPTION_DISALLOW_CONTROL:
                options->allow_control = 0;
                break;

272 273 274 275
            case OPTION_TIMEOUT:
                options->timeout = atol(optarg);
                break;

276 277 278 279
            case OPTION_HIDE:
                options->hide = 1;
                break;

280 281 282 283 284 285 286 287
            case OPTION_PASSWORD:
                options->spice_password = strdup(optarg);
                break;

            case OPTION_PASSWORD_FILE:
                options->password_file = strdup(optarg);
                break;

288 289 290 291
            case OPTION_CONFIG:
                /* This was handled previously; we can ignore */
                break;

Jeremy White's avatar
Jeremy White committed
292 293 294 295 296 297 298 299 300
            case OPTION_SSL:
                options->ssl.enabled = 1;
                if (optarg) {
                    rc = options_handle_ssl(options, optarg);
                    if (rc)
                        return rc;
                }
                break;

301 302 303 304
            case OPTION_GENERATE_PASSWORD:
                options->generate_password = DEFAULT_PASSWORD_LENGTH;
                if (optarg)
                    options->generate_password = atol(optarg);
Jeremy White's avatar
Jeremy White committed
305 306 307 308
                break;

            case OPTION_DISPLAY:
                options->display = strdup(optarg);
309 310
                break;

311 312 313 314
            case OPTION_MINIMIZE:
                options->minimize = 1;
                break;

315
            default:
Jeremy White's avatar
Jeremy White committed
316
                usage(options, argv[0]);
317
                return X11SPICE_ERR_BADARGS;
318 319 320
        }
    }

321 322 323 324 325 326 327 328 329 330 331 332 333
    /* 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;
        }
    }

334 335 336 337 338 339 340 341 342 343 344 345 346
    /* Grab the listen spec, if given */
    if (rc == 0) {
        if (optind >= argc) {
            /* Default */
            options->listen = strdup("5900");
        } else if (optind < (argc - 1)) {
            fprintf(stderr, "Error: too many arguments\n");
            rc = X11SPICE_ERR_BADARGS;
        } else {
            options->listen = strdup(argv[optind]);
        }
    }

347 348
    return rc;
}
349 350 351

void options_from_config(options_t *options)
{
352
    GKeyFile *userkey = g_key_file_new();
353 354 355 356 357 358 359 360 361 362 363 364 365 366
    GKeyFile *systemkey = NULL;
    int config_file_given = options->user_config_file ? TRUE : FALSE;

    if (!config_file_given) {
        options->user_config_file = g_build_filename(g_get_user_config_dir(), "x11spice/x11spice.conf", NULL);

        systemkey = g_key_file_new();
        if (!g_key_file_load_from_dirs(systemkey, "x11spice/x11spice.conf",
                                       (const char **) g_get_system_config_dirs(),
                                       &options->system_config_file, G_KEY_FILE_NONE, NULL)) {
            g_key_file_free(systemkey);
            systemkey = NULL;
        }
    }
367

368
    if (!g_key_file_load_from_file(userkey, options->user_config_file, G_KEY_FILE_NONE, NULL)) {
369 370 371 372
        g_key_file_free(userkey);
        userkey = NULL;
    }

Jeremy White's avatar
Jeremy White committed
373
    options->timeout = int_option(userkey, systemkey, "spice", "timeout");
374
    options->minimize = bool_option(userkey, systemkey, "spice", "minimize");
375
    options->allow_control = bool_option(userkey, systemkey, "spice", "allow-control");
376
    options->generate_password = int_option(userkey, systemkey, "spice", "generate-password");
377
    options->hide = bool_option(userkey, systemkey, "spice", "hide");
Jeremy White's avatar
Jeremy White committed
378 379
    options->display = string_option(userkey, systemkey, "spice", "display");

380
    options->listen = string_option(userkey, systemkey, "spice", "listen");
381
    options->spice_password = string_option(userkey, systemkey, "spice", "password");
382
    options->password_file = string_option(userkey, systemkey, "spice", "password-file");
383 384 385 386
    options->disable_ticketing = bool_option(userkey, systemkey, "spice", "disable-ticketing");
    options->exit_on_disconnect = bool_option(userkey, systemkey, "spice", "exit-on-disconnect");
    options->virtio_path = string_option(userkey, systemkey, "spice", "virtio-path");
    options->uinput_path = string_option(userkey, systemkey, "spice", "uinput-path");
387 388
    options->on_connect = string_option(userkey, systemkey, "spice", "on-connect");
    options->on_disconnect = string_option(userkey, systemkey, "spice", "on-disconnect");
389 390 391 392 393 394 395 396
    options->audit = bool_option(userkey, systemkey, "spice", "audit");
    options->audit_message_type = int_option(userkey, systemkey, "spice", "audit-message-type");

#if defined(HAVE_LIBAUDIT_H)
    /* Pick an arbitrary default in the user range.  CodeWeavers was founed in 1996, so 1196 it is... */
    if (options->audit_message_type == 0)
        options->audit_message_type = AUDIT_LAST_USER_MSG - 3;
#endif
397

Jeremy White's avatar
Jeremy White committed
398 399
    options_handle_ssl_file_options(options, userkey, systemkey);

400 401 402 403
    if (systemkey)
        g_key_file_free(systemkey);
    if (userkey)
        g_key_file_free(userkey);
404

405 406
    g_debug("options listen '%s', disable_ticketing %d", options->listen,
            options->disable_ticketing);
407
}
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
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;
}

491 492 493 494 495 496 497 498 499 500 501 502 503 504
int options_impossible_config(options_t *options)
{
    if (options->spice_password)
        return 0;

    if (options->generate_password || options->password_file)
        return 0;

    if (options->disable_ticketing)
        return 0;

    return 1;
}

505 506 507 508 509 510 511 512
#if defined(OPTIONS_MAIN)
int main(int argc, char *argv[])
{
    options_t options;

    options_init(&options);
    options_parse_arguments(argc, argv, &options);
    options_from_config(&options);
Jeremy White's avatar
Jeremy White committed
513
    g_message("Options parsed");
514 515 516
    options_free(&options);
}
#endif