winUser.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/console.h"
  24. #include "core/stringTable.h"
  25. #include "core/strings/unicode.h"
  26. #ifndef TORQUE_OS_WIN64
  27. typedef long SHANDLE_PTR;
  28. #endif
  29. #include <shlobj.h>
  30. #include <windows.h>
  31. #include <lmcons.h>
  32. #define CSIDL_PROFILE 0x0028
  33. const char *Platform::getUserDataDirectory()
  34. {
  35. TCHAR szBuffer[ MAX_PATH + 1 ];
  36. if(! SHGetSpecialFolderPath( NULL, szBuffer, CSIDL_APPDATA, true ) )
  37. return "";
  38. TCHAR *ptr = szBuffer;
  39. while(*ptr)
  40. {
  41. if(*ptr == '\\')
  42. *ptr = '/';
  43. ++ptr;
  44. }
  45. #ifdef UNICODE
  46. char path[ MAX_PATH * 3 + 1 ];
  47. convertUTF16toUTF8( szBuffer, path );
  48. #else
  49. char* path = szBuffer;
  50. #endif
  51. return StringTable->insert( path );
  52. }
  53. const char *Platform::getUserHomeDirectory()
  54. {
  55. TCHAR szBuffer[ MAX_PATH + 1 ];
  56. if(! SHGetSpecialFolderPath( NULL, szBuffer, CSIDL_PERSONAL, false ) )
  57. if(! SHGetSpecialFolderPath( NULL, szBuffer, CSIDL_COMMON_DOCUMENTS, false ) )
  58. return "";
  59. TCHAR *ptr = szBuffer;
  60. while(*ptr)
  61. {
  62. if(*ptr == '\\')
  63. *ptr = '/';
  64. ++ptr;
  65. }
  66. #ifdef UNICODE
  67. char path[ MAX_PATH * 3 + 1 ];
  68. convertUTF16toUTF8( szBuffer, path );
  69. #else
  70. char* path = szBuffer;
  71. #endif
  72. return StringTable->insert( path );
  73. }
  74. bool Platform::getUserIsAdministrator()
  75. {
  76. BOOL fReturn = FALSE;
  77. DWORD dwStatus;
  78. DWORD dwAccessMask;
  79. DWORD dwAccessDesired;
  80. DWORD dwACLSize;
  81. DWORD dwStructureSize = sizeof(PRIVILEGE_SET);
  82. PACL pACL = NULL;
  83. PSID psidAdmin = NULL;
  84. HANDLE hToken = NULL;
  85. HANDLE hImpersonationToken = NULL;
  86. PRIVILEGE_SET ps;
  87. GENERIC_MAPPING GenericMapping;
  88. PSECURITY_DESCRIPTOR psdAdmin = NULL;
  89. SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
  90. /*
  91. Determine if the current thread is running as a user that is a member of
  92. the local admins group. To do this, create a security descriptor that
  93. has a DACL which has an ACE that allows only local aministrators access.
  94. Then, call AccessCheck with the current thread's token and the security
  95. descriptor. It will say whether the user could access an object if it
  96. had that security descriptor. Note: you do not need to actually create
  97. the object. Just checking access against the security descriptor alone
  98. will be sufficient.
  99. */
  100. const DWORD ACCESS_READ = 1;
  101. const DWORD ACCESS_WRITE = 2;
  102. __try
  103. {
  104. /*
  105. AccessCheck() requires an impersonation token. We first get a primary
  106. token and then create a duplicate impersonation token. The
  107. impersonation token is not actually assigned to the thread, but is
  108. used in the call to AccessCheck. Thus, this function itself never
  109. impersonates, but does use the identity of the thread. If the thread
  110. was impersonating already, this function uses that impersonation context.
  111. */
  112. if (!OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE|TOKEN_QUERY, TRUE, &hToken))
  113. {
  114. if (GetLastError() != ERROR_NO_TOKEN)
  115. __leave;
  116. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &hToken))
  117. __leave;
  118. }
  119. if (!DuplicateToken (hToken, SecurityImpersonation, &hImpersonationToken))
  120. __leave;
  121. /*
  122. Create the binary representation of the well-known SID that
  123. represents the local administrators group. Then create the security
  124. descriptor and DACL with an ACE that allows only local admins access.
  125. After that, perform the access check. This will determine whether
  126. the current user is a local admin.
  127. */
  128. if (!AllocateAndInitializeSid(&SystemSidAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
  129. DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmin))
  130. __leave;
  131. psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  132. if (psdAdmin == NULL)
  133. __leave;
  134. if (!InitializeSecurityDescriptor(psdAdmin, SECURITY_DESCRIPTOR_REVISION))
  135. __leave;
  136. // Compute size needed for the ACL.
  137. dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psidAdmin) - sizeof(DWORD);
  138. pACL = (PACL)LocalAlloc(LPTR, dwACLSize);
  139. if (pACL == NULL)
  140. __leave;
  141. if (!InitializeAcl(pACL, dwACLSize, ACL_REVISION2))
  142. __leave;
  143. dwAccessMask= ACCESS_READ | ACCESS_WRITE;
  144. if (!AddAccessAllowedAce(pACL, ACL_REVISION2, dwAccessMask, psidAdmin))
  145. __leave;
  146. if (!SetSecurityDescriptorDacl(psdAdmin, TRUE, pACL, FALSE))
  147. __leave;
  148. /*
  149. AccessCheck validates a security descriptor somewhat; set the group
  150. and owner so that enough of the security descriptor is filled out to
  151. make AccessCheck happy.
  152. */
  153. SetSecurityDescriptorGroup(psdAdmin, psidAdmin, FALSE);
  154. SetSecurityDescriptorOwner(psdAdmin, psidAdmin, FALSE);
  155. if (!IsValidSecurityDescriptor(psdAdmin))
  156. __leave;
  157. dwAccessDesired = ACCESS_READ;
  158. /*
  159. Initialize GenericMapping structure even though you
  160. do not use generic rights.
  161. */
  162. GenericMapping.GenericRead = ACCESS_READ;
  163. GenericMapping.GenericWrite = ACCESS_WRITE;
  164. GenericMapping.GenericExecute = 0;
  165. GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE;
  166. if (!AccessCheck(psdAdmin, hImpersonationToken, dwAccessDesired,
  167. &GenericMapping, &ps, &dwStructureSize, &dwStatus,
  168. &fReturn))
  169. {
  170. fReturn = FALSE;
  171. __leave;
  172. }
  173. }
  174. __finally
  175. {
  176. // Clean up.
  177. if (pACL) LocalFree(pACL);
  178. if (psdAdmin) LocalFree(psdAdmin);
  179. if (psidAdmin) FreeSid(psidAdmin);
  180. if (hImpersonationToken) CloseHandle (hImpersonationToken);
  181. if (hToken) CloseHandle (hToken);
  182. }
  183. return fReturn;
  184. }