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.

189 lines
5.8 KiB

  1. // Password.cpp : Implementation of CPassword
  2. #include "stdafx.h"
  3. #include "ScriptPW.h"
  4. #include "Password.h"
  5. /////////////////////////////////////////////////////////////////////////////
  6. // CPassword
  7. STDMETHODIMP CPassword::GetPassword( BSTR *bstrOutPassword )
  8. {
  9. // local variables
  10. WCHAR wch;
  11. DWORD dwIndex = 0;
  12. DWORD dwCharsRead = 0;
  13. DWORD dwPrevConsoleMode = 0;
  14. HANDLE hInputConsole = NULL;
  15. BOOL bIndirectionInput = FALSE;
  16. LPWSTR pwszPassword = NULL;
  17. const DWORD dwMaxPasswordSize = 256;
  18. // check the input
  19. if ( bstrOutPassword == NULL )
  20. {
  21. return E_FAIL;
  22. }
  23. // get the handle for the standard input
  24. hInputConsole = GetStdHandle( STD_INPUT_HANDLE );
  25. if ( hInputConsole == NULL )
  26. {
  27. // could not get the handle so return failure
  28. return E_FAIL;
  29. }
  30. // check for the input redirection on console and telnet session
  31. if( ( hInputConsole != (HANDLE)0x0000000F ) &&
  32. ( hInputConsole != (HANDLE)0x00000003 ) &&
  33. ( hInputConsole != INVALID_HANDLE_VALUE ) )
  34. {
  35. bIndirectionInput = TRUE;
  36. }
  37. // change the console mode properties if the input is not redirected
  38. if ( bIndirectionInput == FALSE )
  39. {
  40. // Get the current input mode of the input buffer
  41. GetConsoleMode( hInputConsole, &dwPrevConsoleMode );
  42. // Set the mode such that the control keys are processed by the system
  43. if ( SetConsoleMode( hInputConsole, ENABLE_PROCESSED_INPUT ) == 0 )
  44. {
  45. // could not set the mode, return failure
  46. return E_FAIL;
  47. }
  48. }
  49. // allocate memory for the password buffer
  50. pwszPassword = (LPWSTR) AllocateMemory( (dwMaxPasswordSize + 1) * sizeof( WCHAR ) );
  51. if ( pwszPassword == NULL )
  52. {
  53. return E_FAIL;
  54. }
  55. // Read the characters until a carriage return is hit
  56. for( ;; )
  57. {
  58. if ( bIndirectionInput == TRUE )
  59. {
  60. //read the contents of file
  61. if ( ReadFile( hInputConsole, &wch, 1, &dwCharsRead, NULL ) == FALSE )
  62. {
  63. FreeMemory( (LPVOID*) &pwszPassword );
  64. return E_FAIL;
  65. }
  66. // check for end of file
  67. if ( dwCharsRead == 0 )
  68. {
  69. break;
  70. }
  71. }
  72. else
  73. {
  74. if ( ReadConsole( hInputConsole, &wch, 1, &dwCharsRead, NULL ) == 0 )
  75. {
  76. // Set the original console settings
  77. SetConsoleMode( hInputConsole, dwPrevConsoleMode );
  78. // return failure
  79. FreeMemory( (LPVOID*) &pwszPassword );
  80. return E_FAIL;
  81. }
  82. }
  83. // Check for carraige return
  84. if ( wch == CARRIAGE_RETURN )
  85. {
  86. // break from the loop
  87. break;
  88. }
  89. // Check id back space is hit
  90. if ( wch == BACK_SPACE )
  91. {
  92. if ( dwIndex != 0 )
  93. {
  94. //
  95. // Remove a asterix from the console
  96. // (display of characters onto console is blocked)
  97. // move the cursor one character back
  98. // StringCchPrintfW(
  99. // wszBuffer,
  100. // SIZE_OF_ARRAY( wszBuffer ), L"%c", BACK_SPACE );
  101. // WriteConsole(
  102. // GetStdHandle( STD_OUTPUT_HANDLE ),
  103. // wszBuffer, 1, &dwCharsWritten, NULL );
  104. // replace the existing character with space
  105. // StringCchPrintfW(
  106. // wszBuffer,
  107. // SIZE_OF_ARRAY( wszBuffer ), L"%c", BLANK_CHAR );
  108. // WriteConsole(
  109. // GetStdHandle( STD_OUTPUT_HANDLE ),
  110. // wszBuffer, 1, &dwCharsWritten, NULL );
  111. // now set the cursor at back position
  112. // StringCchPrintfW(
  113. // wszBuffer,
  114. // SIZE_OF_ARRAY( wszBuffer ), L"%c", BACK_SPACE );
  115. // WriteConsole(
  116. // GetStdHandle( STD_OUTPUT_HANDLE ),
  117. // wszBuffer, 1, &dwCharsWritten, NULL );
  118. // decrement the index
  119. dwIndex--;
  120. }
  121. // process the next character
  122. continue;
  123. }
  124. // if the max password length has been reached then sound a beep
  125. if ( dwIndex == ( dwMaxPasswordSize - 1 ) )
  126. {
  127. // WriteConsole(
  128. // GetStdHandle( STD_OUTPUT_HANDLE ),
  129. // BEEP_SOUND, 1, &dwCharsWritten, NULL );
  130. }
  131. else
  132. {
  133. // check for new line character
  134. if ( wch != L'\n' )
  135. {
  136. // store the input character
  137. *( pwszPassword + dwIndex ) = wch;
  138. dwIndex++;
  139. // display asterix onto the console
  140. // WriteConsole(
  141. // GetStdHandle( STD_OUTPUT_HANDLE ),
  142. // ASTERIX, 1, &dwCharsWritten, NULL );
  143. }
  144. }
  145. }
  146. // Add the NULL terminator
  147. *( pwszPassword + dwIndex ) = cwchNullChar;
  148. // display the character ( new line character )
  149. // StringCopy( wszBuffer, L"\n\n", SIZE_OF_ARRAY( wszBuffer ) );
  150. // WriteConsole(
  151. // GetStdHandle( STD_OUTPUT_HANDLE ),
  152. // wszBuffer, 2, &dwCharsWritten, NULL );
  153. CComBSTR bstrPassword( pwszPassword );
  154. *bstrOutPassword = bstrPassword.Copy();
  155. // set the original console settings
  156. SetConsoleMode( hInputConsole, dwPrevConsoleMode );
  157. // free the memory
  158. FreeMemory( (LPVOID*) &pwszPassword );
  159. // return success
  160. return S_OK;
  161. }