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.

411 lines
10 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation 1999 - 2000
  4. // All rights reserved
  5. //
  6. // catlog.cxx
  7. //
  8. //*************************************************************
  9. #include "appmgext.hxx"
  10. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  11. //
  12. // Function: CCategoryInfo::CCategoryInfo
  13. //
  14. // Purpose: Constructor for category encapsulation class --
  15. // initializes state with information about a category
  16. //
  17. // Params: pCategoryInfo -- structure containing information
  18. // about this category
  19. //
  20. // Return value: none
  21. //
  22. // Notes: The class maintains the reference to the passed
  23. // in structure -- therefore, the memory for that
  24. // structure should not be freed before this object
  25. // is used. This class does not own the reference --
  26. // caller should free the pCategoryInfo memory after
  27. // this class is no longer in use.
  28. //
  29. //------------------------------------------------------------
  30. CCategoryInfo::CCategoryInfo(
  31. APPCATEGORYINFO* pCategoryInfo) :
  32. _pCategoryInfo( pCategoryInfo )
  33. {}
  34. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  35. //
  36. // Function: CCategoryInfo::Write
  37. //
  38. // Purpose: Write information regarding this category into
  39. // a database record
  40. //
  41. // Params:
  42. //
  43. // Return value: S_OK if successful, error otherwise
  44. //
  45. // Notes:
  46. //
  47. //------------------------------------------------------------
  48. HRESULT CCategoryInfo::Write()
  49. {
  50. HRESULT hr;
  51. WCHAR wszUniqueId[MAX_SZGUID_LEN];
  52. //
  53. // Get our unique id
  54. //
  55. GuidToString(
  56. _pCategoryInfo->AppCategoryId,
  57. wszUniqueId);
  58. //
  59. // The category guid is the unique id for this record --
  60. //
  61. hr = SetValue(
  62. CAT_ATTRIBUTE_ID,
  63. wszUniqueId);
  64. if (FAILED(hr))
  65. {
  66. return hr;
  67. }
  68. //
  69. // Set the time stamp on the record
  70. //
  71. {
  72. SYSTEMTIME CurrentTime;
  73. //
  74. // This does not fail
  75. //
  76. GetSystemTime( &CurrentTime );
  77. hr = SetValue(
  78. CAT_ATTRIBUTE_CREATIONTIME,
  79. &CurrentTime);
  80. REPORT_ATTRIBUTE_SET_STATUS( CAT_ATTRIBUTE_CREATIONTIME, hr );
  81. }
  82. //
  83. // Set the name of the category
  84. //
  85. hr = SetValue(
  86. CAT_ATTRIBUTE_NAME,
  87. _pCategoryInfo->pszDescription);
  88. REPORT_ATTRIBUTE_SET_STATUS( CAT_ATTRIBUTE_NAME, hr );
  89. return hr;
  90. }
  91. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  92. //
  93. // Function: CCategoryInfoLog::CCategoryInfoLog
  94. //
  95. // Purpose: Initialize the domain app categories to log object
  96. //
  97. // Params:
  98. //
  99. // Return value: none
  100. //
  101. // Notes:
  102. //
  103. //------------------------------------------------------------
  104. CCategoryInfoLog::CCategoryInfoLog(
  105. CRsopContext* pRsopContext,
  106. APPCATEGORYINFOLIST* pCategoryList ) :
  107. _bRsopEnabled( FALSE ),
  108. _pRsopContext( pRsopContext ),
  109. _pCategoryList( pCategoryList )
  110. {
  111. if ( ! pCategoryList )
  112. {
  113. _pCategoryList = & _AppCategoryList;
  114. }
  115. //
  116. // Zero the list of apps so the destructor never mistakes
  117. // unitialized data for real data that it would attempt to free
  118. //
  119. RtlZeroMemory(&_AppCategoryList, sizeof(_AppCategoryList));
  120. }
  121. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  122. //
  123. // Function: CCategoryInfoLog::~CCategoryInfoLog
  124. //
  125. // Purpose: Initialize the domain app categories to log object
  126. //
  127. // Params:
  128. //
  129. // Return value: none
  130. //
  131. // Notes:
  132. //
  133. //------------------------------------------------------------
  134. CCategoryInfoLog::~CCategoryInfoLog()
  135. {
  136. //
  137. // If the members of this function are NULL, this is
  138. // just a noop. Otherwise, it clears all memory
  139. // references by this structure and its members.
  140. //
  141. (void) ReleaseAppCategoryInfoList( &_AppCategoryList );
  142. }
  143. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  144. //
  145. // Function: CCategoryInfoLog::WriteLog
  146. //
  147. // Purpose: Log domain categories to the policy database
  148. //
  149. // Params: none
  150. //
  151. // Return value: S_OK if all categories logged, S_FALSE if
  152. // one or more categories could not be logged,
  153. // other error otherwise
  154. //
  155. // Notes: Does nothing if the rsop logging is disabled
  156. //
  157. //------------------------------------------------------------
  158. HRESULT CCategoryInfoLog::WriteLog()
  159. {
  160. HRESULT hr;
  161. //
  162. // Make sure logging is enabled -- if not, this function
  163. // will just be a noop. Logging is disabled if we have
  164. // any sort of initialization errors.
  165. //
  166. if ( !_pRsopContext->IsRsopEnabled() )
  167. {
  168. return S_OK;
  169. }
  170. //
  171. // Initialize the log so that we can write into it -- if this
  172. // doesn't succeed, we can't log anything.
  173. //
  174. hr = InitCategoryLog();
  175. if (FAILED(hr))
  176. {
  177. return hr;
  178. }
  179. if ( _pRsopContext->IsPlanningModeEnabled() )
  180. {
  181. //
  182. // Now that log support is set, we need to obtain the categories
  183. // which we are going to log
  184. //
  185. hr = GetCategoriesFromDirectory();
  186. if (FAILED(hr))
  187. {
  188. return hr;
  189. }
  190. }
  191. //
  192. // We have the categories we wish to log, now we should write
  193. // all the categories to the log
  194. //
  195. return WriteCategories();
  196. }
  197. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  198. //
  199. // Function: CCategoryInfoLog::InitCategoryLog
  200. //
  201. // Purpose: Initialize the logging support, including
  202. // establishing a connection to the policy database
  203. //
  204. // Params: none
  205. //
  206. // Return value: S_OK if successful, other error otherwise
  207. //
  208. // Notes: If this fails, categories cannot be logged
  209. //
  210. //------------------------------------------------------------
  211. HRESULT CCategoryInfoLog::InitCategoryLog()
  212. {
  213. HRESULT hr;
  214. if ( ! _pRsopContext->IsRsopEnabled() )
  215. {
  216. return S_OK;
  217. }
  218. //
  219. // Initialize the base logging functions to allow logging
  220. // to the policy database of the class of policy
  221. // in which we're interested: software categories. We
  222. // supply a flag indicating whether this is machine or user
  223. // policy since machine and user policy records are logged
  224. // in separate namespaces (i.e. we maintain separate logs).
  225. //
  226. hr = InitLog( _pRsopContext,
  227. RSOP_MANAGED_SOFTWARE_CATEGORY);
  228. //
  229. // If this init fails, we should disable logging so
  230. // subsequent method calls on this object will
  231. // not attempt to write to an inaccessible database
  232. //
  233. if (FAILED(hr))
  234. {
  235. return hr;
  236. }
  237. //
  238. // We have access to the database, we should clear previous
  239. // logs in this namespace
  240. //
  241. hr = ClearLog();
  242. //
  243. // If we couldn't clear the log, we will not attempt to write
  244. // any more records -- we make sure of this by resetting the
  245. // disable flag
  246. //
  247. if (SUCCEEDED(hr))
  248. {
  249. _bRsopEnabled = TRUE;
  250. }
  251. return hr;
  252. }
  253. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  254. //
  255. // Function: CCategoryInfoLog::AddBlankCategory
  256. //
  257. // Purpose: Adds an empty category record to the log
  258. //
  259. // Params: none
  260. //
  261. // Return value: S_OK if successful, other error otherwise
  262. //
  263. // Notes:
  264. //
  265. //------------------------------------------------------------
  266. HRESULT CCategoryInfoLog::AddBlankCategory(CCategoryInfo* pCategoryInfo)
  267. {
  268. return AddBlankRecord(pCategoryInfo);
  269. }
  270. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  271. //
  272. // Function: CCategoryInfoLog::GetCategories()
  273. //
  274. // Purpose: Obtains the list of app categories from the domain
  275. //
  276. // Params: none
  277. //
  278. // Return value: S_OK if successful, other error otherwise
  279. //
  280. // Notes:
  281. //
  282. //------------------------------------------------------------
  283. HRESULT CCategoryInfoLog::GetCategoriesFromDirectory()
  284. {
  285. HRESULT hr;
  286. //
  287. // Call the internal api to the directory service software
  288. // management interface to obtain the list of categories
  289. //
  290. hr = CsGetAppCategories( &_AppCategoryList );
  291. if (FAILED(hr))
  292. {
  293. return hr;
  294. }
  295. return hr;
  296. }
  297. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  298. //
  299. // Function: CCategoryInfoLog::WriteLog
  300. //
  301. // Purpose: Write domain categories as records
  302. // to the policy database
  303. //
  304. // Params: none
  305. //
  306. // Return value: S_OK if all categories logged, S_FALSE if
  307. // one or more categories could not be logged,
  308. // other error otherwise
  309. //
  310. // Notes:
  311. //
  312. //------------------------------------------------------------
  313. HRESULT CCategoryInfoLog::WriteCategories()
  314. {
  315. DWORD iCat;
  316. HRESULT hr;
  317. hr = S_OK;
  318. //
  319. // Iterate through the list of categories so that we
  320. // can log each one.
  321. //
  322. for (iCat = 0; iCat < _pCategoryList->cCategory; iCat++)
  323. {
  324. HRESULT hrWrite;
  325. DebugMsg((DM_VERBOSE, IDS_RSOP_CAT_INFO, _pCategoryList->pCategoryInfo[iCat].pszDescription));
  326. //
  327. // Place this code in a new scope so that the constructor
  328. // and destructor for CCategoryInfo are called each time (we
  329. // need to get a new record object for each iteration in the loop)
  330. //
  331. {
  332. //
  333. // Create a record object with information about the current
  334. // category in this iteration
  335. //
  336. CCategoryInfo CategoryInfo( &(_pCategoryList->pCategoryInfo[iCat]) );
  337. //
  338. // Now write the record into the database.
  339. //
  340. hrWrite = WriteNewRecord( &CategoryInfo );
  341. //
  342. // Set our return value to S_FALSE if we failed in any way to log this category
  343. //
  344. if (FAILED(hrWrite))
  345. {
  346. DebugMsg((DM_VERBOSE, IDS_RSOP_CAT_WRITE_FAIL, _pCategoryList->pCategoryInfo->pszDescription, hr));
  347. hr = S_FALSE;
  348. }
  349. }
  350. }
  351. return hr;
  352. }