OSUtils.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <sys/stat.h>
  23. #include "../node/Constants.hpp"
  24. #ifdef __UNIX_LIKE__
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <sys/stat.h>
  31. #include <sys/uio.h>
  32. #include <dirent.h>
  33. #include <netdb.h>
  34. #endif
  35. #ifdef __WINDOWS__
  36. #include <windows.h>
  37. #include <wincrypt.h>
  38. #include <ShlObj.h>
  39. #include <netioapi.h>
  40. #include <iphlpapi.h>
  41. #endif
  42. #include "OSUtils.hpp"
  43. namespace ZeroTier {
  44. #ifdef __UNIX_LIKE__
  45. bool OSUtils::redirectUnixOutputs(const char *stdoutPath,const char *stderrPath)
  46. throw()
  47. {
  48. int fdout = ::open(stdoutPath,O_WRONLY|O_CREAT,0600);
  49. if (fdout > 0) {
  50. int fderr;
  51. if (stderrPath) {
  52. fderr = ::open(stderrPath,O_WRONLY|O_CREAT,0600);
  53. if (fderr <= 0) {
  54. ::close(fdout);
  55. return false;
  56. }
  57. } else fderr = fdout;
  58. ::close(STDOUT_FILENO);
  59. ::close(STDERR_FILENO);
  60. ::dup2(fdout,STDOUT_FILENO);
  61. ::dup2(fderr,STDERR_FILENO);
  62. return true;
  63. }
  64. return false;
  65. }
  66. #endif // __UNIX_LIKE__
  67. std::vector<std::string> OSUtils::listDirectory(const char *path)
  68. {
  69. std::vector<std::string> r;
  70. #ifdef __WINDOWS__
  71. HANDLE hFind;
  72. WIN32_FIND_DATAA ffd;
  73. if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) {
  74. do {
  75. if ((strcmp(ffd.cFileName,"."))&&(strcmp(ffd.cFileName,".."))&&((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0))
  76. r.push_back(std::string(ffd.cFileName));
  77. } while (FindNextFileA(hFind,&ffd));
  78. FindClose(hFind);
  79. }
  80. #else
  81. struct dirent de;
  82. struct dirent *dptr;
  83. DIR *d = opendir(path);
  84. if (!d)
  85. return r;
  86. dptr = (struct dirent *)0;
  87. for(;;) {
  88. if (readdir_r(d,&de,&dptr))
  89. break;
  90. if (dptr) {
  91. if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,".."))&&(dptr->d_type != DT_DIR))
  92. r.push_back(std::string(dptr->d_name));
  93. } else break;
  94. }
  95. closedir(d);
  96. #endif
  97. return r;
  98. }
  99. std::vector<std::string> OSUtils::listSubdirectories(const char *path)
  100. {
  101. std::vector<std::string> r;
  102. #ifdef __WINDOWS__
  103. HANDLE hFind;
  104. WIN32_FIND_DATAA ffd;
  105. if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) {
  106. do {
  107. if ((strcmp(ffd.cFileName,"."))&&(strcmp(ffd.cFileName,".."))&&((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0))
  108. r.push_back(std::string(ffd.cFileName));
  109. } while (FindNextFileA(hFind,&ffd));
  110. FindClose(hFind);
  111. }
  112. #else
  113. struct dirent de;
  114. struct dirent *dptr;
  115. DIR *d = opendir(path);
  116. if (!d)
  117. return r;
  118. dptr = (struct dirent *)0;
  119. for(;;) {
  120. if (readdir_r(d,&de,&dptr))
  121. break;
  122. if (dptr) {
  123. if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,".."))&&(dptr->d_type == DT_DIR))
  124. r.push_back(std::string(dptr->d_name));
  125. } else break;
  126. }
  127. closedir(d);
  128. #endif
  129. return r;
  130. }
  131. bool OSUtils::rmDashRf(const char *path)
  132. {
  133. #ifdef __WINDOWS__
  134. HANDLE hFind;
  135. WIN32_FIND_DATAA ffd;
  136. if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) {
  137. do {
  138. if ((strcmp(ffd.cFileName,".") != 0)&&(strcmp(ffd.cFileName,"..") != 0)) {
  139. if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
  140. if (DeleteFileA((std::string(path) + ZT_PATH_SEPARATOR_S + ffd.cFileName).c_str()) == FALSE)
  141. return false;
  142. } else {
  143. if (!rmDashRf((std::string(path) + ZT_PATH_SEPARATOR_S + ffd.cFileName).c_str()))
  144. return false;
  145. }
  146. }
  147. } while (FindNextFileA(hFind,&ffd));
  148. FindClose(hFind);
  149. }
  150. return (RemoveDirectoryA(path) != FALSE);
  151. #else
  152. struct dirent de;
  153. struct dirent *dptr;
  154. DIR *d = opendir(path);
  155. if (!d)
  156. return true;
  157. dptr = (struct dirent *)0;
  158. for(;;) {
  159. if (readdir_r(d,&de,&dptr) != 0)
  160. break;
  161. if (!dptr)
  162. break;
  163. if ((strcmp(dptr->d_name,".") != 0)&&(strcmp(dptr->d_name,"..") != 0)&&(strlen(dptr->d_name) > 0)) {
  164. std::string p(path);
  165. p.push_back(ZT_PATH_SEPARATOR);
  166. p.append(dptr->d_name);
  167. if (unlink(p.c_str()) != 0) {
  168. if (!rmDashRf(p.c_str()))
  169. return false;
  170. }
  171. }
  172. }
  173. closedir(d);
  174. return (rmdir(path) == 0);
  175. #endif
  176. }
  177. void OSUtils::lockDownFile(const char *path,bool isDir)
  178. {
  179. #ifdef __UNIX_LIKE__
  180. chmod(path,isDir ? 0700 : 0600);
  181. #else
  182. #ifdef __WINDOWS__
  183. {
  184. STARTUPINFOA startupInfo;
  185. PROCESS_INFORMATION processInfo;
  186. startupInfo.cb = sizeof(startupInfo);
  187. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  188. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  189. if (CreateProcessA(NULL,(LPSTR)(std::string("C:\\Windows\\System32\\icacls.exe \"") + path + "\" /inheritance:d /Q").c_str(),NULL,NULL,FALSE,CREATE_NO_WINDOW,NULL,NULL,&startupInfo,&processInfo)) {
  190. WaitForSingleObject(processInfo.hProcess,INFINITE);
  191. CloseHandle(processInfo.hProcess);
  192. CloseHandle(processInfo.hThread);
  193. }
  194. startupInfo.cb = sizeof(startupInfo);
  195. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  196. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  197. if (CreateProcessA(NULL,(LPSTR)(std::string("C:\\Windows\\System32\\icacls.exe \"") + path + "\" /remove *S-1-5-32-545 /Q").c_str(),NULL,NULL,FALSE,CREATE_NO_WINDOW,NULL,NULL,&startupInfo,&processInfo)) {
  198. WaitForSingleObject(processInfo.hProcess,INFINITE);
  199. CloseHandle(processInfo.hProcess);
  200. CloseHandle(processInfo.hThread);
  201. }
  202. }
  203. #endif
  204. #endif
  205. }
  206. uint64_t OSUtils::getLastModified(const char *path)
  207. {
  208. struct stat s;
  209. if (stat(path,&s))
  210. return 0;
  211. return (((uint64_t)s.st_mtime) * 1000ULL);
  212. }
  213. bool OSUtils::fileExists(const char *path,bool followLinks)
  214. {
  215. struct stat s;
  216. #ifdef __UNIX_LIKE__
  217. if (!followLinks)
  218. return (lstat(path,&s) == 0);
  219. #endif
  220. return (stat(path,&s) == 0);
  221. }
  222. int64_t OSUtils::getFileSize(const char *path)
  223. {
  224. struct stat s;
  225. if (stat(path,&s))
  226. return -1;
  227. #ifdef __WINDOWS__
  228. return s.st_size;
  229. #else
  230. if (S_ISREG(s.st_mode))
  231. return s.st_size;
  232. #endif
  233. return -1;
  234. }
  235. std::vector<InetAddress> OSUtils::resolve(const char *name)
  236. {
  237. std::vector<InetAddress> r;
  238. std::vector<InetAddress>::iterator i;
  239. InetAddress tmp;
  240. struct addrinfo *ai = (struct addrinfo *)0,*p;
  241. if (!getaddrinfo(name,(const char *)0,(const struct addrinfo *)0,&ai)) {
  242. try {
  243. p = ai;
  244. while (p) {
  245. if ((p->ai_addr)&&((p->ai_addr->sa_family == AF_INET)||(p->ai_addr->sa_family == AF_INET6))) {
  246. tmp = *(p->ai_addr);
  247. for(i=r.begin();i!=r.end();++i) {
  248. if (i->ipsEqual(tmp))
  249. goto skip_add_inetaddr;
  250. }
  251. r.push_back(tmp);
  252. }
  253. skip_add_inetaddr:
  254. p = p->ai_next;
  255. }
  256. } catch ( ... ) {}
  257. freeaddrinfo(ai);
  258. }
  259. std::sort(r.begin(),r.end());
  260. return r;
  261. }
  262. bool OSUtils::readFile(const char *path,std::string &buf)
  263. {
  264. char tmp[1024];
  265. FILE *f = fopen(path,"rb");
  266. if (f) {
  267. for(;;) {
  268. long n = (long)fread(tmp,1,sizeof(tmp),f);
  269. if (n > 0)
  270. buf.append(tmp,n);
  271. else break;
  272. }
  273. fclose(f);
  274. return true;
  275. }
  276. return false;
  277. }
  278. bool OSUtils::writeFile(const char *path,const void *buf,unsigned int len)
  279. {
  280. FILE *f = fopen(path,"wb");
  281. if (f) {
  282. if ((long)fwrite(buf,1,len,f) != (long)len) {
  283. fclose(f);
  284. return false;
  285. } else {
  286. fclose(f);
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. std::string OSUtils::platformDefaultHomePath()
  293. {
  294. #ifdef __UNIX_LIKE__
  295. #ifdef __APPLE__
  296. // /Library/... on Apple
  297. return std::string("/Library/Application Support/ZeroTier/One");
  298. #else
  299. #ifdef __BSD__
  300. // BSD likes /var/db instead of /var/lib
  301. return std::string("/var/db/zerotier-one");
  302. #else
  303. // Use /var/lib for Linux and other *nix
  304. return std::string("/var/lib/zerotier-one");
  305. #endif
  306. #endif
  307. #else // not __UNIX_LIKE__
  308. #ifdef __WINDOWS__
  309. // Look up app data folder on Windows, e.g. C:\ProgramData\...
  310. char buf[16384];
  311. if (SUCCEEDED(SHGetFolderPathA(NULL,CSIDL_COMMON_APPDATA,NULL,0,buf)))
  312. return (std::string(buf) + "\\ZeroTier\\One");
  313. else return std::string("C:\\ZeroTier\\One");
  314. #else
  315. return (std::string(ZT_PATH_SEPARATOR_S) + "ZeroTier" + ZT_PATH_SEPARATOR_S + "One"); // UNKNOWN PLATFORM
  316. #endif
  317. #endif // __UNIX_LIKE__ or not...
  318. }
  319. // Used to convert HTTP header names to ASCII lower case
  320. const unsigned char OSUtils::TOLOWER_TABLE[256] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
  321. } // namespace ZeroTier