Changeset 23047 for main


Ignore:
Timestamp:
2010-10-06T11:18:32+13:00 (14 years ago)
Author:
sjm84
Message:

getpw will now handle input from a Windows console more intelligently

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/common-src/src/getpw/getpw.cpp

    r19821 r23047  
    2828// #define _XOPEN_SOURCE_EXTENDED 1
    2929
    30 #include "gsdlconf.h"
    31 
    32 
    33 // include crypt
    34 #if defined(__WIN32__)
    35 #include "crypt.h"
    36 #else
    37 
    38 #include "config.h"
    39 #if defined(HAVE_CRYPT_H)
    40 #include <crypt.h>
    41 #endif
    42 
    43 #if defined(HAVE_UNISTD_H)
    44 #include <unistd.h>
    45 #endif
    46 /* we are probably in trouble if we have neither crypt.h or unistd.h,
    47    but for now we'll do nothing about it... */
    48 
    49 #endif  /* not WIN32 */
     30// Windows version seems to be sensitive to the order of <iostream> and <crypt.h>
     31// having <iostream> *after* <crypt.h> lead to linking error such as:
     32//    __freea already defined in getpw.obj
     33// which seems to be caused by compiler confusion of whether it should be using
     34// /MD or /MT (which control dynamic or static/multi-threaded linking)
    5035
    5136#if defined(GSDL_USE_OBJECTSPACE)
     
    5944// use the standard namespace
    6045#if !defined (GSDL_NAMESPACE_BROKEN)
    61 #if defined(GSDL_USE_OBJECTSPACE)
     46#  if defined(GSDL_USE_OBJECTSPACE)
    6247using namespace ospace::std;
    63 #else
     48#  else
    6449using namespace std;
    65 #endif
     50#  endif
    6651#endif
    6752
     53#include "gsdlconf.h"
     54
     55// include crypt
     56#if defined(__WIN32__)
     57#  include <windows.h>
     58#  include <stdio.h>
     59#  include <crypt.h>
     60#else
     61#  include "config.h"
     62#  if defined(HAVE_CRYPT_H)
     63#  include <crypt.h>
     64#  endif
     65
     66#  if defined(HAVE_UNISTD_H)
     67#  include <unistd.h>
     68#  endif
     69/* we are probably in trouble if we have neither crypt.h or unistd.h,
     70   but for now we'll do nothing about it... */
     71
     72#endif  /* not WIN32 */
    6873
    6974#include <string.h>
    7075//#include <pwd.h>
    7176
     77#if defined(__WIN32__)
     78char* getPassword(HANDLE hSTDIN)
     79{
     80    char a[129];
     81    int charCount = 0;
     82    DWORD cNumRead;
     83    INPUT_RECORD irIn[128];
     84   
     85    bool done = false;
     86    while (!done)
     87    {
     88        if (!ReadConsoleInput(hSTDIN, irIn, 128, &cNumRead))
     89        {
     90            cerr << "There was an error reading input from the console\n";
     91            return strdup("");
     92        }
     93       
     94        for(int j = 0; j < cNumRead; j++)
     95        {
     96            if(irIn[j].EventType == KEY_EVENT)
     97            {
     98                KEY_EVENT_RECORD keyEvent = irIn[j].Event.KeyEvent;
     99                if(keyEvent.bKeyDown == false)
     100                {
     101                    if(keyEvent.wVirtualKeyCode == VK_RETURN)
     102                    {
     103                        done = true;
     104                        cout << "\n";
     105                    }
     106                    else
     107                    {
     108                        if(charCount < 129)
     109                        {
     110                            a[charCount++] = keyEvent.uChar.AsciiChar;
     111                        }
     112                    }
     113                }
     114            }
     115        }
     116    }
     117    a[charCount] = '\0';
     118    return strdup(a);
     119}
     120#endif
    72121
    73122int main (int argc, char *argv[]) {
     
    79128  for (i=0; i<3; i++)  {
    80129#if defined(__WIN32__)
    81     cerr << "Enter password  (will appear on screen): ";
    82     char a[129];
    83     cin.getline(a, 128);
     130    HANDLE hSTDIN;
     131    DWORD fdwMode, fdwPrevMode;
     132    int counter=0;
     133   
     134    hSTDIN = GetStdHandle(STD_INPUT_HANDLE);
     135    if (hSTDIN == INVALID_HANDLE_VALUE)
     136    {
     137        cerr << "There was an error getting a handle to the standard input\n";
     138        return 1;
     139    }
     140   
     141    if (!GetConsoleMode(hSTDIN, &fdwPrevMode))
     142    {
     143        cerr << "There was an error getting a handle to the current console mode\n";
     144        return 2;
     145    }
     146   
     147    fdwMode = NULL;
     148    if (!SetConsoleMode(hSTDIN, fdwMode))
     149    {
     150        cerr << "There was an error setting the new handle mode\n";
     151        return 3;
     152    }
     153   
     154    cout << "Enter password:";
     155    char* a = getPassword(hSTDIN);
     156   
    84157#else
    85     char *a = getpass("Enter password:");
     158    char* a = getpass("Enter password:");
    86159#endif
    87 
    88160    int len = strlen (a);
    89161    if (len < 3 || len > 128) {
     
    93165
    94166    strcpy (c, a);
    95 
     167   
    96168#if defined(__WIN32__)
    97169    cerr << "Re-enter password: ";
    98     char b[129];
    99     cin.getline(b, 128);
     170    char* b = getPassword(hSTDIN);
    100171#else
    101     char *b = getpass("Re-enter password:"); 
     172    char* b = getpass("Re-enter password:"); 
    102173#endif
    103 
    104174    if ((strcmp (c, b)) == 0) {
    105175      password_ok = 1;
     
    115185    return -1;
    116186  }
    117 
    118 
     187 
    119188  char *salt = "Tp";
    120189  char *pw = crypt (c, salt);
    121190
    122191  cout << pw << "\n";
    123  
     192
    124193  return 0;
    125194}
Note: See TracChangeset for help on using the changeset viewer.