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.

333 lines
9.9 KiB

  1. /*++
  2. Copyright (c) 1991-1993 Microsoft Corporation
  3. Module Name:
  4. ConfOpen.c
  5. Abstract:
  6. This module contains:
  7. NetpOpenConfigDataWithPath
  8. NetpOpenConfigDataWithPathEx
  9. Author:
  10. John Rogers (JohnRo) 02-Dec-1991
  11. Mike Swift (MikeSw) 30-Aug-1996
  12. Environment:
  13. Portable to any flat, 32-bit environment. (Uses Win32 typedefs.)
  14. Requires ANSI C extensions: slash-slash comments, long external names.
  15. Revision History:
  16. 02-Dec-1991 JohnRo
  17. Created this routine, to prepare for revised config handlers.
  18. (Actually, I swiped some of this code from RitaW.)
  19. 06-Jan-1992 JohnRo
  20. Added support for FAKE_PER_PROCESS_RW_CONFIG handling.
  21. 09-Jan-1992 JohnRo
  22. Try workaround for lib/linker problem with NetpIsRemote().
  23. 22-Mar-1992 JohnRo
  24. Added support for using the real Win32 registry.
  25. Added debug code to print the fake array.
  26. Fixed a UNICODE bug which PC-LINT caught.
  27. Fixed double _close of RTL config file.
  28. Fixed memory _access error in setting fake end of array.
  29. Use DBGSTATIC where applicable.
  30. 05-May-1992 JohnRo
  31. Reflect movement of keys to under System\CurrentControlSet\Services.
  32. 08-May-1992 JohnRo
  33. Use <prefix.h> equates.
  34. 21-May-1992 JohnRo
  35. RAID 9826: Match revised winreg error codes.
  36. 08-Jul-1992 JohnRo
  37. RAID 10503: srv mgr: repl dialog doesn't come up.
  38. Added more debug output to track down bad error code during logoff.
  39. 23-Jul-1992 JohnRo
  40. RAID 2274: repl svc should impersonate caller.
  41. 22-Sep-1992 JohnRo
  42. Avoid GP fault printing first part of winreg handle.
  43. 28-Oct-1992 JohnRo
  44. RAID 10136: NetConfig APIs don't work to remote NT server.
  45. 12-Apr-1993 JohnRo
  46. RAID 5483: server manager: wrong path given in repl dialog.
  47. 30-Aug-1996 MikeSw
  48. Copied from NetpOpenConfigData to allow configuring non-services
  49. --*/
  50. // These must be included first:
  51. #include <nt.h> // NT definitions
  52. #include <ntrtl.h> // NT Rtl structures
  53. #include <nturtl.h> // NT config Rtl routines
  54. #include <windows.h> // Needed by <configp.h> and <winreg.h>
  55. #include <lmcons.h> // LAN Manager common definitions
  56. #include <netdebug.h> // (Needed by config.h)
  57. // These may be included in any order:
  58. #include <config.h> // My prototype, LPNET_CONFIG_HANDLE.
  59. #include <configp.h> // NET_CONFIG_HANDLE, etc.
  60. #include <debuglib.h> // IF_DEBUG().
  61. #include <icanon.h> // NetpIsRemote(), etc.
  62. #include <lmerr.h> // LAN Manager network error definitions
  63. #include <netlib.h> // NetpMemoryAllocate(), etc.
  64. #include <netlibnt.h> // NetpNtStatusToApiStatus
  65. #include <prefix.h> // PREFIX_ equates.
  66. #include <tstring.h> // NetpAlloc{type}From{type}, STRICMP(), etc.
  67. #define DEFAULT_AREA TEXT("Parameters")
  68. #define DEFAULT_ROOT_KEY HKEY_LOCAL_MACHINE
  69. DBGSTATIC NET_API_STATUS
  70. NetpSetupConfigSection (
  71. IN NET_CONFIG_HANDLE * ConfigHandle,
  72. IN LPTSTR SectionName
  73. );
  74. NET_API_STATUS
  75. NetpOpenConfigDataWithPath(
  76. OUT LPNET_CONFIG_HANDLE *ConfigHandle,
  77. IN LPTSTR UncServerName OPTIONAL,
  78. IN LPTSTR SectionName,
  79. IN BOOL ReadOnly
  80. )
  81. /*++
  82. Routine Description:
  83. This function opens the system configuration file.
  84. Arguments:
  85. ConfigHandle - Points to a pointer which will be set to point to a
  86. net config handle for this section name. ConfigHandle will be set to
  87. NULL if any error occurs.
  88. SectionName - Points to the new (NT) section name to be opened.
  89. ReadOnly - Indicates whether all access through this net config handle is
  90. to be read only.
  91. Return Value:
  92. NET_API_STATUS - NO_ERROR or reason for failure.
  93. --*/
  94. {
  95. return ( NetpOpenConfigDataWithPathEx(
  96. ConfigHandle,
  97. UncServerName,
  98. SectionName, // Must be a SECT_NT_ name.
  99. DEFAULT_AREA,
  100. ReadOnly) );
  101. } // NetpOpenConfigData
  102. // NetpOpenConfigDataEx opens any area of a given service.
  103. NET_API_STATUS
  104. NetpOpenConfigDataWithPathEx(
  105. OUT LPNET_CONFIG_HANDLE *ConfigHandle,
  106. IN LPTSTR UncServerName OPTIONAL,
  107. IN LPTSTR SectionName, // Must be a SECT_NT_ name.
  108. IN LPTSTR AreaUnderSection OPTIONAL,
  109. IN BOOL ReadOnly
  110. )
  111. {
  112. NET_API_STATUS ApiStatus;
  113. DWORD LocalOrRemote; // Will be set to ISLOCAL or ISREMOTE.
  114. NET_CONFIG_HANDLE * MyHandle = NULL;
  115. LONG Error;
  116. HKEY RootKey = DEFAULT_ROOT_KEY;
  117. NetpAssert( ConfigHandle != NULL );
  118. *ConfigHandle = NULL; // Assume error until proven innocent.
  119. if ( (SectionName == NULL) || (*SectionName == TCHAR_EOS) ) {
  120. return (ERROR_INVALID_PARAMETER);
  121. }
  122. NetpAssert( (ReadOnly==TRUE) || (ReadOnly==FALSE) );
  123. if ( (UncServerName != NULL ) && ((*UncServerName) != TCHAR_EOS) ) {
  124. if( STRLEN(UncServerName) > MAX_PATH ) {
  125. return (ERROR_INVALID_PARAMETER);
  126. }
  127. //
  128. // Name was given. Canonicalize it and check if it's remote.
  129. //
  130. ApiStatus = NetpIsRemote(
  131. UncServerName, // input: uncanon name
  132. & LocalOrRemote, // output: local or remote flag
  133. NULL, // dont need output (canon name)
  134. 0, // length of canon name
  135. 0); // flags: normal
  136. IF_DEBUG(CONFIG) {
  137. NetpKdPrint(( PREFIX_NETLIB "NetpOpenConfigDataEx: canon status is "
  138. FORMAT_API_STATUS ", Lcl/rmt=" FORMAT_HEX_DWORD ".\n",
  139. ApiStatus, LocalOrRemote));
  140. }
  141. if (ApiStatus != NO_ERROR) {
  142. return (ApiStatus);
  143. }
  144. if (LocalOrRemote == ISREMOTE) {
  145. //
  146. // Explicit remote name given.
  147. //
  148. Error = RegConnectRegistry(
  149. UncServerName,
  150. DEFAULT_ROOT_KEY,
  151. & RootKey ); // result key
  152. if (Error != ERROR_SUCCESS) {
  153. NetpKdPrint(( PREFIX_NETLIB
  154. "NetpOpenConfigDataEx: RegConnectRegistry(machine '"
  155. FORMAT_LPTSTR "') ret error " FORMAT_LONG ".\n",
  156. UncServerName, Error ));
  157. return ((NET_API_STATUS) Error);
  158. }
  159. NetpAssert( RootKey != DEFAULT_ROOT_KEY );
  160. }
  161. }
  162. else {
  163. LocalOrRemote = ISLOCAL;
  164. }
  165. MyHandle = NetpMemoryAllocate( sizeof(NET_CONFIG_HANDLE) );
  166. if (MyHandle == NULL) {
  167. if (RootKey != DEFAULT_ROOT_KEY) {
  168. (VOID) RegCloseKey( RootKey );
  169. }
  170. return (ERROR_NOT_ENOUGH_MEMORY);
  171. }
  172. {
  173. LPTSTR AreaToUse = DEFAULT_AREA;
  174. DWORD DesiredAccess;
  175. DWORD SubKeySize;
  176. LPTSTR SubKeyString;
  177. HKEY SectionKey;
  178. #define LM_SUBKEY_UNDER_LOCAL_MACHINE \
  179. TEXT("System\\CurrentControlSet\\Services\\")
  180. if (AreaUnderSection != NULL) {
  181. if ((*AreaUnderSection) != TCHAR_EOS) {
  182. AreaToUse = AreaUnderSection;
  183. }
  184. }
  185. SubKeySize = ( STRLEN(SectionName)
  186. + 1 // backslash
  187. + STRLEN(AreaToUse)
  188. + 1 ) // trailing null
  189. * sizeof(TCHAR);
  190. SubKeyString = NetpMemoryAllocate( SubKeySize );
  191. if (SubKeyString == NULL) {
  192. if (MyHandle != NULL) {
  193. NetpMemoryFree( MyHandle );
  194. MyHandle = NULL;
  195. }
  196. if(RootKey != DEFAULT_ROOT_KEY)
  197. {
  198. RegCloseKey(RootKey);
  199. RootKey = DEFAULT_ROOT_KEY;
  200. }
  201. return (ERROR_NOT_ENOUGH_MEMORY);
  202. }
  203. (void) STRCPY( SubKeyString, SectionName );
  204. (void) STRCAT( SubKeyString, TEXT("\\") );
  205. (void) STRCAT( SubKeyString, AreaToUse );
  206. if ( ReadOnly ) {
  207. DesiredAccess = KEY_READ;
  208. } else {
  209. DesiredAccess = KEY_READ | KEY_WRITE;
  210. // DesiredAccess = KEY_ALL_ACCESS; // Everything but SYNCHRONIZE.
  211. }
  212. Error = RegOpenKeyEx (
  213. RootKey,
  214. SubKeyString,
  215. REG_OPTION_NON_VOLATILE,
  216. DesiredAccess,
  217. & SectionKey );
  218. IF_DEBUG(CONFIG) {
  219. NetpKdPrint(( PREFIX_NETLIB
  220. "NetpOpenConfigDataEx: RegOpenKeyEx(subkey '"
  221. FORMAT_LPTSTR "') ret " FORMAT_LONG ", win reg handle at "
  222. FORMAT_LPVOID " is " FORMAT_LPVOID ".\n",
  223. SubKeyString, Error, (LPVOID) &(MyHandle->WinRegKey),
  224. SectionKey ));
  225. }
  226. if (Error == ERROR_FILE_NOT_FOUND) {
  227. ApiStatus = NERR_CfgCompNotFound;
  228. // Code below will free MyHandle, etc., based on ApiStatus.
  229. } else if (Error != ERROR_SUCCESS) {
  230. ApiStatus = (NET_API_STATUS) Error;
  231. // Code below will free MyHandle, etc., based on ApiStatus.
  232. } else {
  233. ApiStatus = NO_ERROR;
  234. }
  235. NetpMemoryFree( SubKeyString );
  236. if (RootKey != DEFAULT_ROOT_KEY) {
  237. (VOID) RegCloseKey( RootKey );
  238. }
  239. MyHandle->WinRegKey = SectionKey;
  240. }
  241. if (ApiStatus != NO_ERROR) {
  242. NetpMemoryFree( MyHandle );
  243. MyHandle = NULL;
  244. }
  245. if (MyHandle != NULL) {
  246. if (LocalOrRemote == ISREMOTE) {
  247. (VOID) STRCPY(
  248. MyHandle->UncServerName, // dest
  249. UncServerName ); // src
  250. } else {
  251. MyHandle->UncServerName[0] = TCHAR_EOS;
  252. }
  253. }
  254. *ConfigHandle = MyHandle; // Points to private handle, or is NULL on err.
  255. return (ApiStatus);
  256. } // NetpOpenConfigDataEx