-void
-parse_options(int argc, char **argv, char **envp)
-{
- int r;
- int option_index = 0;
-
- while((r = getopt_long(argc, argv, "c:Dd::k::n:K::", long_options, &option_index)) != EOF)
- {
- switch(r)
- {
- case 0: /* long option */
- break;
- case 'c': /* config file */
- confbase = xmalloc(strlen(optarg)+1);
- strcpy(confbase, optarg);
- break;
- case 'D': /* no detach */
- do_detach = 0;
- break;
- case 'd': /* inc debug level */
- if(optarg)
- debug_lvl = atoi(optarg);
- else
- debug_lvl++;
- break;
- case 'k': /* kill old tincds */
- kill_tincd = optarg?atoi(optarg):SIGTERM;
- break;
- case 'n': /* net name given */
- netname = xmalloc(strlen(optarg)+1);
- strcpy(netname, optarg);
- break;
- case 'K': /* generate public/private keypair */
- if(optarg)
- {
- generate_keys = atoi(optarg);
- if(generate_keys < 512)
- {
- fprintf(stderr, _("Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n"),
- optarg);
- usage(1);
- }
- generate_keys &= ~7; /* Round it to bytes */
- }
- else
- generate_keys = 1024;
- break;
- case '?':
- usage(1);
- default:
- break;
- }
- }
+static bool parse_options(int argc, char **argv) {
+ config_t *cfg;
+ int r;
+ int option_index = 0;
+ int lineno = 0;
+
+ cmdline_conf = list_alloc((list_action_t)free_config);
+
+ while((r = getopt_long(argc, argv, "c:DLd::n:so:RU:", long_options, &option_index)) != EOF) {
+ switch (r) {
+ case 0: /* long option */
+ break;
+
+ case 'c': /* config file */
+ confbase = xstrdup(optarg);
+ break;
+
+ case 'D': /* no detach */
+ do_detach = false;
+ break;
+
+ case 'L': /* no detach */
+#ifndef HAVE_MLOCKALL
+ logger(DEBUG_ALWAYS, LOG_ERR, "The %s option is not supported on this platform.", argv[optind - 1]);
+ return false;
+#else
+ do_mlock = true;
+ break;
+#endif
+
+ case 'd': /* increase debug level */
+ if(!optarg && optind < argc && *argv[optind] != '-')
+ optarg = argv[optind++];
+ if(optarg)
+ debug_level = atoi(optarg);
+ else
+ debug_level++;
+ break;
+
+ case 'n': /* net name given */
+ netname = xstrdup(optarg);
+ break;
+
+ case 's': /* syslog */
+ use_logfile = false;
+ use_syslog = true;
+ break;
+
+ case 'o': /* option */
+ cfg = parse_config_line(optarg, NULL, ++lineno);
+ if (!cfg)
+ return false;
+ list_insert_tail(cmdline_conf, cfg);
+ break;
+
+#ifdef HAVE_MINGW
+ case 'R':
+ case 'U':
+ logger(DEBUG_ALWAYS, LOG_ERR, "The %s option is not supported on this platform.", argv[optind - 1]);
+ return false;
+#else
+ case 'R': /* chroot to NETNAME dir */
+ do_chroot = true;
+ break;
+
+ case 'U': /* setuid to USER */
+ switchuser = optarg;
+ break;
+#endif
+
+ case 1: /* show help */
+ show_help = true;
+ break;
+
+ case 2: /* show version */
+ show_version = true;
+ break;
+
+ case 3: /* bypass security */
+ bypass_security = true;
+ break;
+
+ case 4: /* write log entries to a file */
+ use_syslog = false;
+ use_logfile = true;
+ if(!optarg && optind < argc && *argv[optind] != '-')
+ optarg = argv[optind++];
+ if(optarg)
+ logfilename = xstrdup(optarg);
+ break;
+
+ case 5: /* open control socket here */
+ pidfilename = xstrdup(optarg);
+ break;
+
+ case '?': /* wrong options */
+ usage(true);
+ return false;
+
+ default:
+ break;
+ }
+ }
+
+ if(optind < argc) {
+ fprintf(stderr, "%s: unrecognized argument '%s'\n", argv[0], argv[optind]);
+ usage(true);
+ return false;
+ }
+
+ if(!netname && (netname = getenv("NETNAME")))
+ netname = xstrdup(netname);
+
+ /* netname "." is special: a "top-level name" */
+
+ if(netname && (!*netname || !strcmp(netname, "."))) {
+ free(netname);
+ netname = NULL;
+ }
+
+ if(netname && !check_netname(netname, false)) {
+ fprintf(stderr, "Invalid character in netname!\n");
+ return false;
+ }
+
+ if(netname && !check_netname(netname, true))
+ fprintf(stderr, "Warning: unsafe character in netname!\n");
+
+ return true;