MFC: pw(8): fix combination of modes -N and -w random

The command "pw usermod nobody -Nw random" (or useradd)
generates random password and prints it in encrypted form
but skips choosen random string that makes not much sense
and contradicts the manual page pw.8

Fix it by showing random password in plain text with -N and
without it equally. Add yet another example of how to generate
pw-style random password.

(cherry picked from commit 1cbe5012cf)
This commit is contained in:
Eugene Grosbein 2022-11-28 21:22:39 +07:00
parent 6974ae0530
commit 293ff98462
2 changed files with 12 additions and 7 deletions

View file

@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd April 3, 2022
.Dd November 28, 2022
.Dt PW 8
.Os
.Sh NAME
@ -988,6 +988,12 @@ in addition to the other groups jsmith is already a member of.
.Bd -literal -offset indent
pw groupmod wheel -m jsmith
.Ed
.Pp
Generate random password and show it in both plain text and
encrypted form not modifying any database.
.Bd -literal -offset indent
pw usermod nobody -Nw random
.Ed
.Sh EXIT STATUS
The
.Nm

View file

@ -80,8 +80,7 @@ static uid_t pw_gidpolicy(struct userconf *cnf, char *grname, char *nam,
static char *pw_homepolicy(struct userconf * cnf, char *homedir,
const char *user);
static char *pw_shellpolicy(struct userconf * cnf);
static char *pw_password(struct userconf * cnf, char const * user,
bool dryrun);
static char *pw_password(struct userconf * cnf, char const * user);
static char *shell_path(char const * path, char *shells[], char *sh);
static void rmat(uid_t uid);
static void rmopie(char const * name);
@ -511,7 +510,7 @@ pw_pwcrypt(char *password)
}
static char *
pw_password(struct userconf * cnf, char const * user, bool dryrun)
pw_password(struct userconf * cnf, char const * user)
{
int i, l;
char pwbuf[32];
@ -528,7 +527,7 @@ pw_password(struct userconf * cnf, char const * user, bool dryrun)
/*
* We give this information back to the user
*/
if (conf.fd == -1 && !dryrun) {
if (conf.fd == -1) {
if (isatty(STDOUT_FILENO))
printf("Password for '%s' is: ", user);
printf("%s\n", pwbuf);
@ -1410,7 +1409,7 @@ pw_user_add(int argc, char **argv, char *arg1)
if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
warn("setting crypt(3) format");
login_close(lc);
pwd->pw_passwd = pw_password(cmdcnf, pwd->pw_name, dryrun);
pwd->pw_passwd = pw_password(cmdcnf, pwd->pw_name);
if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
warnx("WARNING: new account `%s' has a uid of 0 "
"(superuser access!)", pwd->pw_name);
@ -1759,7 +1758,7 @@ pw_user_mod(int argc, char **argv, char *arg1)
login_close(lc);
cnf->default_password = passwd_val(passwd,
cnf->default_password);
pwd->pw_passwd = pw_password(cnf, pwd->pw_name, dryrun);
pwd->pw_passwd = pw_password(cnf, pwd->pw_name);
edited = true;
}