OSUtils.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/stat.h>
  39. #include <sys/uio.h>
  40. #include <dirent.h>
  41. #endif
  42. #ifdef __WINDOWS__
  43. #include <wincrypt.h>
  44. #endif
  45. #include "OSUtils.hpp"
  46. namespace ZeroTier {
  47. static inline std::string _mkDefaultHomePath()
  48. {
  49. #ifdef __UNIX_LIKE__
  50. #ifdef __APPLE__
  51. // /Library/... on Apple
  52. return std::string("/Library/Application Support/ZeroTier/One");
  53. #else
  54. #ifdef __FreeBSD__
  55. // FreeBSD likes /var/db instead of /var/lib
  56. return std::string("/var/db/zerotier-one");
  57. #else
  58. // Use /var/lib for Linux and other *nix
  59. return std::string("/var/lib/zerotier-one");
  60. #endif
  61. #endif
  62. #else // not __UNIX_LIKE__
  63. #ifdef __WINDOWS__
  64. // Look up app data folder on Windows, e.g. C:\ProgramData\...
  65. char buf[16384];
  66. if (SUCCEEDED(SHGetFolderPathA(NULL,CSIDL_COMMON_APPDATA,NULL,0,buf)))
  67. return (std::string(buf) + "\\ZeroTier\\One");
  68. else return std::string("C:\\ZeroTier\\One");
  69. #else
  70. #error Unknown platform, please define a default home path!
  71. #endif
  72. #endif // __UNIX_LIKE__ or not...
  73. }
  74. #ifdef __UNIX_LIKE__
  75. bool OSUtils::redirectUnixOutputs(const char *stdoutPath,const char *stderrPath)
  76. throw()
  77. {
  78. int fdout = ::open(stdoutPath,O_WRONLY|O_CREAT,0600);
  79. if (fdout > 0) {
  80. int fderr;
  81. if (stderrPath) {
  82. fderr = ::open(stderrPath,O_WRONLY|O_CREAT,0600);
  83. if (fderr <= 0) {
  84. ::close(fdout);
  85. return false;
  86. }
  87. } else fderr = fdout;
  88. ::close(STDOUT_FILENO);
  89. ::close(STDERR_FILENO);
  90. ::dup2(fdout,STDOUT_FILENO);
  91. ::dup2(fderr,STDERR_FILENO);
  92. return true;
  93. }
  94. return false;
  95. }
  96. #endif // __UNIX_LIKE__
  97. std::map<std::string,bool> OSUtils::listDirectory(const char *path)
  98. {
  99. std::map<std::string,bool> r;
  100. #ifdef __WINDOWS__
  101. HANDLE hFind;
  102. WIN32_FIND_DATAA ffd;
  103. if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) {
  104. do {
  105. if ((strcmp(ffd.cFileName,"."))&&(strcmp(ffd.cFileName,"..")))
  106. r[std::string(ffd.cFileName)] = ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
  107. } while (FindNextFileA(hFind,&ffd));
  108. FindClose(hFind);
  109. }
  110. #else
  111. struct dirent de;
  112. struct dirent *dptr;
  113. DIR *d = opendir(path);
  114. if (!d)
  115. return r;
  116. dptr = (struct dirent *)0;
  117. for(;;) {
  118. if (readdir_r(d,&de,&dptr))
  119. break;
  120. if (dptr) {
  121. if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,"..")))
  122. r[std::string(dptr->d_name)] = (dptr->d_type == DT_DIR);
  123. } else break;
  124. }
  125. #endif
  126. return r;
  127. }
  128. void OSUtils::lockDownFile(const char *path,bool isDir)
  129. {
  130. #ifdef __UNIX_LIKE__
  131. chmod(path,isDir ? 0700 : 0600);
  132. #else
  133. #ifdef __WINDOWS__
  134. {
  135. STARTUPINFOA startupInfo;
  136. PROCESS_INFORMATION processInfo;
  137. startupInfo.cb = sizeof(startupInfo);
  138. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  139. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  140. if (CreateProcessA(NULL,(LPSTR)(std::string("C:\\Windows\\System32\\icacls.exe \"") + path + "\" /inheritance:d /Q").c_str(),NULL,NULL,FALSE,0,NULL,NULL,&startupInfo,&processInfo)) {
  141. WaitForSingleObject(processInfo.hProcess,INFINITE);
  142. CloseHandle(processInfo.hProcess);
  143. CloseHandle(processInfo.hThread);
  144. }
  145. startupInfo.cb = sizeof(startupInfo);
  146. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  147. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  148. if (CreateProcessA(NULL,(LPSTR)(std::string("C:\\Windows\\System32\\icacls.exe \"") + path + "\" /remove *S-1-5-32-545 /Q").c_str(),NULL,NULL,FALSE,0,NULL,NULL,&startupInfo,&processInfo)) {
  149. WaitForSingleObject(processInfo.hProcess,INFINITE);
  150. CloseHandle(processInfo.hProcess);
  151. CloseHandle(processInfo.hThread);
  152. }
  153. }
  154. #endif
  155. #endif
  156. }
  157. uint64_t OSUtils::getLastModified(const char *path)
  158. {
  159. struct stat s;
  160. if (stat(path,&s))
  161. return 0;
  162. return (((uint64_t)s.st_mtime) * 1000ULL);
  163. }
  164. bool OSUtils::fileExists(const char *path,bool followLinks)
  165. {
  166. struct stat s;
  167. #ifdef __UNIX_LIKE__
  168. if (!followLinks)
  169. return (lstat(path,&s) == 0);
  170. #endif
  171. return (stat(path,&s) == 0);
  172. }
  173. int64_t OSUtils::getFileSize(const char *path)
  174. {
  175. struct stat s;
  176. if (stat(path,&s))
  177. return -1;
  178. #ifdef __WINDOWS__
  179. return s.st_size;
  180. #else
  181. if (S_ISREG(s.st_mode))
  182. return s.st_size;
  183. #endif
  184. return -1;
  185. }
  186. bool OSUtils::readFile(const char *path,std::string &buf)
  187. {
  188. char tmp[4096];
  189. FILE *f = fopen(path,"rb");
  190. if (f) {
  191. for(;;) {
  192. long n = (long)fread(tmp,1,sizeof(tmp),f);
  193. if (n > 0)
  194. buf.append(tmp,n);
  195. else break;
  196. }
  197. fclose(f);
  198. return true;
  199. }
  200. return false;
  201. }
  202. bool OSUtils::writeFile(const char *path,const void *buf,unsigned int len)
  203. {
  204. FILE *f = fopen(path,"wb");
  205. if (f) {
  206. if ((long)fwrite(buf,1,len,f) != (long)len) {
  207. fclose(f);
  208. return false;
  209. } else {
  210. fclose(f);
  211. return true;
  212. }
  213. }
  214. return false;
  215. }
  216. } // namespace ZeroTier