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.

325 lines
7.1 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1992 - 1996
  6. //
  7. // File: kerblist.cxx
  8. //
  9. // Contents: Common list code for the Kerberos package
  10. //
  11. //
  12. // History: 16-April-1996 Created MikeSw
  13. //
  14. //------------------------------------------------------------------------
  15. #include "krbprgma.h"
  16. #include <kerbkrnl.h>
  17. #ifdef ALLOC_PRAGMA
  18. #pragma alloc_text(PAGE, KerbInitializeList)
  19. #pragma alloc_text(PAGE, KerbFreeList)
  20. #pragma alloc_text(PAGEMSG, KerbInitializeListEntry)
  21. #pragma alloc_text(PAGEMSG, KerbInsertListEntry)
  22. #pragma alloc_text(PAGEMSG, KerbReferenceListEntry)
  23. #pragma alloc_text(PAGEMSG, KerbDereferenceListEntry)
  24. #pragma alloc_text(PAGEMSG, KerbValidateListEx)
  25. #endif
  26. //+-------------------------------------------------------------------------
  27. //
  28. // Function: KerbInitializeList
  29. //
  30. // Synopsis: Initializes a kerberos list by initializing the lock
  31. // and the list entry.
  32. //
  33. // Effects:
  34. //
  35. // Arguments: List - List to initialize
  36. //
  37. // Requires:
  38. //
  39. // Returns: STATUS_SUCCESS on success or errors from
  40. // RtlInitializeResources
  41. //
  42. // Notes:
  43. //
  44. //
  45. //--------------------------------------------------------------------------
  46. NTSTATUS
  47. KerbInitializeList(
  48. IN PKERBEROS_LIST List
  49. )
  50. {
  51. NTSTATUS Status;
  52. PAGED_CODE();
  53. InitializeListHead(&List->List);
  54. Status = ExInitializeResourceLite(
  55. &List->Lock
  56. );
  57. return(Status);
  58. }
  59. //+-------------------------------------------------------------------------
  60. //
  61. // Function: KerbFreeList
  62. //
  63. // Synopsis: Frees a kerberos list by deleting the associated
  64. // critical section.
  65. //
  66. // Effects: List - the list to free.
  67. //
  68. // Arguments:
  69. //
  70. // Requires:
  71. //
  72. // Returns: none
  73. //
  74. // Notes: The list must be empty before freeing it.
  75. //
  76. //
  77. //--------------------------------------------------------------------------
  78. VOID
  79. KerbFreeList(
  80. IN PKERBEROS_LIST List
  81. )
  82. {
  83. PAGED_CODE();
  84. //
  85. // Make sure the list is empty first
  86. //
  87. ASSERT(List->List.Flink == List->List.Blink);
  88. ExDeleteResourceLite(&List->Lock);
  89. }
  90. //+-------------------------------------------------------------------------
  91. //
  92. // Function: KerbInitializeListEntry
  93. //
  94. // Synopsis: Initializes a newly created list entry for later
  95. // insertion onto the list.
  96. //
  97. // Effects: The reference count is set to one and the links are set
  98. // to NULL.
  99. //
  100. // Arguments: ListEntry - the list entry to initialize
  101. //
  102. // Requires:
  103. //
  104. // Returns: none
  105. //
  106. // Notes:
  107. //
  108. //
  109. //--------------------------------------------------------------------------
  110. VOID
  111. KerbInitializeListEntry(
  112. IN OUT PKERBEROS_LIST_ENTRY ListEntry
  113. )
  114. {
  115. PAGED_CODE();
  116. ListEntry->ReferenceCount = 1;
  117. ListEntry->Next.Flink = ListEntry->Next.Blink = NULL;
  118. }
  119. //+-------------------------------------------------------------------------
  120. //
  121. // Function: KerbInsertListEntry
  122. //
  123. // Synopsis: Inserts an entry into a kerberos list
  124. //
  125. // Effects: increments the reference count on the entry - if the
  126. // list entry was formly referenced it remains referenced.
  127. //
  128. // Arguments: ListEntry - the entry to insert
  129. // List - the list in which to insert the ListEntry
  130. //
  131. // Requires:
  132. //
  133. // Returns: nothing
  134. //
  135. // Notes:
  136. //
  137. //
  138. //--------------------------------------------------------------------------
  139. VOID
  140. KerbInsertListEntry(
  141. IN PKERBEROS_LIST_ENTRY ListEntry,
  142. IN PKERBEROS_LIST List
  143. )
  144. {
  145. PAGED_CODE();
  146. ListEntry->ReferenceCount++;
  147. KerbLockList(List);
  148. KerbValidateList(List);
  149. InsertHeadList(
  150. &List->List,
  151. &ListEntry->Next
  152. );
  153. KerbValidateList(List);
  154. KerbUnlockList(List);
  155. }
  156. //+-------------------------------------------------------------------------
  157. //
  158. // Function: KerbReferenceListEntry
  159. //
  160. // Synopsis: References a list entry. If the flag RemoveFromList
  161. // has been specified, the entry is unlinked from the
  162. // list.
  163. //
  164. // Effects: bumps the reference count on the entry (unless it is
  165. // being removed from the list)
  166. //
  167. // Arguments:
  168. //
  169. // Requires: The list must be locked when calling this routine
  170. //
  171. // Returns:
  172. //
  173. // Notes:
  174. //
  175. //
  176. //--------------------------------------------------------------------------
  177. VOID
  178. KerbReferenceListEntry(
  179. IN PKERBEROS_LIST List,
  180. IN PKERBEROS_LIST_ENTRY ListEntry,
  181. IN BOOLEAN RemoveFromList
  182. )
  183. {
  184. PAGED_CODE();
  185. KerbValidateList(List);
  186. if (RemoveFromList)
  187. {
  188. RemoveEntryList(&ListEntry->Next);
  189. ListEntry->Next.Flink = NULL;
  190. ListEntry->Next.Blink = NULL;
  191. }
  192. else
  193. {
  194. ListEntry->ReferenceCount++;
  195. }
  196. KerbValidateList(List);
  197. }
  198. //+-------------------------------------------------------------------------
  199. //
  200. // Function: KerbDereferenceListEntry
  201. //
  202. // Synopsis: Dereferences a list entry and returns a flag indicating
  203. // whether the entry should be freed.
  204. //
  205. // Effects: decrements reference count on list entry
  206. //
  207. // Arguments: ListEntry - the list entry to dereference
  208. // List - the list containing the list entry
  209. //
  210. // Requires:
  211. //
  212. // Returns: TRUE - the list entry should be freed
  213. // FALSE - the list entry is still referenced
  214. //
  215. // Notes:
  216. //
  217. //
  218. //--------------------------------------------------------------------------
  219. BOOLEAN
  220. KerbDereferenceListEntry(
  221. IN PKERBEROS_LIST_ENTRY ListEntry,
  222. IN PKERBEROS_LIST List
  223. )
  224. {
  225. BOOLEAN DeleteEntry = FALSE;
  226. PAGED_CODE();
  227. KerbLockList(List);
  228. KerbValidateList(List);
  229. ListEntry->ReferenceCount -= 1;
  230. if (ListEntry->ReferenceCount == 0)
  231. {
  232. DeleteEntry = TRUE;
  233. }
  234. KerbValidateList(List);
  235. KerbUnlockList(List);
  236. return(DeleteEntry);
  237. }
  238. #if DBG
  239. //+-------------------------------------------------------------------------
  240. //
  241. // Function: KerbValidateListEx
  242. //
  243. // Synopsis: Validates that a list is valid
  244. //
  245. // Effects: traverses a list to make sure it is has no loops
  246. //
  247. // Arguments: List - The list to validate
  248. //
  249. // Requires:
  250. //
  251. // Returns: none
  252. //
  253. // Notes: This routine assumes there are less than 1000 entries
  254. // in the list.
  255. //
  256. //
  257. //--------------------------------------------------------------------------
  258. VOID
  259. KerbValidateListEx(
  260. IN PKERBEROS_LIST List
  261. )
  262. {
  263. ULONG Entries = 0;
  264. PLIST_ENTRY ListEntry;
  265. PAGED_CODE();
  266. for (ListEntry = List->List.Flink ;
  267. ListEntry != &List->List ;
  268. ListEntry = ListEntry->Flink )
  269. {
  270. if (++Entries > 1000) {
  271. DebugLog((DEB_ERROR,"List is looping - more than 1000 entries found\n"));
  272. DbgBreakPoint();
  273. break;
  274. }
  275. }
  276. }
  277. #endif // DBG