main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <errno.h>
  32. #include <string>
  33. #include <stdexcept>
  34. #include <iostream>
  35. #include "node/Constants.hpp"
  36. #ifdef __WINDOWS__
  37. #include <Windows.h>
  38. #else
  39. #include <unistd.h>
  40. #include <pwd.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <signal.h>
  44. #endif
  45. #include <openssl/rand.h>
  46. #include "node/Node.hpp"
  47. #include "node/Utils.hpp"
  48. #include "launcher.h"
  49. using namespace ZeroTier;
  50. // ---------------------------------------------------------------------------
  51. // Override libcrypto default RAND_ with Utils::getSecureRandom(), which uses
  52. // a system strong random source. This is because OpenSSL libcrypto's default
  53. // RAND_ implementation uses uninitialized memory as one of its entropy
  54. // sources, which plays havoc with all kinds of debuggers and auditing tools.
  55. static void _zeroTier_rand_cleanup() {}
  56. static void _zeroTier_rand_add(const void *buf, int num, double add_entropy) {}
  57. static int _zeroTier_rand_status() { return 1; }
  58. static void _zeroTier_rand_seed(const void *buf, int num) {}
  59. static int _zeroTier_rand_bytes(unsigned char *buf, int num)
  60. {
  61. Utils::getSecureRandom(buf,num);
  62. return 1;
  63. }
  64. static RAND_METHOD _zeroTierRandMethod = {
  65. _zeroTier_rand_seed,
  66. _zeroTier_rand_bytes,
  67. _zeroTier_rand_cleanup,
  68. _zeroTier_rand_add,
  69. _zeroTier_rand_bytes,
  70. _zeroTier_rand_status
  71. };
  72. static void _initLibCrypto()
  73. {
  74. RAND_set_rand_method(&_zeroTierRandMethod);
  75. }
  76. // ---------------------------------------------------------------------------
  77. static Node *node = (Node *)0;
  78. static void printHelp(const char *cn,FILE *out)
  79. {
  80. fprintf(out,"ZeroTier One version %d.%d.%d\n(c)2012-2013 ZeroTier Networks LLC\nLicensed under the GNU General Public License v3\n\nUsage: %s <home directory>\n",Node::versionMajor(),Node::versionMinor(),Node::versionRevision(),cn);
  81. }
  82. #ifndef _WIN32
  83. static void sighandlerQuit(int sig)
  84. {
  85. Node *n = node;
  86. if (n)
  87. n->terminate();
  88. else exit(0);
  89. }
  90. #endif
  91. int main(int argc,char **argv)
  92. {
  93. #ifndef _WIN32
  94. signal(SIGHUP,SIG_IGN);
  95. signal(SIGPIPE,SIG_IGN);
  96. signal(SIGUSR1,SIG_IGN);
  97. signal(SIGUSR2,SIG_IGN);
  98. signal(SIGALRM,SIG_IGN);
  99. signal(SIGINT,&sighandlerQuit);
  100. signal(SIGTERM,&sighandlerQuit);
  101. signal(SIGQUIT,&sighandlerQuit);
  102. #endif
  103. _initLibCrypto();
  104. if (argc < 2) {
  105. printHelp(argv[0],stderr);
  106. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  107. }
  108. const char *homeDir = (const char *)0;
  109. for(int i=1;i<argc;++i) {
  110. if (argv[i][0] == '-') {
  111. switch(argv[i][1]) {
  112. default:
  113. printHelp(argv[0],stderr);
  114. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  115. }
  116. } else {
  117. if (homeDir) {
  118. printHelp(argv[0],stderr);
  119. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  120. }
  121. homeDir = argv[i];
  122. break;
  123. }
  124. }
  125. if ((!homeDir)||(strlen(homeDir) <= 0)) {
  126. printHelp(argv[0],stderr);
  127. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  128. }
  129. #ifndef _WIN32
  130. mkdir(homeDir,0755); // will fail if it already exists
  131. #endif
  132. int exitCode = ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  133. node = new Node(homeDir);
  134. const char *termReason = (char *)0;
  135. switch(node->run()) {
  136. case Node::NODE_RESTART_FOR_RECONFIGURATION:
  137. exitCode = ZT_EXEC_RETURN_VALUE_PLEASE_RESTART;
  138. break;
  139. case Node::NODE_UNRECOVERABLE_ERROR:
  140. exitCode = ZT_EXEC_RETURN_VALUE_UNRECOVERABLE_ERROR;
  141. termReason = node->reasonForTermination();
  142. fprintf(stderr,"%s: abnormal termination: %s\n",argv[0],(termReason) ? termReason : "(unknown reason)");
  143. break;
  144. case Node::NODE_NEW_VERSION_AVAILABLE:
  145. exitCode = ZT_EXEC_RETURN_VALUE_TERMINATED_FOR_UPGRADE;
  146. break;
  147. default:
  148. break;
  149. }
  150. delete node;
  151. node = (Node *)0;
  152. return exitCode;
  153. }