Add chroot support to chpasswd

function                                             old     new   delta
.rodata                                           170689  170724     +35
packed_usage                                       32850   32876     +26
chpasswd_main                                        411     436     +25
chpasswd_longopts                                     34      41      +7
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/0 up/down: 93/0)               Total: 93 bytes

Signed-off-by: Jon Kolb <kolbyjack@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Jon Kolb 2018-07-31 21:30:24 -04:00 committed by Denys Vlasenko
parent 02cf149ed7
commit 4e30c67fa0

View File

@ -24,18 +24,20 @@
//kbuild:lib-$(CONFIG_CHPASSWD) += chpasswd.o //kbuild:lib-$(CONFIG_CHPASSWD) += chpasswd.o
//usage:#define chpasswd_trivial_usage //usage:#define chpasswd_trivial_usage
//usage: IF_LONG_OPTS("[--md5|--encrypted|--crypt-method]") IF_NOT_LONG_OPTS("[-m|-e|-c]") //usage: IF_LONG_OPTS("[--md5|--encrypted|--crypt-method|--root]") IF_NOT_LONG_OPTS("[-m|-e|-c|-R]")
//usage:#define chpasswd_full_usage "\n\n" //usage:#define chpasswd_full_usage "\n\n"
//usage: "Read user:password from stdin and update /etc/passwd\n" //usage: "Read user:password from stdin and update /etc/passwd\n"
//usage: IF_LONG_OPTS( //usage: IF_LONG_OPTS(
//usage: "\n -e,--encrypted Supplied passwords are in encrypted form" //usage: "\n -e,--encrypted Supplied passwords are in encrypted form"
//usage: "\n -m,--md5 Eencrypt using md5, not des" //usage: "\n -m,--md5 Encrypt using md5, not des"
//usage: "\n -c,--crypt-method ALG "CRYPT_METHODS_HELP_STR //usage: "\n -c,--crypt-method ALG "CRYPT_METHODS_HELP_STR
//usage: "\n -R,--root DIR Directory to chroot into"
//usage: ) //usage: )
//usage: IF_NOT_LONG_OPTS( //usage: IF_NOT_LONG_OPTS(
//usage: "\n -e Supplied passwords are in encrypted form" //usage: "\n -e Supplied passwords are in encrypted form"
//usage: "\n -m Eencrypt using md5, not des" //usage: "\n -m Encrypt using md5, not des"
//usage: "\n -c ALG "CRYPT_METHODS_HELP_STR //usage: "\n -c ALG "CRYPT_METHODS_HELP_STR
//usage: "\n -R DIR Directory to chroot into"
//usage: ) //usage: )
#include "libbb.h" #include "libbb.h"
@ -45,6 +47,7 @@ static const char chpasswd_longopts[] ALIGN1 =
"encrypted\0" No_argument "e" "encrypted\0" No_argument "e"
"md5\0" No_argument "m" "md5\0" No_argument "m"
"crypt-method\0" Required_argument "c" "crypt-method\0" Required_argument "c"
"root\0" Required_argument "R"
; ;
#endif #endif
@ -56,16 +59,21 @@ int chpasswd_main(int argc UNUSED_PARAM, char **argv)
{ {
char *name; char *name;
const char *algo = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO; const char *algo = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
const char *root = NULL;
int opt; int opt;
if (getuid() != 0) if (getuid() != 0)
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
opt = getopt32long(argv, "^" "emc:" "\0" "m--ec:e--mc:c--em", opt = getopt32long(argv, "^" "emc:R:" "\0" "m--ec:e--mc:c--em",
chpasswd_longopts, chpasswd_longopts,
&algo &algo, &root
); );
if (root) {
xchroot(root);
}
while ((name = xmalloc_fgetline(stdin)) != NULL) { while ((name = xmalloc_fgetline(stdin)) != NULL) {
char *free_me; char *free_me;
char *pass; char *pass;