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.

1948 lines
51 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. MPRREG.CXX
  5. Abstract:
  6. Contains functions used by MPR to manipulate the registry.
  7. MprOpenKey
  8. MprGetKeyValue
  9. MprEnumKey
  10. MprGetKeyInfo
  11. MprFindDriveInRegistry
  12. I_MprSaveConn
  13. MprSaveDeferFlags
  14. MprSetRegValue
  15. MprCreateRegKey
  16. MprReadConnectionInfo
  17. MprForgetRedirConnection
  18. MprGetRemoteName
  19. QUESTIONS:
  20. 1) Do I need to call RegFlushKey after creating a new key?
  21. Author:
  22. Dan Lafferty (danl) 12-Dec-1991
  23. Environment:
  24. User Mode - Win32
  25. Revision History:
  26. 21-Feb-1997 AnirudhS
  27. Change MprRememberConnection to I_MprSaveConn and MprSaveDeferFlags
  28. for use by setup and by DEFER_UNKNOWN.
  29. 12-Jun-1996 AnirudhS
  30. Got rid of the REMOVE_COLON/RESTORE_COLON scheme for converting
  31. device names to registry key names, since it caused writes to
  32. read-only input parameters.
  33. 08-Mar-1996 AnirudhS
  34. Save the provider type, not the provider name, for persistent
  35. connections. Fix old heap corruption bugs that show up when the
  36. user profile contains incomplete info.
  37. 16-Jun-1995 AnirudhS
  38. Returned DWORDs rather than BOOLs from some functions; changed some
  39. formal parameters from LPWSTR to LPCWSTR.
  40. 24-Nov-1992 Danl
  41. Fixed compiler warnings by always using HKEY rather than HANDLE.
  42. 03-Sept-1992 Danl
  43. MprGetRemoteName: Changed ERROR_BUFFER_OVERFLOW to WN_MORE_DATA.
  44. 12-Dec-1991 danl
  45. Created
  46. --*/
  47. //
  48. // Includes
  49. //
  50. #include "precomp.hxx"
  51. #include <malloc.h> // _alloca
  52. #include <tstring.h> // MEMCPY
  53. #include <debugfmt.h> // FORMAT_LPTSTR
  54. #include <wincred.h> // CRED_MAX_USERNAME_LENGTH
  55. //
  56. // Macros
  57. //
  58. //
  59. // STACK_ALLOC
  60. //
  61. // Allocates space on the stack for a copy of an input string. The result
  62. // could be NULL if the string is too long to be copied on the stack.
  63. //
  64. #define STACK_ALLOC(str) ((LPWSTR) _alloca((wcslen(str)+1)*sizeof(WCHAR)))
  65. VOID
  66. RemoveColon(
  67. LPWSTR pszCopy,
  68. LPCWSTR pszSource
  69. )
  70. /*++
  71. Routine Description:
  72. This function makes a copy of a string and searches through the copy
  73. for a colon. If a colon is found, it is replaced by a '\0'.
  74. Arguments:
  75. pszCopy - Pointer to the space for the copy.
  76. pszSource - Pointer to the source string.
  77. Return Value:
  78. None.
  79. --*/
  80. {
  81. wcscpy(pszCopy, pszSource);
  82. WCHAR * pColon = wcschr(pszCopy, L':');
  83. if (pColon != NULL)
  84. {
  85. *pColon = L'\0';
  86. }
  87. }
  88. BOOL
  89. MprOpenKey(
  90. IN HKEY hKey,
  91. IN LPTSTR lpSubKey,
  92. OUT PHKEY phKeyHandle,
  93. IN DWORD desiredAccess
  94. )
  95. /*++
  96. Routine Description:
  97. This function opens a handle to a key inside the registry. The major
  98. handle and the path to the subkey are required as input.
  99. Arguments:
  100. hKey - This is one of the well-known root key handles for the portion
  101. of the registry of interest.
  102. lpSubKey - A pointer a string containing the path to the subkey.
  103. phKeyHandle - A pointer to the location where the handle to the subkey
  104. is to be placed.
  105. desiredAccess - Desired Access (Either KEY_READ or KEY_WRITE or both).
  106. Return Value:
  107. TRUE - The operation was successful
  108. FALSE - The operation was not successful.
  109. --*/
  110. {
  111. DWORD status;
  112. REGSAM samDesired = KEY_READ;
  113. HKEY HKCU ;
  114. if(desiredAccess & DA_WRITE) {
  115. samDesired = KEY_READ | KEY_WRITE;
  116. }
  117. else if (desiredAccess & DA_DELETE) {
  118. samDesired = DELETE;
  119. }
  120. HKCU = NULL ;
  121. if ( hKey == HKEY_CURRENT_USER ) {
  122. status = RegOpenCurrentUser(
  123. MAXIMUM_ALLOWED,
  124. &HKCU );
  125. if ( status != 0 )
  126. {
  127. return FALSE ;
  128. }
  129. hKey = HKCU ;
  130. }
  131. status = RegOpenKeyEx(
  132. hKey, // hKey
  133. lpSubKey, // lpSubKey
  134. 0L, // ulOptions (reserved)
  135. samDesired, // desired access security mask
  136. phKeyHandle); // Newly Opened Key Handle
  137. if ( HKCU )
  138. {
  139. RegCloseKey( HKCU );
  140. }
  141. if (status != NO_ERROR) {
  142. MPR_LOG3(ERROR,"MprOpenKey: RegOpenKeyEx(%#lx \"%ws\") failed %d\n",
  143. hKey, lpSubKey, status);
  144. return (FALSE);
  145. }
  146. return(TRUE);
  147. }
  148. BOOL
  149. MprGetKeyValue(
  150. IN HKEY KeyHandle,
  151. IN LPTSTR ValueName,
  152. OUT LPTSTR *ValueString
  153. )
  154. /*++
  155. Routine Description:
  156. This function takes a key handle and a value name, and returns a value
  157. string that is associated with that name.
  158. NOTE: The pointer to the ValueString is allocated by this function.
  159. Arguments:
  160. KeyHandle - This is a handle for the registry key that contains the value.
  161. ValueName - A pointer to a string that identifies the value being
  162. obtained.
  163. ValueString - A pointer to a location that upon exit will contain the
  164. pointer to the returned value.
  165. Return Value:
  166. TRUE - Success
  167. FALSE - A fatal error occured.
  168. --*/
  169. {
  170. DWORD status;
  171. DWORD maxValueLen;
  172. TCHAR Temp[1];
  173. LPTSTR TempValue;
  174. DWORD ValueType;
  175. DWORD NumRequired;
  176. DWORD CharsReturned;
  177. //
  178. // Find the buffer size requirement for the value.
  179. //
  180. status = RegQueryValueEx(
  181. KeyHandle, // hKey
  182. ValueName, // lpValueName
  183. NULL, // lpTitleIndex
  184. &ValueType, // lpType
  185. NULL, // lpData
  186. &maxValueLen); // lpcbData
  187. if (status != NO_ERROR) {
  188. MPR_LOG2(ERROR,"MprGetKeyValue:RegQueryValueEx(\"%ws\") failed %d\n",
  189. ValueName, status);
  190. *ValueString = NULL;
  191. return(FALSE);
  192. }
  193. //
  194. // Allocate buffer to receive the value string.
  195. //
  196. maxValueLen += sizeof(TCHAR);
  197. TempValue = (LPTSTR) LocalAlloc(LMEM_FIXED, maxValueLen);
  198. if(TempValue == NULL) {
  199. MPR_LOG(ERROR,"MprGetKeyValue:LocalAlloc failed\n", 0);
  200. *ValueString = NULL;
  201. return(FALSE);
  202. }
  203. //
  204. // Read the value.
  205. //
  206. status = RegQueryValueEx(
  207. KeyHandle, // hKey
  208. ValueName, // lpValueName
  209. NULL, // lpTitleIndex
  210. &ValueType, // lpType
  211. (LPBYTE)TempValue, // lpData
  212. &maxValueLen); // lpcbData
  213. if (status != NO_ERROR) {
  214. MPR_LOG2(ERROR,"MprGetKeyValue:RegQueryValueEx(\"%ws\") failed %d\n",
  215. ValueName, status);
  216. LocalFree(TempValue);
  217. *ValueString = NULL;
  218. return(FALSE);
  219. }
  220. //
  221. // Make sure the value is null-terminated. Strings obtained from
  222. // the registry may or may not be null-terminated.
  223. //
  224. TempValue [ maxValueLen / sizeof(TCHAR) ] = 0;
  225. //========================================================
  226. //
  227. // If the value is of REG_EXPAND_SZ type, then expand it.
  228. //
  229. //========================================================
  230. if (ValueType != REG_EXPAND_SZ) {
  231. *ValueString = TempValue;
  232. return(TRUE);
  233. }
  234. //
  235. // If the ValueType is REG_EXPAND_SZ, then we must call the
  236. // function to expand environment variables.
  237. //
  238. MPR_LOG(TRACE,"MprGetKeyValue: Must expand the string for "
  239. FORMAT_LPTSTR "\n", ValueName);
  240. //
  241. // Make the first call just to get the number of characters that
  242. // will be returned.
  243. //
  244. NumRequired = ExpandEnvironmentStrings (TempValue,Temp, 1);
  245. if (NumRequired > 1) {
  246. *ValueString = (LPTSTR) LocalAlloc(LPTR, (NumRequired+1)*sizeof(TCHAR));
  247. if (*ValueString == NULL) {
  248. MPR_LOG(ERROR, "MprGetKeyValue: LocalAlloc of numChar= "
  249. FORMAT_DWORD " failed \n",NumRequired );
  250. (void) LocalFree(TempValue);
  251. return(FALSE);
  252. }
  253. CharsReturned = ExpandEnvironmentStrings (
  254. TempValue,
  255. *ValueString,
  256. NumRequired);
  257. (void) LocalFree(TempValue);
  258. if (CharsReturned > NumRequired || CharsReturned == 0) {
  259. MPR_LOG(ERROR, "MprGetKeyValue: ExpandEnvironmentStrings "
  260. " failed for " FORMAT_LPTSTR " \n", ValueName);
  261. (void) LocalFree(*ValueString);
  262. *ValueString = NULL;
  263. return(FALSE);
  264. }
  265. //
  266. // Now insert the NUL terminator.
  267. //
  268. (*ValueString)[CharsReturned] = 0;
  269. }
  270. else {
  271. //
  272. // This call should have failed because of our ridiculously small
  273. // buffer size.
  274. //
  275. MPR_LOG(ERROR, "MprGetKeyValue: ExpandEnvironmentStrings "
  276. " Should have failed because we gave it a BufferSize=1\n",0);
  277. //
  278. // This could happen if the string was a single character long and
  279. // didn't really have any environment values to expand. In this
  280. // case, we return the TempValue buffer pointer.
  281. //
  282. *ValueString = TempValue;
  283. }
  284. return(TRUE);
  285. }
  286. BOOL
  287. MprGetKeyDwordValue(
  288. IN HKEY KeyHandle,
  289. IN LPCWSTR ValueName,
  290. OUT DWORD * Value
  291. )
  292. /*++
  293. Routine Description:
  294. This function takes a key handle and a value name, and returns a DWORD
  295. value that is associated with that name.
  296. Arguments:
  297. KeyHandle - This is a handle for the registry key that contains the value.
  298. ValueName - A pointer to a string that identifies the value being
  299. obtained. If this value does not have REG_DWORD type the function
  300. returns FALSE.
  301. Value - A pointer to a location that upon exit will contain the returned
  302. DWORD value.
  303. Return Value:
  304. TRUE - Success
  305. FALSE - A fatal error occured.
  306. --*/
  307. {
  308. DWORD dwSize = sizeof(DWORD);
  309. DWORD dwType;
  310. DWORD status = RegQueryValueEx(
  311. KeyHandle,
  312. ValueName,
  313. 0, // reserved
  314. &dwType, // type
  315. (LPBYTE) Value,
  316. &dwSize);
  317. if (status)
  318. {
  319. MPR_LOG2(ERROR,"MprGetKeyDwordValue: RegQueryValueEx(\"%ws\") failed %ld\n",
  320. ValueName, status);
  321. return FALSE;
  322. }
  323. else if (dwType != REG_DWORD || dwSize != sizeof(DWORD))
  324. {
  325. MPR_LOG3(ERROR,"MprGetKeyDwordValue: RegQueryValueEx(\"%ws\") returned "
  326. "type %ld, size %ld\n", ValueName, dwType, dwSize);
  327. return FALSE;
  328. }
  329. return TRUE;
  330. }
  331. LONG
  332. MprGetKeyNumberValue(
  333. IN HKEY KeyHandle,
  334. IN LPCWSTR ValueName,
  335. IN LONG Default
  336. )
  337. /*++
  338. Routine Description:
  339. This function takes a key handle and a value name, and returns a numeric
  340. value that is associated with that name. If an error occurs while
  341. retrieving the value, the specified Default value is returned.
  342. For compatibility, the behavior of this function is exactly the same as
  343. Win95's RegEntry::GetNumber function. The value is assumed to be a 4-byte
  344. type, such as REG_BINARY or REG_DWORD. If this is not the case, the
  345. function does exactly the same as Win95.
  346. Arguments:
  347. KeyHandle - This is a handle for the registry key that contains the value.
  348. ValueName - A pointer to a string that identifies the value being
  349. obtained.
  350. Default - Value to return if one could not be obtained from the registry.
  351. Return Value:
  352. Value retrieved from the registry, or default if an error occurs.
  353. --*/
  354. {
  355. LONG dwNumber;
  356. DWORD dwSize = sizeof(dwNumber);
  357. DWORD error = RegQueryValueEx(
  358. KeyHandle,
  359. ValueName,
  360. 0, // reserved
  361. NULL, // type
  362. (LPBYTE) &dwNumber,
  363. &dwSize);
  364. if (error)
  365. dwNumber = Default;
  366. return dwNumber;
  367. }
  368. BOOL
  369. MprGetKeyStringValue(
  370. IN HKEY KeyHandle,
  371. IN LPCWSTR ValueName,
  372. IN DWORD cchMaxValueLength,
  373. OUT LPWSTR *Value
  374. )
  375. /*++
  376. Routine Description:
  377. This function takes a key handle, a value name, and a max size and allocates/returns
  378. a string value that is associated with that name.
  379. Arguments:
  380. KeyHandle - This is a handle for the registry key that contains the value.
  381. ValueName - A pointer to a string that identifies the value being
  382. obtained. If this value does not have REG_SZ type the function
  383. returns FALSE.
  384. cchMaxValueLength - Size of the OUT buffer to allocate, in characters.
  385. Value - A pointer to a location that upon exit will contain the returned
  386. LPWSTR value.
  387. Return Value:
  388. TRUE - Success
  389. FALSE - A fatal error occured.
  390. --*/
  391. {
  392. DWORD status;
  393. DWORD dwType;
  394. DWORD dwSize = (cchMaxValueLength + 1) * sizeof(WCHAR);
  395. *Value = (LPWSTR) LocalAlloc(LMEM_ZEROINIT, dwSize);
  396. if (*Value == NULL)
  397. {
  398. return FALSE;
  399. }
  400. status = RegQueryValueEx(KeyHandle,
  401. ValueName,
  402. 0, // reserved
  403. &dwType, // type
  404. (LPBYTE) *Value,
  405. &dwSize);
  406. if (status || (dwSize % 2) != 0)
  407. {
  408. LocalFree(*Value);
  409. *Value = NULL;
  410. return FALSE;
  411. }
  412. if (dwType != REG_SZ)
  413. {
  414. //
  415. // Legacy -- MPR writes out a NULL username as a DWORD 0x0. Make sure
  416. // these values are NULL-terminated
  417. //
  418. (*Value)[cchMaxValueLength] = L'\0';
  419. return TRUE;
  420. }
  421. return TRUE;
  422. }
  423. DWORD
  424. MprEnumKey(
  425. IN HKEY KeyHandle,
  426. IN DWORD SubKeyIndex,
  427. OUT LPTSTR *SubKeyName,
  428. IN DWORD MaxSubKeyNameLen
  429. )
  430. /*++
  431. Routine Description:
  432. This function obtains a single name of a subkey from the registry.
  433. A key handle for the primary key is passed in. Subkeys are enumerated
  434. one-per-call with the passed in index indicating where we are in the
  435. enumeration.
  436. NOTE: This function allocates memory for the returned SubKeyName.
  437. Arguments:
  438. KeyHandle - Handle to the key whose sub keys are to be enumerated.
  439. SubKeyIndex - Indicates the number (index) of the sub key to be returned.
  440. SubKeyName - A pointer to the location where the pointer to the
  441. subkey name string is to be placed.
  442. MaxSubKeyNameLen - This is the length of the largest subkey. This value
  443. was obtained from calling MprGetKeyInfo. The length is in number
  444. of characters and does not include the NULL terminator.
  445. Return Value:
  446. WN_SUCCESS - The operation was successful.
  447. STATUS_NO_MORE_SUBKEYS - The SubKeyIndex value was larger than the
  448. number of subkeys.
  449. error returned from LocalAlloc
  450. --*/
  451. {
  452. DWORD status;
  453. FILETIME lastWriteTime;
  454. DWORD bufferSize;
  455. //
  456. // Allocate buffer to receive the SubKey Name.
  457. //
  458. // NOTE: Space is allocated for an extra character because in the case
  459. // of a drive name, we need to add the trailing colon.
  460. //
  461. bufferSize = (MaxSubKeyNameLen + 2) * sizeof(TCHAR);
  462. *SubKeyName = (LPTSTR) LocalAlloc(LMEM_FIXED, bufferSize);
  463. if(*SubKeyName == NULL) {
  464. MPR_LOG(ERROR,"MprEnumKey:LocalAlloc failed %d\n", GetLastError());
  465. return(WN_OUT_OF_MEMORY);
  466. }
  467. //
  468. // Get the Subkey name at that index.
  469. //
  470. status = RegEnumKeyEx(
  471. KeyHandle, // hKey
  472. SubKeyIndex, // dwIndex
  473. *SubKeyName, // lpName
  474. &bufferSize, // lpcbName
  475. NULL, // lpTitleIndex
  476. NULL, // lpClass
  477. NULL, // lpcbClass
  478. &lastWriteTime); // lpftLastWriteTime
  479. if (status != NO_ERROR) {
  480. MPR_LOG(ERROR,"MprEnumKey:RegEnumKeyEx failed %d\n",status);
  481. LocalFree(*SubKeyName);
  482. return(status);
  483. }
  484. return(WN_SUCCESS);
  485. }
  486. BOOL
  487. MprGetKeyInfo(
  488. IN HKEY KeyHandle,
  489. OUT LPDWORD TitleIndex OPTIONAL,
  490. OUT LPDWORD NumSubKeys,
  491. OUT LPDWORD MaxSubKeyLen,
  492. OUT LPDWORD NumValues OPTIONAL,
  493. OUT LPDWORD MaxValueLen
  494. )
  495. /*++
  496. Routine Description:
  497. Arguments:
  498. KeyHandle - Handle to the key for which we are to obtain information.
  499. NumSubKeys - This is a pointer to a location where the number
  500. of sub keys is to be placed.
  501. MaxSubKeyLen - This is a pointer to a location where the length of
  502. the longest subkey name is to be placed.
  503. NumValues - This is a pointer to a location where the number of
  504. key values is to be placed. This pointer is optional and can be
  505. NULL.
  506. MaxValueLen - This is a pointer to a location where the length of
  507. the longest data value is to be placed.
  508. Return Value:
  509. TRUE - The operation was successful.
  510. FALSE - A failure occured. The returned values are not to be believed.
  511. --*/
  512. {
  513. DWORD status;
  514. DWORD maxClassLength;
  515. DWORD numValueNames;
  516. DWORD maxValueNameLength;
  517. DWORD securityDescLength;
  518. FILETIME lastWriteTime;
  519. //
  520. // Get the Key Information
  521. //
  522. status = RegQueryInfoKey(
  523. KeyHandle,
  524. NULL, // Class
  525. NULL, // size of class buffer (in bytes)
  526. NULL, // DWORD to receive title index
  527. NumSubKeys, // number of subkeys
  528. MaxSubKeyLen, // length(chars-no null) of longest subkey name
  529. &maxClassLength, // length of longest subkey class string
  530. &numValueNames, // number of valueNames for this key
  531. &maxValueNameLength, // length of longest ValueName
  532. MaxValueLen, // length of longest value's data field
  533. &securityDescLength, // lpcbSecurityDescriptor
  534. &lastWriteTime); // the last time the key was modified
  535. if (status != 0)
  536. {
  537. MPR_LOG(ERROR,"MprGetKeyInfo: RegQueryInfoKey Error %d\n",status);
  538. return(FALSE);
  539. }
  540. if (NumValues != NULL) {
  541. *NumValues = numValueNames;
  542. }
  543. //
  544. // Support for title index has been removed from the Registry API.
  545. //
  546. if (TitleIndex != NULL) {
  547. *TitleIndex = 0;
  548. }
  549. return(TRUE);
  550. }
  551. BOOL
  552. MprFindDriveInRegistry (
  553. IN LPCTSTR DriveName,
  554. IN OUT LPTSTR *pRemoteName
  555. )
  556. /*++
  557. Routine Description:
  558. This function determines whether a particular re-directed drive
  559. name resides in the network connection section of the current user's
  560. registry path. If the drive is already "remembered" in this section,
  561. this function returns TRUE.
  562. Arguments:
  563. DriveName - A pointer to a string containing the name of the redirected
  564. drive.
  565. pRemoteName - If the DriveName is found in the registry, and if this
  566. is non-null, the remote name for the connection is read, and a
  567. pointer to the string is stored in this pointer location.
  568. If the remote name cannot be read from the registry, a NULL
  569. pointer is returned in this location.
  570. Return Value:
  571. TRUE - The redirected drive is "remembered in the registry".
  572. FALSE - The redirected drive is not saved in the registry.
  573. --*/
  574. {
  575. BOOL bStatus = TRUE;
  576. HKEY connectKey = NULL;
  577. HKEY subKey = NULL;
  578. LPWSTR KeyName = STACK_ALLOC(DriveName);
  579. if (KeyName == NULL) {
  580. return FALSE;
  581. }
  582. RemoveColon(KeyName, DriveName);
  583. //
  584. // Get a handle for the connection section of the user's registry
  585. // space.
  586. //
  587. if (!MprOpenKey(
  588. HKEY_CURRENT_USER,
  589. CONNECTION_KEY_NAME,
  590. &connectKey,
  591. DA_READ)) {
  592. MPR_LOG(ERROR,"MprFindDriveInRegistry: MprOpenKey Failed\n",0);
  593. return (FALSE);
  594. }
  595. if (!MprOpenKey(
  596. connectKey,
  597. KeyName,
  598. &subKey,
  599. DA_READ)) {
  600. MPR_LOG(TRACE,"MprFindDriveInRegistry: Drive %s Not Found\n",DriveName);
  601. bStatus = FALSE;
  602. }
  603. else {
  604. //
  605. // The drive was found in the registry, if the caller wants to have
  606. // the RemoteName, then get it.
  607. //
  608. if (pRemoteName != NULL) {
  609. //
  610. // Get the RemoteName (memory is allocated by this function)
  611. //
  612. if(!MprGetKeyValue(
  613. subKey,
  614. REMOTE_PATH_NAME,
  615. pRemoteName)) {
  616. MPR_LOG(TRACE,"MprFindDriveInRegistry: Could not read "
  617. "Remote path for Drive %ws \n",DriveName);
  618. pRemoteName = NULL;
  619. }
  620. }
  621. }
  622. if ( subKey )
  623. RegCloseKey(subKey);
  624. if ( connectKey )
  625. RegCloseKey(connectKey);
  626. return(bStatus);
  627. }
  628. DWORD
  629. I_MprSaveConn(
  630. IN HKEY HiveRoot,
  631. IN LPCWSTR ProviderName,
  632. IN DWORD ProviderType,
  633. IN LPCWSTR UserName,
  634. IN LPCWSTR LocalName,
  635. IN LPCWSTR RemoteName,
  636. IN DWORD ConnectionType,
  637. IN BYTE ProviderFlags,
  638. IN DWORD DeferFlags
  639. )
  640. /*++
  641. Routine Description:
  642. Writes the information about a connection to the network connection
  643. section of a user's registry path.
  644. NOTE: If connection information is already stored in the registry for
  645. this drive, the current information will be overwritten with the new
  646. information.
  647. Arguments:
  648. HiveRoot - A handle to the root of the user hive in which this
  649. information should be written, such as HKEY_CURRENT_USER.
  650. ProviderName - The provider that completed the connection.
  651. ProviderType - The provider type, if known. If not known, zero should
  652. be passed, and a type will not be written to the registry. (This
  653. is used by setup when upgrading from Win95 to NT.)
  654. UserName - The name of the user on whose behalf the connection was made.
  655. LocalName - The name of the local device that is redirected, with or
  656. without a trailing colon, such as "J" or "J:" or "LPT1".
  657. RemoteName - The network path to which the connection was made.
  658. ConnectionType - either RESOURCETYPE_DISK or RESOURCETYPE_PRINT.
  659. ProviderFlags - A byte of data to be saved along with the connection,
  660. and passed back to the provider when the connection is restored.
  661. DeferFlags - A DWORD to be saved in the connection's "Defer" value. If
  662. this is zero, the value is not stored.
  663. The meaning of the bits of this DWORD are as follows:
  664. DEFER_EXPLICIT_PASSWORD - a password was explicitly specified when
  665. the connection was made.
  666. DEFER_UNKNOWN - it is not known whether a password was explicitly
  667. specified when the connection was made.
  668. DEFER_DEFAULT_CRED - The provider believes that default creds were
  669. used when the connection was made.
  670. Return Value:
  671. ERROR_SUCCESS - If the operation was successful.
  672. Other Win32 errors - If the operation failed in any way. If a failure
  673. occurs, the information is not stored in the registry.
  674. --*/
  675. {
  676. HKEY connectKey;
  677. HKEY localDevHandle;
  678. LPCTSTR pUserName;
  679. DWORD status, IgnoredStatus;
  680. //
  681. // Remove the colon on the name since the registry doesn't like
  682. // this in a key name.
  683. //
  684. LPWSTR KeyName = STACK_ALLOC(LocalName);
  685. if (KeyName == NULL) {
  686. return ERROR_NOT_ENOUGH_MEMORY;
  687. }
  688. RemoveColon(KeyName, LocalName);
  689. //
  690. // Get a handle for the connection section of the user's registry
  691. // space.
  692. //
  693. if ((status = MprCreateRegKey(
  694. HiveRoot,
  695. CONNECTION_KEY_NAME,
  696. &connectKey)) != ERROR_SUCCESS) {
  697. MPR_LOG(ERROR,"I_MprSaveConn: \\HKEY_CURRENT_USER\\network "
  698. "could not be opened or created, error %ld\n", status);
  699. return(status);
  700. }
  701. //
  702. // Get (or create) the handle for the local name (without colon).
  703. //
  704. if ((status = MprCreateRegKey(
  705. connectKey,
  706. KeyName,
  707. &localDevHandle)) != ERROR_SUCCESS) {
  708. MPR_LOG(ERROR,"I_MprSaveConn: MprCreateRegKey Failed, "
  709. "error %ld\n", status);
  710. RegCloseKey(connectKey);
  711. return(status);
  712. }
  713. //
  714. // Now that the key is created, store away the appropriate values.
  715. //
  716. MPR_LOG(TRACE,"RememberConnection: Setting RemotePath\n",0);
  717. if((status = MprSetRegValue(
  718. localDevHandle,
  719. REMOTE_PATH_NAME,
  720. RemoteName,
  721. 0)) != ERROR_SUCCESS) {
  722. MPR_LOG(ERROR,
  723. "I_MprSaveConn: MprSetRegValueFailed %lu - RemotePath\n",status);
  724. goto CleanExit;
  725. }
  726. MPR_LOG(TRACE,"RememberConnection: Setting User\n",0);
  727. pUserName = UserName;
  728. if (UserName == NULL) {
  729. pUserName = TEXT("");
  730. }
  731. if((status = MprSetRegValue(
  732. localDevHandle,
  733. USER_NAME,
  734. pUserName,
  735. 0)) != ERROR_SUCCESS) {
  736. goto CleanExit;
  737. }
  738. MPR_LOG(TRACE,"RememberConnection: Setting ProviderName\n",0);
  739. if((status = MprSetRegValue(
  740. localDevHandle,
  741. PROVIDER_NAME,
  742. ProviderName,
  743. 0)) != ERROR_SUCCESS) {
  744. goto CleanExit;
  745. }
  746. if (ProviderType != 0)
  747. {
  748. MPR_LOG(TRACE,"RememberConnection: Setting ProviderType\n",0);
  749. if((status = MprSetRegValue(
  750. localDevHandle,
  751. PROVIDER_TYPE,
  752. NULL,
  753. ProviderType)) != ERROR_SUCCESS) {
  754. goto CleanExit;
  755. }
  756. }
  757. // else RegDeleteValue -- not done because ProviderType is 0 only
  758. // during upgrade, while writing to a fresh user hive
  759. MPR_LOG(TRACE,"RememberConnection: Setting ConnectionType\n",0);
  760. if((status = MprSetRegValue(
  761. localDevHandle,
  762. CONNECTION_TYPE,
  763. NULL,
  764. ConnectionType)) != ERROR_SUCCESS) {
  765. goto CleanExit;
  766. }
  767. if (ProviderFlags != 0)
  768. {
  769. MPR_LOG(TRACE,"RememberConnection: Setting ProviderFlags\n",0);
  770. if((status = MprSetRegValue(
  771. localDevHandle,
  772. PROVIDER_FLAGS,
  773. NULL,
  774. ProviderFlags)) != ERROR_SUCCESS) {
  775. goto CleanExit;
  776. }
  777. }
  778. // We can't roll this back if something fails after it, so we
  779. // must do this last
  780. if ((status = MprSaveDeferFlags(localDevHandle, DeferFlags))
  781. != ERROR_SUCCESS) {
  782. goto CleanExit;
  783. }
  784. //
  785. // Flush the new key, and then close the handle to it.
  786. //
  787. MPR_LOG(TRACE,"RememberConnection: Flushing Registry Key\n",0);
  788. IgnoredStatus = RegFlushKey(localDevHandle);
  789. if (IgnoredStatus != NO_ERROR) {
  790. MPR_LOG(ERROR,"RememberConnection: Flushing Registry Key Failed %ld\n",
  791. IgnoredStatus);
  792. }
  793. CleanExit:
  794. RegCloseKey(localDevHandle);
  795. if (status != ERROR_SUCCESS) {
  796. IgnoredStatus = RegDeleteKey(connectKey, KeyName);
  797. if (IgnoredStatus != NO_ERROR) {
  798. MPR_LOG(ERROR, "RememberConnection: RegDeleteKey Failed %d\n", IgnoredStatus);
  799. }
  800. }
  801. RegCloseKey(connectKey);
  802. return(status);
  803. }
  804. DWORD
  805. MprSaveDeferFlags(
  806. IN HKEY RegKey,
  807. IN DWORD DeferFlags
  808. )
  809. {
  810. DWORD status;
  811. if (DeferFlags == 0)
  812. {
  813. MPR_LOG0(TRACE,"Removing DeferFlags\n");
  814. status = RegDeleteValue(RegKey, DEFER_FLAGS);
  815. if (status == ERROR_FILE_NOT_FOUND)
  816. {
  817. status = ERROR_SUCCESS;
  818. }
  819. }
  820. else
  821. {
  822. MPR_LOG(TRACE,"Setting DeferFlags %#lx\n",DeferFlags);
  823. status = MprSetRegValue(
  824. RegKey,
  825. DEFER_FLAGS,
  826. NULL,
  827. DeferFlags);
  828. }
  829. return status;
  830. }
  831. DWORD
  832. MprSetRegValue(
  833. IN HKEY KeyHandle,
  834. IN LPTSTR ValueName,
  835. IN LPCTSTR ValueString,
  836. IN DWORD LongValue
  837. )
  838. /*++
  839. Routine Description:
  840. Stores a single ValueName and associated data in the registry for
  841. the key identified by the KeyHandle. The data associated with the
  842. value can either be a string or a 32-bit LONG. If the ValueString
  843. argument contains a pointer to a value, then the LongValue argument
  844. is ignored.
  845. Arguments:
  846. KeyHandle - Handle of the key for which the value entry is to be set.
  847. ValueName - Pointer to a string that contains the name of the value
  848. being set.
  849. ValueString - Pointer to a string that is to become the data stored
  850. at that value name. If this argument is not present, then the
  851. LongValue argument is the data stored at the value name. If this
  852. argument is present, then LongValue is ignored.
  853. LongValue - A LONG sized data value that is to be stored at the
  854. value name.
  855. Return Value:
  856. Win32 error from RegSetValueEx (0 = success)
  857. --*/
  858. {
  859. DWORD status;
  860. const BYTE * valueData;
  861. DWORD valueSize;
  862. DWORD valueType;
  863. if( ARGUMENT_PRESENT(ValueString)) {
  864. valueData = (const BYTE *)ValueString;
  865. valueSize = STRSIZE(ValueString);
  866. valueType = REG_SZ;
  867. }
  868. else {
  869. valueData = (const BYTE *)&LongValue;
  870. valueSize = sizeof(DWORD);
  871. valueType = REG_DWORD;
  872. }
  873. status = RegSetValueEx(
  874. KeyHandle, // hKey
  875. ValueName, // lpValueName
  876. 0, // dwValueTitle (OPTIONAL)
  877. valueType, // dwType
  878. valueData, // lpData
  879. valueSize); // cbData
  880. if(status != NO_ERROR) {
  881. MPR_LOG3(ERROR,"MprSetRegValue: RegSetValueEx(%#lx \"%ws\") Failed %ld\n",
  882. KeyHandle, ValueName, status);
  883. }
  884. return(status);
  885. }
  886. DWORD
  887. MprCreateRegKey(
  888. IN HKEY BaseKeyHandle,
  889. IN LPCTSTR KeyName,
  890. OUT PHKEY KeyHandlePtr
  891. )
  892. /*++
  893. Routine Description:
  894. Creates a key in the registry at the location described by KeyName.
  895. Arguments:
  896. BaseKeyHandle - This is a handle for the base (parent) key - where the
  897. subkey is to be created.
  898. KeyName - This is a pointer to a string that describes the path to the
  899. key that is to be created.
  900. KeyHandle - This is a pointer to a location where the the handle for the
  901. newly created key is to be placed.
  902. Return Value:
  903. Win32 error from RegCreateKeyEx (0 = success)
  904. Note:
  905. --*/
  906. {
  907. DWORD status;
  908. DWORD disposition;
  909. //
  910. // Create the new key.
  911. //
  912. status = RegCreateKeyEx(
  913. BaseKeyHandle, // hKey
  914. KeyName, // lpSubKey
  915. 0L, // dwTitleIndex
  916. TEXT("GenericClass"), // lpClass
  917. 0, // ulOptions
  918. KEY_WRITE, // samDesired (desired access)
  919. NULL, // lpSecurityAttrubutes (Security Descriptor)
  920. KeyHandlePtr, // phkResult
  921. &disposition); // lpulDisposition
  922. if (status != NO_ERROR) {
  923. MPR_LOG3(ERROR,"MprCreateRegKey: RegCreateKeyEx(%#lx, \"%ws\") failed %d\n",
  924. BaseKeyHandle, KeyName, status);
  925. }
  926. else {
  927. MPR_LOG(TRACE,"MprCreateRegKey: Disposition = 0x%x\n",disposition);
  928. }
  929. return(status);
  930. }
  931. BOOL
  932. MprReadConnectionInfo(
  933. IN HKEY KeyHandle,
  934. IN LPCTSTR DriveName,
  935. IN DWORD Index,
  936. OUT LPDWORD ProviderFlags,
  937. OUT LPDWORD DeferFlags,
  938. OUT LPTSTR *UserNamePtr,
  939. OUT LPNETRESOURCEW NetResource,
  940. OUT HKEY *SubKeyHandleOut,
  941. IN DWORD MaxSubKeyLen
  942. )
  943. /*++
  944. Routine Description:
  945. This function reads the data associated with a connection key.
  946. Buffers are allocated to store:
  947. UserName, RemoteName, LocalName, Provider
  948. Pointers to those buffers are returned.
  949. Also the connection type is read and stored in the NetResource structure.
  950. If the provider type is found in the registry, and a matching provider
  951. type is found in the GlobalProviderInfo array, the provider name is not
  952. read from the registry. Instead it is read from the GlobalProviderInfo
  953. array.
  954. If the provider name is read from the registry and a matching provider
  955. name is found in the GlobalProviderInfo array, the provider type is
  956. written to the registry.
  957. Arguments:
  958. KeyHandle - This is an already opened handle to the key whose
  959. sub-keys are to be enumerated.
  960. DriveName - This is the local name of the drive (eg. "f:") for which
  961. the connection information is to be obtained. If DriveName is
  962. NULL, then the Index is used to enumerate the keyname. Then
  963. that keyname is used.
  964. Index - This is the index that identifies the subkey for which we
  965. would like to receive information.
  966. ProviderFlags - This is a pointer to a location where the ProviderFlags
  967. value stored with the connection will be placed. If this value
  968. cannot be retrieved, 0 will be placed here.
  969. DeferFlags - This is a pointer to a location where the DeferFlags
  970. value stored with the connection will be placed. If this value
  971. cannot be retrieved, 0 will be placed here.
  972. UserNamePtr - This is a pointer to a location where the pointer to the
  973. UserName string is to be placed. If there is no user name, a
  974. NULL pointer will be returned.
  975. NetResource - This is a pointer to a NETRESOURCE structure where
  976. information such as lpRemoteName, lpLocalName, lpProvider, and Type
  977. are to be placed.
  978. SubKeyHandleOut - This is a pointer to a location where the handle to
  979. the subkey that holds information about this connection will be
  980. placed. This may be NULL. If it is not NULL the caller must close
  981. the handle.
  982. Return Value:
  983. Note:
  984. --*/
  985. {
  986. DWORD status = NO_ERROR;
  987. LPTSTR driveName = NULL;
  988. HKEY subKeyHandle = NULL;
  989. DWORD cbData;
  990. DWORD ProviderType = 0;
  991. LPPROVIDER Provider;
  992. //
  993. // Initialize the Pointers that are to be updated.
  994. //
  995. *UserNamePtr = NULL;
  996. NetResource->lpLocalName = NULL;
  997. NetResource->lpRemoteName = NULL;
  998. NetResource->lpProvider = NULL;
  999. NetResource->dwType = 0L;
  1000. //
  1001. // If we don't have a DriveName, then get one by enumerating the
  1002. // next key name.
  1003. //
  1004. if (DriveName == NULL) {
  1005. //
  1006. // Get the name of a subkey of the network connection key.
  1007. // (memory is allocated by this function).
  1008. //
  1009. status = MprEnumKey(KeyHandle, Index, &driveName, MaxSubKeyLen);
  1010. if (status != WN_SUCCESS) {
  1011. return(FALSE);
  1012. }
  1013. }
  1014. else {
  1015. //
  1016. // We have a drive name, alloc new space and copy it to that
  1017. // location.
  1018. //
  1019. driveName = (LPTSTR) LocalAlloc(LMEM_FIXED, STRSIZE(DriveName));
  1020. if (driveName == NULL) {
  1021. MPR_LOG(ERROR, "MprReadConnectionInfo: Local Alloc Failed %d\n",
  1022. GetLastError());
  1023. return(FALSE);
  1024. }
  1025. RemoveColon(driveName, DriveName);
  1026. }
  1027. MPR_LOG1(TRACE,"MprReadConnectionInfo: LocalName = %ws\n",driveName);
  1028. //
  1029. // Open the sub-key
  1030. //
  1031. if (!MprOpenKey(
  1032. KeyHandle,
  1033. driveName,
  1034. &subKeyHandle,
  1035. DA_WRITE)){
  1036. status = WN_BAD_PROFILE;
  1037. MPR_LOG1(TRACE,"MprReadConnectionInfo: Could not open %ws Key\n",driveName);
  1038. goto CleanExit;
  1039. }
  1040. //
  1041. // Add the trailing colon to the driveName.
  1042. //
  1043. cbData = STRLEN(driveName);
  1044. driveName[cbData] = TEXT(':');
  1045. driveName[cbData+1] = TEXT('\0');
  1046. //
  1047. // Store the drive name in the return structure.
  1048. //
  1049. NetResource->lpLocalName = driveName;
  1050. //
  1051. // Get the RemoteName (memory is allocated by this function)
  1052. //
  1053. if(!MprGetKeyValue(
  1054. subKeyHandle,
  1055. REMOTE_PATH_NAME,
  1056. &(NetResource->lpRemoteName))) {
  1057. status = WN_BAD_PROFILE;
  1058. MPR_LOG0(TRACE,"MprReadConnectionInfo: Could not get RemoteName\n");
  1059. goto CleanExit;
  1060. }
  1061. //
  1062. // Get the UserName (memory is allocated by this function)
  1063. //
  1064. if(!MprGetKeyStringValue(
  1065. subKeyHandle,
  1066. USER_NAME,
  1067. CRED_MAX_USERNAME_LENGTH,
  1068. UserNamePtr))
  1069. {
  1070. status = WN_BAD_PROFILE;
  1071. MPR_LOG0(TRACE,"MprReadConnectionInfo: Could not get UserName\n");
  1072. goto CleanExit;
  1073. }
  1074. else
  1075. {
  1076. //
  1077. // If there is no user name (the length is 0), then set the
  1078. // return pointer to NULL.
  1079. //
  1080. if (STRLEN(*UserNamePtr) == 0) {
  1081. LocalFree(*UserNamePtr);
  1082. *UserNamePtr = NULL;
  1083. }
  1084. }
  1085. //
  1086. // Get the Provider Type and load the providers if necessary. Both
  1087. // MprGetConnection and a remembered enumeration can make it to this
  1088. // point in this state and we don't want to do a Level 2
  1089. // initialization every time one of those functions is called simply
  1090. // because this case _might_ be hit. For example, calling
  1091. // MprGetConnection on an unconnected drive letter may or may not
  1092. // have a name associated with it in the registry. If so, there's
  1093. // no need to load the providers to get information from them. This
  1094. // is equivalent to INIT_IF_NECESSARY(NETWORK_LEVEL,status)
  1095. //
  1096. if (MprGetKeyDwordValue(
  1097. subKeyHandle,
  1098. PROVIDER_TYPE,
  1099. &ProviderType)
  1100. &&
  1101. (MprLevel2Init(NETWORK_LEVEL) == WN_SUCCESS)
  1102. &&
  1103. ((Provider = MprFindProviderByType(ProviderType)) != NULL))
  1104. {
  1105. MPR_LOG(RESTORE,"MprReadConnectionInfo: Found recognized provider type %#lx\n",
  1106. ProviderType);
  1107. //
  1108. // Got a recognized provider type from the registry.
  1109. // If we have a name for this provider in memory, use it, rather than
  1110. // reading the name from the registry.
  1111. // (memory is allocated for the name)
  1112. //
  1113. if (Provider->Resource.lpProvider != NULL)
  1114. {
  1115. NetResource->lpProvider =
  1116. (LPWSTR) LocalAlloc(0, STRSIZE(Provider->Resource.lpProvider));
  1117. if (NetResource->lpProvider == NULL)
  1118. {
  1119. status = WN_BAD_PROFILE;
  1120. MPR_LOG(RESTORE,"MprReadConnectionInfo: LocalAlloc failed %ld\n",
  1121. GetLastError());
  1122. goto CleanExit;
  1123. }
  1124. wcscpy(NetResource->lpProvider, Provider->Resource.lpProvider);
  1125. }
  1126. }
  1127. //
  1128. // If we haven't got a provider name yet, try to read it from the registry.
  1129. // (Memory is allocated by this function.)
  1130. // This could legitimately happen in 2 cases:
  1131. // (1) We are reading a profile that was created by a Windows NT 3.51 or
  1132. // earlier machine and has not yet been converted to a 4.0 or later
  1133. // profile. Windows NT versions 3.51 and earlier wrote only the provider
  1134. // name to the registry, not the type.
  1135. // (2) We are reading a floating profile that was written by another
  1136. // machine which has a network provider installed that isn't installed on
  1137. // this machine. Or, a network provider was de-installed from this
  1138. // machine.
  1139. //
  1140. if (NetResource->lpProvider == NULL)
  1141. {
  1142. if(!MprGetKeyValue(
  1143. subKeyHandle,
  1144. PROVIDER_NAME,
  1145. &(NetResource->lpProvider)))
  1146. {
  1147. status = WN_BAD_PROFILE;
  1148. MPR_LOG0(RESTORE,"MprReadConnectionInfo: Could not get ProviderName\n");
  1149. goto CleanExit;
  1150. }
  1151. //
  1152. // Got a provider name from the registry.
  1153. // If we didn't read a provider type from the registry, but we
  1154. // recognize the provider name, write the type now for future use.
  1155. // (This would occur in case (1) above.)
  1156. // Failure to write the type is ignored.
  1157. // (Pathological cases in which we get an unrecognized type but a
  1158. // recognized name are left untouched.)
  1159. // Since it's possible to get to this point without having the
  1160. // providers loaded, we'll init if necessary here (see reasoning
  1161. // above). This is equivalent to INIT_IF_NECESSARY(NETWORK_LEVEL,status)
  1162. //
  1163. status = MprLevel2Init(NETWORK_LEVEL);
  1164. if (status != WN_SUCCESS) {
  1165. goto CleanExit;
  1166. }
  1167. Provider = MprFindProviderByName(NetResource->lpProvider);
  1168. if (Provider != NULL && Provider->Type != 0 && ProviderType == 0)
  1169. {
  1170. MPR_LOG2(RESTORE,"MprReadConnectionInfo: Setting ProviderType %#lx for %ws\n",
  1171. Provider->Type, driveName);
  1172. status = MprSetRegValue(
  1173. subKeyHandle,
  1174. PROVIDER_TYPE,
  1175. NULL,
  1176. Provider->Type);
  1177. if (status != ERROR_SUCCESS)
  1178. {
  1179. MPR_LOG(RESTORE,"MprReadConnectionInfo: Couldn't set ProviderType, %ld\n",
  1180. status);
  1181. }
  1182. }
  1183. }
  1184. //
  1185. // Get the ProviderFlags (failure is ignored)
  1186. //
  1187. cbData = sizeof(DWORD);
  1188. status = RegQueryValueEx(
  1189. subKeyHandle, // hKey
  1190. PROVIDER_FLAGS, // lpValueName
  1191. NULL, // lpTitleIndex
  1192. NULL, // lpType
  1193. (LPBYTE)ProviderFlags, // lpData
  1194. &cbData); // lpcbData
  1195. if (status == NO_ERROR) {
  1196. MPR_LOG2(RESTORE,"MprReadConnectionInfo: Got ProviderFlags %#lx for %ws\n",
  1197. *ProviderFlags, driveName);
  1198. }
  1199. else {
  1200. *ProviderFlags = 0;
  1201. }
  1202. //
  1203. // Get the DeferFlags (failure is ignored)
  1204. //
  1205. if (MprGetKeyDwordValue(
  1206. subKeyHandle,
  1207. DEFER_FLAGS,
  1208. DeferFlags
  1209. ))
  1210. {
  1211. MPR_LOG2(RESTORE,"MprReadConnectionInfo: Got DeferFlags %#lx for %ws\n",
  1212. *DeferFlags, driveName);
  1213. }
  1214. else
  1215. {
  1216. *DeferFlags = 0;
  1217. }
  1218. //
  1219. // Get the Connection Type
  1220. //
  1221. cbData = sizeof(DWORD);
  1222. status = RegQueryValueEx(
  1223. subKeyHandle, // hKey
  1224. CONNECTION_TYPE, // lpValueName
  1225. NULL, // lpTitleIndex
  1226. NULL, // lpType
  1227. (LPBYTE)&(NetResource->dwType), // lpData
  1228. &cbData); // lpcbData
  1229. if (status != NO_ERROR) {
  1230. MPR_LOG1(ERROR,"MprReadConnectionInfo:RegQueryValueEx failed %d\n",
  1231. status);
  1232. MPR_LOG0(TRACE,"MprReadConnectionInfo: Could not get ConnectionType\n");
  1233. status = WN_BAD_PROFILE;
  1234. }
  1235. CleanExit:
  1236. if (status != NO_ERROR) {
  1237. LocalFree(driveName);
  1238. LocalFree(NetResource->lpRemoteName);
  1239. LocalFree(*UserNamePtr);
  1240. LocalFree(NetResource->lpProvider);
  1241. NetResource->lpLocalName = NULL;
  1242. NetResource->lpRemoteName = NULL;
  1243. NetResource->lpProvider = NULL;
  1244. *UserNamePtr = NULL;
  1245. if (subKeyHandle != NULL) {
  1246. RegCloseKey(subKeyHandle);
  1247. }
  1248. return(FALSE);
  1249. }
  1250. else {
  1251. if (SubKeyHandleOut == NULL) {
  1252. RegCloseKey(subKeyHandle);
  1253. }
  1254. else {
  1255. *SubKeyHandleOut = subKeyHandle;
  1256. }
  1257. return(TRUE);
  1258. }
  1259. }
  1260. VOID
  1261. MprForgetRedirConnection(
  1262. IN LPCTSTR lpName
  1263. )
  1264. /*++
  1265. Routine Description:
  1266. This function removes a key for the specified redirected device from
  1267. the current users portion of the registry.
  1268. Arguments:
  1269. lpName - This is a pointer to a redirected device name.
  1270. Return Value:
  1271. Note:
  1272. --*/
  1273. {
  1274. DWORD status;
  1275. HKEY connectKey;
  1276. MPR_LOG(TRACE,"In MprForgetConnection for %s\n", lpName);
  1277. LPWSTR KeyName = STACK_ALLOC(lpName);
  1278. if (KeyName == NULL) {
  1279. return;
  1280. }
  1281. RemoveColon(KeyName, lpName);
  1282. //
  1283. // Get a handle for the connection section of the user's registry
  1284. // space.
  1285. //
  1286. if (!MprOpenKey(
  1287. HKEY_CURRENT_USER,
  1288. CONNECTION_KEY_NAME,
  1289. &connectKey,
  1290. DA_READ)) {
  1291. MPR_LOG(ERROR,"WNetForgetRedirCon: MprOpenKey #1 Failed\n",0);
  1292. return;
  1293. }
  1294. status = RegDeleteKey(connectKey, KeyName);
  1295. if (status != NO_ERROR) {
  1296. MPR_LOG(ERROR, "WNetForgetRedirCon: NtDeleteKey Failed %d\n", status);
  1297. }
  1298. //
  1299. // Flush the connect key, and then close the handle to it.
  1300. //
  1301. MPR_LOG(TRACE,"ForgetRedirConnection: Flushing Connection Key\n",0);
  1302. status = RegFlushKey(connectKey);
  1303. if (status != NO_ERROR) {
  1304. MPR_LOG(ERROR,"RememberConnection: Flushing Connection Key Failed %ld\n",
  1305. status);
  1306. }
  1307. RegCloseKey(connectKey);
  1308. return;
  1309. }
  1310. BOOL
  1311. MprGetRemoteName(
  1312. IN LPTSTR lpLocalName,
  1313. IN OUT LPDWORD lpBufferSize,
  1314. OUT LPTSTR lpRemoteName,
  1315. OUT LPDWORD lpStatus
  1316. )
  1317. /*++
  1318. Routine Description:
  1319. This fuction Looks in the CURRENT_USER portion of the registry for
  1320. connection information related to the lpLocalName passed in.
  1321. Arguments:
  1322. lpLocalName - Pointer to a string containing the name of the device to
  1323. look up.
  1324. lpBufferSize - Pointer to a the size information for the buffer.
  1325. On input, this contains the size of the buffer passed in.
  1326. if lpStatus contain WN_MORE_DATA, this will contain the
  1327. buffer size required to obtain the full string.
  1328. lpRemoteName - Pointer to a buffer where the remote name string is
  1329. to be placed.
  1330. lpStatus - Pointer to a location where the proper return code is to
  1331. be placed in the case where the connection information exists.
  1332. Return Value:
  1333. TRUE - If the connection information exists.
  1334. FALSE - If the connection information does not exist. When FALSE is
  1335. returned, none of output parameters are valid.
  1336. --*/
  1337. {
  1338. HKEY connectKey;
  1339. DWORD numSubKeys;
  1340. DWORD maxSubKeyLen;
  1341. DWORD maxValueLen;
  1342. DWORD status;
  1343. DWORD ProviderFlags;
  1344. DWORD DeferFlags;
  1345. NETRESOURCEW netResource;
  1346. LPTSTR userName;
  1347. //
  1348. // Get a handle for the connection section of the user's registry
  1349. // space.
  1350. //
  1351. if (!MprOpenKey(
  1352. HKEY_CURRENT_USER,
  1353. CONNECTION_KEY_NAME,
  1354. &connectKey,
  1355. DA_READ)) {
  1356. MPR_LOG(ERROR,"WNetGetConnection: MprOpenKey Failed\n",0);
  1357. return(FALSE);
  1358. }
  1359. if(!MprGetKeyInfo(
  1360. connectKey,
  1361. NULL,
  1362. &numSubKeys,
  1363. &maxSubKeyLen,
  1364. NULL,
  1365. &maxValueLen)) {
  1366. MPR_LOG(ERROR,"WNetGetConnection: MprGetKeyInfo Failed\n",0);
  1367. RegCloseKey(connectKey);
  1368. return(FALSE);
  1369. }
  1370. //
  1371. // Read the connection information.
  1372. // NOTE: This function allocates buffers for UserName and the
  1373. // following strings in the net resource structure:
  1374. // lpRemoteName,
  1375. // lpLocalName,
  1376. // lpProvider
  1377. //
  1378. if (MprReadConnectionInfo(
  1379. connectKey,
  1380. lpLocalName,
  1381. 0,
  1382. &ProviderFlags,
  1383. &DeferFlags,
  1384. &userName,
  1385. &netResource,
  1386. NULL,
  1387. maxSubKeyLen)) {
  1388. //
  1389. // The read succeeded. Therefore we have connection information.
  1390. //
  1391. if (*lpBufferSize >= STRSIZE(netResource.lpRemoteName)) {
  1392. __try {
  1393. STRCPY(lpRemoteName, netResource.lpRemoteName);
  1394. }
  1395. __except(EXCEPTION_EXECUTE_HANDLER) {
  1396. status = GetExceptionCode();
  1397. if (status != EXCEPTION_ACCESS_VIOLATION) {
  1398. MPR_LOG(ERROR,"WNetGetConnection:Unexpected Exception 0x%lx\n",status);
  1399. }
  1400. status = WN_BAD_POINTER;
  1401. }
  1402. if (status != WN_BAD_POINTER) {
  1403. //
  1404. // We successfully copied the remote name to the users
  1405. // buffer without an error.
  1406. //
  1407. status = WN_SUCCESS;
  1408. }
  1409. }
  1410. else {
  1411. *lpBufferSize = STRSIZE(netResource.lpRemoteName);
  1412. status = WN_MORE_DATA;
  1413. }
  1414. //
  1415. // Free up the resources allocated by MprReadConnectionInfo.
  1416. //
  1417. LocalFree(userName);
  1418. LocalFree(netResource.lpLocalName);
  1419. LocalFree(netResource.lpRemoteName);
  1420. LocalFree(netResource.lpProvider);
  1421. *lpStatus = status;
  1422. RegCloseKey(connectKey);
  1423. return(TRUE);
  1424. }
  1425. else {
  1426. //
  1427. // The read did not succeed.
  1428. //
  1429. RegCloseKey(connectKey);
  1430. return(FALSE);
  1431. }
  1432. }
  1433. DWORD
  1434. MprGetPrintKeyInfo(
  1435. HKEY KeyHandle,
  1436. LPDWORD NumValueNames,
  1437. LPDWORD MaxValueNameLength,
  1438. LPDWORD MaxValueLen)
  1439. /*++
  1440. Routine Description:
  1441. This function reads the data associated with a print reconnection key.
  1442. Arguments:
  1443. KeyHandle - This is an already opened handle to the key whose
  1444. info is rto be queried.
  1445. NumValueNames - Used to return the number of values
  1446. MaxValueNameLength - Used to return the max value name length
  1447. MaxValueLen - Used to return the max value data length
  1448. Return Value:
  1449. 0 if success. Win32 error otherwise.
  1450. Note:
  1451. --*/
  1452. {
  1453. DWORD err;
  1454. DWORD maxClassLength;
  1455. DWORD securityDescLength;
  1456. DWORD NumSubKeys ;
  1457. DWORD MaxSubKeyLen ;
  1458. FILETIME lastWriteTime;
  1459. //
  1460. // Get the Key Information
  1461. //
  1462. err = RegQueryInfoKey(
  1463. KeyHandle,
  1464. NULL, // Class
  1465. NULL, // size of class buffer (in bytes)
  1466. NULL, // DWORD to receive title index
  1467. &NumSubKeys, // number of subkeys
  1468. &MaxSubKeyLen, // length of longest subkey name
  1469. &maxClassLength, // length of longest subkey class string
  1470. NumValueNames, // number of valueNames for this key
  1471. MaxValueNameLength, // length of longest ValueName
  1472. MaxValueLen, // length of longest value's data field
  1473. &securityDescLength, // lpcbSecurityDescriptor
  1474. &lastWriteTime); // the last time the key was modified
  1475. return(err);
  1476. }
  1477. DWORD
  1478. MprForgetPrintConnection(
  1479. IN LPTSTR lpName
  1480. )
  1481. /*++
  1482. Routine Description:
  1483. This function removes a rememembered print reconnection value.
  1484. Arguments:
  1485. lpName - name of path to forget
  1486. Return Value:
  1487. 0 if success. Win32 error otherwise.
  1488. Note:
  1489. --*/
  1490. {
  1491. HKEY hKey ;
  1492. DWORD err ;
  1493. if (!MprOpenKey(
  1494. HKEY_CURRENT_USER,
  1495. PRINT_CONNECTION_KEY_NAME,
  1496. &hKey,
  1497. DA_WRITE))
  1498. {
  1499. return (GetLastError()) ;
  1500. }
  1501. err = RegDeleteValue(hKey,
  1502. lpName) ;
  1503. RegCloseKey(hKey) ;
  1504. return err ;
  1505. }