cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

conf-args.c (3551B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "config.h"
     21
     22#include "conf.h"
     23#include "conf-args.h"
     24#include "conf-parse.h"
     25
     26#include <guacamole/mem.h>
     27#include <guacamole/string.h>
     28
     29#include <getopt.h>
     30#include <stdio.h>
     31#include <stdlib.h>
     32#include <string.h>
     33
     34int guacd_conf_parse_args(guacd_config* config, int argc, char** argv) {
     35
     36    /* Parse arguments */
     37    int opt;
     38    while ((opt = getopt(argc, argv, "l:b:p:L:C:K:fv")) != -1) {
     39
     40        /* -l: Bind port */
     41        if (opt == 'l') {
     42            guac_mem_free(config->bind_port);
     43            config->bind_port = guac_strdup(optarg);
     44        }
     45
     46        /* -b: Bind host */
     47        else if (opt == 'b') {
     48            guac_mem_free(config->bind_host);
     49            config->bind_host = guac_strdup(optarg);
     50        }
     51
     52        /* -f: Run in foreground */
     53        else if (opt == 'f') {
     54            config->foreground = 1;
     55        }
     56
     57        /* -v: Print version and exit */
     58        else if (opt == 'v') {
     59            config->print_version = 1;
     60        }
     61
     62        /* -p: PID file */
     63        else if (opt == 'p') {
     64            guac_mem_free(config->pidfile);
     65            config->pidfile = guac_strdup(optarg);
     66        }
     67
     68        /* -L: Log level */
     69        else if (opt == 'L') {
     70
     71            /* Validate and parse log level */
     72            int level = guacd_parse_log_level(optarg);
     73            if (level == -1) {
     74                fprintf(stderr, "Invalid log level. Valid levels are: \"trace\", \"debug\", \"info\", \"warning\", and \"error\".\n");
     75                return 1;
     76            }
     77
     78            config->max_log_level = level;
     79
     80        }
     81
     82#ifdef ENABLE_SSL
     83        /* -C SSL certificate */
     84        else if (opt == 'C') {
     85            guac_mem_free(config->cert_file);
     86            config->cert_file = guac_strdup(optarg);
     87        }
     88
     89        /* -K SSL key */
     90        else if (opt == 'K') {
     91            guac_mem_free(config->key_file);
     92            config->key_file = guac_strdup(optarg);
     93        }
     94#else
     95        else if (opt == 'C' || opt == 'K') {
     96            fprintf(stderr,
     97                    "This guacd does not have SSL/TLS support compiled in.\n\n"
     98
     99                    "If you wish to enable support for the -%c option, please install libssl and\n"
    100                    "recompile guacd.\n",
    101                    opt);
    102            return 1;
    103        }
    104#endif
    105        else {
    106
    107            fprintf(stderr, "USAGE: %s"
    108                    " [-l LISTENPORT]"
    109                    " [-b LISTENADDRESS]"
    110                    " [-p PIDFILE]"
    111                    " [-L LEVEL]"
    112#ifdef ENABLE_SSL
    113                    " [-C CERTIFICATE_FILE]"
    114                    " [-K PEM_FILE]"
    115#endif
    116                    " [-f]"
    117                    " [-v]\n", argv[0]);
    118
    119            return 1;
    120        }
    121    }
    122
    123    /* Success */
    124    return 0;
    125
    126}
    127