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.

267 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. ConfGetD.c
  5. Abstract:
  6. This module contains NetpGetConfigDword().
  7. Author:
  8. John Rogers (JohnRo) 24-Jan-1992
  9. Revision History:
  10. 24-Jan-1992 JohnRo
  11. Created.
  12. 14-Feb-1992 JohnRo
  13. Fixed bug handling default.
  14. 08-May-1992 JohnRo
  15. Allow DWORD to be in win32 registry as REG_SZ or REG_DWORD.
  16. 21-May-1992 JohnRo
  17. RAID 9826: Match revised winreg error codes.
  18. Use PREFIX_ equates.
  19. 09-Jun-1992 JohnRo
  20. RAID 11582: Winreg title index parm is defunct.
  21. RAID 11784: repl svc doesn't default some config parms anymore.
  22. 13-Jun-1992 JohnRo
  23. Net config helpers should allow empty section.
  24. --*/
  25. // These must be included first:
  26. #include <nt.h> // NT definitions
  27. #include <ntrtl.h> // NT Rtl structures
  28. #include <nturtl.h> // NT Rtl structures
  29. #include <windows.h> // Win32 type definitions
  30. #include <lmcons.h> // NET_API_STATUS.
  31. #include <netdebug.h> // (Needed by config.h)
  32. // These may be included in any order:
  33. #include <config.h> // My prototype, LPNET_CONFIG_HANDLE.
  34. #include <configp.h> // USE_WIN32_CONFIG (if defined).
  35. #include <debuglib.h> // IF_DEBUG()
  36. #include <lmapibuf.h> // NetApiBufferFree().
  37. #include <lmerr.h> // NERR_, ERROR_, NO_ERROR equates.
  38. #include <netlib.h> // NetpMemoryAllocate(), etc.
  39. #include <prefix.h> // PREFIX_ equates.
  40. #include <stddef.h> // ptrdiff_t
  41. #include <tstr.h> // ATOL(), STRLEN(), STRSPN().
  42. // Get an unsigned numeric value. Return ERROR_INVALID_DATA if value isn't
  43. // numeric.
  44. NET_API_STATUS
  45. NetpGetConfigDword(
  46. IN LPNET_CONFIG_HANDLE ConfigHandle,
  47. IN LPTSTR KeyWanted,
  48. IN DWORD DefaultValue,
  49. OUT LPDWORD ValueBuffer
  50. )
  51. /*++
  52. Routine Description:
  53. This function gets the keyword value from the configuration file.
  54. This value is converted from a string to a DWORD (unsigned).
  55. Arguments:
  56. SectionPointer - Supplies the pointer to a specific section in the config
  57. file.
  58. KeyWanted - Supplies the string of the keyword within the specified
  59. section to look for.
  60. DefaultValue - Gives a default value to use if KeyWanted isn't present
  61. in the section or if KeyWanted exists but no value is in the config
  62. data.
  63. ValueBuffer - Returns the numeric value of the keyword.
  64. Return Value:
  65. NET_API_STATUS - NERR_Success, ERROR_INVALID_DATA (if string wasn't a
  66. valid unsigned number), or other error code.
  67. --*/
  68. {
  69. NET_API_STATUS ApiStatus;
  70. DWORD CharCount; // Count of TCHARs, not including null.
  71. DWORD TempDword = DefaultValue;
  72. LPTSTR ValueString;
  73. //
  74. // Error check the caller somewhat.
  75. //
  76. if (ValueBuffer == NULL) {
  77. return (ERROR_INVALID_PARAMETER);
  78. }
  79. //
  80. // Check for GP fault and set default value.
  81. //
  82. *ValueBuffer = DefaultValue;
  83. if (ConfigHandle == NULL) {
  84. return (ERROR_INVALID_PARAMETER);
  85. }
  86. {
  87. DWORD dwType;
  88. LONG Error;
  89. NET_CONFIG_HANDLE * MyHandle = ConfigHandle; // conv from opaque type
  90. DWORD ValueLength;
  91. // Find out what max value length is.
  92. ApiStatus = NetpGetConfigMaxSizes (
  93. MyHandle,
  94. NULL, // Don't need max keyword size.
  95. & ValueLength); // Get max value length.
  96. if (ApiStatus != NO_ERROR) {
  97. return (ApiStatus);
  98. }
  99. // Handle empty section.
  100. if (ValueLength == 0 ) {
  101. return (NO_ERROR); // Default already set, so we're done. Ok!
  102. }
  103. // Alloc space for the value.
  104. ValueString = NetpMemoryAllocate( ValueLength );
  105. if (ValueString == NULL) {
  106. return (ERROR_NOT_ENOUGH_MEMORY);
  107. }
  108. // Get the actual value.
  109. Error = RegQueryValueEx (
  110. MyHandle->WinRegKey,
  111. KeyWanted,
  112. NULL, // reserved
  113. & dwType,
  114. (LPVOID) ValueString, // out: value string (TCHARs).
  115. & ValueLength
  116. );
  117. IF_DEBUG(CONFIG) {
  118. NetpKdPrint(( PREFIX_NETLIB "NetpGetConfigDword: RegQueryValueEx("
  119. FORMAT_LPTSTR ") returned " FORMAT_LONG ".\n",
  120. KeyWanted, Error ));
  121. }
  122. if (Error == ERROR_FILE_NOT_FOUND) {
  123. NetpMemoryFree( ValueString );
  124. return (NO_ERROR); // Default already set, so we're done. Ok!
  125. } else if ( Error != ERROR_SUCCESS ) {
  126. NetpMemoryFree( ValueString );
  127. return (Error);
  128. }
  129. NetpAssert( ValueString != NULL );
  130. if (dwType == REG_SZ) {
  131. goto ParseString; // Type is good, go parse the string.
  132. } else if (dwType == REG_DWORD) {
  133. NetpAssert( ValueLength == sizeof(DWORD) );
  134. TempDword = * (LPDWORD) (LPVOID) ValueString;
  135. goto GotValue;
  136. } else {
  137. NetpMemoryFree( ValueString );
  138. IF_DEBUG(CONFIG) {
  139. NetpKdPrint(( PREFIX_NETLIB
  140. "NetpGetConfigDword: read unexpected reg type "
  141. FORMAT_DWORD ".\n", dwType ));
  142. }
  143. return (ERROR_INVALID_DATA);
  144. }
  145. }
  146. NetpAssert( ValueString != NULL );
  147. ParseString:
  148. //
  149. // Do some error checking on the value string.
  150. //
  151. CharCount = STRLEN( ValueString );
  152. if ( CharCount == 0 ) {
  153. //
  154. // If "key=" line exists (but no value), return default and say
  155. // NO_ERROR.
  156. //
  157. ApiStatus = NO_ERROR;
  158. } else if ( _wcsnicmp( ValueString, L"0x", 2 ) == 0 ) {
  159. LPWSTR end;
  160. TempDword = wcstoul( ValueString, &end, 16 );
  161. if ( end - ValueString == (ptrdiff_t) CharCount ) {
  162. ApiStatus = NO_ERROR;
  163. } else {
  164. NetpKdPrint(( PREFIX_NETLIB
  165. "NetpGetConfigDword: invalid string for keyword "
  166. FORMAT_LPTSTR " is '" FORMAT_LPTSTR "'...\n",
  167. KeyWanted, ValueString ));
  168. ApiStatus = ERROR_INVALID_DATA;
  169. }
  170. } else if ( STRSPN( ValueString, TEXT("0123456789") ) != CharCount ) {
  171. NetpKdPrint(( PREFIX_NETLIB
  172. "NetpGetConfigDword: invalid string for keyword "
  173. FORMAT_LPTSTR " is '" FORMAT_LPTSTR "'...\n",
  174. KeyWanted, ValueString ));
  175. ApiStatus = ERROR_INVALID_DATA;
  176. } else {
  177. //
  178. // Convert the string to a numeric value.
  179. //
  180. TempDword = (DWORD) ATOL( ValueString );
  181. ApiStatus = NO_ERROR;
  182. }
  183. IF_DEBUG(CONFIG) {
  184. NetpKdPrint(( PREFIX_NETLIB "NetpGetConfigDword: string for "
  185. FORMAT_LPTSTR " is '" FORMAT_LPTSTR "'...\n",
  186. KeyWanted, ValueString));
  187. }
  188. GotValue:
  189. IF_DEBUG(CONFIG) {
  190. NetpKdPrint(( PREFIX_NETLIB "NetpGetConfigDword: numeric value is "
  191. FORMAT_DWORD ",\n", TempDword ));
  192. NetpKdPrint((" returning ApiStatus " FORMAT_API_STATUS ".\n",
  193. ApiStatus ));
  194. }
  195. (void) NetApiBufferFree( ValueString );
  196. //
  197. // Tell caller how things went.
  198. //
  199. * ValueBuffer = TempDword;
  200. return (ApiStatus);
  201. }