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.

269 lines
5.4 KiB

  1. /*--
  2. Copyright (c) 1997-1997 Microsoft Corporation
  3. Module Name:
  4. trustdom.c
  5. Abstract:
  6. Command line tool for linking 2 domains
  7. Author:
  8. 1-Apr-1997 Mac McLain (macm) Created
  9. Environment:
  10. User mode only.
  11. Requires ANSI C extensions: slash-slash comments, long external names.
  12. Revision History:
  13. --*/
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <ntlsa.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <dsgetdc.h>
  22. #include <lmcons.h>
  23. #include <lmapibuf.h>
  24. #include <dnsapi.h>
  25. #define PARENT_TAG "-parent:"
  26. #define PARENT_TAG_LEN (sizeof( PARENT_TAG ) - 1)
  27. typedef struct _TD_DOM_INFO {
  28. LSA_HANDLE Policy;
  29. LSA_HANDLE TrustedDomain;
  30. PPOLICY_DNS_DOMAIN_INFO DnsDomainInfo;
  31. } TD_DOM_INFO, *PTD_DOM_INFO;
  32. BOOLEAN Dbg = FALSE;
  33. VOID
  34. Usage (
  35. VOID
  36. )
  37. /*++
  38. Routine Description:
  39. Displays the expected usage
  40. Arguments:
  41. Return Value:
  42. VOID
  43. --*/
  44. {
  45. printf("stampdns [dns_domain_name] [-parent:parent_server_name]\n");
  46. }
  47. NTSTATUS
  48. GetDomainInfoForDomain(
  49. IN PWSTR DomainName,
  50. IN PTD_DOM_INFO Info
  51. )
  52. {
  53. NTSTATUS Status = STATUS_SUCCESS;
  54. DWORD dwErr;
  55. UNICODE_STRING Domain;
  56. OBJECT_ATTRIBUTES ObjectAttributes;
  57. PDOMAIN_CONTROLLER_INFO DCInfo = NULL;
  58. if ( DomainName != NULL ) {
  59. dwErr = DsGetDcName( NULL, (LPCWSTR)DomainName, NULL, NULL,
  60. DS_DIRECTORY_SERVICE_REQUIRED, &DCInfo );
  61. if ( dwErr == ERROR_SUCCESS ) {
  62. RtlInitUnicodeString( &Domain, DCInfo->DomainControllerName + 2 );
  63. } else {
  64. Status = STATUS_UNSUCCESSFUL;
  65. }
  66. }
  67. if ( NT_SUCCESS( Status ) ) {
  68. RtlZeroMemory( &ObjectAttributes, sizeof( ObjectAttributes ) );
  69. Status = LsaOpenPolicy( DomainName == NULL ? NULL : &Domain,
  70. &ObjectAttributes,
  71. MAXIMUM_ALLOWED,
  72. &Info->Policy
  73. );
  74. if ( NT_SUCCESS( Status ) ) {
  75. Status = LsaQueryInformationPolicy( Info->Policy,
  76. PolicyDnsDomainInformation,
  77. &(Info->DnsDomainInfo )
  78. );
  79. }
  80. #if DBG
  81. if ( Dbg ) {
  82. printf( "GetDomainInfoForDomain on %ws returned 0x%lx\n", DomainName, Status );
  83. }
  84. #endif
  85. }
  86. NetApiBufferFree( DCInfo );
  87. return( Status );
  88. }
  89. VOID
  90. FreeDomainInfo(
  91. IN PTD_DOM_INFO Info
  92. )
  93. {
  94. if ( Info->DnsDomainInfo != NULL ) {
  95. LsaFreeMemory( Info->DnsDomainInfo );
  96. }
  97. if ( Info->Policy ) {
  98. LsaClose( Info->Policy );
  99. }
  100. }
  101. INT
  102. __cdecl main (
  103. int argc,
  104. char *argv[])
  105. /*++
  106. Routine Description:
  107. The main the for this executable
  108. Arguments:
  109. argc - Count of arguments
  110. argv - List of arguments
  111. Return Value:
  112. VOID
  113. --*/
  114. {
  115. NTSTATUS Status = STATUS_SUCCESS;
  116. WCHAR Buffer[MAX_PATH + 1];
  117. PWSTR Server = NULL, DnsName = NULL;
  118. INT i;
  119. TD_DOM_INFO Local, Remote;
  120. RtlZeroMemory( &Local, sizeof( TD_DOM_INFO ) );
  121. RtlZeroMemory( &Remote, sizeof (TD_DOM_INFO ) );
  122. if (argc < 2 ) {
  123. Usage();
  124. goto Done;
  125. } else {
  126. if ( _stricmp( argv[1], "-?") == 0 ) {
  127. Usage();
  128. goto Done;
  129. } else {
  130. if ( _strnicmp( argv[1], PARENT_TAG, PARENT_TAG_LEN ) == 0 ) {
  131. mbstowcs( Buffer, argv[1] + PARENT_TAG_LEN , strlen( argv[1] ) - PARENT_TAG_LEN + 1 );
  132. Server = Buffer;
  133. } else {
  134. mbstowcs(Buffer, argv[1], strlen(argv[1]) + 1 );
  135. DnsName = Buffer;
  136. }
  137. }
  138. }
  139. Status = GetDomainInfoForDomain( NULL, &Local );
  140. if ( NT_SUCCESS( Status ) && Server ) {
  141. Status = GetDomainInfoForDomain( Server, &Remote );
  142. }
  143. if ( !NT_SUCCESS( Status ) ) {
  144. goto Done;
  145. }
  146. if ( DnsName ) {
  147. RtlInitUnicodeString( &Local.DnsDomainInfo->DnsForestName, DnsName );
  148. RtlInitUnicodeString( &Local.DnsDomainInfo->DnsDomainName, DnsName );
  149. } else {
  150. RtlCopyMemory( &Local.DnsDomainInfo->DnsForestName, &Remote.DnsDomainInfo->DnsForestName,
  151. sizeof( UNICODE_STRING ) );
  152. }
  153. Status = DnsValidateDnsName_W(Local.DnsDomainInfo->DnsForestName.Buffer );
  154. if ( Status != 0 ) {
  155. printf( "Stampdns required a valid Dns name. %wZ is not a valid Dns name\n",
  156. &Local.DnsDomainInfo->DnsForestName );
  157. } else {
  158. printf( "Setting DnsForestName to %wZ\n", &Local.DnsDomainInfo->DnsForestName );
  159. Status = LsaSetInformationPolicy( Local.Policy,
  160. PolicyDnsDomainInformation,
  161. Local.DnsDomainInfo );
  162. }
  163. Done:
  164. FreeDomainInfo( &Local );
  165. FreeDomainInfo( &Remote );
  166. if( NT_SUCCESS( Status ) ) {
  167. printf("The command completed successfully\n");
  168. } else {
  169. printf("The command failed with an error 0x%lx\n", Status );
  170. }
  171. return( NT_SUCCESS( Status ) );
  172. }