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.

119 lines
7.7 KiB

  1. --
  2. -- send message in case of a smart card auth failure
  3. --
  4. USE Winlogon
  5. DECLARE @bSendmail bit
  6. SET @bSendmail = 1
  7. DECLARE @crlf nvarchar(2)
  8. SET @crlf = CHAR(13) + CHAR(10)
  9. DECLARE @SCARD_W_WRONG_CHV bigint
  10. SET @SCARD_W_WRONG_CHV = -2146434965
  11. DECLARE @SCARD_W_INVALID_CHV bigint
  12. SET @SCARD_W_INVALID_CHV = -2146435030
  13. DECLARE @MessageBody nvarchar(4000)
  14. SET @MessageBody = ""
  15. DECLARE @dtChecktime datetime, @dtCurrenttime datetime
  16. SET @dtChecktime = DATEADD(minute, -6, GETDATE())
  17. SET @dtCurrenttime = DATEADD(minute, 1, GETDATE())
  18. --SET @dtChecktime = DATEADD(day, -1, GETDATE())
  19. DECLARE authmon_cursor CURSOR FOR
  20. SELECT BUILDLAB, CARD, CERTISSUER, DC, DOMAIN, MACHINENAME, READER, SESSION, STATUS, TIMESTAMP, UNLOCK, USERNAME
  21. FROM AuthMonitor
  22. WHERE CARD <> ""
  23. AND STATUS <> 0
  24. AND STATUS <> @SCARD_W_WRONG_CHV
  25. AND STATUS <> @SCARD_W_INVALID_CHV
  26. AND TIMESTAMP > @dtChecktime
  27. AND TIMESTAMP < @dtCurrenttime
  28. DECLARE @stBuildlab nvarchar(64)
  29. DECLARE @stCard nvarchar(32)
  30. DECLARE @stCertIssuer nvarchar(32)
  31. DECLARE @stDC nvarchar(32)
  32. DECLARE @stDomain nvarchar(32)
  33. DECLARE @stMachinename nvarchar(32)
  34. DECLARE @stReader nvarchar(32)
  35. DECLARE @bSession bit
  36. DECLARE @iStatus int
  37. DECLARE @dtTimestamp datetime
  38. DECLARE @bUnlock bit
  39. DECLARE @stUsername nvarchar(16)
  40. OPEN authmon_cursor
  41. FETCH NEXT FROM authmon_cursor
  42. INTO @stBuildlab, @stCard, @stCertIssuer, @stDC, @stDomain, @stMachinename, @stReader, @bSession, @iStatus, @dtTimestamp, @bUnlock, @stUsername
  43. DECLARE @iNumFailures int
  44. SET @iNumFailures = 0
  45. WHILE @@FETCH_STATUS = 0
  46. BEGIN
  47. -- ignore some people that are playing with the stuff a lot.
  48. IF LOWER(LEFT(@stMachineName, 7)) <> 'kschutz' AND
  49. LOWER(LEFT(@stMachineName, 8)) <> 'ericperl' AND
  50. LOWER(LEFT(@stMachineName, 5)) <> 'reidk'
  51. BEGIN
  52. SET @iNumFailures = @iNumFailures + 1
  53. DECLARE @stOperation nvarchar(10)
  54. IF @bUnlock = 0
  55. SET @stOperation = "Logon"
  56. ELSE
  57. SET @stOperation = "Unlock"
  58. DECLARE @stSession nvarchar(20)
  59. IF @bSession = 0
  60. SET @stSession = "Local"
  61. ELSE
  62. SET @stSession = "TS Client"
  63. DECLARE @stHex nvarchar(8), @stError nvarchar(32)
  64. EXEC #Dec2Error @iStatus, @stHex OUTPUT, @stError OUTPUT
  65. SET @MessageBody = @MessageBody +
  66. "Time: " + CAST(@dtTimestamp AS nvarchar(20)) + @crlf +
  67. "User: " + @stUsername + @crlf +
  68. "Operation: " + @stOperation + @crlf +
  69. "Session: " + @stSession + @crlf +
  70. "Status: " + @stError + " (0x" + @stHex + ")" + @crlf +
  71. "Machine: " + @stMachinename + @crlf +
  72. "Build: " + @stBuildlab + @crlf +
  73. "Domain: " + @stDomain + @crlf +
  74. "DC: " + @stDC + @crlf +
  75. "Card: " + @stCard + @crlf +
  76. "Certissuer: " + @stCertissuer + @crlf +
  77. "Reader: " + @stReader + @crlf +
  78. @crlf
  79. END
  80. FETCH NEXT FROM authmon_cursor
  81. INTO @stBuildlab, @stCard, @stCertIssuer, @stDC, @stDomain, @stMachinename, @stReader, @bSession, @iStatus, @dtTimestamp, @bUnlock, @stUsername
  82. END
  83. CLOSE authmon_cursor
  84. DEALLOCATE authmon_cursor
  85. IF @bSendmail = 0 AND @iNumFailures <> 0
  86. PRINT @MessageBody
  87. IF @bSendmail = 1 AND @iNumFailures <> 0
  88. BEGIN
  89. DECLARE @Return int
  90. EXEC @Return = master.dbo.xp_sendmail
  91. @recipients = 'smcaft',
  92. @message = @MessageBody,
  93. @subject = 'Smart card authentication failure'
  94. IF @Return <> 0
  95. RAISERROR ("xp_sendmail failed", 1, 1)
  96. END
  97. GO