Source code of Windows XP (NT5)
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.

356 lines
8.3 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1996
  5. //
  6. // File: svcrole.c
  7. //
  8. // Contents: This is the include to include common we need
  9. //
  10. // History:
  11. //
  12. //---------------------------------------------------------------------------
  13. #include "svcrole.h"
  14. #include "secstore.h"
  15. #include <dsgetdc.h>
  16. #include <dsrole.h>
  17. ///////////////////////////////////////////////////////////////////////////////////
  18. BOOL
  19. GetMachineGroup(
  20. LPWSTR pszMachineName,
  21. LPWSTR* pszGroupName
  22. )
  23. /*++
  24. Note:
  25. Code modified from DISPTRUS.C
  26. --*/
  27. {
  28. LSA_HANDLE PolicyHandle;
  29. DWORD dwStatus;
  30. NTSTATUS Status;
  31. NET_API_STATUS nas = NERR_Success; // assume success
  32. BOOL bSuccess = FALSE; // assume this function will fail
  33. PPOLICY_PRIMARY_DOMAIN_INFO PrimaryDomain = NULL;
  34. LPWSTR szPrimaryDomainName = NULL;
  35. LPWSTR DomainController = NULL;
  36. //
  37. // open the policy on the specified machine
  38. //
  39. Status = OpenPolicy(
  40. pszMachineName,
  41. POLICY_VIEW_LOCAL_INFORMATION,
  42. &PolicyHandle
  43. );
  44. if(Status != ERROR_SUCCESS)
  45. {
  46. SetLastError( dwStatus = LsaNtStatusToWinError(Status) );
  47. return FALSE;
  48. }
  49. //
  50. // get the primary domain
  51. //
  52. Status = LsaQueryInformationPolicy(
  53. PolicyHandle,
  54. PolicyPrimaryDomainInformation,
  55. (PVOID *)&PrimaryDomain
  56. );
  57. if(Status != ERROR_SUCCESS)
  58. {
  59. goto cleanup;
  60. }
  61. *pszGroupName = (LPWSTR)LocalAlloc(
  62. LPTR,
  63. PrimaryDomain->Name.Length + sizeof(WCHAR) // existing length + NULL
  64. );
  65. if(*pszGroupName != NULL)
  66. {
  67. //
  68. // copy the existing buffer to the new storage, appending a NULL
  69. //
  70. lstrcpynW(
  71. *pszGroupName,
  72. PrimaryDomain->Name.Buffer,
  73. (PrimaryDomain->Name.Length / sizeof(WCHAR)) + 1
  74. );
  75. bSuccess = TRUE;
  76. }
  77. cleanup:
  78. if(PrimaryDomain != NULL)
  79. {
  80. LsaFreeMemory(PrimaryDomain);
  81. }
  82. //
  83. // close the policy handle
  84. //
  85. if(PolicyHandle != INVALID_HANDLE_VALUE)
  86. {
  87. LsaClose(PolicyHandle);
  88. }
  89. if(!bSuccess)
  90. {
  91. if(Status != ERROR_SUCCESS)
  92. {
  93. SetLastError( LsaNtStatusToWinError(Status) );
  94. }
  95. else if(nas != NERR_Success)
  96. {
  97. SetLastError( nas );
  98. }
  99. }
  100. return bSuccess;
  101. }
  102. ///////////////////////////////////////////////////////////////////////////////////
  103. BOOL
  104. IsDomainController(
  105. LPWSTR Server,
  106. LPBOOL bDomainController
  107. )
  108. /*++
  109. ++*/
  110. {
  111. PSERVER_INFO_101 si101;
  112. NET_API_STATUS nas;
  113. nas = NetServerGetInfo( (LPTSTR)Server,
  114. 101,
  115. (LPBYTE *)&si101 );
  116. if(nas != NERR_Success)
  117. {
  118. SetLastError(nas);
  119. return FALSE;
  120. }
  121. if( (si101->sv101_type & SV_TYPE_DOMAIN_CTRL) ||
  122. (si101->sv101_type & SV_TYPE_DOMAIN_BAKCTRL) )
  123. {
  124. // we are dealing with a DC
  125. //
  126. *bDomainController = TRUE;
  127. }
  128. else
  129. {
  130. *bDomainController = FALSE;
  131. }
  132. NetApiBufferFree(si101);
  133. return TRUE;
  134. }
  135. ///////////////////////////////////////////////////////////////////////////////////
  136. SERVER_ROLE_IN_DOMAIN
  137. GetServerRoleInDomain(
  138. LPWSTR szServer
  139. )
  140. /*++
  141. ++*/
  142. {
  143. SERVER_ROLE_IN_DOMAIN dwRetCode=SERVERROLE_ERROR;
  144. NET_API_STATUS nas;
  145. NTSTATUS Status;
  146. DWORD dwStatus;
  147. LSA_HANDLE PolicyHandle=INVALID_HANDLE_VALUE;
  148. BOOL bPdc;
  149. PPOLICY_ACCOUNT_DOMAIN_INFO AccountDomain=NULL;
  150. PPOLICY_PRIMARY_DOMAIN_INFO PrimaryDomain=NULL;
  151. PSERVER_INFO_101 lpServerInfo101=NULL;
  152. GUID DomainGuid;
  153. PDOMAIN_CONTROLLER_INFO pDomainControllerInfo = NULL;
  154. DSROLE_PRIMARY_DOMAIN_INFO_BASIC *pDomainInfo = NULL;
  155. //
  156. // Check if we're in a workgroup, if this call
  157. // failed, we use NetApi to retrieve information
  158. //
  159. dwStatus = DsRoleGetPrimaryDomainInformation(
  160. NULL,
  161. DsRolePrimaryDomainInfoBasic,
  162. (PBYTE *) &pDomainInfo
  163. );
  164. if ((dwStatus == NO_ERROR) && (pDomainInfo != NULL))
  165. {
  166. switch (pDomainInfo->MachineRole)
  167. {
  168. case DsRole_RoleStandaloneWorkstation:
  169. case DsRole_RoleStandaloneServer:
  170. dwRetCode = SERVERROLE_STANDALONE;
  171. goto cleanup;
  172. break;
  173. }
  174. //
  175. // check if we are in NT5 domain
  176. //
  177. dwStatus = DsGetDcName(
  178. NULL,
  179. NULL,
  180. &DomainGuid,
  181. NULL,
  182. DS_DIRECTORY_SERVICE_PREFERRED,
  183. &pDomainControllerInfo
  184. );
  185. if(dwStatus == NO_ERROR && pDomainControllerInfo != NULL)
  186. {
  187. if(!(pDomainControllerInfo->Flags & DS_DS_FLAG))
  188. {
  189. //
  190. // We are in NT4 domain
  191. //
  192. dwRetCode = SERVERROLE_NT4DOMAIN;
  193. goto cleanup;
  194. }
  195. }
  196. }
  197. if(dwRetCode != SERVERROLE_ERROR)
  198. {
  199. // we already have information, don't try
  200. // NetApi anymore...
  201. goto cleanup;
  202. }
  203. //
  204. // Get Server information
  205. //
  206. nas = NetServerGetInfo( (LPTSTR)szServer,
  207. 101, // info-level
  208. (LPBYTE *)&lpServerInfo101 );
  209. if(nas != NERR_Success)
  210. {
  211. dwRetCode=SERVERROLE_ERROR;
  212. SetLastError(nas);
  213. goto cleanup;
  214. }
  215. //
  216. // open the policy on the specified machine
  217. //
  218. Status = OpenPolicy( szServer,
  219. POLICY_VIEW_LOCAL_INFORMATION,
  220. &PolicyHandle );
  221. if(Status != ERROR_SUCCESS)
  222. {
  223. SetLastError( LsaNtStatusToWinError(Status) );
  224. dwRetCode = SERVERROLE_ERROR;
  225. goto cleanup;
  226. }
  227. //
  228. // get the primary domain
  229. //
  230. Status = LsaQueryInformationPolicy( PolicyHandle,
  231. PolicyPrimaryDomainInformation,
  232. (VOID **)&PrimaryDomain );
  233. if(Status != ERROR_SUCCESS)
  234. {
  235. SetLastError( LsaNtStatusToWinError(Status) );
  236. dwRetCode = SERVERROLE_ERROR;
  237. goto cleanup;
  238. }
  239. //
  240. // if the primary domain Sid is NULL, we are a non-member
  241. //
  242. if(PrimaryDomain->Sid == NULL)
  243. {
  244. dwRetCode = SERVERROLE_STANDALONE;
  245. goto cleanup;
  246. }
  247. //
  248. // get the AccountDoamin domain
  249. //
  250. Status = LsaQueryInformationPolicy( PolicyHandle,
  251. PolicyAccountDomainInformation,
  252. (VOID **)&AccountDomain );
  253. if(Status != ERROR_SUCCESS)
  254. {
  255. SetLastError( LsaNtStatusToWinError(Status) );
  256. dwRetCode = SERVERROLE_ERROR;
  257. goto cleanup;
  258. }
  259. //
  260. // if the primary domain Sid == AccountDomain Sid, we are DC
  261. //
  262. if(EqualSid(PrimaryDomain->Sid, AccountDomain->DomainSid) == FALSE)
  263. {
  264. dwRetCode = SERVERROLE_SERVER;
  265. goto cleanup;
  266. }
  267. dwRetCode = (lpServerInfo101->sv101_type & SV_TYPE_DOMAIN_CTRL) ? SERVERROLE_PDC : SERVERROLE_BDC;
  268. cleanup:
  269. if(AccountDomain)
  270. {
  271. LsaFreeMemory(AccountDomain);
  272. }
  273. if(PrimaryDomain)
  274. {
  275. LsaFreeMemory(PrimaryDomain);
  276. }
  277. if(PolicyHandle != INVALID_HANDLE_VALUE)
  278. {
  279. LsaClose(PolicyHandle);
  280. }
  281. if(lpServerInfo101 != NULL)
  282. {
  283. NetApiBufferFree(lpServerInfo101);
  284. }
  285. if(pDomainControllerInfo != NULL)
  286. {
  287. NetApiBufferFree(pDomainControllerInfo);
  288. }
  289. if(pDomainInfo != NULL)
  290. {
  291. DsRoleFreeMemory(pDomainInfo);
  292. }
  293. return dwRetCode;
  294. }