Source code of Windows XP (NT5)
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.

574 lines
13 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. sgminit.c
  5. Abstract:
  6. Implements the initialization/termination code for the data gather portion
  7. of scanstate v1 compatiblity.
  8. Author:
  9. Jim Schmidt (jimschm) 12-Mar-2000
  10. Revision History:
  11. <alias> <date> <comments>
  12. --*/
  13. //
  14. // Includes
  15. //
  16. #include "pch.h"
  17. #include "v1p.h"
  18. #define DBG_V1 "v1"
  19. //
  20. // Strings
  21. //
  22. // None
  23. //
  24. // Constants
  25. //
  26. // None
  27. //
  28. // Macros
  29. //
  30. // None
  31. //
  32. // Types
  33. //
  34. // None
  35. //
  36. // Globals
  37. //
  38. PMAPSTRUCT g_EnvMap;
  39. PMAPSTRUCT g_UndefMap;
  40. PMAPSTRUCT g_RevEnvMap;
  41. HASHTABLE g_RenameSrcTable;
  42. HASHTABLE g_RenameDestTable;
  43. HASHTABLE g_DePersistTable;
  44. PMHANDLE g_V1Pool;
  45. MIG_OBJECTTYPEID g_FileType;
  46. MIG_OBJECTTYPEID g_RegType;
  47. MIG_ATTRIBUTEID g_OsFileAttribute;
  48. MIG_ATTRIBUTEID g_CopyIfRelevantAttr;
  49. MIG_ATTRIBUTEID g_LockPartitionAttr;
  50. //
  51. // Macro expansion list
  52. //
  53. //
  54. // Private function prototypes
  55. //
  56. VCMINITIALIZE ScriptVcmInitialize;
  57. SGMINITIALIZE ScriptSgmInitialize;
  58. BOOL
  59. pParseAllInfs (
  60. VOID
  61. );
  62. //
  63. // Macro expansion definition
  64. //
  65. // None
  66. //
  67. // Code
  68. //
  69. VOID
  70. pPrepareIsmEnvironment (
  71. VOID
  72. )
  73. {
  74. if (IsmGetRealPlatform() == PLATFORM_SOURCE) {
  75. SetIsmEnvironmentFromPhysicalMachine (g_EnvMap, FALSE, g_UndefMap);
  76. } else {
  77. SetIsmEnvironmentFromVirtualMachine (g_EnvMap, g_RevEnvMap, g_UndefMap);
  78. }
  79. }
  80. BOOL
  81. pInitGlobals (
  82. IN PMIG_LOGCALLBACK LogCallback
  83. )
  84. {
  85. BOOL result;
  86. LogReInit (NULL, NULL, NULL, (PLOGCALLBACK) LogCallback);
  87. g_V1Pool = PmCreateNamedPool ("v1 sgm");
  88. g_EnvMap = CreateStringMapping();
  89. g_UndefMap = CreateStringMapping();
  90. g_RevEnvMap = CreateStringMapping();
  91. g_FileType = MIG_FILE_TYPE;
  92. g_RegType = MIG_REGISTRY_TYPE;
  93. result = g_V1Pool && g_EnvMap && g_UndefMap && g_RevEnvMap;
  94. if (!result) {
  95. DEBUGMSG ((DBG_ERROR, "Unable to initialize gather module globals"));\
  96. }
  97. return result;
  98. }
  99. BOOL
  100. pCommonInit (
  101. IN PVOID Reserved,
  102. IN BOOL VcmMode
  103. )
  104. {
  105. g_OsFileAttribute = IsmRegisterAttribute (S_ATTRIBUTE_OSFILE, FALSE);
  106. g_CopyIfRelevantAttr = IsmRegisterAttribute (S_ATTRIBUTE_COPYIFRELEVANT, FALSE);
  107. g_LockPartitionAttr = IsmRegisterAttribute (S_ATTRIBUTE_PARTITIONLOCK, FALSE);
  108. g_DePersistTable = HtAlloc ();
  109. InitRules();
  110. //
  111. // Call special conversion entry point
  112. //
  113. InitSpecialConversion (PLATFORM_SOURCE);
  114. InitSpecialRename (PLATFORM_SOURCE);
  115. //
  116. // Save shell folder environment
  117. //
  118. pPrepareIsmEnvironment();
  119. return TRUE;
  120. }
  121. BOOL
  122. pGetDomainUserName (
  123. OUT PCTSTR *Domain,
  124. OUT PCTSTR *User
  125. )
  126. {
  127. TCHAR userName[256];
  128. TCHAR domainName[256];
  129. DWORD size;
  130. HANDLE token = NULL;
  131. BOOL b;
  132. PTOKEN_USER tokenInfo = NULL;
  133. BOOL result = FALSE;
  134. DWORD domainSize;
  135. SID_NAME_USE dontCare;
  136. MIG_OBJECTSTRINGHANDLE regObject;
  137. MIG_CONTENT regContent;
  138. *Domain = NULL;
  139. *User = NULL;
  140. //
  141. // Assert that this is the source platform. We access the system directly.
  142. //
  143. MYASSERT (IsmGetRealPlatform() == PLATFORM_SOURCE);
  144. __try {
  145. if (ISWIN9X()) {
  146. size = ARRAYSIZE(userName);
  147. if (!GetUserName (userName, &size)) {
  148. LOG ((LOG_WARNING, (PCSTR) MSG_CANT_GET_USERNAME));
  149. __leave;
  150. }
  151. *User = DuplicateText (userName);
  152. regObject = IsmCreateObjectHandle (
  153. TEXT("HKLM\\System\\CurrentControlSet\\Services\\MSNP32\\NetworkProvider"),
  154. TEXT("AuthenticatingAgent")
  155. );
  156. if (IsmAcquireObject (g_RegType, regObject, &regContent)) {
  157. if (!regContent.ContentInFile &&
  158. regContent.MemoryContent.ContentBytes &&
  159. regContent.Details.DetailsSize == sizeof (DWORD) &&
  160. *((PDWORD) regContent.Details.DetailsData) == REG_SZ
  161. ) {
  162. *Domain = DuplicateText ((PCTSTR) regContent.MemoryContent.ContentBytes);
  163. }
  164. IsmReleaseObject (&regContent);
  165. }
  166. IsmDestroyObjectHandle (regObject);
  167. } else {
  168. if (!OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &token)) {
  169. LOG ((LOG_WARNING, (PCSTR) MSG_PROCESS_TOKEN_ERROR));
  170. __leave;
  171. }
  172. size = 0;
  173. GetTokenInformation (
  174. token,
  175. TokenUser, // sdk enum value
  176. NULL,
  177. 0,
  178. &size
  179. );
  180. if (size) {
  181. tokenInfo = (PTOKEN_USER) MemAllocUninit (size);
  182. b = GetTokenInformation (
  183. token,
  184. TokenUser,
  185. tokenInfo,
  186. size,
  187. &size
  188. );
  189. } else {
  190. b = FALSE;
  191. }
  192. if (!b) {
  193. LOG ((LOG_WARNING, (PCSTR) MSG_PROCESS_TOKEN_INFO_ERROR));
  194. __leave;
  195. }
  196. size = ARRAYSIZE (userName);
  197. domainSize = ARRAYSIZE (domainName);
  198. b = LookupAccountSid (
  199. NULL,
  200. tokenInfo->User.Sid,
  201. userName,
  202. &size,
  203. domainName,
  204. &domainSize,
  205. &dontCare
  206. );
  207. if (!b) {
  208. LOG ((LOG_WARNING, (PCSTR) MSG_SECURITY_ID_LOOKUP_ERROR));
  209. __leave;
  210. }
  211. *User = DuplicateText (userName);
  212. if (*domainName) {
  213. *Domain = DuplicateText (domainName);
  214. }
  215. }
  216. result = TRUE;
  217. }
  218. __finally {
  219. if (tokenInfo) {
  220. FreeAlloc (tokenInfo);
  221. }
  222. if (token) {
  223. CloseHandle (token);
  224. }
  225. }
  226. return result;
  227. }
  228. BOOL
  229. WINAPI
  230. ScriptSgmInitialize (
  231. IN PMIG_LOGCALLBACK LogCallback,
  232. IN PVOID Reserved
  233. )
  234. {
  235. TCHAR userName[256];
  236. TCHAR domainName[256];
  237. PCTSTR srcUserName;
  238. PCTSTR srcDomainName;
  239. if (!pInitGlobals (LogCallback)) {
  240. return FALSE;
  241. }
  242. g_RenameSrcTable = HtAllocWithData (sizeof (MIG_DATAHANDLE));
  243. g_RenameDestTable = HtAllocWithData (sizeof (MIG_DATAHANDLE));
  244. if (IsmGetRealPlatform() == PLATFORM_DESTINATION) {
  245. IsmGetTransportVariable (PLATFORM_SOURCE, S_USER_SECTION, S_USER_INFKEY, userName, sizeof (userName));
  246. if (!IsmGetTransportVariable (PLATFORM_SOURCE, S_USER_SECTION, S_DOMAIN_INFKEY, domainName, sizeof (domainName))) {
  247. if (IsmIsEnvironmentFlagSet (PLATFORM_DESTINATION, NULL, S_REQUIRE_DOMAIN_USER)) {
  248. //
  249. // NOTE: We could create the user account for the non-domain case.
  250. //
  251. IsmSetCancel();
  252. SetLastError (ERROR_INVALID_DOMAINNAME);
  253. LOG ((LOG_ERROR, (PCSTR) MSG_DOMAIN_REQUIRED));
  254. return FALSE;
  255. }
  256. } else {
  257. IsmSetEnvironmentString (
  258. PLATFORM_SOURCE,
  259. S_SYSENVVAR_GROUP,
  260. TEXT("USERDOMAIN"),
  261. domainName
  262. );
  263. AddStringMappingPair (g_EnvMap, TEXT("%DOMAINNAME%"), domainName);
  264. }
  265. IsmSetEnvironmentString (
  266. PLATFORM_SOURCE,
  267. S_SYSENVVAR_GROUP,
  268. TEXT("USERNAME"),
  269. userName
  270. );
  271. AddStringMappingPair (g_EnvMap, TEXT("%USERNAME%"), userName);
  272. } else {
  273. if (!pGetDomainUserName (&srcDomainName, &srcUserName)) {
  274. if (IsmIsEnvironmentFlagSet (PLATFORM_DESTINATION, NULL, S_REQUIRE_DOMAIN_USER)) {
  275. return FALSE;
  276. }
  277. } else {
  278. IsmSetTransportVariable (PLATFORM_SOURCE, S_USER_SECTION, S_USER_INFKEY, srcUserName);
  279. IsmSetEnvironmentString (
  280. PLATFORM_SOURCE,
  281. S_SYSENVVAR_GROUP,
  282. TEXT("USERNAME"),
  283. srcUserName
  284. );
  285. AddStringMappingPair (g_EnvMap, TEXT("%USERNAME%"), srcUserName);
  286. if (srcDomainName) {
  287. IsmSetTransportVariable (PLATFORM_SOURCE, S_USER_SECTION, S_DOMAIN_INFKEY, srcDomainName);
  288. IsmSetEnvironmentString (
  289. PLATFORM_SOURCE,
  290. S_SYSENVVAR_GROUP,
  291. TEXT("USERDOMAIN"),
  292. srcDomainName
  293. );
  294. AddStringMappingPair (g_EnvMap, TEXT("%DOMAINNAME%"), srcDomainName);
  295. }
  296. FreeText (srcUserName);
  297. FreeText (srcDomainName);
  298. }
  299. }
  300. //
  301. // Parse the script and do the rest of the business
  302. //
  303. return pCommonInit (Reserved, FALSE);
  304. }
  305. VOID
  306. pSaveRegDword (
  307. IN PCTSTR InfKeyName,
  308. IN PCTSTR Key,
  309. IN PCTSTR Value
  310. )
  311. {
  312. TCHAR buffer[32];
  313. MIG_OBJECTSTRINGHANDLE regObject;
  314. MIG_CONTENT regContent;
  315. regObject = IsmCreateObjectHandle (Key, Value);
  316. if (IsmAcquireObject (g_RegType, regObject, &regContent)) {
  317. if (!regContent.ContentInFile &&
  318. regContent.MemoryContent.ContentBytes &&
  319. regContent.Details.DetailsSize == sizeof (DWORD) &&
  320. *((PDWORD) regContent.Details.DetailsData) == REG_DWORD
  321. ) {
  322. wsprintf (buffer, TEXT("0x%08X"), *((PDWORD) regContent.MemoryContent.ContentBytes));
  323. IsmSetTransportVariable (PLATFORM_SOURCE, S_SOURCE_MACHINE_SECTION, InfKeyName, buffer);
  324. }
  325. IsmReleaseObject (&regContent);
  326. }
  327. IsmDestroyObjectHandle (regObject);
  328. }
  329. VOID
  330. pSaveRegSz (
  331. IN PCTSTR InfKeyName,
  332. IN PCTSTR Key,
  333. IN PCTSTR Value
  334. )
  335. {
  336. MIG_OBJECTSTRINGHANDLE regObject;
  337. MIG_CONTENT regContent;
  338. regObject = IsmCreateObjectHandle (Key, Value);
  339. if (IsmAcquireObject (g_RegType, regObject, &regContent)) {
  340. if (!regContent.ContentInFile &&
  341. regContent.MemoryContent.ContentBytes &&
  342. regContent.Details.DetailsSize == sizeof (DWORD) &&
  343. *((PDWORD) regContent.Details.DetailsData) == REG_SZ
  344. ) {
  345. IsmSetTransportVariable (
  346. PLATFORM_SOURCE,
  347. S_SOURCE_MACHINE_SECTION,
  348. InfKeyName,
  349. (PCTSTR) regContent.MemoryContent.ContentBytes
  350. );
  351. }
  352. IsmReleaseObject (&regContent);
  353. }
  354. IsmDestroyObjectHandle (regObject);
  355. }
  356. BOOL
  357. WINAPI
  358. ScriptVcmInitialize (
  359. IN PMIG_LOGCALLBACK LogCallback,
  360. IN PVOID Reserved
  361. )
  362. {
  363. TCHAR buffer[256];
  364. DWORD d;
  365. PCTSTR domainName;
  366. PCTSTR userName;
  367. if (!pInitGlobals (LogCallback)) {
  368. return FALSE;
  369. }
  370. //
  371. // Save all the basic settings via the transport string interface
  372. //
  373. // version
  374. d = GetVersion();
  375. wsprintf (buffer, TEXT("0x%08x"), d);
  376. IsmSetTransportVariable (PLATFORM_SOURCE, S_SOURCE_MACHINE_SECTION, S_VERSION_INFKEY, buffer);
  377. // code page
  378. d = (DWORD) GetACP();
  379. wsprintf (buffer, TEXT("0x%08x"), d);
  380. IsmSetTransportVariable (PLATFORM_SOURCE, S_SOURCE_MACHINE_SECTION, S_ACP_INFKEY, buffer);
  381. //
  382. // MACRO EXPANSION LIST -- generate the code necessary to save the settings
  383. // described in the macro expansion list in v1p.h
  384. //
  385. #define DEFMAC(infname,key,value) pSaveRegDword(TEXT(infname),TEXT(key),TEXT(value));
  386. STANDARD_DWORD_SETTINGS
  387. #undef DEFMAC
  388. #define DEFMAC(infname,key,value) pSaveRegSz(TEXT(infname),TEXT(key),TEXT(value));
  389. STANDARD_STRING_SETTINGS
  390. if (ISWIN9X()) {
  391. STANDARD_STRING_SETTINGS_9X
  392. } else {
  393. STANDARD_STRING_SETTINGS_NT
  394. }
  395. #undef DEFMAC
  396. //
  397. // Save the current user
  398. //
  399. if (!pGetDomainUserName (&domainName, &userName)) {
  400. return FALSE;
  401. }
  402. IsmSetTransportVariable (PLATFORM_SOURCE, S_USER_SECTION, S_USER_INFKEY, userName);
  403. IsmSetEnvironmentString (
  404. PLATFORM_SOURCE,
  405. S_SYSENVVAR_GROUP,
  406. TEXT("USERNAME"),
  407. userName
  408. );
  409. AddStringMappingPair (g_EnvMap, TEXT("%USERNAME%"), userName);
  410. if (domainName) {
  411. IsmSetTransportVariable (PLATFORM_SOURCE, S_USER_SECTION, S_DOMAIN_INFKEY, domainName);
  412. IsmSetEnvironmentString (
  413. PLATFORM_SOURCE,
  414. S_SYSENVVAR_GROUP,
  415. TEXT("USERDOMAIN"),
  416. domainName
  417. );
  418. AddStringMappingPair (g_EnvMap, TEXT("%DOMAINNAME%"), domainName);
  419. }
  420. FreeText (userName);
  421. FreeText (domainName);
  422. //
  423. // Parse the script and do the rest of the business
  424. //
  425. return pCommonInit (Reserved, TRUE);
  426. }
  427. VOID
  428. WINAPI
  429. ScriptTerminate (
  430. VOID
  431. )
  432. {
  433. HtFree (g_RenameSrcTable);
  434. g_RenameSrcTable = NULL;
  435. HtFree (g_RenameDestTable);
  436. g_RenameDestTable = NULL;
  437. HtFree (g_DePersistTable);
  438. g_DePersistTable = NULL;
  439. TerminateRestoreCallback ();
  440. TerminateSpecialRename();
  441. TerminateSpecialConversion();
  442. TerminateRules();
  443. }