-int main(int argc, char *argv[]) {
- int fd;
- int result;
- char host[128];
- char port[128];
- int pid;
-
- program_name = argv[0];
-
- if(!parse_options(argc, argv))
- return 1;
-
- make_names();
-
- if(show_version) {
- printf("%s version %s (built %s %s, protocol %d.%d)\n", PACKAGE,
- VERSION, __DATE__, __TIME__, PROT_MAJOR, PROT_MINOR);
- printf("Copyright (C) 1998-2012 Ivo Timmermans, Guus Sliepen and others.\n"
- "See the AUTHORS file for a complete list.\n\n"
- "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
- "and you are welcome to redistribute it under certain conditions;\n"
- "see the file COPYING for details.\n");
-
- return 0;
- }
-
- if(show_help) {
- usage(false);
- return 0;
- }
-
- if(optind >= argc) {
- fprintf(stderr, "Not enough arguments.\n");
- usage(true);
- return 1;
- }
-
- // First handle commands that don't involve connecting to a running tinc daemon.
-
- if(!strcasecmp(argv[optind], "generate-rsa-keys")) {
- return !rsa_keygen(optind < argc - 1 ? atoi(argv[optind + 1]) : 2048);
- }
-
- if(!strcasecmp(argv[optind], "generate-ecdsa-keys")) {
- return !ecdsa_keygen();
- }
-
- if(!strcasecmp(argv[optind], "generate-keys")) {
- return !(rsa_keygen(optind < argc - 1 ? atoi(argv[optind + 1]) : 2048) && ecdsa_keygen());
- }
-
- if(!strcasecmp(argv[optind], "start")) {
- int i, j;
- char *c;
- char *slash = strrchr(argv[0], '/');
-#ifdef HAVE_MINGW
- if ((c = strrchr(argv[0], '\\')) > slash)
- slash = c;
-#endif
- if (slash++) {
- c = xmalloc((slash - argv[0]) + sizeof("tincd"));
- sprintf(c, "%.*stincd", (int)(slash - argv[0]), argv[0]);
- }
- else
- c = "tincd";
- argv[0] = c;
- for(i = j = 1; argv[i]; ++i)
- if (i != optind && strcmp(argv[i], "--") != 0)
- argv[j++] = argv[i];
- argv[j] = NULL;
- execvp(c, argv);
- fprintf(stderr, "Could not start %s: %s\n", c, strerror(errno));
- return 1;
- }
-
- if(!strcasecmp(argv[optind], "init")) {
- char *filename = NULL;
- xasprintf(&filename, "%s/tinc.conf", confbase);
- if(!access(confbase, F_OK)) {
- fprintf(stderr, "Configuration file %s already exists!\n", filename);
- return 1;
- }
-
- if(optind >= argc - 1) {
- if(isatty(0) && isatty(1)) {
- char buf[1024];
- fprintf(stdout, "Enter the Name you want your tinc node to have: ");
- fflush(stdout);
- if(!fgets(buf, sizeof buf, stdin)) {
- fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
- return 1;
- }
- int len = strlen(buf);
- if(len)
- buf[--len] = 0;
- if(!len) {
- fprintf(stderr, "No name given!\n");
- return 1;
- }
- name = strdup(buf);
- } else {
- fprintf(stderr, "No Name given!\n");
- return 1;
- }
- } else {
- name = strdup(argv[optind + 1]);
- if(!*name) {
- fprintf(stderr, "No Name given!\n");
- return 1;
- }
- }
-
- for(int i = 0; i < strlen(name); i++) {
- if(!isalnum(name[i]) && name[i] != '_') {
- fprintf(stderr, "Invalid Name! Only a-z, A-Z, 0-9 and _ are allowed characters.\n");
- return 1;
- }
- }
-
- if(mkdir(CONFDIR, 0755) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", CONFDIR, strerror(errno));
- return 1;
- }
-
- if(mkdir(confbase, 0755) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", confbase, strerror(errno));
- return 1;
- }
-
- char *filename2 = NULL;
- xasprintf(&filename2, "%s/hosts", confbase);
- if(mkdir(filename2, 0755) && errno != EEXIST) {
- fprintf(stderr, "Could not create directory %s: %s\n", filename, strerror(errno));
- return 1;
- }
-
- FILE *f = fopen(filename, "w");
- if(!f) {
- fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
- return 1;
- }
-
- fprintf(f, "Name = %s\n", name);
- fclose(f);
-
- fclose(stdin);
- if(!rsa_keygen(2048) || !ecdsa_keygen())
- return false;
-