lineedit: print prompt and editing operations to stderr
[busybox-git.git] / loginutils / cryptpw.c
blob1c338540fc4b2e0f4870353624666698b30ad4e4
1 /* vi: set sw=4 ts=4: */
2 /*
3 * cryptpw.c - output a crypt(3)ed password to stdout.
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 * Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no>
8 * mkpasswd compatible options added by Bernhard Reutner-Fischer
10 * Licensed under GPLv2, see file LICENSE in this source tree.
12 //config:config CRYPTPW
13 //config: bool "cryptpw (15 kb)"
14 //config: default y
15 //config: help
16 //config: Encrypts the given password with the crypt(3) libc function
17 //config: using the given salt.
18 //config:
19 //config:config MKPASSWD
20 //config: bool "mkpasswd (16 kb)"
21 //config: default y
22 //config: help
23 //config: Encrypts the given password with the crypt(3) libc function
24 //config: using the given salt. Debian has this utility under mkpasswd
25 //config: name. Busybox provides mkpasswd as an alias for cryptpw.
27 //applet:IF_CRYPTPW( APPLET_NOEXEC(cryptpw, cryptpw, BB_DIR_USR_BIN, BB_SUID_DROP, cryptpw))
28 // APPLET_NOEXEC:name main location suid_type help
29 //applet:IF_MKPASSWD(APPLET_NOEXEC(mkpasswd, cryptpw, BB_DIR_USR_BIN, BB_SUID_DROP, cryptpw))
31 //kbuild:lib-$(CONFIG_CRYPTPW) += cryptpw.o
32 //kbuild:lib-$(CONFIG_MKPASSWD) += cryptpw.o
34 //usage:#define cryptpw_trivial_usage
35 //usage: "[-P FD] [-m TYPE] [-S SALT] [PASSWORD] [SALT]"
36 /* We do support -s, we just don't mention it */
37 //usage:#define cryptpw_full_usage "\n\n"
38 //usage: "Print crypt(3) hashed PASSWORD\n"
39 //usage: "\n -P N Read password from fd N"
40 /* //usage: "\n -s Use stdin; like -P0" */
41 //usage: "\n -m TYPE "CRYPT_METHODS_HELP_STR
42 //usage: "\n -S SALT"
44 #include "libbb.h"
46 /* Debian has 'mkpasswd' utility, manpage says:
48 NAME
49 mkpasswd - Overfeatured front end to crypt(3)
50 SYNOPSIS
51 mkpasswd PASSWORD SALT
52 ...
53 OPTIONS
54 -S, --salt=STRING
55 Use the STRING as salt. It must not contain prefixes such as
56 $1$.
57 -R, --rounds=NUMBER
58 Use NUMBER rounds. This argument is ignored if the method
59 chosen does not support variable rounds. For the OpenBSD Blowfish
60 method this is the logarithm of the number of rounds.
61 -m, --method=TYPE
62 Compute the password using the TYPE method. If TYPE is 'help'
63 then the available methods are printed.
64 -P, --password-fd=NUM
65 Read the password from file descriptor NUM instead of using getpass(3).
66 If the file descriptor is not connected to a tty then
67 no other message than the hashed password is printed on stdout.
68 -s, --stdin
69 Like --password-fd=0.
70 ENVIRONMENT
71 $MKPASSWD_OPTIONS
72 A list of options which will be evaluated before the ones
73 specified on the command line.
74 BUGS
75 This program suffers of a bad case of featuritis.
76 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
78 Very true...
80 cryptpw was in bbox before this gem, so we retain it, and alias mkpasswd
81 to cryptpw. -a option (alias for -m) came from cryptpw.
84 int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
85 int cryptpw_main(int argc UNUSED_PARAM, char **argv)
87 /* Supports: cryptpw -m sha256 PASS 'rounds=999999999$SALT' */
88 char salt[MAX_PW_SALT_LEN + sizeof("rounds=999999999$")];
89 char *salt_ptr;
90 char *password;
91 const char *opt_m, *opt_S;
92 int fd;
94 #if ENABLE_LONG_OPTS
95 static const char mkpasswd_longopts[] ALIGN1 =
96 "stdin\0" No_argument "s"
97 "password-fd\0" Required_argument "P"
98 "salt\0" Required_argument "S"
99 "method\0" Required_argument "m"
101 #endif
102 fd = STDIN_FILENO;
103 opt_m = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
104 opt_S = NULL;
105 /* at most two non-option arguments; -P NUM */
106 getopt32long(argv, "^" "sP:+S:m:a:" "\0" "?2",
107 mkpasswd_longopts,
108 &fd, &opt_S, &opt_m, &opt_m
110 argv += optind;
112 /* have no idea how to handle -s... */
114 if (argv[0] && !opt_S)
115 opt_S = argv[1];
117 salt_ptr = crypt_make_pw_salt(salt, opt_m);
118 if (opt_S)
119 /* put user's data after the "$N$" prefix */
120 safe_strncpy(salt_ptr, opt_S, sizeof(salt) - (sizeof("$N$")-1));
122 xmove_fd(fd, STDIN_FILENO);
124 password = argv[0];
125 if (!password) {
126 /* Only mkpasswd, and only from tty, prompts.
127 * Otherwise it is a plain read. */
128 password = (ENABLE_MKPASSWD && applet_name[0] == 'm' && isatty(STDIN_FILENO))
129 ? bb_ask_noecho_stdin("Password: ")
130 : xmalloc_fgetline(stdin)
132 /* may still be NULL on EOF/error */
135 if (password)
136 puts(pw_encrypt(password, salt, 1));
138 return EXIT_SUCCESS;