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.

251 lines
6.8 KiB

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