>This discussion of password encryption leads me to ask a related (and possibly
>redundant) question. When using crypt to generate an encrypted password to be
>compared with the entry in passwd, what is the 2 char string which is needed to
>salt the key?
If you're generating a new password, the salt is any two characters chosen
from the set [a-zA-z0-9./].
If you're validating a password, the salt should be the first two characters
from the encrypted password you want to compare against. You then compare
the returned value against the entire already-encrypted password.
Actually, I think for maximum portability[*] you should pass the entire
encrypted password as the salt when validating. On Sun systems using
password.adjunct (Sun's shadow password mechanism), the password field of a
passwd entry is "##username". When crypt(3) sees that the first two
characters are "##" it performs the validation for you (by calling
pwdauth(3), which uses the salt as a key in the passwd.adjunct file) and
returns the salt argument itself if the comparison succeeded (a similar
thing is done with passwords of the form "#$groupname" and the
group.adjunct file). If the salt string doesn't begin with "#" then
everything after the first two characters will be ignored. In general, the
following should always work to validate a password;
int passwd_ok = 0; char *entered_passwd, *encrypted_passwd;
entered_passwd = getpass("Password:");
encrypted_passwd = crypt(entered_passwd, passwd.pw_passwd);
if (encrypted_passwd != NULL)
passwd_ok = (strcmp(passwd.pw_passwd, encrypted_passwd) == 0);
[*] I supposed it's possible that this technique isn't really upward
compatible on all systems, so you might need to use conditional compilation
for *really* maximum portability.
--
Barry Margolin
System Manager, Thinking Machines Corp.