-int
-main(int argc, char **argv, char **envp)
-{
- program_name = argv[0];
-
- setlocale (LC_ALL, "");
- bindtextdomain (PACKAGE, LOCALEDIR);
- textdomain (PACKAGE);
-
- /* Do some intl stuff right now */
-
- unknown = _("unknown");
-
- environment = envp;
- parse_options(argc, argv, envp);
-
- if(show_version)
- {
- printf(_("%s version %s (built %s %s, protocol %d)\n"), PACKAGE, VERSION, __DATE__, __TIME__, PROT_CURRENT);
- printf(_("Copyright (C) 1998-2001 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(0);
-
- if(geteuid())
- {
- fprintf(stderr, _("You must be root to run this program.\n"));
- return 1;
- }
-
- openlog("tinc", LOG_PERROR, LOG_DAEMON); /* Catch all syslog() calls issued before detaching */
-
- g_argv = argv;
-
- make_names();
-
- /* Slllluuuuuuurrrrp! */
-cp
- RAND_load_file("/dev/urandom", 1024);
-cp
- if(generate_keys)
- {
- read_server_config();
- exit(keygen(generate_keys));
- }
-
- if(kill_tincd)
- exit(kill_other());
-
- if(read_server_config())
- exit(1);
-cp
- if(detach())
- exit(0);
-cp
- if(debug_lvl >= DEBUG_ERROR)
- ERR_load_crypto_strings();
-
- for(;;)
- {
- if(!setup_network_connections())
- {
- main_loop();
- cleanup_and_exit(1);
- }
-
- syslog(LOG_ERR, _("Unrecoverable error"));
- cp_trace();
-
- if(do_detach)
- {
- syslog(LOG_NOTICE, _("Restarting in %d seconds!"), MAXTIMEOUT);
- sleep(MAXTIMEOUT);
- }
- else
- {
- syslog(LOG_ERR, _("Not restarting."));
- exit(1);
+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;
+ }
+#ifndef __ANDROID__
+// Not supported in android NDK
+ endgrent();
+ endpwent();
+#endif
+ }
+ 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
+ return true;
+}
+
+#ifdef HAVE_MINGW
+# define setpriority(level) !SetPriorityClass(GetCurrentProcess(), (level))
+#else
+# define NORMAL_PRIORITY_CLASS 0
+# define BELOW_NORMAL_PRIORITY_CLASS 10
+# define HIGH_PRIORITY_CLASS -10
+# define setpriority(level) (setpriority(PRIO_PROCESS, 0, (level)))
+#endif
+
+int main(int argc, char **argv) {
+ program_name = argv[0];
+
+ if(!parse_options(argc, argv))
+ return 1;
+
+ make_names();
+
+ if(show_version) {
+ printf("%s version %s\n", PACKAGE, VERSION);
+ printf("Copyright (C) 1998-2017 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(kill_tincd)
+ return !kill_other(kill_tincd);
+
+ openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR);
+
+ g_argv = argv;
+
+ if(getenv("LISTEN_PID") && atoi(getenv("LISTEN_PID")) == getpid())
+ do_detach = false;
+#ifdef HAVE_UNSETENV
+ unsetenv("LISTEN_PID");
+#endif
+
+ init_configuration(&config_tree);
+
+ /* Slllluuuuuuurrrrp! */
+
+ RAND_load_file("/dev/urandom", 1024);
+
+ ENGINE_load_builtin_engines();
+ ENGINE_register_all_complete();
+
+ OpenSSL_add_all_algorithms();
+
+ if(generate_keys) {
+ read_server_config();
+ return !keygen(generate_keys);
+ }
+
+ if(!read_server_config())
+ return 1;
+
+#ifdef HAVE_LZO
+ if(lzo_init() != LZO_E_OK) {
+ logger(LOG_ERR, "Error initializing LZO compressor!");
+ return 1;
+ }
+#endif
+
+#ifdef HAVE_MINGW
+ if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
+ logger(LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
+ return 1;
+ }
+
+ if(!do_detach || !init_service())
+ return main2(argc, argv);
+ else
+ return 1;
+}
+
+int main2(int argc, char **argv) {
+ InitializeCriticalSection(&mutex);
+ EnterCriticalSection(&mutex);
+#endif
+ char *priority = NULL;
+
+ if(!detach())
+ return 1;
+
+#ifdef HAVE_MLOCKALL
+ /* Lock all pages into memory if requested.
+ * This has to be done after daemon()/fork() so it works for child.
+ * No need to do that in parent as it's very short-lived. */
+ if(do_mlock && mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
+ logger(LOG_ERR, "System call `%s' failed: %s", "mlockall",
+ strerror(errno));
+ return 1;
+ }
+#endif
+
+ /* Setup sockets and open device. */
+
+ if(!setup_network())
+ goto end;
+
+ /* Initiate all outgoing connections. */
+
+ try_outgoing_connections();
+
+ /* Change process priority */
+
+ if(get_config_string(lookup_config(config_tree, "ProcessPriority"), &priority)) {
+ if(!strcasecmp(priority, "Normal")) {
+ if (setpriority(NORMAL_PRIORITY_CLASS) != 0) {
+ logger(LOG_ERR, "System call `%s' failed: %s",
+ "setpriority", strerror(errno));
+ goto end;
+ }
+ } else if(!strcasecmp(priority, "Low")) {
+ if (setpriority(BELOW_NORMAL_PRIORITY_CLASS) != 0) {
+ logger(LOG_ERR, "System call `%s' failed: %s",
+ "setpriority", strerror(errno));
+ goto end;
+ }
+ } else if(!strcasecmp(priority, "High")) {
+ if (setpriority(HIGH_PRIORITY_CLASS) != 0) {
+ logger(LOG_ERR, "System call `%s' failed: %s",
+ "setpriority", strerror(errno));
+ goto end;
+ }
+ } else {
+ logger(LOG_ERR, "Invalid priority `%s`!", priority);
+ goto end;
+ }