OSUtils.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifdef __UNIX_LIKE__
  48. bool OSUtils::redirectUnixOutputs(const char *stdoutPath,const char *stderrPath)
  49. throw()
  50. {
  51. int fdout = ::open(stdoutPath,O_WRONLY|O_CREAT,0600);
  52. if (fdout > 0) {
  53. int fderr;
  54. if (stderrPath) {
  55. fderr = ::open(stderrPath,O_WRONLY|O_CREAT,0600);
  56. if (fderr <= 0) {
  57. ::close(fdout);
  58. return false;
  59. }
  60. } else fderr = fdout;
  61. ::close(STDOUT_FILENO);
  62. ::close(STDERR_FILENO);
  63. ::dup2(fdout,STDOUT_FILENO);
  64. ::dup2(fderr,STDERR_FILENO);
  65. return true;
  66. }
  67. return false;
  68. }
  69. #endif // __UNIX_LIKE__
  70. std::map<std::string,bool> OSUtils::listDirectory(const char *path)
  71. {
  72. std::map<std::string,bool> r;
  73. #ifdef __WINDOWS__
  74. HANDLE hFind;
  75. WIN32_FIND_DATAA ffd;
  76. if ((hFind = FindFirstFileA((std::string(path) + "\\*").c_str(),&ffd)) != INVALID_HANDLE_VALUE) {
  77. do {
  78. if ((strcmp(ffd.cFileName,"."))&&(strcmp(ffd.cFileName,"..")))
  79. r[std::string(ffd.cFileName)] = ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
  80. } while (FindNextFileA(hFind,&ffd));
  81. FindClose(hFind);
  82. }
  83. #else
  84. struct dirent de;
  85. struct dirent *dptr;
  86. DIR *d = opendir(path);
  87. if (!d)
  88. return r;
  89. dptr = (struct dirent *)0;
  90. for(;;) {
  91. if (readdir_r(d,&de,&dptr))
  92. break;
  93. if (dptr) {
  94. if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,"..")))
  95. r[std::string(dptr->d_name)] = (dptr->d_type == DT_DIR);
  96. } else break;
  97. }
  98. #endif
  99. return r;
  100. }
  101. void OSUtils::lockDownFile(const char *path,bool isDir)
  102. {
  103. #ifdef __UNIX_LIKE__
  104. chmod(path,isDir ? 0700 : 0600);
  105. #else
  106. #ifdef __WINDOWS__
  107. {
  108. STARTUPINFOA startupInfo;
  109. PROCESS_INFORMATION processInfo;
  110. startupInfo.cb = sizeof(startupInfo);
  111. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  112. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  113. 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)) {
  114. WaitForSingleObject(processInfo.hProcess,INFINITE);
  115. CloseHandle(processInfo.hProcess);
  116. CloseHandle(processInfo.hThread);
  117. }
  118. startupInfo.cb = sizeof(startupInfo);
  119. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  120. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  121. 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)) {
  122. WaitForSingleObject(processInfo.hProcess,INFINITE);
  123. CloseHandle(processInfo.hProcess);
  124. CloseHandle(processInfo.hThread);
  125. }
  126. }
  127. #endif
  128. #endif
  129. }
  130. uint64_t OSUtils::getLastModified(const char *path)
  131. {
  132. struct stat s;
  133. if (stat(path,&s))
  134. return 0;
  135. return (((uint64_t)s.st_mtime) * 1000ULL);
  136. }
  137. bool OSUtils::fileExists(const char *path,bool followLinks)
  138. {
  139. struct stat s;
  140. #ifdef __UNIX_LIKE__
  141. if (!followLinks)
  142. return (lstat(path,&s) == 0);
  143. #endif
  144. return (stat(path,&s) == 0);
  145. }
  146. int64_t OSUtils::getFileSize(const char *path)
  147. {
  148. struct stat s;
  149. if (stat(path,&s))
  150. return -1;
  151. #ifdef __WINDOWS__
  152. return s.st_size;
  153. #else
  154. if (S_ISREG(s.st_mode))
  155. return s.st_size;
  156. #endif
  157. return -1;
  158. }
  159. bool OSUtils::readFile(const char *path,std::string &buf)
  160. {
  161. char tmp[4096];
  162. FILE *f = fopen(path,"rb");
  163. if (f) {
  164. for(;;) {
  165. long n = (long)fread(tmp,1,sizeof(tmp),f);
  166. if (n > 0)
  167. buf.append(tmp,n);
  168. else break;
  169. }
  170. fclose(f);
  171. return true;
  172. }
  173. return false;
  174. }
  175. bool OSUtils::writeFile(const char *path,const void *buf,unsigned int len)
  176. {
  177. FILE *f = fopen(path,"wb");
  178. if (f) {
  179. if ((long)fwrite(buf,1,len,f) != (long)len) {
  180. fclose(f);
  181. return false;
  182. } else {
  183. fclose(f);
  184. return true;
  185. }
  186. }
  187. return false;
  188. }
  189. } // namespace ZeroTier