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.

374 lines
8.7 KiB

  1. /*++
  2. Copyright (C) 1993 Microsoft Corporation
  3. Module Name:
  4. NWATTACH.C
  5. Abstract:
  6. This module contains the NetWare(R) SDK support to routines
  7. into the NetWare redirector
  8. Author:
  9. Chris Sandys (a-chrisa) 09-Sep-1993
  10. Revision History:
  11. Chuck Y. Chan (chuckc) 02/06/94 Moved to NWCS. Make it more NT like.
  12. Chuck Y. Chan (chuckc) 02/27/94 Clear out old code.
  13. Make logout work.
  14. Check for error in many places.
  15. Dont hard code strings.
  16. Remove non compatible parameters.
  17. Lotsa other cleanup.
  18. Felix Wong (t-felixw) 09/16/96 Moved functions for Win95 port.
  19. --*/
  20. #include "procs.h"
  21. #include "nwapi32.h"
  22. #include <stdio.h>
  23. //
  24. // Define structure for internal use. Our handle passed back from attach to
  25. // file server will be pointer to this. We keep server string around for
  26. // discnnecting from the server on logout. The structure is freed on detach.
  27. // Callers should not use this structure but treat pointer as opaque handle.
  28. //
  29. typedef struct _NWC_SERVER_INFO {
  30. HANDLE hConn ;
  31. UNICODE_STRING ServerString ;
  32. } NWC_SERVER_INFO, *PNWC_SERVER_INFO ;
  33. //
  34. // define define categories of errors
  35. //
  36. typedef enum _NCP_CLASS {
  37. NcpClassConnect,
  38. NcpClassBindery,
  39. NcpClassDir
  40. } NCP_CLASS ;
  41. //
  42. // define error mapping structure
  43. //
  44. typedef struct _NTSTATUS_TO_NCP {
  45. NTSTATUS NtStatus ;
  46. NWCCODE NcpCode ;
  47. } NTSTATUS_TO_NCP, *LPNTSTATUS_TO_NCP ;
  48. NWCCODE
  49. MapNtStatus(
  50. const NTSTATUS ntstatus,
  51. const NCP_CLASS ncpclass
  52. );
  53. DWORD
  54. SetWin32ErrorFromNtStatus(
  55. NTSTATUS NtStatus
  56. ) ;
  57. DWORD
  58. szToWide(
  59. LPWSTR lpszW,
  60. LPCSTR lpszC,
  61. INT nSize
  62. );
  63. //
  64. // Forwards
  65. //
  66. NTSTATUS
  67. NwAttachToServer(
  68. LPWSTR ServerName,
  69. LPHANDLE phandleServer
  70. );
  71. NTSTATUS
  72. NwDetachFromServer(
  73. HANDLE handleServer
  74. );
  75. NWCCODE NWAPI DLLEXPORT
  76. NWAttachToFileServer(
  77. const char NWFAR *pszServerName,
  78. NWLOCAL_SCOPE ScopeFlag,
  79. NWCONN_HANDLE NWFAR *phNewConn
  80. )
  81. {
  82. DWORD dwRes;
  83. NWCCODE nwRes;
  84. LPWSTR lpwszServerName; // Pointer to buffer for WIDE servername
  85. int nSize;
  86. PNWC_SERVER_INFO pServerInfo ;
  87. //
  88. // check parameters and init return result to be null.
  89. //
  90. if (!pszServerName || !phNewConn)
  91. return INVALID_CONNECTION ;
  92. *phNewConn = NULL ;
  93. //
  94. // Allocate a buffer for wide server name
  95. //
  96. nSize = strlen(pszServerName)+1 ;
  97. if(!(lpwszServerName = (LPWSTR) LocalAlloc(
  98. LPTR,
  99. nSize * sizeof(WCHAR) )))
  100. {
  101. nwRes = REQUESTER_ERROR ;
  102. goto ExitPoint ;
  103. }
  104. if (szToWide( lpwszServerName, pszServerName, nSize ) != NO_ERROR)
  105. {
  106. nwRes = REQUESTER_ERROR ;
  107. goto ExitPoint ;
  108. }
  109. //
  110. // Call createfile to get a handle for the redirector calls
  111. //
  112. nwRes = NWAttachToFileServerW( lpwszServerName,
  113. ScopeFlag,
  114. phNewConn );
  115. ExitPoint:
  116. //
  117. // Free the memory allocated above before exiting
  118. //
  119. if (lpwszServerName)
  120. (void) LocalFree( (HLOCAL) lpwszServerName );
  121. //
  122. // Return with NWCCODE
  123. //
  124. return( nwRes );
  125. }
  126. NWCCODE NWAPI DLLEXPORT
  127. NWAttachToFileServerW(
  128. const WCHAR NWFAR *pszServerName,
  129. NWLOCAL_SCOPE ScopeFlag,
  130. NWCONN_HANDLE NWFAR *phNewConn
  131. )
  132. {
  133. DWORD NtStatus;
  134. NWCCODE nwRes;
  135. LPWSTR lpwszServerName; // Pointer to buffer for WIDE servername
  136. int nSize;
  137. PNWC_SERVER_INFO pServerInfo = NULL;
  138. UNREFERENCED_PARAMETER(ScopeFlag) ;
  139. //
  140. // check parameters and init return result to be null.
  141. //
  142. if (!pszServerName || !phNewConn)
  143. return INVALID_CONNECTION ;
  144. *phNewConn = NULL ;
  145. //
  146. // Allocate a buffer to store the file server name
  147. //
  148. nSize = wcslen(pszServerName)+3 ;
  149. if(!(lpwszServerName = (LPWSTR) LocalAlloc(
  150. LPTR,
  151. nSize * sizeof(WCHAR) )))
  152. {
  153. nwRes = REQUESTER_ERROR ;
  154. goto ExitPoint ;
  155. }
  156. wcscpy( lpwszServerName, L"\\\\" );
  157. wcscat( lpwszServerName, pszServerName );
  158. //
  159. // Allocate a buffer for the server info (handle + name pointer). Also
  160. // init the unicode string.
  161. //
  162. if( !(pServerInfo = (PNWC_SERVER_INFO) LocalAlloc(
  163. LPTR,
  164. sizeof(NWC_SERVER_INFO))) )
  165. {
  166. nwRes = REQUESTER_ERROR ;
  167. goto ExitPoint ;
  168. }
  169. RtlInitUnicodeString(&pServerInfo->ServerString, lpwszServerName) ;
  170. //
  171. // Call createfile to get a handle for the redirector calls
  172. //
  173. NtStatus = NwAttachToServer( lpwszServerName, &pServerInfo->hConn );
  174. if(NT_SUCCESS(NtStatus))
  175. {
  176. nwRes = SUCCESSFUL;
  177. }
  178. else
  179. {
  180. (void) SetWin32ErrorFromNtStatus( NtStatus );
  181. nwRes = MapNtStatus( NtStatus, NcpClassConnect );
  182. }
  183. ExitPoint:
  184. //
  185. // Free the memory allocated above before exiting
  186. //
  187. if (nwRes != SUCCESSFUL)
  188. {
  189. if (lpwszServerName)
  190. (void) LocalFree( (HLOCAL) lpwszServerName );
  191. if (pServerInfo)
  192. (void) LocalFree( (HLOCAL) pServerInfo );
  193. }
  194. else
  195. *phNewConn = (HANDLE) pServerInfo ;
  196. //
  197. // Return with NWCCODE
  198. //
  199. return( nwRes );
  200. }
  201. NWCCODE NWAPI DLLEXPORT
  202. NWDetachFromFileServer(
  203. NWCONN_HANDLE hConn
  204. )
  205. {
  206. PNWC_SERVER_INFO pServerInfo = (PNWC_SERVER_INFO)hConn ;
  207. (void) NwDetachFromServer( pServerInfo->hConn );
  208. (void) LocalFree (pServerInfo->ServerString.Buffer) ;
  209. //
  210. // catch any body that still trirs to use this puppy...
  211. //
  212. pServerInfo->ServerString.Buffer = NULL ;
  213. pServerInfo->hConn = NULL ;
  214. (void) LocalFree (pServerInfo) ;
  215. return SUCCESSFUL;
  216. }
  217. //
  218. // worker routines
  219. //
  220. #define NW_RDR_SERVER_PREFIX L"\\Device\\Nwrdr\\"
  221. NTSTATUS
  222. NwAttachToServer(
  223. IN LPWSTR ServerName,
  224. OUT LPHANDLE phandleServer
  225. )
  226. /*++
  227. Routine Description:
  228. This routine opens a handle to the given server.
  229. Arguments:
  230. ServerName - The server name to attach to.
  231. phandleServer - Receives an opened handle to the preferred or
  232. nearest server.
  233. Return Value:
  234. 0 or reason for failure.
  235. --*/
  236. {
  237. NTSTATUS ntstatus = STATUS_SUCCESS;
  238. IO_STATUS_BLOCK IoStatusBlock;
  239. OBJECT_ATTRIBUTES ObjectAttributes;
  240. LPWSTR FullName;
  241. UNICODE_STRING UServerName;
  242. FullName = (LPWSTR) LocalAlloc( LMEM_ZEROINIT,
  243. (UINT) ( wcslen( NW_RDR_SERVER_PREFIX) +
  244. wcslen( ServerName ) - 1) *
  245. sizeof(WCHAR)
  246. );
  247. if ( FullName == NULL ) {
  248. return STATUS_INSUFFICIENT_RESOURCES ;
  249. }
  250. wcscpy( FullName, NW_RDR_SERVER_PREFIX );
  251. wcscat( FullName, ServerName + 2 ); // Skip past the prefix "\\"
  252. RtlInitUnicodeString( &UServerName, FullName );
  253. InitializeObjectAttributes(
  254. &ObjectAttributes,
  255. &UServerName,
  256. OBJ_CASE_INSENSITIVE,
  257. NULL,
  258. NULL
  259. );
  260. //
  261. // Open a handle to the preferred server.
  262. //
  263. ntstatus = NtOpenFile(
  264. phandleServer,
  265. SYNCHRONIZE | FILE_LIST_DIRECTORY,
  266. &ObjectAttributes,
  267. &IoStatusBlock,
  268. FILE_SHARE_VALID_FLAGS,
  269. FILE_SYNCHRONOUS_IO_NONALERT
  270. );
  271. if ( NT_SUCCESS(ntstatus)) {
  272. ntstatus = IoStatusBlock.Status;
  273. }
  274. if (! NT_SUCCESS(ntstatus)) {
  275. *phandleServer = NULL;
  276. }
  277. LocalFree( FullName );
  278. return (ntstatus);
  279. }
  280. NTSTATUS
  281. NwDetachFromServer(
  282. IN HANDLE handleServer
  283. )
  284. /*++
  285. Routine Description:
  286. This routine closes a handle to the given server.
  287. Arguments:
  288. handleServer - Supplies a open handle to be closed.
  289. Return Value:
  290. NO_ERROR or reason for failure.
  291. --*/
  292. {
  293. NTSTATUS ntstatus = NtClose( handleServer );
  294. return (ntstatus);
  295. };