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.

407 lines
11 KiB

  1. // WizPerm.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "shrwiz.h"
  5. #include "WizPerm.h"
  6. #include "aclpage.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CWizPerm property page
  14. IMPLEMENT_DYNCREATE(CWizPerm, CPropertyPage)
  15. CWizPerm::CWizPerm() : CPropertyPage(CWizPerm::IDD)
  16. {
  17. //{{AFX_DATA_INIT(CWizPerm)
  18. // NOTE: the ClassWizard will add member initialization here
  19. //}}AFX_DATA_INIT
  20. }
  21. CWizPerm::~CWizPerm()
  22. {
  23. }
  24. void CWizPerm::DoDataExchange(CDataExchange* pDX)
  25. {
  26. CPropertyPage::DoDataExchange(pDX);
  27. //{{AFX_DATA_MAP(CWizPerm)
  28. // NOTE: the ClassWizard will add DDX and DDV calls here
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CWizPerm, CPropertyPage)
  32. //{{AFX_MSG_MAP(CWizPerm)
  33. ON_BN_CLICKED(IDC_RADIO_PERM1, OnRadioPerm1)
  34. ON_BN_CLICKED(IDC_RADIO_PERM2, OnRadioPerm2)
  35. ON_BN_CLICKED(IDC_RADIO_PERM3, OnRadioPerm3)
  36. ON_BN_CLICKED(IDC_RADIO_PERM4, OnRadioPerm4)
  37. ON_BN_CLICKED(IDC_PERM_CUSTOM, OnPermCustom)
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CWizPerm message handlers
  42. void CWizPerm::OnRadioPerm1()
  43. {
  44. Reset();
  45. GetDlgItem(IDC_PERM_CUSTOM)->EnableWindow(FALSE);
  46. GetParent()->GetDlgItem(ID_WIZFINISH)->EnableWindow(TRUE);
  47. }
  48. void CWizPerm::OnRadioPerm2()
  49. {
  50. Reset();
  51. GetDlgItem(IDC_PERM_CUSTOM)->EnableWindow(FALSE);
  52. GetParent()->GetDlgItem(ID_WIZFINISH)->EnableWindow(TRUE);
  53. }
  54. void CWizPerm::OnRadioPerm3()
  55. {
  56. Reset();
  57. GetDlgItem(IDC_PERM_CUSTOM)->EnableWindow(FALSE);
  58. GetParent()->GetDlgItem(ID_WIZFINISH)->EnableWindow(TRUE);
  59. }
  60. void CWizPerm::OnRadioPerm4()
  61. {
  62. Reset();
  63. GetDlgItem(IDC_PERM_CUSTOM)->EnableWindow(TRUE);
  64. GetParent()->GetDlgItem(ID_WIZFINISH)->EnableWindow(FALSE);
  65. }
  66. void CWizPerm::OnPermCustom()
  67. {
  68. CWaitCursor wait;
  69. HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  70. if (FAILED(hr))
  71. {
  72. TRACE(_T("CoInitializeEx failed hr=%x"), hr);
  73. return;
  74. }
  75. CShrwizApp *pApp = (CShrwizApp *)AfxGetApp();
  76. CShareSecurityInformation *pssInfo = NULL;
  77. HPROPSHEETPAGE phPages[2];
  78. int cPages = 1;
  79. CString cstrSheetTitle, cstrSharePageTitle;
  80. cstrSheetTitle.LoadString(IDS_CUSTOM_PERM);
  81. cstrSharePageTitle.LoadString(IDS_SHARE_PERMISSIONS);
  82. // create "Share Permissions" property page
  83. BOOL bSFMOnly = (!pApp->m_bSMB && !pApp->m_bFPNW && pApp->m_bSFM);
  84. if (bSFMOnly)
  85. {
  86. PROPSHEETPAGE psp;
  87. ZeroMemory(&psp, sizeof(psp));
  88. psp.dwSize = sizeof(psp);
  89. psp.dwFlags = PSP_USETITLE;
  90. psp.hInstance = AfxGetResourceHandle();
  91. psp.pszTemplate = MAKEINTRESOURCE(IDD_NO_SHARE_PERMISSIONS);
  92. psp.pszTitle = cstrSharePageTitle;
  93. phPages[0] = CreatePropertySheetPage(&psp);
  94. if ( !(phPages[0]) )
  95. {
  96. hr = GetLastError();
  97. DisplayMessageBox(m_hWnd, MB_OK|MB_ICONWARNING, hr, IDS_FAILED_TO_CREATE_ACLUI);
  98. }
  99. } else
  100. {
  101. pssInfo = new CShareSecurityInformation(pApp->m_pSD);
  102. if (!pssInfo)
  103. {
  104. hr = E_OUTOFMEMORY;
  105. DisplayMessageBox(m_hWnd, MB_OK|MB_ICONWARNING, hr, IDS_FAILED_TO_CREATE_ACLUI);
  106. } else
  107. {
  108. pssInfo->Initialize(pApp->m_cstrTargetComputer, pApp->m_cstrShareName, cstrSharePageTitle);
  109. phPages[0] = CreateSecurityPage(pssInfo);
  110. if ( !(phPages[0]) )
  111. {
  112. hr = HRESULT_FROM_WIN32(GetLastError());
  113. DisplayMessageBox(m_hWnd, MB_OK|MB_ICONWARNING, hr, IDS_FAILED_TO_CREATE_ACLUI);
  114. }
  115. }
  116. }
  117. if (SUCCEEDED(hr))
  118. {
  119. // create "File Security" property page
  120. CFileSecurityDataObject *pfsDataObject = new CFileSecurityDataObject;
  121. if (!pfsDataObject)
  122. {
  123. hr = E_OUTOFMEMORY;
  124. DisplayMessageBox(m_hWnd, MB_OK|MB_ICONWARNING, hr, IDS_FAILED_TO_CREATE_ACLUI);
  125. // destroy pages that have not been passed to the PropertySheet function
  126. DestroyPropertySheetPage(phPages[0]);
  127. } else
  128. {
  129. pfsDataObject->Initialize(pApp->m_cstrTargetComputer, pApp->m_cstrFolder);
  130. hr = CreateFileSecurityPropPage(&(phPages[1]), pfsDataObject);
  131. if (SUCCEEDED(hr))
  132. cPages = 2;
  133. PROPSHEETHEADER psh;
  134. ZeroMemory(&psh, sizeof(psh));
  135. psh.dwSize = sizeof(psh);
  136. psh.dwFlags = PSH_DEFAULT | PSH_NOAPPLYNOW;
  137. psh.hwndParent = m_hWnd;
  138. psh.hInstance = AfxGetResourceHandle();
  139. psh.pszCaption = cstrSheetTitle;
  140. psh.nPages = cPages;
  141. psh.phpage = phPages;
  142. // create the property sheet
  143. PropertySheet(&psh);
  144. // acl pages have been successfully added, enable the Finish button
  145. GetParent()->GetDlgItem(ID_WIZFINISH)->EnableWindow(TRUE);
  146. pfsDataObject->Release();
  147. }
  148. }
  149. if (!bSFMOnly)
  150. {
  151. if (pssInfo)
  152. pssInfo->Release();
  153. }
  154. CoUninitialize();
  155. }
  156. LRESULT CWizPerm::OnWizardBack()
  157. {
  158. Reset();
  159. CShrwizApp *pApp = (CShrwizApp *)AfxGetApp();
  160. pApp->m_bNextButtonClicked = FALSE;
  161. return CPropertyPage::OnWizardBack();
  162. }
  163. BOOL CWizPerm::OnWizardFinish()
  164. {
  165. CWaitCursor wait;
  166. HRESULT hr = S_OK;
  167. BOOL bCustom = FALSE;
  168. CShrwizApp *pApp = (CShrwizApp *)AfxGetApp();
  169. pApp->m_bNextButtonClicked = TRUE;
  170. switch (GetCheckedRadioButton(IDC_RADIO_PERM1, IDC_RADIO_PERM4))
  171. {
  172. case IDC_RADIO_PERM1:
  173. {
  174. CPermEntry permEntry;
  175. hr = permEntry.Initialize(pApp->m_cstrTargetComputer, ACCOUNT_EVERYONE, SHARE_PERM_FULL_CONTROL);
  176. if (SUCCEEDED(hr))
  177. hr = BuildSecurityDescriptor(&permEntry, 1, &(pApp->m_pSD));
  178. }
  179. break;
  180. case IDC_RADIO_PERM2:
  181. {
  182. CPermEntry permEntry[2];
  183. hr = permEntry[0].Initialize(pApp->m_cstrTargetComputer, ACCOUNT_EVERYONE, SHARE_PERM_READ_ONLY);
  184. if (SUCCEEDED(hr))
  185. {
  186. hr = permEntry[1].Initialize(pApp->m_cstrTargetComputer, ACCOUNT_ADMINISTRATORS, SHARE_PERM_FULL_CONTROL);
  187. if (SUCCEEDED(hr))
  188. hr = BuildSecurityDescriptor(permEntry, 2, &(pApp->m_pSD));
  189. }
  190. }
  191. break;
  192. case IDC_RADIO_PERM3:
  193. {
  194. CPermEntry permEntry;
  195. hr = permEntry.Initialize(pApp->m_cstrTargetComputer, ACCOUNT_ADMINISTRATORS, SHARE_PERM_FULL_CONTROL);
  196. if (SUCCEEDED(hr))
  197. hr = BuildSecurityDescriptor(&permEntry, 1, &(pApp->m_pSD));
  198. }
  199. break;
  200. case IDC_RADIO_PERM4:
  201. bCustom = TRUE;
  202. break;
  203. default:
  204. ASSERT(FALSE);
  205. return FALSE; // prevent the property sheet from being destroyed
  206. }
  207. if (!bCustom && FAILED(hr))
  208. {
  209. DisplayMessageBox(m_hWnd, MB_OK|MB_ICONERROR, hr, IDS_FAILED_TO_GET_SD);
  210. return FALSE; // prevent the property sheet from being destroyed
  211. }
  212. switch (CreateShare())
  213. {
  214. case IDNO: // succeeded, no more shares
  215. break;
  216. case IDYES: // succeeded, need to create more shares
  217. pApp->Reset(); // jump back to the first page, and fall through
  218. default: // error happened
  219. return FALSE; // prevent the property sheet from being destroyed
  220. }
  221. return CPropertyPage::OnWizardFinish();
  222. }
  223. BOOL CWizPerm::OnSetActive()
  224. {
  225. ((CPropertySheet *)GetParent())->SetWizardButtons(PSWIZB_BACK | PSWIZB_FINISH);
  226. CShrwizApp *pApp = (CShrwizApp *)AfxGetApp();
  227. if (pApp->m_bNextButtonClicked)
  228. {
  229. BOOL bSFMOnly = (!pApp->m_bSMB && !pApp->m_bFPNW && pApp->m_bSFM);
  230. if (bSFMOnly)
  231. {
  232. GetDlgItem(IDC_RADIO_PERM1)->EnableWindow(FALSE);
  233. GetDlgItem(IDC_RADIO_PERM2)->EnableWindow(FALSE);
  234. GetDlgItem(IDC_RADIO_PERM3)->EnableWindow(FALSE);
  235. CheckRadioButton(IDC_RADIO_PERM1, IDC_RADIO_PERM4, IDC_RADIO_PERM4);
  236. OnRadioPerm4();
  237. // enable the Finish button initially if only MAC is selected
  238. GetParent()->GetDlgItem(ID_WIZFINISH)->EnableWindow(TRUE);
  239. } else
  240. {
  241. GetDlgItem(IDC_RADIO_PERM1)->EnableWindow(TRUE);
  242. GetDlgItem(IDC_RADIO_PERM2)->EnableWindow(TRUE);
  243. GetDlgItem(IDC_RADIO_PERM3)->EnableWindow(TRUE);
  244. CheckRadioButton(IDC_RADIO_PERM1, IDC_RADIO_PERM4, IDC_RADIO_PERM1);
  245. OnRadioPerm1();
  246. }
  247. }
  248. return CPropertyPage::OnSetActive();
  249. }
  250. void CWizPerm::Reset()
  251. {
  252. CShrwizApp *pApp = (CShrwizApp *)AfxGetApp();
  253. if (pApp->m_pSD)
  254. {
  255. LocalFree((HLOCAL)(pApp->m_pSD));
  256. pApp->m_pSD = NULL;
  257. }
  258. }
  259. int
  260. CWizPerm::CreateShare()
  261. {
  262. DWORD dwRet = NERR_Success;
  263. CShrwizApp *pApp = (CShrwizApp *)AfxGetApp();
  264. UINT iSuccess = 0;
  265. CString cstrSuccessMsg;
  266. CString cstrFailureMsg;
  267. CString cstrLineReturn;
  268. cstrLineReturn.LoadString(IDS_LINE_RETURN);
  269. do {
  270. CString cstrMsg;
  271. cstrSuccessMsg.LoadString(IDS_SUCCEEDED_IN_CREATING_SHARE);
  272. if (pApp->m_bSMB)
  273. {
  274. CString cstrSMB;
  275. cstrSMB.LoadString(IDS_SMB_CLIENTS);
  276. dwRet = SMBCreateShare(
  277. pApp->m_cstrTargetComputer,
  278. pApp->m_cstrShareName,
  279. pApp->m_cstrShareDescription,
  280. pApp->m_cstrFolder,
  281. pApp->m_pSD
  282. );
  283. if (NERR_Success != dwRet)
  284. {
  285. GetDisplayMessage(cstrMsg, dwRet, IDS_FAILED_TO_CREATE_SHARE, cstrSMB);
  286. cstrFailureMsg += cstrMsg;
  287. cstrFailureMsg += cstrLineReturn;
  288. } else
  289. {
  290. iSuccess++;
  291. cstrSuccessMsg += cstrSMB;
  292. cstrSuccessMsg += cstrLineReturn;
  293. pApp->m_bSMB = FALSE;
  294. if (pApp->m_bIsLocal) // refresh shell
  295. SHChangeNotify(SHCNE_NETSHARE, SHCNF_PATH | SHCNF_FLUSH, pApp->m_cstrFolder, 0);
  296. }
  297. }
  298. if (pApp->m_bFPNW)
  299. {
  300. CString cstrFPNW;
  301. cstrFPNW.LoadString(IDS_FPNW_CLIENTS);
  302. dwRet = FPNWCreateShare(
  303. pApp->m_cstrTargetComputer,
  304. pApp->m_cstrShareName,
  305. pApp->m_cstrFolder,
  306. pApp->m_pSD,
  307. pApp->m_hLibFPNW
  308. );
  309. if (NERR_Success != dwRet)
  310. {
  311. GetDisplayMessage(cstrMsg, dwRet, IDS_FAILED_TO_CREATE_SHARE, cstrFPNW);
  312. cstrFailureMsg += cstrMsg;
  313. cstrFailureMsg += cstrLineReturn;
  314. } else
  315. {
  316. iSuccess++;
  317. cstrSuccessMsg += cstrFPNW;
  318. cstrSuccessMsg += cstrLineReturn;
  319. pApp->m_bFPNW = FALSE;
  320. }
  321. }
  322. if (pApp->m_bSFM)
  323. {
  324. CString cstrSFM;
  325. cstrSFM.LoadString(IDS_SFM_CLIENTS);
  326. dwRet = SFMCreateShare(
  327. pApp->m_cstrTargetComputer,
  328. pApp->m_cstrMACShareName,
  329. pApp->m_cstrFolder,
  330. pApp->m_hLibSFM
  331. );
  332. if (NERR_Success != dwRet)
  333. {
  334. GetDisplayMessage(cstrMsg, dwRet, IDS_FAILED_TO_CREATE_SHARE, cstrSFM);
  335. cstrFailureMsg += cstrMsg;
  336. cstrFailureMsg += cstrLineReturn;
  337. } else
  338. {
  339. iSuccess++;
  340. cstrSuccessMsg += cstrSFM;
  341. cstrSuccessMsg += cstrLineReturn;
  342. pApp->m_bSFM = FALSE;
  343. }
  344. }
  345. } while (0);
  346. cstrSuccessMsg += cstrLineReturn;
  347. if (cstrFailureMsg.IsEmpty())
  348. {
  349. CString cstrMoreShares;
  350. cstrMoreShares.LoadString(IDS_OPERATION_SUCCEEDED_MORE_SHARES);
  351. return DisplayMessageBox(m_hWnd, MB_YESNO|MB_ICONINFORMATION, 0, 0,
  352. cstrSuccessMsg + cstrMoreShares);
  353. } else
  354. {
  355. DisplayMessageBox(m_hWnd, MB_OK|MB_ICONERROR, 0, 0,
  356. (iSuccess ? (cstrSuccessMsg + cstrFailureMsg) : cstrFailureMsg));
  357. return IDRETRY;
  358. }
  359. }