cli.cpp 4.1 KB

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