Using pw adduser to set password in a script

Jez Hancock jez.hancock at
Mon Jun 30 23:12:50 PDT 2003

I'm attempting to use pw adduser to add a new user to the system and
would like confirmation that the following is the correct way to set
the user's password at the same time:

echo "password" | \
pw adduser -q -h - -u user -g group -s shell -d /home/user -c comment

Can anyone also tell me the security implications of doing this, given
that the command is executed from a within a script (actually php but
this is more-or-less irrelevant)?

Is there a better way to do this using file descriptors as described in
the man page for pw?:

     -h fd	   This option provides a special interface by which interac-
		   tive scripts can set an account password using pw.  Because
		   the command line and environment are fundamentally insecure
		   mechanisms by which programs can accept information, pw
		   will only allow setting of account and group passwords via
		   a file descriptor (usually a pipe between an interactive
		   script and the program).  sh, bash, ksh and perl all pos-
		   sess mechanisms by which this can be done.  Alternatively,
		   pw will prompt for the user's password if -h 0 is given,
		   nominating stdin as the file descriptor on which to read
		   the password.  Note that this password will be read only
		   once and is intended for use by a script rather than for
		   interactive use.  If you wish to have new password confir-
		   mation along the lines of passwd(1), this must be imple-
		   mented as part of an interactive script that calls pw.

		   If a value of `-' is given as the argument fd, then the
		   password will be set to `*', rendering the account inacces-
		   sible via password-based login.

Many thanks in advance,

More information about the freebsd-questions mailing list