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.

1238 lines
38 KiB

  1. /* cmdenv.c - Environment supporting functions for command.lib
  2. *
  3. *
  4. * Modification History:
  5. *
  6. * williamh 13-May-1993 Created
  7. */
  8. #include "cmd.h"
  9. #include <cmdsvc.h>
  10. #include <demexp.h>
  11. #include <softpc.h>
  12. #include <mvdm.h>
  13. #include <ctype.h>
  14. #include <memory.h>
  15. #include <oemuni.h>
  16. #include <userenv.h>
  17. #include <userenvp.h>
  18. #define VDM_ENV_INC_SIZE 512
  19. CHAR windir[] = "windir";
  20. extern BOOL fSeparateWow;
  21. // These two functions are temp var filtering instruments
  22. //
  23. //
  24. VOID cmdCheckTempInit(VOID);
  25. LPSTR cmdCheckTemp(LPSTR lpszzEnv);
  26. // Transform the given DOS environment to 32bits environment.
  27. // WARNING!! The environment block we passed to 32bits must be in sort order.
  28. // Therefore, we call RtlSetEnvironmentVariable to do the work
  29. // The result string must be in ANSI character set.
  30. BOOL cmdXformEnvironment(PCHAR pEnv16, PANSI_STRING Env_A)
  31. {
  32. UNICODE_STRING Name_U, Value_U, Temp_U;
  33. STRING String;
  34. PWCHAR pwch, NewEnv, CurEnv, CurEnvCopy, pTmp;
  35. NTSTATUS Status;
  36. BOOL fFoundComSpec;
  37. USHORT NewEnvLen;
  38. PCHAR pEnv;
  39. DWORD Length;
  40. if (pEnv16 == NULL)
  41. return FALSE;
  42. // flag true if we alread found comspec envirnment
  43. // !!!! Do we allow two or more comspec in environment????????
  44. fFoundComSpec = FALSE;
  45. CurEnv = GetEnvironmentStringsW();
  46. pwch = CurEnv;
  47. // figure how long the environment strings is
  48. while (*pwch != UNICODE_NULL || *(pwch + 1) != UNICODE_NULL)
  49. pwch++;
  50. // plus 2 to include the last two NULL chars
  51. CurEnvCopy = malloc((pwch - CurEnv + 2) * sizeof(WCHAR));
  52. if (!CurEnvCopy)
  53. return FALSE;
  54. // make a copy of current process environment so we can walk through
  55. // it. The environment can be changed by any threads in the process
  56. // thus is not safe to walk through without a local copy
  57. RtlMoveMemory(CurEnvCopy, CurEnv, (pwch - CurEnv + 2) * sizeof(WCHAR));
  58. // create a new environment block. We don't want to change
  59. // any currnt process environment variables, instead, we are
  60. // preparing a new one for the new process.
  61. Status = RtlCreateEnvironment(FALSE, (PVOID *)&NewEnv);
  62. if (!NT_SUCCESS(Status)) {
  63. free(CurEnvCopy);
  64. return FALSE;
  65. }
  66. NewEnvLen = 0;
  67. // now pick up environment we want from the current environment
  68. // and set it to the new environment block
  69. // the variables we want:
  70. // (1). comspec
  71. // (2). current directories settings
  72. pwch = CurEnvCopy;
  73. while (*pwch != UNICODE_NULL) {
  74. if (*pwch == L'=') {
  75. // variable names started with L'=' are current directroy settings
  76. pTmp = wcschr(pwch + 1, L'=');
  77. if (pTmp) {
  78. Name_U.Buffer = pwch;
  79. Name_U.Length = (pTmp - pwch) * sizeof(WCHAR);
  80. RtlInitUnicodeString(&Value_U, pTmp + 1);
  81. Status = RtlSetEnvironmentVariable(&NewEnv, &Name_U, &Value_U);
  82. if (!NT_SUCCESS(Status)) {
  83. RtlDestroyEnvironment(NewEnv);
  84. free(CurEnvCopy);
  85. return FALSE;
  86. }
  87. // <name> + <'='> + <value> + <'\0'>
  88. NewEnvLen += Name_U.Length + Value_U.Length + 2 * sizeof(WCHAR);
  89. }
  90. }
  91. else if (!fFoundComSpec) {
  92. fFoundComSpec = !_wcsnicmp(pwch, L"COMSPEC=", 8);
  93. if (fFoundComSpec) {
  94. Name_U.Buffer = pwch;
  95. Name_U.Length = 7 * sizeof(WCHAR);
  96. RtlInitUnicodeString(&Value_U, pwch + 8);
  97. Status = RtlSetEnvironmentVariable(&NewEnv,
  98. &Name_U,
  99. &Value_U
  100. );
  101. if (!NT_SUCCESS(Status)) {
  102. RtlDestroyEnvironment(NewEnv);
  103. free(CurEnvCopy);
  104. return FALSE;
  105. }
  106. NewEnvLen += Name_U.Length + Value_U.Length + 2 * sizeof(WCHAR);
  107. }
  108. }
  109. pwch += wcslen(pwch) + 1;
  110. }
  111. // we are done with current process environment.
  112. free(CurEnvCopy);
  113. cmdCheckTempInit();
  114. // now deal with 16bits settings passed from dos.
  115. // characters in 16bits environment are in OEM character set
  116. // 16bit comspec environment variable
  117. fFoundComSpec = FALSE;
  118. while (*pEnv16 != '\0') {
  119. if (NULL != (pEnv = cmdCheckTemp(pEnv16))) {
  120. RtlInitString(&String, pEnv);
  121. Length = strlen(pEnv16);
  122. }
  123. else {
  124. RtlInitString(&String, pEnv16);
  125. Length = String.Length;
  126. }
  127. // discard 16bits comspec
  128. if (!fFoundComSpec) {
  129. fFoundComSpec = !_strnicmp(pEnv16, comspec, 8);
  130. if (fFoundComSpec) {
  131. // ignore 16bits comspec environment
  132. pEnv16 += Length + 1;
  133. continue;
  134. }
  135. }
  136. Status = RtlOemStringToUnicodeString(&Temp_U, &String, TRUE);
  137. if (!NT_SUCCESS(Status)) {
  138. RtlDestroyEnvironment(NewEnv);
  139. return FALSE;
  140. }
  141. pwch = wcschr(Temp_U.Buffer, L'=');
  142. if (pwch) {
  143. Name_U.Buffer = Temp_U.Buffer;
  144. Name_U.Length = (pwch - Temp_U.Buffer) * sizeof(WCHAR);
  145. RtlInitUnicodeString(&Value_U, pwch + 1);
  146. Status = RtlSetEnvironmentVariable( &NewEnv, &Name_U, &Value_U);
  147. RtlFreeUnicodeString(&Temp_U);
  148. if (!NT_SUCCESS(Status)) {
  149. RtlDestroyEnvironment(NewEnv);
  150. return FALSE;
  151. }
  152. NewEnvLen += Name_U.Length + Value_U.Length + 2 * sizeof(WCHAR);
  153. }
  154. pEnv16 += Length + 1;
  155. }
  156. // count the last terminated null char
  157. Temp_U.Length = NewEnvLen + sizeof(WCHAR);
  158. Temp_U.Buffer = NewEnv;
  159. Status = RtlUnicodeStringToAnsiString(Env_A, &Temp_U, TRUE);
  160. RtlDestroyEnvironment(NewEnv); /* don't need it anymore */
  161. return(NT_SUCCESS(Status));
  162. }
  163. CHAR* cmdFilterTempEnvironmentVariables(CHAR* lpzzEnv, DWORD cchInit)
  164. {
  165. PCHAR pTmp;
  166. PCHAR lpzzEnv32;
  167. PCHAR lpzzTemp;
  168. DWORD cchRemain = cchInit;
  169. DWORD cchIncrement = MAX_PATH;
  170. DWORD Len, LenTmp;
  171. DWORD Offset = 0;
  172. lpzzEnv32 = (PCHAR)malloc(cchInit);
  173. if (NULL == lpzzEnv32) {
  174. return(NULL);
  175. }
  176. cmdCheckTempInit();
  177. while('\0' != *lpzzEnv) {
  178. LenTmp = Len = strlen(lpzzEnv) + 1;
  179. // now copy the string
  180. if (NULL != (pTmp = cmdCheckTemp(lpzzEnv))) {
  181. LenTmp = strlen(pTmp) + 1;
  182. }
  183. else {
  184. pTmp = lpzzEnv;
  185. }
  186. if (cchRemain < (LenTmp + 1)) {
  187. if (cchIncrement < LenTmp) {
  188. cchIncrement = LenTmp;
  189. }
  190. lpzzTemp = (PCHAR)realloc(lpzzEnv32, cchInit + cchIncrement);
  191. if (NULL == lpzzTemp) {
  192. free(lpzzEnv32);
  193. return(NULL);
  194. }
  195. lpzzEnv32 = lpzzTemp;
  196. cchInit += cchIncrement;
  197. cchRemain += cchIncrement;
  198. }
  199. strcpy(lpzzEnv32 + Offset, pTmp);
  200. Offset += LenTmp;
  201. cchRemain -= LenTmp;
  202. lpzzEnv += Len;
  203. }
  204. *(lpzzEnv32 + Offset) = '\0';
  205. return(lpzzEnv32);
  206. }
  207. /* get ntvdm initial environment. This initial environment is necessary
  208. * for the first instance of command.com before it processing autoexec.bat
  209. * this function strips off an environment headed with "=" and
  210. * replace the comspec with 16bits comspec and upper case all environment vars.
  211. *
  212. * Entry: Client (ES:0) = buffer to receive the environment
  213. * Client (BX) = size in paragraphs of the given buffer
  214. *
  215. * Exit: (BX) = 0 if nothing to copy
  216. * (BX) <= the given size, function okay
  217. * (BX) > given size, (BX) has the required size
  218. */
  219. VOID cmdGetInitEnvironment(VOID)
  220. {
  221. CHAR *lpszzEnvBuffer, *lpszEnv;
  222. WORD cchEnvBuffer;
  223. CHAR *lpszzEnvStrings, * lpszz;
  224. WORD cchString;
  225. WORD cchRemain;
  226. WORD cchIncrement = MAX_PATH;
  227. BOOL fFoundComSpec = FALSE;
  228. BOOL fFoundWindir = FALSE;
  229. BOOL fVarIsWindir = FALSE;
  230. // if not during the initialization return nothing
  231. if (!IsFirstCall) {
  232. setBX(0);
  233. return;
  234. }
  235. if (cchInitEnvironment == 0) {
  236. //
  237. // If the PROMPT variable is not set, add it as $P$G. This is to
  238. // keep the command.com shell consistent with SCS cmd.exe(which
  239. // always does this) when we don't have a top level cmd shell.
  240. //
  241. {
  242. CHAR *pPromptStr = "PROMPT";
  243. char ach[2];
  244. if (!GetEnvironmentVariable(pPromptStr,ach,1)) {
  245. SetEnvironmentVariable(pPromptStr, "$P$G");
  246. }
  247. }
  248. cchRemain = 0;
  249. fFoundComSpec = FALSE;
  250. lpszEnv =
  251. lpszzEnvStrings = GetEnvironmentStrings();
  252. if (!lpszzEnvStrings)
  253. {
  254. // not enough memory
  255. RcMessageBox(EG_MALLOC_FAILURE, NULL, NULL,
  256. RMB_ICON_BANG | RMB_ABORT);
  257. TerminateVDM();
  258. }
  259. while (*lpszEnv) {
  260. cchString = strlen(lpszEnv) + 1;
  261. cchVDMEnv32 += cchString;
  262. lpszEnv += cchString;
  263. }
  264. lpszz = lpszzEnvStrings;
  265. if (lpszzVDMEnv32 != NULL)
  266. free(lpszzVDMEnv32);
  267. ++cchVDMEnv32;
  268. lpszzVDMEnv32 = cmdFilterTempEnvironmentVariables(lpszzEnvStrings, cchVDMEnv32);
  269. if (lpszzVDMEnv32 == NULL) {
  270. RcMessageBox(EG_MALLOC_FAILURE, NULL, NULL,
  271. RMB_ICON_BANG | RMB_ABORT);
  272. TerminateVDM();
  273. }
  274. lpszz = lpszzVDMEnv32; // we iterate through our copy
  275. // we have to form a presentable 32-bit environment
  276. // since we make our own copy -- deal with temp issues now
  277. // RtlMoveMemory(lpszzVDMEnv32, lpszzEnvStrings, cchVDMEnv32);
  278. while (*lpszz != '\0') {
  279. cchString = strlen(lpszz) + 1;
  280. if (*lpszz != '=') {
  281. if (!fFoundComSpec && !_strnicmp(lpszz, comspec, 8)){
  282. fFoundComSpec = TRUE;
  283. lpszz += cchString;
  284. continue;
  285. }
  286. if (!fFoundWindir && !_strnicmp(lpszz, windir, 6)) {
  287. fFoundWindir = TRUE;
  288. if (fSeparateWow) {
  289. // starting a separate WOW box - flag this one so its
  290. // name won't be converted to uppercase later.
  291. fVarIsWindir = TRUE;
  292. } else {
  293. // starting a DOS app, so remove "windir" to make sure
  294. // they don't think they are running under Windows.
  295. lpszz += cchString;
  296. continue;
  297. }
  298. }
  299. ///////////////////////// TEMP Var filtering ///////////////
  300. if (cchRemain < cchString) {
  301. if (cchIncrement < cchString)
  302. cchIncrement = cchString;
  303. lpszzEnvBuffer =
  304. (CHAR *)realloc(lpszzInitEnvironment,
  305. cchInitEnvironment + cchRemain + cchIncrement
  306. );
  307. if (lpszzEnvBuffer == NULL) {
  308. if (lpszzInitEnvironment != NULL) {
  309. free(lpszzInitEnvironment);
  310. lpszzInitEnvironment = NULL;
  311. }
  312. cchInitEnvironment = 0;
  313. break;
  314. }
  315. lpszzInitEnvironment = lpszzEnvBuffer;
  316. lpszzEnvBuffer += cchInitEnvironment;
  317. cchRemain += cchIncrement;
  318. }
  319. // the environment strings from base is in ANSI and dos needs OEM
  320. AnsiToOemBuff(lpszz, lpszzEnvBuffer, cchString);
  321. // convert the name to upper case -- ONLY THE NAME, NOT VALUE.
  322. if (!fVarIsWindir && (lpszEnv = strchr(lpszzEnvBuffer, '=')) != NULL){
  323. *lpszEnv = '\0';
  324. _strupr(lpszzEnvBuffer);
  325. *lpszEnv = '=';
  326. } else {
  327. fVarIsWindir = FALSE;
  328. }
  329. cchRemain -= cchString;
  330. cchInitEnvironment += cchString ;
  331. lpszzEnvBuffer += cchString;
  332. }
  333. lpszz += cchString;
  334. }
  335. FreeEnvironmentStrings(lpszzEnvStrings);
  336. lpszzEnvBuffer = (CHAR *) realloc(lpszzInitEnvironment,
  337. cchInitEnvironment + 1
  338. );
  339. if (lpszzEnvBuffer != NULL ) {
  340. lpszzInitEnvironment = lpszzEnvBuffer;
  341. lpszzInitEnvironment[cchInitEnvironment++] = '\0';
  342. }
  343. else {
  344. if (lpszzInitEnvironment != NULL) {
  345. free(lpszzInitEnvironment);
  346. lpszzInitEnvironment = NULL;
  347. }
  348. cchInitEnvironment = 0;
  349. }
  350. }
  351. lpszzEnvBuffer = (CHAR *) GetVDMAddr(getES(), 0);
  352. cchEnvBuffer = (WORD)getBX() << 4;
  353. if (cchEnvBuffer < cchInitEnvironment + cbComSpec) {
  354. setBX((USHORT)((cchInitEnvironment + cbComSpec + 15) >> 4));
  355. return;
  356. }
  357. else {
  358. strncpy(lpszzEnvBuffer, lpszComSpec, cbComSpec);
  359. lpszzEnvBuffer += cbComSpec;
  360. }
  361. if (lpszzInitEnvironment != NULL) {
  362. setBX((USHORT)((cchInitEnvironment + cbComSpec + 15) >> 4));
  363. memcpy(lpszzEnvBuffer, lpszzInitEnvironment, cchInitEnvironment);
  364. free(lpszzInitEnvironment);
  365. lpszzInitEnvironment = NULL;
  366. cchInitEnvironment = 0;
  367. }
  368. else
  369. setBX(0);
  370. return;
  371. }
  372. CHAR szHKTSTemp[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Terminal Server";
  373. CHAR szTSTempVal[] = "RootDrive";
  374. #define MAX_DOS_TEMPVAR_LENGTH 11
  375. //
  376. // this function is located in dem/demlfn.c
  377. //
  378. extern BOOL demIsShortPathName(LPSTR pszPath, BOOL fAllowWildCardName);
  379. typedef enum tageSysRootType {
  380. _SYSTEMROOT, // from systemroot env var
  381. _SYSTEMDRIVE, // from systemdrive env var
  382. _ROOTDRIVE // as specified in the buffer
  383. } SYSROOTTYPE;
  384. BOOL cmdGetSystemrootTemp(LPSTR lpszBuffer, DWORD Length, SYSROOTTYPE SysRoot)
  385. {
  386. CHAR *szTemp = "\\temp";
  387. CHAR *szSystemRoot = "SystemRoot";
  388. CHAR *szSystemDrive = "SystemDrive";
  389. CHAR *szSystemVar;
  390. DWORD len = 0;
  391. DWORD dwAttributes = 0xffffffff;
  392. DWORD dwError = ERROR_SUCCESS;
  393. BOOL fRet = FALSE;
  394. if (_SYSTEMROOT == SysRoot || _SYSTEMDRIVE == SysRoot) {
  395. szSystemVar = _SYSTEMROOT == SysRoot ? szSystemRoot : szSystemDrive;
  396. len = GetEnvironmentVariable(szSystemVar, lpszBuffer, Length);
  397. }
  398. else if (_ROOTDRIVE == SysRoot) {
  399. // so we have to look up the registry and see
  400. HKEY hkey;
  401. LONG lError;
  402. DWORD dwType;
  403. lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  404. szHKTSTemp,
  405. 0,
  406. KEY_QUERY_VALUE,
  407. &hkey);
  408. if (ERROR_SUCCESS == lError) {
  409. len = Length;
  410. lError = RegQueryValueEx(hkey,
  411. szTSTempVal,
  412. NULL,
  413. &dwType,
  414. lpszBuffer,
  415. &len);
  416. RegCloseKey(hkey);
  417. if (ERROR_SUCCESS != lError || REG_SZ != dwType) {
  418. return(FALSE);
  419. }
  420. --len; // length not to include terminating 0
  421. }
  422. }
  423. if (!len || len >= Length || (len + strlen(szTemp)) >= Length) {
  424. return(FALSE);
  425. }
  426. if (*(lpszBuffer + len - 1) == '\\') {
  427. ++szTemp;
  428. }
  429. strcat(lpszBuffer, szTemp);
  430. len = GetShortPathName(lpszBuffer, lpszBuffer, Length);
  431. if (len > 0 && len < Length) {
  432. dwAttributes = GetFileAttributes(lpszBuffer);
  433. }
  434. if (0xffffffff == dwAttributes) {
  435. dwError = GetLastError();
  436. if (ERROR_PATH_NOT_FOUND == dwError || ERROR_FILE_NOT_FOUND == dwError) {
  437. // we create this temp
  438. fRet = CreateDirectory(lpszBuffer, NULL);
  439. if (fRet) {
  440. dwAttributes = GetFileAttributes(lpszBuffer);
  441. }
  442. }
  443. }
  444. if (0xffffffff != dwAttributes) {
  445. fRet = !!(dwAttributes & FILE_ATTRIBUTE_DIRECTORY);
  446. }
  447. return(fRet);
  448. }
  449. #define SYS_ENVVARS "System\\CurrentControlSet\\Control\\Session Manager\\Environment"
  450. //*************************************************************
  451. //
  452. // GetSystemTempDir()
  453. //
  454. // Purpose: Gets the system temp directory in short form
  455. //
  456. // Parameters: lpDir - Receives the directory
  457. // lpcchSize - Size of the lpDir buffer
  458. //
  459. //
  460. // Return: TRUE if successful
  461. // FALSE if an error occurs
  462. //
  463. //*************************************************************
  464. BOOL GetSystemTempDir(LPSTR lpDir, LPDWORD lpcchSize)
  465. {
  466. CHAR szTemp[MAX_PATH];
  467. CHAR szDirectory[MAX_PATH];
  468. DWORD dwLength;
  469. HKEY hKey;
  470. LONG lResult;
  471. DWORD dwSize, dwType;
  472. WIN32_FILE_ATTRIBUTE_DATA fad;
  473. BOOL bRetVal = FALSE;
  474. szTemp[0] = '\0';
  475. szDirectory[0] = '\0';
  476. //
  477. // Look in the system environment variables
  478. //
  479. if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, SYS_ENVVARS, 0,
  480. KEY_READ, &hKey) == ERROR_SUCCESS) {
  481. //
  482. // Check for TEMP
  483. //
  484. dwSize = sizeof(szTemp);
  485. if (RegQueryValueEx (hKey, "TEMP", NULL, &dwType,
  486. (LPBYTE) szTemp, &dwSize) == ERROR_SUCCESS) {
  487. RegCloseKey (hKey);
  488. goto FoundTemp;
  489. }
  490. //
  491. // Check for TMP
  492. //
  493. dwSize = sizeof(szTemp);
  494. if (RegQueryValueEx (hKey, "TMP", NULL, &dwType,
  495. (LPBYTE) szTemp, &dwSize) == ERROR_SUCCESS) {
  496. RegCloseKey (hKey);
  497. goto FoundTemp;
  498. }
  499. RegCloseKey (hKey);
  500. }
  501. //
  502. // Check if %SystemRoot%\Temp exists
  503. //
  504. lstrcpy (szDirectory, "%SystemRoot%\\Temp");
  505. ExpandEnvironmentStrings (szDirectory, szTemp, sizeof(szTemp));
  506. if (GetFileAttributesEx (szTemp, GetFileExInfoStandard, &fad) &&
  507. fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  508. goto FoundTemp;
  509. }
  510. //
  511. // Check if %SystemDrive%\Temp exists
  512. //
  513. lstrcpy (szDirectory, "%SystemDrive%\\Temp");
  514. ExpandEnvironmentStrings (szDirectory, szTemp, sizeof(szTemp));
  515. if (GetFileAttributesEx (szTemp, GetFileExInfoStandard, &fad) &&
  516. fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  517. goto FoundTemp;
  518. }
  519. //
  520. // Last resort is %SystemRoot%
  521. //
  522. lstrcpy (szTemp, "%SystemRoot%");
  523. FoundTemp:
  524. ExpandEnvironmentStrings (szTemp, szDirectory, sizeof (szDirectory));
  525. GetShortPathName (szDirectory, szTemp, sizeof(szTemp));
  526. dwLength = lstrlen(szTemp) + 1;
  527. if (lpDir) {
  528. if (*lpcchSize >= dwLength) {
  529. lstrcpy (lpDir, szTemp);
  530. bRetVal = TRUE;
  531. } else {
  532. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  533. }
  534. } else {
  535. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  536. }
  537. *lpcchSize = dwLength;
  538. return bRetVal;
  539. }
  540. #define FOUND_TMP 0x01
  541. #define FOUND_TEMP 0x02
  542. #define GETSYSTEMTEMPDIRECTORYAORD 125
  543. #define GETSYSTEMTEMPDIRECTORYWORD 126
  544. DWORD gfFoundTmp = 0;
  545. CHAR gszTempDir[MAX_PATH];
  546. BOOL cmdCreateTempEnvironmentVar(
  547. LPSTR lpszTmpVar, // temp variable (or just it's name)
  548. DWORD Length, // the length of TmpVar or 0
  549. LPSTR lpszBuffer, // buffer containing
  550. DWORD LengthBuffer
  551. )
  552. {
  553. PCHAR pch;
  554. BOOL fSysTemp = FALSE;
  555. DWORD LengthTemp;
  556. if (NULL != (pch = strchr(lpszTmpVar, '='))) {
  557. // we have a var to check
  558. LengthTemp = (DWORD)(pch - lpszTmpVar);
  559. ++pch;
  560. if (!Length) { // no length supplied
  561. Length = strlen(pch);
  562. }
  563. else {
  564. Length -= (DWORD)(pch - lpszTmpVar);
  565. }
  566. // pch points to a variable that is to be checked for being dos-compliant
  567. if (strlen(pch) <= MAX_DOS_TEMPVAR_LENGTH && demIsShortPathName(pch, FALSE)) {
  568. return(FALSE); // no need to create anything
  569. }
  570. }
  571. else {
  572. LengthTemp = strlen(lpszTmpVar);
  573. }
  574. LengthTemp = LengthTemp < LengthBuffer-1 ? LengthTemp : LengthBuffer-2;
  575. strncpy(lpszBuffer, lpszTmpVar, LengthTemp);
  576. *(lpszBuffer + LengthTemp) = '=';
  577. lpszBuffer += LengthTemp+1; // past =
  578. LengthBuffer -= LengthTemp+1;
  579. if (gszTempDir[0]) {
  580. fSysTemp = TRUE;
  581. strncpy(lpszBuffer,gszTempDir,LengthBuffer);
  582. lpszBuffer[LengthBuffer-1] = '\0';
  583. }
  584. else {
  585. pch = lpszBuffer;
  586. // see if there is a registry override, used for Terminal Server
  587. fSysTemp = cmdGetSystemrootTemp(lpszBuffer, LengthBuffer, _ROOTDRIVE);
  588. if (fSysTemp && strlen(lpszBuffer) <= MAX_DOS_TEMPVAR_LENGTH) {
  589. return(fSysTemp);
  590. }
  591. fSysTemp = GetSystemTempDir(lpszBuffer, &LengthBuffer);
  592. if (fSysTemp) {
  593. fSysTemp = demIsShortPathName(lpszBuffer, FALSE);
  594. }
  595. if (!fSysTemp) {
  596. fSysTemp = cmdGetSystemrootTemp(lpszBuffer, LengthBuffer, _SYSTEMROOT);
  597. }
  598. if (fSysTemp) {
  599. strncpy(gszTempDir,pch,sizeof(gszTempDir));
  600. gszTempDir[sizeof(gszTempDir)-1] = '\0';
  601. }
  602. }
  603. return(fSysTemp);
  604. }
  605. VOID cmdCheckTempInit(VOID)
  606. {
  607. gfFoundTmp = 0;
  608. }
  609. CHAR*rgpszLongPathNames[] = {
  610. "ALLUSERSPROFILE",
  611. "APPDATA",
  612. "COMMONPROGRAMFILES",
  613. "COMMONPROGRAMFILES(x86)",
  614. "PROGRAMFILES",
  615. "PROGRAMFILES(X86)",
  616. "SYSTEMROOT",
  617. "USERPROFILE",
  618. //build environment vars
  619. "_NTTREE",
  620. "_NTX86TREE",
  621. "_NTPOSTBLD",
  622. "BINPLACE_EXCLUDE_FILE",
  623. "BINPLACE_LOG",
  624. "CLUSTERLOG",
  625. "INIT",
  626. "NTMAKEENV",
  627. "MSWNET",
  628. "PREFAST_ROOT",
  629. "RAZZLETOOLPATH",
  630. "SDXROOT"
  631. };
  632. BOOL cmdMakeShortEnvVar(LPSTR lpvarName, LPSTR lpvarValue, LPSTR lpszBuffer, DWORD Length)
  633. {
  634. DWORD lName, lValue;
  635. lName = strlen(lpvarName);
  636. if (lName + 2 > Length ) {
  637. return(FALSE);
  638. }
  639. strcpy(lpszBuffer, lpvarName);
  640. *(lpszBuffer + lName) = '=';
  641. lpszBuffer += lName + 1;
  642. Length -= lName + 1;
  643. lValue = GetShortPathNameOem(lpvarValue, lpszBuffer, Length);
  644. return (0 != lValue && lValue <= Length);
  645. }
  646. LPSTR cmdCheckTemp(LPSTR lpszzEnv)
  647. {
  648. CHAR *szTemp = "Temp";
  649. CHAR *szTmp = "Tmp";
  650. static CHAR szTmpVarBuffer[MAX_PATH+1];
  651. LPSTR pszTmpVar = NULL;
  652. BOOL fSubst = FALSE;
  653. CHAR *peq;
  654. DWORD i;
  655. peq = strchr(lpszzEnv, '=');
  656. if (NULL == peq) {
  657. return(NULL);
  658. }
  659. for (i = 0; i < sizeof(rgpszLongPathNames)/sizeof(rgpszLongPathNames[0]);++i) {
  660. INT llpn = strlen(rgpszLongPathNames[i]);
  661. if (!_strnicmp(lpszzEnv, rgpszLongPathNames[i], llpn) &&
  662. (llpn == (INT)(peq - lpszzEnv))) {
  663. // found a candidate to subst
  664. if (cmdMakeShortEnvVar(rgpszLongPathNames[i],
  665. peq+1,
  666. szTmpVarBuffer,
  667. sizeof(szTmpVarBuffer)/sizeof(szTmpVarBuffer[0]))) {
  668. pszTmpVar = szTmpVarBuffer;
  669. }
  670. return(pszTmpVar);
  671. }
  672. }
  673. if (!(gfFoundTmp & FOUND_TMP) || !(gfFoundTmp & FOUND_TEMP)) {
  674. if (!(gfFoundTmp & FOUND_TEMP) &&
  675. !_strnicmp(lpszzEnv, szTemp, 4) &&
  676. (4 == (int)(peq - lpszzEnv))) {
  677. // this is Temp env variable -- make a new one
  678. fSubst = TRUE;
  679. gfFoundTmp |= FOUND_TEMP;
  680. }
  681. else {
  682. if (!(gfFoundTmp & FOUND_TMP) &&
  683. !_strnicmp(lpszzEnv, szTmp, 3) &&
  684. (3 == (int)(peq - lpszzEnv))) {
  685. // this is tmp variable
  686. fSubst = TRUE;
  687. gfFoundTmp |= FOUND_TMP;
  688. }
  689. }
  690. if (fSubst) {
  691. // we have a candidate for substitution
  692. if (cmdCreateTempEnvironmentVar(lpszzEnv,
  693. 0,
  694. szTmpVarBuffer,
  695. sizeof(szTmpVarBuffer)/sizeof(szTmpVarBuffer[0]))) {
  696. pszTmpVar = szTmpVarBuffer;
  697. }
  698. }
  699. }
  700. return(pszTmpVar);
  701. }
  702. /** create a DOS environment for DOS.
  703. This is to get 32bits environment(comes with the dos executanle)
  704. and merge it with the environment settings in autoexec.nt so that
  705. COMMAND.COM gets the expected environment. We already created a
  706. double-null terminated string during autoexec.nt parsing. The string
  707. has mutltiple substring:
  708. "EnvName_1 NULL EnvValue_1 NULL[EnvName_n NULL EnvValue_n NULL] NULL"
  709. When name conflicts happened(a environment name was found in both
  710. 16 bits and 32 bits), we do the merging based on the rules:
  711. get 16bits value, expands any environment variables in the string
  712. by using the current environment.
  713. WARINING !!! The changes made by applications through directly manipulation
  714. in command.com environment segment will be lost.
  715. **/
  716. BOOL cmdCreateVDMEnvironment(
  717. PVDMENVBLK pVDMEnvBlk
  718. )
  719. {
  720. PCHAR p1, p2;
  721. BOOL fFoundComSpec;
  722. BOOL fFoundWindir;
  723. BOOL fVarIsWindir;
  724. DWORD Length, EnvStrLength;
  725. PCHAR lpszzVDMEnv, lpszzEnv, lpStrEnv;
  726. CHAR achBuffer[MAX_PATH + 1];
  727. pVDMEnvBlk->lpszzEnv = malloc(cchVDMEnv32 + cbComSpec + 1);
  728. if ((lpszzVDMEnv = pVDMEnvBlk->lpszzEnv) == NULL)
  729. return FALSE;
  730. pVDMEnvBlk->cchRemain = cchVDMEnv32 + cbComSpec + 1;
  731. pVDMEnvBlk->cchEnv = 0;
  732. // grab the 16bits comspec first
  733. if (cbComSpec && lpszComSpec && *lpszComSpec) {
  734. RtlCopyMemory(lpszzVDMEnv, lpszComSpec, cbComSpec);
  735. pVDMEnvBlk->cchEnv += cbComSpec;
  736. pVDMEnvBlk->cchRemain -= cbComSpec;
  737. lpszzVDMEnv += cbComSpec;
  738. }
  739. if (lpszzVDMEnv32) {
  740. // go through the given 32bits environmnet and take what we want:
  741. // everything except:
  742. // (1). variable name begin with '='
  743. // (2). compsec
  744. // (3). string without a '=' -- malformatted environment variable
  745. // (4). windir, so DOS apps don't think they're running under Windows
  746. // Note that strings pointed by lpszzVDMEnv32 are in ANSI character set
  747. fFoundComSpec = FALSE;
  748. fFoundWindir = FALSE;
  749. fVarIsWindir = FALSE;
  750. lpszzEnv = lpszzVDMEnv32;
  751. cmdCheckTempInit();
  752. while (*lpszzEnv) {
  753. Length = strlen(lpszzEnv) + 1;
  754. if (*lpszzEnv != '=' &&
  755. (p1 = strchr(lpszzEnv, '=')) != NULL &&
  756. (fFoundComSpec || !(fFoundComSpec = _strnicmp(lpszzEnv,
  757. comspec,
  758. 8
  759. ) == 0)) ){
  760. if (!fFoundWindir) {
  761. fFoundWindir = (_strnicmp(lpszzEnv,
  762. windir,
  763. 6) == 0);
  764. fVarIsWindir = fFoundWindir;
  765. }
  766. // subst temp variables
  767. lpStrEnv = cmdCheckTemp(lpszzEnv);
  768. if (NULL == lpStrEnv) {
  769. lpStrEnv = lpszzEnv;
  770. EnvStrLength = Length;
  771. }
  772. else {
  773. EnvStrLength = strlen(lpStrEnv) + 1;
  774. }
  775. if (!fVarIsWindir || fSeparateWow) {
  776. if (EnvStrLength >= pVDMEnvBlk->cchRemain) {
  777. lpszzVDMEnv = realloc(pVDMEnvBlk->lpszzEnv,
  778. pVDMEnvBlk->cchEnv +
  779. pVDMEnvBlk->cchRemain +
  780. VDM_ENV_INC_SIZE
  781. );
  782. if (lpszzVDMEnv == NULL){
  783. free(pVDMEnvBlk->lpszzEnv);
  784. return FALSE;
  785. }
  786. pVDMEnvBlk->cchRemain += VDM_ENV_INC_SIZE;
  787. pVDMEnvBlk->lpszzEnv = lpszzVDMEnv;
  788. lpszzVDMEnv += pVDMEnvBlk->cchEnv;
  789. }
  790. AnsiToOemBuff(lpStrEnv, lpszzVDMEnv, EnvStrLength);
  791. if (!fVarIsWindir) {
  792. *(lpszzVDMEnv + (DWORD)(p1 - lpszzEnv)) = '\0';
  793. _strupr(lpszzVDMEnv);
  794. *(lpszzVDMEnv + (DWORD)(p1 - lpszzEnv)) = '=';
  795. } else {
  796. fVarIsWindir = FALSE;
  797. }
  798. pVDMEnvBlk->cchEnv += EnvStrLength;
  799. pVDMEnvBlk->cchRemain -= EnvStrLength;
  800. lpszzVDMEnv += EnvStrLength;
  801. }
  802. else
  803. fVarIsWindir = FALSE;
  804. }
  805. lpszzEnv += Length;
  806. }
  807. }
  808. *lpszzVDMEnv = '\0';
  809. pVDMEnvBlk->cchEnv++;
  810. pVDMEnvBlk->cchRemain--;
  811. if (lpszzcmdEnv16 != NULL) {
  812. lpszzEnv = lpszzcmdEnv16;
  813. while (*lpszzEnv) {
  814. p1 = lpszzEnv + strlen(lpszzEnv) + 1;
  815. p2 = NULL;
  816. if (*p1) {
  817. p2 = achBuffer;
  818. // expand the strings pointed by p1
  819. Length = cmdExpandEnvironmentStrings(pVDMEnvBlk,
  820. p1,
  821. p2,
  822. MAX_PATH + 1
  823. );
  824. if (Length && Length > MAX_PATH) {
  825. p2 = (PCHAR) malloc(Length);
  826. if (p2 == NULL) {
  827. free(pVDMEnvBlk->lpszzEnv);
  828. return FALSE;
  829. }
  830. cmdExpandEnvironmentStrings(pVDMEnvBlk,
  831. p1,
  832. p2,
  833. Length
  834. );
  835. }
  836. }
  837. if (!cmdSetEnvironmentVariable(pVDMEnvBlk,
  838. lpszzEnv,
  839. p2
  840. )){
  841. if (p2 && p2 != achBuffer)
  842. free(p2);
  843. free(pVDMEnvBlk->lpszzEnv);
  844. return FALSE;
  845. }
  846. lpszzEnv = p1 + strlen(p1) + 1;
  847. }
  848. }
  849. lpszzVDMEnv = realloc(pVDMEnvBlk->lpszzEnv, pVDMEnvBlk->cchEnv);
  850. if (lpszzVDMEnv != NULL) {
  851. pVDMEnvBlk->lpszzEnv = lpszzVDMEnv;
  852. pVDMEnvBlk->cchRemain = 0;
  853. }
  854. return TRUE;
  855. }
  856. BOOL cmdSetEnvironmentVariable(
  857. PVDMENVBLK pVDMEnvBlk,
  858. PCHAR lpszName,
  859. PCHAR lpszValue
  860. )
  861. {
  862. PCHAR p, p1, pEnd;
  863. DWORD ExtraLength, Length, cchValue, cchOldValue;
  864. pVDMEnvBlk = (pVDMEnvBlk) ? pVDMEnvBlk : &cmdVDMEnvBlk;
  865. if (pVDMEnvBlk == NULL || lpszName == NULL)
  866. return FALSE;
  867. if (!(p = pVDMEnvBlk->lpszzEnv))
  868. return FALSE;
  869. pEnd = p + pVDMEnvBlk->cchEnv - 1;
  870. cchValue = (lpszValue) ? strlen(lpszValue) : 0;
  871. Length = strlen(lpszName);
  872. while (*p && ((p1 = strchr(p, '=')) == NULL ||
  873. (DWORD)(p1 - p) != Length ||
  874. _strnicmp(p, lpszName, Length)))
  875. p += strlen(p) + 1;
  876. if (*p) {
  877. // name was found in the base environment, replace it
  878. p1++;
  879. cchOldValue = strlen(p1);
  880. if (cchValue <= cchOldValue) {
  881. if (!cchValue) {
  882. RtlMoveMemory(p,
  883. p1 + cchOldValue + 1,
  884. (DWORD)(pEnd - p) - cchOldValue
  885. );
  886. pVDMEnvBlk->cchRemain += Length + cchOldValue + 2;
  887. pVDMEnvBlk->cchEnv -= Length + cchOldValue + 2;
  888. }
  889. else {
  890. RtlCopyMemory(p1,
  891. lpszValue,
  892. cchValue
  893. );
  894. if (cchValue != cchOldValue) {
  895. RtlMoveMemory(p1 + cchValue,
  896. p1 + cchOldValue,
  897. (DWORD)(pEnd - p1) - cchOldValue + 1
  898. );
  899. pVDMEnvBlk->cchEnv -= cchOldValue - cchValue;
  900. pVDMEnvBlk->cchRemain += cchOldValue - cchValue;
  901. }
  902. }
  903. return TRUE;
  904. }
  905. else {
  906. // need more space for the new value
  907. // we delete it from here and fall through
  908. RtlMoveMemory(p,
  909. p1 + cchOldValue + 1,
  910. (DWORD)(pEnd - p1) - cchOldValue
  911. );
  912. pVDMEnvBlk->cchRemain += Length + 1 + cchOldValue + 1;
  913. pVDMEnvBlk->cchEnv -= Length + 1 + cchOldValue + 1;
  914. }
  915. }
  916. if (cchValue) {
  917. ExtraLength = Length + 1 + cchValue + 1;
  918. if (pVDMEnvBlk->cchRemain < ExtraLength) {
  919. p = realloc(pVDMEnvBlk->lpszzEnv,
  920. pVDMEnvBlk->cchEnv + pVDMEnvBlk->cchRemain + ExtraLength
  921. );
  922. if (p == NULL)
  923. return FALSE;
  924. pVDMEnvBlk->lpszzEnv = p;
  925. pVDMEnvBlk->cchRemain += ExtraLength;
  926. }
  927. p = pVDMEnvBlk->lpszzEnv + pVDMEnvBlk->cchEnv - 1;
  928. RtlCopyMemory(p, lpszName, Length + 1);
  929. _strupr(p);
  930. p += Length;
  931. *p++ = '=';
  932. RtlCopyMemory(p, lpszValue, cchValue + 1);
  933. *(p + cchValue + 1) = '\0';
  934. pVDMEnvBlk->cchEnv += ExtraLength;
  935. pVDMEnvBlk->cchRemain -= ExtraLength;
  936. return TRUE;
  937. }
  938. return FALSE;
  939. }
  940. DWORD cmdExpandEnvironmentStrings(
  941. PVDMENVBLK pVDMEnvBlk,
  942. PCHAR lpszSrc,
  943. PCHAR lpszDst,
  944. DWORD cchDst
  945. )
  946. {
  947. DWORD RequiredLength, RemainLength, Length;
  948. PCHAR p1;
  949. RequiredLength = 0;
  950. RemainLength = (lpszDst) ? cchDst : 0;
  951. pVDMEnvBlk = (pVDMEnvBlk) ? pVDMEnvBlk : &cmdVDMEnvBlk;
  952. if (pVDMEnvBlk == NULL || lpszSrc == NULL)
  953. return 0;
  954. while(*lpszSrc) {
  955. if (*lpszSrc == '%') {
  956. p1 = strchr(lpszSrc + 1, '%');
  957. if (p1 != NULL) {
  958. if (p1 == lpszSrc + 1) { // a "%%"
  959. lpszSrc += 2;
  960. continue;
  961. }
  962. *p1 = '\0';
  963. Length = cmdGetEnvironmentVariable(pVDMEnvBlk,
  964. lpszSrc + 1,
  965. lpszDst,
  966. RemainLength
  967. );
  968. *p1 = '%';
  969. lpszSrc = p1 + 1;
  970. if (Length) {
  971. if (Length < RemainLength) {
  972. RemainLength -= Length;
  973. lpszDst += Length;
  974. }
  975. else {
  976. RemainLength = 0;
  977. Length --;
  978. }
  979. RequiredLength += Length;
  980. }
  981. continue;
  982. }
  983. else {
  984. RequiredLength++;
  985. if (RemainLength) {
  986. *lpszDst++ = *lpszSrc;
  987. RemainLength--;
  988. }
  989. lpszSrc++;
  990. continue;
  991. }
  992. }
  993. else {
  994. RequiredLength++;
  995. if (RemainLength) {
  996. *lpszDst++ = *lpszSrc;
  997. RemainLength--;
  998. }
  999. lpszSrc++;
  1000. }
  1001. } // while(*lpszSrc)
  1002. RequiredLength++;
  1003. if (RemainLength)
  1004. *lpszDst = '\0';
  1005. return RequiredLength;
  1006. }
  1007. DWORD cmdGetEnvironmentVariable(
  1008. PVDMENVBLK pVDMEnvBlk,
  1009. PCHAR lpszName,
  1010. PCHAR lpszValue,
  1011. DWORD cchValue
  1012. )
  1013. {
  1014. DWORD RequiredLength, Length;
  1015. PCHAR p, p1;
  1016. pVDMEnvBlk = (pVDMEnvBlk) ? pVDMEnvBlk : &cmdVDMEnvBlk;
  1017. if (pVDMEnvBlk == NULL || lpszName == NULL)
  1018. return 0;
  1019. RequiredLength = 0;
  1020. Length = strlen(lpszName);
  1021. // if the name is "windir", get its value from ntvdm process's environment
  1022. // for DOS because we took it out of the environment block the application
  1023. // will see.
  1024. if (Length == 6 && !fSeparateWow && !_strnicmp(lpszName, windir, 6)) {
  1025. return(GetEnvironmentVariableOem(lpszName, lpszValue, cchValue));
  1026. }
  1027. if (p = pVDMEnvBlk->lpszzEnv) {
  1028. while (*p && ((p1 = strchr(p, '=')) == NULL ||
  1029. (DWORD)(p1 - p) != Length ||
  1030. _strnicmp(lpszName, p, Length)))
  1031. p += strlen(p) + 1;
  1032. if (*p) {
  1033. RequiredLength = strlen(p1 + 1);
  1034. if (cchValue > RequiredLength && lpszValue)
  1035. RtlCopyMemory(lpszValue, p1 + 1, RequiredLength + 1);
  1036. else
  1037. RequiredLength++;
  1038. }
  1039. }
  1040. return RequiredLength;
  1041. }