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.

235 lines
6.2 KiB

  1. /*************************************************************************
  2. *
  3. * ATTACH.C
  4. *
  5. * NT Attach routines
  6. *
  7. * Copyright (c) 1995 Microsoft Corporation
  8. *
  9. * $Log: N:\NT\PRIVATE\NW4\NWSCRIPT\VCS\ATTACH.C $
  10. *
  11. * Rev 1.2 10 Apr 1996 14:21:30 terryt
  12. * Hotfix for 21181hq
  13. *
  14. * Rev 1.2 12 Mar 1996 19:52:08 terryt
  15. * Relative NDS names and merge
  16. *
  17. * Rev 1.1 22 Dec 1995 14:23:32 terryt
  18. * Add Microsoft headers
  19. *
  20. * Rev 1.0 15 Nov 1995 18:06:26 terryt
  21. * Initial revision.
  22. *
  23. * Rev 1.1 23 May 1995 19:36:30 terryt
  24. * Spruce up source
  25. *
  26. * Rev 1.0 15 May 1995 19:10:10 terryt
  27. * Initial revision.
  28. *
  29. *************************************************************************/
  30. #include <stdio.h>
  31. #include <direct.h>
  32. #include <time.h>
  33. #include <stdlib.h>
  34. #include <nt.h>
  35. #include <ntrtl.h>
  36. #include <nturtl.h>
  37. #include <windows.h>
  38. #include <nwapi32.h>
  39. #include <ntddnwfs.h>
  40. #include <nwapi.h>
  41. #include <npapi.h>
  42. #include "inc/common.h"
  43. #include "ntnw.h"
  44. /********************************************************************
  45. GetDefaultConnectionID
  46. Routine Description:
  47. Return the default connection ID ( the "preferred server" )
  48. Arguments:
  49. phNewConn - pointer to connection number
  50. Return Value:
  51. 0 = success
  52. else NetWare error number
  53. *******************************************************************/
  54. unsigned int
  55. GetDefaultConnectionID(
  56. unsigned int *phNewConn
  57. )
  58. {
  59. VERSION_INFO VerInfo;
  60. unsigned int Result;
  61. if ( fNDS )
  62. {
  63. Result = NTAttachToFileServer( NDSTREE, phNewConn );
  64. }
  65. else
  66. {
  67. //
  68. // "*" is the name for the preferred server
  69. //
  70. Result = NTAttachToFileServer( "*", phNewConn );
  71. if ( Result )
  72. return Result;
  73. Result = NWGetFileServerVersionInfo( (NWCONN_HANDLE)*phNewConn,
  74. &VerInfo );
  75. if ( Result )
  76. return Result;
  77. NWDetachFromFileServer( (NWCONN_HANDLE)*phNewConn );
  78. Result = NTAttachToFileServer( VerInfo.szName, phNewConn );
  79. }
  80. return Result;
  81. }
  82. /********************************************************************
  83. NTAttachToFileServer
  84. Routine Description:
  85. Given a server name, return a connection handle.
  86. We need our own because NWAPI32 does it's own mapping
  87. of errors.
  88. Arguments:
  89. pszServerName - Ascii server name
  90. phNewConn - pointer to connection handle
  91. Return Value:
  92. 0 = success
  93. else NetWare error number
  94. *******************************************************************/
  95. unsigned int
  96. NTAttachToFileServer(
  97. unsigned char *pszServerName,
  98. unsigned int *phNewConn
  99. )
  100. {
  101. return ( NWAttachToFileServer( pszServerName, 0,
  102. (NWCONN_HANDLE *)phNewConn ) );
  103. }
  104. /********************************************************************
  105. NTIsConnected
  106. Routine Description:
  107. Given a server name, is there already a connection to it?
  108. Arguments:
  109. pszServerName - ascii server name
  110. Return Value:
  111. TRUE - a connection to the server exists
  112. FALSE - a connection to the server does not exist
  113. *******************************************************************/
  114. unsigned int
  115. NTIsConnected( unsigned char * pszServerName )
  116. {
  117. LPBYTE Buffer ;
  118. DWORD dwErr ;
  119. HANDLE EnumHandle ;
  120. DWORD Count ;
  121. LPWSTR pszServerNameW;
  122. INT nSize;
  123. DWORD BufferSize = 4096;
  124. nSize = (strlen( pszServerName ) + 1 + 2) * sizeof( WCHAR );
  125. //
  126. // allocate memory and open the enumeration
  127. //
  128. if (!(pszServerNameW = LocalAlloc( LPTR, nSize ))) {
  129. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  130. return FALSE;
  131. }
  132. wcscpy( pszServerNameW, L"\\\\" );
  133. szToWide( pszServerNameW + 2, pszServerName, nSize );
  134. //
  135. // allocate memory and open the enumeration
  136. //
  137. if (!(Buffer = LocalAlloc( LPTR, BufferSize ))) {
  138. (void) LocalFree((HLOCAL) pszServerNameW) ;
  139. DisplayMessage(IDR_NOT_ENOUGH_MEMORY);
  140. return FALSE;
  141. }
  142. memset( Buffer, 0, BufferSize );
  143. dwErr = NPOpenEnum(RESOURCE_CONNECTED, 0, 0, NULL, &EnumHandle) ;
  144. if (dwErr != WN_SUCCESS) {
  145. (void) LocalFree((HLOCAL) pszServerNameW) ;
  146. (void) LocalFree((HLOCAL) Buffer) ;
  147. return FALSE;
  148. }
  149. do {
  150. Count = 0xFFFFFFFF ;
  151. BufferSize = 4096;
  152. dwErr = NwEnumConnections(EnumHandle, &Count, Buffer, &BufferSize, TRUE) ;
  153. if ((dwErr == WN_SUCCESS || dwErr == WN_NO_MORE_ENTRIES)
  154. && ( Count != 0xFFFFFFFF) )
  155. {
  156. LPNETRESOURCE lpNetResource ;
  157. DWORD i ;
  158. DWORD ServerLen;
  159. ServerLen = wcslen( pszServerNameW );
  160. lpNetResource = (LPNETRESOURCE) Buffer ;
  161. //
  162. // search for our server
  163. //
  164. for ( i = 0; i < Count; lpNetResource++, i++ )
  165. {
  166. if ( lpNetResource->lpProvider )
  167. if ( _wcsicmp( lpNetResource->lpProvider, NW_PROVIDER ) ) {
  168. continue;
  169. }
  170. if ( lpNetResource->lpRemoteName ) {
  171. if ( wcslen(lpNetResource->lpRemoteName) > ServerLen ) {
  172. if ( lpNetResource->lpRemoteName[ServerLen] == L'\\' )
  173. lpNetResource->lpRemoteName[ServerLen] = L'\0';
  174. }
  175. if ( !_wcsicmp(lpNetResource->lpRemoteName, pszServerNameW )) {
  176. (void) WNetCloseEnum(EnumHandle) ;
  177. (void) LocalFree((HLOCAL) pszServerNameW) ;
  178. (void) LocalFree((HLOCAL) Buffer) ;
  179. return TRUE;
  180. }
  181. }
  182. }
  183. }
  184. } while (dwErr == WN_SUCCESS) ;
  185. (void ) WNetCloseEnum(EnumHandle) ;
  186. (void) LocalFree((HLOCAL) pszServerNameW) ;
  187. (void) LocalFree((HLOCAL) Buffer) ;
  188. return FALSE;
  189. }