+ if(mkdir(confbase, 0755) && errno != EEXIST) {
+ fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
+ return 1;
+ }
+
+ char *hosts_dir = NULL;
+ xasprintf(&hosts_dir, "%s/hosts", confbase);
+ if(mkdir(hosts_dir, 0755) && errno != EEXIST) {
+ fprintf(stderr, "Could not create directory %s: %s\n", hosts_dir, strerror(errno));
+ return 1;
+ }
+
+ FILE *f = fopen(tinc_conf, "w");
+ if(!f) {
+ fprintf(stderr, "Could not create file %s: %s\n", tinc_conf, strerror(errno));
+ return 1;
+ }
+
+ fprintf(f, "Name = %s\n", name);
+ fclose(f);
+
+ fclose(stdin);
+ if(!rsa_keygen(2048) || !ecdsa_keygen())
+ return false;
+
+ return true;
+
+}
+
+static int cmd_generate_keys(int argc, char *argv[]) {
+ return !(rsa_keygen(argc > 1 ? atoi(argv[1]) : 2048) && ecdsa_keygen());
+}
+
+static int cmd_generate_rsa_keys(int argc, char *argv[]) {
+ return !rsa_keygen(argc > 1 ? atoi(argv[1]) : 2048);
+}
+
+static int cmd_generate_ecdsa_keys(int argc, char *argv[]) {
+ return !ecdsa_keygen();
+}
+
+static int cmd_help(int argc, char *argv[]) {
+ usage(false);
+ return 0;
+}
+
+static int cmd_version(int argc, char *argv[]) {
+ version();
+ return 0;
+}
+
+static const char *conffiles[] = {
+ "tinc.conf",
+ "tinc-up",
+ "tinc-down",
+ "subnet-up",
+ "subnet-down",
+ "host-up",
+ "host-down",
+ NULL,
+};
+
+static int cmd_edit(int argc, char *argv[]) {
+ if(argc != 2) {
+ fprintf(stderr, "Invalid number of arguments.\n");
+ return 1;
+ }
+
+ char *filename = NULL;
+
+ if(strncmp(argv[1], "hosts/", 6)) {
+ for(int i = 0; conffiles[i]; i++) {
+ if(!strcmp(argv[1], conffiles[i])) {
+ xasprintf(&filename, "%s/%s", confbase, argv[1]);
+ break;
+ }
+ }
+ } else {
+ argv[1] += 6;
+ }
+
+ if(!filename) {
+ xasprintf(&filename, "%s/%s", hosts_dir, argv[1]);
+ char *dash = strchr(argv[1], '-');
+ if(dash) {
+ *dash++ = 0;
+ if((strcmp(dash, "up") && strcmp(dash, "down")) || !check_id(argv[1])) {
+ fprintf(stderr, "Invalid configuration filename.\n");
+ return 1;
+ }
+ }
+ }
+
+#ifndef HAVE_MINGW
+ char *editor = getenv("VISUAL") ?: getenv("EDITOR") ?: "vi";
+#else
+ char *editor = "edit"
+#endif
+
+ char *command;
+ xasprintf(&command, "\"%s\" \"%s\"", editor, filename);
+ int result = system(command);
+ if(result)
+ return result;
+
+ // Silently try notifying a running tincd of changes.
+ fclose(stderr);
+
+ if(connect_tincd())
+ sendline(fd, "%d %d", CONTROL, REQ_RELOAD);