passhook.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * --- END COPYRIGHT BLOCK --- */
  37. // Created: 2-8-2005
  38. // Author(s): Scott Bridges
  39. #include <windows.h>
  40. #include <ntsecapi.h>
  41. // Work around for enum redefinition
  42. // Effects nssILockOp enumeration in nssilckt.h
  43. #define Unlock Unlock_ntsecapi
  44. #include "../passhand.h"
  45. #ifndef STATUS_SUCCESS
  46. #define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
  47. #endif
  48. NTSTATUS NTAPI PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING Password)
  49. {
  50. char singleByteUsername[PASSHAND_BUF_SIZE];
  51. char singleBytePassword[PASSHAND_BUF_SIZE];
  52. HANDLE passhookEventHandle = OpenEvent(EVENT_MODIFY_STATE, FALSE, PASSHAND_EVENT_NAME);
  53. PASS_INFO newPassInfo;
  54. PASS_INFO_LIST passInfoList;
  55. HKEY regKey;
  56. DWORD type;
  57. unsigned long buffSize;
  58. char regBuff[PASSHAND_BUF_SIZE];
  59. unsigned long logLevel;
  60. fstream outLog;
  61. RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\PasswordSync", &regKey);
  62. buffSize = PASSHAND_BUF_SIZE;
  63. if(RegQueryValueEx(regKey, "Log Level", NULL, &type, (unsigned char*)regBuff, &buffSize) == ERROR_SUCCESS)
  64. {
  65. logLevel = (unsigned long)atoi(regBuff);
  66. }
  67. else
  68. {
  69. logLevel = 0;
  70. }
  71. if(logLevel > 0)
  72. {
  73. outLog.open("passhook.log", ios::out | ios::app);
  74. }
  75. RegCloseKey(regKey);
  76. _snprintf(singleByteUsername, PASSHAND_BUF_SIZE, "%S", UserName->Buffer);
  77. singleByteUsername[UserName->Length / 2] = '\0';
  78. _snprintf(singleBytePassword, PASSHAND_BUF_SIZE, "%S", Password->Buffer);
  79. singleBytePassword[Password->Length / 2] = '\0';
  80. if(outLog.is_open())
  81. {
  82. timeStamp(&outLog);
  83. outLog << "user " << singleByteUsername << " password changed" << endl;
  84. //outLog << "user " << singleByteUsername << " password changed to " << singleBytePassword << endl;
  85. }
  86. if(loadSet(&passInfoList, "passhook.dat") == 0)
  87. {
  88. if(outLog.is_open())
  89. {
  90. timeStamp(&outLog);
  91. outLog << passInfoList.size() << " entries loaded from file" << endl;
  92. }
  93. }
  94. else
  95. {
  96. if(outLog.is_open())
  97. {
  98. timeStamp(&outLog);
  99. outLog << "failed to load entries from file" << endl;
  100. }
  101. }
  102. newPassInfo.username = singleByteUsername;
  103. newPassInfo.password = singleBytePassword;
  104. passInfoList.push_back(newPassInfo);
  105. if(saveSet(&passInfoList, "passhook.dat") == 0)
  106. {
  107. if(outLog.is_open())
  108. {
  109. timeStamp(&outLog);
  110. outLog << passInfoList.size() << " entries saved to file" << endl;
  111. }
  112. }
  113. else
  114. {
  115. if(outLog.is_open())
  116. {
  117. timeStamp(&outLog);
  118. outLog << "failed to save entries to file" << endl;
  119. }
  120. }
  121. if(passhookEventHandle == NULL)
  122. {
  123. if(outLog.is_open())
  124. {
  125. timeStamp(&outLog);
  126. outLog << "can not get password sync service event handle, service not running" << endl;
  127. }
  128. }
  129. else
  130. {
  131. SetEvent(passhookEventHandle);
  132. }
  133. outLog.close();
  134. return STATUS_SUCCESS;
  135. }
  136. BOOL NTAPI PasswordFilter(PUNICODE_STRING UserName, PUNICODE_STRING FullName, PUNICODE_STRING Password, BOOL SetOperation)
  137. {
  138. return TRUE;
  139. }
  140. BOOL NTAPI InitializeChangeNotify()
  141. {
  142. return TRUE;
  143. }