[1493] | 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 |
|
---|
[2999] | 26 | #define _XOPEN_SOURCE 1
|
---|
[19821] | 27 | // This was added for Solaris, but it makes things worse on Solaris for me...
|
---|
| 28 | // #define _XOPEN_SOURCE_EXTENDED 1
|
---|
[2999] | 29 |
|
---|
[23047] | 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)
|
---|
[2999] | 35 |
|
---|
[1600] | 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 |
|
---|
[19275] | 44 | // use the standard namespace
|
---|
| 45 | #if !defined (GSDL_NAMESPACE_BROKEN)
|
---|
[23047] | 46 | # if defined(GSDL_USE_OBJECTSPACE)
|
---|
[19275] | 47 | using namespace ospace::std;
|
---|
[23047] | 48 | # else
|
---|
[19275] | 49 | using namespace std;
|
---|
[23047] | 50 | # endif
|
---|
[19275] | 51 | #endif
|
---|
| 52 |
|
---|
[23047] | 53 | #include "gsdlconf.h"
|
---|
[19275] | 54 |
|
---|
[23047] | 55 | // include crypt
|
---|
[26649] | 56 | #if defined(_MSC_VER)
|
---|
[23047] | 57 | # include <windows.h>
|
---|
| 58 | # include <stdio.h>
|
---|
| 59 | # include <crypt.h>
|
---|
| 60 | #else
|
---|
| 61 | # include "config.h"
|
---|
[26649] | 62 | # include <getpass.h>
|
---|
[23047] | 63 | # include <crypt.h>
|
---|
| 64 |
|
---|
[26649] | 65 |
|
---|
[23047] | 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 |
|
---|
[26649] | 72 | #endif /* not _MSC_VER */
|
---|
[23047] | 73 |
|
---|
[1493] | 74 | #include <string.h>
|
---|
[19275] | 75 | //#include <pwd.h>
|
---|
[1493] | 76 |
|
---|
[26649] | 77 | #if defined(_MSC_VER)
|
---|
[23047] | 78 | char* 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
|
---|
[19275] | 121 |
|
---|
[26798] | 122 |
|
---|
| 123 | #if defined(__ANDROID__)
|
---|
| 124 |
|
---|
| 125 | // See:
|
---|
| 126 | // http://stackoverflow.com/questions/1196418/getting-a-password-in-c-without-using-getpass-3
|
---|
| 127 |
|
---|
| 128 | #include <stdio.h>
|
---|
| 129 | #include <stdlib.h>
|
---|
| 130 | #include <termios.h>
|
---|
| 131 |
|
---|
| 132 | char* getpass(const char* prompt)
|
---|
| 133 | {
|
---|
| 134 | struct termios oflags, nflags;
|
---|
| 135 | char* password = (char*)malloc(64);
|
---|
| 136 |
|
---|
| 137 | /* disabling echo */
|
---|
| 138 | tcgetattr(fileno(stdin), &oflags);
|
---|
| 139 | nflags = oflags;
|
---|
| 140 | nflags.c_lflag &= ~ECHO;
|
---|
| 141 | nflags.c_lflag |= ECHONL;
|
---|
| 142 |
|
---|
| 143 | if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
|
---|
| 144 | perror("tcsetattr");
|
---|
| 145 | return NULL;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | printf(prompt);
|
---|
| 149 | fgets(password, sizeof(password), stdin);
|
---|
| 150 | password[strlen(password) - 1] = 0;
|
---|
| 151 |
|
---|
| 152 | /* restore terminal */
|
---|
| 153 | if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
|
---|
| 154 | perror("tcsetattr");
|
---|
| 155 | return NULL;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | return password;
|
---|
| 159 | }
|
---|
| 160 | #endif
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 |
|
---|
[1493] | 165 | int main (int argc, char *argv[]) {
|
---|
| 166 |
|
---|
[18493] | 167 | int password_ok = 0;
|
---|
[19275] | 168 | char c[129];
|
---|
[18493] | 169 | int i;
|
---|
| 170 |
|
---|
| 171 | for (i=0; i<3; i++) {
|
---|
[26649] | 172 | #if defined(_MSC_VER)
|
---|
[23047] | 173 | HANDLE hSTDIN;
|
---|
| 174 | DWORD fdwMode, fdwPrevMode;
|
---|
| 175 | int counter=0;
|
---|
| 176 |
|
---|
| 177 | hSTDIN = GetStdHandle(STD_INPUT_HANDLE);
|
---|
| 178 | if (hSTDIN == INVALID_HANDLE_VALUE)
|
---|
| 179 | {
|
---|
| 180 | cerr << "There was an error getting a handle to the standard input\n";
|
---|
| 181 | return 1;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | if (!GetConsoleMode(hSTDIN, &fdwPrevMode))
|
---|
| 185 | {
|
---|
| 186 | cerr << "There was an error getting a handle to the current console mode\n";
|
---|
| 187 | return 2;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | fdwMode = NULL;
|
---|
| 191 | if (!SetConsoleMode(hSTDIN, fdwMode))
|
---|
| 192 | {
|
---|
| 193 | cerr << "There was an error setting the new handle mode\n";
|
---|
| 194 | return 3;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[23048] | 197 | cerr << "Enter password:";
|
---|
[23047] | 198 | char* a = getPassword(hSTDIN);
|
---|
[19275] | 199 | #else
|
---|
[23047] | 200 | char* a = getpass("Enter password:");
|
---|
[19275] | 201 | #endif
|
---|
[1493] | 202 | int len = strlen (a);
|
---|
[18493] | 203 | if (len < 3 || len > 128) {
|
---|
| 204 | cerr << "Password must be between 3 and 128 characters long. Try again\n";
|
---|
[1493] | 205 | continue;
|
---|
| 206 | }
|
---|
[18493] | 207 |
|
---|
[1493] | 208 | strcpy (c, a);
|
---|
[23047] | 209 |
|
---|
[26649] | 210 | #if defined(_MSC_VER)
|
---|
[19279] | 211 | cerr << "Re-enter password: ";
|
---|
[23047] | 212 | char* b = getPassword(hSTDIN);
|
---|
[19275] | 213 | #else
|
---|
[23047] | 214 | char* b = getpass("Re-enter password:");
|
---|
[19275] | 215 | #endif
|
---|
[18493] | 216 | if ((strcmp (c, b)) == 0) {
|
---|
| 217 | password_ok = 1;
|
---|
| 218 | break;
|
---|
| 219 | }
|
---|
| 220 | else {
|
---|
| 221 | cerr << "Passwords didn't match. Try again.\n";
|
---|
| 222 | }
|
---|
[1493] | 223 | }
|
---|
| 224 |
|
---|
[18493] | 225 | if (!password_ok) {
|
---|
| 226 | cerr << "Failed to capture password." << endl;
|
---|
| 227 | return -1;
|
---|
| 228 | }
|
---|
[23047] | 229 |
|
---|
[1493] | 230 | char *salt = "Tp";
|
---|
| 231 | char *pw = crypt (c, salt);
|
---|
| 232 |
|
---|
| 233 | cout << pw << "\n";
|
---|
[23047] | 234 |
|
---|
[1493] | 235 | return 0;
|
---|
| 236 | }
|
---|