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.

340 lines
8.7 KiB

  1. //
  2. // Include Files.
  3. //
  4. #include <nt.h>
  5. #include <ntrtl.h>
  6. #include <nturtl.h>
  7. #include "input.h"
  8. #include "winnlsp.h"
  9. #include <windowsx.h>
  10. #include <regstr.h>
  11. #include <tchar.h>
  12. #include <stdlib.h>
  13. #include <setupapi.h>
  14. #include <syssetup.h>
  15. #include <winuserp.h>
  16. #include <userenv.h>
  17. #include "inputdlg.h"
  18. #include "util.h"
  19. //
  20. // Global Variables.
  21. //
  22. static TCHAR szIntlInf[] = TEXT("intl.inf");
  23. //
  24. // Function Prototypes.
  25. //
  26. VOID
  27. Region_RebootTheSystem();
  28. BOOL
  29. Region_OpenIntlInfFile(HINF *phInf);
  30. BOOL
  31. Region_CloseInfFile(HINF *phInf);
  32. BOOL
  33. Region_ReadDefaultLayoutFromInf(
  34. LPTSTR pszLocale,
  35. LPDWORD pdwLocale,
  36. LPDWORD pdwLayout,
  37. LPDWORD pdwLocale2,
  38. LPDWORD pdwLayout2,
  39. HINF hIntlInf);
  40. ////////////////////////////////////////////////////////////////////////////
  41. //
  42. // Region_RebootTheSystem
  43. //
  44. // This routine enables all privileges in the token, calls ExitWindowsEx
  45. // to reboot the system, and then resets all of the privileges to their
  46. // old state.
  47. //
  48. ////////////////////////////////////////////////////////////////////////////
  49. VOID Region_RebootTheSystem()
  50. {
  51. HANDLE Token = NULL;
  52. ULONG ReturnLength, Index;
  53. PTOKEN_PRIVILEGES NewState = NULL;
  54. PTOKEN_PRIVILEGES OldState = NULL;
  55. BOOL Result;
  56. // Only allow admin privilege user for system reboot.
  57. if (!IsAdminPrivilegeUser())
  58. return;
  59. Result = OpenProcessToken( GetCurrentProcess(),
  60. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  61. &Token );
  62. if (Result)
  63. {
  64. ReturnLength = 4096;
  65. NewState = (PTOKEN_PRIVILEGES)LocalAlloc(LPTR, ReturnLength);
  66. OldState = (PTOKEN_PRIVILEGES)LocalAlloc(LPTR, ReturnLength);
  67. Result = (BOOL)((NewState != NULL) && (OldState != NULL));
  68. if (Result)
  69. {
  70. Result = GetTokenInformation( Token, // TokenHandle
  71. TokenPrivileges, // TokenInformationClass
  72. NewState, // TokenInformation
  73. ReturnLength, // TokenInformationLength
  74. &ReturnLength ); // ReturnLength
  75. if (Result)
  76. {
  77. //
  78. // Set the state settings so that all privileges are
  79. // enabled...
  80. //
  81. if (NewState->PrivilegeCount > 0)
  82. {
  83. for (Index = 0; Index < NewState->PrivilegeCount; Index++)
  84. {
  85. NewState->Privileges[Index].Attributes = SE_PRIVILEGE_ENABLED;
  86. }
  87. }
  88. Result = AdjustTokenPrivileges( Token, // TokenHandle
  89. FALSE, // DisableAllPrivileges
  90. NewState, // NewState
  91. ReturnLength, // BufferLength
  92. OldState, // PreviousState
  93. &ReturnLength ); // ReturnLength
  94. if (Result)
  95. {
  96. ExitWindowsEx(EWX_REBOOT, 0);
  97. AdjustTokenPrivileges( Token,
  98. FALSE,
  99. OldState,
  100. 0,
  101. NULL,
  102. NULL );
  103. }
  104. }
  105. }
  106. }
  107. if (NewState != NULL)
  108. {
  109. LocalFree(NewState);
  110. }
  111. if (OldState != NULL)
  112. {
  113. LocalFree(OldState);
  114. }
  115. if (Token != NULL)
  116. {
  117. CloseHandle(Token);
  118. }
  119. }
  120. ////////////////////////////////////////////////////////////////////////////
  121. //
  122. // Region_OpenInfFile
  123. //
  124. ////////////////////////////////////////////////////////////////////////////
  125. BOOL Region_OpenIntlInfFile(HINF *phInf)
  126. {
  127. HINF hIntlInf;
  128. //
  129. // Open the intl.inf file.
  130. //
  131. hIntlInf = SetupOpenInfFile(szIntlInf, NULL, INF_STYLE_WIN4, NULL);
  132. if (hIntlInf == INVALID_HANDLE_VALUE)
  133. {
  134. return (FALSE);
  135. }
  136. if (!SetupOpenAppendInfFile(NULL, hIntlInf, NULL))
  137. {
  138. SetupCloseInfFile(hIntlInf);
  139. return (FALSE);
  140. }
  141. *phInf = hIntlInf;
  142. return (TRUE);
  143. }
  144. ////////////////////////////////////////////////////////////////////////////
  145. //
  146. // RegionCloseInfFile
  147. //
  148. ////////////////////////////////////////////////////////////////////////////
  149. BOOL Region_CloseInfFile(HINF *phInf)
  150. {
  151. SetupCloseInfFile(*phInf);
  152. *phInf = INVALID_HANDLE_VALUE;
  153. return (TRUE);
  154. }
  155. ////////////////////////////////////////////////////////////////////////////
  156. //
  157. // Region_ReadDefaultLayoutFromInf
  158. //
  159. ////////////////////////////////////////////////////////////////////////////
  160. BOOL Region_ReadDefaultLayoutFromInf(
  161. LPTSTR pszLocale,
  162. LPDWORD pdwLocale,
  163. LPDWORD pdwLayout,
  164. LPDWORD pdwLocale2,
  165. LPDWORD pdwLayout2,
  166. HINF hIntlInf)
  167. {
  168. INFCONTEXT Context;
  169. TCHAR szPair[MAX_PATH * 2];
  170. LPTSTR pPos;
  171. DWORD dwLangIn = LANGIDFROMLCID(TransNum(pszLocale));
  172. int iField;
  173. //
  174. // Get the first (default) LANGID:HKL pair for the given locale.
  175. // Example String: "0409:00000409"
  176. //
  177. szPair[0] = 0;
  178. if (SetupFindFirstLine( hIntlInf,
  179. TEXT("Locales"),
  180. pszLocale,
  181. &Context ))
  182. {
  183. SetupGetStringField(&Context, 5, szPair, MAX_PATH, NULL);
  184. }
  185. //
  186. // Make sure we have a string.
  187. //
  188. if (szPair[0] == 0)
  189. {
  190. return (FALSE);
  191. }
  192. //
  193. // Find the colon in the string and then set the position
  194. // pointer to the next character.
  195. //
  196. pPos = szPair;
  197. while (*pPos)
  198. {
  199. if ((*pPos == CHAR_COLON) && (pPos != szPair))
  200. {
  201. *pPos = 0;
  202. pPos++;
  203. break;
  204. }
  205. pPos++;
  206. }
  207. if (pdwLayout2)
  208. *pdwLayout2 = 0;
  209. if (pdwLocale2)
  210. *pdwLocale2 = 0;
  211. //
  212. // If there is a layout, then return the input locale and the layout.
  213. //
  214. if ((*pPos) &&
  215. (*pdwLocale = TransNum(szPair)) &&
  216. (*pdwLayout = TransNum(pPos)))
  217. {
  218. if ((!pdwLocale2) ||
  219. (!pdwLayout2) ||
  220. (dwLangIn == LANGIDFROMLCID(*pdwLocale)))
  221. {
  222. return (TRUE);
  223. }
  224. //
  225. // If we get here, the language has a default layout that has a
  226. // different locale than the language (e.g. Thai). We want the
  227. // default locale to be English (so that logon can occur with a US
  228. // keyboard), but the first Thai keyboard layout should be installed
  229. // when the Thai locale is chosen. This is why we have two locales
  230. // and layouts passed back to the caller.
  231. //
  232. iField = 6;
  233. while (SetupGetStringField(&Context, iField, szPair, MAX_PATH, NULL))
  234. {
  235. DWORD dwLoc, dwLay;
  236. //
  237. // Make sure we have a string.
  238. //
  239. if (szPair[0] == 0)
  240. {
  241. iField++;
  242. continue;
  243. }
  244. //
  245. // Find the colon in the string and then set the position
  246. // pointer to the next character.
  247. //
  248. pPos = szPair;
  249. while (*pPos)
  250. {
  251. if ((*pPos == CHAR_COLON) && (pPos != szPair))
  252. {
  253. *pPos = 0;
  254. pPos++;
  255. break;
  256. }
  257. pPos++;
  258. }
  259. if (*pPos == 0)
  260. {
  261. iField++;
  262. continue;
  263. }
  264. dwLoc = TransNum(szPair);
  265. dwLay = TransNum(pPos);
  266. if ((dwLoc == 0) || (dwLay == 0))
  267. {
  268. iField++;
  269. continue;
  270. }
  271. if (LANGIDFROMLCID(dwLoc) == dwLangIn)
  272. {
  273. *pdwLayout2 = dwLay;
  274. *pdwLocale2 = dwLoc;
  275. return (TRUE);
  276. }
  277. iField++;
  278. }
  279. //
  280. // If we get here, then no matching locale could be found.
  281. // This should not happen, but do the right thing and
  282. // only pass back the default layout if it does.
  283. //
  284. return (TRUE);
  285. }
  286. //
  287. // Return failure.
  288. //
  289. return (FALSE);
  290. }