Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
8.1 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // global utility functions
  4. //
  5. // 8-14-97 sburns
  6. // KMH: originally named burnslib\utility.* but that filename was
  7. // getting a little overused.
  8. // threadsafe
  9. #include "precomp.h"
  10. #include "netUtility.h"
  11. #define ACCESS_READ 1
  12. #define ACCESS_WRITE 2
  13. int Round(double n)
  14. {
  15. int n1 = (int) n;
  16. if (n - n1 >= 0.5)
  17. {
  18. return n1 + 1;
  19. }
  20. return n1;
  21. }
  22. // threadsafe
  23. void gripe(HWND parentDialog, int editResID, int errStringResID)
  24. {
  25. //gripe(parentDialog, editResID, String::load(errStringResID));
  26. }
  27. void gripe(HWND parentDialog,
  28. int editResID,
  29. const CHString& message,
  30. int titleResID)
  31. {
  32. //gripe(parentDialog, editResID, message, String::load(titleResID));
  33. }
  34. void gripe(HWND parentDialog,
  35. int editResID,
  36. const CHString& message,
  37. const CHString& title)
  38. {
  39. // ATLASSERT(::IsWindow(parentDialog));
  40. // ATLASSERT(!message.empty());
  41. // ATLASSERT(editResID > 0);
  42. ::MessageBox(parentDialog, message,
  43. title, MB_OK | MB_ICONERROR | MB_APPLMODAL);
  44. HWND edit = ::GetDlgItem(parentDialog, editResID);
  45. ::SendMessage(edit, EM_SETSEL, 0, -1);
  46. ::SetFocus(edit);
  47. }
  48. void gripe(HWND parentDialog,
  49. int editResID,
  50. HRESULT hr,
  51. const CHString& message,
  52. int titleResID)
  53. {
  54. //gripe(parentDialog, editResID, hr, message, String::load(titleResID));
  55. }
  56. void gripe(HWND parentDialog,
  57. int editResID,
  58. HRESULT hr,
  59. const CHString& message,
  60. const CHString& title)
  61. {
  62. //error(parentDialog, hr, message, title);
  63. HWND edit = ::GetDlgItem(parentDialog, editResID);
  64. ::SendMessage(edit, EM_SETSEL, 0, -1);
  65. ::SetFocus(edit);
  66. }
  67. // threadsafe
  68. void gripe(HWND parentDialog, int editResID, const CHString& message)
  69. {
  70. //gripe(parentDialog, editResID, message, String());
  71. }
  72. void FlipBits(long& bits, long mask, bool state)
  73. {
  74. // ATLASSERT(mask);
  75. if (state)
  76. {
  77. bits |= mask;
  78. }
  79. else
  80. {
  81. bits &= ~mask;
  82. }
  83. }
  84. void error(HWND parent,
  85. HRESULT hr,
  86. const CHString& message)
  87. {
  88. //error(parent, hr, message, String());
  89. }
  90. void error(HWND parent,
  91. HRESULT hr,
  92. const CHString& message,
  93. int titleResID)
  94. {
  95. //ATLASSERT(titleResID > 0);
  96. //error(parent, hr, message, String::load(titleResID));
  97. }
  98. void error(HWND parent,
  99. HRESULT hr,
  100. const CHString& message,
  101. const CHString& title)
  102. {
  103. // ATLASSERT(::IsWindow(parent));
  104. // ATLASSERT(!message.empty());
  105. CHString new_message = message + TEXT("\n\n");
  106. if (FAILED(hr))
  107. {
  108. if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
  109. {
  110. // new_message += GetErrorMessage(hr & 0x0000ffff);
  111. }
  112. else
  113. {
  114. // new_message += CHString::Format(IDS_HRESULT_SANS_MESSAGE, hr);
  115. }
  116. }
  117. MessageBox(parent, new_message,
  118. title, MB_ICONERROR | MB_OK | MB_APPLMODAL);
  119. }
  120. void error(HWND parent,
  121. HRESULT hr,
  122. int messageResID,
  123. int titleResID)
  124. {
  125. // error(parent, hr, String::load(messageResID), String::load(titleResID));
  126. }
  127. void error(HWND parent,
  128. HRESULT hr,
  129. int messageResID)
  130. {
  131. // error(parent, hr, String::load(messageResID));
  132. }
  133. BOOL IsCurrentUserAdministrator()
  134. {
  135. HANDLE hToken;
  136. DWORD dwStatus;
  137. DWORD dwAccessMask;
  138. DWORD dwAccessDesired;
  139. DWORD dwACLSize;
  140. DWORD dwStructureSize = sizeof(PRIVILEGE_SET);
  141. PACL pACL = NULL;
  142. PSID psidAdmin = NULL;
  143. BOOL bReturn = FALSE;
  144. PRIVILEGE_SET ps;
  145. GENERIC_MAPPING GenericMapping; PSECURITY_DESCRIPTOR psdAdmin = NULL;
  146. SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
  147. __try {
  148. // AccessCheck() requires an impersonation token.
  149. ImpersonateSelf(SecurityImpersonation);
  150. if (!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE,&hToken))
  151. {
  152. if (GetLastError() != ERROR_NO_TOKEN)
  153. __leave;// If the thread does not have an access token, we'll
  154. // examine the access token associated with the process.
  155. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
  156. __leave;
  157. }
  158. if (!AllocateAndInitializeSid(
  159. &SystemSidAuthority,
  160. 2,
  161. SECURITY_BUILTIN_DOMAIN_RID,
  162. DOMAIN_ALIAS_RID_ADMINS,
  163. 0, 0, 0, 0, 0, 0, &psidAdmin))
  164. __leave;
  165. psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  166. if (psdAdmin == NULL)
  167. __leave;
  168. if (!InitializeSecurityDescriptor(
  169. psdAdmin,
  170. SECURITY_DESCRIPTOR_REVISION))
  171. __leave;
  172. // Compute size needed for the ACL.
  173. dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) +
  174. GetLengthSid(psidAdmin) - sizeof(DWORD); // Allocate memory for ACL.
  175. pACL = (PACL)LocalAlloc(LPTR, dwACLSize);
  176. if (pACL == NULL)
  177. __leave; // Initialize the new ACL.
  178. if (!InitializeAcl(pACL, dwACLSize, ACL_REVISION2))
  179. __leave;
  180. dwAccessMask= ACCESS_READ | ACCESS_WRITE;
  181. // Add the access-allowed ACE to the DACL.
  182. if (!AddAccessAllowedAce(pACL, ACL_REVISION2,
  183. dwAccessMask, psidAdmin))
  184. __leave; // Set our DACL to the SD.
  185. if (!SetSecurityDescriptorDacl(psdAdmin, TRUE, pACL, FALSE))
  186. __leave; // AccessCheck is sensitive about what is in the SD; set
  187. // the group and owner.
  188. SetSecurityDescriptorGroup(psdAdmin, psidAdmin, FALSE);
  189. SetSecurityDescriptorOwner(psdAdmin, psidAdmin, FALSE);
  190. if (!IsValidSecurityDescriptor(psdAdmin))
  191. __leave;
  192. dwAccessDesired = ACCESS_READ; //
  193. // Initialize GenericMapping structure even though we
  194. // won't be using generic rights.
  195. //
  196. GenericMapping.GenericRead = ACCESS_READ;
  197. GenericMapping.GenericWrite = ACCESS_WRITE;
  198. GenericMapping.GenericExecute = 0;
  199. GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE;
  200. if (!AccessCheck(psdAdmin, hToken, dwAccessDesired,
  201. &GenericMapping, &ps, &dwStructureSize, &dwStatus,
  202. &bReturn)) {
  203. __leave;
  204. }
  205. } __finally { // Cleanup
  206. RevertToSelf();
  207. if (pACL) LocalFree(pACL);
  208. if (psdAdmin) LocalFree(psdAdmin);
  209. if (psidAdmin) FreeSid(psidAdmin);
  210. }
  211. return bReturn;
  212. }
  213. bool IsTCPIPInstalled()
  214. {
  215. /* HKEY key = 0;
  216. LONG result =
  217. Win::RegOpenKeyEx(
  218. HKEY_LOCAL_MACHINE,
  219. TEXT("System\\CurrentControlSet\\Services\\Tcpip\\Linkage"),
  220. KEY_QUERY_VALUE,
  221. key);
  222. if (result == ERROR_SUCCESS)
  223. {
  224. DWORD data_size = 0;
  225. result =
  226. Win::RegQueryValueEx(
  227. key,
  228. TEXT("Export"),
  229. 0,
  230. 0,
  231. &data_size);
  232. ATLASSERT(result == ERROR_SUCCESS);
  233. if (data_size > 2)
  234. {
  235. // the value is non-null
  236. return true;
  237. }
  238. }
  239. */
  240. return false;
  241. }
  242. CHString GetTrimmedDlgItemText(HWND parentDialog, UINT itemResID)
  243. {
  244. // ATLASSERT(IsWindow(parentDialog));
  245. // ATLASSERT(itemResID > 0);
  246. HWND item = GetDlgItem(parentDialog, itemResID);
  247. if (!item)
  248. {
  249. // The empty string
  250. return CHString();
  251. }
  252. TCHAR temp[256] = {0};
  253. ::GetWindowText(item, temp, 256);
  254. return CHString(temp);
  255. }
  256. void StringLoad(UINT resID, LPCTSTR buf, UINT size)
  257. {
  258. }