1 /* vi: set sw=4 ts=4: */
5 * Written for SLIND (from passwd.c) by Alexander Shishkin <virtuoso@slind.org>
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 //config:config CHPASSWD
9 //config: bool "chpasswd (19 kb)"
12 //config: Reads a file of user name and password pairs from standard input
13 //config: and uses this information to update a group of existing users.
15 //config:config FEATURE_DEFAULT_PASSWD_ALGO
16 //config: string "Default encryption method (passwd -a, cryptpw -m, chpasswd -c ALG)"
17 //config: default "des"
18 //config: depends on PASSWD || CRYPTPW || CHPASSWD
20 //config: Possible choices are "d[es]", "m[d5]", "s[ha256]" or "sha512".
22 //applet:IF_CHPASSWD(APPLET(chpasswd, BB_DIR_USR_SBIN, BB_SUID_DROP))
24 //kbuild:lib-$(CONFIG_CHPASSWD) += chpasswd.o
26 //usage:#define chpasswd_trivial_usage
27 //usage: "[-me] [-c ALG] [-R DIR]"
28 //usage:#define chpasswd_full_usage "\n\n"
29 //usage: "Read user:password from stdin and update /etc/passwd\n"
30 //usage: "\n -e Supplied passwords are in encrypted form"
31 //usage: "\n -m Encrypt using md5, not des"
32 //usage: "\n -c ALG "CRYPT_METHODS_HELP_STR
33 //usage: "\n -R DIR Directory to chroot into"
38 static const char chpasswd_longopts
[] ALIGN1
=
39 "encrypted\0" No_argument
"e"
40 "md5\0" No_argument
"m"
41 "crypt-method\0" Required_argument
"c"
42 "root\0" Required_argument
"R"
49 int chpasswd_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
50 int chpasswd_main(int argc UNUSED_PARAM
, char **argv
)
53 const char *algo
= CONFIG_FEATURE_DEFAULT_PASSWD_ALGO
;
54 const char *root
= NULL
;
58 bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root
);
60 opt
= getopt32long(argv
, "^" "emc:R:" "\0" "m--ec:e--mc:c--em",
69 while ((name
= xmalloc_fgetline(stdin
)) != NULL
) {
74 pass
= strchr(name
, ':');
76 bb_simple_error_msg_and_die("missing new password");
79 xuname2uid(name
); /* dies if there is no such user */
82 if (!(opt
& OPT_ENC
)) {
83 char salt
[MAX_PW_SALT_LEN
];
86 /* Force MD5 if the -m flag is set */
90 crypt_make_pw_salt(salt
, algo
);
91 free_me
= pass
= pw_encrypt(pass
, salt
, 0);
94 /* This is rather complex: if user is not found in /etc/shadow,
95 * we try to find & change his passwd in /etc/passwd */
96 #if ENABLE_FEATURE_SHADOWPASSWDS
97 rc
= update_passwd(bb_path_shadow_file
, name
, pass
, NULL
);
98 if (rc
> 0) /* password in /etc/shadow was updated */
101 /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */
103 rc
= update_passwd(bb_path_passwd_file
, name
, pass
, NULL
);
104 /* LOGMODE_BOTH logs to syslog also */
105 logmode
= LOGMODE_BOTH
;
107 bb_error_msg_and_die("an error occurred updating password for %s", name
);
109 bb_info_msg("password for '%s' changed", name
);
110 logmode
= LOGMODE_STDIO
;