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.

378 lines
10 KiB

  1. // shrwiz.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "wizFirst.h"
  5. #include "wizDir.h"
  6. #include "wizClnt.h"
  7. #include "wizPerm.h"
  8. #include "wizLast.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CShrwizApp
  16. BEGIN_MESSAGE_MAP(CShrwizApp, CWinApp)
  17. //{{AFX_MSG_MAP(CShrwizApp)
  18. // NOTE - the ClassWizard will add and remove mapping macros here.
  19. // DO NOT EDIT what you see in these blocks of generated code!
  20. //}}AFX_MSG
  21. // ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  22. END_MESSAGE_MAP()
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CShrwizApp construction
  25. CShrwizApp::CShrwizApp()
  26. {
  27. m_hTitleFont = NULL;
  28. // filled in by initialization routine in shrwiz.cpp
  29. m_cstrTargetComputer.Empty();
  30. m_cstrUNCPrefix.Empty();
  31. m_bIsLocal = FALSE;
  32. m_bServerSBS = FALSE;
  33. m_bServerSFM = FALSE;
  34. m_bCSC = FALSE;
  35. m_hLibNTDLL = NULL;
  36. m_pfnIsDosDeviceName = NULL;
  37. m_hLibSFM = NULL;
  38. m_pWizard = NULL;
  39. // filled in by the folder page
  40. m_cstrFolder.Empty();
  41. // filled in by the client page
  42. m_cstrShareName.Empty();
  43. m_cstrShareDescription.Empty();
  44. m_cstrMACShareName.Empty();
  45. m_bSMB = TRUE;
  46. m_bSFM = FALSE;
  47. m_dwCSCFlag = CSC_CACHE_MANUAL_REINT;
  48. // filled in by the permission page
  49. m_pSD = NULL;
  50. m_cstrFinishTitle.Empty();
  51. m_cstrFinishStatus.Empty();
  52. m_cstrFinishSummary.Empty();
  53. m_bFolderPathPageInitialized = FALSE;
  54. m_bShareNamePageInitialized = FALSE;
  55. m_bPermissionsPageInitialized = FALSE;
  56. }
  57. CShrwizApp::~CShrwizApp()
  58. {
  59. TRACE(_T("CShrwizApp::~CShrwizApp\n"));
  60. if (m_hLibNTDLL)
  61. FreeLibrary(m_hLibNTDLL);
  62. if (m_hLibSFM)
  63. FreeLibrary(m_hLibSFM);
  64. if (m_pSD)
  65. LocalFree((HLOCAL)m_pSD);
  66. }
  67. void
  68. CShrwizApp::Reset()
  69. {
  70. // filled in by the folder page
  71. m_cstrFolder.Empty();
  72. // filled in by the client page
  73. m_cstrShareName.Empty();
  74. m_cstrShareDescription.Empty();
  75. m_cstrMACShareName.Empty();
  76. m_bSMB = TRUE;
  77. m_bSFM = FALSE;
  78. m_dwCSCFlag = CSC_CACHE_MANUAL_REINT;
  79. // filled in by the permission page
  80. if (m_pSD)
  81. {
  82. LocalFree((HLOCAL)m_pSD);
  83. m_pSD = NULL;
  84. }
  85. m_cstrFinishTitle.Empty();
  86. m_cstrFinishStatus.Empty();
  87. m_bFolderPathPageInitialized = FALSE;
  88. m_bShareNamePageInitialized = FALSE;
  89. m_bPermissionsPageInitialized = FALSE;
  90. m_pWizard->PostMessage(PSM_SETCURSEL, 1);
  91. }
  92. /////////////////////////////////////////////////////////////////////////////
  93. // The one and only CShrwizApp object
  94. CShrwizApp theApp;
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CShrwizApp initialization
  97. BOOL CShrwizApp::InitInstance()
  98. {
  99. InitCommonControls(); // use XP theme-aware common controls
  100. Enable3dControls(); // Call this when using MFC in a shared DLL
  101. if (!GetTargetComputer())
  102. return FALSE;
  103. m_pMainWnd = NULL;
  104. HBITMAP hbmWatermark = NULL;
  105. HBITMAP hbmHeader = NULL;
  106. do {
  107. hbmWatermark = (HBITMAP)LoadImage(m_hInstance, MAKEINTRESOURCE(IDB_WATERMARK), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
  108. if (!hbmWatermark)
  109. break;
  110. hbmHeader = (HBITMAP)LoadImage(m_hInstance, MAKEINTRESOURCE(IDB_HEADER), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
  111. if (!hbmHeader)
  112. break;
  113. m_pWizard = new CPropertySheetEx(IDS_WIZARD_TITLE, NULL, 0, hbmWatermark, NULL, hbmHeader);
  114. if (!m_pWizard)
  115. break;
  116. CWizWelcome welcomePage;
  117. CWizFolder folderPage;
  118. CWizClient0 clientPage0;
  119. CWizClient clientPage;
  120. CWizPerm permPage;
  121. CWizFinish finishPage;
  122. //Set up the font for the titles on the welcome page
  123. NONCLIENTMETRICS ncm = {0};
  124. ncm.cbSize = sizeof(ncm);
  125. SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0);
  126. LOGFONT TitleLogFont = ncm.lfMessageFont;
  127. TitleLogFont.lfWeight = FW_BOLD;
  128. lstrcpy(TitleLogFont.lfFaceName, _T("Verdana Bold"));
  129. HDC hdc = GetDC(NULL); //gets the screen DC
  130. INT FontSize = 12;
  131. TitleLogFont.lfHeight = 0 - GetDeviceCaps(hdc, LOGPIXELSY) * FontSize / 72;
  132. m_hTitleFont = CreateFontIndirect(&TitleLogFont);
  133. ReleaseDC(NULL, hdc);
  134. if (!m_hTitleFont)
  135. break;
  136. // add wizard pages
  137. m_pWizard->AddPage(&welcomePage);
  138. m_pWizard->AddPage(&folderPage);
  139. if (m_bServerSFM)
  140. m_pWizard->AddPage(&clientPage);
  141. else
  142. m_pWizard->AddPage(&clientPage0);
  143. m_pWizard->AddPage(&permPage);
  144. m_pWizard->AddPage(&finishPage);
  145. m_pWizard->SetWizardMode();
  146. (m_pWizard->m_psh).dwFlags |= (PSH_WIZARD97 | PSH_USEHBMWATERMARK | PSH_USEHBMHEADER);
  147. // start the wizard
  148. m_pWizard->DoModal();
  149. } while (FALSE);
  150. if (m_hTitleFont)
  151. DeleteObject(m_hTitleFont);
  152. if (m_pWizard)
  153. {
  154. delete m_pWizard;
  155. m_pWizard = NULL;
  156. }
  157. if (hbmHeader)
  158. DeleteObject(hbmHeader);
  159. if (hbmWatermark)
  160. DeleteObject(hbmWatermark);
  161. // Since the dialog has been closed, return FALSE so that we exit the
  162. // application, rather than start the application's message pump.
  163. return FALSE;
  164. }
  165. // ProductSuite:
  166. // Key: HKLM\SYSTEM\CurrentControlSet\Control\ProductOptions
  167. // Value: ProductSuite
  168. // Type: REG_MULTI_SZ
  169. // Data: for SBS, it by default contains 2 strings "Small Business" and "Small Business(Restricted)"
  170. #define KEY_PRODUCT_SUITE _T("SYSTEM\\CurrentControlSet\\Control\\ProductOptions")
  171. #define VALUE_PRODUCT_SUITE _T("ProductSuite")
  172. #define SBS_PRODUCT_SUITE _T("Small Business")
  173. BOOL IsServerSBS(IN LPCTSTR pszComputer)
  174. {
  175. BOOL bSBS = FALSE;
  176. HKEY hkeyMachine = HKEY_LOCAL_MACHINE;
  177. DWORD dwErr = ERROR_SUCCESS;
  178. if (pszComputer && *pszComputer)
  179. {
  180. dwErr = RegConnectRegistry(pszComputer, HKEY_LOCAL_MACHINE, &hkeyMachine);
  181. if (ERROR_SUCCESS != dwErr)
  182. return bSBS;
  183. }
  184. HKEY hkey = NULL;
  185. dwErr = RegOpenKeyEx(hkeyMachine, KEY_PRODUCT_SUITE, 0, KEY_READ, &hkey ) ;
  186. if (ERROR_SUCCESS == dwErr)
  187. {
  188. DWORD cbData = 0;
  189. dwErr = RegQueryValueEx(hkey, VALUE_PRODUCT_SUITE, 0, NULL, NULL, &cbData);
  190. if (ERROR_SUCCESS == dwErr)
  191. {
  192. LPBYTE pBuffer = (LPBYTE)calloc(cbData, sizeof(BYTE));
  193. if (pBuffer)
  194. {
  195. DWORD dwType = REG_MULTI_SZ;
  196. dwErr = RegQueryValueEx(hkey, VALUE_PRODUCT_SUITE, 0, &dwType, pBuffer, &cbData);
  197. if (ERROR_SUCCESS == dwErr && REG_MULTI_SZ == dwType)
  198. {
  199. // pBuffer is pointing at an array of null-terminated strings, terminated by two null characters.
  200. PTSTR p = (PTSTR)pBuffer;
  201. while (*p)
  202. {
  203. if (!_tcsncmp(p, SBS_PRODUCT_SUITE, lstrlen(SBS_PRODUCT_SUITE)))
  204. {
  205. bSBS = TRUE;
  206. break;
  207. }
  208. p += lstrlen(p) + 1; // point to the next null-terminated string
  209. }
  210. }
  211. free(pBuffer);
  212. }
  213. }
  214. RegCloseKey(hkey);
  215. }
  216. if (pszComputer && *pszComputer)
  217. RegCloseKey(hkeyMachine);
  218. return bSBS;
  219. }
  220. BOOL
  221. CShrwizApp::GetTargetComputer()
  222. {
  223. BOOL bReturn = FALSE;
  224. m_cstrTargetComputer.Empty();
  225. m_cstrUNCPrefix.Empty();
  226. m_bIsLocal = FALSE;
  227. do { // false loop
  228. CString cstrCmdLine = m_lpCmdLine;
  229. cstrCmdLine.TrimLeft();
  230. cstrCmdLine.TrimRight();
  231. if (cstrCmdLine.IsEmpty() || cstrCmdLine == _T("/s"))
  232. { // local computer
  233. TCHAR szBuffer[MAX_COMPUTERNAME_LENGTH + 1];
  234. DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
  235. if (GetComputerName(szBuffer, &dwSize))
  236. {
  237. m_cstrTargetComputer = szBuffer;
  238. m_bIsLocal = TRUE;
  239. } else
  240. {
  241. DisplayMessageBox(::GetActiveWindow(), MB_OK|MB_ICONERROR, GetLastError(), IDS_CANNOT_GET_LOCAL_COMPUTER);
  242. break;
  243. }
  244. } else if (_T("/s") == cstrCmdLine.Left(2))
  245. {
  246. if (_istspace(cstrCmdLine.GetAt(2)))
  247. {
  248. cstrCmdLine = cstrCmdLine.Mid(3);
  249. cstrCmdLine.TrimLeft();
  250. if ( _T("\\\\") == cstrCmdLine.Left(2) )
  251. m_cstrTargetComputer = cstrCmdLine.Mid(2);
  252. else
  253. m_cstrTargetComputer = cstrCmdLine;
  254. }
  255. }
  256. if (m_cstrTargetComputer.IsEmpty())
  257. {
  258. CString cstrAppName = AfxGetAppName();
  259. CString cstrUsage;
  260. cstrUsage.FormatMessage(IDS_CMDLINE_PARAMS, cstrAppName);
  261. DisplayMessageBox(::GetActiveWindow(), MB_OK|MB_ICONERROR, 0, IDS_APP_USAGE, cstrUsage);
  262. break;
  263. } else
  264. {
  265. SERVER_INFO_101 *pInfo = NULL;
  266. DWORD dwRet = ::NetServerGetInfo(
  267. const_cast<LPTSTR>(static_cast<LPCTSTR>(m_cstrTargetComputer)), 101, (LPBYTE*)&pInfo);
  268. if (NERR_Success == dwRet)
  269. {
  270. if ((pInfo->sv101_version_major & MAJOR_VERSION_MASK) >= 5)
  271. {
  272. m_bCSC = TRUE;
  273. m_bServerSBS = IsServerSBS(static_cast<LPCTSTR>(m_cstrTargetComputer));
  274. // we compile this wizard with UNICODE defined.
  275. #ifdef UNICODE
  276. // load ntdll.
  277. m_hLibNTDLL = ::LoadLibrary (_T("ntdll.dll"));
  278. if ( m_hLibNTDLL )
  279. {
  280. m_pfnIsDosDeviceName = (PfnRtlIsDosDeviceName_U)::GetProcAddress(m_hLibNTDLL, "RtlIsDosDeviceName_U");
  281. }
  282. #endif // UNICODE
  283. }
  284. m_bServerSFM = (pInfo->sv101_type & SV_TYPE_AFP) &&
  285. (m_hLibSFM = LoadLibrary(_T("sfmapi.dll")));
  286. NetApiBufferFree(pInfo);
  287. if (!m_bIsLocal)
  288. m_bIsLocal = IsLocalComputer(m_cstrTargetComputer);
  289. bReturn = TRUE;
  290. } else
  291. {
  292. DisplayMessageBox(::GetActiveWindow(), MB_OK|MB_ICONERROR, dwRet, IDS_CANNOT_CONTACT_COMPUTER, m_cstrTargetComputer);
  293. break;
  294. }
  295. }
  296. } while (0);
  297. if (!bReturn)
  298. {
  299. m_cstrTargetComputer.Empty();
  300. } else
  301. {
  302. m_cstrUNCPrefix = _T("\\\\");
  303. m_cstrUNCPrefix += m_cstrTargetComputer;
  304. m_cstrUNCPrefix += _T("\\");
  305. }
  306. return bReturn;
  307. }