tincd.c 18 KB

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