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.

302 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. csc.c
  5. Abstract:
  6. These are the browser service API RPC client stubs for CSC
  7. --*/
  8. #include <nt.h>
  9. #include <ntrtl.h>
  10. #include <nturtl.h>
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include <rpcutil.h>
  17. #include <lmcons.h>
  18. #include <lmerr.h>
  19. #include <lmapibuf.h>
  20. #include <lmserver.h>
  21. #include "cscp.h"
  22. static FARPROC pCSCFindFirstFile = NULL;
  23. static FARPROC pCSCFindNextFile = NULL;
  24. static FARPROC pCSCFindClose = NULL;
  25. static FARPROC pCSCIsServerOffline = NULL;
  26. BrowserGetCSCEntryPoints()
  27. {
  28. HANDLE hMod;
  29. if( pCSCFindFirstFile == NULL ) {
  30. //
  31. // Get the entry points in reverse order for multithread protection
  32. //
  33. hMod = LoadLibrary(L"cscdll.dll");
  34. if( hMod == NULL ) {
  35. return 0;
  36. }
  37. pCSCFindClose = GetProcAddress(hMod,"CSCFindClose");
  38. if( pCSCFindClose == NULL ) {
  39. return 0;
  40. }
  41. pCSCFindNextFile = GetProcAddress(hMod,"CSCFindNextFileW" );
  42. if( pCSCFindNextFile == NULL ) {
  43. return 0;
  44. }
  45. pCSCIsServerOffline = GetProcAddress(hMod, "CSCIsServerOfflineW" );
  46. if( pCSCIsServerOffline == NULL ) {
  47. return 0;
  48. }
  49. pCSCFindFirstFile = GetProcAddress(hMod,"CSCFindFirstFileW" );
  50. }
  51. return pCSCFindFirstFile != 0;
  52. }
  53. BOOLEAN NET_API_FUNCTION
  54. CSCIsOffline()
  55. {
  56. BOOL isOffline;
  57. if( BrowserGetCSCEntryPoints() &&
  58. pCSCIsServerOffline( NULL, &isOffline ) &&
  59. isOffline == TRUE ) {
  60. return TRUE;
  61. }
  62. return FALSE;
  63. }
  64. NET_API_STATUS NET_API_FUNCTION
  65. CSCNetServerEnumEx(
  66. IN DWORD level,
  67. OUT LPBYTE *bufptr,
  68. IN DWORD prefmaxlen,
  69. OUT LPDWORD entriesread,
  70. OUT LPDWORD totalentries
  71. )
  72. /*++
  73. Arguments:
  74. level - Supplies the requested level of information.
  75. bufptr - Returns a pointer to a buffer which contains the
  76. requested transport information.
  77. prefmaxlen - Supplies the number of bytes of information to return in the buffer.
  78. Ignored for this case.
  79. entriesread - Returns the number of entries read into the buffer.
  80. totalentries - Returns the total number of entries available.
  81. --*/
  82. {
  83. HANDLE hFind = INVALID_HANDLE_VALUE;
  84. WIN32_FIND_DATAW sFind32;
  85. DWORD dwError, dwStatus, dwPinCount, dwHintFlags;
  86. FILETIME ftOrgTime;
  87. NET_API_STATUS apiStatus;
  88. LPWSTR server, share;
  89. PBYTE outbuf = NULL, endp;
  90. DWORD count, numFound, serverlen;
  91. try {
  92. count = 1024;
  93. retry:
  94. numFound = 0;
  95. //
  96. // Allocate space for the results
  97. //
  98. if( outbuf != NULL ) {
  99. NetApiBufferFree( outbuf );
  100. outbuf = NULL;
  101. count *= 2;
  102. }
  103. apiStatus = NetApiBufferAllocate( count, &outbuf );
  104. if( apiStatus != NO_ERROR ) {
  105. goto try_exit;
  106. }
  107. endp = outbuf + count;
  108. RtlZeroMemory( outbuf, count );
  109. //
  110. // See if we can enumerate the cached servers and shares
  111. //
  112. if( hFind != INVALID_HANDLE_VALUE ) {
  113. pCSCFindClose( hFind );
  114. hFind = INVALID_HANDLE_VALUE;
  115. }
  116. hFind = (HANDLE)pCSCFindFirstFile( NULL,
  117. &sFind32,
  118. &dwStatus,
  119. &dwPinCount,
  120. &dwHintFlags,
  121. &ftOrgTime
  122. );
  123. if( hFind == INVALID_HANDLE_VALUE ) {
  124. NetApiBufferFree( outbuf );
  125. apiStatus = ERROR_NOT_SUPPORTED;
  126. goto try_exit;
  127. }
  128. do {
  129. //
  130. // For each entry, take a look to see if it's one that we want. If
  131. // it is one, pack the results into the output buffer. If the output
  132. // buffer is too small, grow the buffer and start over again.
  133. //
  134. //
  135. // The name returned should be \\server\sharename
  136. //
  137. if( sFind32.cFileName[0] != L'\\' || sFind32.cFileName[1] != L'\\' ||
  138. sFind32.cFileName[2] == L'\0' ) {
  139. //
  140. // We got a strange server name entry
  141. //
  142. continue;
  143. }
  144. server = &sFind32.cFileName[2];
  145. for( share = server; *share && *share != '\\'; share++ );
  146. if( share[0] != '\\' ) {
  147. //
  148. // No share component?
  149. //
  150. continue;
  151. }
  152. //
  153. // NULL terminate the servername
  154. //
  155. *share++ = L'\0';
  156. serverlen = (DWORD)(share - server) * sizeof( WCHAR ) ;
  157. //
  158. // We've found a server entry!
  159. //
  160. if( level == 0 ) {
  161. PSERVER_INFO_100 s100 = (PSERVER_INFO_100)outbuf + numFound;
  162. PSERVER_INFO_100 s;
  163. if( (PBYTE)(endp - serverlen) < (PBYTE)(s100 + sizeof( s100 )) ) {
  164. goto retry;
  165. }
  166. //
  167. // If we've already gotten this server, skip it
  168. //
  169. for( s = (PSERVER_INFO_100)outbuf; s < s100; s++ ) {
  170. if( !lstrcmpiW( s->sv100_name, server ) ) {
  171. break;
  172. }
  173. }
  174. if( s != s100 ) {
  175. continue;
  176. }
  177. endp -= serverlen;
  178. RtlCopyMemory( endp, server, serverlen );
  179. s100->sv100_name = (LPWSTR)endp;
  180. s100->sv100_platform_id = SV_PLATFORM_ID_NT;
  181. } else {
  182. PSERVER_INFO_101 s101 = (PSERVER_INFO_101)outbuf + numFound;
  183. PSERVER_INFO_101 s;
  184. if( (PBYTE)(endp - serverlen) < (PBYTE)(s101 + sizeof( s101 )) ) {
  185. goto retry;
  186. }
  187. //
  188. // If we've already gotten this server, skip it
  189. //
  190. for( s = (PSERVER_INFO_101)outbuf; s < s101; s++ ) {
  191. if( !lstrcmpiW( s->sv101_name, server ) ) {
  192. break;
  193. }
  194. }
  195. if( s != s101 ) {
  196. continue;
  197. }
  198. endp -= serverlen;
  199. RtlCopyMemory( endp, server, serverlen );
  200. s101->sv101_name = (LPWSTR)endp;
  201. s101->sv101_platform_id = SV_PLATFORM_ID_NT;
  202. s101->sv101_version_major = 5;
  203. s101->sv101_version_minor = 0;
  204. s101->sv101_type = SV_TYPE_SERVER;
  205. s101->sv101_comment = (LPWSTR)(endp + serverlen - sizeof(WCHAR));
  206. }
  207. numFound++;
  208. } while( pCSCFindNextFile(hFind, &sFind32, &dwStatus, &dwPinCount, &dwHintFlags, &ftOrgTime) );
  209. pCSCFindClose(hFind);
  210. if( numFound != 0 ) {
  211. apiStatus = NERR_Success;
  212. } else {
  213. NetApiBufferFree( outbuf );
  214. outbuf = NULL;
  215. apiStatus = NERR_BrowserTableIncomplete;
  216. }
  217. *bufptr = outbuf;
  218. *entriesread = numFound;
  219. *totalentries = numFound;
  220. try_exit:;
  221. } except( EXCEPTION_EXECUTE_HANDLER ) {
  222. if( outbuf != NULL ) {
  223. NetApiBufferFree( outbuf );
  224. }
  225. if( hFind != INVALID_HANDLE_VALUE ) {
  226. pCSCFindClose( hFind );
  227. }
  228. apiStatus = ERROR_INVALID_PARAMETER;
  229. }
  230. return apiStatus;
  231. }