source: main/trunk/greenstone2/common-src/src/getpw/getpw.cpp@ 23047

Last change on this file since 23047 was 23047, checked in by sjm84, 14 years ago

getpw will now handle input from a Windows console more intelligently

  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
1/**********************************************************************
2 *
3 * getpw.cpp --
4 * A component of the Greenstone digital library software
5 * from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 2000 The New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 **********************************************************************/
25
26#define _XOPEN_SOURCE 1
27// This was added for Solaris, but it makes things worse on Solaris for me...
28// #define _XOPEN_SOURCE_EXTENDED 1
29
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)
35
36#if defined(GSDL_USE_OBJECTSPACE)
37# include <ospace\std\iostream>
38#elif defined(GSDL_USE_IOS_H)
39# include <iostream.h>
40#else
41# include <iostream>
42#endif
43
44// use the standard namespace
45#if !defined (GSDL_NAMESPACE_BROKEN)
46# if defined(GSDL_USE_OBJECTSPACE)
47using namespace ospace::std;
48# else
49using namespace std;
50# endif
51#endif
52
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 */
73
74#include <string.h>
75//#include <pwd.h>
76
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
121
122int main (int argc, char *argv[]) {
123
124 int password_ok = 0;
125 char c[129];
126 int i;
127
128 for (i=0; i<3; i++) {
129#if defined(__WIN32__)
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
157#else
158 char* a = getpass("Enter password:");
159#endif
160 int len = strlen (a);
161 if (len < 3 || len > 128) {
162 cerr << "Password must be between 3 and 128 characters long. Try again\n";
163 continue;
164 }
165
166 strcpy (c, a);
167
168#if defined(__WIN32__)
169 cerr << "Re-enter password: ";
170 char* b = getPassword(hSTDIN);
171#else
172 char* b = getpass("Re-enter password:");
173#endif
174 if ((strcmp (c, b)) == 0) {
175 password_ok = 1;
176 break;
177 }
178 else {
179 cerr << "Passwords didn't match. Try again.\n";
180 }
181 }
182
183 if (!password_ok) {
184 cerr << "Failed to capture password." << endl;
185 return -1;
186 }
187
188 char *salt = "Tp";
189 char *pw = crypt (c, salt);
190
191 cout << pw << "\n";
192
193 return 0;
194}
Note: See TracBrowser for help on using the repository browser.