NodeControlClient.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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 "NodeControlClient.hpp"
  28. #include "../node/Constants.hpp"
  29. #include "../node/Utils.hpp"
  30. namespace ZeroTier {
  31. struct _NodeControlClientImpl
  32. {
  33. void (*resultHandler)(void *,const char *);
  34. void *arg;
  35. IpcConnection *ipcc;
  36. std::string err;
  37. };
  38. static void _CBipcResultHandler(void *arg,IpcConnection *ipcc,IpcConnection::EventType event,const char *result)
  39. {
  40. if ((event == IpcConnection::IPC_EVENT_COMMAND)&&(result)) {
  41. if (strcmp(result,"200 auth OK"))
  42. ((_NodeControlClientImpl *)arg)->resultHandler(((_NodeControlClientImpl *)arg)->arg,result);
  43. }
  44. }
  45. NodeControlClient::NodeControlClient(const char *hp,void (*resultHandler)(void *,const char *),void *arg,const char *authToken)
  46. throw() :
  47. _impl((void *)new _NodeControlClientImpl)
  48. {
  49. _NodeControlClientImpl *impl = (_NodeControlClientImpl *)_impl;
  50. impl->ipcc = (IpcConnection *)0;
  51. if (!hp)
  52. hp = ZT_DEFAULTS.defaultHomePath.c_str();
  53. std::string at;
  54. if (authToken)
  55. at = authToken;
  56. else if (!Utils::readFile(authTokenDefaultSystemPath(),at)) {
  57. if (!Utils::readFile(authTokenDefaultUserPath(),at)) {
  58. impl->err = "no authentication token specified and authtoken.secret not readable";
  59. return;
  60. }
  61. }
  62. std::string myid;
  63. if (Utils::readFile((std::string(hp) + ZT_PATH_SEPARATOR_S + "identity.public").c_str(),myid)) {
  64. std::string myaddr(myid.substr(0,myid.find(':')));
  65. if (myaddr.length() != 10)
  66. impl->err = "invalid address extracted from identity.public";
  67. else {
  68. try {
  69. impl->resultHandler = resultHandler;
  70. impl->arg = arg;
  71. impl->ipcc = new IpcConnection((std::string(ZT_IPC_ENDPOINT_BASE) + myaddr).c_str(),&_CBipcResultHandler,_impl);
  72. impl->ipcc->printf("auth %s"ZT_EOL_S,at.c_str());
  73. } catch ( ... ) {
  74. impl->ipcc = (IpcConnection *)0;
  75. impl->err = "failure connecting to running ZeroTier One service";
  76. }
  77. }
  78. } else impl->err = "unable to read identity.public";
  79. }
  80. NodeControlClient::~NodeControlClient()
  81. {
  82. if (_impl) {
  83. delete ((_NodeControlClientImpl *)_impl)->ipcc;
  84. delete (_NodeControlClientImpl *)_impl;
  85. }
  86. }
  87. const char *NodeControlClient::error() const
  88. throw()
  89. {
  90. if (((_NodeControlClientImpl *)_impl)->err.length())
  91. return ((_NodeControlClientImpl *)_impl)->err.c_str();
  92. return (const char *)0;
  93. }
  94. void NodeControlClient::send(const char *command)
  95. throw()
  96. {
  97. try {
  98. if (((_NodeControlClientImpl *)_impl)->ipcc)
  99. ((_NodeControlClientImpl *)_impl)->ipcc->printf("%s"ZT_EOL_S,command);
  100. } catch ( ... ) {}
  101. }
  102. std::vector<std::string> NodeControlClient::splitLine(const char *line)
  103. {
  104. return Utils::split(line," ","\\","\"");
  105. }
  106. const char *NodeControlClient::authTokenDefaultUserPath()
  107. {
  108. static std::string dlp;
  109. static Mutex dlp_m;
  110. Mutex::Lock _l(dlp_m);
  111. #ifdef __WINDOWS__
  112. if (!dlp.length()) {
  113. char buf[16384];
  114. if (SUCCEEDED(SHGetFolderPathA(NULL,CSIDL_APPDATA,NULL,0,buf)))
  115. dlp = (std::string(buf) + "\\ZeroTier\\One\\authtoken.secret");
  116. }
  117. #else // not __WINDOWS__
  118. if (!dlp.length()) {
  119. const char *home = getenv("HOME");
  120. if (home) {
  121. #ifdef __APPLE__
  122. dlp = (std::string(home) + "/Library/Application Support/ZeroTier/One/authtoken.secret");
  123. #else
  124. dlp = (std::string(home) + "/.zeroTierOneAuthToken");
  125. #endif
  126. }
  127. }
  128. #endif // __WINDOWS__ or not __WINDOWS__
  129. return dlp.c_str();
  130. }
  131. const char *NodeControlClient::authTokenDefaultSystemPath()
  132. {
  133. static std::string dsp;
  134. static Mutex dsp_m;
  135. Mutex::Lock _l(dsp_m);
  136. if (!dsp.length())
  137. dsp = (ZT_DEFAULTS.defaultHomePath + ZT_PATH_SEPARATOR_S"authtoken.secret");
  138. return dsp.c_str();
  139. }
  140. } // namespace ZeroTier