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.

337 lines
6.7 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. /*++
  4. Copyright (c) 1990 Microsoft Corporation
  5. Module Name:
  6. nt_env.c
  7. Abstract:
  8. 1. Contains routines to get and set NVRAM variables.
  9. Author:
  10. Sunil Pai (sunilp) 20-Nov-1991
  11. --*/
  12. #define MAX_ENV_VAR_LEN 4096
  13. // Detect Command: (transfer to detect1.c)
  14. //
  15. // Get value of a MIPS environment variable. Returns the value in list form.
  16. // If the value is a path (semicolon-separated components) each component
  17. // is an element of the list.
  18. //
  19. CB
  20. GetNVRAMVar(
  21. IN RGSZ Args,
  22. IN USHORT cArgs,
  23. OUT SZ ReturnBuffer,
  24. IN CB cbReturnBuffer
  25. )
  26. {
  27. CHAR EnvValue[ MAX_ENV_VAR_LEN ];
  28. SZ sz;
  29. Unused( cbReturnBuffer );
  30. ReturnBuffer[0] = '\0';
  31. if (cArgs > 0) {
  32. if ( !GetEnvironmentString( Args[0], EnvValue, MAX_ENV_VAR_LEN ) ) {
  33. //
  34. // Env. Variable not defined, return empty list
  35. //
  36. #define UNDEF_VAR_VALUE "{}"
  37. lstrcpy( ReturnBuffer, UNDEF_VAR_VALUE );
  38. return lstrlen( ReturnBuffer )+1;
  39. } else if ( sz = SzListValueFromPath( EnvValue ) ) {
  40. lstrcpy( ReturnBuffer, sz );
  41. SFree( sz );
  42. return lstrlen( ReturnBuffer)+1;
  43. }
  44. }
  45. return 0;
  46. }
  47. BOOL
  48. GetEnvironmentString(
  49. IN LPSTR lpVar,
  50. OUT LPSTR lpValue,
  51. IN USHORT MaxLengthValue
  52. )
  53. {
  54. PWSTR wBuffer;
  55. USHORT ReturnLength;
  56. UNICODE_STRING VarString_U, ValueString_U;
  57. ANSI_STRING VarString_A, ValueString_A;
  58. NTSTATUS Status;
  59. LONG Privilege = SE_SYSTEM_ENVIRONMENT_PRIVILEGE;
  60. TOKEN_PRIVILEGES PrevState;
  61. ULONG PrevLength = sizeof( TOKEN_PRIVILEGES );
  62. //
  63. // Initialise the wide char buffer which will receive the env value
  64. //
  65. if ((wBuffer = SAlloc((MAX_ENV_VAR_LEN) * sizeof(WCHAR))) == NULL) {
  66. SetErrorText(IDS_ERROR_RTLOOM);
  67. return FALSE;
  68. }
  69. //
  70. // get the environment variable
  71. //
  72. RtlInitAnsiString(&VarString_A, lpVar);
  73. Status = RtlAnsiStringToUnicodeString(
  74. &VarString_U,
  75. &VarString_A,
  76. TRUE
  77. );
  78. if(!NT_SUCCESS(Status)) {
  79. SetErrorText(IDS_ERROR_RTLOOM);
  80. SFree(wBuffer);
  81. return(FALSE);
  82. }
  83. //
  84. // Enable the SE_SYSTEM_ENVIRONMENT_PRIVILEGE, if this fails we
  85. // cannot query the environment string
  86. //
  87. if( !AdjustPrivilege(
  88. Privilege,
  89. ENABLE_PRIVILEGE,
  90. &PrevState,
  91. &PrevLength
  92. )
  93. ) {
  94. SFree( wBuffer );
  95. return( FALSE );
  96. }
  97. //
  98. // Query the system environment value
  99. //
  100. Status = NtQuerySystemEnvironmentValue(
  101. &VarString_U,
  102. wBuffer,
  103. MAX_ENV_VAR_LEN * sizeof(WCHAR),
  104. &ReturnLength
  105. );
  106. //
  107. // Restore the SE_SYSTEM_ENVIRONMENT_PRIVILEGE to its previous state
  108. //
  109. RestorePrivilege( &PrevState );
  110. //
  111. // Examine the query system environment value operation
  112. if(!NT_SUCCESS(Status)) {
  113. //
  114. // first free the resources involved
  115. //
  116. SFree(wBuffer);
  117. RtlFreeUnicodeString(&VarString_U);
  118. //
  119. // special handling for var not found
  120. //
  121. if (Status == STATUS_UNSUCCESSFUL) {
  122. lpValue[0] = 0; //Null terminate
  123. return(TRUE);
  124. }
  125. else {
  126. SetErrorText(IDS_ERROR_ENVVARREAD);
  127. return(FALSE);
  128. }
  129. }
  130. //
  131. // Free the Unicode var string, no longer needed
  132. //
  133. RtlFreeUnicodeString(&VarString_U);
  134. //
  135. // Convert the value to an Ansi string
  136. //
  137. RtlInitUnicodeString(&ValueString_U, wBuffer);
  138. Status = RtlUnicodeStringToAnsiString(
  139. &ValueString_A,
  140. &ValueString_U,
  141. TRUE
  142. );
  143. if (!NT_SUCCESS(Status)) {
  144. SetErrorText(IDS_ERROR_RTLOOM);
  145. SFree(wBuffer);
  146. return (FALSE);
  147. }
  148. //
  149. // Move it to the buffer passed in
  150. //
  151. if (ValueString_A.Length >= MaxLengthValue) {
  152. SetErrorText(IDS_ERROR_ENVVAROVF);
  153. RtlFreeAnsiString(&ValueString_A);
  154. SFree(wBuffer);
  155. return FALSE;
  156. }
  157. //
  158. // move the ansi string to the buffer passed in
  159. //
  160. RtlMoveMemory(lpValue, ValueString_A.Buffer, ValueString_A.Length);
  161. lpValue[ValueString_A.Length]=0; //Null terminate
  162. //
  163. // free the value ansi string
  164. //
  165. RtlFreeAnsiString(&ValueString_A);
  166. //
  167. // return success
  168. //
  169. return TRUE;
  170. }
  171. BOOL
  172. SetEnvironmentString(
  173. IN LPSTR lpVar,
  174. IN LPSTR lpValue
  175. )
  176. {
  177. UNICODE_STRING VarString_U, ValueString_U;
  178. ANSI_STRING VarString_A, ValueString_A;
  179. NTSTATUS Status;
  180. LONG Privilege = SE_SYSTEM_ENVIRONMENT_PRIVILEGE;
  181. TOKEN_PRIVILEGES PrevState;
  182. ULONG PrevLength = sizeof( TOKEN_PRIVILEGES );
  183. //
  184. // Initialise the unicode strings for the environment variable
  185. //
  186. RtlInitAnsiString(&VarString_A, lpVar);
  187. Status = RtlAnsiStringToUnicodeString(
  188. &VarString_U,
  189. &VarString_A,
  190. TRUE
  191. );
  192. if(!NT_SUCCESS(Status)) {
  193. SetErrorText(IDS_ERROR_RTLOOM);
  194. return(FALSE);
  195. }
  196. //
  197. // Initialise the unicode string for the environment value
  198. //
  199. RtlInitAnsiString(&ValueString_A, lpValue);
  200. Status = RtlAnsiStringToUnicodeString(
  201. &ValueString_U,
  202. &ValueString_A,
  203. TRUE
  204. );
  205. if(!NT_SUCCESS(Status)) {
  206. SetErrorText(IDS_ERROR_RTLOOM);
  207. RtlFreeUnicodeString(&VarString_U);
  208. return(FALSE);
  209. }
  210. //
  211. // Enable the SE_SYSTEM_ENVIRONMENT_PRIVILEGE, if this fails we
  212. // cannot query the environment string
  213. //
  214. if( !AdjustPrivilege(
  215. Privilege,
  216. ENABLE_PRIVILEGE,
  217. &PrevState,
  218. &PrevLength
  219. )
  220. ) {
  221. SetErrorText(IDS_ERROR_PRIVILEGE);
  222. RtlFreeUnicodeString(&VarString_U);
  223. return( FALSE );
  224. }
  225. //
  226. // call the NT Function to set the environment variable
  227. //
  228. Status = NtSetSystemEnvironmentValue(
  229. &VarString_U,
  230. &ValueString_U
  231. );
  232. //
  233. // Restore the SE_SYSTEM_ENVIRONMENT_PRIVILEGE to its previous state
  234. //
  235. RestorePrivilege( &PrevState );
  236. //
  237. // free the two unicode strings
  238. //
  239. RtlFreeUnicodeString(&VarString_U);
  240. RtlFreeUnicodeString(&ValueString_U);
  241. //
  242. // return Success/Failure
  243. //
  244. if (!NT_SUCCESS(Status)) {
  245. SetErrorText(IDS_ERROR_ENVVARWRITE);
  246. return ( FALSE );
  247. }
  248. return ( TRUE );
  249. }