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.

307 lines
7.5 KiB

  1. #define _NTAPI_ULIB_
  2. #include "ulib.hxx"
  3. #include "path.hxx"
  4. #include "wstring.hxx"
  5. #include "redir.hxx"
  6. //
  7. // The string below represents the path used to determine whether or not
  8. // an LPT device is redirected to a COM device.
  9. // Due to performance, this path should always be defined in upper case.
  10. //
  11. #define LPT_REDIRECTION_PATH (LPWSTR)L"\\??\\COM"
  12. BOOLEAN
  13. REDIR::Redirect (
  14. IN PCPATH Device,
  15. IN PCPATH Destination
  16. )
  17. /*++
  18. Routine Description:
  19. Redirects a device. The device is redirected by creating a symbolic link
  20. to the destination device. If this is the first redirection of the device,
  21. the original symbolic link is saved in the registry under a volatile key
  22. so that it can be recovered latter on.
  23. Note that redirection requires sufficient privileges to create symbolic
  24. links and to create entries in the registry under SAVE_ROOT.
  25. Arguments:
  26. Device - Supplies the device to be redirected.
  27. Destination - Supplies the device to be redirected to
  28. Return Value:
  29. BOOLEAN - TRUE if the device was successfully redirected.
  30. FALSE otherwise.
  31. --*/
  32. {
  33. BOOLEAN Redirected = FALSE;
  34. PCWSTRING DeviceName;
  35. PCWSTRING DestinationName;
  36. DebugPtrAssert( Device );
  37. DebugPtrAssert( Destination );
  38. if( ( Device != NULL ) &&
  39. ( Destination != NULL ) &&
  40. ( ( DeviceName = Device->GetPathString() ) != NULL ) &&
  41. ( ( DestinationName = Destination->GetPathString() ) != NULL )
  42. ) {
  43. Redirected = DefineDosDevice( 0,
  44. DeviceName->GetWSTR(),
  45. DestinationName->GetWSTR() ) ? TRUE : FALSE;
  46. #if DBG
  47. if( !Redirected ) {
  48. DebugPrint( "MODE: DefineDosDevice() failed" );
  49. DebugPrintTrace(( "MODE: DefineDosDevice() failed, Device = %ls, Destination = %ls, Error = %d \n",
  50. DeviceName->GetWSTR(),
  51. DestinationName->GetWSTR(),
  52. GetLastError() ));
  53. }
  54. #endif
  55. }
  56. return Redirected;
  57. }
  58. BOOLEAN
  59. REDIR::IsRedirected (
  60. OUT PREDIR_STATUS Status,
  61. IN PCPATH Device,
  62. IN PCPATH Destination
  63. )
  64. /*++
  65. Routine Description:
  66. Determines if a device is being redirected to a specific device.
  67. Arguments:
  68. Status - Supplies pointer to redirection status. Only set in
  69. if the return value of this method is FALSE
  70. Device - Supplies the device about which we want to find out if
  71. it is redirected or not.
  72. Destination - Supplies a pointer to a destination device.
  73. Return Value:
  74. TRUE if the device is redirected to the destination
  75. FALSE otherwise
  76. --*/
  77. {
  78. BOOLEAN Redirected = FALSE;
  79. PCWSTRING DeviceName;
  80. PCWSTRING DestinationName;
  81. DSTRING DstRedir;
  82. FSTRING LptRedirectionPath;
  83. DSTRING TmpString;
  84. WCHAR buf[ 2*(MAX_PATH + 1) ];
  85. PWSTR pwstrTarget;
  86. DebugPtrAssert( Device );
  87. *Status = REDIR_STATUS_ERROR;
  88. if (NULL == (DeviceName = Device->GetPathString())) {
  89. return FALSE;
  90. }
  91. if (!QueryDosDevice(DeviceName->GetWSTR(),
  92. buf,
  93. sizeof(buf) / sizeof(WCHAR))) {
  94. // The device probably doesn't exist
  95. return FALSE;
  96. }
  97. *Status = REDIR_STATUS_NONEXISTENT;
  98. pwstrTarget = buf;
  99. // Find out if the LPT device is redirected to the destination.
  100. DstRedir.Initialize(pwstrTarget);
  101. return INVALID_CHNUM != DstRedir.Strstr(Destination->GetPathString());
  102. }
  103. BOOLEAN
  104. REDIR::IsRedirected (
  105. OUT PREDIR_STATUS Status,
  106. IN PCPATH Device
  107. )
  108. /*++
  109. Routine Description:
  110. Determines if a device is being redirected to any device.
  111. Arguments:
  112. Status - Supplies pointer to redirection status. Only set in
  113. if the return value of this method is FALSE
  114. Device - Supplies the device about which we want to find out if
  115. it is redirected or not.
  116. Return Value:
  117. TRUE if redirected, FALSE otherwise.
  118. --*/
  119. {
  120. BOOLEAN Redirected = FALSE;
  121. PCWSTRING DeviceName;
  122. PCWSTRING DestinationName;
  123. DSTRING DstRedir;
  124. FSTRING LptRedirectionPath;
  125. DSTRING TmpString;
  126. WCHAR Buffer[ 2*(MAX_PATH + 1) ];
  127. PWSTR Pointer;
  128. PPATH Destination = NULL;
  129. DebugPtrAssert( Device );
  130. *Status = REDIR_STATUS_ERROR;
  131. if( ( Device != NULL ) &&
  132. ( ( DeviceName = Device->GetPathString() ) != NULL ) ) {
  133. if( QueryDosDevice( DeviceName->GetWSTR(),
  134. Buffer,
  135. sizeof( Buffer ) / sizeof( WCHAR ) ) == 0 ) {
  136. //
  137. // The device probably doesn't exist
  138. //
  139. return( FALSE );
  140. }
  141. //
  142. // At this point we know that the device exists.
  143. // Assume that the device is not redirected.
  144. //
  145. *Status = REDIR_STATUS_NONEXISTENT;
  146. Pointer = Buffer;
  147. LptRedirectionPath.Initialize( LPT_REDIRECTION_PATH );
  148. //
  149. // Find out if the LPT device is redirected to a COM device
  150. //
  151. while( ( *Pointer != ( WCHAR )'\0' ) &&
  152. DstRedir.Initialize( Pointer ) &&
  153. ( DstRedir.Strupr() != NULL ) &&
  154. !Redirected ) {
  155. if( DstRedir.Strstr( &LptRedirectionPath ) != INVALID_CHNUM ) {
  156. //
  157. // The LPT device is redirected to a COM device
  158. //
  159. if( Destination != NULL ) {
  160. if( ( ( DestinationName = Destination->GetPathString() ) != NULL ) &&
  161. ( DstRedir.Strstr( DestinationName ) != INVALID_CHNUM ) ) {
  162. Redirected = TRUE;
  163. }
  164. } else {
  165. Redirected = TRUE;
  166. }
  167. }
  168. Pointer += DstRedir.QueryChCount() + 1;
  169. }
  170. }
  171. return Redirected;
  172. }
  173. BOOLEAN
  174. REDIR::EndRedirection (
  175. IN PCPATH Device
  176. )
  177. /*++
  178. Routine Description:
  179. Ends the redirection of a device
  180. Arguments:
  181. Device - Supplies the device
  182. Return Value:
  183. TRUE if the device's redirection has ended.
  184. FALSE otherwise
  185. --*/
  186. {
  187. BOOLEAN Done = FALSE;
  188. PCWSTRING DeviceName;
  189. REDIR_STATUS Status;
  190. DebugPtrAssert( Device );
  191. if( IsRedirected( &Status, Device ) ) {
  192. if( ( Device != NULL ) &&
  193. ( ( DeviceName = Device->GetPathString() ) != NULL ) ) {
  194. Done = DefineDosDevice( DDD_REMOVE_DEFINITION /* | DDD_RAW_TARGET_PATH */,
  195. DeviceName->GetWSTR(),
  196. NULL ) ? TRUE : FALSE;
  197. #if DBG
  198. if( !Done ) {
  199. DebugPrint( "MODE: DefineDosDevice() failed" );
  200. DebugPrintTrace(( "MODE: DefineDosDevice() failed, Device = %ls, Destination = %ls, Error = %d \n",
  201. DeviceName->GetWSTR(),
  202. LPT_REDIRECTION_PATH,
  203. GetLastError() ));
  204. }
  205. #endif
  206. }
  207. }
  208. return Done;
  209. }