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.

298 lines
7.0 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1991-1996 **/
  4. /**********************************************************************/
  5. /*
  6. where.cpp
  7. Property Page support for Group management wizard
  8. FILE HISTORY:
  9. jony Apr-1996 created
  10. */
  11. #include "stdafx.h"
  12. #include "Romaine.h"
  13. #include "NetTree.h"
  14. #include "Where.h"
  15. #include <winreg.h>
  16. #include <lmcons.h>
  17. #include <lmaccess.h>
  18. #include <lmerr.h>
  19. #include <lmapibuf.h>
  20. #include <winnetwk.h>
  21. #include <lmserver.h>
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. unsigned int WhichNTProduct(CString& lpMachineName);
  28. int ClassifyMachine(CString& csMachineName);
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CWhere property page
  31. IMPLEMENT_DYNCREATE(CWhere, CPropertyPage)
  32. CWhere::CWhere() : CPropertyPage(CWhere::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CWhere)
  35. m_csMachineName = _T("");
  36. //}}AFX_DATA_INIT
  37. m_bExpandedOnce = 0;
  38. }
  39. CWhere::~CWhere()
  40. {
  41. }
  42. void CWhere::DoDataExchange(CDataExchange* pDX)
  43. {
  44. CPropertyPage::DoDataExchange(pDX);
  45. //{{AFX_DATA_MAP(CWhere)
  46. DDX_Control(pDX, IDC_SERVER_TREE, m_ctServerTree);
  47. DDX_Text(pDX, IDC_MACHINE_NAME, m_csMachineName);
  48. //}}AFX_DATA_MAP
  49. }
  50. BEGIN_MESSAGE_MAP(CWhere, CPropertyPage)
  51. //{{AFX_MSG_MAP(CWhere)
  52. ON_WM_SHOWWINDOW()
  53. ON_NOTIFY(TVN_SELCHANGED, IDC_SERVER_TREE, OnSelchangedServerTree)
  54. //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CWhere message handlers
  58. LRESULT CWhere::OnWizardNext()
  59. {
  60. UpdateData(TRUE);
  61. if (m_csMachineName == "")
  62. {
  63. AfxMessageBox(IDS_NO_MACHINE_NAME);
  64. CWnd* pWnd = GetDlgItem(IDC_MACHINE_NAME);
  65. pWnd->SetFocus();
  66. return -1;
  67. }
  68. int nVal = ClassifyMachine(m_csMachineName);
  69. CRomaineApp* pApp = (CRomaineApp*)AfxGetApp();
  70. pApp->m_csServer = m_csMachineName;
  71. // go ahead and check the name for uniqueness
  72. TCHAR* pServer = m_csMachineName.GetBuffer(m_csMachineName.GetLength());
  73. m_csMachineName.ReleaseBuffer();
  74. TCHAR* pGroupName = pApp->m_csGroupName.GetBuffer(pApp->m_csGroupName.GetLength());
  75. pApp->m_csGroupName.ReleaseBuffer();
  76. GROUP_INFO_0* pInfo;
  77. NET_API_STATUS nAPI = NetGroupGetInfo(pServer,
  78. pGroupName,
  79. 0,
  80. (LPBYTE*)&pInfo);
  81. if (nAPI == ERROR_SUCCESS)
  82. {
  83. AfxMessageBox(IDS_GROUP_EXISTS);
  84. return IDD_NAME_DLG;
  85. }
  86. LOCALGROUP_INFO_0* pLInfo;
  87. nAPI = NetLocalGroupGetInfo(pServer,
  88. pGroupName,
  89. 0,
  90. (LPBYTE*)&pLInfo);
  91. if (nAPI == ERROR_SUCCESS)
  92. {
  93. AfxMessageBox(IDS_GROUP_EXISTS);
  94. return IDD_NAME_DLG;
  95. }
  96. return nVal;
  97. }
  98. int ClassifyMachine(CString& csMachineName)
  99. {
  100. UINT ui;
  101. HKEY hKey;
  102. DWORD dwRet;
  103. DWORD cbProv = 0;
  104. CRomaineApp* pApp = (CRomaineApp*)AfxGetApp();
  105. csMachineName.MakeUpper();
  106. // if a machine name is entered we need to know the domain
  107. // if a domain name is entered we need to know the DC name
  108. if (csMachineName.Left(2) == "\\\\")
  109. {
  110. pApp->m_bDomain = FALSE;
  111. ui = WhichNTProduct(csMachineName);
  112. // depending on the server type, provide an option for group type
  113. if ((ui == 2) || (ui == 1)) // standalone server or wks
  114. {
  115. pApp->m_bServer = FALSE;
  116. pApp->m_csServer = csMachineName;
  117. pApp->m_nGroupType = 1;
  118. if (pApp->m_sMode == 0) return IDD_LOCAL_USERS;
  119. else return IDD_GROUP_LIST_DIALOG;
  120. }
  121. else if (ui == 3) //pdc \ bdc
  122. {
  123. AfxMessageBox(IDS_DOMAIN_SET);
  124. // find out what domain this is a server for
  125. TCHAR* lpProv = NULL;
  126. CRomaineApp* pApp = (CRomaineApp*)AfxGetApp();
  127. long lRet = RegConnectRegistry(
  128. (LPTSTR)csMachineName.GetBuffer(csMachineName.GetLength()),
  129. HKEY_LOCAL_MACHINE,
  130. &hKey);
  131. dwRet = RegOpenKey(hKey,
  132. TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hKey );
  133. TCHAR* lpPrimaryDomain = NULL;
  134. if ((dwRet = RegQueryValueEx( hKey, TEXT("CachePrimaryDomain"), NULL, NULL, NULL, &cbProv )) == ERROR_SUCCESS)
  135. {
  136. lpPrimaryDomain = (TCHAR*)malloc(cbProv);
  137. if (lpPrimaryDomain == NULL)
  138. {
  139. AfxMessageBox(IDS_GENERIC_NO_HEAP, MB_ICONEXCLAMATION);
  140. ExitProcess(1);
  141. }
  142. dwRet = RegQueryValueEx( hKey, TEXT("CachePrimaryDomain"), NULL, NULL, (LPBYTE) lpPrimaryDomain, &cbProv );
  143. }
  144. RegCloseKey(hKey);
  145. TCHAR* lpszPrimaryDC;
  146. DWORD err = NetGetDCName(NULL,
  147. lpPrimaryDomain, // Domain Name
  148. (LPBYTE *)&lpszPrimaryDC ); // returned PDC *
  149. pApp->m_csDomain = lpPrimaryDomain;
  150. pApp->m_csServer = lpszPrimaryDC;
  151. pApp->m_bDomain = TRUE;
  152. pApp->m_bServer = FALSE;
  153. free(lpPrimaryDomain);
  154. if (pApp->m_sMode == 0) return IDD_GROUP_TYPE_DLG;
  155. else return IDD_GROUP_LIST_DIALOG;
  156. }
  157. else
  158. {
  159. AfxMessageBox(IDS_GENERIC_BAD_MACHINE);
  160. return -1;
  161. }
  162. }
  163. else
  164. {
  165. pApp->m_bDomain = TRUE;
  166. pApp->m_bServer = FALSE;
  167. TCHAR* lpszPrimaryDC;
  168. TCHAR* lpwDomain = csMachineName.GetBuffer(csMachineName.GetLength());
  169. DWORD err = NetGetDCName(NULL,
  170. lpwDomain, // Domain Name
  171. (LPBYTE *)&lpszPrimaryDC ); // returned PDC *
  172. csMachineName.ReleaseBuffer();
  173. if (err == 2453)
  174. {
  175. AfxMessageBox(IDS_NO_DC);
  176. return -1;
  177. }
  178. pApp->m_csDomain = csMachineName;
  179. pApp->m_csServer = lpszPrimaryDC;
  180. if (pApp->m_sMode == 0) return IDD_GROUP_TYPE_DLG;
  181. else return IDD_GROUP_LIST_DIALOG;
  182. }
  183. return -1;
  184. }
  185. // given a machine name, return whether its a server or wks
  186. unsigned int WhichNTProduct(CString& csMachineName)
  187. {
  188. UINT uiRetVal = 0;
  189. PSERVER_INFO_101 si101;
  190. NET_API_STATUS nas;
  191. nas = NetServerGetInfo(
  192. (LPTSTR)csMachineName.GetBuffer(csMachineName.GetLength()),
  193. 101, // info-level
  194. (LPBYTE *)&si101
  195. );
  196. if(nas != NERR_Success)
  197. {
  198. NetApiBufferFree(si101);
  199. SetLastError(nas);
  200. return 0;
  201. }
  202. if( (si101->sv101_type & SV_TYPE_DOMAIN_CTRL) ||
  203. (si101->sv101_type & SV_TYPE_DOMAIN_BAKCTRL) )
  204. uiRetVal = 3;
  205. else if (si101->sv101_type & SV_TYPE_WORKSTATION) uiRetVal = 2; //wks
  206. else if (si101->sv101_type & SV_TYPE_SERVER) uiRetVal = 1; // server
  207. NetApiBufferFree(si101);
  208. // else return Unknown
  209. return uiRetVal;
  210. }
  211. void CWhere::OnShowWindow(BOOL bShow, UINT nStatus)
  212. {
  213. CPropertyPage::OnShowWindow(bShow, nStatus);
  214. // Do the default domain expansion only once.
  215. if ((bShow) && (!m_bExpandedOnce))
  216. {
  217. m_bExpandedOnce = TRUE;
  218. m_ctServerTree.PopulateTree();
  219. }
  220. }
  221. void CWhere::OnSelchangedServerTree(NMHDR* pNMHDR, LRESULT* pResult)
  222. {
  223. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  224. HTREEITEM hItem = m_ctServerTree.GetSelectedItem();
  225. int nImage;
  226. m_ctServerTree.GetItemImage(hItem, nImage, nImage);
  227. if (nImage > 0)
  228. {
  229. CString csName;
  230. csName = m_ctServerTree.GetItemText(hItem);
  231. m_csMachineName = csName;
  232. }
  233. UpdateData(FALSE);
  234. *pResult = 0;
  235. }
  236. LRESULT CWhere::OnWizardBack()
  237. {
  238. CRomaineApp* pApp = (CRomaineApp*)AfxGetApp();
  239. if (pApp->m_sMode == 1) return IDD_WELCOME_DLG;
  240. else return CPropertyPage::OnWizardBack();
  241. }