| 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/xstring.h" |
| 27 | #include "common/setup_after.h" |
| 28 | |
| 29 | using namespace pvpgn; |
| 30 | |
| 31 | namespace |
| 32 | { |
| 33 | |
| 34 | void usage(char const * progname) |
| 35 | { |
| 36 | std::fprintf(stderr, |
| 37 | "usage: %s [<options>] [--] [<cleartextpassword>]\n" |
| 38 | " -h, --help, --usage show this information and exit\n" |
| 39 | " -v, --version print version number and exit\n", progname); |
| 40 | |
| 41 | std::exit(EXIT_FAILURE); |
| 42 | } |
| 43 | |
| 44 | } |
| 45 | |
| 46 | extern int main(int argc, char * argv[]) |
| 47 | { |
| 48 | |
| 49 | char const * pass = NULL; |
| 50 | int a; |
| 51 | int forcepass = 0; |
| 52 | |
| 53 | if (argc < 1 || !argv || !argv[0]) |
| 54 | { |
| 55 | std::fprintf(stderr, "bad arguments\n"); |
| 56 | return EXIT_FAILURE; |
| 57 | } |
| 58 | |
| 59 | for (a = 1; a < argc; a++) |
| 60 | if (forcepass && !pass) |
| 61 | pass = argv[a]; |
| 62 | else if (std::strcmp(argv[a], "-") == 0 && !pass) |
| 63 | pass = argv[a]; |
| 64 | else if (argv[a][0] != '-' && !pass) |
| 65 | pass = argv[a]; |
| 66 | else if (forcepass || argv[a][0] != '-' || std::strcmp(argv[a], "-") == 0) |
| 67 | { |
| 68 | std::fprintf(stderr, "%s: extra password argument \"%s\"\n", argv[0], argv[a]); |
| 69 | usage(argv[0]); |
| 70 | } |
| 71 | else if (std::strcmp(argv[a], "--") == 0) |
| 72 | forcepass = 1; |
| 73 | else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0) |
| 74 | { |
| 75 | std::printf("version " PVPGN_VERSION "\n"); |
| 76 | return EXIT_SUCCESS; |
| 77 | } |
| 78 | else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage") |
| 79 | == 0) |
| 80 | usage(argv[0]); |
| 81 | else |
| 82 | { |
| 83 | std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]); |
| 84 | usage(argv[0]); |
| 85 | } |
| 86 | |
| 87 | { |
| 88 | char buff[256]; |
| 89 | t_hash hash; |
| 90 | |
| 91 | eventlog_set(stderr); /* bnet_hash() and friends use eventlog */ |
| 92 | |
| 93 | if (!pass) |
| 94 | { |
| 95 | std::printf("Enter password to hash: "); |
| 96 | std::fflush(stdout); |
| 97 | std::fgets(buff, 256, stdin); |
| 98 | if (buff[0] != '\0') |
| 99 | buff[std::strlen(buff) - 1] = '\0'; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | std::strncpy(buff, pass, sizeof(buff)); |
| 104 | buff[sizeof(buff)-1] = '\0'; |
| 105 | } |
| 106 | |
| 107 | /* FIXME: what is the max password length? */ |
| 108 | strtolower(buff); |
| 109 | |
| 110 | bnet_hash(&hash, std::strlen(buff), buff); |
| 111 | std::printf("\"BNET\\\\acct\\\\passhash1\"=\"%s\"\n", hash_get_str(hash)); |
| 112 | } |
| 113 | |
| 114 | return EXIT_SUCCESS; |
| 115 | } |