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.

284 lines
7.4 KiB

  1. // CUsersInitialAlert.cpp : Implementation of CUsersInitialAlertApp and DLL registration.
  2. #include "stdafx.h"
  3. #include "UsersInitialAlert.h"
  4. #include "CUsersInitialAlert.h"
  5. #include <usermsg.h>
  6. #include <activeds.h>
  7. #include <appliancetask.h>
  8. #include <taskctx.h>
  9. #include <appsrvcs.h>
  10. //
  11. // Alert source information
  12. //
  13. const WCHAR ALERT_LOG_NAME[] = L"MSSAKitComm";
  14. const WCHAR ALERT_SOURCE [] = L"usermsg.dll";
  15. //
  16. // Various strings used in the program
  17. //
  18. const WCHAR SZ_METHOD_NAME[] = L"MethodName";
  19. const WCHAR SZ_APPLIANCE_INITIALIZATION_TASK []=L"ApplianceInitializationTask";
  20. const WCHAR SZ_WINNT_PATH[] = L"WinNT://";
  21. const WCHAR SZ_GUESTUSER_NAME[] = L"/Guest";
  22. const WORD MAXAPPLIANCEDNSNAME = 127;
  23. //////////////////////////////////////////////////////////////////////////////
  24. //
  25. // Function: CUsersInitialAlert::OnTaskComplete
  26. //
  27. // Synopsis:
  28. //
  29. // Arguments: pTaskContext - The TaskContext object contains the method name
  30. // and parameters as name value pairs
  31. //
  32. // Returns: HRESULT
  33. //
  34. //////////////////////////////////////////////////////////////////////////////
  35. STDMETHODIMP
  36. CUsersInitialAlert::OnTaskComplete(IUnknown *pTaskContext,
  37. LONG lTaskResult)
  38. {
  39. HRESULT hrRet = E_FAIL;
  40. LPWSTR pstrApplianceName = NULL;
  41. VARIANT_BOOL bIsDisabled;
  42. CComPtr<IADsUser> pUser;
  43. do
  44. {
  45. hrRet = ParseTaskParameter( pTaskContext );
  46. if( FAILED( hrRet ) )
  47. {
  48. break;
  49. }
  50. if ( lTaskResult == SA_TASK_RESULT_COMMIT )
  51. {
  52. if( FALSE == GetApplianceName( &pstrApplianceName ) )
  53. {
  54. SATraceString(
  55. "AlertEmail:Initialize GetApplianceName failed"
  56. );
  57. break;
  58. }
  59. CComBSTR bstrADSPath;
  60. bstrADSPath.Append( SZ_WINNT_PATH );
  61. bstrADSPath += CComBSTR( pstrApplianceName );
  62. bstrADSPath += CComBSTR( SZ_GUESTUSER_NAME );
  63. hrRet = ADsGetObject( bstrADSPath, IID_IADsUser, (void**)&pUser );
  64. if( FAILED( hrRet ) )
  65. {
  66. break;
  67. }
  68. hrRet = pUser->get_AccountDisabled( &bIsDisabled );
  69. if( FAILED( hrRet ) )
  70. {
  71. break;
  72. }
  73. if( !bIsDisabled )
  74. {
  75. hrRet = RaiseUsersInitialAlert();
  76. }
  77. }
  78. else
  79. {
  80. //
  81. // Do nothing on Commit failure
  82. //
  83. hrRet = S_OK;
  84. }
  85. }
  86. while( false );
  87. if( pstrApplianceName != NULL )
  88. {
  89. ::free( pstrApplianceName );
  90. }
  91. return hrRet;
  92. }
  93. ///////////////////////////////////////////////////////////////////////////////
  94. //
  95. // Function: CUsersInitialAlert::OnTaskExecute
  96. //
  97. // Synopsis: This function is the entry point for AppMgr.
  98. //
  99. // Arguments: pTaskContext - The TaskContext object contains the method name
  100. // and parameters as name value pairs
  101. //
  102. // Returns: HRESULT
  103. //
  104. ///////////////////////////////////////////////////////////////////////////////
  105. STDMETHODIMP
  106. CUsersInitialAlert::OnTaskExecute(IUnknown *pTaskContext)
  107. {
  108. return ParseTaskParameter(pTaskContext);
  109. }
  110. HRESULT
  111. CUsersInitialAlert::ParseTaskParameter(IUnknown *pTaskContext)
  112. {
  113. CComVariant varValue;
  114. CComPtr<ITaskContext> pTaskParameter;
  115. HRESULT hrRet = E_INVALIDARG;
  116. try
  117. {
  118. do
  119. {
  120. if(NULL == pTaskContext)
  121. {
  122. break;
  123. }
  124. hrRet = pTaskContext->QueryInterface(IID_ITaskContext,
  125. (void **)&pTaskParameter);
  126. if(FAILED(hrRet))
  127. {
  128. break;
  129. }
  130. hrRet = pTaskParameter->GetParameter(
  131. CComBSTR(SZ_METHOD_NAME),
  132. &varValue
  133. );
  134. if ( FAILED( hrRet ) )
  135. {
  136. break;
  137. }
  138. if ( V_VT( &varValue ) != VT_BSTR )
  139. {
  140. break;
  141. }
  142. if ( lstrcmp( V_BSTR(&varValue), SZ_APPLIANCE_INITIALIZATION_TASK ) == 0 )
  143. {
  144. hrRet=S_OK;
  145. }
  146. }
  147. while(false);
  148. }
  149. catch(...)
  150. {
  151. hrRet=E_FAIL;
  152. }
  153. return hrRet;
  154. }
  155. HRESULT
  156. CUsersInitialAlert::RaiseUsersInitialAlert()
  157. {
  158. DWORD dwAlertType = SA_ALERT_TYPE_ATTENTION;
  159. DWORD dwAlertId = L_GUESTUSER_ENABLED;
  160. HRESULT hrRet = E_FAIL;
  161. CComVariant varReplacementStrings;
  162. CComVariant varRawData;
  163. LONG lCookie;
  164. CComPtr<IApplianceServices> pAppSrvcs;
  165. try
  166. {
  167. do
  168. {
  169. hrRet = CoCreateInstance(CLSID_ApplianceServices,
  170. NULL,
  171. CLSCTX_INPROC_SERVER,
  172. IID_IApplianceServices,
  173. (void**)&pAppSrvcs);
  174. if (FAILED(hrRet))
  175. {
  176. break;
  177. }
  178. //
  179. // Initialize() is called prior to using other component services.
  180. //Performscomponent initialization operations.
  181. //
  182. hrRet = pAppSrvcs->Initialize();
  183. if (FAILED(hrRet))
  184. {
  185. break;
  186. }
  187. hrRet = pAppSrvcs->RaiseAlert(
  188. dwAlertType,
  189. dwAlertId,
  190. CComBSTR(ALERT_LOG_NAME),
  191. CComBSTR(ALERT_SOURCE),
  192. SA_ALERT_DURATION_ETERNAL,
  193. &varReplacementStrings,
  194. &varRawData,
  195. &lCookie
  196. );
  197. }
  198. while(false);
  199. }
  200. catch(...)
  201. {
  202. hrRet=E_FAIL;
  203. }
  204. return hrRet;
  205. }
  206. BOOL
  207. CUsersInitialAlert::GetApplianceName(
  208. LPWSTR* pstrComputerName
  209. )
  210. {
  211. BOOL bReturn = FALSE;
  212. DWORD dwSize = 0;
  213. DWORD dwCount = 1;
  214. do
  215. {
  216. if( *pstrComputerName != NULL )
  217. {
  218. ::free( *pstrComputerName );
  219. }
  220. dwSize = MAXAPPLIANCEDNSNAME * dwCount;
  221. *pstrComputerName = ( LPWSTR ) ::malloc( sizeof(WCHAR) * dwSize );
  222. if( *pstrComputerName == NULL )
  223. {
  224. SATraceString(
  225. "AlertEmail:GetApplianceName malloc failed"
  226. );
  227. break;
  228. }
  229. //
  230. // Get local computer name.
  231. //
  232. bReturn = ::GetComputerNameEx(
  233. ComputerNameDnsFullyQualified,
  234. *pstrComputerName,
  235. &dwSize
  236. );
  237. dwCount <<= 1;
  238. }
  239. while( !bReturn &&
  240. ERROR_MORE_DATA == ::GetLastError() &&
  241. dwCount < 8
  242. );
  243. return bReturn;
  244. }