OSUtils.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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 <string.h>
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include <sys/stat.h>
  32. #include "../node/Constants.hpp"
  33. #ifdef __UNIX_LIKE__
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include <fcntl.h>
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <sys/stat.h>
  40. #include <sys/uio.h>
  41. #include <dirent.h>
  42. #include <netdb.h>
  43. #endif
  44. #ifdef __WINDOWS__
  45. #include <wincrypt.h>
  46. #endif
  47. #include "OSUtils.hpp"
  48. namespace ZeroTier {
  49. #ifdef __UNIX_LIKE__
  50. bool OSUtils::redirectUnixOutputs(const char *stdoutPath,const char *stderrPath)
  51. throw()
  52. {
  53. int fdout = ::open(stdoutPath,O_WRONLY|O_CREAT,0600);
  54. if (fdout > 0) {
  55. int fderr;
  56. if (stderrPath) {
  57. fderr = ::open(stderrPath,O_WRONLY|O_CREAT,0600);
  58. if (fderr <= 0) {
  59. ::close(fdout);
  60. return false;
  61. }
  62. } else fderr = fdout;
  63. ::close(STDOUT_FILENO);
  64. ::close(STDERR_FILENO);
  65. ::dup2(fdout,STDOUT_FILENO);
  66. ::dup2(fderr,STDERR_FILENO);
  67. return true;
  68. }
  69. return false;
  70. }
  71. #endif // __UNIX_LIKE__
  72. std::vector<std::string> OSUtils::listDirectory(const char *path)
  73. {
  74. std::vector<std::string> r;
  75. #ifdef __WINDOWS__
  76. HANDLE hFind;
  77. WIN32_FIND_DATAA ffd;
  78. if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) {
  79. do {
  80. if ((strcmp(ffd.cFileName,"."))&&(strcmp(ffd.cFileName,".."))&&((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0))
  81. r.push_back(std::string(ffd.cFileName));
  82. } while (FindNextFileA(hFind,&ffd));
  83. FindClose(hFind);
  84. }
  85. #else
  86. struct dirent de;
  87. struct dirent *dptr;
  88. DIR *d = opendir(path);
  89. if (!d)
  90. return r;
  91. dptr = (struct dirent *)0;
  92. for(;;) {
  93. if (readdir_r(d,&de,&dptr))
  94. break;
  95. if (dptr) {
  96. if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,".."))&&(dptr->d_type != DT_DIR))
  97. r.push_back(std::string(dptr->d_name));
  98. } else break;
  99. }
  100. closedir(d);
  101. #endif
  102. return r;
  103. }
  104. void OSUtils::lockDownFile(const char *path,bool isDir)
  105. {
  106. #ifdef __UNIX_LIKE__
  107. chmod(path,isDir ? 0700 : 0600);
  108. #else
  109. #ifdef __WINDOWS__
  110. {
  111. STARTUPINFOA startupInfo;
  112. PROCESS_INFORMATION processInfo;
  113. startupInfo.cb = sizeof(startupInfo);
  114. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  115. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  116. 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)) {
  117. WaitForSingleObject(processInfo.hProcess,INFINITE);
  118. CloseHandle(processInfo.hProcess);
  119. CloseHandle(processInfo.hThread);
  120. }
  121. startupInfo.cb = sizeof(startupInfo);
  122. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  123. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  124. 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)) {
  125. WaitForSingleObject(processInfo.hProcess,INFINITE);
  126. CloseHandle(processInfo.hProcess);
  127. CloseHandle(processInfo.hThread);
  128. }
  129. }
  130. #endif
  131. #endif
  132. }
  133. uint64_t OSUtils::getLastModified(const char *path)
  134. {
  135. struct stat s;
  136. if (stat(path,&s))
  137. return 0;
  138. return (((uint64_t)s.st_mtime) * 1000ULL);
  139. }
  140. bool OSUtils::fileExists(const char *path,bool followLinks)
  141. {
  142. struct stat s;
  143. #ifdef __UNIX_LIKE__
  144. if (!followLinks)
  145. return (lstat(path,&s) == 0);
  146. #endif
  147. return (stat(path,&s) == 0);
  148. }
  149. int64_t OSUtils::getFileSize(const char *path)
  150. {
  151. struct stat s;
  152. if (stat(path,&s))
  153. return -1;
  154. #ifdef __WINDOWS__
  155. return s.st_size;
  156. #else
  157. if (S_ISREG(s.st_mode))
  158. return s.st_size;
  159. #endif
  160. return -1;
  161. }
  162. std::vector<InetAddress> OSUtils::resolve(const char *name)
  163. {
  164. std::vector<InetAddress> r;
  165. std::vector<InetAddress>::iterator i;
  166. InetAddress tmp;
  167. struct addrinfo *ai = (struct addrinfo *)0,*p;
  168. if (!getaddrinfo(name,(const char *)0,(const struct addrinfo *)0,&ai)) {
  169. try {
  170. p = ai;
  171. while (p) {
  172. if ((p->ai_addr)&&((p->ai_addr->sa_family == AF_INET)||(p->ai_addr->sa_family == AF_INET6))) {
  173. tmp = *(p->ai_addr);
  174. for(i=r.begin();i!=r.end();++i) {
  175. if (i->ipsEqual(tmp))
  176. goto skip_add_inetaddr;
  177. }
  178. r.push_back(tmp);
  179. }
  180. skip_add_inetaddr:
  181. p = p->ai_next;
  182. }
  183. } catch ( ... ) {}
  184. freeaddrinfo(ai);
  185. }
  186. std::sort(r.begin(),r.end());
  187. return r;
  188. }
  189. bool OSUtils::readFile(const char *path,std::string &buf)
  190. {
  191. char tmp[4096];
  192. FILE *f = fopen(path,"rb");
  193. if (f) {
  194. for(;;) {
  195. long n = (long)fread(tmp,1,sizeof(tmp),f);
  196. if (n > 0)
  197. buf.append(tmp,n);
  198. else break;
  199. }
  200. fclose(f);
  201. return true;
  202. }
  203. return false;
  204. }
  205. bool OSUtils::writeFile(const char *path,const void *buf,unsigned int len)
  206. {
  207. FILE *f = fopen(path,"wb");
  208. if (f) {
  209. if ((long)fwrite(buf,1,len,f) != (long)len) {
  210. fclose(f);
  211. return false;
  212. } else {
  213. fclose(f);
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219. // Used to convert HTTP header names to ASCII lower case
  220. 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 };
  221. } // namespace ZeroTier