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.

312 lines
6.7 KiB

  1. /*++
  2. SERVERS.CXX
  3. Copyright (C) 1999 Microsoft Corporation, all rights reserved.
  4. DESCRIPTION: adding/removing servers
  5. Created, May 21, 1999 by DavidCHR.
  6. --*/
  7. #include "everything.hxx"
  8. NTSTATUS
  9. RemoveDomainName( IN LPWSTR *pRealmToRemove ) {
  10. LPWSTR TargetRealm = *pRealmToRemove;
  11. DWORD dwErr;
  12. HKEY hDomainsKey;
  13. NTSTATUS N = STATUS_UNSUCCESSFUL;
  14. dwErr = OpenSubKey( NULL,
  15. &hDomainsKey );
  16. if ( ERROR_SUCCESS == dwErr ) {
  17. dwErr = RegDeleteKeyW( hDomainsKey,
  18. TargetRealm );
  19. switch( dwErr ) {
  20. case ERROR_SUCCESS:
  21. N = STATUS_SUCCESS;
  22. break;
  23. case ERROR_FILE_NOT_FOUND:
  24. case ERROR_PATH_NOT_FOUND:
  25. printf( "No realm mappings found for %ws.\n",
  26. TargetRealm );
  27. break;
  28. default:
  29. printf( "Failed to delete registry mapping for %ws. Error 0x%x.\n",
  30. TargetRealm,
  31. dwErr );
  32. }
  33. RegCloseKey( hDomainsKey );
  34. } // else an error was already logged.
  35. return N;
  36. }
  37. NTSTATUS
  38. RemoveServerName( IN LPWSTR *Parameters,
  39. IN LPWSTR KeyName,
  40. OUT PBOOL pbDeletedLastEntry OPTIONAL ) {
  41. DWORD RegErr;
  42. HKEY DomainHandle = NULL;
  43. DWORD Disposition;
  44. LPWSTR OldServerNames = NULL;
  45. LPWSTR NewServerNames = NULL;
  46. ULONG TotalKdcLength, OldKdcLength = 0;
  47. ULONG NewKdcLength = 0;
  48. ULONG Type, Length;
  49. BOOL PrintedNewServers = FALSE;
  50. CMULTISTRING StringClass;
  51. RegErr = OpenSubKey( Parameters,
  52. &DomainHandle );
  53. if (RegErr)
  54. {
  55. goto Cleanup;
  56. }
  57. RegErr = STATUS_UNSUCCESSFUL;
  58. if ( StringClass.ReadFromRegistry( DomainHandle,
  59. KeyName ) ) {
  60. if ( StringClass.RemoveString( Parameters[ 1 ] ) ) {
  61. if ( StringClass.WriteToRegistry( DomainHandle,
  62. KeyName ) ) {
  63. RegErr = ERROR_SUCCESS;
  64. if ( pbDeletedLastEntry ) {
  65. *pbDeletedLastEntry = ( StringClass.cEntries == 0 );
  66. }
  67. }
  68. }
  69. }
  70. Cleanup:
  71. if (NewServerNames)
  72. {
  73. LocalFree(NewServerNames);
  74. }
  75. if (OldServerNames)
  76. {
  77. LocalFree(OldServerNames);
  78. }
  79. if (DomainHandle)
  80. {
  81. RegCloseKey(DomainHandle);
  82. }
  83. if (RegErr)
  84. {
  85. return(STATUS_UNSUCCESSFUL);
  86. }
  87. return(STATUS_SUCCESS);
  88. }
  89. NTSTATUS
  90. AddServerName(IN LPWSTR * Parameters,
  91. IN LPWSTR KeyName
  92. )
  93. {
  94. DWORD RegErr;
  95. HKEY KerbHandle = NULL;
  96. HKEY DomainHandle = NULL;
  97. HKEY DomainRoot = NULL;
  98. DWORD Disposition;
  99. LPWSTR OldServerNames = NULL;
  100. LPWSTR NewServerNames = NULL;
  101. ULONG OldKdcLength = 0;
  102. ULONG NewKdcLength = 0;
  103. ULONG Type;
  104. CMULTISTRING StringClass;
  105. RegErr = OpenSubKey( Parameters,
  106. &DomainHandle );
  107. if (RegErr)
  108. {
  109. goto Cleanup;
  110. }
  111. RegErr = STATUS_UNSUCCESSFUL;
  112. if ( StringClass.ReadFromRegistry( DomainHandle,
  113. KeyName ) ) {
  114. StringClass.AddString( Parameters[ 1 ] );
  115. if ( StringClass.WriteToRegistry( DomainHandle,
  116. KeyName ) ) {
  117. RegErr = ERROR_SUCCESS;
  118. }
  119. }
  120. Cleanup:
  121. if (NewServerNames)
  122. {
  123. LocalFree(NewServerNames);
  124. }
  125. if (OldServerNames)
  126. {
  127. LocalFree(OldServerNames);
  128. }
  129. if (DomainHandle)
  130. {
  131. RegCloseKey(DomainHandle);
  132. }
  133. if (DomainRoot)
  134. {
  135. RegCloseKey(DomainRoot);
  136. }
  137. if (KerbHandle)
  138. {
  139. RegCloseKey(KerbHandle);
  140. }
  141. if (RegErr)
  142. {
  143. return(STATUS_UNSUCCESSFUL);
  144. }
  145. return(STATUS_SUCCESS);
  146. }
  147. NTSTATUS
  148. AddKdcName(
  149. LPWSTR * Parameters
  150. )
  151. {
  152. if( !CheckUppercase( Parameters[0] ) )
  153. {
  154. return STATUS_UNSUCCESSFUL;
  155. }
  156. if( Parameters[1] == NULL )
  157. {
  158. HKEY DomainHandle = NULL;
  159. NTSTATUS Status;
  160. Status = OpenSubKey( Parameters, &DomainHandle );
  161. if( DomainHandle )
  162. {
  163. RegCloseKey( DomainHandle );
  164. }
  165. return Status;
  166. }
  167. else
  168. {
  169. return(AddServerName(Parameters, KERB_DOMAIN_KDC_NAMES_VALUE));
  170. }
  171. }
  172. NTSTATUS
  173. DeleteKdcName( IN LPWSTR * Parameters ) {
  174. NTSTATUS N;
  175. BOOL bLastOne, bRemoveFromDomain;
  176. PPOLICY_DNS_DOMAIN_INFO DnsDomainInfo = NULL;
  177. UNICODE_STRING tempDomain;
  178. if( Parameters[1] == NULL )
  179. {
  180. return( RemoveDomainName( Parameters ) );
  181. }
  182. N = RemoveServerName( Parameters,
  183. KERB_DOMAIN_KDC_NAMES_VALUE,
  184. &bLastOne );
  185. if ( NT_SUCCESS( N ) && bLastOne ) {
  186. fprintf( stderr,
  187. "NOTE: no kdc's are currently defined for the %ws realm.\n",
  188. Parameters[ 0 ] );
  189. /* we removed the last KDC; check to see if we're directly "joined"
  190. to this domain. */
  191. N = LsaQueryInformationPolicy( LsaHandle,
  192. PolicyDnsDomainInformation,
  193. (PVOID *) &DnsDomainInfo
  194. );
  195. if ( NT_SUCCESS( N ) ) {
  196. RtlInitUnicodeString( &tempDomain,
  197. Parameters[ 0 ] );
  198. if ( RtlCompareUnicodeString( &tempDomain,
  199. &DnsDomainInfo->DnsDomainName,
  200. TRUE ) // case insensitive
  201. == 0 ) {
  202. if ( DnsDomainInfo->Sid != NULL ) {
  203. fprintf( stderr,
  204. "NOTE: %wZ is an NT domain.\n"
  205. " If you want to leave the domain, use the SYSTEM Control Panel applet.\n",
  206. &DnsDomainInfo->DnsDomainName );
  207. } else {
  208. // this was our primary domain. Unjoin from it.
  209. }
  210. } // else, this was not our primary domain. Do nothing further.
  211. LsaFreeMemory( DnsDomainInfo );
  212. } else {
  213. fprintf( stderr,
  214. "Unable to determine domain membership (error 0x%x).\n",
  215. N );
  216. }
  217. }
  218. return N;
  219. }
  220. NTSTATUS
  221. AddKpasswdName(
  222. LPWSTR * Parameters
  223. )
  224. {
  225. return(AddServerName(Parameters, KERB_DOMAIN_KPASSWD_NAMES_VALUE));
  226. }
  227. NTSTATUS
  228. DelKpasswdName( IN LPWSTR * Parameters ) {
  229. return RemoveServerName( Parameters,
  230. KERB_DOMAIN_KPASSWD_NAMES_VALUE,
  231. NULL );
  232. }