[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
[2725] | 3 | * cryptpw.c - output a crypt(3)ed password to stdout.
|
---|
[1765] | 4 | *
|
---|
[2725] | 5 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
| 6 | *
|
---|
[1765] | 7 | * Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no>
|
---|
[2725] | 8 | * mkpasswd compatible options added by Bernhard Reutner-Fischer
|
---|
| 9 | *
|
---|
| 10 | * Licensed under GPLv2, see file LICENSE in this source tree.
|
---|
[1765] | 11 | */
|
---|
| 12 |
|
---|
[3232] | 13 | //usage:#define cryptpw_trivial_usage
|
---|
| 14 | //usage: "[OPTIONS] [PASSWORD] [SALT]"
|
---|
| 15 | /* We do support -s, we just don't mention it */
|
---|
| 16 | //usage:#define cryptpw_full_usage "\n\n"
|
---|
| 17 | //usage: "Crypt PASSWORD using crypt(3)\n"
|
---|
| 18 | //usage: IF_LONG_OPTS(
|
---|
| 19 | //usage: "\n -P,--password-fd=N Read password from fd N"
|
---|
| 20 | /* //usage: "\n -s,--stdin Use stdin; like -P0" */
|
---|
| 21 | //usage: "\n -m,--method=TYPE Encryption method"
|
---|
| 22 | //usage: "\n -S,--salt=SALT"
|
---|
| 23 | //usage: )
|
---|
| 24 | //usage: IF_NOT_LONG_OPTS(
|
---|
| 25 | //usage: "\n -P N Read password from fd N"
|
---|
| 26 | /* //usage: "\n -s Use stdin; like -P0" */
|
---|
| 27 | //usage: "\n -m TYPE Encryption method TYPE"
|
---|
| 28 | //usage: "\n -S SALT"
|
---|
| 29 | //usage: )
|
---|
| 30 |
|
---|
| 31 | /* mkpasswd is an alias to cryptpw */
|
---|
| 32 | //usage:#define mkpasswd_trivial_usage
|
---|
| 33 | //usage: "[OPTIONS] [PASSWORD] [SALT]"
|
---|
| 34 | /* We do support -s, we just don't mention it */
|
---|
| 35 | //usage:#define mkpasswd_full_usage "\n\n"
|
---|
| 36 | //usage: "Crypt PASSWORD using crypt(3)\n"
|
---|
| 37 | //usage: IF_LONG_OPTS(
|
---|
| 38 | //usage: "\n -P,--password-fd=N Read password from fd N"
|
---|
| 39 | /* //usage: "\n -s,--stdin Use stdin; like -P0" */
|
---|
| 40 | //usage: "\n -m,--method=TYPE Encryption method"
|
---|
| 41 | //usage: "\n -S,--salt=SALT"
|
---|
| 42 | //usage: )
|
---|
| 43 | //usage: IF_NOT_LONG_OPTS(
|
---|
| 44 | //usage: "\n -P N Read password from fd N"
|
---|
| 45 | /* //usage: "\n -s Use stdin; like -P0" */
|
---|
| 46 | //usage: "\n -m TYPE Encryption method TYPE"
|
---|
| 47 | //usage: "\n -S SALT"
|
---|
| 48 | //usage: )
|
---|
| 49 |
|
---|
[1765] | 50 | #include "libbb.h"
|
---|
| 51 |
|
---|
[2725] | 52 | /* Debian has 'mkpasswd' utility, manpage says:
|
---|
| 53 |
|
---|
| 54 | NAME
|
---|
| 55 | mkpasswd - Overfeatured front end to crypt(3)
|
---|
| 56 | SYNOPSIS
|
---|
| 57 | mkpasswd PASSWORD SALT
|
---|
| 58 | ...
|
---|
| 59 | OPTIONS
|
---|
| 60 | -S, --salt=STRING
|
---|
| 61 | Use the STRING as salt. It must not contain prefixes such as
|
---|
| 62 | $1$.
|
---|
| 63 | -R, --rounds=NUMBER
|
---|
| 64 | Use NUMBER rounds. This argument is ignored if the method
|
---|
| 65 | choosen does not support variable rounds. For the OpenBSD Blowfish
|
---|
| 66 | method this is the logarithm of the number of rounds.
|
---|
| 67 | -m, --method=TYPE
|
---|
| 68 | Compute the password using the TYPE method. If TYPE is 'help'
|
---|
| 69 | then the available methods are printed.
|
---|
| 70 | -P, --password-fd=NUM
|
---|
| 71 | Read the password from file descriptor NUM instead of using getpass(3).
|
---|
| 72 | If the file descriptor is not connected to a tty then
|
---|
| 73 | no other message than the hashed password is printed on stdout.
|
---|
| 74 | -s, --stdin
|
---|
| 75 | Like --password-fd=0.
|
---|
| 76 | ENVIRONMENT
|
---|
| 77 | $MKPASSWD_OPTIONS
|
---|
| 78 | A list of options which will be evaluated before the ones
|
---|
| 79 | specified on the command line.
|
---|
| 80 | BUGS
|
---|
| 81 | This programs suffers of a bad case of featuritis.
|
---|
| 82 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
---|
| 83 |
|
---|
| 84 | Very true...
|
---|
| 85 |
|
---|
| 86 | cryptpw was in bbox before this gem, so we retain it, and alias mkpasswd
|
---|
| 87 | to cryptpw. -a option (alias for -m) came from cryptpw.
|
---|
| 88 | */
|
---|
| 89 |
|
---|
| 90 | int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 91 | int cryptpw_main(int argc UNUSED_PARAM, char **argv)
|
---|
[1765] | 92 | {
|
---|
[3232] | 93 | char salt[MAX_PW_SALT_LEN];
|
---|
[2725] | 94 | char *salt_ptr;
|
---|
| 95 | const char *opt_m, *opt_S;
|
---|
| 96 | int fd;
|
---|
[1765] | 97 |
|
---|
[2725] | 98 | #if ENABLE_LONG_OPTS
|
---|
| 99 | static const char mkpasswd_longopts[] ALIGN1 =
|
---|
| 100 | "stdin\0" No_argument "s"
|
---|
| 101 | "password-fd\0" Required_argument "P"
|
---|
| 102 | "salt\0" Required_argument "S"
|
---|
| 103 | "method\0" Required_argument "m"
|
---|
| 104 | ;
|
---|
| 105 | applet_long_options = mkpasswd_longopts;
|
---|
| 106 | #endif
|
---|
| 107 | fd = STDIN_FILENO;
|
---|
[3232] | 108 | opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
|
---|
[2725] | 109 | opt_S = NULL;
|
---|
| 110 | /* at most two non-option arguments; -P NUM */
|
---|
| 111 | opt_complementary = "?2:P+";
|
---|
| 112 | getopt32(argv, "sP:S:m:a:", &fd, &opt_S, &opt_m, &opt_m);
|
---|
| 113 | argv += optind;
|
---|
| 114 |
|
---|
| 115 | /* have no idea how to handle -s... */
|
---|
| 116 |
|
---|
| 117 | if (argv[0] && !opt_S)
|
---|
| 118 | opt_S = argv[1];
|
---|
| 119 |
|
---|
[3232] | 120 | salt_ptr = crypt_make_pw_salt(salt, opt_m);
|
---|
[2725] | 121 | if (opt_S)
|
---|
[3232] | 122 | safe_strncpy(salt_ptr, opt_S, sizeof(salt) - (sizeof("$N$")-1));
|
---|
[1765] | 123 |
|
---|
[2725] | 124 | xmove_fd(fd, STDIN_FILENO);
|
---|
[1765] | 125 |
|
---|
[2725] | 126 | puts(pw_encrypt(
|
---|
| 127 | argv[0] ? argv[0] : (
|
---|
| 128 | /* Only mkpasswd, and only from tty, prompts.
|
---|
| 129 | * Otherwise it is a plain read. */
|
---|
| 130 | (isatty(STDIN_FILENO) && applet_name[0] == 'm')
|
---|
| 131 | ? bb_ask_stdin("Password: ")
|
---|
| 132 | : xmalloc_fgetline(stdin)
|
---|
| 133 | ),
|
---|
| 134 | salt, 1));
|
---|
| 135 |
|
---|
| 136 | return EXIT_SUCCESS;
|
---|
[1765] | 137 | }
|
---|