-/*
- Generate a public/private RSA keypair, and ask for a file to store
- them in.
-*/
-int keygen(int bits)
-{
- RSA *rsa_key;
- FILE *f;
- char *name = NULL;
- char *filename;
-
- fprintf(stderr, _("Generating %d bits keys:\n"), bits);
- rsa_key = RSA_generate_key(bits, 0xFFFF, indicator, NULL);
-
- if(!rsa_key)
- {
- fprintf(stderr, _("Error during key generation!\n"));
- return -1;
- }
- else
- fprintf(stderr, _("Done.\n"));
-
- get_config_string(lookup_config(config_tree, "Name"), &name);
-
- if(name)
- asprintf(&filename, "%s/hosts/%s", confbase, name);
- else
- asprintf(&filename, "%s/rsa_key.pub", confbase);
-
- f = ask_and_safe_open(filename, _("public RSA key"), "a");
-
- if(!f)
- return -1;
-
- if(ftell(f))
- fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
-
- PEM_write_RSAPublicKey(f, rsa_key);
- fclose(f);
- free(filename);
-
- asprintf(&filename, "%s/rsa_key.priv", confbase);
- f = ask_and_safe_open(filename, _("private RSA key"), "a");
-
- if(!f)
- return -1;
-
- if(ftell(f))
- fprintf(stderr, _("Appending key to existing contents.\nMake sure only one key is stored in the file.\n"));
-
- PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
- fclose(f);
- free(filename);
-
- return 0;
+static bool parse_options(int argc, char **argv) {
+ config_t *cfg;
+ int r;
+ int option_index = 0;
+ int lineno = 0;
+
+ 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 */
+ free(confbase);
+ 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]);
+ goto exit_fail;
+#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 */
+ free(netname);
+ 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) {
+ goto exit_fail;
+ }
+
+ 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]);
+ goto exit_fail;
+#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) {
+ free(logfilename);
+ logfilename = xstrdup(optarg);
+ }
+
+ break;
+
+ case 5: /* open control socket here */
+ free(pidfilename);
+ pidfilename = xstrdup(optarg);
+ break;
+
+ case '?': /* wrong options */
+ usage(true);
+ goto exit_fail;
+
+ default:
+ break;
+ }
+ }
+
+ if(optind < argc) {
+ fprintf(stderr, "%s: unrecognized argument '%s'\n", argv[0], argv[optind]);
+ usage(true);
+ goto exit_fail;
+ }
+
+ 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");
+ goto exit_fail;
+ }
+
+ if(netname && !check_netname(netname, true)) {
+ fprintf(stderr, "Warning: unsafe character in netname!\n");
+ }
+
+ return true;
+
+exit_fail:
+ free_names();
+ list_empty_list(&cmdline_conf);
+ return false;