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.

371 lines
10 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. DomName.c
  5. Abstract:
  6. This file contains NetpGetDomainName().
  7. Author:
  8. John Rogers (JohnRo) 09-Jan-1992
  9. Environment:
  10. User Mode - Win32
  11. Portable to any flat, 32-bit environment. (Uses Win32 typedefs.)
  12. Requires ANSI C extensions: slash-slash comments, long external names.
  13. Revision History:
  14. 09-Jan-1992 JohnRo
  15. Created.
  16. 13-Feb-1992 JohnRo
  17. Moved section name equates to ConfName.h.
  18. 13-Mar-1992 JohnRo
  19. Get rid of old config helper callers.
  20. --*/
  21. #include <nt.h> // NT definitions (temporary)
  22. #include <ntrtl.h> // NT Rtl structure definitions (temporary)
  23. #include <ntlsa.h>
  24. #include <windef.h> // Win32 type definitions
  25. #include <lmcons.h> // LAN Manager common definitions
  26. #include <lmerr.h> // LAN Manager error code
  27. #include <lmapibuf.h> // NetApiBufferAllocate()
  28. #include <netdebug.h> // LPDEBUG_STRING typedef.
  29. #include <config.h> // NetpConfig helpers.
  30. #include <confname.h> // SECT_NT_ equates.
  31. #include <debuglib.h> // IF_DEBUG().
  32. #include <netlib.h> // My prototype.
  33. #include <winerror.h> // ERROR_ equates, NO_ERROR.
  34. NET_API_STATUS
  35. NetpGetDomainNameExExEx (
  36. OUT LPTSTR *DomainNamePtr,
  37. OUT LPTSTR *DnsDomainNamePtr OPTIONAL,
  38. OUT LPTSTR *DnsForestNamePtr OPTIONAL,
  39. OUT GUID **DomainGuidPtr OPTIONAL,
  40. OUT PBOOLEAN IsWorkgroupName
  41. )
  42. /*++
  43. Routine Description:
  44. Returns the name of the domain or workgroup this machine belongs to.
  45. Arguments:
  46. DomainNamePtr - The name of the domain or workgroup
  47. Free the returned buffer user NetApiBufferFree.
  48. DnsDomainNamePtr - Returns the DNS name of the domain this machine is
  49. a member of. NULL is returned if the machine is not a member of
  50. a domain or if that domain has no DNS name.
  51. Free the returned buffer user NetApiBufferFree.
  52. DnsForestNamePtr - Returns the DNS forest name of the forest this
  53. machine is in. NULL is returned if the machine is not a member of
  54. a domain or if that domain has no DNS name.
  55. Free the returned buffer user NetApiBufferFree.
  56. DomainGuidPtr - Returns the domain GUID of the domain this machine is
  57. a member of. NULL is return if the machine is not a member of
  58. a domain or if that domain has no domain GUID.
  59. Free the returned buffer user NetApiBufferFree.
  60. IsWorkgroupName - Returns TRUE if the name is a workgroup name.
  61. Returns FALSE if the name is a domain name.
  62. Return Value:
  63. NERR_Success - Success.
  64. NERR_CfgCompNotFound - There was an error determining the domain name
  65. --*/
  66. {
  67. NET_API_STATUS NetStatus;
  68. NTSTATUS Status;
  69. LSA_HANDLE PolicyHandle = NULL;
  70. PPOLICY_DNS_DOMAIN_INFO PrimaryDomainInfo = NULL;
  71. OBJECT_ATTRIBUTES ObjAttributes;
  72. //
  73. // Check for caller's errors.
  74. //
  75. if (DomainNamePtr == NULL) {
  76. return ERROR_INVALID_PARAMETER;
  77. }
  78. *DomainNamePtr = NULL;
  79. if ( ARGUMENT_PRESENT(DnsDomainNamePtr)) {
  80. *DnsDomainNamePtr = NULL;
  81. }
  82. if ( ARGUMENT_PRESENT(DnsForestNamePtr)) {
  83. *DnsForestNamePtr = NULL;
  84. }
  85. if ( ARGUMENT_PRESENT(DomainGuidPtr)) {
  86. *DomainGuidPtr = NULL;
  87. }
  88. //
  89. // Open a handle to the local security policy. Initialize the
  90. // objects attributes structure first.
  91. //
  92. InitializeObjectAttributes(
  93. &ObjAttributes,
  94. NULL,
  95. 0L,
  96. NULL,
  97. NULL
  98. );
  99. Status = LsaOpenPolicy(
  100. NULL,
  101. &ObjAttributes,
  102. POLICY_VIEW_LOCAL_INFORMATION,
  103. &PolicyHandle
  104. );
  105. if (! NT_SUCCESS(Status)) {
  106. NetpKdPrint(("NetpGetDomainName: LsaOpenPolicy returned " FORMAT_NTSTATUS
  107. "\n", Status));
  108. NetStatus = NERR_CfgCompNotFound;
  109. goto Cleanup;
  110. }
  111. //
  112. // Get the name of the primary domain from LSA
  113. //
  114. Status = LsaQueryInformationPolicy(
  115. PolicyHandle,
  116. PolicyDnsDomainInformation,
  117. (PVOID *) &PrimaryDomainInfo
  118. );
  119. if (! NT_SUCCESS(Status)) {
  120. NetpKdPrint(("NetpGetDomainName: LsaQueryInformationPolicy failed "
  121. FORMAT_NTSTATUS "\n", Status));
  122. NetStatus = NERR_CfgCompNotFound;
  123. goto Cleanup;
  124. }
  125. //
  126. // Copy the Netbios domain name.
  127. //
  128. if ((NetStatus = NetApiBufferAllocate(
  129. PrimaryDomainInfo->Name.Length + sizeof(WCHAR),
  130. DomainNamePtr
  131. )) != NERR_Success) {
  132. goto Cleanup;
  133. }
  134. memcpy(
  135. *DomainNamePtr,
  136. PrimaryDomainInfo->Name.Buffer,
  137. PrimaryDomainInfo->Name.Length
  138. );
  139. (*DomainNamePtr)[PrimaryDomainInfo->Name.Length/sizeof(WCHAR)] = L'\0';
  140. //
  141. // Copy the DNS domain name.
  142. //
  143. if ( ARGUMENT_PRESENT(DnsDomainNamePtr) &&
  144. PrimaryDomainInfo->DnsDomainName.Length != 0 ) {
  145. if ((NetStatus = NetApiBufferAllocate(
  146. PrimaryDomainInfo->DnsDomainName.Length + sizeof(WCHAR),
  147. DnsDomainNamePtr
  148. )) != NERR_Success) {
  149. goto Cleanup;
  150. }
  151. memcpy(
  152. *DnsDomainNamePtr,
  153. PrimaryDomainInfo->DnsDomainName.Buffer,
  154. PrimaryDomainInfo->DnsDomainName.Length
  155. );
  156. (*DnsDomainNamePtr)[PrimaryDomainInfo->DnsDomainName.Length/sizeof(WCHAR)] = L'\0';
  157. }
  158. //
  159. // Copy the DNS forest name.
  160. //
  161. if ( ARGUMENT_PRESENT(DnsForestNamePtr) &&
  162. PrimaryDomainInfo->DnsForestName.Length != 0 ) {
  163. if ((NetStatus = NetApiBufferAllocate(
  164. PrimaryDomainInfo->DnsForestName.Length + sizeof(WCHAR),
  165. DnsForestNamePtr
  166. )) != NERR_Success) {
  167. goto Cleanup;
  168. }
  169. memcpy(
  170. *DnsForestNamePtr,
  171. PrimaryDomainInfo->DnsForestName.Buffer,
  172. PrimaryDomainInfo->DnsForestName.Length
  173. );
  174. (*DnsForestNamePtr)[PrimaryDomainInfo->DnsForestName.Length/sizeof(WCHAR)] = L'\0';
  175. }
  176. //
  177. // Copy the domain GUID.
  178. //
  179. if ( ARGUMENT_PRESENT(DomainGuidPtr) &&
  180. RtlCompareMemoryUlong( &PrimaryDomainInfo->DomainGuid,
  181. sizeof(GUID),
  182. 0 ) != sizeof(GUID) ) {
  183. if ((NetStatus = NetApiBufferAllocate(
  184. sizeof(GUID),
  185. DomainGuidPtr
  186. )) != NERR_Success) {
  187. goto Cleanup;
  188. }
  189. memcpy( *DomainGuidPtr, &PrimaryDomainInfo->DomainGuid, sizeof(GUID));
  190. }
  191. *IsWorkgroupName = (PrimaryDomainInfo->Sid == NULL);
  192. IF_DEBUG(CONFIG) {
  193. NetpKdPrint(("NetpGetDomainName got " FORMAT_LPTSTR "\n",
  194. *DomainNamePtr));
  195. }
  196. NetStatus = NO_ERROR;
  197. Cleanup:
  198. if ( NetStatus != NO_ERROR ) {
  199. if ( *DomainNamePtr != NULL ) {
  200. NetApiBufferFree( *DomainNamePtr );
  201. *DomainNamePtr = NULL;
  202. }
  203. if ( ARGUMENT_PRESENT(DnsDomainNamePtr)) {
  204. if ( *DnsDomainNamePtr != NULL ) {
  205. NetApiBufferFree( *DnsDomainNamePtr );
  206. *DnsDomainNamePtr = NULL;
  207. }
  208. }
  209. if ( ARGUMENT_PRESENT(DnsForestNamePtr)) {
  210. if ( *DnsForestNamePtr != NULL ) {
  211. NetApiBufferFree( *DnsForestNamePtr );
  212. *DnsForestNamePtr = NULL;
  213. }
  214. }
  215. if ( ARGUMENT_PRESENT(DomainGuidPtr)) {
  216. if ( *DomainGuidPtr != NULL ) {
  217. NetApiBufferFree( *DomainGuidPtr );
  218. *DomainGuidPtr = NULL;
  219. }
  220. }
  221. }
  222. if ( PrimaryDomainInfo != NULL ) {
  223. (void) LsaFreeMemory((PVOID) PrimaryDomainInfo);
  224. }
  225. if ( PolicyHandle != NULL ) {
  226. (void) LsaClose(PolicyHandle);
  227. }
  228. return NetStatus;
  229. }
  230. NET_API_STATUS
  231. NetpGetDomainNameExEx (
  232. OUT LPTSTR *DomainNamePtr,
  233. OUT LPTSTR *DnsDomainNamePtr OPTIONAL,
  234. OUT PBOOLEAN IsWorkgroupName
  235. )
  236. /*++
  237. Routine Description:
  238. Returns the name of the domain or workgroup this machine belongs to.
  239. Arguments:
  240. DomainNamePtr - The name of the domain or workgroup
  241. Free the returned buffer user NetApiBufferFree.
  242. DnsDomainNamePtr - Returns the DNS name of the domain this machine is
  243. is member of. NULL is returned if the machine is not a member of
  244. a domain or for that domain has no DNS name.
  245. Free the returned buffer user NetApiBufferFree.
  246. IsWorkgroupName - Returns TRUE if the name is a workgroup name.
  247. Returns FALSE if the name is a domain name.
  248. Return Value:
  249. NERR_Success - Success.
  250. NERR_CfgCompNotFound - There was an error determining the domain name
  251. --*/
  252. {
  253. return NetpGetDomainNameExExEx( DomainNamePtr, DnsDomainNamePtr, NULL, NULL, IsWorkgroupName );
  254. }
  255. NET_API_STATUS
  256. NetpGetDomainNameEx (
  257. OUT LPTSTR *DomainNamePtr, // alloc and set ptr (free with NetApiBufferFree)
  258. OUT PBOOLEAN IsWorkgroupName
  259. )
  260. /*++
  261. Routine Description:
  262. Returns the name of the domain or workgroup this machine belongs to.
  263. Arguments:
  264. DomainNamePtr - The name of the domain or workgroup
  265. IsWorkgroupName - Returns TRUE if the name is a workgroup name.
  266. Returns FALSE if the name is a domain name.
  267. Return Value:
  268. NERR_Success - Success.
  269. NERR_CfgCompNotFound - There was an error determining the domain name
  270. --*/
  271. {
  272. return NetpGetDomainNameExExEx( DomainNamePtr, NULL, NULL, NULL, IsWorkgroupName );
  273. }
  274. NET_API_STATUS
  275. NetpGetDomainName (
  276. IN LPTSTR *DomainNamePtr // alloc and set ptr (free with NetApiBufferFree)
  277. )
  278. {
  279. BOOLEAN IsWorkgroupName;
  280. return NetpGetDomainNameExExEx( DomainNamePtr, NULL, NULL, NULL, &IsWorkgroupName );
  281. }