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.

282 lines
8.6 KiB

  1. #include "StdAfx.h"
  2. #include "WNetUtil.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. #define VALD_NETDOMAIN_ENUM 1000666 // bad pointer validation signature
  9. #define DOM_BUFFER_SIZE (32768) // buffer size held allocated by class
  10. class TNetDomainEnum
  11. {
  12. public:
  13. TNetDomainEnum();
  14. ~TNetDomainEnum();
  15. DWORD GetMsNetProvider( NETRESOURCE * resourceInfo, DWORD infoSize );
  16. WCHAR * GetNext();
  17. DWORD GetLastRc() const { return rc; };
  18. BOOL IsValid() const { return vald == VALD_NETDOMAIN_ENUM; };
  19. private:
  20. DWORD vald;
  21. HANDLE hEnum;
  22. NETRESOURCE * resourceBuffer;
  23. DWORD rc,
  24. totEntries,
  25. nextEntry,
  26. buffSize;
  27. };
  28. //-----------------------------------------------------------------------------
  29. // TNetDomainEnum::TNetDomainEnum
  30. //-----------------------------------------------------------------------------
  31. TNetDomainEnum::TNetDomainEnum()
  32. {
  33. //--------------------------
  34. // Initialize Class Members
  35. //--------------------------
  36. vald = VALD_NETDOMAIN_ENUM;
  37. hEnum = INVALID_HANDLE_VALUE;
  38. // init currEntry > totEntries to force first-time read
  39. totEntries = 0;
  40. nextEntry = 1;
  41. resourceBuffer = NULL;
  42. resourceBuffer = (NETRESOURCE *)new char[DOM_BUFFER_SIZE];
  43. if ( !resourceBuffer )
  44. {
  45. rc = 1;
  46. }
  47. else
  48. {
  49. //-----------------------------------
  50. // Determine Network Provider to Use
  51. //-----------------------------------
  52. char buffer[16384];
  53. NETRESOURCE * info = (NETRESOURCE *)buffer;
  54. rc = GetMsNetProvider( info, sizeof(buffer));
  55. if ( ! rc )
  56. {
  57. rc = WNetOpenEnum( RESOURCE_GLOBALNET,
  58. RESOURCETYPE_ANY,
  59. RESOURCEUSAGE_CONTAINER,
  60. info,
  61. &hEnum );
  62. delete [] info->lpProvider;
  63. }
  64. if ( rc )
  65. {
  66. if ( resourceBuffer )
  67. {
  68. delete [] resourceBuffer;
  69. resourceBuffer = NULL;
  70. }
  71. }
  72. }
  73. }
  74. //-----------------------------------------------------------------------------
  75. // TNetDomainEnum::~TNetDomainEnum
  76. //-----------------------------------------------------------------------------
  77. TNetDomainEnum::~TNetDomainEnum()
  78. {
  79. if ( hEnum != INVALID_HANDLE_VALUE )
  80. {
  81. WNetCloseEnum( hEnum );
  82. hEnum = INVALID_HANDLE_VALUE;
  83. }
  84. vald = 0;
  85. if ( resourceBuffer )
  86. {
  87. delete resourceBuffer;
  88. }
  89. }
  90. //-----------------------------------------------------------------------------
  91. // GetMsNetProvider - Retrieves network resource information for the 'Microsoft
  92. // Windows Network' provider.
  93. //
  94. // Input: A pointer to a NETRESOURCE structure that we will fill if we find a
  95. // resource meeting our needs.
  96. //
  97. // Output: 0 of we found a provider. resourceInfo populated in this case
  98. // non-zero if no provider. resourceInfo contents undefined
  99. //-----------------------------------------------------------------------------
  100. DWORD TNetDomainEnum::GetMsNetProvider( NETRESOURCE * resourceInfo, DWORD infoSize )
  101. {
  102. _TCHAR szProvider[_MAX_PATH];
  103. DWORD cchProvider = sizeof(szProvider) / sizeof(szProvider[0]);
  104. DWORD dwError = WNetGetProviderName(WNNC_NET_LANMAN, szProvider, &cchProvider);
  105. if (dwError == NO_ERROR)
  106. {
  107. memset(resourceInfo, 0, infoSize);
  108. resourceInfo->dwScope = RESOURCE_GLOBALNET;
  109. resourceInfo->dwType = RESOURCETYPE_ANY;
  110. resourceInfo->dwDisplayType = RESOURCEDISPLAYTYPE_NETWORK;
  111. resourceInfo->dwUsage = RESOURCEUSAGE_CONTAINER;
  112. resourceInfo->lpProvider = new _TCHAR[_tcslen(szProvider) + 1];
  113. if (resourceInfo->lpProvider)
  114. {
  115. _tcscpy(resourceInfo->lpProvider, szProvider);
  116. rc = NO_ERROR;
  117. }
  118. else
  119. {
  120. rc = ERROR_OUTOFMEMORY;
  121. }
  122. }
  123. else
  124. {
  125. rc = dwError;
  126. }
  127. return rc;
  128. }
  129. //-----------------------------------------------------------------------------
  130. // TNetDomainEnum::GetNext()
  131. //-----------------------------------------------------------------------------
  132. WCHAR *
  133. TNetDomainEnum::GetNext()
  134. {
  135. rc = (DWORD)-1; // init rc to internal error before reset
  136. if ( hEnum == INVALID_HANDLE_VALUE )
  137. return NULL;
  138. if ( !resourceBuffer )
  139. return NULL;
  140. if ( nextEntry >= totEntries )
  141. {
  142. buffSize = DOM_BUFFER_SIZE;
  143. totEntries = (DWORD)-1;
  144. rc = WNetEnumResource(
  145. hEnum,
  146. &totEntries,
  147. (void *)resourceBuffer,
  148. &buffSize );
  149. if ( rc == 0 )
  150. nextEntry = 0;
  151. else
  152. {
  153. totEntries = 0;
  154. return NULL;
  155. }
  156. }
  157. else
  158. rc = 0;
  159. return resourceBuffer[nextEntry++].lpRemoteName;
  160. }
  161. //#pragma page()
  162. /*============================================================================*\
  163. Windows Network Domain Enumeration APIs. These are a shell around the
  164. TNetDomainEnum class member function. The handle used is nothing more
  165. than the "this" pointer to the instantiated object.
  166. \*============================================================================*/
  167. //-----------------------------------------------------------------------------
  168. // EaWNetDomainEnumOpen
  169. //
  170. // Creates the enumeration object and gives the caller the handle
  171. //-----------------------------------------------------------------------------
  172. DWORD _stdcall // ret-0 or error code
  173. EaWNetDomainEnumOpen(
  174. void ** handle // out-opaque handle addr to enum
  175. )
  176. {
  177. TNetDomainEnum * domainEnum = new TNetDomainEnum();
  178. *handle = (PVOID)domainEnum;
  179. if ( ! domainEnum )
  180. return (DWORD)-1; // internal error
  181. return domainEnum->GetLastRc();
  182. }
  183. //-----------------------------------------------------------------------------
  184. // EaWNetDomainEnumNext
  185. //
  186. // Sets the domain string buffer to the next domain name in the enumeration
  187. //-----------------------------------------------------------------------------
  188. DWORD _stdcall // ret-0 or error code
  189. EaWNetDomainEnumNext(
  190. void * handle ,// i/o-opaque handle to enumeration
  191. EaWNetDomainInfo * domain // out-domain information structure
  192. )
  193. {
  194. TNetDomainEnum * domainEnum = (TNetDomainEnum *)handle;
  195. WCHAR * str;
  196. if ( !domainEnum || !domainEnum->IsValid() )
  197. return ERROR_INVALID_PARAMETER; // caller's error - invalid handle
  198. str = domainEnum->GetNext();
  199. if ( !str )
  200. {
  201. domain->name[0] = _T('\0');
  202. }
  203. else
  204. {
  205. wcsncpy(domain->name, str, EA_MAX_DOMAIN_NAME_SIZE);
  206. domain->name[EA_MAX_DOMAIN_NAME_SIZE - 1] = _T('\0');
  207. }
  208. return domainEnum->GetLastRc();
  209. }
  210. //-----------------------------------------------------------------------------
  211. // EaWNetDomainEnumFirst
  212. //
  213. // Sets the domain string buffer to the first domain name in the enumeration
  214. //-----------------------------------------------------------------------------
  215. DWORD _stdcall // ret-0 or error code
  216. EaWNetDomainEnumFirst(
  217. void * handle ,// i/o-opaque handle to enumeration
  218. EaWNetDomainInfo * domain // out-domain information structure
  219. )
  220. {
  221. // We're cheating here by making the first/next the same. We probably want to
  222. // change this eventually to make "first" really properly reset the enum to the
  223. // start
  224. return EaWNetDomainEnumNext(handle, domain);
  225. }
  226. //-----------------------------------------------------------------------------
  227. // EaWNetDomainEnumClose
  228. //
  229. // Closes and destroys the enumeration handle and the objects it contains
  230. //-----------------------------------------------------------------------------
  231. DWORD _stdcall // ret-0 or error code
  232. EaWNetDomainEnumClose(
  233. void * handle // i/o-opaque handle addr to enum
  234. )
  235. {
  236. TNetDomainEnum * domainEnum = (TNetDomainEnum *)handle;
  237. if ( !domainEnum || !domainEnum->IsValid() )
  238. return ERROR_INVALID_PARAMETER; // caller's error - invalid handle
  239. delete domainEnum;
  240. return 0;
  241. }