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.

221 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 1991-1993 Microsoft Corporation
  3. Module Name:
  4. ConfGet.c
  5. Abstract:
  6. This module contains helper routines to _read fields out of the NT
  7. configuration files. This is for temporary use until the Configuration
  8. Registry is available.
  9. Author:
  10. John Rogers (JohnRo) 27-Nov-1991
  11. Revision History:
  12. 27-Nov-1991 JohnRo
  13. Prepare for revised config handlers. (Actually swiped the NtRtl
  14. version of this code from RitaW.)
  15. 03-Dec-1991 JohnRo
  16. Fixed MIPS build problem.
  17. 11-Mar-1992 JohnRo
  18. Added some error checking on caller's args.
  19. Added support for using the real Win32 registry.
  20. Added support for FAKE_PER_PROCESS_RW_CONFIG handling.
  21. 07-May-1992 JohnRo
  22. REG_SZ now implies a UNICODE string, so we can't use REG_USZ anymore.
  23. 08-May-1992 JohnRo
  24. Use <prefix.h> equates.
  25. 10-May-1992 JohnRo
  26. Get winreg APIs to expand environment variables for us.
  27. 21-May-1992 JohnRo
  28. RAID 9826: Match revised winreg error codes.
  29. Got rid of a few compiler warnings under UNICODE.
  30. 29-May-1992 JohnRo
  31. RAID 10447: NetpGetConfigValue() passes *byte* values to
  32. ExpandEnvironmentStrings() (ChadS found the bug and the fix).
  33. 04-Jun-1992 JohnRo
  34. ExpandEnvironmentStrings() returns a character count as well.
  35. 08-Jun-1992 JohnRo
  36. Winreg title index parm is defunct.
  37. Made changes suggested by PC-LINT.
  38. 13-Apr-1993 JohnRo
  39. RAID 5483: server manager: wrong path given in repl dialog.
  40. --*/
  41. // These must be included first:
  42. #include <nt.h> // NT definitions
  43. #include <ntrtl.h> // NT Rtl structures
  44. #include <nturtl.h> // NT Rtl structures
  45. #include <windows.h> // Needed by <configp.h> and <winreg.h>
  46. #include <lmcons.h> // LAN Manager common definitions
  47. #include <netdebug.h> // (Needed by config.h)
  48. // These may be included in any order:
  49. #include <config.h> // My prototype, LPNET_CONFIG_HANDLE, etc.
  50. #include <configp.h> // NET_CONFIG_HANDLE.
  51. #include <debuglib.h> // IF_DEBUG()
  52. #include <lmerr.h> // LAN Manager network error definitions
  53. #include <netlib.h> // NetpMemoryAllocate(), etc.
  54. #include <netlibnt.h> // NetpAllocTStrFromString().
  55. #include <prefix.h> // PREFIX_ equates.
  56. #include <tstring.h> // NetpAllocWStrFromTStr(), TCHAR_EOS.
  57. NET_API_STATUS
  58. NetpGetConfigValue(
  59. IN LPNET_CONFIG_HANDLE ConfigHandle,
  60. IN LPTSTR lptstrKeyword,
  61. OUT LPTSTR * ValueBufferPtr // Must be freed by NetApiBufferFree().
  62. )
  63. /*++
  64. Routine Description:
  65. This function gets the keyword value string from the configuration file.
  66. Arguments:
  67. SectionPointer - Supplies the pointer to a specific section in the config
  68. file.
  69. lptstrKeyword - Supplies the string of the keyword within the specified
  70. section to look for.
  71. ValueBufferPtr - Returns the string of the keyword value which is
  72. copied into this buffer. This string will be allocated by this routine
  73. and must be freed by NetApiBufferFree(). Environment variables will
  74. be automagically expanded in this buffer.
  75. Return Value:
  76. NET_API_STATUS - NERR_Success or reason for failure.
  77. --*/
  78. {
  79. NET_API_STATUS ApiStatus;
  80. NET_CONFIG_HANDLE * MyHandle = ConfigHandle; // conv from opaque type
  81. LPTSTR lptstrValue;
  82. NetpAssert( ValueBufferPtr != NULL );
  83. *ValueBufferPtr = NULL; // assume error until proven otherwise.
  84. if ( (lptstrKeyword == NULL) || ((*lptstrKeyword) == TCHAR_EOS ) ) {
  85. return (ERROR_INVALID_PARAMETER);
  86. }
  87. {
  88. DWORD dwType;
  89. LONG Error;
  90. DWORD ValueSize;
  91. // Find out what max value length is.
  92. ApiStatus = NetpGetConfigMaxSizes (
  93. MyHandle,
  94. NULL, // Don't need max keyword size.
  95. & ValueSize); // Get max value length.
  96. if (ApiStatus != NO_ERROR) {
  97. NetpAssert( ApiStatus == NO_ERROR );
  98. return ApiStatus;
  99. }
  100. if (ValueSize == 0) {
  101. return (NERR_CfgParamNotFound);
  102. }
  103. // Alloc space for the value.
  104. lptstrValue = NetpMemoryAllocate( ValueSize );
  105. if (lptstrValue == NULL) {
  106. return (ERROR_NOT_ENOUGH_MEMORY);
  107. }
  108. //
  109. // Get the actual value.
  110. // (This does not expand REG_EXPAND_SZ for us; we'll do that below.)
  111. //
  112. Error = RegQueryValueEx(
  113. MyHandle->WinRegKey,
  114. lptstrKeyword,
  115. NULL, // reserved
  116. & dwType,
  117. (LPVOID) lptstrValue, // out: value string (TCHARs).
  118. & ValueSize
  119. );
  120. IF_DEBUG(CONFIG) {
  121. NetpKdPrint(( PREFIX_NETLIB "NetpGetConfigValue: RegQueryValueEx("
  122. FORMAT_LPTSTR ") returned " FORMAT_LONG ".\n",
  123. lptstrKeyword, Error ));
  124. }
  125. if (Error == ERROR_FILE_NOT_FOUND) {
  126. NetpMemoryFree( lptstrValue );
  127. return (NERR_CfgParamNotFound);
  128. } else if ( Error != ERROR_SUCCESS ) {
  129. NetpMemoryFree( lptstrValue );
  130. return ( (NET_API_STATUS) Error );
  131. } else if (dwType == REG_EXPAND_SZ) {
  132. LPTSTR UnexpandedString = lptstrValue;
  133. LPTSTR ExpandedString = NULL;
  134. // Expand string, using remote environment if necessary.
  135. ApiStatus = NetpExpandConfigString(
  136. MyHandle->UncServerName, // server name (or null char)
  137. UnexpandedString,
  138. &ExpandedString ); // expanded: alloc and set ptr
  139. IF_DEBUG( CONFIG ) {
  140. NetpKdPrint(( PREFIX_NETLIB
  141. "NetpGetConfigValue: done expanding '"
  142. FORMAT_LPTSTR "' to '" FORMAT_LPTSTR "'.\n",
  143. UnexpandedString, ExpandedString ));
  144. }
  145. if (ApiStatus != NO_ERROR) {
  146. NetpKdPrint(( PREFIX_NETLIB
  147. "NetpGetConfigValue: ERROR EXPANDING '"
  148. FORMAT_LPTSTR "' to '" FORMAT_LPTSTR "', API status="
  149. FORMAT_API_STATUS ".\n",
  150. UnexpandedString, ExpandedString, ApiStatus ));
  151. NetpMemoryFree( lptstrValue );
  152. if (ExpandedString != NULL) {
  153. NetpMemoryFree( ExpandedString );
  154. }
  155. return (ApiStatus);
  156. }
  157. NetpMemoryFree( UnexpandedString );
  158. lptstrValue = ExpandedString;
  159. } else if (dwType != REG_SZ) {
  160. NetpMemoryFree( lptstrValue );
  161. IF_DEBUG(CONFIG) {
  162. NetpKdPrint(( PREFIX_NETLIB
  163. "NetpGetConfigValue: read unexpected reg type "
  164. FORMAT_DWORD ":\n", dwType ));
  165. NetpDbgHexDump( (LPVOID) lptstrValue, ValueSize );
  166. }
  167. return (ERROR_INVALID_DATA);
  168. }
  169. }
  170. NetpAssert( lptstrValue != NULL );
  171. IF_DEBUG(CONFIG) {
  172. NetpKdPrint(( PREFIX_NETLIB "NetpGetConfigValue: value for '"
  173. FORMAT_LPTSTR "' is '" FORMAT_LPTSTR "'.\n",
  174. lptstrKeyword, lptstrValue));
  175. }
  176. *ValueBufferPtr = lptstrValue;
  177. return NERR_Success;
  178. }