tincd.c 18 KB

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