NodeControlClient.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2015 ZeroTier Networks
  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. #include "../node/Defaults.hpp"
  31. #include "IpcConnection.hpp"
  32. #include "IpcListener.hpp"
  33. #include "NodeControlService.hpp"
  34. #ifdef __WINDOWS__
  35. #include <WinSock2.h>
  36. #include <Windows.h>
  37. #include <tchar.h>
  38. #include <wchar.h>
  39. #include <ShlObj.h>
  40. #endif // __WINDOWS__
  41. namespace ZeroTier {
  42. struct _NodeControlClientImpl
  43. {
  44. void (*resultHandler)(void *,const char *);
  45. void *arg;
  46. bool ignoreNextBreak;
  47. IpcConnection *ipcc;
  48. std::string err;
  49. };
  50. static void _CBipcResultHandler(void *arg,IpcConnection *ipcc,IpcConnection::EventType event,const char *result)
  51. {
  52. if ((event == IpcConnection::IPC_EVENT_COMMAND)&&(result)) {
  53. if (!strcmp(result,"200 auth OK")) {
  54. ((_NodeControlClientImpl *)arg)->ignoreNextBreak = true;
  55. } else if ((((_NodeControlClientImpl *)arg)->ignoreNextBreak)&&(!strcmp(result,"."))) {
  56. ((_NodeControlClientImpl *)arg)->ignoreNextBreak = false;
  57. } else ((_NodeControlClientImpl *)arg)->resultHandler(((_NodeControlClientImpl *)arg)->arg,result);
  58. }
  59. }
  60. NodeControlClient::NodeControlClient(const char *ep,const char *authToken,void (*resultHandler)(void *,const char *),void *arg)
  61. throw() :
  62. _impl((void *)new _NodeControlClientImpl)
  63. {
  64. _NodeControlClientImpl *impl = (_NodeControlClientImpl *)_impl;
  65. impl->resultHandler = resultHandler;
  66. impl->arg = arg;
  67. impl->ignoreNextBreak = false;
  68. try {
  69. impl->ipcc = new IpcConnection(ep,ZT_IPC_TIMEOUT,&_CBipcResultHandler,_impl);
  70. impl->ipcc->printf("auth %s"ZT_EOL_S,authToken);
  71. } catch ( ... ) {
  72. impl->ipcc = (IpcConnection *)0;
  73. impl->err = "failure connecting to running ZeroTier One service";
  74. }
  75. }
  76. NodeControlClient::~NodeControlClient()
  77. {
  78. if (_impl) {
  79. delete ((_NodeControlClientImpl *)_impl)->ipcc;
  80. delete (_NodeControlClientImpl *)_impl;
  81. }
  82. }
  83. const char *NodeControlClient::error() const
  84. throw()
  85. {
  86. if (((_NodeControlClientImpl *)_impl)->err.length())
  87. return ((_NodeControlClientImpl *)_impl)->err.c_str();
  88. return (const char *)0;
  89. }
  90. void NodeControlClient::send(const char *command)
  91. throw()
  92. {
  93. try {
  94. if (((_NodeControlClientImpl *)_impl)->ipcc)
  95. ((_NodeControlClientImpl *)_impl)->ipcc->printf("%s"ZT_EOL_S,command);
  96. } catch ( ... ) {}
  97. }
  98. std::vector<std::string> NodeControlClient::splitLine(const char *line)
  99. {
  100. return Utils::split(line," ","\\","\"");
  101. }
  102. const char *NodeControlClient::authTokenDefaultUserPath()
  103. {
  104. static std::string dlp;
  105. static Mutex dlp_m;
  106. Mutex::Lock _l(dlp_m);
  107. #ifdef __WINDOWS__
  108. if (!dlp.length()) {
  109. char buf[16384];
  110. if (SUCCEEDED(SHGetFolderPathA(NULL,CSIDL_APPDATA,NULL,0,buf)))
  111. dlp = (std::string(buf) + "\\ZeroTier\\One\\authtoken.secret");
  112. }
  113. #else // not __WINDOWS__
  114. if (!dlp.length()) {
  115. const char *home = getenv("HOME");
  116. if (home) {
  117. #ifdef __APPLE__
  118. dlp = (std::string(home) + "/Library/Application Support/ZeroTier/One/authtoken.secret");
  119. #else
  120. dlp = (std::string(home) + "/.zeroTierOneAuthToken");
  121. #endif
  122. }
  123. }
  124. #endif // __WINDOWS__ or not __WINDOWS__
  125. return dlp.c_str();
  126. }
  127. std::string NodeControlClient::getAuthToken(const char *path,bool generateIfNotFound)
  128. {
  129. unsigned char randbuf[24];
  130. std::string token;
  131. if (Utils::readFile(path,token))
  132. return Utils::trim(token);
  133. else token = "";
  134. if (generateIfNotFound) {
  135. Utils::getSecureRandom(randbuf,sizeof(randbuf));
  136. for(unsigned int i=0;i<sizeof(randbuf);++i)
  137. token.push_back(("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")[(unsigned int)randbuf[i] % 62]);
  138. if (!Utils::writeFile(path,token))
  139. return std::string();
  140. Utils::lockDownFile(path,false);
  141. }
  142. return token;
  143. }
  144. } // namespace ZeroTier