- fprintf(stderr, "Generating %d bits keys:\n", bits);
- rsa_key = RSA_generate_key(bits, 0x10001, indicator, NULL);
-
- if(!rsa_key) {
- fprintf(stderr, "Error during key generation!\n");
- return false;
- } else
- fprintf(stderr, "Done.\n");
-
- xasprintf(&filename, "%s/rsa_key.priv", confbase);
- f = ask_and_open(filename, "private RSA key");
-
- if(!f)
- return false;
-
- if(disable_old_keys(f))
- fprintf(stderr, "Warning: old key(s) found and disabled.\n");
-
-#ifdef HAVE_FCHMOD
- /* Make it unreadable for others. */
- fchmod(fileno(f), 0600);
-#endif
-
- fputc('\n', f);
- PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
- fclose(f);
- free(filename);
-
- if(name)
- xasprintf(&filename, "%s/hosts/%s", confbase, name);
- else
- xasprintf(&filename, "%s/rsa_key.pub", confbase);
-
- f = ask_and_open(filename, "public RSA key");
-
- if(!f)
- return false;
-
- if(disable_old_keys(f))
- fprintf(stderr, "Warning: old key(s) found and disabled.\n");
-
- fputc('\n', f);
- PEM_write_RSAPublicKey(f, rsa_key);
- fclose(f);
- free(filename);
- if(name)
- free(name);
-
- return true;
-}
-
-/*
- Set all files and paths according to netname
-*/
-static void make_names(void) {
-#ifdef HAVE_MINGW
- HKEY key;
- char installdir[1024] = "";
- long len = sizeof(installdir);
-#endif
-
- if(netname)
- xasprintf(&identname, "tinc.%s", netname);
- else
- identname = xstrdup("tinc");
-
-#ifdef HAVE_MINGW
- if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
- if(!RegQueryValueEx(key, NULL, 0, 0, installdir, &len)) {
- if(!logfilename)
- xasprintf(&logfilename, "%s/log/%s.log", identname);
- if(!confbase) {
- if(netname)
- xasprintf(&confbase, "%s/%s", installdir, netname);
- else
- xasprintf(&confbase, "%s", installdir);
- }
- }
- RegCloseKey(key);
- if(*installdir)
- return;
- }
-#endif
-
- if(!pidfilename)
- xasprintf(&pidfilename, LOCALSTATEDIR "/run/%s.pid", identname);
-
- if(!logfilename)
- xasprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
-
- if(netname) {
- if(!confbase)
- xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
- else
- logger(LOG_INFO, "Both netname and configuration directory given, using the latter...");
- } else {
- if(!confbase)
- xasprintf(&confbase, CONFDIR "/tinc");
- }
-}
-
-static void free_names() {
- if (identname) free(identname);
- if (netname) free(netname);
- if (pidfilename) free(pidfilename);
- if (logfilename) free(logfilename);
- if (confbase) free(confbase);
-}
-
-static bool drop_privs() {
-#ifdef HAVE_MINGW
- if (switchuser) {
- logger(LOG_ERR, "%s not supported on this platform", "-U");
- return false;
- }
- if (do_chroot) {
- logger(LOG_ERR, "%s not supported on this platform", "-R");
- return false;
- }
-#else
- uid_t uid = 0;
- if (switchuser) {
- struct passwd *pw = getpwnam(switchuser);
- if (!pw) {
- logger(LOG_ERR, "unknown user `%s'", switchuser);
- return false;
- }
- uid = pw->pw_uid;
- if (initgroups(switchuser, pw->pw_gid) != 0 ||
- setgid(pw->pw_gid) != 0) {
- logger(LOG_ERR, "System call `%s' failed: %s",
- "initgroups", strerror(errno));
- return false;
- }
- endgrent();
- endpwent();
- }
- if (do_chroot) {
- tzset(); /* for proper timestamps in logs */
- if (chroot(confbase) != 0 || chdir("/") != 0) {
- logger(LOG_ERR, "System call `%s' failed: %s",
- "chroot", strerror(errno));
- return false;
- }
- free(confbase);
- confbase = xstrdup("");
- }
- if (switchuser)
- if (setuid(uid) != 0) {
- logger(LOG_ERR, "System call `%s' failed: %s",
- "setuid", strerror(errno));
- return false;
- }
-#endif