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.

399 lines
11 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright (c) 1997-1999 Microsoft Corporation
  4. /**********************************************************************/
  5. /*
  6. PAGEBASE.CPP
  7. This file contains the implementation of the CBasePage base class.
  8. */
  9. #include "precomp.h"
  10. #include "pagebase.h"
  11. #include "ServiceThread.h"
  12. #include "resource.h"
  13. #include <commctrl.h>
  14. #include "DataSrc.h"
  15. #include <commdlg.h>
  16. #include <cominit.h>
  17. //#include <afxres.h>
  18. #include "WMIHelp.h"
  19. #ifdef SNAPIN
  20. const TCHAR c_HelpFile2[] = _T("newfeat1.hlp");
  21. #else
  22. const TCHAR c_HelpFile2[] = _T("WbemCntl.hlp");
  23. #endif
  24. //-------------------------------------------------------------------
  25. CBasePage::CBasePage(DataSource *ds, WbemServiceThread *serviceThread) :
  26. m_DS(ds),
  27. m_userCancelled(false),
  28. m_hDlg(NULL),
  29. m_alreadyAsked(false),
  30. m_service(NULL),
  31. g_serviceThread(serviceThread)
  32. {
  33. if((g_serviceThread != 0) &&
  34. g_serviceThread->m_status == WbemServiceThread::ready)
  35. {
  36. m_WbemServices = g_serviceThread->m_WbemServices;
  37. m_WbemServices.GetServices(&m_service);
  38. m_WbemServices.SetBlanket(m_service);
  39. }
  40. if (m_DS)
  41. {
  42. m_DS->AddRef();
  43. }
  44. }
  45. //-------------------------------------------------------------------
  46. CBasePage::CBasePage(CWbemServices &service) :
  47. m_DS(NULL),
  48. m_userCancelled(false),
  49. m_hDlg(NULL),
  50. m_alreadyAsked(false),
  51. m_service(NULL),
  52. g_serviceThread(NULL)
  53. {
  54. m_WbemServices = service;
  55. }
  56. //-------------------------------------------------------------------
  57. CBasePage::~CBasePage( void )
  58. {
  59. if(m_service)
  60. {
  61. m_service->Release();
  62. m_service = 0;
  63. }
  64. m_WbemServices.DisconnectServer();
  65. m_alreadyAsked = false;
  66. if (m_DS)
  67. {
  68. m_DS->Release();
  69. }
  70. }
  71. //-------------------------------------------------------------------
  72. HPROPSHEETPAGE CBasePage::CreatePropSheetPage(LPCTSTR pszDlgTemplate,
  73. LPCTSTR pszDlgTitle,
  74. DWORD moreFlags)
  75. {
  76. PROPSHEETPAGE psp;
  77. psp.dwSize = sizeof(psp);
  78. psp.dwFlags = PSP_USECALLBACK | moreFlags;
  79. psp.hInstance = _Module.GetModuleInstance();
  80. psp.pszTemplate = pszDlgTemplate;
  81. psp.pszTitle = pszDlgTitle;
  82. psp.pfnDlgProc = CBasePage::_DlgProc;
  83. psp.lParam = (LPARAM)this;
  84. psp.pfnCallback = CBasePage::_PSPageCallback;
  85. if (pszDlgTitle != NULL)
  86. psp.dwFlags |= PSP_USETITLE;
  87. return CreatePropertySheetPage(&psp);
  88. }
  89. //-------------------------------------------------------------------
  90. UINT CBasePage::PSPageCallback(HWND hwnd,
  91. UINT uMsg,
  92. LPPROPSHEETPAGE ppsp)
  93. {
  94. return S_OK;
  95. }
  96. //-------------------------------------------------------------------
  97. INT_PTR CALLBACK CBasePage::_DlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  98. {
  99. CBasePage *pThis = (CBasePage *)GetWindowLongPtr(hDlg, DWLP_USER);
  100. // The following messages arrive before WM_INITDIALOG
  101. // which means pThis is NULL for them. We don't need these
  102. // messages so let DefDlgProc handle them.
  103. //
  104. // WM_SETFONT
  105. // WM_NOTIFYFORMAT
  106. // WM_NOTIFY (LVN_HEADERCREATED)
  107. if (uMsg == WM_INITDIALOG)
  108. {
  109. pThis = (CBasePage *)(((LPPROPSHEETPAGE)lParam)->lParam);
  110. SetWindowLongPtr(hDlg, DWLP_USER, (LPARAM)pThis);
  111. }
  112. if (pThis != NULL) return pThis->DlgProc(hDlg, uMsg, wParam, lParam);
  113. return FALSE;
  114. }
  115. //-------------------------------------------------------------------
  116. UINT CALLBACK CBasePage::_PSPageCallback(HWND hWnd, UINT uMsg, LPPROPSHEETPAGE ppsp)
  117. {
  118. CBasePage *pThis = (CBasePage *)ppsp->lParam;
  119. if (pThis)
  120. {
  121. UINT nResult = pThis->PSPageCallback(hWnd, uMsg, ppsp);
  122. switch (uMsg)
  123. {
  124. case PSPCB_CREATE:
  125. break;
  126. case PSPCB_RELEASE:
  127. ppsp->lParam = NULL;
  128. delete pThis;
  129. break;
  130. }
  131. }
  132. //
  133. // Always return non-zero or else our tab will disappear and whichever
  134. // property page becomes active won't repaint properly. Instead, use
  135. // the m_bAbortPage flag during WM_INITDIALOG to disable the page if
  136. // the callback failed.
  137. //
  138. return 1;
  139. }
  140. //---------------------------------------------------------
  141. typedef struct {
  142. LOGIN_CREDENTIALS *credentials;
  143. } LOGIN_CFG;
  144. //------------------------------------------------------------------------
  145. size_t CredentialUserLen3(LOGIN_CREDENTIALS *credentials)
  146. {
  147. return credentials->authIdent->UserLength;
  148. }
  149. //------------------------------------------------------------------------
  150. void CredentialUser3(LOGIN_CREDENTIALS *credentials, LPTSTR *user)
  151. {
  152. bstr_t trustee = _T("");
  153. if ((TCHAR *)trustee == NULL)
  154. return;
  155. if(credentials->authIdent->DomainLength > 0)
  156. {
  157. trustee += credentials->authIdent->Domain;
  158. trustee += _T("\\");
  159. trustee += credentials->authIdent->User;
  160. }
  161. else
  162. {
  163. trustee = credentials->authIdent->User;
  164. if ((TCHAR *)trustee == NULL)
  165. return;
  166. }
  167. #ifdef UNICODE
  168. if(credentials->authIdent->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
  169. {
  170. size_t size = mbstowcs(NULL, trustee, 0);
  171. *user = new wchar_t[size+1];
  172. if(*user != NULL)
  173. mbstowcs(*user, trustee, size+1);
  174. }
  175. else // already UNICODE
  176. {
  177. size_t size = wcslen(trustee);
  178. *user = new wchar_t[size+1];
  179. if(*user != NULL)
  180. wcscpy(*user, trustee);
  181. }
  182. #else
  183. if(credentials->authIdent->Flags == SEC_WINNT_AUTH_IDENTITY_ANSI)
  184. {
  185. size_t size = strlen(trustee);
  186. *user = new char[size+1];
  187. if(*user != NULL)
  188. strcpy(*user, (char *)trustee);
  189. }
  190. else // convert the UNICODE
  191. {
  192. size_t size = wcstombs(NULL, trustee, 0);
  193. *user = new char[size+1];
  194. if(*user != NULL)
  195. wcstombs(*user, trustee, size+1);
  196. }
  197. #endif
  198. }
  199. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  200. void SetCurrentUser2(HWND hDlg, bool currUser)
  201. {
  202. Button_SetCheck(GetDlgItem(hDlg, IDC_CHECKCURRENTUSER),
  203. (currUser? BST_CHECKED:BST_UNCHECKED));
  204. BOOL enable = (currUser? FALSE: TRUE);
  205. ::EnableWindow(GetDlgItem(hDlg, IDC_EDITUSERNAME), enable);
  206. ::EnableWindow(GetDlgItem(hDlg, IDC_EDITPASSWORD), enable);
  207. ::EnableWindow(GetDlgItem(hDlg, IDC_USER_LABEL), enable);
  208. ::EnableWindow(GetDlgItem(hDlg, IDC_PW_LABEL), enable);
  209. }
  210. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  211. const static DWORD logonHelpIDs[] = { // Context Help IDs
  212. IDC_CHECKCURRENTUSER, IDH_WMI_CTRL_GENERAL_WMILOGIN_CHECKBOX,
  213. IDC_USER_LABEL, IDH_WMI_CTRL_GENERAL_WMILOGIN_USERNAME,
  214. IDC_EDITUSERNAME, IDH_WMI_CTRL_GENERAL_WMILOGIN_USERNAME,
  215. IDC_PW_LABEL, IDH_WMI_CTRL_GENERAL_WMILOGIN_PASSWORD,
  216. IDC_EDITPASSWORD, IDH_WMI_CTRL_GENERAL_WMILOGIN_PASSWORD,
  217. 0, 0
  218. };
  219. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  220. INT_PTR CALLBACK LoginDlgProc2(HWND hwndDlg,
  221. UINT uMsg,
  222. WPARAM wParam,
  223. LPARAM lParam)
  224. {
  225. BOOL retval = FALSE;
  226. switch(uMsg)
  227. {
  228. case WM_INITDIALOG:
  229. { //BEGIN
  230. SetWindowLongPtr(hwndDlg, DWLP_USER, lParam);
  231. LOGIN_CFG *data = (LOGIN_CFG *)GetWindowLongPtr(hwndDlg, DWLP_USER);
  232. SetCurrentUser2(hwndDlg, data->credentials->currUser);
  233. } //END
  234. retval = TRUE;
  235. break;
  236. case WM_COMMAND:
  237. {
  238. LOGIN_CFG *data = (LOGIN_CFG *)GetWindowLongPtr(hwndDlg, DWLP_USER);
  239. switch(LOWORD(wParam))
  240. {
  241. case IDC_CHECKCURRENTUSER:
  242. {
  243. if(HIWORD(wParam) == BN_CLICKED)
  244. {
  245. bool currUser = (IsDlgButtonChecked(hwndDlg, IDC_CHECKCURRENTUSER) == BST_CHECKED ?true:false);
  246. // toggle and respond.
  247. SetCurrentUser2(hwndDlg, currUser);
  248. }
  249. }
  250. break;
  251. case IDOK:
  252. {
  253. if(HIWORD(wParam) == BN_CLICKED)
  254. {
  255. data->credentials->currUser = (IsDlgButtonChecked(hwndDlg, IDC_CHECKCURRENTUSER) == BST_CHECKED ?true:false);
  256. if(data->credentials->currUser == false)
  257. {
  258. TCHAR user[100] = {0}, pw[100] = {0};
  259. GetWindowText(GetDlgItem(hwndDlg, IDC_EDITUSERNAME), user, 100);
  260. GetWindowText(GetDlgItem(hwndDlg, IDC_EDITPASSWORD), pw, 100);
  261. BSTR bDomUser, bUser = NULL, bDomain = NULL, bAuth = NULL;
  262. //#ifdef SNAPIN
  263. wchar_t *temp = pw;
  264. bDomUser = SysAllocString(user);
  265. /*#else
  266. wchar_t temp[100] = {0};
  267. mbstowcs(temp, user, 100);
  268. bDomUser = SysAllocString(temp);
  269. mbstowcs(temp, pw, 100);
  270. #endif*/
  271. if (bDomUser != NULL &&
  272. SUCCEEDED(DetermineLoginType(bDomain,
  273. bUser,
  274. bAuth,
  275. bDomUser)))
  276. {
  277. if(data->credentials->authIdent != 0)
  278. {
  279. if(data->credentials->fullAcct)
  280. {
  281. data->credentials->fullAcct[0] = 0;
  282. }
  283. WbemFreeAuthIdentity(data->credentials->authIdent);
  284. data->credentials->authIdent = 0;
  285. }
  286. if (SUCCEEDED(WbemAllocAuthIdentity(bUser,
  287. temp,
  288. bDomain,
  289. &(data->credentials->authIdent))))
  290. {
  291. _tcscpy(data->credentials->fullAcct,
  292. user);
  293. }
  294. }
  295. }
  296. EndDialog(hwndDlg, IDOK);
  297. }
  298. }
  299. break;
  300. case IDCANCEL:
  301. {
  302. if(HIWORD(wParam) == BN_CLICKED)
  303. {
  304. EndDialog(hwndDlg, IDCANCEL);
  305. }
  306. }
  307. break;
  308. default:
  309. return(FALSE);
  310. } // switch
  311. break;
  312. } // - - - - - - - - endswitch LOWORD()
  313. break;
  314. case WM_HELP:
  315. if(IsWindowEnabled(hwndDlg))
  316. {
  317. WinHelp((HWND)((LPHELPINFO)lParam)->hItemHandle,
  318. c_HelpFile2,
  319. HELP_WM_HELP,
  320. (ULONG_PTR)logonHelpIDs);
  321. }
  322. break;
  323. case WM_CONTEXTMENU:
  324. if(IsWindowEnabled(hwndDlg))
  325. {
  326. WinHelp(hwndDlg, c_HelpFile2,
  327. HELP_CONTEXTMENU,
  328. (ULONG_PTR)logonHelpIDs);
  329. }
  330. break;
  331. default: break;
  332. } //endswitch uMsg
  333. return retval;
  334. }
  335. //---------------------------------------------------------
  336. INT_PTR CBasePage::DisplayLoginDlg(HWND hWnd,
  337. LOGIN_CREDENTIALS *credentials)
  338. {
  339. LOGIN_CFG cfg;
  340. cfg.credentials = credentials;
  341. return DialogBoxParam(_Module.GetModuleInstance(),
  342. MAKEINTRESOURCE(IDD_LOGIN),
  343. hWnd, LoginDlgProc2,
  344. (LPARAM)&cfg);
  345. }