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

Last change on this file since 26649 was 26649, checked in by davidb, 8 years ago

Code changed from using WIN32 use to _MSC_VER (the difference being the former is set for any Windows based compiler, whilst the latter is specifically set by a MicroSoft Visual Studio compiler). Up to this point the difference was not important, however to allow for cross-compilation (using mingw under Linux to produce native Windows binaries) the difference is imporant, and needs to be used more carefully

  • 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(_MSC_VER)
57# include <windows.h>
58# include <stdio.h>
59# include <crypt.h>
60#else
61# include "config.h"
62# include <getpass.h>
63# include <crypt.h>
64
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 _MSC_VER */
73
74#include <string.h>
75//#include <pwd.h>
76
77#if defined(_MSC_VER)
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(_MSC_VER)
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 cerr << "Enter password:";
155 char* a = getPassword(hSTDIN);
156#else
157 char* a = getpass("Enter password:");
158#endif
159 int len = strlen (a);
160 if (len < 3 || len > 128) {
161 cerr << "Password must be between 3 and 128 characters long. Try again\n";
162 continue;
163 }
164
165 strcpy (c, a);
166
167#if defined(_MSC_VER)
168 cerr << "Re-enter password: ";
169 char* b = getPassword(hSTDIN);
170#else
171 char* b = getpass("Re-enter password:");
172#endif
173 if ((strcmp (c, b)) == 0) {
174 password_ok = 1;
175 break;
176 }
177 else {
178 cerr << "Passwords didn't match. Try again.\n";
179 }
180 }
181
182 if (!password_ok) {
183 cerr << "Failed to capture password." << endl;
184 return -1;
185 }
186
187 char *salt = "Tp";
188 char *pw = crypt (c, salt);
189
190 cout << pw << "\n";
191
192 return 0;
193}
Note: See TracBrowser for help on using the repository browser.