source: branches/3.2/mindi-busybox/loginutils/cryptpw.c @ 3232

Last change on this file since 3232 was 3232, checked in by bruno, 5 years ago
  • Update mindi-busybox to 1.21.1
  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * cryptpw.c - output a crypt(3)ed password to stdout.
4 *
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6 *
7 * Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no>
8 * mkpasswd compatible options added by Bernhard Reutner-Fischer
9 *
10 * Licensed under GPLv2, see file LICENSE in this source tree.
11 */
12
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
50#include "libbb.h"
51
52/* Debian has 'mkpasswd' utility, manpage says:
53
54NAME
55    mkpasswd - Overfeatured front end to crypt(3)
56SYNOPSIS
57    mkpasswd PASSWORD SALT
58...
59OPTIONS
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.
76ENVIRONMENT
77    $MKPASSWD_OPTIONS
78    A list of options which will be evaluated before the ones
79    specified on the command line.
80BUGS
81    This programs suffers of a bad case of featuritis.
82    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
83
84Very true...
85
86cryptpw was in bbox before this gem, so we retain it, and alias mkpasswd
87to cryptpw. -a option (alias for -m) came from cryptpw.
88*/
89
90int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
91int cryptpw_main(int argc UNUSED_PARAM, char **argv)
92{
93    char salt[MAX_PW_SALT_LEN];
94    char *salt_ptr;
95    const char *opt_m, *opt_S;
96    int fd;
97
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;
108    opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
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
120    salt_ptr = crypt_make_pw_salt(salt, opt_m);
121    if (opt_S)
122        safe_strncpy(salt_ptr, opt_S, sizeof(salt) - (sizeof("$N$")-1));
123
124    xmove_fd(fd, STDIN_FILENO);
125
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;
137}
Note: See TracBrowser for help on using the repository browser.