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.

486 lines
12 KiB

  1. #define NL_MAX_DNS_LABEL_LENGTH 63
  2. #include <nt.h>
  3. #include <ntrtl.h>
  4. #include <nturtl.h>
  5. #include <ntddbrow.h>
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include <lmcons.h>
  9. #include <winsock2.h>
  10. #include <nlsite.h>
  11. #include <dsgetdc.h>
  12. #include <nlcommon.h>
  13. #define MAX_PRINTF_LEN 1024 // Arbitrary.
  14. VOID
  15. NlPrintRoutine(
  16. IN DWORD DebugFlag,
  17. IN LPSTR Format,
  18. ...
  19. )
  20. {
  21. va_list arglist;
  22. char OutputBuffer[MAX_PRINTF_LEN];
  23. //
  24. // Put a the information requested by the caller onto the line
  25. //
  26. va_start(arglist, Format);
  27. (VOID) vsprintf(OutputBuffer, Format, arglist);
  28. va_end(arglist);
  29. printf( "%s", OutputBuffer );
  30. return;
  31. UNREFERENCED_PARAMETER( DebugFlag );
  32. }
  33. VOID
  34. NlAssertFailed(
  35. IN PVOID FailedAssertion,
  36. IN PVOID FileName,
  37. IN ULONG LineNumber,
  38. IN PCHAR Message OPTIONAL
  39. )
  40. {
  41. printf( "\n*** Assertion failed: %s%s\n*** Source File: %s, line %ld\n\n",
  42. Message ? Message : "",
  43. FailedAssertion,
  44. FileName,
  45. LineNumber
  46. );
  47. }
  48. NTSTATUS
  49. NlBrowserSendDatagram(
  50. IN PVOID ContextDomainInfo,
  51. IN ULONG IpAddress,
  52. IN LPWSTR UnicodeDestinationName,
  53. IN DGRECEIVER_NAME_TYPE NameType,
  54. IN LPWSTR TransportName,
  55. IN LPSTR OemMailslotName,
  56. IN PVOID Buffer,
  57. IN ULONG BufferSize
  58. )
  59. /*++
  60. Routine Description:
  61. Send the specified mailslot message to the specified mailslot on the
  62. specified server on the specified transport..
  63. Arguments:
  64. DomainInfo - Hosted domain sending the datagram
  65. IpAddress - IpAddress of the machine to send the pind to.
  66. If zero, UnicodeDestinationName must be specified.
  67. UnicodeDestinationName -- Name of the server to send to.
  68. NameType -- Type of name represented by UnicodeDestinationName.
  69. TransportName -- Name of the transport to send on.
  70. Use NULL to send on all transports.
  71. OemMailslotName -- Name of the mailslot to send to.
  72. Buffer -- Specifies a pointer to the mailslot message to send.
  73. BufferSize -- Size in bytes of the mailslot message
  74. Return Value:
  75. Status of the operation.
  76. --*/
  77. {
  78. return STATUS_INTERNAL_ERROR;
  79. // If this routine is ever needed, copy it from logonsrv\client\getdcnam.c
  80. UNREFERENCED_PARAMETER(ContextDomainInfo);
  81. UNREFERENCED_PARAMETER(IpAddress);
  82. UNREFERENCED_PARAMETER(UnicodeDestinationName);
  83. UNREFERENCED_PARAMETER(NameType);
  84. UNREFERENCED_PARAMETER(TransportName);
  85. UNREFERENCED_PARAMETER(OemMailslotName);
  86. UNREFERENCED_PARAMETER(Buffer);
  87. UNREFERENCED_PARAMETER(BufferSize);
  88. }
  89. NET_API_STATUS
  90. NlGetLocalPingResponse(
  91. IN LPCWSTR NetbiosDomainName OPTIONAL,
  92. IN LPCWSTR DnsDomainName OPTIONAL,
  93. IN GUID *DomainGuid OPTIONAL,
  94. IN PSID DomainSid OPTIONAL,
  95. IN BOOL PdcOnly,
  96. IN LPCWSTR UnicodeComputerName,
  97. IN LPCWSTR UnicodeUserName OPTIONAL,
  98. IN ULONG AllowableAccountControlBits,
  99. IN ULONG NtVersion,
  100. IN ULONG NtVersionFlags,
  101. OUT PSOCKET_ADDRESS *ResponseDcAddress,
  102. OUT PVOID *Message,
  103. OUT PULONG MessageSize
  104. )
  105. /*++
  106. Routine Description:
  107. Build the message to ping a DC to see if it exists.
  108. Arguments:
  109. NetbiosDomainName - Netbios Domain Name of the domain to query.
  110. DnsDomainName - DNS Domain Name of the domain to query.
  111. PdcOnly - True if only the PDC should respond.
  112. UnicodeComputerName - Netbios computer name of the machine to respond to.
  113. UnicodeUserName - Account name of the user being pinged.
  114. If NULL, DC will always respond affirmatively.
  115. AllowableAccountControlBits - Mask of allowable account types for UnicodeUserName.
  116. NtVersion - Version of the message
  117. NtVersionFlags - Version of the message.
  118. 0: For backward compatibility.
  119. NETLOGON_NT_VERSION_5: for NT 5.0 message.
  120. Message - Returns the message to be sent to the DC in question.
  121. Buffer must be free using NetpMemoryFree().
  122. MessageSize - Returns the size (in bytes) of the returned message
  123. Return Value:
  124. NO_ERROR - Operation completed successfully;
  125. ERROR_NO_SUCH_DOMAIN - If the machine isn't a DC for the requested domain.
  126. ERROR_NOT_ENOUGH_MEMORY - The message could not be allocated.
  127. --*/
  128. {
  129. // This stub routine simply does nothing harmful
  130. return ERROR_NO_SUCH_DOMAIN;
  131. UNREFERENCED_PARAMETER( NetbiosDomainName );
  132. UNREFERENCED_PARAMETER( DnsDomainName );
  133. UNREFERENCED_PARAMETER( DomainGuid );
  134. UNREFERENCED_PARAMETER( DomainSid );
  135. UNREFERENCED_PARAMETER( PdcOnly );
  136. UNREFERENCED_PARAMETER( UnicodeComputerName );
  137. UNREFERENCED_PARAMETER( UnicodeUserName );
  138. UNREFERENCED_PARAMETER( AllowableAccountControlBits );
  139. UNREFERENCED_PARAMETER( NtVersion );
  140. UNREFERENCED_PARAMETER( NtVersionFlags );
  141. UNREFERENCED_PARAMETER( ResponseDcAddress );
  142. UNREFERENCED_PARAMETER( Message );
  143. UNREFERENCED_PARAMETER( MessageSize );
  144. }
  145. VOID
  146. NlpDumpBuffer(
  147. IN DWORD DebugFlag,
  148. PVOID Buffer,
  149. DWORD BufferSize
  150. )
  151. /*++
  152. Routine Description:
  153. Dumps the buffer content on to the debugger output.
  154. Arguments:
  155. DebugFlag: Debug flag to pass on to NlPrintRoutine
  156. Buffer: buffer pointer.
  157. BufferSize: size of the buffer.
  158. Return Value:
  159. none
  160. --*/
  161. {
  162. #define NUM_CHARS 16
  163. DWORD i, limit;
  164. CHAR TextBuffer[NUM_CHARS + 1];
  165. LPBYTE BufferPtr = Buffer;
  166. //
  167. // Hex dump of the bytes
  168. //
  169. limit = ((BufferSize - 1) / NUM_CHARS + 1) * NUM_CHARS;
  170. for (i = 0; i < limit; i++) {
  171. if (i < BufferSize) {
  172. printf("%02x ", BufferPtr[i]);
  173. if (BufferPtr[i] < 31 ) {
  174. TextBuffer[i % NUM_CHARS] = '.';
  175. } else if (BufferPtr[i] == '\0') {
  176. TextBuffer[i % NUM_CHARS] = ' ';
  177. } else {
  178. TextBuffer[i % NUM_CHARS] = (CHAR) BufferPtr[i];
  179. }
  180. } else {
  181. printf(0," ");
  182. TextBuffer[i % NUM_CHARS] = ' ';
  183. }
  184. if ((i + 1) % NUM_CHARS == 0) {
  185. TextBuffer[NUM_CHARS] = 0;
  186. printf(" %s\n", TextBuffer);
  187. }
  188. }
  189. UNREFERENCED_PARAMETER( DebugFlag );
  190. }
  191. VOID
  192. DoAdd(
  193. IN LPWSTR SubnetName,
  194. IN LPWSTR SiteName
  195. )
  196. {
  197. ULONG SubnetAddress;
  198. ULONG SubnetMask;
  199. BYTE SubnetBitCount;
  200. NET_API_STATUS NetStatus;
  201. NetStatus = NlParseSubnetString( SubnetName,
  202. &SubnetAddress,
  203. &SubnetMask,
  204. &SubnetBitCount );
  205. if ( NetStatus != NO_ERROR ) {
  206. return;
  207. }
  208. printf( "Doing %ws %8.8lX %8.8lX %ld\n",
  209. SubnetName, SubnetAddress, SubnetMask, SubnetBitCount );
  210. NetStatus = NlSitesAddSubnet( SiteName, SubnetName );
  211. if ( NetStatus != NO_ERROR ) {
  212. printf("Cannot add site %ws %ws %ld\n", SiteName, SubnetName, NetStatus );
  213. return;
  214. }
  215. }
  216. VOID
  217. DoLook(
  218. IN LPWSTR IpAddress
  219. )
  220. {
  221. INT WsaStatus;
  222. SOCKADDR SockAddr;
  223. INT SockAddrSize;
  224. PNL_SITE_ENTRY SiteEntry;
  225. //
  226. // Convert the address to a sockaddr
  227. //
  228. SockAddrSize = sizeof(SockAddr);
  229. WsaStatus = WSAStringToAddressW( IpAddress,
  230. AF_INET,
  231. NULL,
  232. (PSOCKADDR)&SockAddr,
  233. &SockAddrSize );
  234. if ( WsaStatus != 0 ) {
  235. WsaStatus = WSAGetLastError();
  236. printf("DoLook: %ws: Wsa Error %ld\n", IpAddress, WsaStatus );
  237. return;
  238. }
  239. if ( SockAddr.sa_family != AF_INET ) {
  240. printf("DoLook: %ws: address not AF_INET\n", IpAddress );
  241. return;
  242. }
  243. SiteEntry = NlFindSiteEntryBySockAddr( &SockAddr );
  244. if ( SiteEntry == NULL ) {
  245. printf("DoLook: %ws: address cannot be found\n", IpAddress );
  246. return;
  247. }
  248. printf("%ws is in site %ws\n", IpAddress, SiteEntry->SiteName );
  249. NlDerefSiteEntry( SiteEntry );
  250. }
  251. __cdecl main (int argc, char *argv[])
  252. {
  253. NET_API_STATUS NetStatus;
  254. PNL_SITE_ENTRY Site1;
  255. PNL_SITE_ENTRY Site2;
  256. PNL_SITE_ENTRY Site3;
  257. WORD wVersionRequested;
  258. WSADATA wsaData;
  259. int err;
  260. //
  261. // Initialize winsock.
  262. //
  263. wVersionRequested = MAKEWORD( 1, 1 );
  264. NetStatus = WSAStartup( wVersionRequested, &wsaData );
  265. if ( NetStatus != 0 ) {
  266. printf( "NETAPI32.DLL: Cannot initialize winsock %ld.\n", NetStatus );
  267. return NetStatus;
  268. }
  269. if ( LOBYTE( wsaData.wVersion ) != 1 ||
  270. HIBYTE( wsaData.wVersion ) != 1 ) {
  271. WSACleanup();
  272. printf( "NETAPI32.DLL: Wrong winsock version %ld.\n", wsaData.wVersion );
  273. return WSANOTINITIALISED;
  274. }
  275. //
  276. // Init
  277. NetStatus = NlSiteInitialize();
  278. if ( NetStatus != NO_ERROR ) {
  279. printf( "Cannot NlSiteInitialize %ld\n", NetStatus );
  280. return 1;
  281. }
  282. #ifdef notdef
  283. //
  284. // Add some sites.
  285. //
  286. Site1 = NlFindSiteEntry( L"Site 1" );
  287. if ( Site1 == NULL ) {
  288. printf( "Cannot Create Site1\n");
  289. return 1;
  290. }
  291. printf( "%lx: %ws\n", Site1, Site1->SiteName );
  292. Site2 = NlFindSiteEntry( L"Site 1" );
  293. if ( Site2 == NULL ) {
  294. printf( "Cannot Create Site2\n");
  295. return 2;
  296. }
  297. printf( "%lx: %ws\n", Site2, Site2->SiteName );
  298. Site3 = NlFindSiteEntry( L"Site 3" );
  299. if ( Site3 == NULL ) {
  300. printf( "Cannot Create Site3\n");
  301. return 3;
  302. }
  303. printf( "%lx: %ws\n", Site3, Site3->SiteName );
  304. NlDerefSiteEntry( Site1 );
  305. NlDerefSiteEntry( Site2 );
  306. NlDerefSiteEntry( Site3 );
  307. #endif // notdef
  308. //
  309. // Test invalid subnet strings
  310. //
  311. #ifdef notdef
  312. DoAdd( L"999.0.0.0/1", L"Site 1" );
  313. DoAdd( L"1.0.0.0/0", L"Site 1" );
  314. DoAdd( L"1.0.0.0/1", L"Site 1" );
  315. DoAdd( L"1.0.0.0/33", L"Site 1" );
  316. DoAdd( L"1.0.0.0/1p", L"Site 1" );
  317. DoAdd( L"1.0.0.0", L"Site 1" );
  318. DoAdd( L"128.0.0.0/1", L"Site 1" );
  319. DoAdd( L"128.0.0.0/2", L"Site 1" );
  320. DoAdd( L"128.0.0.0/3", L"Site 1" );
  321. DoAdd( L"128.0.0.0/4", L"Site 1" );
  322. DoAdd( L"128.0.0.0/5", L"Site 1" );
  323. DoAdd( L"128.0.0.0/6", L"Site 1" );
  324. DoAdd( L"128.0.0.0/7", L"Site 1" );
  325. DoAdd( L"128.0.0.0/8", L"Site 1" );
  326. DoAdd( L"128.0.0.0/9", L"Site 1" );
  327. DoAdd( L"128.0.0.0/10", L"Site 1" );
  328. DoAdd( L"128.0.0.0/11", L"Site 1" );
  329. DoAdd( L"128.0.0.0/12", L"Site 1" );
  330. DoAdd( L"128.0.0.0/13", L"Site 1" );
  331. DoAdd( L"128.0.0.0/14", L"Site 1" );
  332. DoAdd( L"128.0.0.0/15", L"Site 1" );
  333. DoAdd( L"128.0.0.0/16", L"Site 1" );
  334. DoAdd( L"128.0.0.0/17", L"Site 1" );
  335. DoAdd( L"128.0.0.0/18", L"Site 1" );
  336. DoAdd( L"128.0.0.0/19", L"Site 1" );
  337. DoAdd( L"128.0.0.0/20", L"Site 1" );
  338. DoAdd( L"128.0.0.0/21", L"Site 1" );
  339. DoAdd( L"128.0.0.0/22", L"Site 1" );
  340. DoAdd( L"128.0.0.0/23", L"Site 1" );
  341. DoAdd( L"128.0.0.0/24", L"Site 1" );
  342. DoAdd( L"128.0.0.0/25", L"Site 1" );
  343. DoAdd( L"128.0.0.0/26", L"Site 1" );
  344. DoAdd( L"128.0.0.0/27", L"Site 1" );
  345. DoAdd( L"128.0.0.0/28", L"Site 1" );
  346. DoAdd( L"128.0.0.0/29", L"Site 1" );
  347. DoAdd( L"128.0.0.0/30", L"Site 1" );
  348. DoAdd( L"128.0.0.0/31", L"Site 1" );
  349. DoAdd( L"128.0.0.0/32", L"Site 1" );
  350. #endif // notdef
  351. {
  352. ULONG i;
  353. for ( i=0; i<2; i++ ) {
  354. DoAdd( L"128.0.0.0/8", L"Site 1" );
  355. DoAdd( L"128.0.0.0/7", L"Site 2" );
  356. DoAdd( L"128.0.0.0/8", L"Site 3" );
  357. // DoAdd( L"128.0.0.0/1", L"Site 4" );
  358. DoAdd( L"157.55.0.0/16", L"Site 5" );
  359. DoAdd( L"157.55.80.0/20", L"Site 6" );
  360. NlSitesEndSubnetEnum();
  361. }
  362. }
  363. DoLook( L"157.55.95.68" );
  364. DoLook( L"157.55.24.68" );
  365. #ifdef notdef
  366. DoAdd( L"157.55.0.0/16", L"Site 1" );
  367. DoAdd( L"157.55.240.0/20", L"Site 1" );
  368. DoAdd( L"128.0.0.0/1", L"Site 1" );
  369. #endif // notdef
  370. //
  371. // Done.
  372. //
  373. NlSiteTerminate();
  374. printf( "Done\n" );
  375. return 0;
  376. }