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.

246 lines
6.6 KiB

  1. // amisafeDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "amisafe.h"
  5. #include "amisafeDlg.h"
  6. #include "Security.h"
  7. #include "SendMail.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. #define ICONID_GREENCHECK 0
  14. #define ICONID_REDCROSS 1
  15. #define ICONID_BLUEDOT 2
  16. BEGIN_MESSAGE_MAP(CAmisafeDlg, CDialog)
  17. //{{AFX_MSG_MAP(CAmisafeDlg)
  18. ON_WM_CLOSE()
  19. //}}AFX_MSG_MAP
  20. END_MESSAGE_MAP()
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CAmisafeDlg dialog
  23. CAmisafeDlg::CAmisafeDlg(CWnd* pParent /*=NULL*/)
  24. : CDialog(CAmisafeDlg::IDD, pParent), hThread(NULL)
  25. {
  26. //{{AFX_DATA_INIT(CAmisafeDlg)
  27. // NOTE: the ClassWizard will add member initialization here
  28. //}}AFX_DATA_INIT
  29. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  30. imagelist.Create(IDB_BITMAP1, 16, 0, RGB(255,255,255));
  31. // find out who to send to from the command line.
  32. szRecipients[0] = '\0';
  33. #if 0
  34. LPTSTR szCmdLine = GetCommandLine();
  35. if (szCmdLine != NULL) {
  36. if (*szCmdLine == '"') {
  37. for (szCmdLine++; *szCmdLine != '"'; szCmdLine++);
  38. }
  39. while (*szCmdLine == ' ') szCmdLine++;
  40. strncpy(szRecipients, szCmdLine,
  41. sizeof(szRecipients) / sizeof(TCHAR) - 1);
  42. szRecipients[sizeof(szRecipients) / sizeof(TCHAR) - 1] = '\0';
  43. }
  44. #else
  45. if (__argc > 1 && *__argv[1] != '\0') {
  46. strncpy(szRecipients, __argv[1],
  47. sizeof(szRecipients) / sizeof(TCHAR) - 1);
  48. szRecipients[sizeof(szRecipients) / sizeof(TCHAR) - 1] = '\0';
  49. }
  50. #endif
  51. // if we still do not have a valid recipient, then apply a default.
  52. if (lstrlen(szRecipients) < 2) {
  53. if (!CSecurity::GetLoggedInUsername(szRecipients, sizeof(szRecipients) / sizeof(TCHAR))) {
  54. strcpy(szRecipients, "dbayer");
  55. }
  56. }
  57. }
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CAmisafeDlg message handlers
  60. BOOL CAmisafeDlg::OnInitDialog()
  61. {
  62. CDialog::OnInitDialog();
  63. HWND hwndList = ::GetDlgItem(GetSafeHwnd(), IDC_LIST1);
  64. if (!hwndList) return FALSE;
  65. if (!listcontrol.Attach(hwndList)) return FALSE;
  66. listcontrol.SetImageList(&imagelist, LVSIL_SMALL);
  67. RECT listrect;
  68. listcontrol.GetClientRect(&listrect);
  69. listcontrol.InsertColumn(0, "text", LVCFMT_LEFT, listrect.right);
  70. SetIcon(m_hIcon, TRUE); // Set big icon
  71. SetIcon(m_hIcon, FALSE); // Set small icon
  72. // TODO: Add extra initialization here
  73. listcontrol.DeleteAllItems();
  74. InsertMessage("Security checker starting up...", ICONID_BLUEDOT);
  75. hThread = ::CreateThread(NULL, 0, ThreadProc, (LPVOID) this, 0, NULL);
  76. return TRUE; // return TRUE unless you set the focus to a control
  77. }
  78. void CAmisafeDlg::RunSecurityCheck()
  79. {
  80. //
  81. // Determine if our process token is a member of the Administrators group.
  82. //
  83. Sleep(2000);
  84. if (CSecurity::IsAdministrator()) {
  85. InsertMessage("Running with Administrator privileges", ICONID_GREENCHECK);
  86. } else {
  87. InsertMessage("Running without Administrator privileges", ICONID_REDCROSS);
  88. }
  89. //
  90. // Determine if our process token is "untrusted" and does not have access to
  91. // resources that are ACLed specifically to the TokenUser SID.
  92. //
  93. Sleep(2000);
  94. if (CSecurity::IsUntrusted()) {
  95. InsertMessage("Running in an untrusted environment", ICONID_REDCROSS);
  96. } else {
  97. InsertMessage("Running as trusted code", ICONID_GREENCHECK);
  98. }
  99. //
  100. // Perform a simple MAPI test to send an email to someone.
  101. //
  102. Sleep(2000);
  103. InsertMessage("Attempting to initialize MAPI...", ICONID_BLUEDOT);
  104. {
  105. CInitMapi initmapi;
  106. if (HR_FAILED(initmapi.InitMapi())) {
  107. AppendToLastMessage("fail");
  108. InsertMessage("Failed to initialize MAPI", ICONID_REDCROSS);
  109. } else {
  110. AppendToLastMessage("ok");
  111. Sleep(500);
  112. //
  113. // Try to resolve some addresses to prove that we can
  114. // access the MAPI address book.
  115. //
  116. CAddressEnum addrenum(initmapi);
  117. char szNameBuffer[100];
  118. for (int i = 0; ; i++)
  119. {
  120. // figure out which address we will look up.
  121. LPSTR szSourceName = NULL;
  122. switch (i) {
  123. case 0: szSourceName = "billg"; break;
  124. case 1: szSourceName = "steve bal"; break;
  125. case 2: szSourceName = "daveth"; break;
  126. case 3: szSourceName = "praerit"; break;
  127. case 4: szSourceName = "jeff lawson"; break;
  128. }
  129. if (szSourceName == NULL) break;
  130. // log something within the window.
  131. InsertMessage("Looking up ", ICONID_BLUEDOT);
  132. AppendToLastMessage(szSourceName);
  133. AppendToLastMessage("...");
  134. // do the resolution.
  135. if (HR_FAILED(addrenum.LookupAddress(szSourceName,
  136. szNameBuffer, sizeof(szNameBuffer))))
  137. {
  138. AppendToLastMessage("fail");
  139. } else {
  140. AppendToLastMessage(szNameBuffer);
  141. }
  142. }
  143. //
  144. // Compose an outgoing email to a specified recipient
  145. //
  146. InsertMessage("Creating an outgoing e-mail...", ICONID_BLUEDOT);
  147. CSendMail sendmail(initmapi);
  148. if (HR_FAILED(sendmail.CreateMail("Test Message from AMISAFE"))) {
  149. AppendToLastMessage("fail");
  150. InsertMessage("Failed to create MAPI message", ICONID_REDCROSS);
  151. } else {
  152. if (HR_FAILED(sendmail.SetRecipients(szRecipients))) {
  153. AppendToLastMessage("fail");
  154. InsertMessage("Failed to send to ", ICONID_REDCROSS);
  155. AppendToLastMessage(szRecipients);
  156. } else {
  157. AppendToLastMessage("ok");
  158. Sleep(500);
  159. InsertMessage("Starting e-mail transmission...", ICONID_BLUEDOT);
  160. if (HR_FAILED(sendmail.Transmit())) {
  161. AppendToLastMessage("fail");
  162. InsertMessage("Failed to transmit MAPI message", ICONID_REDCROSS);
  163. } else {
  164. AppendToLastMessage("ok");
  165. InsertMessage("Successfully sent to ", ICONID_GREENCHECK);
  166. AppendToLastMessage(szRecipients);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. Sleep(2000);
  173. InsertMessage("Finished tests.", ICONID_BLUEDOT);
  174. }
  175. DWORD WINAPI CAmisafeDlg::ThreadProc(LPVOID lpParameter)
  176. {
  177. ((CAmisafeDlg*)lpParameter)->RunSecurityCheck();
  178. return 0;
  179. }
  180. void CAmisafeDlg::InsertMessage(LPCSTR szMessage, DWORD dwIconId)
  181. {
  182. listcontrol.InsertItem(listcontrol.GetItemCount(), szMessage, dwIconId);
  183. listcontrol.UpdateWindow();
  184. }
  185. void CAmisafeDlg::AppendToLastMessage(LPCSTR szMessage, DWORD dwNewIconId)
  186. {
  187. int count = listcontrol.GetItemCount();
  188. if (count > 0) {
  189. char szBuffer[200] = "";
  190. listcontrol.GetItemText(count - 1, 0, szBuffer, sizeof(szBuffer));
  191. strncat(szBuffer, szMessage, sizeof(szBuffer) - strlen(szBuffer) - 1);
  192. szBuffer[sizeof(szBuffer) - 1] = '\0';
  193. listcontrol.SetItemText(count - 1, 0, szBuffer);
  194. listcontrol.UpdateWindow();
  195. }
  196. }
  197. void CAmisafeDlg::OnClose()
  198. {
  199. if (hThread != NULL) {
  200. InsertMessage("waiting for shutdown", ICONID_BLUEDOT);
  201. TerminateThread(hThread, 0); // not the nicest thing
  202. Sleep(500);
  203. WaitForSingleObject(hThread, INFINITE);
  204. hThread = NULL;
  205. }
  206. CDialog::OnClose();
  207. }