winUser.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. typedef long SHANDLE_PTR;
  27. #include <shlobj.h>
  28. #include <windows.h>
  29. #include <lmcons.h>
  30. #define CSIDL_PROFILE 0x0028
  31. const char *Platform::getUserDataDirectory()
  32. {
  33. TCHAR szBuffer[ MAX_PATH + 1 ];
  34. if(! SHGetSpecialFolderPath( NULL, szBuffer, CSIDL_APPDATA, true ) )
  35. return "";
  36. TCHAR *ptr = szBuffer;
  37. while(*ptr)
  38. {
  39. if(*ptr == '\\')
  40. *ptr = '/';
  41. ++ptr;
  42. }
  43. #ifdef UNICODE
  44. char path[ MAX_PATH * 3 + 1 ];
  45. convertUTF16toUTF8( szBuffer, path, sizeof( path ) );
  46. #else
  47. char* path = szBuffer;
  48. #endif
  49. return StringTable->insert( path );
  50. }
  51. const char *Platform::getUserHomeDirectory()
  52. {
  53. TCHAR szBuffer[ MAX_PATH + 1 ];
  54. if(! SHGetSpecialFolderPath( NULL, szBuffer, CSIDL_PERSONAL, false ) )
  55. if(! SHGetSpecialFolderPath( NULL, szBuffer, CSIDL_COMMON_DOCUMENTS, false ) )
  56. return "";
  57. TCHAR *ptr = szBuffer;
  58. while(*ptr)
  59. {
  60. if(*ptr == '\\')
  61. *ptr = '/';
  62. ++ptr;
  63. }
  64. #ifdef UNICODE
  65. char path[ MAX_PATH * 3 + 1 ];
  66. convertUTF16toUTF8( szBuffer, path, sizeof( path ) );
  67. #else
  68. char* path = szBuffer;
  69. #endif
  70. return StringTable->insert( path );
  71. }
  72. bool Platform::getUserIsAdministrator()
  73. {
  74. BOOL fReturn = FALSE;
  75. DWORD dwStatus;
  76. DWORD dwAccessMask;
  77. DWORD dwAccessDesired;
  78. DWORD dwACLSize;
  79. DWORD dwStructureSize = sizeof(PRIVILEGE_SET);
  80. PACL pACL = NULL;
  81. PSID psidAdmin = NULL;
  82. HANDLE hToken = NULL;
  83. HANDLE hImpersonationToken = NULL;
  84. PRIVILEGE_SET ps;
  85. GENERIC_MAPPING GenericMapping;
  86. PSECURITY_DESCRIPTOR psdAdmin = NULL;
  87. SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
  88. /*
  89. Determine if the current thread is running as a user that is a member of
  90. the local admins group. To do this, create a security descriptor that
  91. has a DACL which has an ACE that allows only local aministrators access.
  92. Then, call AccessCheck with the current thread's token and the security
  93. descriptor. It will say whether the user could access an object if it
  94. had that security descriptor. Note: you do not need to actually create
  95. the object. Just checking access against the security descriptor alone
  96. will be sufficient.
  97. */
  98. const DWORD ACCESS_READ = 1;
  99. const DWORD ACCESS_WRITE = 2;
  100. __try
  101. {
  102. /*
  103. AccessCheck() requires an impersonation token. We first get a primary
  104. token and then create a duplicate impersonation token. The
  105. impersonation token is not actually assigned to the thread, but is
  106. used in the call to AccessCheck. Thus, this function itself never
  107. impersonates, but does use the identity of the thread. If the thread
  108. was impersonating already, this function uses that impersonation context.
  109. */
  110. if (!OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE|TOKEN_QUERY, TRUE, &hToken))
  111. {
  112. if (GetLastError() != ERROR_NO_TOKEN)
  113. __leave;
  114. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &hToken))
  115. __leave;
  116. }
  117. if (!DuplicateToken (hToken, SecurityImpersonation, &hImpersonationToken))
  118. __leave;
  119. /*
  120. Create the binary representation of the well-known SID that
  121. represents the local administrators group. Then create the security
  122. descriptor and DACL with an ACE that allows only local admins access.
  123. After that, perform the access check. This will determine whether
  124. the current user is a local admin.
  125. */
  126. if (!AllocateAndInitializeSid(&SystemSidAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
  127. DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmin))
  128. __leave;
  129. psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  130. if (psdAdmin == NULL)
  131. __leave;
  132. if (!InitializeSecurityDescriptor(psdAdmin, SECURITY_DESCRIPTOR_REVISION))
  133. __leave;
  134. // Compute size needed for the ACL.
  135. dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psidAdmin) - sizeof(DWORD);
  136. pACL = (PACL)LocalAlloc(LPTR, dwACLSize);
  137. if (pACL == NULL)
  138. __leave;
  139. if (!InitializeAcl(pACL, dwACLSize, ACL_REVISION2))
  140. __leave;
  141. dwAccessMask= ACCESS_READ | ACCESS_WRITE;
  142. if (!AddAccessAllowedAce(pACL, ACL_REVISION2, dwAccessMask, psidAdmin))
  143. __leave;
  144. if (!SetSecurityDescriptorDacl(psdAdmin, TRUE, pACL, FALSE))
  145. __leave;
  146. /*
  147. AccessCheck validates a security descriptor somewhat; set the group
  148. and owner so that enough of the security descriptor is filled out to
  149. make AccessCheck happy.
  150. */
  151. SetSecurityDescriptorGroup(psdAdmin, psidAdmin, FALSE);
  152. SetSecurityDescriptorOwner(psdAdmin, psidAdmin, FALSE);
  153. if (!IsValidSecurityDescriptor(psdAdmin))
  154. __leave;
  155. dwAccessDesired = ACCESS_READ;
  156. /*
  157. Initialize GenericMapping structure even though you
  158. do not use generic rights.
  159. */
  160. GenericMapping.GenericRead = ACCESS_READ;
  161. GenericMapping.GenericWrite = ACCESS_WRITE;
  162. GenericMapping.GenericExecute = 0;
  163. GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE;
  164. if (!AccessCheck(psdAdmin, hImpersonationToken, dwAccessDesired,
  165. &GenericMapping, &ps, &dwStructureSize, &dwStatus,
  166. &fReturn))
  167. {
  168. fReturn = FALSE;
  169. __leave;
  170. }
  171. }
  172. __finally
  173. {
  174. // Clean up.
  175. if (pACL) LocalFree(pACL);
  176. if (psdAdmin) LocalFree(psdAdmin);
  177. if (psidAdmin) FreeSid(psidAdmin);
  178. if (hImpersonationToken) CloseHandle (hImpersonationToken);
  179. if (hToken) CloseHandle (hToken);
  180. }
  181. return fReturn;
  182. }