tincd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. tincd.c -- the main file for tincd
  3. Copyright (C) 1998-2005 Ivo Timmermans
  4. 2000-2019 Guus Sliepen <guus@tinc-vpn.org>
  5. 2008 Max Rijevski <maksuf@gmail.com>
  6. 2009 Michael Tokarev <mjt@tls.msk.ru>
  7. 2010 Julien Muchembled <jm@jmuchemb.eu>
  8. 2010 Timothy Redaelli <timothy@redaelli.eu>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License along
  18. with this program; if not, write to the Free Software Foundation, Inc.,
  19. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "system.h"
  22. /* Darwin (MacOS/X) needs the following definition... */
  23. #ifndef _P1003_1B_VISIBLE
  24. #define _P1003_1B_VISIBLE
  25. #endif
  26. #ifdef HAVE_SYS_MMAN_H
  27. #include <sys/mman.h>
  28. #endif
  29. #include <openssl/rand.h>
  30. #include <openssl/rsa.h>
  31. #include <openssl/pem.h>
  32. #include <openssl/evp.h>
  33. #include <openssl/engine.h>
  34. #include <openssl/bn.h>
  35. #ifdef HAVE_LZO
  36. #include LZO1X_H
  37. #endif
  38. #ifndef HAVE_MINGW
  39. #include <pwd.h>
  40. #include <grp.h>
  41. #include <time.h>
  42. #endif
  43. #ifdef HAVE_GETOPT_LONG
  44. #include <getopt.h>
  45. #else
  46. #include "getopt.h"
  47. #endif
  48. #include "pidfile.h"
  49. #include "conf.h"
  50. #include "device.h"
  51. #include "logger.h"
  52. #include "net.h"
  53. #include "netutl.h"
  54. #include "process.h"
  55. #include "protocol.h"
  56. #include "utils.h"
  57. #include "xalloc.h"
  58. /* The name this program was run with. */
  59. char *program_name = NULL;
  60. /* If nonzero, display usage information and exit. */
  61. bool show_help = false;
  62. /* If nonzero, print the version on standard output and exit. */
  63. bool show_version = false;
  64. /* If nonzero, it will attempt to kill a running tincd and exit. */
  65. int kill_tincd = 0;
  66. /* If nonzero, generate public/private keypair for this host/net. */
  67. int generate_keys = 0;
  68. /* If nonzero, use null ciphers and skip all key exchanges. */
  69. bool bypass_security = false;
  70. /* If nonzero, disable swapping for this process. */
  71. bool do_mlock = false;
  72. /* If nonzero, chroot to netdir after startup. */
  73. static bool do_chroot = false;
  74. /* If !NULL, do setuid to given user after startup */
  75. static const char *switchuser = NULL;
  76. /* If nonzero, write log entries to a separate file. */
  77. bool use_logfile = false;
  78. char *identname = NULL; /* program name for syslog */
  79. char *pidfilename = NULL; /* pid file location */
  80. char *logfilename = NULL; /* log file location */
  81. char **g_argv; /* a copy of the cmdline arguments */
  82. static int status = 1;
  83. static struct option const long_options[] = {
  84. {"config", required_argument, NULL, 'c'},
  85. {"kill", optional_argument, NULL, 'k'},
  86. {"net", required_argument, NULL, 'n'},
  87. {"help", no_argument, NULL, 1},
  88. {"version", no_argument, NULL, 2},
  89. {"no-detach", no_argument, NULL, 'D'},
  90. {"generate-keys", optional_argument, NULL, 'K'},
  91. {"debug", optional_argument, NULL, 'd'},
  92. {"bypass-security", no_argument, NULL, 3},
  93. {"mlock", no_argument, NULL, 'L'},
  94. {"chroot", no_argument, NULL, 'R'},
  95. {"user", required_argument, NULL, 'U'},
  96. {"logfile", optional_argument, NULL, 4},
  97. {"pidfile", required_argument, NULL, 5},
  98. {"option", required_argument, NULL, 'o'},
  99. {NULL, 0, NULL, 0}
  100. };
  101. #ifdef HAVE_MINGW
  102. static struct WSAData wsa_state;
  103. CRITICAL_SECTION mutex;
  104. int main2(int argc, char **argv);
  105. #endif
  106. static void usage(bool status) {
  107. if(status)
  108. fprintf(stderr, "Try `%s --help\' for more information.\n",
  109. program_name);
  110. else {
  111. printf("Usage: %s [option]...\n\n", program_name);
  112. printf(" -c, --config=DIR Read configuration options from DIR.\n"
  113. " -D, --no-detach Don't fork and detach.\n"
  114. " -d, --debug[=LEVEL] Increase debug level or set it to LEVEL.\n"
  115. " -k, --kill[=SIGNAL] Attempt to kill a running tincd and exit.\n"
  116. " -n, --net=NETNAME Connect to net NETNAME.\n"
  117. " -K, --generate-keys[=BITS] Generate public/private RSA keypair.\n"
  118. " -L, --mlock Lock tinc into main memory.\n"
  119. " --logfile[=FILENAME] Write log entries to a logfile.\n"
  120. " --pidfile=FILENAME Write PID to FILENAME.\n"
  121. " -o, --option=[HOST.]KEY=VALUE Set global/host configuration value.\n"
  122. " -R, --chroot chroot to NET dir at startup.\n"
  123. " -U, --user=USER setuid to given USER at startup.\n"
  124. " --help Display this help and exit.\n"
  125. " --version Output version information and exit.\n\n");
  126. printf("Report bugs to tinc@tinc-vpn.org.\n");
  127. }
  128. }
  129. static bool parse_options(int argc, char **argv) {
  130. config_t *cfg;
  131. int r;
  132. int option_index = 0;
  133. int lineno = 0;
  134. cmdline_conf = list_alloc((list_action_t)free_config);
  135. while((r = getopt_long(argc, argv, "c:DLd::k::n:o:K::RU:", long_options, &option_index)) != EOF) {
  136. switch(r) {
  137. case 0: /* long option */
  138. break;
  139. case 'c': /* config file */
  140. if(confbase) {
  141. fprintf(stderr, "Only one configuration directory can be given.\n");
  142. usage(true);
  143. return false;
  144. }
  145. confbase = xstrdup(optarg);
  146. break;
  147. case 'D': /* no detach */
  148. do_detach = false;
  149. break;
  150. case 'L': /* no detach */
  151. #ifndef HAVE_MLOCKALL
  152. logger(LOG_ERR, "%s not supported on this platform", "mlockall()");
  153. return false;
  154. #else
  155. do_mlock = true;
  156. break;
  157. #endif
  158. case 'd': /* increase debug level */
  159. if(!optarg && optind < argc && *argv[optind] != '-') {
  160. optarg = argv[optind++];
  161. }
  162. if(optarg) {
  163. debug_level = atoi(optarg);
  164. } else {
  165. debug_level++;
  166. }
  167. break;
  168. case 'k': /* kill old tincds */
  169. #ifndef HAVE_MINGW
  170. if(!optarg && optind < argc && *argv[optind] != '-') {
  171. optarg = argv[optind++];
  172. }
  173. if(optarg) {
  174. if(!strcasecmp(optarg, "HUP")) {
  175. kill_tincd = SIGHUP;
  176. } else if(!strcasecmp(optarg, "TERM")) {
  177. kill_tincd = SIGTERM;
  178. } else if(!strcasecmp(optarg, "KILL")) {
  179. kill_tincd = SIGKILL;
  180. } else if(!strcasecmp(optarg, "USR1")) {
  181. kill_tincd = SIGUSR1;
  182. } else if(!strcasecmp(optarg, "USR2")) {
  183. kill_tincd = SIGUSR2;
  184. } else if(!strcasecmp(optarg, "WINCH")) {
  185. kill_tincd = SIGWINCH;
  186. } else if(!strcasecmp(optarg, "INT")) {
  187. kill_tincd = SIGINT;
  188. } else if(!strcasecmp(optarg, "ALRM")) {
  189. kill_tincd = SIGALRM;
  190. } else if(!strcasecmp(optarg, "ABRT")) {
  191. kill_tincd = SIGABRT;
  192. } else {
  193. kill_tincd = atoi(optarg);
  194. if(!kill_tincd) {
  195. fprintf(stderr, "Invalid argument `%s'; SIGNAL must be a number or one of HUP, TERM, KILL, USR1, USR2, WINCH, INT or ALRM.\n",
  196. optarg);
  197. usage(true);
  198. return false;
  199. }
  200. }
  201. } else {
  202. kill_tincd = SIGTERM;
  203. }
  204. #else
  205. kill_tincd = 1;
  206. #endif
  207. break;
  208. case 'n': /* net name given */
  209. /* netname "." is special: a "top-level name" */
  210. if(netname) {
  211. fprintf(stderr, "Only one netname can be given.\n");
  212. usage(true);
  213. return false;
  214. }
  215. if(optarg && strcmp(optarg, ".")) {
  216. netname = xstrdup(optarg);
  217. }
  218. break;
  219. case 'o': /* option */
  220. cfg = parse_config_line(optarg, NULL, ++lineno);
  221. if(!cfg) {
  222. return false;
  223. }
  224. list_insert_tail(cmdline_conf, cfg);
  225. break;
  226. case 'K': /* generate public/private keypair */
  227. if(!optarg && optind < argc && *argv[optind] != '-') {
  228. optarg = argv[optind++];
  229. }
  230. if(optarg) {
  231. generate_keys = atoi(optarg);
  232. if(generate_keys < 512) {
  233. fprintf(stderr, "Invalid argument `%s'; BITS must be a number equal to or greater than 512.\n",
  234. optarg);
  235. usage(true);
  236. return false;
  237. }
  238. generate_keys &= ~7; /* Round it to bytes */
  239. } else {
  240. generate_keys = 2048;
  241. }
  242. break;
  243. case 'R': /* chroot to NETNAME dir */
  244. do_chroot = true;
  245. break;
  246. case 'U': /* setuid to USER */
  247. switchuser = optarg;
  248. break;
  249. case 1: /* show help */
  250. show_help = true;
  251. break;
  252. case 2: /* show version */
  253. show_version = true;
  254. break;
  255. case 3: /* bypass security */
  256. bypass_security = true;
  257. break;
  258. case 4: /* write log entries to a file */
  259. use_logfile = true;
  260. if(!optarg && optind < argc && *argv[optind] != '-') {
  261. optarg = argv[optind++];
  262. }
  263. if(optarg) {
  264. if(logfilename) {
  265. fprintf(stderr, "Only one logfile can be given.\n");
  266. usage(true);
  267. return false;
  268. }
  269. logfilename = xstrdup(optarg);
  270. }
  271. break;
  272. case 5: /* write PID to a file */
  273. if(pidfilename) {
  274. fprintf(stderr, "Only one pidfile can be given.\n");
  275. usage(true);
  276. return false;
  277. }
  278. pidfilename = xstrdup(optarg);
  279. break;
  280. case '?':
  281. usage(true);
  282. return false;
  283. default:
  284. break;
  285. }
  286. }
  287. if(optind < argc) {
  288. fprintf(stderr, "%s: unrecognized argument '%s'\n", argv[0], argv[optind]);
  289. usage(true);
  290. return false;
  291. }
  292. return true;
  293. }
  294. /* This function prettyprints the key generation process */
  295. static int indicator(int a, int b, BN_GENCB *cb) {
  296. (void)cb;
  297. switch(a) {
  298. case 0:
  299. fprintf(stderr, ".");
  300. break;
  301. case 1:
  302. fprintf(stderr, "+");
  303. break;
  304. case 2:
  305. fprintf(stderr, "-");
  306. break;
  307. case 3:
  308. switch(b) {
  309. case 0:
  310. fprintf(stderr, " p\n");
  311. break;
  312. case 1:
  313. fprintf(stderr, " q\n");
  314. break;
  315. default:
  316. fprintf(stderr, "?");
  317. }
  318. break;
  319. default:
  320. fprintf(stderr, "?");
  321. }
  322. return 1;
  323. }
  324. /*
  325. Generate a public/private RSA keypair, and ask for a file to store
  326. them in.
  327. */
  328. static bool keygen(int bits) {
  329. BIGNUM *e = NULL;
  330. RSA *rsa_key;
  331. FILE *f;
  332. char filename[PATH_MAX];
  333. BN_GENCB *cb;
  334. int result;
  335. fprintf(stderr, "Generating %d bits keys:\n", bits);
  336. cb = BN_GENCB_new();
  337. if(!cb) {
  338. abort();
  339. }
  340. BN_GENCB_set(cb, indicator, NULL);
  341. rsa_key = RSA_new();
  342. if(BN_hex2bn(&e, "10001") == 0) {
  343. abort();
  344. }
  345. if(!rsa_key || !e) {
  346. abort();
  347. }
  348. result = RSA_generate_key_ex(rsa_key, bits, e, cb);
  349. BN_free(e);
  350. BN_GENCB_free(cb);
  351. if(!result) {
  352. fprintf(stderr, "Error during key generation!\n");
  353. RSA_free(rsa_key);
  354. return false;
  355. } else {
  356. fprintf(stderr, "Done.\n");
  357. }
  358. snprintf(filename, sizeof(filename), "%s/rsa_key.priv", confbase);
  359. f = ask_and_open(filename, "private RSA key");
  360. if(!f) {
  361. RSA_free(rsa_key);
  362. return false;
  363. }
  364. #ifdef HAVE_FCHMOD
  365. /* Make it unreadable for others. */
  366. fchmod(fileno(f), 0600);
  367. #endif
  368. fputc('\n', f);
  369. PEM_write_RSAPrivateKey(f, rsa_key, NULL, NULL, 0, NULL, NULL);
  370. fclose(f);
  371. char *name = get_name();
  372. if(name) {
  373. snprintf(filename, sizeof(filename), "%s/hosts/%s", confbase, name);
  374. free(name);
  375. } else {
  376. snprintf(filename, sizeof(filename), "%s/rsa_key.pub", confbase);
  377. }
  378. f = ask_and_open(filename, "public RSA key");
  379. if(!f) {
  380. RSA_free(rsa_key);
  381. return false;
  382. }
  383. fputc('\n', f);
  384. PEM_write_RSAPublicKey(f, rsa_key);
  385. fclose(f);
  386. RSA_free(rsa_key);
  387. return true;
  388. }
  389. /*
  390. Set all files and paths according to netname
  391. */
  392. static void make_names(void) {
  393. #ifdef HAVE_MINGW
  394. HKEY key;
  395. char installdir[1024] = "";
  396. DWORD len = sizeof(installdir);
  397. #endif
  398. if(netname) {
  399. xasprintf(&identname, "tinc.%s", netname);
  400. } else {
  401. identname = xstrdup("tinc");
  402. }
  403. #ifdef HAVE_MINGW
  404. if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\tinc", 0, KEY_READ, &key)) {
  405. if(!RegQueryValueEx(key, NULL, 0, 0, (LPBYTE)installdir, &len)) {
  406. if(!confbase) {
  407. if(netname) {
  408. xasprintf(&confbase, "%s/%s", installdir, netname);
  409. } else {
  410. xasprintf(&confbase, "%s", installdir);
  411. }
  412. }
  413. if(!logfilename) {
  414. xasprintf(&logfilename, "%s/tinc.log", confbase);
  415. }
  416. }
  417. RegCloseKey(key);
  418. if(*installdir) {
  419. return;
  420. }
  421. }
  422. #endif
  423. if(!pidfilename) {
  424. xasprintf(&pidfilename, RUNSTATEDIR "/%s.pid", identname);
  425. }
  426. if(!logfilename) {
  427. xasprintf(&logfilename, LOCALSTATEDIR "/log/%s.log", identname);
  428. }
  429. if(netname) {
  430. if(!confbase) {
  431. xasprintf(&confbase, CONFDIR "/tinc/%s", netname);
  432. } else {
  433. logger(LOG_INFO, "Both netname and configuration directory given, using the latter...");
  434. }
  435. } else {
  436. if(!confbase) {
  437. xasprintf(&confbase, CONFDIR "/tinc");
  438. }
  439. }
  440. }
  441. static void free_names() {
  442. free(identname);
  443. free(netname);
  444. free(pidfilename);
  445. free(logfilename);
  446. free(confbase);
  447. }
  448. static bool drop_privs() {
  449. #ifdef HAVE_MINGW
  450. if(switchuser) {
  451. logger(LOG_ERR, "%s not supported on this platform", "-U");
  452. return false;
  453. }
  454. if(do_chroot) {
  455. logger(LOG_ERR, "%s not supported on this platform", "-R");
  456. return false;
  457. }
  458. #else
  459. uid_t uid = 0;
  460. if(switchuser) {
  461. struct passwd *pw = getpwnam(switchuser);
  462. if(!pw) {
  463. logger(LOG_ERR, "unknown user `%s'", switchuser);
  464. return false;
  465. }
  466. uid = pw->pw_uid;
  467. if(initgroups(switchuser, pw->pw_gid) != 0 ||
  468. setgid(pw->pw_gid) != 0) {
  469. logger(LOG_ERR, "System call `%s' failed: %s",
  470. "initgroups", strerror(errno));
  471. return false;
  472. }
  473. #ifndef ANDROID
  474. // Not supported in android NDK
  475. endgrent();
  476. endpwent();
  477. #endif
  478. }
  479. if(do_chroot) {
  480. tzset(); /* for proper timestamps in logs */
  481. if(chroot(confbase) != 0 || chdir("/") != 0) {
  482. logger(LOG_ERR, "System call `%s' failed: %s",
  483. "chroot", strerror(errno));
  484. return false;
  485. }
  486. free(confbase);
  487. confbase = xstrdup("");
  488. }
  489. if(switchuser)
  490. if(setuid(uid) != 0) {
  491. logger(LOG_ERR, "System call `%s' failed: %s",
  492. "setuid", strerror(errno));
  493. return false;
  494. }
  495. #endif
  496. return true;
  497. }
  498. #ifdef HAVE_MINGW
  499. # define setpriority(level) !SetPriorityClass(GetCurrentProcess(), (level))
  500. #else
  501. # define NORMAL_PRIORITY_CLASS 0
  502. # define BELOW_NORMAL_PRIORITY_CLASS 10
  503. # define HIGH_PRIORITY_CLASS -10
  504. # define setpriority(level) (setpriority(PRIO_PROCESS, 0, (level)))
  505. #endif
  506. int main(int argc, char **argv) {
  507. program_name = argv[0];
  508. if(!parse_options(argc, argv)) {
  509. return 1;
  510. }
  511. if(show_version) {
  512. printf("%s version %s\n", PACKAGE, VERSION);
  513. printf("Copyright (C) 1998-2019 Ivo Timmermans, Guus Sliepen and others.\n"
  514. "See the AUTHORS file for a complete list.\n\n"
  515. "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
  516. "and you are welcome to redistribute it under certain conditions;\n"
  517. "see the file COPYING for details.\n");
  518. return 0;
  519. }
  520. if(show_help) {
  521. usage(false);
  522. return 0;
  523. }
  524. make_names();
  525. if(kill_tincd) {
  526. return !kill_other(kill_tincd);
  527. }
  528. openlogger("tinc", use_logfile ? LOGMODE_FILE : LOGMODE_STDERR);
  529. g_argv = argv;
  530. if(getenv("LISTEN_PID") && atoi(getenv("LISTEN_PID")) == getpid()) {
  531. do_detach = false;
  532. }
  533. #ifdef HAVE_UNSETENV
  534. unsetenv("LISTEN_PID");
  535. #endif
  536. init_configuration(&config_tree);
  537. ENGINE_load_builtin_engines();
  538. if(generate_keys) {
  539. read_server_config();
  540. return !keygen(generate_keys);
  541. }
  542. if(!read_server_config()) {
  543. return 1;
  544. }
  545. #ifdef HAVE_LZO
  546. if(lzo_init() != LZO_E_OK) {
  547. logger(LOG_ERR, "Error initializing LZO compressor!");
  548. return 1;
  549. }
  550. #endif
  551. #ifdef HAVE_MINGW
  552. if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) {
  553. logger(LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError()));
  554. return 1;
  555. }
  556. if(!do_detach || !init_service()) {
  557. return main2(argc, argv);
  558. } else {
  559. return 1;
  560. }
  561. }
  562. int main2(int argc, char **argv) {
  563. InitializeCriticalSection(&mutex);
  564. EnterCriticalSection(&mutex);
  565. #endif
  566. char *priority = NULL;
  567. if(!detach()) {
  568. return 1;
  569. }
  570. #ifdef HAVE_MLOCKALL
  571. /* Lock all pages into memory if requested.
  572. * This has to be done after daemon()/fork() so it works for child.
  573. * No need to do that in parent as it's very short-lived. */
  574. if(do_mlock && mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
  575. logger(LOG_ERR, "System call `%s' failed: %s", "mlockall",
  576. strerror(errno));
  577. return 1;
  578. }
  579. #endif
  580. /* Setup sockets and open device. */
  581. if(!setup_network()) {
  582. goto end;
  583. }
  584. /* Initiate all outgoing connections. */
  585. try_outgoing_connections();
  586. /* Change process priority */
  587. if(get_config_string(lookup_config(config_tree, "ProcessPriority"), &priority)) {
  588. if(!strcasecmp(priority, "Normal")) {
  589. if(setpriority(NORMAL_PRIORITY_CLASS) != 0) {
  590. logger(LOG_ERR, "System call `%s' failed: %s",
  591. "setpriority", strerror(errno));
  592. goto end;
  593. }
  594. } else if(!strcasecmp(priority, "Low")) {
  595. if(setpriority(BELOW_NORMAL_PRIORITY_CLASS) != 0) {
  596. logger(LOG_ERR, "System call `%s' failed: %s",
  597. "setpriority", strerror(errno));
  598. goto end;
  599. }
  600. } else if(!strcasecmp(priority, "High")) {
  601. if(setpriority(HIGH_PRIORITY_CLASS) != 0) {
  602. logger(LOG_ERR, "System call `%s' failed: %s",
  603. "setpriority", strerror(errno));
  604. goto end;
  605. }
  606. } else {
  607. logger(LOG_ERR, "Invalid priority `%s`!", priority);
  608. goto end;
  609. }
  610. }
  611. /* drop privileges */
  612. if(!drop_privs()) {
  613. goto end;
  614. }
  615. /* Start main loop. It only exits when tinc is killed. */
  616. status = main_loop();
  617. /* Shutdown properly. */
  618. ifdebug(CONNECTIONS)
  619. devops.dump_stats();
  620. close_network_connections();
  621. end:
  622. logger(LOG_NOTICE, "Terminating");
  623. #ifndef HAVE_MINGW
  624. remove_pid(pidfilename);
  625. #endif
  626. free(priority);
  627. exit_configuration(&config_tree);
  628. list_delete_list(cmdline_conf);
  629. free_names();
  630. return status;
  631. }