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.

371 lines
8.1 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. expinfo.cpp
  5. Abstract:
  6. Author:
  7. Magnus Hedlund (MagnusH) --
  8. Revision History:
  9. --*/
  10. #include "stdafx.h"
  11. #include "oleutil.h"
  12. #include "nntpcmn.h"
  13. #include "nntptype.h"
  14. #include "nntpapi.h"
  15. #include "expinfo.h"
  16. #include <lmapibuf.h>
  17. // Must define THIS_FILE_* macros to use NntpCreateException()
  18. #define THIS_FILE_HELP_CONTEXT 0
  19. #define THIS_FILE_PROG_ID _T("Nntpadm.Expiration.1")
  20. #define THIS_FILE_IID IID_INntpAdminExpiration
  21. CExpirationPolicy::CExpirationPolicy ( ) :
  22. m_dwExpireId ( 0 ),
  23. m_dwSize ( 0 ),
  24. m_dwTime ( 0 )
  25. // CMultiSz's are set to NULL automatically.
  26. {
  27. }
  28. CExpirationPolicy::~CExpirationPolicy ( )
  29. {
  30. // CMultiSz's are deleted automatically.
  31. }
  32. void CExpirationPolicy::Destroy ()
  33. {
  34. m_mszNewsgroups.Empty ();
  35. }
  36. const CExpirationPolicy & CExpirationPolicy::operator= ( const CExpirationPolicy & Expire )
  37. {
  38. // Check for assignment to self:
  39. if ( &Expire == this ) {
  40. return *this;
  41. }
  42. // Empty the old Expire values:
  43. this->Destroy ();
  44. // Copy all member variables:
  45. m_dwExpireId = Expire.m_dwExpireId;
  46. m_strPolicyName = Expire.m_strPolicyName;
  47. m_dwSize = Expire.m_dwSize;
  48. m_dwTime = Expire.m_dwTime;
  49. m_mszNewsgroups = Expire.m_mszNewsgroups;
  50. // If anything didn't work, CheckValid will fail.
  51. return *this;
  52. }
  53. BOOL CExpirationPolicy::CheckValid ( )
  54. {
  55. // Check Strings:
  56. if (
  57. !m_mszNewsgroups
  58. ) {
  59. return FALSE;
  60. }
  61. return TRUE;
  62. }
  63. void CExpirationPolicy::FromExpireInfo ( const NNTP_EXPIRE_INFO * pExpireInfo )
  64. {
  65. this->Destroy ();
  66. m_dwExpireId = pExpireInfo->ExpireId;
  67. m_strPolicyName = pExpireInfo->ExpirePolicy;
  68. m_dwSize = pExpireInfo->ExpireSizeHorizon;
  69. m_dwTime = pExpireInfo->ExpireTime;
  70. m_mszNewsgroups = (LPWSTR) pExpireInfo->Newsgroups;
  71. _ASSERT ( pExpireInfo->cbNewsgroups == m_mszNewsgroups.SizeInBytes () );
  72. }
  73. HRESULT CExpirationPolicy::ToExpireInfo ( LPNNTP_EXPIRE_INFO pExpireInfo )
  74. {
  75. TraceFunctEnter ( "CExpirationPolicy::ToExpireInfo" );
  76. _ASSERT ( IS_VALID_OUT_PARAM ( pExpireInfo ) );
  77. HRESULT hr = NOERROR;
  78. ZeroMemory ( pExpireInfo, sizeof ( *pExpireInfo ) );
  79. pExpireInfo->ExpireId = m_dwExpireId;
  80. pExpireInfo->ExpirePolicy = m_strPolicyName;
  81. pExpireInfo->ExpireSizeHorizon = m_dwSize;
  82. pExpireInfo->ExpireTime = m_dwTime;
  83. pExpireInfo->Newsgroups = (UCHAR *) (LPCWSTR) m_mszNewsgroups;
  84. pExpireInfo->cbNewsgroups = m_mszNewsgroups.SizeInBytes ();
  85. TraceFunctLeave ();
  86. return hr;
  87. }
  88. HRESULT CExpirationPolicy::Add ( LPCWSTR strServer, DWORD dwInstance)
  89. {
  90. TraceFunctEnter ( "CExpirationPolicy::Add" );
  91. HRESULT hr = NOERROR;
  92. DWORD dwError = NOERROR;
  93. DWORD dwParmErr = 0;
  94. DWORD dwNewId = 0;
  95. NNTP_EXPIRE_INFO Expireinfo;
  96. FillMemory ( &Expireinfo, sizeof (Expireinfo), 0 );
  97. hr = ToExpireInfo ( &Expireinfo );
  98. if ( FAILED(hr) ) {
  99. goto Exit;
  100. }
  101. Expireinfo.ExpireId = 0;
  102. dwError = NntpAddExpire ( (LPWSTR) strServer, dwInstance, &Expireinfo, &dwParmErr, &dwNewId );
  103. if ( dwError != NOERROR ) {
  104. ErrorTraceX ( (LPARAM) this, "Failed to add Expire %x", dwError );
  105. hr = RETURNCODETOHRESULT ( dwError );
  106. goto Exit;
  107. }
  108. this->m_dwExpireId = dwNewId;
  109. Exit:
  110. TRACE_HRESULT(hr);
  111. TraceFunctLeave ();
  112. return hr;
  113. }
  114. HRESULT CExpirationPolicy::Set ( LPCWSTR strServer, DWORD dwInstance)
  115. {
  116. TraceFunctEnter ( "CExpirationPolicy::Set" );
  117. HRESULT hr = NOERROR;
  118. DWORD dwError = NOERROR;
  119. DWORD dwParmErr = 0;
  120. NNTP_EXPIRE_INFO Expireinfo;
  121. FillMemory ( &Expireinfo, sizeof (Expireinfo), 0 );
  122. hr = ToExpireInfo ( &Expireinfo );
  123. if ( FAILED(hr) ) {
  124. goto Exit;
  125. }
  126. dwError = NntpSetExpireInformation ( (LPWSTR) strServer, dwInstance, &Expireinfo, &dwParmErr );
  127. if ( dwError != NOERROR ) {
  128. ErrorTraceX ( (LPARAM) this, "Failed to set Expire[%d]: %x", m_dwExpireId, dwError );
  129. hr = RETURNCODETOHRESULT ( dwError );
  130. goto Exit;
  131. }
  132. Exit:
  133. TRACE_HRESULT(hr);
  134. TraceFunctLeave ();
  135. return hr;
  136. }
  137. HRESULT CExpirationPolicy::Remove ( LPCWSTR strServer, DWORD dwInstance)
  138. {
  139. TraceFunctEnter ( "CExpirationPolicy::Remove" );
  140. HRESULT hr = NOERROR;
  141. DWORD dwError = NOERROR;
  142. dwError = NntpDeleteExpire ( (LPWSTR) strServer, dwInstance, m_dwExpireId );
  143. if ( dwError != NOERROR ) {
  144. ErrorTraceX ( (LPARAM) this, "Failed to remove Expire[%d]: %x", m_dwExpireId );
  145. hr = RETURNCODETOHRESULT ( dwError );
  146. goto Exit;
  147. }
  148. Exit:
  149. TRACE_HRESULT(hr);
  150. TraceFunctLeave ();
  151. return hr;
  152. }
  153. #if 0
  154. BOOL CExpirationPolicy::CheckPolicyProperties ( )
  155. {
  156. return TRUE;
  157. }
  158. HRESULT CExpirationPolicy::GetFromMetabase ( CMetabaseKey * pmkeyExpiration, const LPWSTR wszPolicyKey )
  159. {
  160. TraceFunctEnter ( "CExpirationPolicy::GetFromMetabase" );
  161. HRESULT hr = NOERROR;
  162. DWORD cbData = 0;
  163. char Dummy[5];
  164. WCHAR * msz = NULL;
  165. m_dwExpireId = GetExpireId ( szPolicyKey );
  166. // Assume that the ID is non-zero:
  167. _ASSERT ( m_dwExpireId != 0 );
  168. hr = pmkeyExpiration->GetDword ( szPolicyKey, MD_EXPIRE_SPACE, IIS_MD_UT_SERVER, &m_dwSize, 0 );
  169. if ( FAILED(hr) ) {
  170. m_dwSize = (DWORD) -1;
  171. hr = NOERROR;
  172. }
  173. hr = pmkeyExpiration->GetDword ( szPolicyKey, MD_EXPIRE_TIME, IIS_MD_UT_SERVER, &m_dwTime, 0 );
  174. if ( FAILED(hr) ) {
  175. m_dwTime = (DWORD) -1;
  176. hr = NOERROR;
  177. }
  178. hr = pmkeyExpiration->GetData ( szPolicyKey, MD_EXPIRE_NEWSGROUPS, IIS_MD_UT_SERVER, BINARY_METADATA, &Dummy, &cbData, 0 );
  179. if ( FAILED (hr) ) {
  180. cbData = 0;
  181. hr = NOERROR;
  182. }
  183. if ( cbData == 0 ) {
  184. m_mszNewsgroups = _T("\0");
  185. }
  186. else {
  187. _ASSERT ( (cbData % 2) == 0 );
  188. msz = new WCHAR [ (cbData + 1) / 2 ];
  189. if ( msz == NULL ) {
  190. FatalTrace ( (LPARAM) this, "Out of memory" );
  191. hr = E_OUTOFMEMORY;
  192. goto Exit;
  193. }
  194. hr = pmkeyExpiration->GetData ( szPolicyKey, MD_EXPIRE_NEWSGROUPS, IIS_MD_UT_SERVER, BINARY_METADATA, msz, &cbData, 0 );
  195. m_mszNewsgroups = msz;
  196. }
  197. Exit:
  198. if ( msz ) {
  199. delete msz;
  200. }
  201. TraceFunctLeave ();
  202. return hr;
  203. }
  204. HRESULT CExpirationPolicy::SendToMetabase ( CMetabaseKey * pmkeyExpiration, DWORD bvChangedFields )
  205. {
  206. TraceFunctEnter ( "CExpirationPolicy::SendToMetabase" );
  207. HRESULT hr = NOERROR;
  208. DWORD cbNewsgroups = 0;
  209. CHAR szPolicyKey[ METADATA_MAX_NAME_LEN ];
  210. _ASSERT ( m_dwExpireId != 0 );
  211. wsprintfA ( szPolicyKey, "expire%ud", m_dwExpireId );
  212. hr = pmkeyExpiration->SetDword ( szPolicyKey, MD_EXPIRE_SPACE, IIS_MD_UT_SERVER, m_dwSize );
  213. if ( FAILED(hr) ) {
  214. goto Exit;
  215. }
  216. hr = pmkeyExpiration->SetDword ( szPolicyKey, MD_EXPIRE_TIME, IIS_MD_UT_SERVER, m_dwTime );
  217. if ( FAILED(hr) ) {
  218. goto Exit;
  219. }
  220. cbNewsgroups = m_mszNewsgroups.SizeInBytes ();
  221. hr = pmkeyExpiration->SetData ( szPolicyKey, MD_EXPIRE_NEWSGROUPS, IIS_MD_UT_SERVER, BINARY_METADATA, (void *) (LPCWSTR) m_mszNewsgroups, cbNewsgroups );
  222. if ( FAILED(hr) ) {
  223. goto Exit;
  224. }
  225. Exit:
  226. TraceFunctLeave ();
  227. return hr;
  228. }
  229. HRESULT CExpirationPolicy::AddToMetabase ( CMetabaseKey * pmkeyExpiration )
  230. {
  231. TraceFunctEnter ( "CExpirationPolicy::AddToMetabase" );
  232. HRESULT hr = NOERROR;
  233. char szExpireKey [ METADATA_MAX_NAME_LEN ];
  234. m_dwExpireId = 0;
  235. while ( 1 ) {
  236. m_dwExpireId++;
  237. wsprintfA ( szExpireKey, "expire%ud", m_dwExpireId );
  238. hr = pmkeyExpiration->CreateChild ( szExpireKey );
  239. if ( SUCCEEDED(hr) ) {
  240. // Success, get out of the loop:
  241. break;
  242. }
  243. if ( HRESULTTOWIN32 ( hr ) == ERROR_ALREADY_EXISTS ) {
  244. // This key already exists, try the next one:
  245. continue;
  246. }
  247. _ASSERT ( FAILED(hr) );
  248. if ( FAILED(hr) ) {
  249. ErrorTraceX ( (LPARAM) this, "Error adding new expire policy [%d] : %x", m_dwExpireId, hr );
  250. goto Exit;
  251. }
  252. }
  253. return SendToMetabase ( pmkeyExpiration, (DWORD) -1 );
  254. Exit:
  255. TraceFunctLeave ();
  256. return hr;
  257. }
  258. DWORD GetExpireId ( const LPWSTR wszKey )
  259. {
  260. if ( strncmp ( szKey, "expire", sizeof ("expire") - 1 ) != 0 ) {
  261. return 0;
  262. }
  263. return atoi ( szKey + sizeof("expire") - 1 );
  264. }
  265. BOOL IsKeyValidExpire ( const LPWSTR wszKey )
  266. {
  267. /*
  268. if ( _strnicmp ( szKey, "expire" ) != 0 ) {
  269. return FALSE;
  270. }
  271. */
  272. if ( GetExpireId ( szKey ) != 0 ) {
  273. return TRUE;
  274. }
  275. return FALSE;
  276. }
  277. #endif