cli.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifndef __WINDOWS__
  31. #include <unistd.h>
  32. #endif
  33. #include "node/Node.hpp"
  34. #include "node/Constants.hpp"
  35. #include "node/Utils.hpp"
  36. #include "node/Thread.hpp"
  37. #include "node/Condition.hpp"
  38. using namespace ZeroTier;
  39. static void printHelp(FILE *out,const char *exename)
  40. {
  41. fprintf(out,"Usage: %s [-switches] <command>"ZT_EOL_S,exename);
  42. fprintf(out,ZT_EOL_S);
  43. fprintf(out,"Available switches:"ZT_EOL_S);
  44. fprintf(out," -c<port> - Communicate with daemon over this local port"ZT_EOL_S);
  45. fprintf(out," -t<token> - Specify token on command line"ZT_EOL_S);
  46. fprintf(out," -T<file> - Read token from file"ZT_EOL_S);
  47. fprintf(out,ZT_EOL_S);
  48. fprintf(out,"Use the 'help' command to get help from ZeroTier One itself."ZT_EOL_S);
  49. }
  50. static volatile unsigned int numResults = 0;
  51. static Condition doneCondition;
  52. static void resultHandler(void *arg,unsigned long id,const char *line)
  53. {
  54. ++numResults;
  55. if (strlen(line))
  56. fprintf(stdout,"%s"ZT_EOL_S,line);
  57. else doneCondition.signal();
  58. }
  59. int main(int argc,char **argv)
  60. {
  61. if (argc <= 1) {
  62. printHelp(stdout,argv[0]);
  63. return -1;
  64. }
  65. std::string authToken;
  66. std::string command;
  67. bool pastSwitches = false;
  68. unsigned int controlPort = 0;
  69. for(int i=1;i<argc;++i) {
  70. if ((argv[i][0] == '-')&&(!pastSwitches)) {
  71. if (strlen(argv[i]) <= 1) {
  72. printHelp(stdout,argv[0]);
  73. return -1;
  74. }
  75. switch(argv[i][1]) {
  76. case 'c':
  77. controlPort = Utils::strToUInt(argv[i] + 2);
  78. break;
  79. case 't':
  80. authToken.assign(argv[i] + 2);
  81. break;
  82. case 'T':
  83. if (!Utils::readFile(argv[i] + 2,authToken)) {
  84. fprintf(stdout,"FATAL ERROR: unable to read token from '%s'"ZT_EOL_S,argv[i] + 2);
  85. return -2;
  86. }
  87. break;
  88. default:
  89. return -1;
  90. }
  91. } else {
  92. pastSwitches = true;
  93. if (command.length())
  94. command.push_back(' ');
  95. command.append(argv[i]);
  96. }
  97. }
  98. if (!authToken.length()) {
  99. const char *home = getenv("HOME");
  100. if (home) {
  101. std::string dotZeroTierAuthToken(home);
  102. dotZeroTierAuthToken.push_back(ZT_PATH_SEPARATOR);
  103. dotZeroTierAuthToken.append(".zerotierOneAuthToken");
  104. if (!Utils::readFile(dotZeroTierAuthToken.c_str(),authToken)) {
  105. #ifndef __WINDOWS__
  106. #ifdef __APPLE__
  107. const char *systemAuthTokenPath = "/Library/Application Support/ZeroTier/One/authtoken.secret";
  108. #else
  109. const char *systemAuthTokenPath = "/var/lib/zerotier-one/authtoken.secret";
  110. #endif
  111. if (!Utils::readFile(systemAuthTokenPath,authToken)) {
  112. fprintf(stdout,"FATAL ERROR: no token specified on command line and could not read '%s' or '%s'"ZT_EOL_S,dotZeroTierAuthToken.c_str(),systemAuthTokenPath);
  113. return -2;
  114. }
  115. #else // __WINDOWS__
  116. fprintf(stdout,"FATAL ERROR: no token specified on command line and could not read '%s'"ZT_EOL_S,dotZeroTierAuthToken.c_str());
  117. return -2;
  118. #endif // __WINDOWS__
  119. }
  120. }
  121. }
  122. if (!authToken.length()) {
  123. fprintf(stdout,"FATAL ERROR: could not find auth token"ZT_EOL_S);
  124. return -2;
  125. }
  126. Node::LocalClient client(authToken.c_str(),controlPort,&resultHandler,(void *)0);
  127. client.send(command.c_str());
  128. doneCondition.wait(1000);
  129. if (!numResults) {
  130. fprintf(stdout,"ERROR: no results received. Is ZeroTier One running?"ZT_EOL_S);
  131. return -1;
  132. }
  133. return 0;
  134. }