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.

259 lines
5.1 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. /*++
  4. Copyright (c) 1991 Microsoft Corporation
  5. Module Name:
  6. netcon.c
  7. Abstract:
  8. This file has the Wnet network connection routines
  9. Author:
  10. Sunil Pai (sunilp) July 1991
  11. --*/
  12. typedef DWORD (APIENTRY *PFWNETPROC)();
  13. #define DRIVE_LETTER_RANGE ('Z'+1-'A')
  14. LPSTR apUncPath [ DRIVE_LETTER_RANGE ] =
  15. {
  16. NULL, NULL, NULL, NULL, NULL, NULL,
  17. NULL, NULL, NULL, NULL, NULL, NULL,
  18. NULL, NULL, NULL, NULL, NULL, NULL,
  19. NULL, NULL, NULL, NULL, NULL, NULL,
  20. NULL, NULL
  21. };
  22. VOID
  23. DeleteAllConnectionsWorker(
  24. VOID
  25. )
  26. {
  27. CHAR chDrive[3] = "A:" ;
  28. INT i ;
  29. for ( i = 0 ; i < DRIVE_LETTER_RANGE ; i++ )
  30. {
  31. if ( apUncPath[i] )
  32. {
  33. // Delete the connection. This automatically cleans up
  34. // the UNC path table.
  35. chDrive[0] = 'A' + i ;
  36. DeleteNetConnectionWorker( chDrive, "TRUE" ) ;
  37. }
  38. }
  39. }
  40. //
  41. // Check to see if a connection is already known to the given UNC name.
  42. // Return the drive letter if so or zero if not.
  43. //
  44. CHAR CheckNetConnection(
  45. LPSTR szUNCName
  46. )
  47. {
  48. INT i ;
  49. for ( i = 0 ; i < DRIVE_LETTER_RANGE ; i++ )
  50. {
  51. if ( apUncPath[i] )
  52. {
  53. if ( _strcmpi( szUNCName, apUncPath[i] ) == 0 )
  54. return 'A'+i ;
  55. }
  56. }
  57. return 0 ;
  58. }
  59. BOOL
  60. GetMprProcAddr(
  61. LPSTR szProcName,
  62. PFWNETPROC * ppFunc
  63. )
  64. {
  65. static
  66. HMODULE WNetModule = NULL ;
  67. //
  68. // Load the wnet dll if necessary (first cycle)
  69. //
  70. if ( WNetModule == NULL )
  71. {
  72. if((WNetModule = LoadLibrary("mpr.dll")) == NULL) {
  73. SetErrorText(IDS_ERROR_NONETWORK);
  74. return(FALSE);
  75. }
  76. }
  77. //
  78. // Get the addresses of the WNetAddConnection entry point
  79. //
  80. *ppFunc = (PFWNETPROC)GetProcAddress( WNetModule,
  81. szProcName
  82. );
  83. if( *ppFunc == NULL ) {
  84. SetErrorText(IDS_ERROR_NONETWORK);
  85. return(FALSE);
  86. }
  87. return TRUE ;
  88. }
  89. // arg0 = Remote name
  90. // arg1 = Password
  91. // arg2 = Local Name
  92. BOOL
  93. AddNetConnectionWorker(
  94. LPSTR szUNCName,
  95. LPSTR szPassword,
  96. LPSTR szLocalName
  97. )
  98. {
  99. DWORD dwStatus;
  100. PFWNETPROC PFWNetAddConnection2;
  101. NETRESOURCE NetResource;
  102. LPSTR szUNCSave ;
  103. INT i ;
  104. if ( ! GetMprProcAddr( "WNetAddConnection2A",
  105. & PFWNetAddConnection2 ) )
  106. {
  107. return FALSE ;
  108. }
  109. //
  110. // Build the netresource structure and call the function
  111. //
  112. NetResource.dwScope = 0;
  113. NetResource.dwType = RESOURCETYPE_DISK;
  114. NetResource.dwUsage = 0;
  115. NetResource.lpLocalName = szLocalName;
  116. NetResource.lpRemoteName = szUNCName;
  117. NetResource.lpComment = NULL;
  118. NetResource.lpProvider = NULL;
  119. dwStatus = PFWNetAddConnection2(
  120. &NetResource,
  121. szPassword,
  122. NULL,
  123. 0
  124. );
  125. switch (dwStatus) {
  126. case WN_SUCCESS:
  127. // Success: create an entry in the UNC mapping table
  128. i = toupper( szLocalName[0] ) - 'A' ;
  129. if ( i >= 0
  130. && i < DRIVE_LETTER_RANGE
  131. && szLocalName[1] == ':'
  132. && (szUNCSave = SAlloc( strlen( szUNCName ) + 1 )) )
  133. {
  134. strcpy( szUNCSave, szUNCName ) ;
  135. apUncPath[i] = szUNCSave ;
  136. }
  137. return ( TRUE );
  138. case WN_BAD_NETNAME:
  139. SetErrorText(IDS_ERROR_BADNETNAME);
  140. break;
  141. case WN_BAD_LOCALNAME:
  142. SetErrorText(IDS_ERROR_BADLOCALNAME);
  143. break;
  144. case WN_BAD_PASSWORD:
  145. SetErrorText(IDS_ERROR_BADPASSWORD);
  146. break;
  147. case WN_ALREADY_CONNECTED:
  148. SetErrorText(IDS_ERROR_ALREADYCONNECTED);
  149. break;
  150. case WN_ACCESS_DENIED:
  151. SetErrorText(IDS_ERROR_ACCESSDENIED);
  152. break;
  153. case WN_NO_NETWORK:
  154. default:
  155. SetErrorText(IDS_ERROR_NONETWORK);
  156. break;
  157. }
  158. return ( FALSE );
  159. }
  160. //
  161. // Arg[0]: Local Name
  162. // Arg[1]: Force closure -- "TRUE" | "FALSE"
  163. //
  164. BOOL
  165. DeleteNetConnectionWorker(
  166. LPSTR szLocalName,
  167. LPSTR szForceClosure
  168. )
  169. {
  170. DWORD dwStatus;
  171. PFWNETPROC PFWNetCancelConnection;
  172. INT i ;
  173. if ( ! GetMprProcAddr( "WNetCancelConnectionA",
  174. & PFWNetCancelConnection ) )
  175. {
  176. return FALSE ;
  177. }
  178. // Remove the UNC path data from the table regardless of
  179. // the result of the connection cancellation.
  180. i = toupper( szLocalName[0] ) - 'A' ;
  181. if ( i >= 0
  182. && i < DRIVE_LETTER_RANGE
  183. && szLocalName[1] == ':'
  184. && apUncPath[i] != NULL )
  185. {
  186. SFree( apUncPath[i] ) ;
  187. apUncPath[i] = NULL ;
  188. }
  189. dwStatus = PFWNetCancelConnection(szLocalName, !lstrcmpi(szForceClosure, "TRUE"));
  190. switch (dwStatus) {
  191. case WN_SUCCESS:
  192. return ( TRUE );
  193. case WN_OPEN_FILES:
  194. SetErrorText(IDS_ERROR_NETOPENFILES);
  195. break;
  196. case WN_NOT_CONNECTED:
  197. default:
  198. SetErrorText(IDS_ERROR_NOTCONNECTED);
  199. break;
  200. }
  201. return ( FALSE );
  202. }