passhook.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2005 Red Hat, Inc.
  3. * All rights reserved.
  4. * --- END COPYRIGHT BLOCK --- */
  5. // Created: 2-8-2005
  6. // Author(s): Scott Bridges
  7. #include <windows.h>
  8. #include <ntsecapi.h>
  9. // Work around for enum redefinition
  10. // Effects nssILockOp enumeration in nssilckt.h
  11. #define Unlock Unlock_ntsecapi
  12. #include "../passhand.h"
  13. #ifndef STATUS_SUCCESS
  14. #define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
  15. #endif
  16. NTSTATUS NTAPI PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING Password)
  17. {
  18. char singleByteUsername[PASSHAND_BUF_SIZE];
  19. char singleBytePassword[PASSHAND_BUF_SIZE];
  20. HANDLE passhookEventHandle = OpenEvent(EVENT_MODIFY_STATE, FALSE, PASSHAND_EVENT_NAME);
  21. PASS_INFO newPassInfo;
  22. PASS_INFO_LIST passInfoList;
  23. HKEY regKey;
  24. DWORD type;
  25. unsigned long buffSize;
  26. char regBuff[PASSHAND_BUF_SIZE];
  27. unsigned long logLevel;
  28. fstream outLog;
  29. RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\PasswordSync", &regKey);
  30. buffSize = PASSHAND_BUF_SIZE;
  31. if(RegQueryValueEx(regKey, "Log Level", NULL, &type, (unsigned char*)regBuff, &buffSize) == ERROR_SUCCESS)
  32. {
  33. logLevel = (unsigned long)atoi(regBuff);
  34. }
  35. else
  36. {
  37. logLevel = 0;
  38. }
  39. if(logLevel > 0)
  40. {
  41. outLog.open("passhook.log", ios::out | ios::app);
  42. }
  43. RegCloseKey(regKey);
  44. _snprintf(singleByteUsername, PASSHAND_BUF_SIZE, "%S", UserName->Buffer);
  45. singleByteUsername[UserName->Length / 2] = '\0';
  46. _snprintf(singleBytePassword, PASSHAND_BUF_SIZE, "%S", Password->Buffer);
  47. singleBytePassword[Password->Length / 2] = '\0';
  48. if(outLog.is_open())
  49. {
  50. timeStamp(&outLog);
  51. outLog << "user " << singleByteUsername << " password changed" << endl;
  52. //outLog << "user " << singleByteUsername << " password changed to " << singleBytePassword << endl;
  53. }
  54. if(loadSet(&passInfoList, "passhook.dat") == 0)
  55. {
  56. if(outLog.is_open())
  57. {
  58. timeStamp(&outLog);
  59. outLog << passInfoList.size() << " entries loaded from file" << endl;
  60. }
  61. }
  62. else
  63. {
  64. if(outLog.is_open())
  65. {
  66. timeStamp(&outLog);
  67. outLog << "failed to load entries from file" << endl;
  68. }
  69. }
  70. newPassInfo.username = singleByteUsername;
  71. newPassInfo.password = singleBytePassword;
  72. passInfoList.push_back(newPassInfo);
  73. if(saveSet(&passInfoList, "passhook.dat") == 0)
  74. {
  75. if(outLog.is_open())
  76. {
  77. timeStamp(&outLog);
  78. outLog << passInfoList.size() << " entries saved to file" << endl;
  79. }
  80. }
  81. else
  82. {
  83. if(outLog.is_open())
  84. {
  85. timeStamp(&outLog);
  86. outLog << "failed to save entries to file" << endl;
  87. }
  88. }
  89. if(passhookEventHandle == NULL)
  90. {
  91. if(outLog.is_open())
  92. {
  93. timeStamp(&outLog);
  94. outLog << "can not get password sync service event handle, service not running" << endl;
  95. }
  96. }
  97. else
  98. {
  99. SetEvent(passhookEventHandle);
  100. }
  101. outLog.close();
  102. return STATUS_SUCCESS;
  103. }
  104. BOOL NTAPI PasswordFilter(PUNICODE_STRING UserName, PUNICODE_STRING FullName, PUNICODE_STRING Password, BOOL SetOperation)
  105. {
  106. return TRUE;
  107. }
  108. BOOL NTAPI InitializeChangeNotify()
  109. {
  110. return TRUE;
  111. }