| 1 | /* |
| 2 | * Copyright (C) 1999 Ross Combs ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #include "common/setup_before.h" |
| 19 | #include <cstdlib> |
| 20 | #include <cstring> |
| 21 | #include <cctype> |
| 22 | |
| 23 | #include "common/eventlog.h" |
| 24 | #include "common/bnethash.h" |
| 25 | #include "common/version.h" |
| 26 | #include "common/setup_after.h" |
| 27 | |
| 28 | using namespace pvpgn; |
| 29 | |
| 30 | namespace |
| 31 | { |
| 32 | |
| 33 | void usage(char const * progname) |
| 34 | { |
| 35 | std::fprintf(stderr, |
| 36 | "usage: %s [<options>] [--] [<cleartextpassword>]\n" |
| 37 | " -h, --help, --usage show this information and exit\n" |
| 38 | " -v, --version print version number and exit\n", progname); |
| 39 | |
| 40 | std::exit(EXIT_FAILURE); |
| 41 | } |
| 42 | |
| 43 | } |
| 44 | |
| 45 | extern int main(int argc, char * argv[]) |
| 46 | { |
| 47 | |
| 48 | char const * pass = NULL; |
| 49 | int a; |
| 50 | int forcepass = 0; |
| 51 | |
| 52 | if (argc < 1 || !argv || !argv[0]) |
| 53 | { |
| 54 | std::fprintf(stderr, "bad arguments\n"); |
| 55 | return EXIT_FAILURE; |
| 56 | } |
| 57 | |
| 58 | for (a = 1; a < argc; a++) |
| 59 | if (forcepass && !pass) |
| 60 | pass = argv[a]; |
| 61 | else if (std::strcmp(argv[a], "-") == 0 && !pass) |
| 62 | pass = argv[a]; |
| 63 | else if (argv[a][0] != '-' && !pass) |
| 64 | pass = argv[a]; |
| 65 | else if (forcepass || argv[a][0] != '-' || std::strcmp(argv[a], "-") == 0) |
| 66 | { |
| 67 | std::fprintf(stderr, "%s: extra password argument \"%s\"\n", argv[0], argv[a]); |
| 68 | usage(argv[0]); |
| 69 | } |
| 70 | else if (std::strcmp(argv[a], "--") == 0) |
| 71 | forcepass = 1; |
| 72 | else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0) |
| 73 | { |
| 74 | std::printf("version " PVPGN_VERSION "\n"); |
| 75 | return EXIT_SUCCESS; |
| 76 | } |
| 77 | else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage") |
| 78 | == 0) |
| 79 | usage(argv[0]); |
| 80 | else |
| 81 | { |
| 82 | std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]); |
| 83 | usage(argv[0]); |
| 84 | } |
| 85 | |
| 86 | { |
| 87 | char buff[256]; |
| 88 | t_hash hash; |
| 89 | |
| 90 | eventlog_set(stderr); /* sha1_hash() and friends use eventlog */ |
| 91 | |
| 92 | if (!pass) |
| 93 | { |
| 94 | std::printf("Enter password to hash: "); |
| 95 | std::fflush(stdout); |
| 96 | std::fgets(buff, 256, stdin); |
| 97 | if (buff[0] != '\0') |
| 98 | buff[std::strlen(buff) - 1] = '\0'; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | std::strncpy(buff, pass, sizeof(buff)); |
| 103 | buff[sizeof(buff)-1] = '\0'; |
| 104 | } |
| 105 | |
| 106 | sha1_hash(&hash, std::strlen(buff), buff); |
| 107 | std::printf("sha1 hash = %s\n", hash_get_str(hash)); |
| 108 | } |
| 109 | |
| 110 | return EXIT_SUCCESS; |
| 111 | } |