chpasswd: add get_salt for generating salt value

The function that generates the salt value is extracted separately, and it is more convenient to modify it later.
This commit is contained in:
juyin 2022-03-31 16:45:19 +08:00 committed by Serge Hallyn
parent a43d0b95c4
commit a026154c6f

View File

@ -392,12 +392,49 @@ static void close_files (void)
pw_locked = false; pw_locked = false;
} }
static const char *get_salt(void)
{
if ( !eflg
&& ( (NULL == crypt_method)
|| (0 != strcmp (crypt_method, "NONE")))) {
void *arg = NULL;
if (md5flg) {
crypt_method = "MD5";
}
#if defined(USE_SHA_CRYPT) || defined(USE_BCRYPT) || defined(USE_YESCRYPT)
if (sflg) {
#if defined(USE_SHA_CRYPT)
if ( (0 == strcmp (crypt_method, "SHA256"))
|| (0 == strcmp (crypt_method, "SHA512"))) {
arg = &sha_rounds;
}
#endif /* USE_SHA_CRYPT */
#if defined(USE_BCRYPT)
if (0 == strcmp (crypt_method, "BCRYPT")) {
arg = &bcrypt_rounds;
}
#endif /* USE_BCRYPT */
#if defined(USE_YESCRYPT)
if (0 == strcmp (crypt_method, "YESCRYPT")) {
arg = &yescrypt_cost;
}
#endif /* USE_YESCRYPT */
}
#endif
return crypt_make_salt (crypt_method, arg);
}
return NULL;
}
int main (int argc, char **argv) int main (int argc, char **argv)
{ {
char buf[BUFSIZ]; char buf[BUFSIZ];
char *name; char *name;
char *newpwd; char *newpwd;
char *cp; char *cp;
const char *salt;
#ifdef USE_PAM #ifdef USE_PAM
bool use_pam = true; bool use_pam = true;
@ -508,35 +545,8 @@ int main (int argc, char **argv)
const struct passwd *pw; const struct passwd *pw;
struct passwd newpw; struct passwd newpw;
if ( !eflg salt = get_salt();
&& ( (NULL == crypt_method) if (salt) {
|| (0 != strcmp (crypt_method, "NONE")))) {
void *arg = NULL;
const char *salt;
if (md5flg) {
crypt_method = "MD5";
}
#if defined(USE_SHA_CRYPT) || defined(USE_BCRYPT) || defined(USE_YESCRYPT)
if (sflg) {
#if defined(USE_SHA_CRYPT)
if ( (0 == strcmp (crypt_method, "SHA256"))
|| (0 == strcmp (crypt_method, "SHA512"))) {
arg = &sha_rounds;
}
#endif /* USE_SHA_CRYPT */
#if defined(USE_BCRYPT)
if (0 == strcmp (crypt_method, "BCRYPT")) {
arg = &bcrypt_rounds;
}
#endif /* USE_BCRYPT */
#if defined(USE_YESCRYPT)
if (0 == strcmp (crypt_method, "YESCRYPT")) {
arg = &yescrypt_cost;
}
#endif /* USE_YESCRYPT */
}
#endif
salt = crypt_make_salt (crypt_method, arg);
cp = pw_encrypt (newpwd, salt); cp = pw_encrypt (newpwd, salt);
if (NULL == cp) { if (NULL == cp) {
fprintf (stderr, fprintf (stderr,