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.

353 lines
18 KiB

  1. USE Winlogon
  2. DECLARE @bSendmail bit
  3. SET @bSendmail = 1
  4. DECLARE @SCARD_W_WRONG_CHV bigint
  5. SET @SCARD_W_WRONG_CHV = -2146434965
  6. DECLARE @crlf nvarchar(2)
  7. SET @crlf = CHAR(13) + CHAR(10)
  8. DECLARE @MessageBody nvarchar(4000)
  9. SET @MessageBody = ""
  10. DECLARE @Buffer nvarchar(256), @Number nvarchar(5), @Percent nvarchar(3)
  11. DECLARE @Checkdate datetime
  12. SET @Checkdate = DATEADD(day, -8, GETDATE())
  13. --SET @Checkdate = DATEADD(day, -1, GETDATE())
  14. --
  15. -- Get number of card authentications
  16. --
  17. SELECT CARD
  18. FROM AuthMonitor
  19. WHERE TIMESTAMP > @Checkdate
  20. AND CARD <> ""
  21. DECLARE @NumCardAuth int
  22. SET @NumCardAuth = @@ROWCOUNT
  23. --
  24. -- Get number of card failures
  25. --
  26. SELECT CARD
  27. FROM AuthMonitor
  28. WHERE TIMESTAMP > @Checkdate
  29. AND CARD <> ""
  30. AND STATUS <> 0
  31. AND STATUS <> @SCARD_W_WRONG_CHV
  32. DECLARE @NumCardFailures int
  33. SET @NumCardFailures = @@ROWCOUNT
  34. --
  35. -- Get number of TS card auth.
  36. --
  37. SELECT CARD
  38. FROM AuthMonitor
  39. WHERE TIMESTAMP > @Checkdate
  40. AND CARD <> ""
  41. AND SESSION <> 0
  42. DECLARE @NumTSCardAuth int
  43. SET @NumTSCardAuth = @@ROWCOUNT
  44. --
  45. -- Get number of TS card failures
  46. --
  47. SELECT CARD
  48. FROM AuthMonitor
  49. WHERE TIMESTAMP > @Checkdate
  50. AND CARD <> ""
  51. AND SESSION <> 0
  52. AND STATUS <> 0
  53. DECLARE @NumTSCardFailures int
  54. SET @NumTSCardFailures = @@ROWCOUNT
  55. --
  56. -- Get number of authentications per CSP
  57. --
  58. CREATE TABLE #CspAuth
  59. (
  60. CARD nvarchar(64),
  61. FAILURE int,
  62. NUMBER int
  63. )
  64. DECLARE @iCardHandle int, @stCard nvarchar(64)
  65. SET @iCardHandle = 0
  66. EXEC #GetCard @stCard OUTPUT, @iCardHandle OUTPUT
  67. WHILE @stCard <> ""
  68. BEGIN
  69. -- Get total number of card authentications
  70. SELECT CARD
  71. FROM AuthMonitor
  72. WHERE TIMESTAMP > @Checkdate
  73. AND CARD = @stCard
  74. INSERT INTO #CspAuth VALUES (@stCard, 0, @@ROWCOUNT)
  75. -- Get number of failures per card
  76. SELECT CARD
  77. FROM AuthMonitor
  78. WHERE TIMESTAMP > @Checkdate
  79. AND CARD = @stCard
  80. AND STATUS <> 0
  81. AND STATUS <> @SCARD_W_WRONG_CHV
  82. INSERT INTO #CspAuth VALUES (@stCard, 1, @@ROWCOUNT)
  83. EXEC #GetCard @stCard OUTPUT, @iCardHandle OUTPUT
  84. END
  85. --
  86. -- Create the message for card authentications and failures
  87. --
  88. DECLARE @PerCardAuth AS nvarchar(1000)
  89. SET @PerCardAuth = "Smart card authentications Total | Failures" + @crlf +
  90. REPLICATE("-", 60) + @crlf
  91. -- Total
  92. SET @Number = CAST(@NumCardAuth AS nvarchar(5))
  93. EXEC master.dbo.xp_sprintf @Buffer OUTPUT, "� %-34s%5s%4s%%", "Total", @Number, "100"
  94. SET @PerCardAuth = @PerCardAuth + @Buffer
  95. -- Total failures
  96. SET @Number = CAST(@NumCardFailures AS nvarchar(5))
  97. IF @NumCardAuth <> 0
  98. SET @Percent = CAST(@NumCardFailures * 100 / @NumCardAuth AS nvarchar(3))
  99. ELSE
  100. SET @Percent = 0
  101. EXEC master.dbo.xp_sprintf @Buffer OUTPUT, " | %5s%4s%%", @Number, @Percent
  102. SET @PerCardAuth = @PerCardAuth + @Buffer + @crlf
  103. -- TS
  104. SET @Number = CAST(@NumTSCardAuth AS nvarchar(5))
  105. SET @Percent = CAST(@NumTSCardAuth * 100 / @NumCardAuth AS nvarchar(3))
  106. EXEC master.dbo.xp_sprintf @Buffer OUTPUT, "� %-34s%5s%4s%%", "TS Client", @Number, @Percent
  107. SET @PerCardAuth = @PerCardAuth + @Buffer
  108. -- TS failures
  109. SET @Number = CAST(@NumTSCardFailures AS nvarchar(5))
  110. IF @NumCardAuth <> 0
  111. SET @Percent = CAST(@NumTSCardFailures * 100 / @NumTSCardAuth AS nvarchar(3))
  112. ELSE
  113. SET @Percent = 0
  114. EXEC master.dbo.xp_sprintf @Buffer OUTPUT, " | %5s%4s%%", @Number, @Percent
  115. SET @PerCardAuth = @PerCardAuth + @Buffer + @crlf
  116. DECLARE CardCursor CURSOR FOR
  117. SELECT CARD, FAILURE, NUMBER
  118. FROM #CspAuth
  119. ORDER BY CARD ASC
  120. DECLARE @Failure int, @NumAuth int
  121. DECLARE @NumPerCardAuth int, @NumPerCardFailures int
  122. SET @NumPerCardAuth = -1
  123. SET @NumPerCardFailures = -1
  124. OPEN CardCursor
  125. FETCH NEXT FROM CardCursor
  126. INTO @stCard, @Failure, @NumAuth
  127. WHILE @@FETCH_STATUS = 0
  128. BEGIN
  129. IF @Failure = 0
  130. SET @NumPerCardAuth = @NumAuth
  131. ELSE
  132. SET @NumPerCardFailures = @NumAuth
  133. IF @NumPerCardAuth <> -1 AND @NumPerCardFailures <> -1
  134. BEGIN
  135. -- Smart card
  136. SET @Number = CAST(@NumPerCardAuth AS nvarchar(5))
  137. SET @Percent = CAST(@NumPerCardAuth * 100 / @NumCardAuth AS nvarchar(3))
  138. EXEC master.dbo.xp_sprintf @Buffer OUTPUT, "� %-34s%5s%4s%%", @stCard, @Number, @Percent
  139. SET @PerCardAuth = @PerCardAuth + @Buffer
  140. -- Smart card failures
  141. SET @Number = CAST(@NumPerCardFailures AS nvarchar(5))
  142. IF @NumPerCardAuth <> 0
  143. SET @Percent = CAST(@NumPerCardFailures * 100 / @NumPerCardAuth AS nvarchar(3))
  144. ELSE
  145. SET @Percent = 0
  146. EXEC master.dbo.xp_sprintf @Buffer OUTPUT, " | %5s%4s%%", @Number, @Percent
  147. SET @PerCardAuth = @PerCardAuth + @Buffer + @crlf
  148. SET @NumPerCardAuth = -1
  149. SET @NumPerCardFailures = -1
  150. END
  151. FETCH NEXT FROM CardCursor
  152. INTO @stCard, @Failure, @NumAuth
  153. END
  154. CLOSE CardCursor
  155. DEALLOCATE CardCursor
  156. --
  157. -- Get number of authentications per Reader
  158. --
  159. CREATE TABLE #ReaderAuth
  160. (
  161. READER nvarchar(64),
  162. FAILURE int,
  163. NUMBER int
  164. )
  165. DECLARE @iReaderHandle int, @stReader nvarchar(64)
  166. SET @iReaderHandle = 0
  167. EXEC #GetReader @stReader OUTPUT, @iReaderHandle OUTPUT
  168. WHILE @stReader <> ""
  169. BEGIN
  170. -- Get total number of reader operations
  171. SELECT READER
  172. FROM AuthMonitor
  173. WHERE TIMESTAMP > @Checkdate
  174. AND READER LIKE @stReader + "%"
  175. INSERT INTO #ReaderAuth VALUES (@stReader, 0, @@ROWCOUNT)
  176. -- Get number of failures per reader
  177. SELECT READER
  178. FROM AuthMonitor
  179. WHERE TIMESTAMP > @Checkdate
  180. AND READER LIKE @stReader + "%"
  181. AND STATUS <> 0
  182. AND STATUS <> @SCARD_W_WRONG_CHV
  183. INSERT INTO #ReaderAuth VALUES (@stReader, 1, @@ROWCOUNT)
  184. EXEC #GetReader @stReader OUTPUT, @iReaderHandle OUTPUT
  185. END
  186. --
  187. -- Create the message for Reader authentications and failures
  188. --
  189. DECLARE @PerReaderAuth AS nvarchar(1000)
  190. SET @PerReaderAuth = "Reader operations Total | Failures" + @crlf +
  191. REPLICATE("-", 60) + @crlf
  192. DECLARE ReaderCursor CURSOR FOR
  193. SELECT READER, FAILURE, NUMBER
  194. FROM #ReaderAuth
  195. ORDER BY READER ASC
  196. DECLARE @NumPerReaderAuth int, @NumPerReaderFailures int
  197. SET @NumPerReaderAuth = -1
  198. SET @NumPerReaderFailures = -1
  199. OPEN ReaderCursor
  200. FETCH NEXT FROM ReaderCursor
  201. INTO @stReader, @Failure, @NumAuth
  202. WHILE @@FETCH_STATUS = 0
  203. BEGIN
  204. IF @Failure = 0
  205. SET @NumPerReaderAuth = @NumAuth
  206. ELSE
  207. SET @NumPerReaderFailures = @NumAuth
  208. IF @NumPerReaderAuth <> -1 AND @NumPerReaderFailures <> -1
  209. BEGIN
  210. -- Reader
  211. SET @Number = CAST(@NumPerReaderAuth AS nvarchar(5))
  212. SET @Percent = CAST(@NumPerReaderAuth * 100 / @NumCardAuth AS nvarchar(3))
  213. EXEC master.dbo.xp_sprintf @Buffer OUTPUT, "� %-34s%5s%4s%%", @stReader, @Number, @Percent
  214. SET @PerReaderAuth = @PerReaderAuth + @Buffer
  215. -- Reader failures
  216. SET @Number = CAST(@NumPerReaderFailures AS nvarchar(5))
  217. IF @NumPerReaderAuth <> 0
  218. SET @Percent = CAST(@NumPerReaderFailures * 100 / @NumPerReaderAuth AS nvarchar(3))
  219. ELSE
  220. SET @Percent = 0
  221. EXEC master.dbo.xp_sprintf @Buffer OUTPUT, " | %5s%4s%%", @Number, @Percent
  222. SET @PerReaderAuth = @PerReaderAuth + @Buffer + @crlf
  223. SET @NumPerReaderAuth = -1
  224. SET @NumPerReaderFailures = -1
  225. END
  226. FETCH NEXT FROM ReaderCursor
  227. INTO @stReader, @Failure, @NumAuth
  228. END
  229. CLOSE ReaderCursor
  230. DEALLOCATE ReaderCursor
  231. --
  232. -- Get number of failures per failure type
  233. --
  234. DECLARE @Status bigint
  235. DECLARE @FailureNumbers nvarchar(1400)
  236. SET @FailureNumbers = "Failures by error code" + @crlf +
  237. REPLICATE("-", 47) + @crlf
  238. DECLARE FailureCursor CURSOR FOR
  239. SELECT DISTINCT STATUS
  240. FROM AuthMonitor
  241. WHERE TIMESTAMP > @Checkdate
  242. AND CARD <> ""
  243. AND STATUS < 0
  244. AND STATUS <> @SCARD_W_WRONG_CHV
  245. OPEN FailureCursor
  246. FETCH NEXT FROM FailureCursor
  247. INTO @Status
  248. WHILE @@FETCH_STATUS = 0
  249. BEGIN
  250. SELECT STATUS
  251. FROM AuthMonitor
  252. WHERE TIMESTAMP > @Checkdate
  253. AND CARD <> ""
  254. AND STATUS = @Status
  255. DECLARE @iNumFailures int
  256. SET @iNumFailures = @@ROWCOUNT
  257. SET @Number = CAST(@iNumFailures AS nvarchar(5))
  258. IF @NumCardFailures <> 0
  259. SET @Percent = CAST(@iNumFailures * 100 / @NumCardFailures AS nvarchar(3))
  260. ELSE
  261. SET @Percent = 0
  262. DECLARE @stError nvarchar(32), @stHex nvarchar(8)
  263. EXEC #Dec2Error @Status, @stHex OUTPUT, @stError OUTPUT
  264. EXEC master.dbo.xp_sprintf @Buffer OUTPUT, "� %-34s%5s %3s%%", @stError, @Number, @Percent
  265. SET @FailureNumbers =
  266. @FailureNumbers + @Buffer + @crlf
  267. FETCH NEXT FROM FailureCursor
  268. INTO @Status
  269. END
  270. CLOSE FailureCursor
  271. DEALLOCATE FailureCursor
  272. --
  273. -- Send mail
  274. --
  275. SET @MessageBody = @PerCardAuth + @crlf +
  276. @PerReaderAuth + @crlf +
  277. @FailureNumbers
  278. IF @bSendmail <> 0
  279. EXEC master.dbo.xp_sendmail
  280. @recipients = 'smcaft',
  281. @message = @MessageBody,
  282. @subject = 'Smart card self host report - authentication statistics'
  283. ELSE
  284. PRINT @MessageBody
  285. GO