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.

256 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. port.c
  5. Abstract:
  6. This module contains the code for port handling
  7. Author:
  8. Dave Snipp (DaveSn) 15-Mar-1991
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #include <lm.h>
  13. #include <lmuse.h>
  14. #include <lmapibuf.h>
  15. #include <w32types.h>
  16. #include <local.h>
  17. LPWSTR pMonitorName = L"LAN Manager Print Share";
  18. PWINIPORT pIniFirstPort = NULL;
  19. PWINIPORT
  20. CreatePortEntry(
  21. LPWSTR pPortName,
  22. PPWINIPORT ppFirstPort
  23. )
  24. {
  25. DWORD cb;
  26. PWINIPORT pIniPort, pPort, pFirstPort;
  27. pFirstPort = *ppFirstPort;
  28. cb = sizeof(WINIPORT);
  29. EnterSplSem();
  30. if (pIniPort = AllocSplMem(cb)) {
  31. pIniPort->pName = AllocSplStr( pPortName );
  32. pIniPort->cb = cb;
  33. pIniPort->pNext = 0;
  34. pIniPort->signature = WIPO_SIGNATURE;
  35. if (pPort = pFirstPort) {
  36. while (pPort->pNext)
  37. pPort = pPort->pNext;
  38. pPort->pNext = pIniPort;
  39. } else
  40. *ppFirstPort = pIniPort;
  41. }
  42. LeaveSplSem();
  43. return pIniPort;
  44. }
  45. BOOL
  46. DeletePortEntry(
  47. LPWSTR pPortName,
  48. PPWINIPORT ppFirstPort
  49. )
  50. {
  51. DWORD cb;
  52. BOOL rc;
  53. PWINIPORT pPort, pPrevPort, pFirstPort;
  54. pFirstPort = *ppFirstPort;
  55. cb = sizeof(WINIPORT) + wcslen(pPortName)*sizeof(WCHAR) + sizeof(WCHAR);
  56. EnterSplSem();
  57. pPort = pFirstPort;
  58. while (pPort && lstrcmpi(pPort->pName, pPortName)) {
  59. pPrevPort = pPort;
  60. pPort = pPort->pNext;
  61. }
  62. if (pPort) {
  63. if ( pPort == pFirstPort ) {
  64. *ppFirstPort = pPort->pNext;
  65. } else {
  66. pPrevPort->pNext = pPort->pNext;
  67. }
  68. FreeSplStr( pPort->pName );
  69. FreeSplMem(pPort);
  70. rc = TRUE;
  71. }
  72. else
  73. rc = FALSE;
  74. LeaveSplSem();
  75. return rc;
  76. }
  77. DWORD
  78. CreateRegistryEntry(
  79. LPWSTR pPortName
  80. )
  81. {
  82. LONG Status;
  83. HKEY hkeyPath;
  84. HKEY hkeyPortNames;
  85. HANDLE hToken;
  86. hToken = RevertToPrinterSelf();
  87. Status = hToken ? NO_ERROR : GetLastError();
  88. if ( Status == NO_ERROR ) {
  89. Status = RegCreateKeyEx( HKEY_LOCAL_MACHINE, szRegistryPath, 0,
  90. NULL, 0, KEY_WRITE, NULL, &hkeyPath, NULL );
  91. }
  92. if( Status == NO_ERROR ) {
  93. Status = RegCreateKeyEx( hkeyPath, szRegistryPortNames, 0,
  94. NULL, 0, KEY_WRITE, NULL, &hkeyPortNames, NULL );
  95. if( Status == NO_ERROR ) {
  96. Status = RegSetValueEx( hkeyPortNames,
  97. pPortName,
  98. 0,
  99. REG_SZ,
  100. (LPBYTE)L"",
  101. 0 );
  102. RegCloseKey( hkeyPortNames );
  103. } else {
  104. DBGMSG( DBG_ERROR, ( "RegCreateKeyEx (%ws) failed: Error = %d\n",
  105. szRegistryPortNames, Status ) );
  106. }
  107. RegCloseKey( hkeyPath );
  108. } else {
  109. DBGMSG( DBG_ERROR, ( "RegCreateKeyEx (%ws) failed: Error = %d\n",
  110. szRegistryPath, Status ) );
  111. }
  112. if ( hToken ) {
  113. if ( !ImpersonatePrinterClient(hToken) && Status == ERROR_SUCCESS ) {
  114. Status = GetLastError();
  115. }
  116. }
  117. return Status;
  118. }
  119. DWORD
  120. DeleteRegistryEntry(
  121. LPWSTR pPortName
  122. )
  123. {
  124. LONG Status;
  125. HKEY hkeyPath;
  126. HKEY hkeyPortNames;
  127. HANDLE hToken;
  128. hToken = RevertToPrinterSelf();
  129. Status = hToken ? NO_ERROR : GetLastError();
  130. if ( Status == NO_ERROR ) {
  131. Status = RegOpenKeyEx( HKEY_LOCAL_MACHINE, szRegistryPath, 0,
  132. KEY_WRITE, &hkeyPath );
  133. }
  134. if( Status == NO_ERROR ) {
  135. Status = RegOpenKeyEx( hkeyPath, szRegistryPortNames, 0,
  136. KEY_WRITE, &hkeyPortNames );
  137. if( Status == NO_ERROR ) {
  138. RegDeleteValue( hkeyPortNames, pPortName );
  139. RegCloseKey( hkeyPortNames );
  140. } else {
  141. DBGMSG( DBG_WARNING, ( "RegOpenKeyEx (%ws) failed: Error = %d\n",
  142. szRegistryPortNames, Status ) );
  143. }
  144. RegCloseKey( hkeyPath );
  145. } else {
  146. DBGMSG( DBG_WARNING, ( "RegOpenKeyEx (%ws) failed: Error = %d\n",
  147. szRegistryPath, Status ) );
  148. }
  149. if ( hToken ) {
  150. if ( !ImpersonatePrinterClient(hToken) && Status == ERROR_SUCCESS ) {
  151. Status = GetLastError();
  152. }
  153. }
  154. return Status;
  155. }
  156. BOOL
  157. LMDeletePort(
  158. LPWSTR pName,
  159. HWND hWnd,
  160. LPWSTR pPortName
  161. )
  162. {
  163. BOOL rc;
  164. if (!MyName(pName)) {
  165. SetLastError(ERROR_INVALID_NAME);
  166. return FALSE;
  167. }
  168. rc = DeletePortEntry( pPortName, &pIniFirstPort );
  169. if(rc) {
  170. rc = BoolFromStatus(DeleteRegistryEntry(pPortName));
  171. } else {
  172. SetLastError(ERROR_UNKNOWN_PORT);
  173. }
  174. return rc;
  175. }
  176.